2 * Wine Message Compiler output generation
4 * Copyright 2000 Bertho A. Stultiens (BS)
20 * The binary resource layout is as follows:
31 * Block 1 | Low ID | |
36 * +===============+ | |
40 * +===============+ <-+ |
41 * B0 LoID | Len | Flags | |
51 * B0 HiID | Len | Flags | |
56 * +===============+ <----+
57 * B1 LoID | Len | Flags |
68 * All Fields are aligned on their natural boundaries. The length
69 * field (Len) covers both the length of the string and the header
70 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
71 * for normal character strings and 1 for unicode strings.
74 static char str_header[] =
75 "/* This file is generated with wmc version " WMC_FULLVERSION ". Do not edit! */\n"
82 static char *dup_u2c(int cp, const WCHAR *uc)
84 int len = unistrlen(uc);
85 char *cptr = xmalloc(len+1);
86 const union cptable *cpdef = find_codepage(cp);
88 internal_error(__FILE__, __LINE__, "Codepage %d not found (vanished?)", cp);
89 if((len = cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len+1, NULL, NULL)) < 0)
90 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", len);
94 static void killnl(char *s, int ddd)
97 tmp = strstr(s, "\r\n");
100 if(ddd && tmp - s > 3)
102 tmp[0] = tmp[1] = tmp[2] = '.';
108 tmp = strchr(s, '\n');
111 if(ddd && tmp - s > 3)
113 tmp[0] = tmp[1] = tmp[2] = '.';
121 static int killcomment(char *s)
125 while((tmp = strstr(tmp, "/*")))
131 while((tmp = strstr(tmp, "*/")))
139 void write_h_file(const char *fname)
151 fp = fopen(fname, "w");
159 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
160 fprintf(fp, "#ifndef __WMCGENERATED_%08lx_H\n", now);
161 fprintf(fp, "#define __WMCGENERATED_%08lx_H\n", now);
164 /* Write severity and facility aliases */
165 get_tokentable(&ttab, &ntab);
166 fprintf(fp, "/* Severity codes */\n");
167 for(i = 0; i < ntab; i++)
169 if(ttab[i].type == tok_severity && ttab[i].alias)
171 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
172 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
178 fprintf(fp, "/* Facility codes */\n");
179 for(i = 0; i < ntab; i++)
181 if(ttab[i].type == tok_facility && ttab[i].alias)
183 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
184 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
190 /* Write the message codes */
191 fprintf(fp, "/* Message definitions */\n");
192 for(ndp = nodehead; ndp; ndp = ndp->next)
197 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
201 fprintf(fp, "/* %s */\n", cptr);
210 * Search for an english text.
211 * If not found, then use the first in the list
214 for(i = 0; i < ndp->u.msg->nmsgs; i++)
216 if(ndp->u.msg->msgs[i]->lan == 0x409)
224 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
225 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
228 fprintf(fp, "/* Approx. msg: %s */\n", cptr);
230 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
232 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
235 switch(ndp->u.msg->base)
239 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
241 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
245 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
247 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
251 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
253 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
256 internal_error(__FILE__, __LINE__, "Invalid base for number print");
263 internal_error(__FILE__, __LINE__, "Invalid node type %d", ndp->type);
266 fprintf(fp, "\n#endif\n");
270 static void write_rcbin(FILE *fp)
277 get_tokentable(&ttab, &ntab);
279 for(lbp = lanblockhead; lbp; lbp = lbp->next)
282 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
283 for(i = 0; i < ntab; i++)
285 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
288 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
293 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
294 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
299 static char *make_string(WCHAR *uc, int len, int codepage)
301 char *str = xmalloc(7*len + 1);
311 for(i = b = 0; i < len; i++, uc++)
315 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
316 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
317 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
318 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
319 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
320 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
321 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
322 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
323 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
325 if (*uc < 0x100 && isprint(*uc))
332 int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
338 if(i < len-1 && b >= 72)
349 len = (len + 3) & ~3;
364 char *tmp = xmalloc(2*len+1);
366 const union cptable *cpdef = find_codepage(codepage);
368 assert(cpdef != NULL);
369 if((i = cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
370 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
373 for(i = b = 0; i < len; i++, cc++)
377 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
378 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
379 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
380 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
381 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
382 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
383 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
384 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
385 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
394 int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
400 if(i < len-1 && b >= 72)
410 len = (len + 3) & ~3;
425 static void write_rcinline(FILE *fp)
431 for(lbp = lanblockhead; lbp; lbp = lbp->next)
433 unsigned offs = 4 * (lbp->nblk * 3 + 1);
434 fprintf(fp, "\n1 MESSAGETABLE\n");
435 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
437 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
438 for(i = 0; i < lbp->nblk; i++)
440 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
444 offs += lbp->blks[i].size;
446 for(i = 0; i < lbp->nblk; i++)
448 block_t *blk = &lbp->blks[i];
449 for(j = 0; j < blk->nmsg; j++)
452 int l = blk->msgs[j]->len;
453 char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
454 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
455 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
457 (unicodeout ? (l*2+3)&~3 : (l+3)&~3)+4,
458 unicodeout ? '1' : '0');
459 fprintf(fp, "%s%s\n", cptr, comma);
467 void write_rc_file(const char *fname)
472 fp = fopen(fname, "w");
480 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
489 void write_bin_files(void)
491 assert(rcinline == 0);