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)
99 const union cptable *cpdef = find_codepage(cp);
101 internal_error(__FILE__, __LINE__, "Codepage %d not found (vanished?)", cp);
102 len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
104 if((len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len, NULL, NULL)) < 0)
105 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", len);
109 static void killnl(char *s, int ddd)
112 tmp = strstr(s, "\r\n");
115 if(ddd && tmp - s > 3)
117 tmp[0] = tmp[1] = tmp[2] = '.';
123 tmp = strchr(s, '\n');
126 if(ddd && tmp - s > 3)
128 tmp[0] = tmp[1] = tmp[2] = '.';
136 static int killcomment(char *s)
140 while((tmp = strstr(tmp, "/*")))
146 while((tmp = strstr(tmp, "*/")))
154 void write_h_file(const char *fname)
166 fp = fopen(fname, "w");
174 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
175 fprintf(fp, "#ifndef __WMCGENERATED_%08lx_H\n", (long)now);
176 fprintf(fp, "#define __WMCGENERATED_%08lx_H\n", (long)now);
179 /* Write severity and facility aliases */
180 get_tokentable(&ttab, &ntab);
181 fprintf(fp, "/* Severity codes */\n");
182 for(i = 0; i < ntab; i++)
184 if(ttab[i].type == tok_severity && ttab[i].alias)
186 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
187 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
193 fprintf(fp, "/* Facility codes */\n");
194 for(i = 0; i < ntab; i++)
196 if(ttab[i].type == tok_facility && ttab[i].alias)
198 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
199 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
205 /* Write the message codes */
206 fprintf(fp, "/* Message definitions */\n");
207 for(ndp = nodehead; ndp; ndp = ndp->next)
212 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
216 fprintf(fp, "/* %s */\n", cptr);
225 * Search for an english text.
226 * If not found, then use the first in the list
229 for(i = 0; i < ndp->u.msg->nmsgs; i++)
231 if(ndp->u.msg->msgs[i]->lan == 0x409)
239 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
240 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
243 fprintf(fp, "/* Approx. msg: %s */\n", cptr);
245 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
247 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
250 switch(ndp->u.msg->base)
254 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
256 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
260 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
262 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
266 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
268 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
271 internal_error(__FILE__, __LINE__, "Invalid base for number print");
278 internal_error(__FILE__, __LINE__, "Invalid node type %d", ndp->type);
281 fprintf(fp, "\n#endif\n");
285 static void write_rcbin(FILE *fp)
292 get_tokentable(&ttab, &ntab);
294 for(lbp = lanblockhead; lbp; lbp = lbp->next)
297 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
298 for(i = 0; i < ntab; i++)
300 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
303 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
308 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
309 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
314 static char *make_string(WCHAR *uc, int len, int codepage)
316 char *str = xmalloc(7*len + 1);
326 for(i = b = 0; i < len; i++, uc++)
330 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
331 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
332 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
333 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
334 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
335 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
336 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
337 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
338 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
340 if (*uc < 0x100 && isprint(*uc))
347 int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
353 if(i < len-1 && b >= 72)
365 len = (len + 1) & ~1;
367 len = (len + 3) & ~3;
384 const union cptable *cpdef = find_codepage(codepage);
386 assert(cpdef != NULL);
387 mlen = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
388 cc = tmp = xmalloc(mlen);
389 if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, mlen, NULL, NULL)) < 0)
390 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
393 for(i = b = 0; i < len; i++, cc++)
397 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
398 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
399 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
400 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
401 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
402 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
403 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
404 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
405 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
414 int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
420 if(i < len-1 && b >= 72)
430 len = (len + 3) & ~3;
445 static void write_rcinline(FILE *fp)
451 for(lbp = lanblockhead; lbp; lbp = lbp->next)
453 unsigned offs = 4 * (lbp->nblk * 3 + 1);
454 fprintf(fp, "\n1 MESSAGETABLE\n");
455 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
457 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
458 for(i = 0; i < lbp->nblk; i++)
460 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
464 offs += lbp->blks[i].size;
466 for(i = 0; i < lbp->nblk; i++)
468 block_t *blk = &lbp->blks[i];
469 for(j = 0; j < blk->nmsg; j++)
472 int l = blk->msgs[j]->len;
473 char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
474 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
475 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
477 (unicodeout ? (l*2+3)&~3 : (l+3)&~3) + 4,
478 unicodeout ? '1' : '0');
479 fprintf(fp, "%s%s\n", cptr, comma);
487 void write_rc_file(const char *fname)
492 fp = fopen(fname, "w");
500 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
509 void write_bin_files(void)
511 assert(rcinline == 0);