Bugfix: Declare CALL32_CBClient[Ex] without WINAPI.
[wine/wine64.git] / tools / wrc / wrc.c
blob52f8e576657e59bc5e33be2bf898f874363773af
1 /*
3 * Copyright Martin von Loewis, 1994
4 * Copyrignt 1998 Bertho A. Stultiens (BS)
6 * 20-Jun-1998 BS - Added -L option to prevent case conversion
7 * of embedded filenames.
9 * 08-Jun-1998 BS - Added -A option to generate autoregister code
10 * for winelib operation.
12 * 21-May-1998 BS - Removed the CPP option. Its internal now.
13 * - Added implementations for defines and includes
14 * on the commandline.
16 * 30-Apr-1998 BS - The options now contain nearly the entire alphabet.
17 * Seems to be a sign for too much freedom. I implemeted
18 * most of them as a user choice possibility for things
19 * that I do not know what to put there by default.
20 * - -l and -L options are now known as -t and -T.
22 * 23-Apr-1998 BS - Finally gave up on backward compatibility on the
23 * commandline (after a blessing from the newsgroup).
24 * So, I changed the lot.
26 * 17-Apr-1998 BS - Added many new command-line options but took care
27 * that it would not break old scripts (sigh).
29 * 16-Apr-1998 BS - There is not much left of the original source...
30 * I had to rewrite most of it because the parser
31 * changed completely with all the types etc..
35 #include "config.h"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41 #include <ctype.h>
43 #include "resource.h" /* For HAVE_WINE_CONSTRUCTOR */
45 #include "wrc.h"
46 #include "utils.h"
47 #include "writeres.h"
48 #include "readres.h"
49 #include "dumpres.h"
50 #include "genres.h"
51 #include "newstruc.h"
52 #include "preproc.h"
53 #include "parser.h"
55 char usage[] = "Usage: wrc [options...] [infile[.rc|.res]]\n"
56 " -a n Alignment of resource (win16 only, default is 4)\n"
57 " -A Auto register resources (only with gcc 2.7 and better)\n"
58 " -b Create a C array from a binary .res file\n"
59 " -c Add 'const' prefix to C constants\n"
60 " -C cp Set the resource's codepage to cp (default is 0)\n"
61 " -d n Set debug level to 'n'\n"
62 " -D id[=val] Define preprocessor identifier id=val\n"
63 " -e Disable recognition of win32 keywords in 16bit compile\n"
64 " -g Add symbols to the global c namespace\n"
65 " -h Also generate a .h file\n"
66 " -H file Same as -h but written to file\n"
67 " -I path Set include search dir to path (multiple -I allowed)\n"
68 " -l lan Set default language to lan (default is neutral {0})\n"
69 " -L Leave case of embedded filenames as is\n"
70 " -n Do not generate .s file\n"
71 " -o file Output to file (default is infile.[res|s|h]\n"
72 " -p prefix Give a prefix for the generated names\n"
73 " -r Create binary .res file (compile only)\n"
74 " -s Add structure with win32/16 (PE/NE) resource directory\n"
75 " -t Generate indirect loadable resource tables\n"
76 " -T Generate only indirect loadable resources tables\n"
77 " -V Print version end exit\n"
78 " -w 16|32 Select win16 or win32 output (default is win32)\n"
79 " -W Enable pedantic warnings\n"
80 "Input is taken from stdin if no sourcefile specified.\n"
81 "Debug level 'n' is a bitmask with following meaning:\n"
82 " * 0x01 Tell which resource is parsed (verbose mode)\n"
83 " * 0x02 Dump internal structures\n"
84 " * 0x04 Create a parser trace (yydebug=1)\n"
85 "The -o option only applies to the final destination file, which is\n"
86 "in case of normal compile a .s file. You must use the '-H header.h'\n"
87 "option to override the header-filename.\n"
88 "If no input filename is given and the output name is not overridden\n"
89 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
92 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
93 "Copyright 1998,1999 Bertho A. Stultiens\n"
94 " 1994 Martin von Loewis\n";
97 * Default prefix for resource names used in the C array.
98 * Option '-p name' sets it to 'name'
100 #ifdef NEED_UNDERSCORE_PREFIX
101 char *prefix = "__Resource";
102 #else
103 char *prefix = "_Resource";
104 #endif
107 * Set if compiling in 32bit mode (default).
109 int win32 = 1;
112 * Set when generated C variables should be prefixed with 'const'
114 int constant = 0;
117 * Create a .res file from the source and exit (-r option).
119 int create_res = 0;
122 * debuglevel == DEBUGLEVEL_NONE Don't bother
123 * debuglevel & DEBUGLEVEL_CHAT Say whats done
124 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
125 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
127 int debuglevel = DEBUGLEVEL_NONE;
130 * Recognize win32 keywords if set (-w 32 enforces this),
131 * otherwise set with -e option.
133 int extensions = 1;
136 * Set when creating C array from .res file (-b option).
138 int binary = 0;
141 * Set when an additional C-header is to be created in compile (-h option).
143 int create_header = 0;
146 * Set when the NE/PE resource directory should be dumped into
147 * the output file.
149 int create_dir = 0;
152 * Set when all symbols should be added to the global namespace (-g option)
154 int global = 0;
157 * Set when indirect loadable resource tables should be created (-t)
159 int indirect = 0;
162 * Set when _only_ indirect loadable resource tables should be created (-T)
164 int indirect_only = 0;
167 * NE segment resource aligment (-a option)
169 int alignment = 4;
170 int alignment_pwr;
173 * Cleared when the assembly file must be suppressed (-n option)
175 int create_s = 1;
178 * Language setting for resources (-l option)
180 language_t *currentlanguage = NULL;
183 * The codepage to write in win32 PE resource segment (-C option)
185 DWORD codepage = 0;
188 * Set when extra warnings should be generated (-W option)
190 int pedantic = 0;
193 * Set when autoregister code must be added to the output (-A option)
195 int auto_register = 0;
198 * Set when the case of embedded filenames should not be converted
199 * to lower case (-L option)
201 int leave_case = 0;
203 char *output_name; /* The name given by the -o option */
204 char *input_name; /* The name given on the command-line */
205 char *header_name; /* The name given by the -H option */
207 char *cmdline; /* The entire commandline */
209 resource_t *resource_top; /* The top of the parsed resources */
211 int getopt (int argc, char *const *argv, const char *optstring);
213 int main(int argc,char *argv[])
215 extern char* optarg;
216 extern int optind;
217 int optc;
218 int lose = 0;
219 int ret;
220 int a;
221 int i;
222 int cmdlen;
224 /* First rebuild the commandline to put in destination */
225 /* Could be done through env[], but not all OS-es support it */
226 cmdlen = 4; /* for "wrc " */
227 for(i = 1; i < argc; i++)
228 cmdlen += strlen(argv[i]) + 1;
229 cmdline = (char *)xmalloc(cmdlen);
230 strcpy(cmdline, "wrc ");
231 for(i = 1; i < argc; i++)
233 strcat(cmdline, argv[i]);
234 if(i < argc-1)
235 strcat(cmdline, " ");
238 while((optc = getopt(argc, argv, "a:AbcC:d:D:eghH:I:l:Lno:p:rstTVw:W")) != EOF)
240 switch(optc)
242 case 'a':
243 alignment = atoi(optarg);
244 break;
245 case 'A':
246 auto_register = 1;
247 break;
248 case 'b':
249 binary = 1;
250 break;
251 case 'c':
252 constant = 1;
253 break;
254 case 'C':
255 codepage = strtol(optarg, NULL, 0);
256 break;
257 case 'd':
258 debuglevel = strtol(optarg, NULL, 0);
259 break;
260 case 'D':
261 add_cmdline_define(optarg);
262 break;
263 case 'e':
264 extensions = 0;
265 break;
266 case 'g':
267 global = 1;
268 break;
269 case 'H':
270 header_name = strdup(optarg);
271 /* Fall through */
272 case 'h':
273 create_header = 1;
274 break;
275 case 'I':
276 add_include_path(optarg);
277 break;
278 case 'l':
280 int lan;
281 lan = strtol(optarg, NULL, 0);
282 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
284 break;
285 case 'L':
286 leave_case = 1;
287 break;
288 case 'n':
289 create_s = 0;
290 break;
291 case 'o':
292 output_name = strdup(optarg);
293 break;
294 case 'p':
295 #ifdef NEED_UNDERSCORE_PREFIX
296 prefix = (char *)xmalloc(strlen(optarg)+2);
297 prefix[0] = '_';
298 strcpy(prefix+1, optarg);
299 #else
300 prefix = xstrdup(optarg);
301 #endif
302 break;
303 case 'r':
304 create_res = 1;
305 break;
306 case 's':
307 create_dir = 1;
308 break;
309 case 'T':
310 indirect_only = 1;
311 /* Fall through */
312 case 't':
313 indirect = 1;
314 break;
315 case 'V':
316 printf(version_string);
317 exit(0);
318 break;
319 case 'w':
320 if(!strcmp(optarg, "16"))
321 win32 = 0;
322 else if(!strcmp(optarg, "32"))
323 win32 = 1;
324 else
325 lose++;
326 break;
327 case 'W':
328 pedantic = 1;
329 break;
330 default:
331 lose++;
332 break;
336 if(lose)
338 fprintf(stderr, usage);
339 return 1;
342 /* Check the command line options for invalid combinations */
343 if(win32)
345 if(!extensions)
347 warning("Option -e ignored with 32bit compile\n");
348 extensions = 1;
352 if(create_res)
354 if(constant)
356 warning("Option -c ignored with compile to .res\n");
357 constant = 0;
360 if(create_header)
362 warning("Option -[h|H] ignored with compile to .res\n");
363 create_header = 0;
366 if(indirect)
368 warning("Option -l ignored with compile to .res\n");
369 indirect = 0;
372 if(indirect_only)
374 warning("Option -L ignored with compile to .res\n");
375 indirect_only = 0;
378 if(global)
380 warning("Option -g ignored with compile to .res\n");
381 global = 0;
384 if(create_dir)
386 error("Option -r and -s cannot be used together\n");
389 if(binary)
391 error("Option -r and -b cannot be used together\n");
395 #if !defined(HAVE_WINE_CONSTRUCTOR)
396 if(auto_register)
398 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
399 auto_register = 0;
401 #endif
403 /* Set alignment power */
404 a = alignment;
405 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
407 a >>= 1;
409 if(a != 1)
411 error("Alignment must be between 1 and 1024");
413 if((1 << alignment_pwr) != alignment)
415 error("Alignment must be a power of 2");
418 /* Kill io buffering when some kind of debuglevel is enabled */
419 if(debuglevel)
421 setbuf(stdout,0);
422 setbuf(stderr,0);
425 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
427 /* Set the default defined stuff */
428 add_cmdline_define("RC_INVOKED=1");
429 add_cmdline_define("__WRC__=1");
430 if(win32)
432 add_cmdline_define("__WIN32__=1");
433 add_cmdline_define("__FLAT__=1");
436 /* Check if the user set a language, else set default */
437 if(!currentlanguage)
438 currentlanguage = new_language(0, 0);
440 /* Check for input file on command-line */
441 if(optind < argc)
443 input_name = argv[optind];
444 yyin = fopen(input_name, "rb");
445 if(!yyin)
447 error("Could not open %s\n", input_name);
450 else
452 yyin = stdin;
455 if(binary && !input_name)
457 error("Binary mode requires .res file as input");
460 if(!output_name)
462 output_name = dup_basename(input_name, binary ? ".res" : ".rc");
463 strcat(output_name, create_res ? ".res" : ".s");
466 if(!header_name && !create_res)
468 header_name = dup_basename(input_name, binary ? ".res" : ".rc");
469 strcat(header_name, ".h");
472 if(!binary)
474 /* Go from .rc to .res or .s */
475 chat("Starting parse");
476 ret = yyparse();
478 if(input_name)
479 fclose(yyin);
481 if(ret)
483 /* Error during parse */
484 exit(1);
487 if(debuglevel & DEBUGLEVEL_DUMP)
488 dump_resources(resource_top);
490 /* Convert the internal lists to binary data */
491 resources2res(resource_top);
493 if(create_res)
495 chat("Writing .res-file");
496 write_resfile(output_name, resource_top);
498 else
500 if(create_s)
502 chat("Writing .s-file");
503 write_s_file(output_name, resource_top);
505 if(create_header)
507 chat("Writing .h-file");
508 write_h_file(header_name, resource_top);
513 else
515 /* Go from .res to .s */
516 chat("Reading .res-file");
517 resource_top = read_resfile(input_name);
518 if(create_s)
520 chat("Writing .s-file");
521 write_s_file(output_name, resource_top);
523 if(create_header)
525 chat("Writing .h-file");
526 write_h_file(header_name, resource_top);
530 return 0;