Add TextWriter, IOException, per-VM module data and pre-load imports; Add console module with Console class
This commit is contained in:
@@ -0,0 +1,668 @@
|
||||
/**
|
||||
* @PROJECT AerScript Interpreter
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE modules/console/console.c
|
||||
* @DESCRIPTION Console class module for AerScript Interpreter
|
||||
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef __WINNT__
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
#include "console.h"
|
||||
|
||||
#ifndef STDERR_FILENO
|
||||
#define STDERR_FILENO 2
|
||||
#endif
|
||||
|
||||
/* .NET ConsoleColor values; published as COLOR_* constants. */
|
||||
enum console_color {
|
||||
COLOR_BLACK = 0, COLOR_BLUE = 9, COLOR_GREEN = 10, COLOR_CYAN = 11,
|
||||
COLOR_RED = 12, COLOR_MAGENTA = 13, COLOR_YELLOW = 14, COLOR_WHITE = 15,
|
||||
/* .NET spelling: only the underscored DARK_* names are published (D2). */
|
||||
COLOR_DARK_BLUE = 1, COLOR_DARK_GREEN = 2, COLOR_DARK_CYAN = 3,
|
||||
COLOR_DARK_RED = 4, COLOR_DARK_MAGENTA = 5, COLOR_DARK_YELLOW = 6,
|
||||
COLOR_GRAY = 7, COLOR_DARK_GRAY = 8
|
||||
};
|
||||
|
||||
/* Per-VM console state (C13). Colors are remembered even when output is
|
||||
* redirected: the terminal escape is emitted only for a real console. State
|
||||
* lives in the module slot owned by the VM, never in a process-global, so two
|
||||
* VMs cannot observe each other's colors. */
|
||||
typedef struct console_state {
|
||||
int nFgColor;
|
||||
int nBgColor;
|
||||
} console_state;
|
||||
|
||||
static void ConsoleStateRelease(void *pData) {
|
||||
free(pData);
|
||||
}
|
||||
static console_state *ConsoleState(ph7_vm *pVm) {
|
||||
console_state *pState = (console_state *)PH7_VmModuleData(pVm, "console");
|
||||
if(pState == 0) {
|
||||
pState = (console_state *)malloc(sizeof(*pState));
|
||||
if(pState) {
|
||||
pState->nFgColor = -1;
|
||||
pState->nBgColor = -1;
|
||||
if(PH7_VmSetModuleData(pVm, "console", pState, ConsoleStateRelease) != SXRET_OK) {
|
||||
free(pState);
|
||||
pState = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return pState;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Terminal helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int ConsoleIsRedirected(int nFd) {
|
||||
#ifdef __WINNT__
|
||||
DWORD dwMode = 0;
|
||||
HANDLE h;
|
||||
switch(nFd) {
|
||||
case 0: h = GetStdHandle(STD_INPUT_HANDLE); break;
|
||||
case 2: h = GetStdHandle(STD_ERROR_HANDLE); break;
|
||||
default: h = GetStdHandle(STD_OUTPUT_HANDLE); break;
|
||||
}
|
||||
if(h == INVALID_HANDLE_VALUE || h == 0) {
|
||||
return 1;
|
||||
}
|
||||
if(!GetConsoleMode(h, &dwMode)) {
|
||||
return 1; /* A pipe or a redirected file. */
|
||||
}
|
||||
/* No side effects here: virtual terminal processing is enabled once at
|
||||
* module initialization (initializeModule), never from a query. */
|
||||
return 0;
|
||||
#else
|
||||
return isatty(nFd) ? 0 : 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Positive decimal dimension from the environment, 0 when absent/invalid. */
|
||||
static int ConsoleEnvDim(const char *zName) {
|
||||
const char *z = getenv(zName);
|
||||
char *zEnd;
|
||||
long n;
|
||||
if(z == 0 || *z == 0) {
|
||||
return 0;
|
||||
}
|
||||
n = strtol(z, &zEnd, 10);
|
||||
if(zEnd == z || *zEnd != 0 || n < 1 || n > 0x7FFF) {
|
||||
return 0;
|
||||
}
|
||||
return (int)n;
|
||||
}
|
||||
static void ConsoleSize(int *pWidth, int *pHeight) {
|
||||
*pWidth = 0;
|
||||
*pHeight = 0;
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
CONSOLE_SCREEN_BUFFER_INFO sInfo;
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if(h != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(h, &sInfo) &&
|
||||
sInfo.srWindow.Right >= sInfo.srWindow.Left && sInfo.srWindow.Bottom >= sInfo.srWindow.Top) {
|
||||
*pWidth = (int)(sInfo.srWindow.Right - sInfo.srWindow.Left + 1);
|
||||
*pHeight = (int)(sInfo.srWindow.Bottom - sInfo.srWindow.Top + 1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Under a harness stdout/stderr are pipes: try every standard descriptor
|
||||
* before falling back to the environment and finally the canonical 80x24.
|
||||
* Never report zero. */
|
||||
{
|
||||
int aFd[] = { 1, 2, 0 };
|
||||
unsigned int i;
|
||||
for(i = 0 ; i < SX_ARRAYSIZE(aFd) ; ++i) {
|
||||
#ifdef __UNIXES__
|
||||
struct winsize sWin;
|
||||
if(!ConsoleIsRedirected(aFd[i]) && ioctl(aFd[i], TIOCGWINSZ, &sWin) == 0 &&
|
||||
sWin.ws_col > 0 && sWin.ws_row > 0) {
|
||||
*pWidth = (int)sWin.ws_col;
|
||||
*pHeight = (int)sWin.ws_row;
|
||||
break;
|
||||
}
|
||||
#else
|
||||
(void)aFd[i];
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
if(*pWidth < 1 || *pHeight < 1) {
|
||||
int nCols = ConsoleEnvDim("COLUMNS");
|
||||
int nRows = ConsoleEnvDim("LINES");
|
||||
*pWidth = nCols > 0 ? nCols : 80;
|
||||
*pHeight = nRows > 0 ? nRows : 24;
|
||||
}
|
||||
}
|
||||
|
||||
static void ConsoleEmit(ph7_context *pCtx, const char *zData) {
|
||||
if(!ConsoleIsRedirected(1)) {
|
||||
(void)ph7_context_output(pCtx, zData, -1);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Writer backends */
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Standard output reuses the VM output consumer, so output buffers and
|
||||
* host consumers observe these bytes exactly as they observe print(). */
|
||||
static int ConsoleOutWrite(void *pUserData, const void *pData, unsigned int nByte) {
|
||||
ph7_vm *pVm = (ph7_vm *)pUserData;
|
||||
SyString sData;
|
||||
if(nByte == 0) {
|
||||
return SXRET_OK;
|
||||
}
|
||||
SyStringInitFromBuf(&sData, (const char *)pData, (int)nByte);
|
||||
return PH7_VmOutputConsume(pVm, &sData);
|
||||
}
|
||||
static int ConsoleOutFlush(void *pUserData) {
|
||||
SXUNUSED(pUserData);
|
||||
return SXRET_OK;
|
||||
}
|
||||
static void ConsoleOutRelease(void *pUserData) {
|
||||
SXUNUSED(pUserData);
|
||||
}
|
||||
/* Standard error bypasses ob_* and the VM consumer entirely. */
|
||||
static int ConsoleErrWrite(void *pUserData, const void *pData, unsigned int nByte) {
|
||||
const char *zData = (const char *)pData;
|
||||
unsigned int nDone = 0;
|
||||
SXUNUSED(pUserData);
|
||||
while(nDone < nByte) {
|
||||
#ifdef __WINNT__
|
||||
DWORD nWritten = 0;
|
||||
if(!WriteFile(GetStdHandle(STD_ERROR_HANDLE), zData + nDone, nByte - nDone, &nWritten, 0) || nWritten == 0) {
|
||||
return SXERR_ABORT;
|
||||
}
|
||||
nDone += (unsigned int)nWritten;
|
||||
#else
|
||||
ssize_t nWritten = write(STDERR_FILENO, zData + nDone, nByte - nDone);
|
||||
if(nWritten < 0) {
|
||||
return SXERR_ABORT;
|
||||
}
|
||||
nDone += (unsigned int)nWritten;
|
||||
#endif
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
static int ConsoleErrFlush(void *pUserData) {
|
||||
SXUNUSED(pUserData);
|
||||
return SXRET_OK;
|
||||
}
|
||||
static void ConsoleErrRelease(void *pUserData) {
|
||||
SXUNUSED(pUserData);
|
||||
}
|
||||
static const ph7_text_writer_methods sOutMethods = {
|
||||
ConsoleOutWrite, ConsoleOutFlush, ConsoleOutRelease
|
||||
};
|
||||
static const ph7_text_writer_methods sErrMethods = {
|
||||
ConsoleErrWrite, ConsoleErrFlush, ConsoleErrRelease
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Argument conversion helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int ConsoleCtxWrite(void *pUserData, const void *pData, unsigned int nByte) {
|
||||
return ph7_context_output((ph7_context *)pUserData, (const char *)pData, (int)nByte);
|
||||
}
|
||||
|
||||
static int ConsolePrintImpl(ph7_context *pCtx, int nArg, ph7_value **apArg, int bLine) {
|
||||
/* One C implementation for every path: zero arguments emit only the EOL,
|
||||
* one argument is literal, several arguments use the first as the PH7
|
||||
* format. Validation lives in ph7_text_writer_emit() in the core. */
|
||||
return ph7_text_writer_emit(pCtx, ConsoleCtxWrite, pCtx, nArg, apArg, bLine);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Console method callbacks */
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Keep the legacy print() contract: the call is an expression whose value is
|
||||
* NULL, which matters for default-parameter sentinels. */
|
||||
static int ConsoleNullResult(ph7_context *pCtx, int rc) {
|
||||
return rc == SXRET_OK ? ph7_result_null(pCtx) : rc;
|
||||
}
|
||||
static int Console_Print(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
return ConsoleNullResult(pCtx, ConsolePrintImpl(pCtx, nArg, apArg, 0));
|
||||
}
|
||||
static int Console_PrintLine(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
return ConsoleNullResult(pCtx, ConsolePrintImpl(pCtx, nArg, apArg, 1));
|
||||
}
|
||||
static int Console_GetOut(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
||||
sxi32 rc;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
if(pResult == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
rc = ph7_text_writer_new(pCtx, &sOutMethods, pCtx->pVm, pResult);
|
||||
return rc == PH7_OK ? ph7_result_value(pCtx, pResult) : rc;
|
||||
}
|
||||
static int Console_GetError(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
||||
sxi32 rc;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
if(pResult == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
rc = ph7_text_writer_new(pCtx, &sErrMethods, 0, pResult);
|
||||
return rc == PH7_OK ? ph7_result_value(pCtx, pResult) : rc;
|
||||
}
|
||||
static int Console_Read(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
int cp;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
/* Read a whole UTF-8 sequence from the one stdin handle shared with
|
||||
* fgets()/fread(): a Unicode code point, 0xFFFD for invalid input, -1
|
||||
* at EOF. Unused look-ahead bytes are returned to the stream. */
|
||||
cp = PH7_StdinReadCodepoint(pCtx->pVm);
|
||||
return ph7_result_int(pCtx, cp);
|
||||
}
|
||||
static int Console_ReadLine(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
char *zLine;
|
||||
int nLen, rc;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
zLine = PH7_StdinReadLine(pCtx->pVm, &nLen);
|
||||
if(zLine == 0) {
|
||||
return ph7_result_null(pCtx);
|
||||
}
|
||||
/* Drop exactly one line feed and a carriage return only when it directly
|
||||
* precedes it: trailing bare CRs are data and must survive. */
|
||||
if(nLen > 0 && zLine[nLen - 1] == '\n') {
|
||||
--nLen;
|
||||
if(nLen > 0 && zLine[nLen - 1] == '\r') {
|
||||
--nLen;
|
||||
}
|
||||
}
|
||||
rc = ph7_result_string(pCtx, zLine, nLen);
|
||||
SyMemBackendFree(&pCtx->pVm->sAllocator, zLine);
|
||||
return rc;
|
||||
}
|
||||
static int Console_IsInputRedirected(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_bool(pCtx, ConsoleIsRedirected(0));
|
||||
}
|
||||
static int Console_IsOutputRedirected(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_bool(pCtx, ConsoleIsRedirected(1));
|
||||
}
|
||||
static int Console_IsErrorRedirected(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_bool(pCtx, ConsoleIsRedirected(2));
|
||||
}
|
||||
static int Console_GetCommandLineArgs(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_value *pArray = ph7_context_new_array(pCtx);
|
||||
ph7_value *pEntry;
|
||||
SyBlob *pBlob;
|
||||
SyString *pFile;
|
||||
const char *zData, *zEntry;
|
||||
sxu32 nByte, nOfft = 0, nLen;
|
||||
sxi32 rc;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
if(pArray == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
/* Element 0 is the path of the MAIN script. pVm->aFiles[0] is installed by
|
||||
* PH7_VmPushFilePath() with bMain=TRUE and is already canonicalized with
|
||||
* SyRealPath(), so this is an ABSOLUTE path -- never the literal argv[0],
|
||||
* and never the path of an include file. For scripts compiled from memory
|
||||
* (stdin/eval) the placeholder is "[MEMORY]" and is published as "". */
|
||||
if(SySetUsed(&pCtx->pVm->aFiles) > 0) {
|
||||
pFile = *(SyString **)SySetAt(&pCtx->pVm->aFiles, 0);
|
||||
} else {
|
||||
pFile = 0;
|
||||
}
|
||||
pEntry = ph7_context_new_scalar(pCtx);
|
||||
if(pEntry == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
if(pFile && !(pFile->nByte == sizeof("[MEMORY]") - 1 &&
|
||||
SyMemcmp(pFile->zString, "[MEMORY]", sizeof("[MEMORY]") - 1) == 0)) {
|
||||
rc = ph7_value_string(pEntry, pFile->zString, (int)pFile->nByte);
|
||||
} else {
|
||||
rc = ph7_value_string(pEntry, "", 0);
|
||||
}
|
||||
if(rc != PH7_OK || ph7_array_add_elem(pArray, 0, pEntry) != PH7_OK) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
/* Remaining entries are the script arguments: pVm->sArgvEntries is the
|
||||
* very same list that backs $args, which is left untouched. */
|
||||
pBlob = &pCtx->pVm->sArgvEntries;
|
||||
nByte = SyBlobLength(pBlob);
|
||||
zData = (const char *)SyBlobData(pBlob);
|
||||
while(nOfft < nByte) {
|
||||
zEntry = zData + nOfft;
|
||||
nLen = SyStrlen(zEntry);
|
||||
pEntry = ph7_context_new_scalar(pCtx);
|
||||
if(pEntry == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
if(ph7_value_string(pEntry, zEntry, (int)nLen) != PH7_OK ||
|
||||
ph7_array_add_elem(pArray, 0, pEntry) != PH7_OK) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
nOfft += nLen + 1;
|
||||
}
|
||||
/* All entries are strings: stamp the primitive element type directly,
|
||||
* never PH7_VmInternType() (that makes a named class type). */
|
||||
pArray->nType = MEMOBJ_STRING | MEMOBJ_HASHMAP;
|
||||
return ph7_result_value(pCtx, pArray);
|
||||
}
|
||||
static int Console_Clear(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#ifdef __WINNT__
|
||||
if(!ConsoleIsRedirected(1)) {
|
||||
CONSOLE_SCREEN_BUFFER_INFO sInfo;
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
DWORD nWritten;
|
||||
COORD sOrigin = { 0, 0 };
|
||||
if(GetConsoleScreenBufferInfo(h, &sInfo)) {
|
||||
FillConsoleOutputCharacter(h, ' ', sInfo.dwSize.X * sInfo.dwSize.Y, sOrigin, &nWritten);
|
||||
FillConsoleOutputAttribute(h, sInfo.wAttributes, sInfo.dwSize.X * sInfo.dwSize.Y, sOrigin, &nWritten);
|
||||
SetConsoleCursorPosition(h, sOrigin);
|
||||
}
|
||||
}
|
||||
#else
|
||||
ConsoleEmit(pCtx, "\033[2J\033[H");
|
||||
#endif
|
||||
return PH7_OK;
|
||||
}
|
||||
static int Console_Beep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#ifdef __WINNT__
|
||||
MessageBeep(MB_OK);
|
||||
#else
|
||||
ConsoleEmit(pCtx, "\a");
|
||||
#endif
|
||||
return PH7_OK;
|
||||
}
|
||||
static int Console_SetTitle(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
const char *zTitle;
|
||||
int nLen;
|
||||
if(nArg < 1) {
|
||||
return PH7_OK;
|
||||
}
|
||||
zTitle = ph7_value_to_string(apArg[0], &nLen);
|
||||
if(pCtx->pVm->iPendingTransfer) {
|
||||
return pCtx->pVm->iPendingTransfer;
|
||||
}
|
||||
/* Reject control characters: an embedded BEL/OSC could forge terminal
|
||||
* escapes. No title or state is modified before this check. */
|
||||
{
|
||||
int i;
|
||||
for(i = 0 ; i < nLen ; ++i) {
|
||||
unsigned char c = (unsigned char)zTitle[i];
|
||||
if(c < 0x20 || c == 0x7F) {
|
||||
return PH7_VmThrowIoException(pCtx, 1, "Title contains a control character");
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef __WINNT__
|
||||
if(!ConsoleIsRedirected(1)) {
|
||||
SetConsoleTitleA(zTitle);
|
||||
}
|
||||
#else
|
||||
if(!ConsoleIsRedirected(1)) {
|
||||
char *zBuf;
|
||||
zBuf = (char *)ph7_context_alloc_chunk(pCtx, (unsigned int)nLen + 6, FALSE, TRUE);
|
||||
if(zBuf) {
|
||||
memcpy(zBuf, "\033]0;", 4);
|
||||
memcpy(zBuf + 4, zTitle, (size_t)nLen);
|
||||
memcpy(zBuf + 4 + nLen, "\007", 1);
|
||||
(void)ph7_context_output(pCtx, zBuf, nLen + 5);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return PH7_OK;
|
||||
}
|
||||
static int Console_GetWidth(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
int nWidth, nHeight;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
ConsoleSize(&nWidth, &nHeight);
|
||||
return ph7_result_int(pCtx, nWidth);
|
||||
}
|
||||
static int Console_GetHeight(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
int nWidth, nHeight;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
ConsoleSize(&nWidth, &nHeight);
|
||||
return ph7_result_int(pCtx, nHeight);
|
||||
}
|
||||
static int Console_SetForegroundColor(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
int nColor;
|
||||
char zCode[16];
|
||||
console_state *pState;
|
||||
if(nArg < 1) {
|
||||
return PH7_OK;
|
||||
}
|
||||
nColor = ph7_value_to_int(apArg[0]);
|
||||
if(nColor < 0 || nColor > 15) {
|
||||
return PH7_VmThrowIoException(pCtx, 1, "Foreground color must be between 0 and 15");
|
||||
}
|
||||
pState = ConsoleState(pCtx->pVm);
|
||||
if(pState == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
pState->nFgColor = nColor;
|
||||
#ifdef __WINNT__
|
||||
if(!ConsoleIsRedirected(1) && nColor >= 0 && nColor < 16) {
|
||||
static const WORD aColor[] = {
|
||||
0, FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_GREEN | FOREGROUND_BLUE,
|
||||
FOREGROUND_RED, FOREGROUND_RED | FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_GREEN,
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
||||
FOREGROUND_INTENSITY, FOREGROUND_INTENSITY | FOREGROUND_BLUE,
|
||||
FOREGROUND_INTENSITY | FOREGROUND_GREEN, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
||||
FOREGROUND_INTENSITY | FOREGROUND_RED, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
|
||||
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
|
||||
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
|
||||
};
|
||||
CONSOLE_SCREEN_BUFFER_INFO sInfo;
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if(GetConsoleScreenBufferInfo(h, &sInfo)) {
|
||||
WORD wAttr = (sInfo.wAttributes & 0xF0) | aColor[nColor];
|
||||
SetConsoleTextAttribute(h, wAttr);
|
||||
}
|
||||
}
|
||||
#else
|
||||
if(!ConsoleIsRedirected(1) && nColor >= 0 && nColor < 16) {
|
||||
static const int aSgr[] = { 30, 34, 32, 36, 31, 35, 33, 37, 90, 94, 92, 96, 91, 95, 93, 97 };
|
||||
snprintf(zCode, sizeof(zCode), "\033[%dm", aSgr[nColor]);
|
||||
(void)ph7_context_output(pCtx, zCode, -1);
|
||||
}
|
||||
#endif
|
||||
return PH7_OK;
|
||||
}
|
||||
static int Console_SetBackgroundColor(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
int nColor;
|
||||
char zCode[16];
|
||||
console_state *pState;
|
||||
if(nArg < 1) {
|
||||
return PH7_OK;
|
||||
}
|
||||
nColor = ph7_value_to_int(apArg[0]);
|
||||
if(nColor < 0 || nColor > 15) {
|
||||
return PH7_VmThrowIoException(pCtx, 1, "Background color must be between 0 and 15");
|
||||
}
|
||||
pState = ConsoleState(pCtx->pVm);
|
||||
if(pState == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
pState->nBgColor = nColor;
|
||||
#ifdef __WINNT__
|
||||
if(!ConsoleIsRedirected(1) && nColor >= 0 && nColor < 16) {
|
||||
static const WORD aColor[] = {
|
||||
0, BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_GREEN | BACKGROUND_BLUE,
|
||||
BACKGROUND_RED, BACKGROUND_RED | BACKGROUND_BLUE, BACKGROUND_RED | BACKGROUND_GREEN,
|
||||
BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
|
||||
BACKGROUND_INTENSITY, BACKGROUND_INTENSITY | BACKGROUND_BLUE,
|
||||
BACKGROUND_INTENSITY | BACKGROUND_GREEN, BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
|
||||
BACKGROUND_INTENSITY | BACKGROUND_RED, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
|
||||
BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
|
||||
BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
|
||||
};
|
||||
CONSOLE_SCREEN_BUFFER_INFO sInfo;
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if(GetConsoleScreenBufferInfo(h, &sInfo)) {
|
||||
WORD wAttr = (sInfo.wAttributes & 0x0F) | aColor[nColor];
|
||||
SetConsoleTextAttribute(h, wAttr);
|
||||
}
|
||||
}
|
||||
#else
|
||||
if(!ConsoleIsRedirected(1) && nColor >= 0 && nColor < 16) {
|
||||
static const int aSgr[] = { 40, 44, 42, 46, 41, 45, 43, 47, 100, 104, 102, 106, 101, 105, 103, 107 };
|
||||
snprintf(zCode, sizeof(zCode), "\033[%dm", aSgr[nColor]);
|
||||
(void)ph7_context_output(pCtx, zCode, -1);
|
||||
}
|
||||
#endif
|
||||
return PH7_OK;
|
||||
}
|
||||
static int Console_GetForegroundColor(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
console_state *pState;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
pState = ConsoleState(pCtx->pVm);
|
||||
return ph7_result_int(pCtx, pState ? pState->nFgColor : -1);
|
||||
}
|
||||
static int Console_GetBackgroundColor(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
console_state *pState;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
pState = ConsoleState(pCtx->pVm);
|
||||
return ph7_result_int(pCtx, pState ? pState->nBgColor : -1);
|
||||
}
|
||||
static int Console_ResetColor(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
console_state *pState;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
pState = ConsoleState(pCtx->pVm);
|
||||
if(pState) {
|
||||
pState->nFgColor = -1;
|
||||
pState->nBgColor = -1;
|
||||
}
|
||||
#ifdef __WINNT__
|
||||
if(!ConsoleIsRedirected(1)) {
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
||||
}
|
||||
#else
|
||||
ConsoleEmit(pCtx, "\033[0m");
|
||||
#endif
|
||||
return PH7_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Registration */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static sxi32 ConsoleRegister(ph7_vm *pVm) {
|
||||
#define CTY(T) { PH7_TYPE_##T, 0, 0 }
|
||||
#define CONST_(N,V) { N, 0, PH7_MEMBER_PUBLIC, 0, 0 }
|
||||
static ph7_constant_def aConst[] = {
|
||||
CONST_("COLOR_BLACK", 0), CONST_("COLOR_DARK_BLUE", 1),
|
||||
CONST_("COLOR_DARK_GREEN", 2), CONST_("COLOR_DARK_CYAN", 3),
|
||||
CONST_("COLOR_DARK_RED", 4), CONST_("COLOR_DARK_MAGENTA", 5),
|
||||
CONST_("COLOR_DARK_YELLOW", 6), CONST_("COLOR_GRAY", 7),
|
||||
CONST_("COLOR_DARK_GRAY", 8), CONST_("COLOR_BLUE", 9),
|
||||
CONST_("COLOR_GREEN", 10), CONST_("COLOR_CYAN", 11),
|
||||
CONST_("COLOR_RED", 12), CONST_("COLOR_MAGENTA", 13),
|
||||
CONST_("COLOR_YELLOW", 14), CONST_("COLOR_WHITE", 15)
|
||||
};
|
||||
static const int aValue[] = {
|
||||
COLOR_BLACK, COLOR_DARK_BLUE, COLOR_DARK_GREEN, COLOR_DARK_CYAN,
|
||||
COLOR_DARK_RED, COLOR_DARK_MAGENTA, COLOR_DARK_YELLOW, COLOR_GRAY,
|
||||
COLOR_DARK_GRAY, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
|
||||
COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE
|
||||
};
|
||||
static const ph7_param_def aTitleParam[] = {
|
||||
{ "title", CTY(STRING), 0, 0, 0, 0 }
|
||||
};
|
||||
static const ph7_param_def aColorParam[] = {
|
||||
{ "color", CTY(INT), 0, 0, 0, 0 }
|
||||
};
|
||||
#define SM(NAME,RET,PARAMS,NPARAM,FUNC) \
|
||||
{ NAME, CTY(RET), PARAMS, NPARAM, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
|
||||
#define SMV(NAME,RET,PARAMS,NPARAM,FUNC) \
|
||||
{ NAME, CTY(RET), PARAMS, NPARAM, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC | PH7_MEMBER_VARIADIC }
|
||||
static const ph7_method_def aMethod[] = {
|
||||
SMV("Print", MIXED, 0, 0, Console_Print),
|
||||
SMV("PrintLine", MIXED, 0, 0, Console_PrintLine),
|
||||
SM("GetOut", OBJECT, 0, 0, Console_GetOut),
|
||||
SM("GetError", OBJECT, 0, 0, Console_GetError),
|
||||
SM("Read", INT, 0, 0, Console_Read),
|
||||
SM("ReadLine", MIXED, 0, 0, Console_ReadLine),
|
||||
SM("IsInputRedirected", BOOL, 0, 0, Console_IsInputRedirected),
|
||||
SM("IsOutputRedirected", BOOL, 0, 0, Console_IsOutputRedirected),
|
||||
SM("IsErrorRedirected", BOOL, 0, 0, Console_IsErrorRedirected),
|
||||
{ "GetCommandLineArgs", { PH7_TYPE_STRING, 0, 1 }, 0, 0,
|
||||
Console_GetCommandLineArgs, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
||||
SM("Clear", VOID, 0, 0, Console_Clear),
|
||||
SM("Beep", VOID, 0, 0, Console_Beep),
|
||||
SM("SetTitle", VOID, aTitleParam, 1, Console_SetTitle),
|
||||
SM("GetWidth", INT, 0, 0, Console_GetWidth),
|
||||
SM("GetHeight", INT, 0, 0, Console_GetHeight),
|
||||
SM("SetForegroundColor", VOID, aColorParam, 1, Console_SetForegroundColor),
|
||||
SM("SetBackgroundColor", VOID, aColorParam, 1, Console_SetBackgroundColor),
|
||||
SM("GetForegroundColor", INT, 0, 0, Console_GetForegroundColor),
|
||||
SM("GetBackgroundColor", INT, 0, 0, Console_GetBackgroundColor),
|
||||
SM("ResetColor", VOID, 0, 0, Console_ResetColor)
|
||||
};
|
||||
ph7_class_def sClass = {
|
||||
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, "Console",
|
||||
0, 0, aConst, SX_ARRAYSIZE(aConst),
|
||||
aMethod, SX_ARRAYSIZE(aMethod), PH7_DECL_FINAL | PH7_DECL_VIRTUAL,
|
||||
0, 0, 0, 0, 0, 0
|
||||
};
|
||||
ph7_value *apValue[SX_ARRAYSIZE(aConst)];
|
||||
sxu32 n;
|
||||
sxi32 rc = PH7_OK;
|
||||
SyZero(apValue, sizeof(apValue));
|
||||
for(n = 0 ; n < SX_ARRAYSIZE(apValue) ; ++n) {
|
||||
apValue[n] = ph7_new_scalar(pVm);
|
||||
if(apValue[n] == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Cleanup;
|
||||
}
|
||||
ph7_value_int(apValue[n], aValue[n]);
|
||||
aConst[n].pValue = apValue[n];
|
||||
}
|
||||
rc = ph7_create_class(pVm, &sClass);
|
||||
Cleanup:
|
||||
for(n = 0 ; n < SX_ARRAYSIZE(apValue) ; ++n) {
|
||||
if(apValue[n]) {
|
||||
ph7_release_value(pVm, apValue[n]);
|
||||
aConst[n].pValue = 0;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
#undef SMV
|
||||
#undef SM
|
||||
#undef CONST_
|
||||
#undef CTY
|
||||
}
|
||||
|
||||
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
|
||||
#ifdef __WINNT__
|
||||
/* Enable virtual terminal processing once, at module initialization,
|
||||
* never from a redirected-state query. */
|
||||
DWORD dwMode = 0;
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if(h != INVALID_HANDLE_VALUE && GetConsoleMode(h, &dwMode) &&
|
||||
(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0) {
|
||||
SetConsoleMode(h, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
}
|
||||
#endif
|
||||
desc->zString = MODULE_DESC;
|
||||
*ver = MODULE_VER;
|
||||
return ConsoleRegister(pVm);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @PROJECT AerScript Interpreter
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE modules/console/console.h
|
||||
* @DESCRIPTION Console class module for AerScript Interpreter
|
||||
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
|
||||
*/
|
||||
#ifndef __CONSOLE_H__
|
||||
#define __CONSOLE_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ph7.h"
|
||||
#include "ph7int.h"
|
||||
|
||||
#define MODULE_DESC "Console Module"
|
||||
#define MODULE_VER 1.0
|
||||
|
||||
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user