include: Add DIPROPPOINTER definition.
[wine.git] / tools / wmc / write.c
blob361d1a9943e117817b2b9ef9a86ebe0d777645d2
1 /*
2 * Wine Message Compiler output generation
4 * Copyright 2000 Bertho A. Stultiens (BS)
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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "wmc.h"
31 #include "utils.h"
32 #include "lang.h"
33 #include "write.h"
36 * The binary resource layout is as follows:
38 * +===============+
39 * Header | NBlocks |
40 * +===============+
41 * Block 0 | Low ID |
42 * +---------------+
43 * | High ID |
44 * +---------------+
45 * | Offset |---+
46 * +===============+ |
47 * Block 1 | Low ID | |
48 * +---------------+ |
49 * | High ID | |
50 * +---------------+ |
51 * | Offset |------+
52 * +===============+ | |
53 * | | | |
54 * ... ... | |
55 * | | | |
56 * +===============+ <-+ |
57 * B0 LoID | Len | Flags | |
58 * +---+---+---+---+ |
59 * | b | l | a | b | |
60 * +---+---+---+---+ |
61 * | l | a | \0| \0| |
62 * +===============+ |
63 * | | |
64 * ... ... |
65 * | | |
66 * +===============+ |
67 * B0 HiID | Len | Flags | |
68 * +---+---+---+---+ |
69 * | M | o | r | e | |
70 * +---+---+---+---+ |
71 * | b | l | a | \0| |
72 * +===============+ <----+
73 * B1 LoID | Len | Flags |
74 * +---+---+---+---+
75 * | J | u | n | k |
76 * +---+---+---+---+
77 * | \0| \0| \0| \0|
78 * +===============+
79 * | |
80 * ... ...
81 * | |
82 * +===============+
84 * All Fields are aligned on their natural boundaries. The length
85 * field (Len) covers both the length of the string and the header
86 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
87 * for normal character strings and 1 for unicode strings.
90 static const char str_header[] =
91 "/* This file is generated with wmc version " PACKAGE_VERSION ". Do not edit! */\n"
92 "/* Source : %s */\n"
93 "/* Cmdline: %s */\n"
94 "\n"
97 static char *dup_u2c(const WCHAR *uc)
99 int i;
100 char *cptr = xmalloc( unistrlen(uc)+1 );
102 for (i = 0; *uc; i++, uc++) cptr[i] = (*uc <= 0xff) ? *uc : '_';
103 cptr[i] = 0;
104 return cptr;
107 static void killnl(char *s, int ddd)
109 char *tmp;
110 tmp = strstr(s, "\r\n");
111 if(tmp)
113 if(ddd && tmp - s > 3)
115 tmp[0] = tmp[1] = tmp[2] = '.';
116 tmp[3] = '\0';
118 else
119 *tmp = '\0';
121 tmp = strchr(s, '\n');
122 if(tmp)
124 if(ddd && tmp - s > 3)
126 tmp[0] = tmp[1] = tmp[2] = '.';
127 tmp[3] = '\0';
129 else
130 *tmp = '\0';
134 static int killcomment(char *s)
136 char *tmp = s;
137 int b = 0;
138 while((tmp = strstr(tmp, "/*")))
140 tmp[1] = 'x';
141 b++;
143 tmp = s;
144 while((tmp = strstr(tmp, "*/")))
146 tmp[0] = 'x';
147 b++;
149 return b;
152 void write_h_file(const char *fname)
154 node_t *ndp;
155 char *cptr;
156 char *cast;
157 FILE *fp;
158 token_t *ttab;
159 int ntab;
160 int i;
161 int once = 0;
162 int idx_en = 0;
164 fp = fopen(fname, "w");
165 if(!fp)
167 perror(fname);
168 exit(1);
170 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
171 fprintf(fp, "#ifndef __WMCGENERATED_H\n");
172 fprintf(fp, "#define __WMCGENERATED_H\n");
173 fprintf(fp, "\n");
175 /* Write severity and facility aliases */
176 get_tokentable(&ttab, &ntab);
177 fprintf(fp, "/* Severity codes */\n");
178 for(i = 0; i < ntab; i++)
180 if(ttab[i].type == tok_severity && ttab[i].alias)
182 cptr = dup_u2c(ttab[i].alias);
183 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
184 free(cptr);
187 fprintf(fp, "\n");
189 fprintf(fp, "/* Facility codes */\n");
190 for(i = 0; i < ntab; i++)
192 if(ttab[i].type == tok_facility && ttab[i].alias)
194 cptr = dup_u2c(ttab[i].alias);
195 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
196 free(cptr);
199 fprintf(fp, "\n");
201 /* Write the message codes */
202 fprintf(fp, "/* Message definitions */\n");
203 for(ndp = nodehead; ndp; ndp = ndp->next)
205 switch(ndp->type)
207 case nd_comment:
208 cptr = dup_u2c(ndp->u.comment+1);
209 killnl(cptr, 0);
210 killcomment(cptr);
211 if(*cptr)
212 fprintf(fp, "/* %s */\n", cptr);
213 else
214 fprintf(fp, "\n");
215 free(cptr);
216 break;
217 case nd_msg:
218 if(!once)
221 * Search for an English text.
222 * If not found, then use the first in the list
224 once++;
225 for(i = 0; i < ndp->u.msg->nmsgs; i++)
227 if(ndp->u.msg->msgs[i]->lan == 0x409)
229 idx_en = i;
230 break;
233 fprintf(fp, "\n");
235 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
236 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->msg);
237 killnl(cptr, 0);
238 killcomment(cptr);
239 fprintf(fp, "/* Approximate msg: %s */\n", cptr);
240 free(cptr);
241 cptr = dup_u2c(ndp->u.msg->sym);
242 if(ndp->u.msg->cast)
243 cast = dup_u2c(ndp->u.msg->cast);
244 else
245 cast = NULL;
246 switch(ndp->u.msg->base)
248 case 8:
249 if(cast)
250 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
251 else
252 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
253 break;
254 case 10:
255 if(cast)
256 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
257 else
258 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
259 break;
260 case 16:
261 if(cast)
262 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
263 else
264 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
265 break;
266 default:
267 internal_error(__FILE__, __LINE__, "Invalid base for number print\n");
269 free(cptr);
270 free(cast);
271 break;
272 default:
273 internal_error(__FILE__, __LINE__, "Invalid node type %d\n", ndp->type);
276 fprintf(fp, "\n#endif\n");
277 fclose(fp);
280 static void write_rcbin(FILE *fp)
282 lan_blk_t *lbp;
283 token_t *ttab;
284 int ntab;
285 int i;
287 get_tokentable(&ttab, &ntab);
289 for(lbp = lanblockhead; lbp; lbp = lbp->next)
291 char *cptr = NULL;
292 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
293 for(i = 0; i < ntab; i++)
295 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
297 if(ttab[i].alias)
298 cptr = dup_u2c(ttab[i].alias);
299 break;
302 if(!cptr)
303 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
304 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
305 free(cptr);
309 static char *make_string(WCHAR *uc, int len)
311 char *str = xmalloc(7*len + 12);
312 char *cptr = str;
313 int i;
314 int b;
316 *cptr++ = ' ';
317 *cptr++ = 'L';
318 *cptr++ = '"';
319 for(i = b = 0; i < len; i++, uc++)
321 switch(*uc)
323 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
324 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
325 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
326 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
327 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
328 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
329 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
330 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
331 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
332 default:
333 if (*uc < 0x100 && isprint(*uc))
335 *cptr++ = *uc;
336 b++;
338 else
340 int n = sprintf(cptr, "\\x%04x", *uc);
341 cptr += n;
342 b += n;
344 break;
346 if(i < len-1 && b >= 72)
348 *cptr++ = '"';
349 *cptr++ = ',';
350 *cptr++ = '\n';
351 *cptr++ = ' ';
352 *cptr++ = 'L';
353 *cptr++ = '"';
354 b = 0;
357 len = (len + 1) & ~1;
358 for(; i < len; i++)
360 *cptr++ = '\\';
361 *cptr++ = 'x';
362 *cptr++ = '0';
363 *cptr++ = '0';
364 *cptr++ = '0';
365 *cptr++ = '0';
367 *cptr++ = '"';
368 *cptr = '\0';
369 return str;
372 static void write_rcinline(FILE *fp)
374 lan_blk_t *lbp;
375 int i;
376 int j;
378 for(lbp = lanblockhead; lbp; lbp = lbp->next)
380 unsigned offs = 4 * (lbp->nblk * 3 + 1);
381 fprintf(fp, "\n1 MESSAGETABLE\n");
382 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
383 fprintf(fp, "{\n");
384 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
385 for(i = 0; i < lbp->nblk; i++)
387 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
388 lbp->blks[i].idlo,
389 lbp->blks[i].idhi,
390 offs);
391 offs += lbp->blks[i].size;
393 for(i = 0; i < lbp->nblk; i++)
395 block_t *blk = &lbp->blks[i];
396 for(j = 0; j < blk->nmsg; j++)
398 char *cptr;
399 int l = blk->msgs[j]->len;
400 const char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
401 cptr = make_string(blk->msgs[j]->msg, l);
402 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x0001,\n",
403 blk->idlo + j, ((l * 2 + 3) & ~3) + 4 );
404 fprintf(fp, "%s%s\n", cptr, comma);
405 free(cptr);
408 fprintf(fp, "}\n");
412 void write_rc_file(const char *fname)
414 FILE *fp = fopen(fname, "w");
416 if(!fp)
418 perror(fname);
419 exit(1);
421 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
423 if(rcinline)
424 write_rcinline(fp);
425 else
426 write_rcbin(fp);
427 fclose(fp);
430 static void output_bin_data( lan_blk_t *lbp )
432 unsigned int offs = 4 * (lbp->nblk * 3 + 1);
433 int i, j, k;
435 put_dword( lbp->nblk ); /* NBlocks */
436 for (i = 0; i < lbp->nblk; i++)
438 put_dword( lbp->blks[i].idlo ); /* Lo */
439 put_dword( lbp->blks[i].idhi ); /* Hi */
440 put_dword( offs ); /* Offs */
441 offs += lbp->blks[i].size;
443 for (i = 0; i < lbp->nblk; i++)
445 block_t *blk = &lbp->blks[i];
446 for (j = 0; j < blk->nmsg; j++)
448 int len = (2 * blk->msgs[j]->len + 3) & ~3;
449 put_word( len + 4 );
450 put_word( 1 );
451 for (k = 0; k < blk->msgs[j]->len; k++) put_word( blk->msgs[j]->msg[k] );
452 align_output( 4 );
457 void write_bin_files(void)
459 lan_blk_t *lbp;
460 token_t *ttab;
461 int ntab;
462 int i;
464 get_tokentable(&ttab, &ntab);
466 for (lbp = lanblockhead; lbp; lbp = lbp->next)
468 char *cptr = NULL;
470 for (i = 0; i < ntab; i++)
472 if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
474 if (ttab[i].alias) cptr = dup_u2c(ttab[i].alias);
475 break;
478 if(!cptr)
479 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
480 init_output_buffer();
481 output_bin_data( lbp );
482 cptr = xrealloc( cptr, strlen(cptr) + 5 );
483 strcat( cptr, ".bin" );
484 flush_output_buffer( cptr );
485 free(cptr);
489 void write_res_file( const char *name )
491 lan_blk_t *lbp;
492 int i, j;
494 init_output_buffer();
496 put_dword( 0 ); /* ResSize */
497 put_dword( 32 ); /* HeaderSize */
498 put_word( 0xffff ); /* ResType */
499 put_word( 0x0000 );
500 put_word( 0xffff ); /* ResName */
501 put_word( 0x0000 );
502 put_dword( 0 ); /* DataVersion */
503 put_word( 0 ); /* Memory options */
504 put_word( 0 ); /* Language */
505 put_dword( 0 ); /* Version */
506 put_dword( 0 ); /* Characteristics */
508 for (lbp = lanblockhead; lbp; lbp = lbp->next)
510 unsigned int data_size = 4 * (lbp->nblk * 3 + 1);
511 unsigned int header_size = 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
513 for (i = 0; i < lbp->nblk; i++)
515 block_t *blk = &lbp->blks[i];
516 for (j = 0; j < blk->nmsg; j++) data_size += 4 + ((blk->msgs[j]->len * 2 + 3) & ~3);
519 put_dword( data_size ); /* ResSize */
520 put_dword( header_size ); /* HeaderSize */
521 put_word( 0xffff ); /* ResType */
522 put_word( 0x000b /*RT_MESSAGETABLE*/ );
523 put_word( 0xffff ); /* ResName */
524 put_word( 0x0001 );
525 align_output( 4 );
526 put_dword( 0 ); /* DataVersion */
527 put_word( 0x30 ); /* Memory options */
528 put_word( lbp->lan ); /* Language */
529 put_dword( lbp->version ); /* Version */
530 put_dword( 0 ); /* Characteristics */
532 output_bin_data( lbp );
534 flush_output_buffer( name );