kernel32/tests/sync: Enable compilation with long types.
[wine.git] / tools / make_xftmpl.c
blobd707dc06cd28aa35f6db15924e3f8bcf8c24d0e1
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"
23 #include <signal.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "windef.h"
29 #include "guiddef.h"
30 #include "tools.h"
32 #define TOKEN_NAME 1
33 #define TOKEN_STRING 2
34 #define TOKEN_INTEGER 3
35 #define TOKEN_GUID 5
36 #define TOKEN_INTEGER_LIST 6
37 #define TOKEN_FLOAT_LIST 7
38 #define TOKEN_OBRACE 10
39 #define TOKEN_CBRACE 11
40 #define TOKEN_OPAREN 12
41 #define TOKEN_CPAREN 13
42 #define TOKEN_OBRACKET 14
43 #define TOKEN_CBRACKET 15
44 #define TOKEN_OANGLE 16
45 #define TOKEN_CANGLE 17
46 #define TOKEN_DOT 18
47 #define TOKEN_COMMA 19
48 #define TOKEN_SEMICOLON 20
49 #define TOKEN_TEMPLATE 31
50 #define TOKEN_WORD 40
51 #define TOKEN_DWORD 41
52 #define TOKEN_FLOAT 42
53 #define TOKEN_DOUBLE 43
54 #define TOKEN_CHAR 44
55 #define TOKEN_UCHAR 45
56 #define TOKEN_SWORD 46
57 #define TOKEN_SDWORD 47
58 #define TOKEN_VOID 48
59 #define TOKEN_LPSTR 49
60 #define TOKEN_UNICODE 50
61 #define TOKEN_CSTRING 51
62 #define TOKEN_ARRAY 52
64 struct keyword
66 const char *word;
67 WORD token;
70 static const struct keyword reserved_words[] = {
71 {"ARRAY", TOKEN_ARRAY},
72 {"CHAR", TOKEN_CHAR},
73 {"CSTRING", TOKEN_CSTRING},
74 {"DOUBLE", TOKEN_DOUBLE},
75 {"DWORD", TOKEN_DWORD},
76 {"FLOAT", TOKEN_FLOAT},
77 {"SDWORD", TOKEN_SDWORD},
78 {"STRING", TOKEN_LPSTR},
79 {"SWORD", TOKEN_SWORD},
80 {"TEMPLATE", TOKEN_TEMPLATE},
81 {"UCHAR", TOKEN_UCHAR},
82 {"UNICODE", TOKEN_UNICODE},
83 {"VOID", TOKEN_VOID},
84 {"WORD", TOKEN_WORD}
87 static BOOL option_header;
88 static char *option_inc_var_name = NULL;
89 static char *option_inc_size_name = NULL;
90 static const char *option_outfile_name = "-";
91 static char *program_name;
92 static FILE *infile;
93 static int line_no;
94 static const char *infile_name;
95 static FILE *outfile;
97 unsigned char *output_buffer = NULL;
98 size_t output_buffer_pos = 0;
99 size_t output_buffer_size = 0;
101 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
103 static void fatal_error( const char *msg, ... )
105 va_list valist;
106 va_start( valist, msg );
107 if (infile_name)
109 fprintf( stderr, "%s:%d:", infile_name, line_no );
110 fprintf( stderr, " error: " );
112 else fprintf( stderr, "%s: error: ", program_name );
113 vfprintf( stderr, msg, valist );
114 va_end( valist );
115 exit( 1 );
119 static inline BOOL read_byte( char *byte )
121 int c = fgetc(infile);
122 *byte = c;
123 if (c == '\n') line_no++;
124 return c != EOF;
127 static inline BOOL unread_byte( char last_byte )
129 if (last_byte == '\n') line_no--;
130 return ungetc(last_byte, infile) != EOF;
133 static inline BOOL read_bytes( void *data, DWORD size )
135 return fread(data, size, 1, infile) > 0;
138 static BOOL write_c_hex_bytes(void)
140 UINT i;
141 for (i = 0; i < output_buffer_pos; i++)
143 if (i % 12 == 0)
144 fprintf(outfile, "\n ");
145 fprintf(outfile, " 0x%02x,", output_buffer[i]);
147 return TRUE;
150 static BOOL write_raw_bytes(void)
152 return fwrite(output_buffer, output_buffer_pos, 1, outfile) > 0;
155 static inline void put_float(float value)
157 DWORD val;
158 memcpy( &val, &value, sizeof(value) );
159 return put_dword( val );
162 static inline void put_guid(const GUID *guid)
164 put_dword( guid->Data1 );
165 put_word( guid->Data2 );
166 put_word( guid->Data3 );
167 put_data( guid->Data4, sizeof(guid->Data4) );
170 static int compare_names(const void *a, const void *b)
172 return strcasecmp(*(const char **)a, *(const char **)b);
175 static BOOL parse_keyword( const char *name )
177 const struct keyword *keyword;
179 keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
180 sizeof(reserved_words[0]), compare_names);
181 if (!keyword)
182 return FALSE;
184 put_word(keyword->token);
185 return TRUE;
188 static void parse_guid(void)
190 char buf[39];
191 GUID guid;
192 DWORD tab[10];
193 BOOL ret;
194 static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
196 buf[0] = '<';
197 if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
198 buf[38] = 0;
200 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);
201 if (ret != 11) fatal_error( "invalid GUID '%s'\n", buf );
203 guid.Data2 = tab[0];
204 guid.Data3 = tab[1];
205 guid.Data4[0] = tab[2];
206 guid.Data4[1] = tab[3];
207 guid.Data4[2] = tab[4];
208 guid.Data4[3] = tab[5];
209 guid.Data4[4] = tab[6];
210 guid.Data4[5] = tab[7];
211 guid.Data4[6] = tab[8];
212 guid.Data4[7] = tab[9];
214 put_word(TOKEN_GUID);
215 put_guid(&guid);
218 static void parse_name(void)
220 char c;
221 int len = 0;
222 char name[512];
224 while (read_byte(&c) && len < sizeof(name) &&
225 (isalnum(c) || c == '_' || c == '-'))
227 if (len + 1 < sizeof(name))
228 name[len++] = c;
230 unread_byte(c);
231 name[len] = 0;
233 if (!parse_keyword(name))
235 put_word(TOKEN_NAME);
236 put_dword(len);
237 put_data(name, len);
241 static void parse_number(void)
243 int len = 0;
244 char c;
245 char buffer[512];
246 BOOL dot = FALSE;
247 BOOL ret;
249 while (read_byte(&c) &&
250 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
252 if (len + 1 < sizeof(buffer))
253 buffer[len++] = c;
254 if (c == '.')
255 dot = TRUE;
257 unread_byte(c);
258 buffer[len] = 0;
260 if (dot) {
261 float value;
262 ret = sscanf(buffer, "%f", &value);
263 if (!ret) fatal_error( "invalid float token\n" );
264 put_word(TOKEN_FLOAT);
265 put_float(value);
266 } else {
267 int value;
268 ret = sscanf(buffer, "%d", &value);
269 if (!ret) fatal_error( "invalid integer token\n" );
270 put_word(TOKEN_INTEGER);
271 put_dword(value);
275 static BOOL parse_token(void)
277 char c;
278 int len;
279 char *tok, buffer[512];
281 if (!read_byte(&c))
282 return FALSE;
284 switch (c)
286 case '\n':
287 case '\r':
288 case ' ':
289 case '\t':
290 break;
292 case '{': put_word(TOKEN_OBRACE); break;
293 case '}': put_word(TOKEN_CBRACE); break;
294 case '[': put_word(TOKEN_OBRACKET); break;
295 case ']': put_word(TOKEN_CBRACKET); break;
296 case '(': put_word(TOKEN_OPAREN); break;
297 case ')': put_word(TOKEN_CPAREN); break;
298 case ',': put_word(TOKEN_COMMA); break;
299 case ';': put_word(TOKEN_SEMICOLON); break;
300 case '.': put_word(TOKEN_DOT); break;
302 case '/':
303 if (!read_byte(&c) || c != '/')
304 fatal_error( "invalid single '/' comment token\n" );
305 while (read_byte(&c) && c != '\n');
306 return c == '\n';
308 case '#':
309 len = 0;
310 while (read_byte(&c) && c != '\n')
311 if (len + 1 < sizeof(buffer)) buffer[len++] = c;
312 if (c != '\n') fatal_error( "line too long\n" );
313 buffer[len] = 0;
314 tok = strtok( buffer, " \t" );
315 if (!tok || strcmp( tok, "pragma" )) break;
316 tok = strtok( NULL, " \t" );
317 if (!tok || strcmp( tok, "xftmpl" )) break;
318 tok = strtok( NULL, " \t" );
319 if (!tok) break;
320 if (!strcmp( tok, "name" ))
322 tok = strtok( NULL, " \t" );
323 if (tok && !option_inc_var_name) option_inc_var_name = xstrdup( tok );
325 else if (!strcmp( tok, "size" ))
327 tok = strtok( NULL, " \t" );
328 if (tok && !option_inc_size_name) option_inc_size_name = xstrdup( tok );
330 break;
332 case '<':
333 parse_guid();
334 break;
336 case '"':
337 len = 0;
339 /* FIXME: Handle '\' (e.g. "valid\"string") */
340 while (read_byte(&c) && c != '"') {
341 if (len + 1 < sizeof(buffer))
342 buffer[len++] = c;
344 if (c != '"') fatal_error( "unterminated string\n" );
345 put_word(TOKEN_STRING);
346 put_dword(len);
347 put_data(buffer, len);
348 break;
350 default:
351 unread_byte(c);
352 if (isdigit(c) || c == '-') parse_number();
353 else if (isalpha(c) || c == '_') parse_name();
354 else fatal_error( "invalid character '%c' to start token\n", c );
357 return TRUE;
360 static const char *output_file;
362 static void cleanup_files(void)
364 if (output_file) unlink(output_file);
367 static void exit_on_signal( int sig )
369 exit(1); /* this will call the atexit functions */
372 static void usage(void)
374 fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
375 "Options:\n"
376 " -H Output to a c header file instead of a binary file\n"
377 " -i NAME Output to a c header file, data in variable NAME\n"
378 " -s NAME In a c header file, define NAME to be the data size\n"
379 " -o FILE Write output to FILE\n",
380 program_name);
383 static void option_callback( int optc, char *optarg )
385 switch (optc)
387 case 'h':
388 usage();
389 exit(0);
390 case 'H':
391 option_header = TRUE;
392 break;
393 case 'i':
394 option_header = TRUE;
395 option_inc_var_name = xstrdup(optarg);
396 break;
397 case 'o':
398 option_outfile_name = xstrdup(optarg);
399 break;
400 case 's':
401 option_inc_size_name = xstrdup(optarg);
402 break;
403 case '?':
404 fprintf( stderr, "%s: %s\n", program_name, optarg );
405 exit(1);
409 int main(int argc, char **argv)
411 char header[16];
412 struct strarray args;
413 char *header_name = NULL;
415 program_name = argv[0];
417 args = parse_options(argc, argv, "hHi:o:s:", NULL, 0, option_callback );
418 if (!args.count)
420 usage();
421 return 1;
423 infile_name = args.str[0];
425 infile = stdin;
426 outfile = NULL;
428 if (!strcmp(infile_name, "-")) {
429 infile_name = "stdin";
430 } else if (!(infile = fopen(infile_name, "rb"))) {
431 perror(infile_name);
432 goto error;
435 if (!read_bytes(header, sizeof(header))) {
436 fprintf(stderr, "%s: Failed to read file header\n", program_name);
437 goto error;
439 if (strncmp(header, "xof ", 4))
441 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
442 goto error;
444 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
446 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
447 goto error;
449 if (strncmp(header + 8, "txt ", 4))
451 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
452 program_name);
453 goto error;
455 if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
457 fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
458 program_name, header + 12);
459 goto error;
462 init_output_buffer();
463 put_data("xof 0302bin 0064", 16);
465 line_no = 1;
466 while (parse_token());
468 if (ferror(infile))
470 perror(infile_name);
471 return 1;
473 fclose(infile);
475 if (!strcmp(option_outfile_name, "-")) {
476 option_outfile_name = "stdout";
477 outfile = stdout;
478 } else {
479 output_file = option_outfile_name;
480 atexit(cleanup_files);
481 signal(SIGTERM, exit_on_signal);
482 signal(SIGINT, exit_on_signal);
483 #ifdef SIGHUP
484 signal(SIGHUP, exit_on_signal);
485 #endif
486 if (!(outfile = fopen(output_file, "wb"))) {
487 perror(option_outfile_name);
488 goto error;
492 if (ferror(outfile))
493 goto error;
495 if (option_header)
497 char *str_ptr;
499 if (!option_inc_var_name)
500 fatal_error( "variable name must be specified with -i or #pragma name\n" );
502 header_name = get_basename( option_outfile_name );
503 str_ptr = header_name;
504 while (*str_ptr) {
505 if (*str_ptr == '.')
506 *str_ptr = '_';
507 else
508 *str_ptr = toupper(*str_ptr);
509 str_ptr++;
512 fprintf(outfile,
513 "/* File generated automatically from %s; do not edit */\n"
514 "\n"
515 "#ifndef __WINE_%s\n"
516 "#define __WINE_%s\n"
517 "\n"
518 "unsigned char %s[] = {",
519 infile_name, header_name, header_name, option_inc_var_name);
520 write_c_hex_bytes();
521 fprintf(outfile, "\n};\n\n");
522 if (option_inc_size_name)
523 fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, (unsigned int)output_buffer_pos);
524 fprintf(outfile, "#endif /* __WINE_%s */\n", header_name);
525 if (ferror(outfile))
526 goto error;
528 else write_raw_bytes();
530 fclose(outfile);
531 output_file = NULL;
533 return 0;
534 error:
535 if (outfile) {
536 if (ferror(outfile))
537 perror(option_outfile_name);
538 fclose(outfile);
540 return 1;