ieframe: Use nameless unions.
[wine/zf.git] / tools / make_xftmpl.c
blob49a1273bad4aa40d7e4df7a267eaa4e8e79506e0
1 /*
2 * Binary encode X templates from text format.
4 * Copyright 2011 Dylan Smith
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_GETOPT_H
29 # include <getopt.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #include "windef.h"
36 #include "guiddef.h"
38 #define TOKEN_NAME 1
39 #define TOKEN_STRING 2
40 #define TOKEN_INTEGER 3
41 #define TOKEN_GUID 5
42 #define TOKEN_INTEGER_LIST 6
43 #define TOKEN_FLOAT_LIST 7
44 #define TOKEN_OBRACE 10
45 #define TOKEN_CBRACE 11
46 #define TOKEN_OPAREN 12
47 #define TOKEN_CPAREN 13
48 #define TOKEN_OBRACKET 14
49 #define TOKEN_CBRACKET 15
50 #define TOKEN_OANGLE 16
51 #define TOKEN_CANGLE 17
52 #define TOKEN_DOT 18
53 #define TOKEN_COMMA 19
54 #define TOKEN_SEMICOLON 20
55 #define TOKEN_TEMPLATE 31
56 #define TOKEN_WORD 40
57 #define TOKEN_DWORD 41
58 #define TOKEN_FLOAT 42
59 #define TOKEN_DOUBLE 43
60 #define TOKEN_CHAR 44
61 #define TOKEN_UCHAR 45
62 #define TOKEN_SWORD 46
63 #define TOKEN_SDWORD 47
64 #define TOKEN_VOID 48
65 #define TOKEN_LPSTR 49
66 #define TOKEN_UNICODE 50
67 #define TOKEN_CSTRING 51
68 #define TOKEN_ARRAY 52
70 struct keyword
72 const char *word;
73 WORD token;
76 static const struct keyword reserved_words[] = {
77 {"ARRAY", TOKEN_ARRAY},
78 {"CHAR", TOKEN_CHAR},
79 {"CSTRING", TOKEN_CSTRING},
80 {"DOUBLE", TOKEN_DOUBLE},
81 {"DWORD", TOKEN_DWORD},
82 {"FLOAT", TOKEN_FLOAT},
83 {"SDWORD", TOKEN_SDWORD},
84 {"STRING", TOKEN_LPSTR},
85 {"SWORD", TOKEN_SWORD},
86 {"TEMPLATE", TOKEN_TEMPLATE},
87 {"UCHAR", TOKEN_UCHAR},
88 {"UNICODE", TOKEN_UNICODE},
89 {"VOID", TOKEN_VOID},
90 {"WORD", TOKEN_WORD}
93 extern int getopt(int argc, char *const *argv, const char *optstring);
95 static BOOL option_header;
96 static char *option_inc_var_name = NULL;
97 static char *option_inc_size_name = NULL;
98 static const char *option_outfile_name = "-";
99 static char *program_name;
100 static FILE *infile;
101 static int line_no;
102 static const char *infile_name;
103 static FILE *outfile;
104 static BYTE *output_data;
105 static UINT output_pos, output_size;
107 #ifndef __GNUC__
108 #define __attribute__(x)
109 #endif
111 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
113 static void fatal_error( const char *msg, ... )
115 va_list valist;
116 va_start( valist, msg );
117 if (infile_name)
119 fprintf( stderr, "%s:%d:", infile_name, line_no );
120 fprintf( stderr, " error: " );
122 else fprintf( stderr, "%s: error: ", program_name );
123 vfprintf( stderr, msg, valist );
124 va_end( valist );
125 exit( 1 );
129 static inline BOOL read_byte( char *byte )
131 int c = fgetc(infile);
132 *byte = c;
133 if (c == '\n') line_no++;
134 return c != EOF;
137 static inline BOOL unread_byte( char last_byte )
139 if (last_byte == '\n') line_no--;
140 return ungetc(last_byte, infile) != EOF;
143 static inline BOOL read_bytes( void *data, DWORD size )
145 return fread(data, size, 1, infile) > 0;
148 static BOOL write_c_hex_bytes(void)
150 UINT i;
151 for (i = 0; i < output_pos; i++)
153 if (i % 12 == 0)
154 fprintf(outfile, "\n ");
155 fprintf(outfile, " 0x%02x,", output_data[i]);
157 return TRUE;
160 static BOOL write_raw_bytes(void)
162 return fwrite(output_data, output_pos, 1, outfile) > 0;
165 static inline BOOL write_bytes(const void *data, DWORD size)
167 if (output_pos + size > output_size)
169 output_size = max( output_size * 2, size );
170 output_data = realloc( output_data, output_size );
171 if (!output_data) return FALSE;
173 memcpy( output_data + output_pos, data, size );
174 output_pos += size;
175 return TRUE;
178 static inline BOOL write_byte(BYTE value)
180 return write_bytes( &value, sizeof(value) );
183 static inline BOOL write_word(WORD value)
185 return write_byte( value ) &&
186 write_byte( value >> 8 );
189 static inline BOOL write_dword(DWORD value)
191 return write_word( value ) &&
192 write_word( value >> 16 );
195 static inline BOOL write_float(float value)
197 DWORD val;
198 memcpy( &val, &value, sizeof(value) );
199 return write_dword( val );
202 static inline BOOL write_guid(const GUID *guid)
204 return write_dword( guid->Data1 ) &&
205 write_word( guid->Data2 ) &&
206 write_word( guid->Data3 ) &&
207 write_bytes( guid->Data4, sizeof(guid->Data4) );
210 static int compare_names(const void *a, const void *b)
212 return strcasecmp(*(const char **)a, *(const char **)b);
215 static BOOL parse_keyword( const char *name )
217 const struct keyword *keyword;
219 keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
220 sizeof(reserved_words[0]), compare_names);
221 if (!keyword)
222 return FALSE;
224 return write_word(keyword->token);
227 static BOOL parse_guid(void)
229 char buf[39];
230 GUID guid;
231 DWORD tab[10];
232 BOOL ret;
233 static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
235 buf[0] = '<';
236 if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
237 buf[38] = 0;
239 ret = sscanf(buf, guidfmt, &guid.Data1, tab, tab+1, tab+2, tab+3, tab+4, tab+5, tab+6, tab+7, tab+8, tab+9);
240 if (ret != 11) fatal_error( "invalid GUID '%s'\n", buf );
242 guid.Data2 = tab[0];
243 guid.Data3 = tab[1];
244 guid.Data4[0] = tab[2];
245 guid.Data4[1] = tab[3];
246 guid.Data4[2] = tab[4];
247 guid.Data4[3] = tab[5];
248 guid.Data4[4] = tab[6];
249 guid.Data4[5] = tab[7];
250 guid.Data4[6] = tab[8];
251 guid.Data4[7] = tab[9];
253 return write_word(TOKEN_GUID) &&
254 write_guid(&guid);
257 static BOOL parse_name(void)
259 char c;
260 int len = 0;
261 char name[512];
263 while (read_byte(&c) && len < sizeof(name) &&
264 (isalnum(c) || c == '_' || c == '-'))
266 if (len + 1 < sizeof(name))
267 name[len++] = c;
269 unread_byte(c);
270 name[len] = 0;
272 if (parse_keyword(name)) {
273 return TRUE;
274 } else {
275 return write_word(TOKEN_NAME) &&
276 write_dword(len) &&
277 write_bytes(name, len);
281 static BOOL parse_number(void)
283 int len = 0;
284 char c;
285 char buffer[512];
286 BOOL dot = FALSE;
287 BOOL ret;
289 while (read_byte(&c) &&
290 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
292 if (len + 1 < sizeof(buffer))
293 buffer[len++] = c;
294 if (c == '.')
295 dot = TRUE;
297 unread_byte(c);
298 buffer[len] = 0;
300 if (dot) {
301 float value;
302 ret = sscanf(buffer, "%f", &value);
303 if (!ret) fatal_error( "invalid float token\n" );
304 ret = write_word(TOKEN_FLOAT) &&
305 write_float(value);
306 } else {
307 int value;
308 ret = sscanf(buffer, "%d", &value);
309 if (!ret) fatal_error( "invalid integer token\n" );
310 ret = write_word(TOKEN_INTEGER) &&
311 write_dword(value);
314 return ret;
317 static BOOL parse_token(void)
319 char c;
320 int len;
321 char *tok, buffer[512];
323 if (!read_byte(&c))
324 return FALSE;
326 switch (c)
328 case '\n':
329 case '\r':
330 case ' ':
331 case '\t':
332 return TRUE;
334 case '{': return write_word(TOKEN_OBRACE);
335 case '}': return write_word(TOKEN_CBRACE);
336 case '[': return write_word(TOKEN_OBRACKET);
337 case ']': return write_word(TOKEN_CBRACKET);
338 case '(': return write_word(TOKEN_OPAREN);
339 case ')': return write_word(TOKEN_CPAREN);
340 case ',': return write_word(TOKEN_COMMA);
341 case ';': return write_word(TOKEN_SEMICOLON);
342 case '.': return write_word(TOKEN_DOT);
344 case '/':
345 if (!read_byte(&c) || c != '/')
346 fatal_error( "invalid single '/' comment token\n" );
347 while (read_byte(&c) && c != '\n');
348 return c == '\n';
350 case '#':
351 len = 0;
352 while (read_byte(&c) && c != '\n')
353 if (len + 1 < sizeof(buffer)) buffer[len++] = c;
354 if (c != '\n') fatal_error( "line too long\n" );
355 buffer[len] = 0;
356 tok = strtok( buffer, " \t" );
357 if (!tok || strcmp( tok, "pragma" )) return TRUE;
358 tok = strtok( NULL, " \t" );
359 if (!tok || strcmp( tok, "xftmpl" )) return TRUE;
360 tok = strtok( NULL, " \t" );
361 if (!tok) return TRUE;
362 if (!strcmp( tok, "name" ))
364 tok = strtok( NULL, " \t" );
365 if (tok && !option_inc_var_name) option_inc_var_name = strdup( tok );
367 else if (!strcmp( tok, "size" ))
369 tok = strtok( NULL, " \t" );
370 if (tok && !option_inc_size_name) option_inc_size_name = strdup( tok );
372 return TRUE;
374 case '<':
375 return parse_guid();
377 case '"':
378 len = 0;
380 /* FIXME: Handle '\' (e.g. "valid\"string") */
381 while (read_byte(&c) && c != '"') {
382 if (len + 1 < sizeof(buffer))
383 buffer[len++] = c;
385 if (c != '"') fatal_error( "unterminated string\n" );
386 return write_word(TOKEN_STRING) &&
387 write_dword(len) &&
388 write_bytes(buffer, len);
390 default:
391 unread_byte(c);
392 if (isdigit(c) || c == '-')
393 return parse_number();
394 if (isalpha(c) || c == '_')
395 return parse_name();
396 fatal_error( "invalid character '%c' to start token\n", c );
399 return TRUE;
402 static const char *output_file;
404 static void cleanup_files(void)
406 if (output_file) unlink(output_file);
409 static void exit_on_signal( int sig )
411 exit(1); /* this will call the atexit functions */
414 static void usage(void)
416 fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
417 "Options:\n"
418 " -H Output to a c header file instead of a binary file\n"
419 " -i NAME Output to a c header file, data in variable NAME\n"
420 " -s NAME In a c header file, define NAME to be the data size\n"
421 " -o FILE Write output to FILE\n",
422 program_name);
425 static char **parse_options(int argc, char **argv)
427 int optc;
429 while ((optc = getopt(argc, argv, "hHi:o:s:")) != -1)
431 switch (optc)
433 case 'h':
434 usage();
435 exit(0);
436 case 'H':
437 option_header = TRUE;
438 break;
439 case 'i':
440 option_header = TRUE;
441 option_inc_var_name = strdup(optarg);
442 break;
443 case 'o':
444 option_outfile_name = strdup(optarg);
445 break;
446 case 's':
447 option_inc_size_name = strdup(optarg);
448 break;
451 return &argv[optind];
454 int main(int argc, char **argv)
456 char header[16];
457 char **args;
458 char *header_name = NULL;
460 program_name = argv[0];
462 args = parse_options(argc, argv);
463 infile_name = *args++;
464 if (!infile_name || *args)
466 usage();
467 return 1;
470 infile = stdin;
471 outfile = NULL;
473 if (!strcmp(infile_name, "-")) {
474 infile_name = "stdin";
475 } else if (!(infile = fopen(infile_name, "rb"))) {
476 perror(infile_name);
477 goto error;
480 if (!read_bytes(header, sizeof(header))) {
481 fprintf(stderr, "%s: Failed to read file header\n", program_name);
482 goto error;
484 if (strncmp(header, "xof ", 4))
486 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
487 goto error;
489 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
491 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
492 goto error;
494 if (strncmp(header + 8, "txt ", 4))
496 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
497 program_name);
498 goto error;
500 if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
502 fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
503 program_name, header + 12);
504 goto error;
507 if (!strcmp(option_outfile_name, "-")) {
508 option_outfile_name = "stdout";
509 outfile = stdout;
510 } else {
511 output_file = option_outfile_name;
512 atexit(cleanup_files);
513 signal(SIGTERM, exit_on_signal);
514 signal(SIGINT, exit_on_signal);
515 #ifdef SIGHUP
516 signal(SIGHUP, exit_on_signal);
517 #endif
518 if (!(outfile = fopen(output_file, "wb"))) {
519 perror(option_outfile_name);
520 goto error;
524 if (!write_bytes("xof 0302bin 0064", 16))
525 goto error;
527 line_no = 1;
528 while (parse_token());
530 if (ferror(outfile) || ferror(infile))
531 goto error;
533 if (option_header)
535 char *str_ptr;
537 if (!option_inc_var_name)
538 fatal_error( "variable name must be specified with -i or #pragma name\n" );
540 header_name = strrchr(option_outfile_name, '/');
541 if (header_name)
542 header_name = strdup(header_name + 1);
543 else
544 header_name = strdup(option_outfile_name);
545 if (!header_name) {
546 fprintf(stderr, "Out of memory\n");
547 goto error;
550 str_ptr = header_name;
551 while (*str_ptr) {
552 if (*str_ptr == '.')
553 *str_ptr = '_';
554 else
555 *str_ptr = toupper(*str_ptr);
556 str_ptr++;
559 fprintf(outfile,
560 "/* File generated automatically from %s; do not edit */\n"
561 "\n"
562 "#ifndef __WINE_%s\n"
563 "#define __WINE_%s\n"
564 "\n"
565 "unsigned char %s[] = {",
566 infile_name, header_name, header_name, option_inc_var_name);
567 write_c_hex_bytes();
568 fprintf(outfile, "\n};\n\n");
569 if (option_inc_size_name)
570 fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, output_pos);
571 fprintf(outfile, "#endif /* __WINE_%s */\n", header_name);
572 if (ferror(outfile))
573 goto error;
575 else write_raw_bytes();
577 fclose(infile);
578 fclose(outfile);
579 output_file = NULL;
581 return 0;
582 error:
583 if (infile) {
584 if (ferror(infile))
585 perror(infile_name);
586 fclose(infile);
588 if (outfile) {
589 if (ferror(outfile))
590 perror(option_outfile_name);
591 fclose(outfile);
593 return 1;