kernel32/tests: Also test wrong architecture with matching 32/64 bitness.
[wine.git] / tools / make_xftmpl.c
blob6a22fefc5df8b82462087aa94b378f8f74f81d7c
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 ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
40 #define TOKEN_NAME 1
41 #define TOKEN_STRING 2
42 #define TOKEN_INTEGER 3
43 #define TOKEN_GUID 5
44 #define TOKEN_INTEGER_LIST 6
45 #define TOKEN_FLOAT_LIST 7
46 #define TOKEN_OBRACE 10
47 #define TOKEN_CBRACE 11
48 #define TOKEN_OPAREN 12
49 #define TOKEN_CPAREN 13
50 #define TOKEN_OBRACKET 14
51 #define TOKEN_CBRACKET 15
52 #define TOKEN_OANGLE 16
53 #define TOKEN_CANGLE 17
54 #define TOKEN_DOT 18
55 #define TOKEN_COMMA 19
56 #define TOKEN_SEMICOLON 20
57 #define TOKEN_TEMPLATE 31
58 #define TOKEN_WORD 40
59 #define TOKEN_DWORD 41
60 #define TOKEN_FLOAT 42
61 #define TOKEN_DOUBLE 43
62 #define TOKEN_CHAR 44
63 #define TOKEN_UCHAR 45
64 #define TOKEN_SWORD 46
65 #define TOKEN_SDWORD 47
66 #define TOKEN_VOID 48
67 #define TOKEN_LPSTR 49
68 #define TOKEN_UNICODE 50
69 #define TOKEN_CSTRING 51
70 #define TOKEN_ARRAY 52
72 struct keyword
74 const char *word;
75 WORD token;
78 static const struct keyword reserved_words[] = {
79 {"ARRAY", TOKEN_ARRAY},
80 {"CHAR", TOKEN_CHAR},
81 {"CSTRING", TOKEN_CSTRING},
82 {"DOUBLE", TOKEN_DOUBLE},
83 {"DWORD", TOKEN_DWORD},
84 {"FLOAT", TOKEN_FLOAT},
85 {"SDWORD", TOKEN_SDWORD},
86 {"STRING", TOKEN_LPSTR},
87 {"SWORD", TOKEN_SWORD},
88 {"TEMPLATE", TOKEN_TEMPLATE},
89 {"UCHAR", TOKEN_UCHAR},
90 {"UNICODE", TOKEN_UNICODE},
91 {"VOID", TOKEN_VOID},
92 {"WORD", TOKEN_WORD}
95 extern int getopt(int argc, char *const *argv, const char *optstring);
97 static BOOL option_header;
98 static char *option_inc_var_name = NULL;
99 static char *option_inc_size_name = NULL;
100 static const char *option_outfile_name = "-";
101 static char *program_name;
102 static FILE *infile;
103 static int line_no;
104 static const char *infile_name;
105 static FILE *outfile;
106 static BYTE *output_data;
107 static UINT output_pos, output_size;
109 #ifndef __GNUC__
110 #define __attribute__(x)
111 #endif
113 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
115 static void fatal_error( const char *msg, ... )
117 va_list valist;
118 va_start( valist, msg );
119 if (infile_name)
121 fprintf( stderr, "%s:%d:", infile_name, line_no );
122 fprintf( stderr, " error: " );
124 else fprintf( stderr, "%s: error: ", program_name );
125 vfprintf( stderr, msg, valist );
126 va_end( valist );
127 exit( 1 );
131 static inline BOOL read_byte( char *byte )
133 int c = fgetc(infile);
134 *byte = c;
135 if (c == '\n') line_no++;
136 return c != EOF;
139 static inline BOOL unread_byte( char last_byte )
141 if (last_byte == '\n') line_no--;
142 return ungetc(last_byte, infile) != EOF;
145 static inline BOOL read_bytes( void *data, DWORD size )
147 return fread(data, size, 1, infile) > 0;
150 static BOOL write_c_hex_bytes(void)
152 UINT i;
153 for (i = 0; i < output_pos; i++)
155 if (i % 12 == 0)
156 fprintf(outfile, "\n ");
157 fprintf(outfile, " 0x%02x,", output_data[i]);
159 return TRUE;
162 static BOOL write_raw_bytes(void)
164 return fwrite(output_data, output_pos, 1, outfile) > 0;
167 static inline BOOL write_bytes(const void *data, DWORD size)
169 if (output_pos + size > output_size)
171 output_size = max( output_size * 2, size );
172 output_data = realloc( output_data, output_size );
173 if (!output_data) return FALSE;
175 memcpy( output_data + output_pos, data, size );
176 output_pos += size;
177 return TRUE;
180 static inline BOOL write_byte(BYTE value)
182 return write_bytes( &value, sizeof(value) );
185 static inline BOOL write_word(WORD value)
187 return write_byte( value ) &&
188 write_byte( value >> 8 );
191 static inline BOOL write_dword(DWORD value)
193 return write_word( value ) &&
194 write_word( value >> 16 );
197 static inline BOOL write_float(float value)
199 DWORD val;
200 memcpy( &val, &value, sizeof(value) );
201 return write_dword( val );
204 static inline BOOL write_guid(const GUID *guid)
206 return write_dword( guid->Data1 ) &&
207 write_word( guid->Data2 ) &&
208 write_word( guid->Data3 ) &&
209 write_bytes( guid->Data4, sizeof(guid->Data4) );
212 static int compare_names(const void *a, const void *b)
214 return strcasecmp(*(const char **)a, *(const char **)b);
217 static BOOL parse_keyword( const char *name )
219 const struct keyword *keyword;
221 keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
222 sizeof(reserved_words[0]), compare_names);
223 if (!keyword)
224 return FALSE;
226 return write_word(keyword->token);
229 static BOOL parse_guid(void)
231 char buf[39];
232 GUID guid;
233 DWORD tab[10];
234 BOOL ret;
235 static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
237 buf[0] = '<';
238 if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
239 buf[38] = 0;
241 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);
242 if (ret != 11) fatal_error( "invalid GUID '%s'\n", buf );
244 guid.Data2 = tab[0];
245 guid.Data3 = tab[1];
246 guid.Data4[0] = tab[2];
247 guid.Data4[1] = tab[3];
248 guid.Data4[2] = tab[4];
249 guid.Data4[3] = tab[5];
250 guid.Data4[4] = tab[6];
251 guid.Data4[5] = tab[7];
252 guid.Data4[6] = tab[8];
253 guid.Data4[7] = tab[9];
255 return write_word(TOKEN_GUID) &&
256 write_guid(&guid);
259 static BOOL parse_name(void)
261 char c;
262 int len = 0;
263 char name[512];
265 while (read_byte(&c) && len < sizeof(name) &&
266 (isalnum(c) || c == '_' || c == '-'))
268 if (len + 1 < sizeof(name))
269 name[len++] = c;
271 unread_byte(c);
272 name[len] = 0;
274 if (parse_keyword(name)) {
275 return TRUE;
276 } else {
277 return write_word(TOKEN_NAME) &&
278 write_dword(len) &&
279 write_bytes(name, len);
283 static BOOL parse_number(void)
285 int len = 0;
286 char c;
287 char buffer[512];
288 BOOL dot = FALSE;
289 BOOL ret;
291 while (read_byte(&c) &&
292 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
294 if (len + 1 < sizeof(buffer))
295 buffer[len++] = c;
296 if (c == '.')
297 dot = TRUE;
299 unread_byte(c);
300 buffer[len] = 0;
302 if (dot) {
303 float value;
304 ret = sscanf(buffer, "%f", &value);
305 if (!ret) fatal_error( "invalid float token\n" );
306 ret = write_word(TOKEN_FLOAT) &&
307 write_float(value);
308 } else {
309 int value;
310 ret = sscanf(buffer, "%d", &value);
311 if (!ret) fatal_error( "invalid integer token\n" );
312 ret = write_word(TOKEN_INTEGER) &&
313 write_dword(value);
316 return ret;
319 static BOOL parse_token(void)
321 char c;
322 int len;
323 char *tok, buffer[512];
325 if (!read_byte(&c))
326 return FALSE;
328 switch (c)
330 case '\n':
331 case '\r':
332 case ' ':
333 case '\t':
334 return TRUE;
336 case '{': return write_word(TOKEN_OBRACE);
337 case '}': return write_word(TOKEN_CBRACE);
338 case '[': return write_word(TOKEN_OBRACKET);
339 case ']': return write_word(TOKEN_CBRACKET);
340 case '(': return write_word(TOKEN_OPAREN);
341 case ')': return write_word(TOKEN_CPAREN);
342 case ',': return write_word(TOKEN_COMMA);
343 case ';': return write_word(TOKEN_SEMICOLON);
344 case '.': return write_word(TOKEN_DOT);
346 case '/':
347 if (!read_byte(&c) || c != '/')
348 fatal_error( "invalid single '/' comment token\n" );
349 while (read_byte(&c) && c != '\n');
350 return c == '\n';
352 case '#':
353 len = 0;
354 while (read_byte(&c) && c != '\n')
355 if (len + 1 < sizeof(buffer)) buffer[len++] = c;
356 if (c != '\n') fatal_error( "line too long\n" );
357 buffer[len] = 0;
358 tok = strtok( buffer, " \t" );
359 if (!tok || strcmp( tok, "pragma" )) return TRUE;
360 tok = strtok( NULL, " \t" );
361 if (!tok || strcmp( tok, "xftmpl" )) return TRUE;
362 tok = strtok( NULL, " \t" );
363 if (!tok) return TRUE;
364 if (!strcmp( tok, "name" ))
366 tok = strtok( NULL, " \t" );
367 if (tok && !option_inc_var_name) option_inc_var_name = strdup( tok );
369 else if (!strcmp( tok, "size" ))
371 tok = strtok( NULL, " \t" );
372 if (tok && !option_inc_size_name) option_inc_size_name = strdup( tok );
374 return TRUE;
376 case '<':
377 return parse_guid();
379 case '"':
380 len = 0;
382 /* FIXME: Handle '\' (e.g. "valid\"string") */
383 while (read_byte(&c) && c != '"') {
384 if (len + 1 < sizeof(buffer))
385 buffer[len++] = c;
387 if (c != '"') fatal_error( "unterminated string\n" );
388 return write_word(TOKEN_STRING) &&
389 write_dword(len) &&
390 write_bytes(buffer, len);
392 default:
393 unread_byte(c);
394 if (isdigit(c) || c == '-')
395 return parse_number();
396 if (isalpha(c) || c == '_')
397 return parse_name();
398 fatal_error( "invalid character '%c' to start token\n", c );
401 return TRUE;
404 static const char *output_file;
406 static void cleanup_files(void)
408 if (output_file) unlink(output_file);
411 static void exit_on_signal( int sig )
413 exit(1); /* this will call the atexit functions */
416 static void usage(void)
418 fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
419 "Options:\n"
420 " -H Output to a c header file instead of a binary file\n"
421 " -i NAME Output to a c header file, data in variable NAME\n"
422 " -s NAME In a c header file, define NAME to be the data size\n"
423 " -o FILE Write output to FILE\n",
424 program_name);
427 static char **parse_options(int argc, char **argv)
429 int optc;
431 while ((optc = getopt(argc, argv, "hHi:o:s:")) != -1)
433 switch (optc)
435 case 'h':
436 usage();
437 exit(0);
438 case 'H':
439 option_header = TRUE;
440 break;
441 case 'i':
442 option_header = TRUE;
443 option_inc_var_name = strdup(optarg);
444 break;
445 case 'o':
446 option_outfile_name = strdup(optarg);
447 break;
448 case 's':
449 option_inc_size_name = strdup(optarg);
450 break;
453 return &argv[optind];
456 int main(int argc, char **argv)
458 char header[16];
459 char **args;
460 char *header_name = NULL;
462 program_name = argv[0];
464 args = parse_options(argc, argv);
465 infile_name = *args++;
466 if (!infile_name || *args)
468 usage();
469 return 1;
472 infile = stdin;
473 outfile = NULL;
475 if (!strcmp(infile_name, "-")) {
476 infile_name = "stdin";
477 } else if (!(infile = fopen(infile_name, "rb"))) {
478 perror(infile_name);
479 goto error;
482 if (!read_bytes(header, sizeof(header))) {
483 fprintf(stderr, "%s: Failed to read file header\n", program_name);
484 goto error;
486 if (strncmp(header, "xof ", 4))
488 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
489 goto error;
491 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
493 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
494 goto error;
496 if (strncmp(header + 8, "txt ", 4))
498 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
499 program_name);
500 goto error;
502 if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
504 fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
505 program_name, header + 12);
506 goto error;
509 if (!strcmp(option_outfile_name, "-")) {
510 option_outfile_name = "stdout";
511 outfile = stdout;
512 } else {
513 output_file = option_outfile_name;
514 atexit(cleanup_files);
515 signal(SIGTERM, exit_on_signal);
516 signal(SIGINT, exit_on_signal);
517 #ifdef SIGHUP
518 signal(SIGHUP, exit_on_signal);
519 #endif
520 if (!(outfile = fopen(output_file, "wb"))) {
521 perror(option_outfile_name);
522 goto error;
526 if (!write_bytes("xof 0302bin 0064", 16))
527 goto error;
529 line_no = 1;
530 while (parse_token());
532 if (ferror(outfile) || ferror(infile))
533 goto error;
535 if (option_header)
537 char *str_ptr;
539 if (!option_inc_var_name)
540 fatal_error( "variable name must be specified with -i or #pragma name\n" );
542 header_name = strrchr(option_outfile_name, '/');
543 if (header_name)
544 header_name = strdup(header_name + 1);
545 else
546 header_name = strdup(option_outfile_name);
547 if (!header_name) {
548 fprintf(stderr, "Out of memory\n");
549 goto error;
552 str_ptr = header_name;
553 while (*str_ptr) {
554 if (*str_ptr == '.')
555 *str_ptr = '_';
556 else
557 *str_ptr = toupper(*str_ptr);
558 str_ptr++;
561 fprintf(outfile,
562 "/* File generated automatically from %s; do not edit */\n"
563 "\n"
564 "#ifndef __WINE_%s\n"
565 "#define __WINE_%s\n"
566 "\n"
567 "unsigned char %s[] = {",
568 infile_name, header_name, header_name, option_inc_var_name);
569 write_c_hex_bytes();
570 fprintf(outfile, "\n};\n\n");
571 if (option_inc_size_name)
572 fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, output_pos);
573 fprintf(outfile, "#endif /* __WINE_%s */\n", header_name);
574 if (ferror(outfile))
575 goto error;
577 else write_raw_bytes();
579 fclose(infile);
580 fclose(outfile);
581 output_file = NULL;
583 return 0;
584 error:
585 if (infile) {
586 if (ferror(infile))
587 perror(infile_name);
588 fclose(infile);
590 if (outfile) {
591 if (ferror(outfile))
592 perror(option_outfile_name);
593 fclose(outfile);
595 return 1;