If there is a single target, it should also get the 'project_settings
[wine/multimedia.git] / tools / wmc / write.c
blob035b9b75bfff40cfb4d780ae4edac4a069a90ac4
1 /*
2 * Wine Message Compiler output generation
4 * Copyright 2000 Bertho A. Stultiens (BS)
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <ctype.h>
14 #include "wmc.h"
15 #include "utils.h"
16 #include "lang.h"
17 #include "write.h"
20 * The binary resource layout is as follows:
22 * +===============+
23 * Header | NBlocks |
24 * +===============+
25 * Block 0 | Low ID |
26 * +---------------+
27 * | High ID |
28 * +---------------+
29 * | Offset |---+
30 * +===============+ |
31 * Block 1 | Low ID | |
32 * +---------------+ |
33 * | High ID | |
34 * +---------------+ |
35 * | Offset |------+
36 * +===============+ | |
37 * | | | |
38 * ... ... | |
39 * | | | |
40 * +===============+ <-+ |
41 * B0 LoID | Len | Flags | |
42 * +---+---+---+---+ |
43 * | b | l | a | b | |
44 * +---+---+---+---+ |
45 * | l | a | \0| \0| |
46 * +===============+ |
47 * | | |
48 * ... ... |
49 * | | |
50 * +===============+ |
51 * B0 HiID | Len | Flags | |
52 * +---+---+---+---+ |
53 * | M | o | r | e | |
54 * +---+---+---+---+ |
55 * | b | l | a | \0| |
56 * +===============+ <----+
57 * B1 LoID | Len | Flags |
58 * +---+---+---+---+
59 * | J | u | n | k |
60 * +---+---+---+---+
61 * | \0| \0| \0| \0|
62 * +===============+
63 * | |
64 * ... ...
65 * | |
66 * +===============+
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"
76 "/* Source : %s */\n"
77 "/* Cmdline: %s */\n"
78 "/* Date : %s */\n"
79 "\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);
87 if(!cpdef)
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);
91 return cptr;
94 static void killnl(char *s, int ddd)
96 char *tmp;
97 tmp = strstr(s, "\r\n");
98 if(tmp)
100 if(ddd && tmp - s > 3)
102 tmp[0] = tmp[1] = tmp[2] = '.';
103 tmp[3] = '\0';
105 else
106 *tmp = '\0';
108 tmp = strchr(s, '\n');
109 if(tmp)
111 if(ddd && tmp - s > 3)
113 tmp[0] = tmp[1] = tmp[2] = '.';
114 tmp[3] = '\0';
116 else
117 *tmp = '\0';
121 static int killcomment(char *s)
123 char *tmp = s;
124 int b = 0;
125 while((tmp = strstr(tmp, "/*")))
127 tmp[1] = 'x';
128 b++;
130 tmp = s;
131 while((tmp = strstr(tmp, "*/")))
133 tmp[0] = 'x';
134 b++;
136 return b;
139 void write_h_file(const char *fname)
141 node_t *ndp;
142 char *cptr;
143 char *cast;
144 FILE *fp;
145 token_t *ttab;
146 int ntab;
147 int i;
148 int once = 0;
149 int idx_en = 0;
151 fp = fopen(fname, "w");
152 if(!fp)
154 perror(fname);
155 exit(1);
157 cptr = ctime(&now);
158 killnl(cptr, 0);
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);
162 fprintf(fp, "\n");
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);
173 free(cptr);
176 fprintf(fp, "\n");
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);
185 free(cptr);
188 fprintf(fp, "\n");
190 /* Write the message codes */
191 fprintf(fp, "/* Message definitions */\n");
192 for(ndp = nodehead; ndp; ndp = ndp->next)
194 switch(ndp->type)
196 case nd_comment:
197 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
198 killnl(cptr, 0);
199 killcomment(cptr);
200 if(*cptr)
201 fprintf(fp, "/* %s */\n", cptr);
202 else
203 fprintf(fp, "\n");
204 free(cptr);
205 break;
206 case nd_msg:
207 if(!once)
210 * Search for an english text.
211 * If not found, then use the first in the list
213 once++;
214 for(i = 0; i < ndp->u.msg->nmsgs; i++)
216 if(ndp->u.msg->msgs[i]->lan == 0x409)
218 idx_en = i;
219 break;
222 fprintf(fp, "\n");
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);
226 killnl(cptr, 0);
227 killcomment(cptr);
228 fprintf(fp, "/* Approx. msg: %s */\n", cptr);
229 free(cptr);
230 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
231 if(ndp->u.msg->cast)
232 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
233 else
234 cast = NULL;
235 switch(ndp->u.msg->base)
237 case 8:
238 if(cast)
239 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
240 else
241 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
242 break;
243 case 10:
244 if(cast)
245 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
246 else
247 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
248 break;
249 case 16:
250 if(cast)
251 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
252 else
253 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
254 break;
255 default:
256 internal_error(__FILE__, __LINE__, "Invalid base for number print");
258 free(cptr);
259 if(cast)
260 free(cast);
261 break;
262 default:
263 internal_error(__FILE__, __LINE__, "Invalid node type %d", ndp->type);
266 fprintf(fp, "\n#endif\n");
267 fclose(fp);
270 static void write_rcbin(FILE *fp)
272 lan_blk_t *lbp;
273 token_t *ttab;
274 int ntab;
275 int i;
277 get_tokentable(&ttab, &ntab);
279 for(lbp = lanblockhead; lbp; lbp = lbp->next)
281 char *cptr = NULL;
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)
287 if(ttab[i].alias)
288 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
289 break;
292 if(!cptr)
293 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
294 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
295 free(cptr);
299 static char *make_string(WCHAR *uc, int len, int codepage)
301 char *str = xmalloc(7*len + 1);
302 char *cptr = str;
303 int i;
304 int b;
306 if(!codepage)
308 *cptr++ = ' ';
309 *cptr++ = 'L';
310 *cptr++ = '"';
311 for(i = b = 0; i < len; i++, uc++)
313 int n;
314 if(*uc < 0x100)
316 if(isprint(*uc))
318 *cptr++ = *uc;
319 b++;
321 else
323 switch(*uc)
325 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
326 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
327 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
328 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
329 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
330 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
331 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
332 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
333 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
334 default:
335 n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
336 cptr += n;
337 b += n;
341 else
343 n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
344 cptr += n;
345 b += n;
347 if(i < len-1 && b >= 72)
349 *cptr++ = '"';
350 *cptr++ = ',';
351 *cptr++ = '\n';
352 *cptr++ = ' ';
353 *cptr++ = 'L';
354 *cptr++ = '"';
355 b = 0;
358 len = (len + 3) & ~3;
359 for(; i < len; i++)
361 *cptr++ = '\\';
362 *cptr++ = 'x';
363 *cptr++ = '0';
364 *cptr++ = '0';
365 *cptr++ = '0';
366 *cptr++ = '0';
368 *cptr++ = '"';
369 *cptr = '\0';
371 else
373 char *tmp = xmalloc(2*len+1);
374 char *cc = tmp;
375 const union cptable *cpdef = find_codepage(codepage);
377 assert(cpdef != NULL);
378 if((i = cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0)
379 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
380 *cptr++ = ' ';
381 *cptr++ = '"';
382 for(i = b = 0; i < len; i++, cc++)
384 int n;
385 if(isprint(*cc))
387 *cptr++ = *cc;
388 b++;
390 else
392 switch(*cc)
394 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
395 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
396 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
397 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
398 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
399 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
400 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
401 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
402 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
403 default:
404 n = sprintf(cptr, "\\x%02x", *cc & 0xff);
405 cptr += n;
406 b += n;
409 if(i < len-1 && b >= 72)
411 *cptr++ = '"';
412 *cptr++ = ',';
413 *cptr++ = '\n';
414 *cptr++ = ' ';
415 *cptr++ = '"';
416 b = 0;
419 len = (len + 3) & ~3;
420 for(; i < len; i++)
422 *cptr++ = '\\';
423 *cptr++ = 'x';
424 *cptr++ = '0';
425 *cptr++ = '0';
427 *cptr++ = '"';
428 *cptr = '\0';
429 free(tmp);
431 return str;
434 static void write_rcinline(FILE *fp)
436 lan_blk_t *lbp;
437 int i;
438 int j;
440 for(lbp = lanblockhead; lbp; lbp = lbp->next)
442 unsigned offs = 4 * (lbp->nblk * 3 + 1);
443 fprintf(fp, "\n1 MESSAGETABLE\n");
444 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
445 fprintf(fp, "{\n");
446 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
447 for(i = 0; i < lbp->nblk; i++)
449 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
450 lbp->blks[i].idlo,
451 lbp->blks[i].idhi,
452 offs);
453 offs += lbp->blks[i].size;
455 for(i = 0; i < lbp->nblk; i++)
457 block_t *blk = &lbp->blks[i];
458 for(j = 0; j < blk->nmsg; j++)
460 char *cptr;
461 int l = blk->msgs[j]->len;
462 char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
463 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
464 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
465 blk->idlo + j,
466 (unicodeout ? (l*2+3)&~3 : (l+3)&~3)+4,
467 unicodeout ? '1' : '0');
468 fprintf(fp, "%s%s\n", cptr, comma);
469 free(cptr);
472 fprintf(fp, "}\n");
476 void write_rc_file(const char *fname)
478 FILE *fp;
479 char *cptr;
481 fp = fopen(fname, "w");
482 if(!fp)
484 perror(fname);
485 exit(1);
487 cptr = ctime(&now);
488 killnl(cptr, 0);
489 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
491 if(rcinline)
492 write_rcinline(fp);
493 else
494 write_rcbin(fp);
495 fclose(fp);
498 void write_bin_files(void)
500 assert(rcinline == 0);