d2d1: Create feature level 10.0 device context state objects.
[wine.git] / tools / make_xftmpl.c
blobb7f964c28d777f6a328cee11614912cdebdf50e3
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;
96 static BYTE *output_data;
97 static UINT output_pos, output_size;
99 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
101 static void fatal_error( const char *msg, ... )
103 va_list valist;
104 va_start( valist, msg );
105 if (infile_name)
107 fprintf( stderr, "%s:%d:", infile_name, line_no );
108 fprintf( stderr, " error: " );
110 else fprintf( stderr, "%s: error: ", program_name );
111 vfprintf( stderr, msg, valist );
112 va_end( valist );
113 exit( 1 );
117 static inline BOOL read_byte( char *byte )
119 int c = fgetc(infile);
120 *byte = c;
121 if (c == '\n') line_no++;
122 return c != EOF;
125 static inline BOOL unread_byte( char last_byte )
127 if (last_byte == '\n') line_no--;
128 return ungetc(last_byte, infile) != EOF;
131 static inline BOOL read_bytes( void *data, DWORD size )
133 return fread(data, size, 1, infile) > 0;
136 static BOOL write_c_hex_bytes(void)
138 UINT i;
139 for (i = 0; i < output_pos; i++)
141 if (i % 12 == 0)
142 fprintf(outfile, "\n ");
143 fprintf(outfile, " 0x%02x,", output_data[i]);
145 return TRUE;
148 static BOOL write_raw_bytes(void)
150 return fwrite(output_data, output_pos, 1, outfile) > 0;
153 static inline BOOL write_bytes(const void *data, DWORD size)
155 if (output_pos + size > output_size)
157 output_size = max( output_size * 2, size );
158 output_data = realloc( output_data, output_size );
159 if (!output_data) return FALSE;
161 memcpy( output_data + output_pos, data, size );
162 output_pos += size;
163 return TRUE;
166 static inline BOOL write_byte(BYTE value)
168 return write_bytes( &value, sizeof(value) );
171 static inline BOOL write_word(WORD value)
173 return write_byte( value ) &&
174 write_byte( value >> 8 );
177 static inline BOOL write_dword(DWORD value)
179 return write_word( value ) &&
180 write_word( value >> 16 );
183 static inline BOOL write_float(float value)
185 DWORD val;
186 memcpy( &val, &value, sizeof(value) );
187 return write_dword( val );
190 static inline BOOL write_guid(const GUID *guid)
192 return write_dword( guid->Data1 ) &&
193 write_word( guid->Data2 ) &&
194 write_word( guid->Data3 ) &&
195 write_bytes( guid->Data4, sizeof(guid->Data4) );
198 static int compare_names(const void *a, const void *b)
200 return strcasecmp(*(const char **)a, *(const char **)b);
203 static BOOL parse_keyword( const char *name )
205 const struct keyword *keyword;
207 keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
208 sizeof(reserved_words[0]), compare_names);
209 if (!keyword)
210 return FALSE;
212 return write_word(keyword->token);
215 static BOOL parse_guid(void)
217 char buf[39];
218 GUID guid;
219 DWORD tab[10];
220 BOOL ret;
221 static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
223 buf[0] = '<';
224 if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
225 buf[38] = 0;
227 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);
228 if (ret != 11) fatal_error( "invalid GUID '%s'\n", buf );
230 guid.Data2 = tab[0];
231 guid.Data3 = tab[1];
232 guid.Data4[0] = tab[2];
233 guid.Data4[1] = tab[3];
234 guid.Data4[2] = tab[4];
235 guid.Data4[3] = tab[5];
236 guid.Data4[4] = tab[6];
237 guid.Data4[5] = tab[7];
238 guid.Data4[6] = tab[8];
239 guid.Data4[7] = tab[9];
241 return write_word(TOKEN_GUID) &&
242 write_guid(&guid);
245 static BOOL parse_name(void)
247 char c;
248 int len = 0;
249 char name[512];
251 while (read_byte(&c) && len < sizeof(name) &&
252 (isalnum(c) || c == '_' || c == '-'))
254 if (len + 1 < sizeof(name))
255 name[len++] = c;
257 unread_byte(c);
258 name[len] = 0;
260 if (parse_keyword(name)) {
261 return TRUE;
262 } else {
263 return write_word(TOKEN_NAME) &&
264 write_dword(len) &&
265 write_bytes(name, len);
269 static BOOL parse_number(void)
271 int len = 0;
272 char c;
273 char buffer[512];
274 BOOL dot = FALSE;
275 BOOL ret;
277 while (read_byte(&c) &&
278 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
280 if (len + 1 < sizeof(buffer))
281 buffer[len++] = c;
282 if (c == '.')
283 dot = TRUE;
285 unread_byte(c);
286 buffer[len] = 0;
288 if (dot) {
289 float value;
290 ret = sscanf(buffer, "%f", &value);
291 if (!ret) fatal_error( "invalid float token\n" );
292 ret = write_word(TOKEN_FLOAT) &&
293 write_float(value);
294 } else {
295 int value;
296 ret = sscanf(buffer, "%d", &value);
297 if (!ret) fatal_error( "invalid integer token\n" );
298 ret = write_word(TOKEN_INTEGER) &&
299 write_dword(value);
302 return ret;
305 static BOOL parse_token(void)
307 char c;
308 int len;
309 char *tok, buffer[512];
311 if (!read_byte(&c))
312 return FALSE;
314 switch (c)
316 case '\n':
317 case '\r':
318 case ' ':
319 case '\t':
320 return TRUE;
322 case '{': return write_word(TOKEN_OBRACE);
323 case '}': return write_word(TOKEN_CBRACE);
324 case '[': return write_word(TOKEN_OBRACKET);
325 case ']': return write_word(TOKEN_CBRACKET);
326 case '(': return write_word(TOKEN_OPAREN);
327 case ')': return write_word(TOKEN_CPAREN);
328 case ',': return write_word(TOKEN_COMMA);
329 case ';': return write_word(TOKEN_SEMICOLON);
330 case '.': return write_word(TOKEN_DOT);
332 case '/':
333 if (!read_byte(&c) || c != '/')
334 fatal_error( "invalid single '/' comment token\n" );
335 while (read_byte(&c) && c != '\n');
336 return c == '\n';
338 case '#':
339 len = 0;
340 while (read_byte(&c) && c != '\n')
341 if (len + 1 < sizeof(buffer)) buffer[len++] = c;
342 if (c != '\n') fatal_error( "line too long\n" );
343 buffer[len] = 0;
344 tok = strtok( buffer, " \t" );
345 if (!tok || strcmp( tok, "pragma" )) return TRUE;
346 tok = strtok( NULL, " \t" );
347 if (!tok || strcmp( tok, "xftmpl" )) return TRUE;
348 tok = strtok( NULL, " \t" );
349 if (!tok) return TRUE;
350 if (!strcmp( tok, "name" ))
352 tok = strtok( NULL, " \t" );
353 if (tok && !option_inc_var_name) option_inc_var_name = xstrdup( tok );
355 else if (!strcmp( tok, "size" ))
357 tok = strtok( NULL, " \t" );
358 if (tok && !option_inc_size_name) option_inc_size_name = xstrdup( tok );
360 return TRUE;
362 case '<':
363 return parse_guid();
365 case '"':
366 len = 0;
368 /* FIXME: Handle '\' (e.g. "valid\"string") */
369 while (read_byte(&c) && c != '"') {
370 if (len + 1 < sizeof(buffer))
371 buffer[len++] = c;
373 if (c != '"') fatal_error( "unterminated string\n" );
374 return write_word(TOKEN_STRING) &&
375 write_dword(len) &&
376 write_bytes(buffer, len);
378 default:
379 unread_byte(c);
380 if (isdigit(c) || c == '-')
381 return parse_number();
382 if (isalpha(c) || c == '_')
383 return parse_name();
384 fatal_error( "invalid character '%c' to start token\n", c );
387 return TRUE;
390 static const char *output_file;
392 static void cleanup_files(void)
394 if (output_file) unlink(output_file);
397 static void exit_on_signal( int sig )
399 exit(1); /* this will call the atexit functions */
402 static void usage(void)
404 fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
405 "Options:\n"
406 " -H Output to a c header file instead of a binary file\n"
407 " -i NAME Output to a c header file, data in variable NAME\n"
408 " -s NAME In a c header file, define NAME to be the data size\n"
409 " -o FILE Write output to FILE\n",
410 program_name);
413 static void option_callback( int optc, char *optarg )
415 switch (optc)
417 case 'h':
418 usage();
419 exit(0);
420 case 'H':
421 option_header = TRUE;
422 break;
423 case 'i':
424 option_header = TRUE;
425 option_inc_var_name = xstrdup(optarg);
426 break;
427 case 'o':
428 option_outfile_name = xstrdup(optarg);
429 break;
430 case 's':
431 option_inc_size_name = xstrdup(optarg);
432 break;
433 case '?':
434 fprintf( stderr, "%s: %s\n", program_name, optarg );
435 exit(1);
439 int main(int argc, char **argv)
441 char header[16];
442 struct strarray args;
443 char *header_name = NULL;
445 program_name = argv[0];
447 args = parse_options(argc, argv, "hHi:o:s:", NULL, 0, option_callback );
448 if (!args.count)
450 usage();
451 return 1;
453 infile_name = args.str[0];
455 infile = stdin;
456 outfile = NULL;
458 if (!strcmp(infile_name, "-")) {
459 infile_name = "stdin";
460 } else if (!(infile = fopen(infile_name, "rb"))) {
461 perror(infile_name);
462 goto error;
465 if (!read_bytes(header, sizeof(header))) {
466 fprintf(stderr, "%s: Failed to read file header\n", program_name);
467 goto error;
469 if (strncmp(header, "xof ", 4))
471 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
472 goto error;
474 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
476 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
477 goto error;
479 if (strncmp(header + 8, "txt ", 4))
481 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
482 program_name);
483 goto error;
485 if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
487 fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
488 program_name, header + 12);
489 goto error;
492 if (!strcmp(option_outfile_name, "-")) {
493 option_outfile_name = "stdout";
494 outfile = stdout;
495 } else {
496 output_file = option_outfile_name;
497 atexit(cleanup_files);
498 signal(SIGTERM, exit_on_signal);
499 signal(SIGINT, exit_on_signal);
500 #ifdef SIGHUP
501 signal(SIGHUP, exit_on_signal);
502 #endif
503 if (!(outfile = fopen(output_file, "wb"))) {
504 perror(option_outfile_name);
505 goto error;
509 if (!write_bytes("xof 0302bin 0064", 16))
510 goto error;
512 line_no = 1;
513 while (parse_token());
515 if (ferror(outfile) || ferror(infile))
516 goto error;
518 if (option_header)
520 char *str_ptr;
522 if (!option_inc_var_name)
523 fatal_error( "variable name must be specified with -i or #pragma name\n" );
525 header_name = get_basename( option_outfile_name );
526 str_ptr = header_name;
527 while (*str_ptr) {
528 if (*str_ptr == '.')
529 *str_ptr = '_';
530 else
531 *str_ptr = toupper(*str_ptr);
532 str_ptr++;
535 fprintf(outfile,
536 "/* File generated automatically from %s; do not edit */\n"
537 "\n"
538 "#ifndef __WINE_%s\n"
539 "#define __WINE_%s\n"
540 "\n"
541 "unsigned char %s[] = {",
542 infile_name, header_name, header_name, option_inc_var_name);
543 write_c_hex_bytes();
544 fprintf(outfile, "\n};\n\n");
545 if (option_inc_size_name)
546 fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, output_pos);
547 fprintf(outfile, "#endif /* __WINE_%s */\n", header_name);
548 if (ferror(outfile))
549 goto error;
551 else write_raw_bytes();
553 fclose(infile);
554 fclose(outfile);
555 output_file = NULL;
557 return 0;
558 error:
559 if (infile) {
560 if (ferror(infile))
561 perror(infile_name);
562 fclose(infile);
564 if (outfile) {
565 if (ferror(outfile))
566 perror(option_outfile_name);
567 fclose(outfile);
569 return 1;