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
33 * The binary resource layout is as follows:
44 * Block 1 | Low ID | |
49 * +===============+ | |
53 * +===============+ <-+ |
54 * B0 LoID | Len | Flags | |
64 * B0 HiID | Len | Flags | |
69 * +===============+ <----+
70 * B1 LoID | Len | Flags |
81 * All Fields are aligned on their natural boundaries. The length
82 * field (Len) covers both the length of the string and the header
83 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
84 * for normal character strings and 1 for unicode strings.
87 static char str_header[] =
88 "/* This file is generated with wmc version " WMC_FULLVERSION ". Do not edit! */\n"
95 static char *dup_u2c(int cp, const WCHAR *uc)
97 int len = unistrlen(uc);
98 char *cptr = xmalloc(len+1);
99 const union cptable *cpdef = find_codepage(cp);
101 internal_error(__FILE__, __LINE__, "Codepage %d not found (vanished?)", cp);
102 if((len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len+1, NULL, NULL)) < 0)
103 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", len);
107 static void killnl(char *s, int ddd)
110 tmp = strstr(s, "\r\n");
113 if(ddd && tmp - s > 3)
115 tmp[0] = tmp[1] = tmp[2] = '.';
121 tmp = strchr(s, '\n');
124 if(ddd && tmp - s > 3)
126 tmp[0] = tmp[1] = tmp[2] = '.';
134 static int killcomment(char *s)
138 while((tmp = strstr(tmp, "/*")))
144 while((tmp = strstr(tmp, "*/")))
152 void write_h_file(const char *fname)
164 fp = fopen(fname, "w");
172 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
173 fprintf(fp, "#ifndef __WMCGENERATED_%08lx_H\n", now);
174 fprintf(fp, "#define __WMCGENERATED_%08lx_H\n", now);
177 /* Write severity and facility aliases */
178 get_tokentable(&ttab, &ntab);
179 fprintf(fp, "/* Severity codes */\n");
180 for(i = 0; i < ntab; i++)
182 if(ttab[i].type == tok_severity && ttab[i].alias)
184 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
185 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
191 fprintf(fp, "/* Facility codes */\n");
192 for(i = 0; i < ntab; i++)
194 if(ttab[i].type == tok_facility && ttab[i].alias)
196 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
197 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
203 /* Write the message codes */
204 fprintf(fp, "/* Message definitions */\n");
205 for(ndp = nodehead; ndp; ndp = ndp->next)
210 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
214 fprintf(fp, "/* %s */\n", cptr);
223 * Search for an english text.
224 * If not found, then use the first in the list
227 for(i = 0; i < ndp->u.msg->nmsgs; i++)
229 if(ndp->u.msg->msgs[i]->lan == 0x409)
237 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
238 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
241 fprintf(fp, "/* Approx. msg: %s */\n", cptr);
243 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
245 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
248 switch(ndp->u.msg->base)
252 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
254 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
258 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
260 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
264 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
266 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
269 internal_error(__FILE__, __LINE__, "Invalid base for number print");
276 internal_error(__FILE__, __LINE__, "Invalid node type %d", ndp->type);
279 fprintf(fp, "\n#endif\n");
283 static void write_rcbin(FILE *fp)
290 get_tokentable(&ttab, &ntab);
292 for(lbp = lanblockhead; lbp; lbp = lbp->next)
295 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
296 for(i = 0; i < ntab; i++)
298 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
301 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
306 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
307 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
312 static char *make_string(WCHAR *uc, int len, int codepage)
314 char *str = xmalloc(7*len + 1);
324 for(i = b = 0; i < len; i++, uc++)
328 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
329 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
330 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
331 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
332 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
333 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
334 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
335 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
336 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
338 if (*uc < 0x100 && isprint(*uc))
345 int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
351 if(i < len-1 && b >= 72)
363 len = (len + 1) & ~1;
365 len = (len + 3) & ~3;
380 char *tmp = xmalloc(2*len+1);
382 const union cptable *cpdef = find_codepage(codepage);
384 assert(cpdef != NULL);
385 if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
386 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
389 for(i = b = 0; i < len; i++, cc++)
393 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
394 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
395 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
396 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
397 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
398 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
399 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
400 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
401 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
410 int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
416 if(i < len-1 && b >= 72)
426 len = (len + 3) & ~3;
441 static void write_rcinline(FILE *fp)
447 for(lbp = lanblockhead; lbp; lbp = lbp->next)
449 unsigned offs = 4 * (lbp->nblk * 3 + 1);
450 fprintf(fp, "\n1 MESSAGETABLE\n");
451 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
453 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
454 for(i = 0; i < lbp->nblk; i++)
456 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
460 offs += lbp->blks[i].size;
462 for(i = 0; i < lbp->nblk; i++)
464 block_t *blk = &lbp->blks[i];
465 for(j = 0; j < blk->nmsg; j++)
468 int l = blk->msgs[j]->len;
469 char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
470 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
471 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
473 (unicodeout ? (l*2+3)&~3 : (l+3)&~3) + 4,
474 unicodeout ? '1' : '0');
475 fprintf(fp, "%s%s\n", cptr, comma);
483 void write_rc_file(const char *fname)
488 fp = fopen(fname, "w");
496 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
505 void write_bin_files(void)
507 assert(rcinline == 0);