Skip to content

Commit

Permalink
Adapt Windows-specific code to recent changes
Browse files Browse the repository at this point in the history
This included renaming the `WORD` token in our lexer to `BXWORD`,
in order to avoid a conflict with a symbol in windows.h.
  • Loading branch information
tsjensen committed Feb 15, 2024
1 parent 2b0e3ce commit 5456fc2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
24 changes: 22 additions & 2 deletions src/boxes.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <string.h>
#include <uniconv.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#endif

#include "boxes.h"
#include "bxstring.h"
Expand Down Expand Up @@ -103,7 +106,8 @@ static int build_design(design_t **adesigns, const char *cld)
dp->tags = (char **) calloc(2, sizeof(char *));
dp->tags[0] = "transient";

uint32_t *cld_u32 = u32_strconv_from_arg(cld, "UTF-8"); /* CHECK wrong on Windows (UTF-16) or different IME */
/* We always use UTF-8, which is correct for Linux and MacOS, and for modern Windows configured for UTF-8. */
uint32_t *cld_u32 = u32_strconv_from_arg(cld, "UTF-8");
bxstr_t *cldW = bxs_from_unicode(cld_u32);
BFREE(cld_u32);

Expand Down Expand Up @@ -453,6 +457,22 @@ static int check_color_support(int opt_color)



/**
* Switch from default "C" encoding to system encoding.
*/
static void activateSystemEncoding()
{
#ifdef _WIN32
SetConsoleOutputCP(CP_ACP);
SetConsoleCP(CP_ACP);
/* If it should one day turn out that this doesn't have the desired effect, try setlocale(LC_ALL, ".UTF8"). */
#else
setlocale(LC_ALL, "");
#endif
}



/* _\|/_
(o o)
+----oOO-{_}-OOo------------------------------------------------------------+
Expand All @@ -470,7 +490,7 @@ int main(int argc, char *argv[])
#endif

/* Temporarily set the system encoding, for proper output of --help text etc. */
setlocale(LC_ALL, ""); /* switch from default "C" encoding to system encoding */
activateSystemEncoding();
encoding = locale_charset();

handle_command_line(argc, argv);
Expand Down
6 changes: 3 additions & 3 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ PFILENAME [^\r\n]+

<DELIMSPEC>[^ \t\r\n]+ {
/*
* String delimiter spec - like WORD, but allow any character
* String delimiter spec - like BXWORD, but allow any character
*/
yylval->s = bxs_from_ascii("IGNORED");
char *str = (char *) strdup(yytext);
Expand Down Expand Up @@ -415,10 +415,10 @@ PFILENAME [^\r\n]+
exit (EXIT_FAILURE);
}
#ifdef LEXER_DEBUG
fprintf (stderr, " WORD: %s\n", u32_strconv_to_output(utf8));
fprintf (stderr, " BXWORD: %s\n", u32_strconv_to_output(utf8));
#endif
BFREE(utf8);
return WORD;
return BXWORD;
}


Expand Down
6 changes: 3 additions & 3 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ typedef struct {
%token YPARENT YSHAPES YELASTIC YPADDING YSAMPLE YENDSAMPLE YBOX YEND YUNREC
%token YREPLACE YREVERSE YTO YWITH YCHGDEL YTAGS
%token <ascii> KEYWORD
%token <s> WORD
%token <s> BXWORD
%token <ascii> ASCII_ID
%token <s> STRING
%token <s> FILENAME
Expand Down Expand Up @@ -224,7 +224,7 @@ alias_list: alias | alias_list ',' alias;

design_id: ASCII_ID | ASCII_ID ',' alias_list

| WORD
| BXWORD
{
yyerror(bison_args, "box design name must consist of printable standard ASCII characters.");
YYERROR;
Expand Down Expand Up @@ -282,7 +282,7 @@ entry: KEYWORD STRING

| YTAGS '(' tag_list ')' | YTAGS tag_entry

| WORD STRING | ASCII_ID STRING
| BXWORD STRING | ASCII_ID STRING
{
#ifdef PARSER_DEBUG
fprintf (stderr, " Parser: Discarding entry [%s = %s].\n", $1, bxs_to_output($2));
Expand Down
3 changes: 2 additions & 1 deletion src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,10 @@ FILE *bx_fopen(char *pathname, char *mode)
/*
* On Linux/UNIX and OS X (Mac), one can access files with non-ASCII file names by passing them to fopen() as UTF-8.
* On Windows, a different function must be called. (Info: https://stackoverflow.com/a/35065142/1005481)
* On newer Windows, we're good:
* https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale#utf-8-support
*/
FILE *f = fopen(pathname, mode);
// TODO Windows
return f;
}

Expand Down
1 change: 1 addition & 0 deletions utest/unicode_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void test_u32_insert_space_at(void **state)
u32_insert_space_at(&s, 1, 1);
u32_insert_space_at(&s, 10000, 2);

assert_non_null(s);
assert_int_equal(0, u32_strcmp(expected, s));

BFREE(s);
Expand Down

0 comments on commit 5456fc2

Please sign in to comment.