Skip to content

Commit 5456fc2

Browse files
committed
Adapt Windows-specific code to recent changes
This included renaming the `WORD` token in our lexer to `BXWORD`, in order to avoid a conflict with a symbol in windows.h.
1 parent 2b0e3ce commit 5456fc2

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

src/boxes.c

+22-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <string.h>
2525
#include <uniconv.h>
2626
#include <unistd.h>
27+
#ifdef _WIN32
28+
#include <windows.h>
29+
#endif
2730

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

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

@@ -453,6 +457,22 @@ static int check_color_support(int opt_color)
453457

454458

455459

460+
/**
461+
* Switch from default "C" encoding to system encoding.
462+
*/
463+
static void activateSystemEncoding()
464+
{
465+
#ifdef _WIN32
466+
SetConsoleOutputCP(CP_ACP);
467+
SetConsoleCP(CP_ACP);
468+
/* If it should one day turn out that this doesn't have the desired effect, try setlocale(LC_ALL, ".UTF8"). */
469+
#else
470+
setlocale(LC_ALL, "");
471+
#endif
472+
}
473+
474+
475+
456476
/* _\|/_
457477
(o o)
458478
+----oOO-{_}-OOo------------------------------------------------------------+
@@ -470,7 +490,7 @@ int main(int argc, char *argv[])
470490
#endif
471491

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

476496
handle_command_line(argc, argv);

src/lexer.l

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ PFILENAME [^\r\n]+
137137

138138
<DELIMSPEC>[^ \t\r\n]+ {
139139
/*
140-
* String delimiter spec - like WORD, but allow any character
140+
* String delimiter spec - like BXWORD, but allow any character
141141
*/
142142
yylval->s = bxs_from_ascii("IGNORED");
143143
char *str = (char *) strdup(yytext);
@@ -415,10 +415,10 @@ PFILENAME [^\r\n]+
415415
exit (EXIT_FAILURE);
416416
}
417417
#ifdef LEXER_DEBUG
418-
fprintf (stderr, " WORD: %s\n", u32_strconv_to_output(utf8));
418+
fprintf (stderr, " BXWORD: %s\n", u32_strconv_to_output(utf8));
419419
#endif
420420
BFREE(utf8);
421-
return WORD;
421+
return BXWORD;
422422
}
423423

424424

src/parser.y

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ typedef struct {
127127
%token YPARENT YSHAPES YELASTIC YPADDING YSAMPLE YENDSAMPLE YBOX YEND YUNREC
128128
%token YREPLACE YREVERSE YTO YWITH YCHGDEL YTAGS
129129
%token <ascii> KEYWORD
130-
%token <s> WORD
130+
%token <s> BXWORD
131131
%token <ascii> ASCII_ID
132132
%token <s> STRING
133133
%token <s> FILENAME
@@ -224,7 +224,7 @@ alias_list: alias | alias_list ',' alias;
224224

225225
design_id: ASCII_ID | ASCII_ID ',' alias_list
226226

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

283283
| YTAGS '(' tag_list ')' | YTAGS tag_entry
284284

285-
| WORD STRING | ASCII_ID STRING
285+
| BXWORD STRING | ASCII_ID STRING
286286
{
287287
#ifdef PARSER_DEBUG
288288
fprintf (stderr, " Parser: Discarding entry [%s = %s].\n", $1, bxs_to_output($2));

src/tools.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,10 @@ FILE *bx_fopen(char *pathname, char *mode)
741741
/*
742742
* On Linux/UNIX and OS X (Mac), one can access files with non-ASCII file names by passing them to fopen() as UTF-8.
743743
* On Windows, a different function must be called. (Info: https://stackoverflow.com/a/35065142/1005481)
744+
* On newer Windows, we're good:
745+
* https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale#utf-8-support
744746
*/
745747
FILE *f = fopen(pathname, mode);
746-
// TODO Windows
747748
return f;
748749
}
749750

utest/unicode_test.c

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ void test_u32_insert_space_at(void **state)
196196
u32_insert_space_at(&s, 1, 1);
197197
u32_insert_space_at(&s, 10000, 2);
198198

199+
assert_non_null(s);
199200
assert_int_equal(0, u32_strcmp(expected, s));
200201

201202
BFREE(s);

0 commit comments

Comments
 (0)