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(int cp
, const WCHAR
*uc
)
101 const union cptable
*cpdef
= find_codepage(cp
);
104 len
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, NULL
, 0, NULL
, NULL
);
106 len
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, NULL
, 0);
109 len
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, cptr
, len
, NULL
, NULL
);
111 len
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, cptr
, len
);
113 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d\n", len
);
117 static void killnl(char *s
, int ddd
)
120 tmp
= strstr(s
, "\r\n");
123 if(ddd
&& tmp
- s
> 3)
125 tmp
[0] = tmp
[1] = tmp
[2] = '.';
131 tmp
= strchr(s
, '\n');
134 if(ddd
&& tmp
- s
> 3)
136 tmp
[0] = tmp
[1] = tmp
[2] = '.';
144 static int killcomment(char *s
)
148 while((tmp
= strstr(tmp
, "/*")))
154 while((tmp
= strstr(tmp
, "*/")))
162 void write_h_file(const char *fname
)
174 fp
= fopen(fname
, "w");
180 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
);
181 fprintf(fp
, "#ifndef __WMCGENERATED_H\n");
182 fprintf(fp
, "#define __WMCGENERATED_H\n");
185 /* Write severity and facility aliases */
186 get_tokentable(&ttab
, &ntab
);
187 fprintf(fp
, "/* Severity codes */\n");
188 for(i
= 0; i
< ntab
; i
++)
190 if(ttab
[i
].type
== tok_severity
&& ttab
[i
].alias
)
192 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
193 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
199 fprintf(fp
, "/* Facility codes */\n");
200 for(i
= 0; i
< ntab
; i
++)
202 if(ttab
[i
].type
== tok_facility
&& ttab
[i
].alias
)
204 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
205 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
211 /* Write the message codes */
212 fprintf(fp
, "/* Message definitions */\n");
213 for(ndp
= nodehead
; ndp
; ndp
= ndp
->next
)
218 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.comment
+1);
222 fprintf(fp
, "/* %s */\n", cptr
);
231 * Search for an English text.
232 * If not found, then use the first in the list
235 for(i
= 0; i
< ndp
->u
.msg
->nmsgs
; i
++)
237 if(ndp
->u
.msg
->msgs
[i
]->lan
== 0x409)
245 fprintf(fp
, "/* MessageId : 0x%08x */\n", ndp
->u
.msg
->realid
);
246 cptr
= dup_u2c(ndp
->u
.msg
->msgs
[idx_en
]->cp
, ndp
->u
.msg
->msgs
[idx_en
]->msg
);
249 fprintf(fp
, "/* Approximate msg: %s */\n", cptr
);
251 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.msg
->sym
);
253 cast
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.msg
->cast
);
256 switch(ndp
->u
.msg
->base
)
260 fprintf(fp
, "#define %s\t((%s)0%oL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
262 fprintf(fp
, "#define %s\t0%oL\n\n", cptr
, ndp
->u
.msg
->realid
);
266 fprintf(fp
, "#define %s\t((%s)%dL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
268 fprintf(fp
, "#define %s\t%dL\n\n", cptr
, ndp
->u
.msg
->realid
);
272 fprintf(fp
, "#define %s\t((%s)0x%08xL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
274 fprintf(fp
, "#define %s\t0x%08xL\n\n", cptr
, ndp
->u
.msg
->realid
);
277 internal_error(__FILE__
, __LINE__
, "Invalid base for number print\n");
283 internal_error(__FILE__
, __LINE__
, "Invalid node type %d\n", ndp
->type
);
286 fprintf(fp
, "\n#endif\n");
290 static void write_rcbin(FILE *fp
)
297 get_tokentable(&ttab
, &ntab
);
299 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
302 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
303 for(i
= 0; i
< ntab
; i
++)
305 if(ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
308 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
313 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
314 fprintf(fp
, "1 MESSAGETABLE \"%s.bin\"\n", cptr
);
319 static char *make_string(WCHAR
*uc
, int len
, int codepage
)
321 char *str
= xmalloc(7*len
+ 1);
331 for(i
= b
= 0; i
< len
; i
++, uc
++)
335 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
336 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
337 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
338 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
339 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
340 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
341 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
342 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
343 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
345 if (*uc
< 0x100 && isprint(*uc
))
352 int n
= sprintf(cptr
, "\\x%04x", *uc
& 0xffff);
358 if(i
< len
-1 && b
>= 72)
370 len
= (len
+ 1) & ~1;
372 len
= (len
+ 3) & ~3;
389 const union cptable
*cpdef
= find_codepage(codepage
);
392 mlen
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, NULL
, 0, NULL
, NULL
);
394 mlen
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, NULL
, 0);
395 cc
= tmp
= xmalloc(mlen
);
397 if((i
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, tmp
, mlen
, NULL
, NULL
)) < 0)
398 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d\n", i
);
400 if((i
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, tmp
, mlen
)) < 0)
401 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d\n", i
);
405 for(i
= b
= 0; i
< len
; i
++, cc
++)
409 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
410 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
411 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
412 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
413 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
414 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
415 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
416 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
417 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
426 int n
= sprintf(cptr
, "\\x%02x", *cc
& 0xff);
432 if(i
< len
-1 && b
>= 72)
442 len
= (len
+ 3) & ~3;
457 static void write_rcinline(FILE *fp
)
463 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
465 unsigned offs
= 4 * (lbp
->nblk
* 3 + 1);
466 fprintf(fp
, "\n1 MESSAGETABLE\n");
467 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
469 fprintf(fp
, " /* NBlocks */ 0x%08xL,\n", lbp
->nblk
);
470 for(i
= 0; i
< lbp
->nblk
; i
++)
472 fprintf(fp
, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
476 offs
+= lbp
->blks
[i
].size
;
478 for(i
= 0; i
< lbp
->nblk
; i
++)
480 block_t
*blk
= &lbp
->blks
[i
];
481 for(j
= 0; j
< blk
->nmsg
; j
++)
484 int l
= blk
->msgs
[j
]->len
;
485 const char *comma
= j
== blk
->nmsg
-1 && i
== lbp
->nblk
-1 ? "" : ",";
486 cptr
= make_string(blk
->msgs
[j
]->msg
, l
, unicodeout
? 0 : blk
->msgs
[j
]->cp
);
487 fprintf(fp
, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
489 (unicodeout
? (l
*2+3)&~3 : (l
+3)&~3) + 4,
490 unicodeout
? '1' : '0');
491 fprintf(fp
, "%s%s\n", cptr
, comma
);
499 void write_rc_file(const char *fname
)
501 FILE *fp
= fopen(fname
, "w");
508 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
);
517 static void output_bin_data( lan_blk_t
*lbp
)
519 unsigned int offs
= 4 * (lbp
->nblk
* 3 + 1);
522 put_dword( lbp
->nblk
); /* NBlocks */
523 for (i
= 0; i
< lbp
->nblk
; i
++)
525 put_dword( lbp
->blks
[i
].idlo
); /* Lo */
526 put_dword( lbp
->blks
[i
].idhi
); /* Hi */
527 put_dword( offs
); /* Offs */
528 offs
+= lbp
->blks
[i
].size
;
530 for (i
= 0; i
< lbp
->nblk
; i
++)
532 block_t
*blk
= &lbp
->blks
[i
];
533 for (j
= 0; j
< blk
->nmsg
; j
++)
535 int len
= (2 * blk
->msgs
[j
]->len
+ 3) & ~3;
538 for (k
= 0; k
< blk
->msgs
[j
]->len
; k
++) put_word( blk
->msgs
[j
]->msg
[k
] );
544 void write_bin_files(void)
551 get_tokentable(&ttab
, &ntab
);
553 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
557 for (i
= 0; i
< ntab
; i
++)
559 if (ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
561 if (ttab
[i
].alias
) cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
566 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
567 init_output_buffer();
568 output_bin_data( lbp
);
569 cptr
= xrealloc( cptr
, strlen(cptr
) + 5 );
570 strcat( cptr
, ".bin" );
571 flush_output_buffer( cptr
);
576 void write_res_file( const char *name
)
581 init_output_buffer();
583 put_dword( 0 ); /* ResSize */
584 put_dword( 32 ); /* HeaderSize */
585 put_word( 0xffff ); /* ResType */
587 put_word( 0xffff ); /* ResName */
589 put_dword( 0 ); /* DataVersion */
590 put_word( 0 ); /* Memory options */
591 put_word( 0 ); /* Language */
592 put_dword( 0 ); /* Version */
593 put_dword( 0 ); /* Characteristics */
595 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
597 unsigned int data_size
= 4 * (lbp
->nblk
* 3 + 1);
598 unsigned int header_size
= 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
600 for (i
= 0; i
< lbp
->nblk
; i
++)
602 block_t
*blk
= &lbp
->blks
[i
];
603 for (j
= 0; j
< blk
->nmsg
; j
++) data_size
+= 4 + ((blk
->msgs
[j
]->len
* 2 + 3) & ~3);
606 put_dword( data_size
); /* ResSize */
607 put_dword( header_size
); /* HeaderSize */
608 put_word( 0xffff ); /* ResType */
609 put_word( 0x000b /*RT_MESSAGETABLE*/ );
610 put_word( 0xffff ); /* ResName */
613 put_dword( 0 ); /* DataVersion */
614 put_word( 0x30 ); /* Memory options */
615 put_word( lbp
->lan
); /* Language */
616 put_dword( lbp
->version
); /* Version */
617 put_dword( 0 ); /* Characteristics */
619 output_bin_data( lbp
);
621 flush_output_buffer( name
);