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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * The binary resource layout is as follows:
45 * Block 1 | Low ID | |
50 * +===============+ | |
54 * +===============+ <-+ |
55 * B0 LoID | Len | Flags | |
65 * B0 HiID | Len | Flags | |
70 * +===============+ <----+
71 * B1 LoID | Len | Flags |
82 * All Fields are aligned on their natural boundaries. The length
83 * field (Len) covers both the length of the string and the header
84 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
85 * for normal character strings and 1 for unicode strings.
88 static char str_header[] =
89 "/* This file is generated with wmc version " WMC_FULLVERSION ". Do not edit! */\n"
96 static char *dup_u2c(int cp, const WCHAR *uc)
98 int len = unistrlen(uc);
99 char *cptr = xmalloc(len+1);
100 // const union cptable *cpdef = find_codepage(cp);
102 // internal_error(__FILE__, __LINE__, "Codepage %d not found (vanished?)", cp);
103 // if((len = cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len+1, NULL, NULL)) < 0)
104 if((len = WideCharToMultiByte(cp, 0, uc, unistrlen(uc)+1, cptr, len+1, NULL, NULL)) < 0)
106 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", len);
110 static void killnl(char *s, int ddd)
113 tmp = strstr(s, "\r\n");
116 if(ddd && tmp - s > 3)
118 tmp[0] = tmp[1] = tmp[2] = '.';
124 tmp = strchr(s, '\n');
127 if(ddd && tmp - s > 3)
129 tmp[0] = tmp[1] = tmp[2] = '.';
137 static int killcomment(char *s)
141 while((tmp = strstr(tmp, "/*")))
147 while((tmp = strstr(tmp, "*/")))
155 void write_h_file(const char *fname)
167 fp = fopen(fname, "w");
175 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
176 fprintf(fp, "#ifndef __WMCGENERATED_%08lx_H\n", now);
177 fprintf(fp, "#define __WMCGENERATED_%08lx_H\n", now);
180 /* Write severity and facility aliases */
181 get_tokentable(&ttab, &ntab);
182 fprintf(fp, "/* Severity codes */\n");
183 for(i = 0; i < ntab; i++)
185 if(ttab[i].type == tok_severity && ttab[i].alias)
187 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
188 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
194 fprintf(fp, "/* Facility codes */\n");
195 for(i = 0; i < ntab; i++)
197 if(ttab[i].type == tok_facility && ttab[i].alias)
199 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
200 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
206 /* Write the message codes */
207 fprintf(fp, "/* Message definitions */\n");
208 for(ndp = nodehead; ndp; ndp = ndp->next)
213 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
217 fprintf(fp, "/* %s */\n", cptr);
226 * Search for an english text.
227 * If not found, then use the first in the list
230 for(i = 0; i < ndp->u.msg->nmsgs; i++)
232 if(ndp->u.msg->msgs[i]->lan == 0x409)
240 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
241 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
244 fprintf(fp, "/* Approx. msg: %s */\n", cptr);
246 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
248 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
251 switch(ndp->u.msg->base)
255 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
257 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
261 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
263 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
267 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
269 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
272 internal_error(__FILE__, __LINE__, "Invalid base for number print");
279 internal_error(__FILE__, __LINE__, "Invalid node type %d", ndp->type);
282 fprintf(fp, "\n#endif\n");
286 static void write_rcbin(FILE *fp)
293 get_tokentable(&ttab, &ntab);
295 for(lbp = lanblockhead; lbp; lbp = lbp->next)
298 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
299 for(i = 0; i < ntab; i++)
301 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
304 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
309 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
310 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
315 static char *make_string(WCHAR *uc, int len, int codepage)
317 char *str = xmalloc(7*len + 1);
327 for(i = b = 0; i < len; i++, uc++)
341 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
342 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
343 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
344 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
345 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
346 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
347 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
348 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
349 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
351 n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
359 n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
363 if(i < len-1 && b >= 72)
374 len = (len + 3) & ~3;
389 char *tmp = xmalloc(2*len+1);
391 // const union cptable *cpdef = find_codepage(codepage);
393 // assert(cpdef != NULL);
394 // if((i = cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
395 if((i = WideCharToMultiByte(codepage, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
397 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
400 for(i = b = 0; i < len; i++, 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;
422 n = sprintf(cptr, "\\x%02x", *cc & 0xff);
427 if(i < len-1 && b >= 72)
437 len = (len + 3) & ~3;
453 static char *make_bin_string(WCHAR *uc, int len, int *retlen, int codepage)
456 char *str = xmalloc(7 * len + 1);
462 WCHAR *cptr = (WCHAR*)str;
464 for(i = b = 0; i < len; i++, uc++)
468 len = (len + 1) & ~1;
477 char *tmp = xmalloc(2*len+1);
480 // const union cptable *cpdef = find_codepage(codepage);
482 // assert(cpdef != NULL);
483 // if((i = cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
484 if((i = WideCharToMultiByte(codepage, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
485 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
486 for(i = b = 0; i < len; i++, cc++)
491 len = (len + 3) & ~3;
502 static void write_rcinline(FILE *fp)
508 for(lbp = lanblockhead; lbp; lbp = lbp->next)
510 unsigned offs = 4 * (lbp->nblk * 3 + 1);
511 fprintf(fp, "\n1 MESSAGETABLE\n");
512 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
514 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
515 for(i = 0; i < lbp->nblk; i++)
517 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
521 offs += lbp->blks[i].size;
523 for(i = 0; i < lbp->nblk; i++)
525 block_t *blk = &lbp->blks[i];
526 for(j = 0; j < blk->nmsg; j++)
529 int l = blk->msgs[j]->len;
530 char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
531 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
532 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
534 (unicodeout ? (l*2+3)&~3 : (l+3)&~3)+4,
535 unicodeout ? '1' : '0');
536 fprintf(fp, "%s%s\n", cptr, comma);
544 void write_rc_file(const char *fname)
549 fp = fopen(fname, "w");
557 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
566 void write_bin_files(void)
576 get_tokentable(&ttab, &ntab);
578 for (lbp = lanblockhead; lbp; lbp = lbp->next)
580 unsigned offs = 4 * (lbp->nblk * 3 + 1);
583 for(i = 0; i < ntab; i++)
585 if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
588 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
594 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
595 sprintf(fname, "%s.bin", cptr);
598 fp = fopen(fname, "wb");
606 fwrite(&lbp->nblk, sizeof(unsigned long), 1, fp);
607 for(i = 0; i < lbp->nblk; i++)
610 fwrite(&lbp->blks[i].idlo, sizeof(unsigned long), 1, fp);
612 fwrite(&lbp->blks[i].idhi, sizeof(unsigned long), 1, fp);
614 fwrite(&offs, sizeof(unsigned long), 1, fp);
615 offs += lbp->blks[i].size;
618 for (i = 0; i < lbp->nblk; i++)
620 block_t *blk = &lbp->blks[i];
621 for (j = 0; j < blk->nmsg; j++)
624 int l = blk->msgs[j]->len;
627 unsigned short length = (unicodeout ? (l*2+3)&~3 : (l+3)&~3)+4;
628 unsigned short flags = unicodeout ? 1 : 0;
631 fwrite(&length, sizeof(unsigned short), 1, fp);
633 fwrite(&flags, sizeof(unsigned short), 1, fp);
636 cptr = make_bin_string(blk->msgs[j]->msg, l, &retlen, unicodeout ? 0 : blk->msgs[j]->cp);
637 fwrite(cptr, retlen, 1, fp);