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
++)
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;
335 n
= sprintf(cptr
, "\\x%04x", *uc
& 0xffff);
343 n
= sprintf(cptr
, "\\x%04x", *uc
& 0xffff);
347 if(i
< len
-1 && b
>= 72)
358 len
= (len
+ 3) & ~3;
373 char *tmp
= xmalloc(2*len
+1);
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
);
382 for(i
= b
= 0; i
< len
; i
++, 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;
404 n
= sprintf(cptr
, "\\x%02x", *cc
& 0xff);
409 if(i
< len
-1 && b
>= 72)
419 len
= (len
+ 3) & ~3;
434 static void write_rcinline(FILE *fp
)
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);
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",
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
++)
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",
466 (unicodeout
? (l
*2+3)&~3 : (l
+3)&~3)+4,
467 unicodeout
? '1' : '0');
468 fprintf(fp
, "%s%s\n", cptr
, comma
);
476 void write_rc_file(const char *fname
)
481 fp
= fopen(fname
, "w");
489 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
, cptr
);
498 void write_bin_files(void)
500 assert(rcinline
== 0);