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
35 * The binary resource layout is as follows:
46 * Block 1 | Low ID | |
51 * +===============+ | |
55 * +===============+ <-+ |
56 * B0 LoID | Len | Flags | |
66 * B0 HiID | Len | Flags | |
71 * +===============+ <----+
72 * B1 LoID | Len | Flags |
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"
96 static char *dup_u2c(const WCHAR
*uc
)
99 char *cptr
= xmalloc( unistrlen(uc
)+1 );
101 for (i
= 0; *uc
; i
++, uc
++) cptr
[i
] = (*uc
<= 0xff) ? *uc
: '_';
106 static void killnl(char *s
, int ddd
)
109 tmp
= strstr(s
, "\r\n");
112 if(ddd
&& tmp
- s
> 3)
114 tmp
[0] = tmp
[1] = tmp
[2] = '.';
120 tmp
= strchr(s
, '\n');
123 if(ddd
&& tmp
- s
> 3)
125 tmp
[0] = tmp
[1] = tmp
[2] = '.';
133 static int killcomment(char *s
)
137 while((tmp
= strstr(tmp
, "/*")))
143 while((tmp
= strstr(tmp
, "*/")))
151 void write_h_file(const char *fname
)
163 fp
= fopen(fname
, "w");
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");
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
);
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
);
200 /* Write the message codes */
201 fprintf(fp
, "/* Message definitions */\n");
202 for(ndp
= nodehead
; ndp
; ndp
= ndp
->next
)
207 cptr
= dup_u2c(ndp
->u
.comment
+1);
211 fprintf(fp
, "/* %s */\n", cptr
);
220 * Search for an English text.
221 * If not found, then use the first in the list
224 for(i
= 0; i
< ndp
->u
.msg
->nmsgs
; i
++)
226 if(ndp
->u
.msg
->msgs
[i
]->lan
== 0x409)
234 fprintf(fp
, "/* MessageId : 0x%08x */\n", ndp
->u
.msg
->realid
);
235 cptr
= dup_u2c(ndp
->u
.msg
->msgs
[idx_en
]->msg
);
238 fprintf(fp
, "/* Approximate msg: %s */\n", cptr
);
240 cptr
= dup_u2c(ndp
->u
.msg
->sym
);
242 cast
= dup_u2c(ndp
->u
.msg
->cast
);
245 switch(ndp
->u
.msg
->base
)
249 fprintf(fp
, "#define %s\t((%s)0%oL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
251 fprintf(fp
, "#define %s\t0%oL\n\n", cptr
, ndp
->u
.msg
->realid
);
255 fprintf(fp
, "#define %s\t((%s)%dL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
257 fprintf(fp
, "#define %s\t%dL\n\n", cptr
, ndp
->u
.msg
->realid
);
261 fprintf(fp
, "#define %s\t((%s)0x%08xL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
263 fprintf(fp
, "#define %s\t0x%08xL\n\n", cptr
, ndp
->u
.msg
->realid
);
266 internal_error(__FILE__
, __LINE__
, "Invalid base for number print\n");
272 internal_error(__FILE__
, __LINE__
, "Invalid node type %d\n", ndp
->type
);
275 fprintf(fp
, "\n#endif\n");
279 static void write_rcbin(FILE *fp
)
286 get_tokentable(&ttab
, &ntab
);
288 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
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
)
297 cptr
= dup_u2c(ttab
[i
].alias
);
302 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
303 fprintf(fp
, "1 MESSAGETABLE \"%s.bin\"\n", cptr
);
308 static char *make_string(WCHAR
*uc
, int len
)
310 char *str
= xmalloc(7*len
+ 12);
318 for(i
= b
= 0; i
< len
; i
++, 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;
332 if (*uc
< 0x100 && isprint(*uc
))
339 int n
= sprintf(cptr
, "\\x%04x", *uc
);
345 if(i
< len
-1 && b
>= 72)
356 len
= (len
+ 1) & ~1;
371 static void write_rcinline(FILE *fp
)
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);
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",
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
++)
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
);
411 void write_rc_file(const char *fname
)
413 FILE *fp
= fopen(fname
, "w");
420 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
);
429 static void output_bin_data( struct lan_blk
*lbp
)
431 unsigned int offs
= 4 * (lbp
->nblk
* 3 + 1);
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;
450 for (k
= 0; k
< blk
->msgs
[j
]->len
; k
++) put_word( blk
->msgs
[j
]->msg
[k
] );
456 void write_bin_files(void)
463 get_tokentable(&ttab
, &ntab
);
465 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
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
);
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
);
488 void write_res_file( const char *name
)
493 init_output_buffer();
495 put_dword( 0 ); /* ResSize */
496 put_dword( 32 ); /* HeaderSize */
497 put_word( 0xffff ); /* ResType */
499 put_word( 0xffff ); /* ResName */
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 */
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
);