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
22 #include "wine/port.h"
38 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
41 #define TOKEN_STRING 2
42 #define TOKEN_INTEGER 3
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
55 #define TOKEN_COMMA 19
56 #define TOKEN_SEMICOLON 20
57 #define TOKEN_TEMPLATE 31
59 #define TOKEN_DWORD 41
60 #define TOKEN_FLOAT 42
61 #define TOKEN_DOUBLE 43
63 #define TOKEN_UCHAR 45
64 #define TOKEN_SWORD 46
65 #define TOKEN_SDWORD 47
67 #define TOKEN_LPSTR 49
68 #define TOKEN_UNICODE 50
69 #define TOKEN_CSTRING 51
70 #define TOKEN_ARRAY 52
85 static const struct keyword reserved_words
[] = {
86 {"ARRAY", TOKEN_ARRAY
},
88 {"CSTRING", TOKEN_CSTRING
},
89 {"DOUBLE", TOKEN_DOUBLE
},
90 {"DWORD", TOKEN_DWORD
},
91 {"FLOAT", TOKEN_FLOAT
},
92 {"SDWORD", TOKEN_SDWORD
},
93 {"STRING", TOKEN_LPSTR
},
94 {"SWORD", TOKEN_SWORD
},
95 {"TEMPLATE", TOKEN_TEMPLATE
},
96 {"UCHAR", TOKEN_UCHAR
},
97 {"UNICODE", TOKEN_UNICODE
},
102 extern int getopt(int argc
, char *const *argv
, const char *optstring
);
104 static BOOL option_header
;
105 static char *option_inc_var_name
= NULL
;
106 static char *option_inc_size_name
= NULL
;
107 static const char *option_outfile_name
= "-";
108 static char *program_name
;
109 static const char *infile_name
;
110 static BYTE
*output_data
;
111 static UINT output_pos
, output_size
;
114 #define __attribute__(x)
117 static void fatal_error( struct parser
*parser
, const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 2, 3)));
119 static void fatal_error( struct parser
*parser
, const char *msg
, ... )
122 va_start( valist
, msg
);
125 fprintf( stderr
, "%s:%d:", infile_name
, parser
->line_no
);
126 fprintf( stderr
, " error: " );
128 else fprintf( stderr
, "%s: error: ", program_name
);
129 vfprintf( stderr
, msg
, valist
);
135 static inline BOOL
read_byte(struct parser
*parser
, char *byte
)
137 int c
= fgetc(parser
->infile
);
139 if (c
== '\n') parser
->line_no
++;
143 static inline BOOL
unread_byte(struct parser
*parser
, char last_byte
)
145 if (last_byte
== '\n') parser
->line_no
--;
146 return ungetc(last_byte
, parser
->infile
) != EOF
;
149 static inline BOOL
read_bytes(struct parser
*parser
, void *data
, DWORD size
)
151 return fread(data
, size
, 1, parser
->infile
) > 0;
154 static BOOL
write_c_hex_bytes(struct parser
*parser
)
157 for (i
= 0; i
< output_pos
; i
++)
160 fprintf(parser
->outfile
, "\n ");
161 fprintf(parser
->outfile
, " 0x%02x,", output_data
[i
]);
166 static BOOL
write_raw_bytes(struct parser
*parser
)
168 return fwrite(output_data
, output_pos
, 1, parser
->outfile
) > 0;
171 static inline BOOL
write_bytes(struct parser
*parser
, const void *data
, DWORD size
)
173 if (output_pos
+ size
> output_size
)
175 output_size
= max( output_size
* 2, size
);
176 output_data
= realloc( output_data
, output_size
);
177 if (!output_data
) return FALSE
;
179 memcpy( output_data
+ output_pos
, data
, size
);
184 static inline BOOL
write_byte(struct parser
*parser
, BYTE value
)
186 return write_bytes(parser
, &value
, sizeof(value
));
189 static inline BOOL
write_word(struct parser
*parser
, WORD value
)
191 return write_bytes(parser
, &value
, sizeof(value
));
194 static inline BOOL
write_dword(struct parser
*parser
, DWORD value
)
196 return write_bytes(parser
, &value
, sizeof(value
));
199 static int compare_names(const void *a
, const void *b
)
201 return strcasecmp(*(const char **)a
, *(const char **)b
);
204 static BOOL
parse_keyword(struct parser
*parser
, const char *name
)
206 const struct keyword
*keyword
;
208 keyword
= bsearch(&name
, reserved_words
, ARRAY_SIZE(reserved_words
),
209 sizeof(reserved_words
[0]), compare_names
);
213 return write_word(parser
, keyword
->token
);
216 static BOOL
parse_guid(struct parser
*parser
)
222 static const char *guidfmt
= "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
225 if (!read_bytes(parser
, buf
+ 1, 37)) fatal_error( parser
, "truncated GUID\n" );
228 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);
229 if (ret
!= 11) fatal_error( parser
, "invalid GUID '%s'\n", buf
);
233 guid
.Data4
[0] = tab
[2];
234 guid
.Data4
[1] = tab
[3];
235 guid
.Data4
[2] = tab
[4];
236 guid
.Data4
[3] = tab
[5];
237 guid
.Data4
[4] = tab
[6];
238 guid
.Data4
[5] = tab
[7];
239 guid
.Data4
[6] = tab
[8];
240 guid
.Data4
[7] = tab
[9];
242 return write_word(parser
, TOKEN_GUID
) &&
243 write_bytes(parser
, &guid
, sizeof(guid
));
246 static BOOL
parse_name(struct parser
*parser
)
252 while (read_byte(parser
, &c
) && len
< sizeof(name
) &&
253 (isalnum(c
) || c
== '_' || c
== '-'))
255 if (len
+ 1 < sizeof(name
))
258 unread_byte(parser
, c
);
261 if (parse_keyword(parser
, name
)) {
264 return write_word(parser
, TOKEN_NAME
) &&
265 write_dword(parser
, len
) &&
266 write_bytes(parser
, name
, len
);
270 static BOOL
parse_number(struct parser
*parser
)
278 while (read_byte(parser
, &c
) &&
279 ((!len
&& c
== '-') || (!dot
&& c
== '.') || isdigit(c
)))
281 if (len
+ 1 < sizeof(buffer
))
286 unread_byte(parser
, c
);
291 ret
= sscanf(buffer
, "%f", &value
);
292 if (!ret
) fatal_error( parser
, "invalid float token\n" );
293 ret
= write_word(parser
, TOKEN_FLOAT
) &&
294 write_bytes(parser
, &value
, sizeof(value
));
297 ret
= sscanf(buffer
, "%d", &value
);
298 if (!ret
) fatal_error( parser
, "invalid integer token\n" );
299 ret
= write_word(parser
, TOKEN_INTEGER
) &&
300 write_dword(parser
, value
);
306 static BOOL
parse_token(struct parser
*parser
)
310 char *tok
, buffer
[512];
312 if (!read_byte(parser
, &c
))
323 case '{': return write_word(parser
, TOKEN_OBRACE
);
324 case '}': return write_word(parser
, TOKEN_CBRACE
);
325 case '[': return write_word(parser
, TOKEN_OBRACKET
);
326 case ']': return write_word(parser
, TOKEN_CBRACKET
);
327 case '(': return write_word(parser
, TOKEN_OPAREN
);
328 case ')': return write_word(parser
, TOKEN_CPAREN
);
329 case ',': return write_word(parser
, TOKEN_COMMA
);
330 case ';': return write_word(parser
, TOKEN_SEMICOLON
);
331 case '.': return write_word(parser
, TOKEN_DOT
);
334 if (!read_byte(parser
, &c
) || c
!= '/')
335 fatal_error( parser
, "invalid single '/' comment token\n" );
336 while (read_byte(parser
, &c
) && c
!= '\n');
341 while (read_byte(parser
, &c
) && c
!= '\n')
342 if (len
+ 1 < sizeof(buffer
)) buffer
[len
++] = c
;
343 if (c
!= '\n') fatal_error( parser
, "line too long\n" );
345 tok
= strtok( buffer
, " \t" );
346 if (!tok
|| strcmp( tok
, "pragma" )) return TRUE
;
347 tok
= strtok( NULL
, " \t" );
348 if (!tok
|| strcmp( tok
, "xftmpl" )) return TRUE
;
349 tok
= strtok( NULL
, " \t" );
350 if (!tok
) return TRUE
;
351 if (!strcmp( tok
, "name" ))
353 tok
= strtok( NULL
, " \t" );
354 if (tok
&& !option_inc_var_name
) option_inc_var_name
= strdup( tok
);
356 else if (!strcmp( tok
, "size" ))
358 tok
= strtok( NULL
, " \t" );
359 if (tok
&& !option_inc_size_name
) option_inc_size_name
= strdup( tok
);
364 return parse_guid(parser
);
369 /* FIXME: Handle '\' (e.g. "valid\"string") */
370 while (read_byte(parser
, &c
) && c
!= '"') {
371 if (len
+ 1 < sizeof(buffer
))
374 if (c
!= '"') fatal_error( parser
, "unterminated string\n" );
375 return write_word(parser
, TOKEN_STRING
) &&
376 write_dword(parser
, len
) &&
377 write_bytes(parser
, buffer
, len
);
380 unread_byte(parser
, c
);
381 if (isdigit(c
) || c
== '-')
382 return parse_number(parser
);
383 if (isalpha(c
) || c
== '_')
384 return parse_name(parser
);
385 fatal_error( parser
, "invalid character '%c' to start token\n", c
);
391 static const char *output_file
;
393 static void cleanup_files(void)
395 if (output_file
) unlink(output_file
);
398 static void exit_on_signal( int sig
)
400 exit(1); /* this will call the atexit functions */
403 static void usage(void)
405 fprintf(stderr
, "Usage: %s [OPTIONS] INFILE\n"
407 " -H Output to a c header file instead of a binary file\n"
408 " -i NAME Output to a c header file, data in variable NAME\n"
409 " -s NAME In a c header file, define NAME to be the data size\n"
410 " -o FILE Write output to FILE\n",
414 static char **parse_options(int argc
, char **argv
)
418 while ((optc
= getopt(argc
, argv
, "hHi:o:s:")) != -1)
426 option_header
= TRUE
;
429 option_header
= TRUE
;
430 option_inc_var_name
= strdup(optarg
);
433 option_outfile_name
= strdup(optarg
);
436 option_inc_size_name
= strdup(optarg
);
440 return &argv
[optind
];
443 int main(int argc
, char **argv
)
446 struct parser parser
;
448 char *header_name
= NULL
;
450 program_name
= argv
[0];
452 args
= parse_options(argc
, argv
);
453 infile_name
= *args
++;
454 if (!infile_name
|| *args
)
460 parser
.infile
= stdin
;
461 parser
.outfile
= NULL
;
463 if (!strcmp(infile_name
, "-")) {
464 infile_name
= "stdin";
465 } else if (!(parser
.infile
= fopen(infile_name
, "rb"))) {
470 if (!read_bytes(&parser
, header
, sizeof(header
))) {
471 fprintf(stderr
, "%s: Failed to read file header\n", program_name
);
474 if (strncmp(header
, "xof ", 4))
476 fprintf(stderr
, "%s: Invalid magic value '%.4s'\n", program_name
, header
);
479 if (strncmp(header
+ 4, "0302", 4) && strncmp(header
+ 4, "0303", 4))
481 fprintf(stderr
, "%s: Unsupported version '%.4s'\n", program_name
, header
+ 4);
484 if (strncmp(header
+ 8, "txt ", 4))
486 fprintf(stderr
, "%s: Only support conversion from text encoded X files.",
490 if (strncmp(header
+ 12, "0032", 4) && strncmp(header
+ 12, "0064", 4))
492 fprintf(stderr
, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
493 program_name
, header
+ 12);
497 if (!strcmp(option_outfile_name
, "-")) {
498 option_outfile_name
= "stdout";
499 parser
.outfile
= stdout
;
501 output_file
= option_outfile_name
;
502 atexit(cleanup_files
);
503 signal(SIGTERM
, exit_on_signal
);
504 signal(SIGINT
, exit_on_signal
);
506 signal(SIGHUP
, exit_on_signal
);
508 if (!(parser
.outfile
= fopen(output_file
, "wb"))) {
509 perror(option_outfile_name
);
514 if (!write_bytes(&parser
, "xof 0302bin 0064", 16))
518 while (parse_token(&parser
));
520 if (ferror(parser
.outfile
) || ferror(parser
.infile
))
527 if (!option_inc_var_name
)
528 fatal_error( &parser
, "variable name must be specified with -i or #pragma name\n" );
530 header_name
= strrchr(option_outfile_name
, '/');
532 header_name
= strdup(header_name
+ 1);
534 header_name
= strdup(option_outfile_name
);
536 fprintf(stderr
, "Out of memory\n");
540 str_ptr
= header_name
;
545 *str_ptr
= toupper(*str_ptr
);
549 fprintf(parser
.outfile
,
550 "/* File generated automatically from %s; do not edit */\n"
552 "#ifndef __WINE_%s\n"
553 "#define __WINE_%s\n"
555 "unsigned char %s[] = {",
556 infile_name
, header_name
, header_name
, option_inc_var_name
);
557 write_c_hex_bytes( &parser
);
558 fprintf(parser
.outfile
, "\n};\n\n");
559 if (option_inc_size_name
)
560 fprintf(parser
.outfile
, "#define %s %u\n\n", option_inc_size_name
, output_pos
);
561 fprintf(parser
.outfile
, "#endif /* __WINE_%s */\n", header_name
);
562 if (ferror(parser
.outfile
))
565 else write_raw_bytes( &parser
);
567 fclose(parser
.infile
);
568 fclose(parser
.outfile
);
574 if (ferror(parser
.infile
))
576 fclose(parser
.infile
);
578 if (parser
.outfile
) {
579 if (ferror(parser
.outfile
))
580 perror(option_outfile_name
);
581 fclose(parser
.outfile
);