Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/wrc/wrc.c
4388 views
1
/*
2
* Copyright 1994 Martin von Loewis
3
* Copyright 1998 Bertho A. Stultiens (BS)
4
* Copyright 2003 Dimitrie O. Paun
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
*
20
*/
21
22
#include "config.h"
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <assert.h>
28
#include <ctype.h>
29
#include <limits.h>
30
#include <sys/types.h>
31
32
#include "../tools.h"
33
#include "wrc.h"
34
#include "utils.h"
35
#include "newstruc.h"
36
#include "parser.h"
37
#include "wpp_private.h"
38
39
static const char usage[] =
40
"Usage: wrc [options...] [infile[.rc|.res]]\n"
41
" -D, --define id[=val] Define preprocessor identifier id=val\n"
42
" --debug=nn Set debug level to 'nn'\n"
43
" -E Preprocess only\n"
44
" -F TARGET Ignored, for compatibility with windres\n"
45
" -fo FILE Synonym for -o for compatibility with windres\n"
46
" -h, --help Prints this summary\n"
47
" -i, --input=FILE The name of the input file\n"
48
" -I, --include-dir=DIR Add dir to the include search path (multiple -I allowed)\n"
49
" -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
50
" -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
51
" -m16 Build a 16-bit resource file\n"
52
" --nls-dir=DIR Directory containing the NLS codepage mappings\n"
53
" --no-use-temp-file Ignored for compatibility with windres\n"
54
" --nostdinc Disables searching the standard include path\n"
55
" -o, --output=FILE Output to file (default is infile.res)\n"
56
" -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
57
" --pedantic Enable pedantic warnings\n"
58
" --po-dir=DIR Directory containing po files for translations\n"
59
" --preprocessor Specifies the preprocessor to use, including arguments\n"
60
" -r Ignored for compatibility with rc\n"
61
" --sysroot=DIR Prefix include paths with DIR\n"
62
" -U, --undefine id Undefine preprocessor identifier id\n"
63
" --use-temp-file Ignored for compatibility with windres\n"
64
" -v, --verbose Enable verbose mode\n"
65
" --verify-translations Check the status of the various translations\n"
66
" --version Print version and exit\n"
67
"Input is taken from stdin if no sourcefile specified.\n"
68
"Debug level 'n' is a bitmask with following meaning:\n"
69
" * 0x01 Tell which resource is parsed (verbose mode)\n"
70
" * 0x02 Dump internal structures\n"
71
" * 0x04 Create a parser trace (yydebug=1)\n"
72
" * 0x08 Preprocessor messages\n"
73
" * 0x10 Preprocessor lex messages\n"
74
" * 0x20 Preprocessor yacc trace\n"
75
"If no input filename is given and the output name is not overridden\n"
76
"with -o, then the output is written to \"wrc.tab.res\"\n"
77
;
78
79
static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
80
"Copyright 1998-2000 Bertho A. Stultiens\n"
81
" 1994 Martin von Loewis\n";
82
83
/*
84
* Set if compiling in 32bit mode (default).
85
*/
86
int win32 = 1;
87
88
/*
89
* debuglevel == DEBUGLEVEL_NONE Don't bother
90
* debuglevel & DEBUGLEVEL_CHAT Say what's done
91
* debuglevel & DEBUGLEVEL_DUMP Dump internal structures
92
* debuglevel & DEBUGLEVEL_TRACE Create parser trace
93
* debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
94
* debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
95
* debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
96
*/
97
int debuglevel = DEBUGLEVEL_NONE;
98
99
/*
100
* Recognize win32 keywords if set (-w 32 enforces this),
101
* otherwise set with -e option.
102
*/
103
int extensions = 1;
104
105
/*
106
* Language setting for resources (-l option)
107
*/
108
static language_t defaultlanguage;
109
language_t currentlanguage = 0;
110
111
/*
112
* Set when extra warnings should be generated (-W option)
113
*/
114
int pedantic = 0;
115
116
/*
117
* Set when _only_ to run the preprocessor (-E option)
118
*/
119
int preprocess_only = 0;
120
121
/*
122
* Set when _not_ to run the preprocessor (-P cat option)
123
*/
124
int no_preprocess = 0;
125
126
int utf8_input = 0;
127
128
int check_utf8 = 1; /* whether to check for valid utf8 */
129
130
static char *output_name; /* The name given by the -o option */
131
const char *input_name = NULL; /* The name given on the command-line */
132
static struct strarray input_files;
133
const char *temp_dir = NULL;
134
struct strarray temp_files = { 0 };
135
136
static int stdinc = 1;
137
static int po_mode;
138
static const char *po_dir;
139
static const char *sysroot = "";
140
static const char *bindir;
141
static const char *includedir;
142
const char *nlsdirs[3] = { NULL, DATADIR "/wine/nls", NULL };
143
144
int line_number = 1; /* The current line */
145
int char_number = 1; /* The current char pos within the line */
146
147
int parser_debug, yy_flex_debug;
148
149
resource_t *resource_top; /* The top of the parsed resources */
150
151
static void cleanup_files(void);
152
153
enum long_options_values
154
{
155
LONG_OPT_NOSTDINC = 1,
156
LONG_OPT_TMPFILE,
157
LONG_OPT_NOTMPFILE,
158
LONG_OPT_NLS_DIR,
159
LONG_OPT_PO_DIR,
160
LONG_OPT_PREPROCESSOR,
161
LONG_OPT_SYSROOT,
162
LONG_OPT_VERSION,
163
LONG_OPT_DEBUG,
164
LONG_OPT_PEDANTIC,
165
};
166
167
static const char short_options[] =
168
"b:D:Ef:F:hi:I:J:l:m:o:O:ruU:v";
169
static const struct long_option long_options[] = {
170
{ "debug", 1, LONG_OPT_DEBUG },
171
{ "define", 1, 'D' },
172
{ "help", 0, 'h' },
173
{ "include-dir", 1, 'I' },
174
{ "input", 1, 'i' },
175
{ "input-format", 1, 'J' },
176
{ "language", 1, 'l' },
177
{ "nls-dir", 1, LONG_OPT_NLS_DIR },
178
{ "no-use-temp-file", 0, LONG_OPT_NOTMPFILE },
179
{ "nostdinc", 0, LONG_OPT_NOSTDINC },
180
{ "output", 1, 'o' },
181
{ "output-format", 1, 'O' },
182
{ "pedantic", 0, LONG_OPT_PEDANTIC },
183
{ "po-dir", 1, LONG_OPT_PO_DIR },
184
{ "preprocessor", 1, LONG_OPT_PREPROCESSOR },
185
{ "sysroot", 1, LONG_OPT_SYSROOT },
186
{ "target", 1, 'F' },
187
{ "utf8", 0, 'u' },
188
{ "undefine", 1, 'U' },
189
{ "use-temp-file", 0, LONG_OPT_TMPFILE },
190
{ "verbose", 0, 'v' },
191
{ "version", 0, LONG_OPT_VERSION },
192
{ NULL }
193
};
194
195
static void set_version_defines(void)
196
{
197
char *version = xstrdup( PACKAGE_VERSION );
198
char *major, *minor, *patchlevel;
199
char buffer[100];
200
201
if ((minor = strchr( version, '.' )))
202
{
203
major = version;
204
*minor++ = 0;
205
if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
206
}
207
else /* pre 0.9 version */
208
{
209
major = NULL;
210
patchlevel = version;
211
}
212
snprintf( buffer, sizeof(buffer), "__WRC__=%s", major ? major : "0" );
213
wpp_add_cmdline_define(buffer);
214
snprintf( buffer, sizeof(buffer), "__WRC_MINOR__=%s", minor ? minor : "0" );
215
wpp_add_cmdline_define(buffer);
216
snprintf( buffer, sizeof(buffer), "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
217
wpp_add_cmdline_define(buffer);
218
free( version );
219
}
220
221
/* clean things up when aborting on a signal */
222
static void exit_on_signal( int sig )
223
{
224
exit(1); /* this will call the atexit functions */
225
}
226
227
/* load a single input file */
228
static int load_file( const char *input_name, const char *output_name )
229
{
230
int ret;
231
232
/* Run the preprocessor on the input */
233
if(!no_preprocess)
234
{
235
FILE *output;
236
int ret;
237
char *name;
238
239
/*
240
* Preprocess the input to a temp-file, or stdout if
241
* no output was given.
242
*/
243
244
if (preprocess_only)
245
{
246
if (output_name)
247
{
248
if (!(output = fopen( output_name, "w" )))
249
fatal_perror( "Could not open %s for writing", output_name );
250
ret = wpp_parse( input_name, output );
251
fclose( output );
252
}
253
else ret = wpp_parse( input_name, stdout );
254
255
if (ret) return ret;
256
output_name = NULL;
257
exit(0);
258
}
259
260
name = make_temp_file( output_name, "" );
261
if (!(output = fopen(name, "wt")))
262
error("Could not open fd %s for writing\n", name);
263
264
ret = wpp_parse( input_name, output );
265
fclose( output );
266
if (ret) return ret;
267
input_name = name;
268
}
269
270
/* Reset the language */
271
currentlanguage = defaultlanguage;
272
check_utf8 = 1;
273
274
/* Go from .rc to .res */
275
chat("Starting parse\n");
276
277
if(!(parser_in = fopen(input_name, "rb")))
278
fatal_perror("Could not open %s for input", input_name);
279
280
ret = parser_parse();
281
fclose(parser_in);
282
parser_lex_destroy();
283
return ret;
284
}
285
286
static void option_callback( int optc, char *optarg )
287
{
288
switch(optc)
289
{
290
case LONG_OPT_NOSTDINC:
291
stdinc = 0;
292
break;
293
case LONG_OPT_TMPFILE:
294
if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
295
break;
296
case LONG_OPT_NOTMPFILE:
297
if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
298
break;
299
case LONG_OPT_NLS_DIR:
300
nlsdirs[0] = xstrdup( optarg );
301
break;
302
case LONG_OPT_PO_DIR:
303
po_dir = xstrdup( optarg );
304
break;
305
case LONG_OPT_PREPROCESSOR:
306
if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
307
else fprintf(stderr, "-P option not yet supported, ignored.\n");
308
break;
309
case LONG_OPT_SYSROOT:
310
sysroot = xstrdup( optarg );
311
break;
312
case LONG_OPT_VERSION:
313
printf(version_string);
314
exit(0);
315
break;
316
case LONG_OPT_DEBUG:
317
debuglevel = strtol(optarg, NULL, 0);
318
break;
319
case LONG_OPT_PEDANTIC:
320
pedantic = 1;
321
break;
322
case 'D':
323
wpp_add_cmdline_define(optarg);
324
break;
325
case 'E':
326
preprocess_only = 1;
327
break;
328
case 'b':
329
case 'F':
330
break;
331
case 'h':
332
printf(usage);
333
exit(0);
334
case 'i':
335
strarray_add( &input_files, optarg );
336
break;
337
case 'I':
338
wpp_add_include_path(optarg);
339
break;
340
case 'J':
341
if (strcmp(optarg, "rc16") == 0) extensions = 0;
342
else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
343
break;
344
case 'l':
345
defaultlanguage = strtol(optarg, NULL, 0);
346
if (get_language_codepage( defaultlanguage ) == -1)
347
error("Language %04x is not supported\n", defaultlanguage);
348
break;
349
case 'm':
350
if (!strcmp( optarg, "16" )) win32 = 0;
351
else win32 = 1;
352
break;
353
case 'f':
354
if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
355
optarg++;
356
/* fall through */
357
case 'o':
358
if (!output_name) output_name = xstrdup(optarg);
359
else error("Too many output files.\n");
360
break;
361
case 'O':
362
if (strcmp(optarg, "po") == 0) po_mode = 1;
363
else if (strcmp(optarg, "pot") == 0) po_mode = 2;
364
else if (strcmp(optarg, "res16") == 0) win32 = 0;
365
else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
366
break;
367
case 'r':
368
/* ignored for compatibility with rc */
369
break;
370
case 'u':
371
utf8_input = 1;
372
break;
373
case 'U':
374
wpp_del_define(optarg);
375
break;
376
case 'v':
377
debuglevel = DEBUGLEVEL_CHAT;
378
break;
379
case '?':
380
fprintf(stderr, "wrc: %s\n\n%s", optarg, usage);
381
exit(1);
382
}
383
}
384
385
int main(int argc,char *argv[])
386
{
387
int i;
388
389
init_signals( exit_on_signal );
390
bindir = get_bindir( argv[0] );
391
includedir = get_includedir( bindir );
392
nlsdirs[0] = get_nlsdir( bindir, "/tools/wrc" );
393
394
/* Set the default defined stuff */
395
set_version_defines();
396
wpp_add_cmdline_define("RC_INVOKED=1");
397
/* Microsoft RC always searches current directory */
398
wpp_add_include_path(".");
399
400
strarray_addall( &input_files,
401
parse_options( argc, argv, short_options, long_options, 0, option_callback ));
402
403
if (win32) wpp_add_cmdline_define("_WIN32=1");
404
405
/* If we do need to search standard includes, add them to the path */
406
if (stdinc)
407
{
408
static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
409
410
if (includedir)
411
{
412
wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
413
wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
414
}
415
for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
416
{
417
if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
418
wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
419
wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
420
}
421
}
422
423
/* Kill io buffering when some kind of debuglevel is enabled */
424
if(debuglevel)
425
{
426
setbuf(stdout, NULL);
427
setbuf(stderr, NULL);
428
}
429
430
parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
431
yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
432
433
wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
434
(debuglevel & DEBUGLEVEL_PPTRACE) != 0,
435
(debuglevel & DEBUGLEVEL_PPMSG) != 0 );
436
437
atexit(cleanup_files);
438
439
for (i = 0; i < input_files.count; i++)
440
{
441
input_name = input_files.str[i];
442
if (load_file( input_name, output_name )) exit(1);
443
}
444
/* stdin special case. NULL means "stdin" for wpp. */
445
if (input_files.count == 0 && load_file( NULL, output_name )) exit(1);
446
447
if (!po_mode && output_name)
448
{
449
if (strendswith( output_name, ".po" )) po_mode = 1;
450
else if (strendswith( output_name, ".pot" )) po_mode = 2;
451
}
452
if (po_mode)
453
{
454
if (!win32) error( "PO files are not supported in 16-bit mode\n" );
455
if (po_mode == 2) /* pot file */
456
{
457
if (!output_name)
458
{
459
const char *name = input_files.count ? get_basename(input_files.str[0]) : "wrc.tab";
460
output_name = replace_extension( name, ".rc", ".pot" );
461
}
462
write_pot_file( output_name );
463
}
464
else write_po_files( output_name );
465
output_name = NULL;
466
exit(0);
467
}
468
if (win32) add_translations( po_dir );
469
470
chat("Writing .res-file\n");
471
if (!output_name)
472
{
473
const char *name = input_files.count ? get_basename(input_files.str[0]) : "wrc.tab";
474
output_name = replace_extension( name, ".rc", ".res" );
475
}
476
write_resfile(output_name, resource_top);
477
output_name = NULL;
478
479
return 0;
480
}
481
482
483
static void cleanup_files(void)
484
{
485
if (output_name) unlink(output_name);
486
remove_temp_files();
487
}
488
489