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
22 #include "wine/port.h"
36 * The binary resource layout is as follows:
47 * Block 1 | Low ID | |
52 * +===============+ | |
56 * +===============+ <-+ |
57 * B0 LoID | Len | Flags | |
67 * B0 HiID | Len | Flags | |
72 * +===============+ <----+
73 * B1 LoID | Len | Flags |
84 * All Fields are aligned on their natural boundaries. The length
85 * field (Len) covers both the length of the string and the header
86 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
87 * for normal character strings and 1 for unicode strings.
90 static const char str_header
[] =
91 "/* This file is generated with wmc version " PACKAGE_VERSION
". Do not edit! */\n"
97 static char *dup_u2c(const WCHAR
*uc
)
100 char *cptr
= xmalloc( unistrlen(uc
)+1 );
102 for (i
= 0; *uc
; i
++, uc
++) cptr
[i
] = (*uc
<= 0xff) ? *uc
: '_';
107 static void killnl(char *s
, int ddd
)
110 tmp
= strstr(s
, "\r\n");
113 if(ddd
&& tmp
- s
> 3)
115 tmp
[0] = tmp
[1] = tmp
[2] = '.';
121 tmp
= strchr(s
, '\n');
124 if(ddd
&& tmp
- s
> 3)
126 tmp
[0] = tmp
[1] = tmp
[2] = '.';
134 static int killcomment(char *s
)
138 while((tmp
= strstr(tmp
, "/*")))
144 while((tmp
= strstr(tmp
, "*/")))
152 void write_h_file(const char *fname
)
164 fp
= fopen(fname
, "w");
170 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
);
171 fprintf(fp
, "#ifndef __WMCGENERATED_H\n");
172 fprintf(fp
, "#define __WMCGENERATED_H\n");
175 /* Write severity and facility aliases */
176 get_tokentable(&ttab
, &ntab
);
177 fprintf(fp
, "/* Severity codes */\n");
178 for(i
= 0; i
< ntab
; i
++)
180 if(ttab
[i
].type
== tok_severity
&& ttab
[i
].alias
)
182 cptr
= dup_u2c(ttab
[i
].alias
);
183 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
189 fprintf(fp
, "/* Facility codes */\n");
190 for(i
= 0; i
< ntab
; i
++)
192 if(ttab
[i
].type
== tok_facility
&& ttab
[i
].alias
)
194 cptr
= dup_u2c(ttab
[i
].alias
);
195 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
201 /* Write the message codes */
202 fprintf(fp
, "/* Message definitions */\n");
203 for(ndp
= nodehead
; ndp
; ndp
= ndp
->next
)
208 cptr
= dup_u2c(ndp
->u
.comment
+1);
212 fprintf(fp
, "/* %s */\n", cptr
);
221 * Search for an English text.
222 * If not found, then use the first in the list
225 for(i
= 0; i
< ndp
->u
.msg
->nmsgs
; i
++)
227 if(ndp
->u
.msg
->msgs
[i
]->lan
== 0x409)
235 fprintf(fp
, "/* MessageId : 0x%08x */\n", ndp
->u
.msg
->realid
);
236 cptr
= dup_u2c(ndp
->u
.msg
->msgs
[idx_en
]->msg
);
239 fprintf(fp
, "/* Approximate msg: %s */\n", cptr
);
241 cptr
= dup_u2c(ndp
->u
.msg
->sym
);
243 cast
= dup_u2c(ndp
->u
.msg
->cast
);
246 switch(ndp
->u
.msg
->base
)
250 fprintf(fp
, "#define %s\t((%s)0%oL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
252 fprintf(fp
, "#define %s\t0%oL\n\n", cptr
, ndp
->u
.msg
->realid
);
256 fprintf(fp
, "#define %s\t((%s)%dL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
258 fprintf(fp
, "#define %s\t%dL\n\n", cptr
, ndp
->u
.msg
->realid
);
262 fprintf(fp
, "#define %s\t((%s)0x%08xL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
264 fprintf(fp
, "#define %s\t0x%08xL\n\n", cptr
, ndp
->u
.msg
->realid
);
267 internal_error(__FILE__
, __LINE__
, "Invalid base for number print\n");
273 internal_error(__FILE__
, __LINE__
, "Invalid node type %d\n", ndp
->type
);
276 fprintf(fp
, "\n#endif\n");
280 static void write_rcbin(FILE *fp
)
287 get_tokentable(&ttab
, &ntab
);
289 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
292 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
293 for(i
= 0; i
< ntab
; i
++)
295 if(ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
298 cptr
= dup_u2c(ttab
[i
].alias
);
303 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
304 fprintf(fp
, "1 MESSAGETABLE \"%s.bin\"\n", cptr
);
309 static char *make_string(WCHAR
*uc
, int len
)
311 char *str
= xmalloc(7*len
+ 12);
319 for(i
= b
= 0; i
< len
; i
++, uc
++)
323 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
324 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
325 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
326 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
327 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
328 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
329 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
330 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
331 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
333 if (*uc
< 0x100 && isprint(*uc
))
340 int n
= sprintf(cptr
, "\\x%04x", *uc
);
346 if(i
< len
-1 && b
>= 72)
357 len
= (len
+ 1) & ~1;
372 static void write_rcinline(FILE *fp
)
378 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
380 unsigned offs
= 4 * (lbp
->nblk
* 3 + 1);
381 fprintf(fp
, "\n1 MESSAGETABLE\n");
382 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
384 fprintf(fp
, " /* NBlocks */ 0x%08xL,\n", lbp
->nblk
);
385 for(i
= 0; i
< lbp
->nblk
; i
++)
387 fprintf(fp
, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
391 offs
+= lbp
->blks
[i
].size
;
393 for(i
= 0; i
< lbp
->nblk
; i
++)
395 block_t
*blk
= &lbp
->blks
[i
];
396 for(j
= 0; j
< blk
->nmsg
; j
++)
399 int l
= blk
->msgs
[j
]->len
;
400 const char *comma
= j
== blk
->nmsg
-1 && i
== lbp
->nblk
-1 ? "" : ",";
401 cptr
= make_string(blk
->msgs
[j
]->msg
, l
);
402 fprintf(fp
, "\n /* Msg 0x%08x */ 0x%04x, 0x0001,\n",
403 blk
->idlo
+ j
, ((l
* 2 + 3) & ~3) + 4 );
404 fprintf(fp
, "%s%s\n", cptr
, comma
);
412 void write_rc_file(const char *fname
)
414 FILE *fp
= fopen(fname
, "w");
421 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
);
430 static void output_bin_data( lan_blk_t
*lbp
)
432 unsigned int offs
= 4 * (lbp
->nblk
* 3 + 1);
435 put_dword( lbp
->nblk
); /* NBlocks */
436 for (i
= 0; i
< lbp
->nblk
; i
++)
438 put_dword( lbp
->blks
[i
].idlo
); /* Lo */
439 put_dword( lbp
->blks
[i
].idhi
); /* Hi */
440 put_dword( offs
); /* Offs */
441 offs
+= lbp
->blks
[i
].size
;
443 for (i
= 0; i
< lbp
->nblk
; i
++)
445 block_t
*blk
= &lbp
->blks
[i
];
446 for (j
= 0; j
< blk
->nmsg
; j
++)
448 int len
= (2 * blk
->msgs
[j
]->len
+ 3) & ~3;
451 for (k
= 0; k
< blk
->msgs
[j
]->len
; k
++) put_word( blk
->msgs
[j
]->msg
[k
] );
457 void write_bin_files(void)
464 get_tokentable(&ttab
, &ntab
);
466 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
470 for (i
= 0; i
< ntab
; i
++)
472 if (ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
474 if (ttab
[i
].alias
) cptr
= dup_u2c(ttab
[i
].alias
);
479 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
480 init_output_buffer();
481 output_bin_data( lbp
);
482 cptr
= xrealloc( cptr
, strlen(cptr
) + 5 );
483 strcat( cptr
, ".bin" );
484 flush_output_buffer( cptr
);
489 void write_res_file( const char *name
)
494 init_output_buffer();
496 put_dword( 0 ); /* ResSize */
497 put_dword( 32 ); /* HeaderSize */
498 put_word( 0xffff ); /* ResType */
500 put_word( 0xffff ); /* ResName */
502 put_dword( 0 ); /* DataVersion */
503 put_word( 0 ); /* Memory options */
504 put_word( 0 ); /* Language */
505 put_dword( 0 ); /* Version */
506 put_dword( 0 ); /* Characteristics */
508 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
510 unsigned int data_size
= 4 * (lbp
->nblk
* 3 + 1);
511 unsigned int header_size
= 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
513 for (i
= 0; i
< lbp
->nblk
; i
++)
515 block_t
*blk
= &lbp
->blks
[i
];
516 for (j
= 0; j
< blk
->nmsg
; j
++) data_size
+= 4 + ((blk
->msgs
[j
]->len
* 2 + 3) & ~3);
519 put_dword( data_size
); /* ResSize */
520 put_dword( header_size
); /* HeaderSize */
521 put_word( 0xffff ); /* ResType */
522 put_word( 0x000b /*RT_MESSAGETABLE*/ );
523 put_word( 0xffff ); /* ResName */
526 put_dword( 0 ); /* DataVersion */
527 put_word( 0x30 ); /* Memory options */
528 put_word( lbp
->lan
); /* Language */
529 put_dword( lbp
->version
); /* Version */
530 put_dword( 0 ); /* Characteristics */
532 output_bin_data( lbp
);
534 flush_output_buffer( name
);