winecfg: Added missing break (Coverity).
[wine.git] / tools / wmc / write.c
blob0fc38adacbf9a0e38ff1b9594d06a0f3c87541d9
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 "/* Date : %s */\n"
95 "\n"
98 static char *dup_u2c(int cp, const WCHAR *uc)
100 int len;
101 char *cptr;
102 const union cptable *cpdef = find_codepage(cp);
104 if(cpdef)
105 len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
106 else
107 len = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, NULL, 0);
108 cptr = xmalloc(len);
109 if (cpdef)
110 len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len, NULL, NULL);
111 else
112 len = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, cptr, len);
113 if (len < 0)
114 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", len);
115 return cptr;
118 static void killnl(char *s, int ddd)
120 char *tmp;
121 tmp = strstr(s, "\r\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';
132 tmp = strchr(s, '\n');
133 if(tmp)
135 if(ddd && tmp - s > 3)
137 tmp[0] = tmp[1] = tmp[2] = '.';
138 tmp[3] = '\0';
140 else
141 *tmp = '\0';
145 static int killcomment(char *s)
147 char *tmp = s;
148 int b = 0;
149 while((tmp = strstr(tmp, "/*")))
151 tmp[1] = 'x';
152 b++;
154 tmp = s;
155 while((tmp = strstr(tmp, "*/")))
157 tmp[0] = 'x';
158 b++;
160 return b;
163 void write_h_file(const char *fname)
165 node_t *ndp;
166 char *cptr;
167 char *cast;
168 FILE *fp;
169 token_t *ttab;
170 int ntab;
171 int i;
172 int once = 0;
173 int idx_en = 0;
175 fp = fopen(fname, "w");
176 if(!fp)
178 perror(fname);
179 exit(1);
181 cptr = ctime(&now);
182 killnl(cptr, 0);
183 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
184 fprintf(fp, "#ifndef __WMCGENERATED_%08lx_H\n", (long)now);
185 fprintf(fp, "#define __WMCGENERATED_%08lx_H\n", (long)now);
186 fprintf(fp, "\n");
188 /* Write severity and facility aliases */
189 get_tokentable(&ttab, &ntab);
190 fprintf(fp, "/* Severity codes */\n");
191 for(i = 0; i < ntab; i++)
193 if(ttab[i].type == tok_severity && ttab[i].alias)
195 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
196 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
197 free(cptr);
200 fprintf(fp, "\n");
202 fprintf(fp, "/* Facility codes */\n");
203 for(i = 0; i < ntab; i++)
205 if(ttab[i].type == tok_facility && ttab[i].alias)
207 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
208 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
209 free(cptr);
212 fprintf(fp, "\n");
214 /* Write the message codes */
215 fprintf(fp, "/* Message definitions */\n");
216 for(ndp = nodehead; ndp; ndp = ndp->next)
218 switch(ndp->type)
220 case nd_comment:
221 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
222 killnl(cptr, 0);
223 killcomment(cptr);
224 if(*cptr)
225 fprintf(fp, "/* %s */\n", cptr);
226 else
227 fprintf(fp, "\n");
228 free(cptr);
229 break;
230 case nd_msg:
231 if(!once)
234 * Search for an english text.
235 * If not found, then use the first in the list
237 once++;
238 for(i = 0; i < ndp->u.msg->nmsgs; i++)
240 if(ndp->u.msg->msgs[i]->lan == 0x409)
242 idx_en = i;
243 break;
246 fprintf(fp, "\n");
248 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
249 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
250 killnl(cptr, 0);
251 killcomment(cptr);
252 fprintf(fp, "/* Approximate msg: %s */\n", cptr);
253 free(cptr);
254 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
255 if(ndp->u.msg->cast)
256 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
257 else
258 cast = NULL;
259 switch(ndp->u.msg->base)
261 case 8:
262 if(cast)
263 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
264 else
265 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
266 break;
267 case 10:
268 if(cast)
269 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
270 else
271 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
272 break;
273 case 16:
274 if(cast)
275 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
276 else
277 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
278 break;
279 default:
280 internal_error(__FILE__, __LINE__, "Invalid base for number print\n");
282 free(cptr);
283 free(cast);
284 break;
285 default:
286 internal_error(__FILE__, __LINE__, "Invalid node type %d\n", ndp->type);
289 fprintf(fp, "\n#endif\n");
290 fclose(fp);
293 static void write_rcbin(FILE *fp)
295 lan_blk_t *lbp;
296 token_t *ttab;
297 int ntab;
298 int i;
300 get_tokentable(&ttab, &ntab);
302 for(lbp = lanblockhead; lbp; lbp = lbp->next)
304 char *cptr = NULL;
305 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
306 for(i = 0; i < ntab; i++)
308 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
310 if(ttab[i].alias)
311 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
312 break;
315 if(!cptr)
316 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
317 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
318 free(cptr);
322 static char *make_string(WCHAR *uc, int len, int codepage)
324 char *str = xmalloc(7*len + 1);
325 char *cptr = str;
326 int i;
327 int b;
329 if(!codepage)
331 *cptr++ = ' ';
332 *cptr++ = 'L';
333 *cptr++ = '"';
334 for(i = b = 0; i < len; i++, uc++)
336 switch(*uc)
338 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
339 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
340 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
341 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
342 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
343 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
344 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
345 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
346 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
347 default:
348 if (*uc < 0x100 && isprint(*uc))
350 *cptr++ = *uc;
351 b++;
353 else
355 int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
356 cptr += n;
357 b += n;
359 break;
361 if(i < len-1 && b >= 72)
363 *cptr++ = '"';
364 *cptr++ = ',';
365 *cptr++ = '\n';
366 *cptr++ = ' ';
367 *cptr++ = 'L';
368 *cptr++ = '"';
369 b = 0;
372 if (unicodeout)
373 len = (len + 1) & ~1;
374 else
375 len = (len + 3) & ~3;
376 for(; i < len; i++)
378 *cptr++ = '\\';
379 *cptr++ = 'x';
380 *cptr++ = '0';
381 *cptr++ = '0';
382 *cptr++ = '0';
383 *cptr++ = '0';
385 *cptr++ = '"';
386 *cptr = '\0';
388 else
390 char *tmp, *cc;
391 int mlen;
392 const union cptable *cpdef = find_codepage(codepage);
394 if (cpdef)
395 mlen = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
396 else
397 mlen = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, NULL, 0);
398 cc = tmp = xmalloc(mlen);
399 if (cpdef) {
400 if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, mlen, NULL, NULL)) < 0)
401 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", i);
402 } else {
403 if((i = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, tmp, mlen)) < 0)
404 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", i);
406 *cptr++ = ' ';
407 *cptr++ = '"';
408 for(i = b = 0; i < len; i++, cc++)
410 switch(*cc)
412 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
413 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
414 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
415 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
416 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
417 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
418 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
419 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
420 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
421 default:
422 if(isprint(*cc))
424 *cptr++ = *cc;
425 b++;
427 else
429 int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
430 cptr += n;
431 b += n;
433 break;
435 if(i < len-1 && b >= 72)
437 *cptr++ = '"';
438 *cptr++ = ',';
439 *cptr++ = '\n';
440 *cptr++ = ' ';
441 *cptr++ = '"';
442 b = 0;
445 len = (len + 3) & ~3;
446 for(; i < len; i++)
448 *cptr++ = '\\';
449 *cptr++ = 'x';
450 *cptr++ = '0';
451 *cptr++ = '0';
453 *cptr++ = '"';
454 *cptr = '\0';
455 free(tmp);
457 return str;
460 static void write_rcinline(FILE *fp)
462 lan_blk_t *lbp;
463 int i;
464 int j;
466 for(lbp = lanblockhead; lbp; lbp = lbp->next)
468 unsigned offs = 4 * (lbp->nblk * 3 + 1);
469 fprintf(fp, "\n1 MESSAGETABLE\n");
470 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
471 fprintf(fp, "{\n");
472 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
473 for(i = 0; i < lbp->nblk; i++)
475 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
476 lbp->blks[i].idlo,
477 lbp->blks[i].idhi,
478 offs);
479 offs += lbp->blks[i].size;
481 for(i = 0; i < lbp->nblk; i++)
483 block_t *blk = &lbp->blks[i];
484 for(j = 0; j < blk->nmsg; j++)
486 char *cptr;
487 int l = blk->msgs[j]->len;
488 const char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
489 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
490 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
491 blk->idlo + j,
492 (unicodeout ? (l*2+3)&~3 : (l+3)&~3) + 4,
493 unicodeout ? '1' : '0');
494 fprintf(fp, "%s%s\n", cptr, comma);
495 free(cptr);
498 fprintf(fp, "}\n");
502 void write_rc_file(const char *fname)
504 FILE *fp;
505 char *cptr;
507 fp = fopen(fname, "w");
508 if(!fp)
510 perror(fname);
511 exit(1);
513 cptr = ctime(&now);
514 killnl(cptr, 0);
515 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
517 if(rcinline)
518 write_rcinline(fp);
519 else
520 write_rcbin(fp);
521 fclose(fp);
524 static void output_bin_data( lan_blk_t *lbp )
526 unsigned int offs = 4 * (lbp->nblk * 3 + 1);
527 int i, j, k;
529 put_dword( lbp->nblk ); /* NBlocks */
530 for (i = 0; i < lbp->nblk; i++)
532 put_dword( lbp->blks[i].idlo ); /* Lo */
533 put_dword( lbp->blks[i].idhi ); /* Hi */
534 put_dword( offs ); /* Offs */
535 offs += lbp->blks[i].size;
537 for (i = 0; i < lbp->nblk; i++)
539 block_t *blk = &lbp->blks[i];
540 for (j = 0; j < blk->nmsg; j++)
542 int len = (2 * blk->msgs[j]->len + 3) & ~3;
543 put_word( len + 4 );
544 put_word( 1 );
545 for (k = 0; k < blk->msgs[j]->len; k++) put_word( blk->msgs[j]->msg[k] );
546 align_output( 4 );
551 void write_bin_files(void)
553 lan_blk_t *lbp;
554 token_t *ttab;
555 int ntab;
556 int i;
558 get_tokentable(&ttab, &ntab);
560 for (lbp = lanblockhead; lbp; lbp = lbp->next)
562 char *cptr = NULL;
564 for (i = 0; i < ntab; i++)
566 if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
568 if (ttab[i].alias) cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
569 break;
572 if(!cptr)
573 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
574 init_output_buffer();
575 output_bin_data( lbp );
576 cptr = xrealloc( cptr, strlen(cptr) + 5 );
577 strcat( cptr, ".bin" );
578 flush_output_buffer( cptr );
579 free(cptr);
583 void write_res_file( const char *name )
585 lan_blk_t *lbp;
586 int i, j;
588 init_output_buffer();
590 put_dword( 0 ); /* ResSize */
591 put_dword( 32 ); /* HeaderSize */
592 put_word( 0xffff ); /* ResType */
593 put_word( 0x0000 );
594 put_word( 0xffff ); /* ResName */
595 put_word( 0x0000 );
596 put_dword( 0 ); /* DataVersion */
597 put_word( 0 ); /* Memory options */
598 put_word( 0 ); /* Language */
599 put_dword( 0 ); /* Version */
600 put_dword( 0 ); /* Characteristics */
602 for (lbp = lanblockhead; lbp; lbp = lbp->next)
604 unsigned int data_size = 4 * (lbp->nblk * 3 + 1);
605 unsigned int header_size = 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
607 for (i = 0; i < lbp->nblk; i++)
609 block_t *blk = &lbp->blks[i];
610 for (j = 0; j < blk->nmsg; j++) data_size += 4 + ((blk->msgs[j]->len * 2 + 3) & ~3);
613 put_dword( data_size ); /* ResSize */
614 put_dword( header_size ); /* HeaderSize */
615 put_word( 0xffff ); /* ResType */
616 put_word( 0x000b /*RT_MESSAGETABLE*/ );
617 put_word( 0xffff ); /* ResName */
618 put_word( 0x0001 );
619 align_output( 4 );
620 put_dword( 0 ); /* DataVersion */
621 put_word( 0x30 ); /* Memory options */
622 put_word( lbp->lan ); /* Language */
623 put_dword( lbp->version ); /* Version */
624 put_dword( 0 ); /* Characteristics */
626 output_bin_data( lbp );
628 flush_output_buffer( name );