wineps: Don't use CDECL for private functions.
[wine.git] / tools / wmc / write.c
blob353a503f67490d730f4af19f550954262e5e29a3
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <ctype.h>
29 #include "wmc.h"
30 #include "utils.h"
31 #include "lang.h"
32 #include "write.h"
35 * The binary resource layout is as follows:
37 * +===============+
38 * Header | NBlocks |
39 * +===============+
40 * Block 0 | Low ID |
41 * +---------------+
42 * | High ID |
43 * +---------------+
44 * | Offset |---+
45 * +===============+ |
46 * Block 1 | Low ID | |
47 * +---------------+ |
48 * | High ID | |
49 * +---------------+ |
50 * | Offset |------+
51 * +===============+ | |
52 * | | | |
53 * ... ... | |
54 * | | | |
55 * +===============+ <-+ |
56 * B0 LoID | Len | Flags | |
57 * +---+---+---+---+ |
58 * | b | l | a | b | |
59 * +---+---+---+---+ |
60 * | l | a | \0| \0| |
61 * +===============+ |
62 * | | |
63 * ... ... |
64 * | | |
65 * +===============+ |
66 * B0 HiID | Len | Flags | |
67 * +---+---+---+---+ |
68 * | M | o | r | e | |
69 * +---+---+---+---+ |
70 * | b | l | a | \0| |
71 * +===============+ <----+
72 * B1 LoID | Len | Flags |
73 * +---+---+---+---+
74 * | J | u | n | k |
75 * +---+---+---+---+
76 * | \0| \0| \0| \0|
77 * +===============+
78 * | |
79 * ... ...
80 * | |
81 * +===============+
83 * All Fields are aligned on their natural boundaries. The length
84 * field (Len) covers both the length of the string and the header
85 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
86 * for normal character strings and 1 for unicode strings.
89 static const char str_header[] =
90 "/* This file is generated with wmc version " PACKAGE_VERSION ". Do not edit! */\n"
91 "/* Source : %s */\n"
92 "/* Cmdline: %s */\n"
93 "\n"
96 static char *dup_u2c(const WCHAR *uc)
98 int i;
99 char *cptr = xmalloc( unistrlen(uc)+1 );
101 for (i = 0; *uc; i++, uc++) cptr[i] = (*uc <= 0xff) ? *uc : '_';
102 cptr[i] = 0;
103 return cptr;
106 static void killnl(char *s, int ddd)
108 char *tmp;
109 tmp = strstr(s, "\r\n");
110 if(tmp)
112 if(ddd && tmp - s > 3)
114 tmp[0] = tmp[1] = tmp[2] = '.';
115 tmp[3] = '\0';
117 else
118 *tmp = '\0';
120 tmp = strchr(s, '\n');
121 if(tmp)
123 if(ddd && tmp - s > 3)
125 tmp[0] = tmp[1] = tmp[2] = '.';
126 tmp[3] = '\0';
128 else
129 *tmp = '\0';
133 static int killcomment(char *s)
135 char *tmp = s;
136 int b = 0;
137 while((tmp = strstr(tmp, "/*")))
139 tmp[1] = 'x';
140 b++;
142 tmp = s;
143 while((tmp = strstr(tmp, "*/")))
145 tmp[0] = 'x';
146 b++;
148 return b;
151 void write_h_file(const char *fname)
153 struct node *ndp;
154 char *cptr;
155 char *cast;
156 FILE *fp;
157 struct token *ttab;
158 int ntab;
159 int i;
160 int once = 0;
161 int idx_en = 0;
163 fp = fopen(fname, "w");
164 if(!fp)
166 perror(fname);
167 exit(1);
169 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
170 fprintf(fp, "#ifndef __WMCGENERATED_H\n");
171 fprintf(fp, "#define __WMCGENERATED_H\n");
172 fprintf(fp, "\n");
174 /* Write severity and facility aliases */
175 get_tokentable(&ttab, &ntab);
176 fprintf(fp, "/* Severity codes */\n");
177 for(i = 0; i < ntab; i++)
179 if(ttab[i].type == tok_severity && ttab[i].alias)
181 cptr = dup_u2c(ttab[i].alias);
182 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
183 free(cptr);
186 fprintf(fp, "\n");
188 fprintf(fp, "/* Facility codes */\n");
189 for(i = 0; i < ntab; i++)
191 if(ttab[i].type == tok_facility && ttab[i].alias)
193 cptr = dup_u2c(ttab[i].alias);
194 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
195 free(cptr);
198 fprintf(fp, "\n");
200 /* Write the message codes */
201 fprintf(fp, "/* Message definitions */\n");
202 for(ndp = nodehead; ndp; ndp = ndp->next)
204 switch(ndp->type)
206 case nd_comment:
207 cptr = dup_u2c(ndp->u.comment+1);
208 killnl(cptr, 0);
209 killcomment(cptr);
210 if(*cptr)
211 fprintf(fp, "/* %s */\n", cptr);
212 else
213 fprintf(fp, "\n");
214 free(cptr);
215 break;
216 case nd_msg:
217 if(!once)
220 * Search for an English text.
221 * If not found, then use the first in the list
223 once++;
224 for(i = 0; i < ndp->u.msg->nmsgs; i++)
226 if(ndp->u.msg->msgs[i]->lan == 0x409)
228 idx_en = i;
229 break;
232 fprintf(fp, "\n");
234 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
235 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->msg);
236 killnl(cptr, 0);
237 killcomment(cptr);
238 fprintf(fp, "/* Approximate msg: %s */\n", cptr);
239 free(cptr);
240 cptr = dup_u2c(ndp->u.msg->sym);
241 if(ndp->u.msg->cast)
242 cast = dup_u2c(ndp->u.msg->cast);
243 else
244 cast = NULL;
245 switch(ndp->u.msg->base)
247 case 8:
248 if(cast)
249 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
250 else
251 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
252 break;
253 case 10:
254 if(cast)
255 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
256 else
257 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
258 break;
259 case 16:
260 if(cast)
261 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
262 else
263 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
264 break;
265 default:
266 internal_error(__FILE__, __LINE__, "Invalid base for number print\n");
268 free(cptr);
269 free(cast);
270 break;
271 default:
272 internal_error(__FILE__, __LINE__, "Invalid node type %d\n", ndp->type);
275 fprintf(fp, "\n#endif\n");
276 fclose(fp);
279 static void write_rcbin(FILE *fp)
281 struct lan_blk *lbp;
282 struct token *ttab;
283 int ntab;
284 int i;
286 get_tokentable(&ttab, &ntab);
288 for(lbp = lanblockhead; lbp; lbp = lbp->next)
290 char *cptr = NULL;
291 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
292 for(i = 0; i < ntab; i++)
294 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
296 if(ttab[i].alias)
297 cptr = dup_u2c(ttab[i].alias);
298 break;
301 if(!cptr)
302 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
303 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
304 free(cptr);
308 static char *make_string(WCHAR *uc, int len)
310 char *str = xmalloc(7*len + 12);
311 char *cptr = str;
312 int i;
313 int b;
315 *cptr++ = ' ';
316 *cptr++ = 'L';
317 *cptr++ = '"';
318 for(i = b = 0; i < len; i++, uc++)
320 switch(*uc)
322 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
323 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
324 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
325 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
326 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
327 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
328 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
329 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
330 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
331 default:
332 if (*uc < 0x100 && isprint(*uc))
334 *cptr++ = *uc;
335 b++;
337 else
339 int n = sprintf(cptr, "\\x%04x", *uc);
340 cptr += n;
341 b += n;
343 break;
345 if(i < len-1 && b >= 72)
347 *cptr++ = '"';
348 *cptr++ = ',';
349 *cptr++ = '\n';
350 *cptr++ = ' ';
351 *cptr++ = 'L';
352 *cptr++ = '"';
353 b = 0;
356 len = (len + 1) & ~1;
357 for(; i < len; i++)
359 *cptr++ = '\\';
360 *cptr++ = 'x';
361 *cptr++ = '0';
362 *cptr++ = '0';
363 *cptr++ = '0';
364 *cptr++ = '0';
366 *cptr++ = '"';
367 *cptr = '\0';
368 return str;
371 static void write_rcinline(FILE *fp)
373 struct lan_blk *lbp;
374 int i;
375 int j;
377 for(lbp = lanblockhead; lbp; lbp = lbp->next)
379 unsigned offs = 4 * (lbp->nblk * 3 + 1);
380 fprintf(fp, "\n1 MESSAGETABLE\n");
381 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
382 fprintf(fp, "{\n");
383 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
384 for(i = 0; i < lbp->nblk; i++)
386 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
387 lbp->blks[i].idlo,
388 lbp->blks[i].idhi,
389 offs);
390 offs += lbp->blks[i].size;
392 for(i = 0; i < lbp->nblk; i++)
394 struct block *blk = &lbp->blks[i];
395 for(j = 0; j < blk->nmsg; j++)
397 char *cptr;
398 int l = blk->msgs[j]->len;
399 const char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
400 cptr = make_string(blk->msgs[j]->msg, l);
401 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x0001,\n",
402 blk->idlo + j, ((l * 2 + 3) & ~3) + 4 );
403 fprintf(fp, "%s%s\n", cptr, comma);
404 free(cptr);
407 fprintf(fp, "}\n");
411 void write_rc_file(const char *fname)
413 FILE *fp = fopen(fname, "w");
415 if(!fp)
417 perror(fname);
418 exit(1);
420 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
422 if(rcinline)
423 write_rcinline(fp);
424 else
425 write_rcbin(fp);
426 fclose(fp);
429 static void output_bin_data( struct lan_blk *lbp )
431 unsigned int offs = 4 * (lbp->nblk * 3 + 1);
432 int i, j, k;
434 put_dword( lbp->nblk ); /* NBlocks */
435 for (i = 0; i < lbp->nblk; i++)
437 put_dword( lbp->blks[i].idlo ); /* Lo */
438 put_dword( lbp->blks[i].idhi ); /* Hi */
439 put_dword( offs ); /* Offs */
440 offs += lbp->blks[i].size;
442 for (i = 0; i < lbp->nblk; i++)
444 struct block *blk = &lbp->blks[i];
445 for (j = 0; j < blk->nmsg; j++)
447 int len = (2 * blk->msgs[j]->len + 3) & ~3;
448 put_word( len + 4 );
449 put_word( 1 );
450 for (k = 0; k < blk->msgs[j]->len; k++) put_word( blk->msgs[j]->msg[k] );
451 align_output( 4 );
456 void write_bin_files(void)
458 struct lan_blk *lbp;
459 struct token *ttab;
460 int ntab;
461 int i;
463 get_tokentable(&ttab, &ntab);
465 for (lbp = lanblockhead; lbp; lbp = lbp->next)
467 char *cptr = NULL;
469 for (i = 0; i < ntab; i++)
471 if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
473 if (ttab[i].alias) cptr = dup_u2c(ttab[i].alias);
474 break;
477 if(!cptr)
478 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
479 init_output_buffer();
480 output_bin_data( lbp );
481 cptr = xrealloc( cptr, strlen(cptr) + 5 );
482 strcat( cptr, ".bin" );
483 flush_output_buffer( cptr );
484 free(cptr);
488 void write_res_file( const char *name )
490 struct lan_blk *lbp;
491 int i, j;
493 init_output_buffer();
495 put_dword( 0 ); /* ResSize */
496 put_dword( 32 ); /* HeaderSize */
497 put_word( 0xffff ); /* ResType */
498 put_word( 0x0000 );
499 put_word( 0xffff ); /* ResName */
500 put_word( 0x0000 );
501 put_dword( 0 ); /* DataVersion */
502 put_word( 0 ); /* Memory options */
503 put_word( 0 ); /* Language */
504 put_dword( 0 ); /* Version */
505 put_dword( 0 ); /* Characteristics */
507 for (lbp = lanblockhead; lbp; lbp = lbp->next)
509 unsigned int data_size = 4 * (lbp->nblk * 3 + 1);
510 unsigned int header_size = 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
512 for (i = 0; i < lbp->nblk; i++)
514 struct block *blk = &lbp->blks[i];
515 for (j = 0; j < blk->nmsg; j++) data_size += 4 + ((blk->msgs[j]->len * 2 + 3) & ~3);
518 put_dword( data_size ); /* ResSize */
519 put_dword( header_size ); /* HeaderSize */
520 put_word( 0xffff ); /* ResType */
521 put_word( 0x000b /*RT_MESSAGETABLE*/ );
522 put_word( 0xffff ); /* ResName */
523 put_word( 0x0001 );
524 align_output( 4 );
525 put_dword( 0 ); /* DataVersion */
526 put_word( 0x30 ); /* Memory options */
527 put_word( lbp->lan ); /* Language */
528 put_dword( lbp->version ); /* Version */
529 put_dword( 0 ); /* Characteristics */
531 output_bin_data( lbp );
533 flush_output_buffer( name );