Skip to content

Commit

Permalink
Fix UTF-8 in Windows console (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtereshkov committed Feb 25, 2024
1 parent 783d2d2 commit 81e78c6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Allocates memory for the interpreter and returns the interpreter instance handle

```
UMKA_API bool umkaInit(void *umka, const char *fileName, const char *sourceString,
int stackSize, const char *locale,
int stackSize, void *reserved,
int argc, char **argv,
bool fileSystemEnabled, bool implLibsEnabled,
UmkaWarningCallback warningCallback);
```

Initializes the interpreter instance. Here, `umka` is the interpreter instance handle, `fileName` is the Umka source file name, `sourceString` is an optional string buffer that contains the program source, `stackSize` is the fiber stack size, in slots, `argc` and `argv` represent the standard C/C++ command-line parameter data. If `sourceString` is not `NULL`, the program source is read from this string rather than from a file. A fictitious `fileName` should nevertheless be specified. An optional `locale` string can be specified. The `fileSystemEnabled` flag allows the Umka program to access the file system, and the `implLibsEnabled` flag allows it to use UMIs. If `warningCallback` is not `NULL`, it will be called on every warning. Returns `true` if the source has been successfully loaded.
Initializes the interpreter instance. Here, `umka` is the interpreter instance handle, `fileName` is the Umka source file name, `sourceString` is an optional string buffer that contains the program source, `stackSize` is the fiber stack size, in slots, `argc` and `argv` represent the standard C/C++ command-line parameter data. If `sourceString` is not `NULL`, the program source is read from this string rather than from a file. A fictitious `fileName` should nevertheless be specified. The `fileSystemEnabled` flag allows the Umka program to access the file system, and the `implLibsEnabled` flag allows it to use UMIs. If `warningCallback` is not `NULL`, it will be called on every warning. Returns `true` if the source has been successfully loaded.

```
UMKA_API bool umkaAddModule(void *umka, const char *fileName, const char *sourceString);
Expand Down
2 changes: 1 addition & 1 deletion playground/umka.js

Large diffs are not rendered by default.

16 changes: 1 addition & 15 deletions src/umka.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void help(void)
printf("Usage: umka [<parameters>] <file.um> [<script-parameters>]\n");
printf("Parameters:\n");
printf(" -stack <stack-size> - Set stack size\n");
printf(" -locale <locale-string> - Set locale\n");
printf(" -asm - Write assembly listing\n");
printf(" -check - Compile only\n");
printf(" -warn - Enable warnings\n");
Expand Down Expand Up @@ -96,7 +95,6 @@ int main(int argc, char **argv)
{
// Parse interpreter parameters
int stackSize = DEFAULT_STACK_SIZE;
const char *locale = NULL;
bool writeAsm = false;
bool compileOnly = false;
bool printWarnings = false;
Expand All @@ -122,18 +120,6 @@ int main(int argc, char **argv)

i += 2;
}
else if (strcmp(argv[i], "-locale") == 0)
{
if (i + 1 == argc)
{
fprintf(stderr, "No locale string\n");
return 1;
}

locale = argv[i + 1];

i += 2;
}
else if (strcmp(argv[i], "-asm") == 0)
{
writeAsm = true;
Expand Down Expand Up @@ -166,7 +152,7 @@ int main(int argc, char **argv)
}

void *umka = umkaAlloc();
bool ok = umkaInit(umka, argv[i], NULL, stackSize, locale, argc - i, argv + i, !isSandbox, !isSandbox, printWarnings ? printCompileWarning : NULL);
bool ok = umkaInit(umka, argv[i], NULL, stackSize, NULL, argc - i, argv + i, !isSandbox, !isSandbox, printWarnings ? printCompileWarning : NULL);
int exitCode = 0;

if (ok)
Expand Down
4 changes: 2 additions & 2 deletions src/umka_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ UMKA_API void *umkaAlloc(void)
}


UMKA_API bool umkaInit(void *umka, const char *fileName, const char *sourceString, int stackSize, const char *locale, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled, UmkaWarningCallback warningCallback)
UMKA_API bool umkaInit(void *umka, const char *fileName, const char *sourceString, int stackSize, void *reserved, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled, UmkaWarningCallback warningCallback)
{
Compiler *comp = umka;
memset(comp, 0, sizeof(Compiler));
Expand All @@ -95,7 +95,7 @@ UMKA_API bool umkaInit(void *umka, const char *fileName, const char *sourceStrin

if (setjmp(comp->error.jumper) == 0)
{
compilerInit(comp, fileName, sourceString, stackSize, locale, argc, argv, fileSystemEnabled, implLibsEnabled);
compilerInit(comp, fileName, sourceString, stackSize, argc, argv, fileSystemEnabled, implLibsEnabled);
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/umka_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ typedef void (*UmkaWarningCallback)(UmkaError *warning);
typedef struct
{
void *(*umkaAlloc) (void);
bool (*umkaInit) (void *umka, const char *fileName, const char *sourceString, int stackSize, const char *locale, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled, UmkaWarningCallback warningCallback);
bool (*umkaInit) (void *umka, const char *fileName, const char *sourceString, int stackSize, void *reserved, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled, UmkaWarningCallback warningCallback);
bool (*umkaCompile) (void *umka);
int (*umkaRun) (void *umka);
int (*umkaCall) (void *umka, int entryOffset, int numParamSlots, UmkaStackSlot *params, UmkaStackSlot *result);
Expand All @@ -116,7 +116,7 @@ typedef struct


UMKA_API void *umkaAlloc (void);
UMKA_API bool umkaInit (void *umka, const char *fileName, const char *sourceString, int stackSize, const char *locale, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled, UmkaWarningCallback warningCallback);
UMKA_API bool umkaInit (void *umka, const char *fileName, const char *sourceString, int stackSize, void *reserved, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled, UmkaWarningCallback warningCallback);
UMKA_API bool umkaCompile (void *umka);
UMKA_API int umkaRun (void *umka);
UMKA_API int umkaCall (void *umka, int entryOffset, int numParamSlots, UmkaStackSlot *params, UmkaStackSlot *result);
Expand Down
19 changes: 14 additions & 5 deletions src/umka_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>

#ifdef _WIN32
#include <windows.h>
#endif

#include "umka_compiler.h"
#include "umka_runtime_src.h"
Expand Down Expand Up @@ -177,8 +180,13 @@ static void compilerDeclareExternalFuncs(Compiler *comp, bool fileSystemEnabled)
}


void compilerInit(Compiler *comp, const char *fileName, const char *sourceString, int stackSize, const char *locale, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled)
void compilerInit(Compiler *comp, const char *fileName, const char *sourceString, int stackSize, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled)
{
#ifdef _WIN32
comp->originalCodepage = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
#endif

compilerSetAPI(comp);

storageInit (&comp->storage);
Expand All @@ -203,9 +211,6 @@ void compilerInit(Compiler *comp, const char *fileName, const char *sourceString

lexInit(&comp->lex, &comp->storage, &comp->debug, filePath, sourceString, &comp->error);

if (locale && !setlocale(LC_ALL, locale))
comp->error.handler(comp->error.context, "Cannot set locale");

comp->argc = argc;
comp->argv = argv;

Expand Down Expand Up @@ -252,6 +257,10 @@ void compilerFree(Compiler *comp)
blocksFree (&comp->blocks);
moduleFree (&comp->modules);
storageFree (&comp->storage);

#ifdef _WIN32
SetConsoleOutputCP(comp->originalCodepage);
#endif
}


Expand Down
5 changes: 4 additions & 1 deletion src/umka_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ typedef struct
int argc;
char **argv;

// Original codepage (Windows only)
unsigned int originalCodepage;

} Compiler;


void compilerInit (Compiler *comp, const char *fileName, const char *sourceString, int stackSize, const char *locale, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled);
void compilerInit (Compiler *comp, const char *fileName, const char *sourceString, int stackSize, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled);
void compilerFree (Compiler *comp);
void compilerCompile (Compiler *comp);
void compilerRun (Compiler *comp);
Expand Down

0 comments on commit 81e78c6

Please sign in to comment.