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"
98 static char *dup_u2c(int cp
, const WCHAR
*uc
)
102 const union cptable
*cpdef
= find_codepage(cp
);
105 len
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, NULL
, 0, NULL
, NULL
);
107 len
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, NULL
, 0);
110 len
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, cptr
, len
, NULL
, NULL
);
112 len
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, cptr
, len
);
114 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d\n", len
);
118 static void killnl(char *s
, int ddd
)
121 tmp
= strstr(s
, "\r\n");
124 if(ddd
&& tmp
- s
> 3)
126 tmp
[0] = tmp
[1] = tmp
[2] = '.';
132 tmp
= strchr(s
, '\n');
135 if(ddd
&& tmp
- s
> 3)
137 tmp
[0] = tmp
[1] = tmp
[2] = '.';
145 static int killcomment(char *s
)
149 while((tmp
= strstr(tmp
, "/*")))
155 while((tmp
= strstr(tmp
, "*/")))
163 void write_h_file(const char *fname
)
175 fp
= fopen(fname
, "w");
183 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
, cptr
);
184 fprintf(fp
, "#ifndef __WMCGENERATED_%08lx_H\n", (long)now
);
185 fprintf(fp
, "#define __WMCGENERATED_%08lx_H\n", (long)now
);
188 /* Write severity and facility aliases */
189 get_tokentable(&ttab
, &ntab
);
190 fprintf(fp
, "/* Severity codes */\n");
191 for(i
= 0; i
< ntab
; i
++)
193 if(ttab
[i
].type
== tok_severity
&& ttab
[i
].alias
)
195 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
196 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
202 fprintf(fp
, "/* Facility codes */\n");
203 for(i
= 0; i
< ntab
; i
++)
205 if(ttab
[i
].type
== tok_facility
&& ttab
[i
].alias
)
207 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
208 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
214 /* Write the message codes */
215 fprintf(fp
, "/* Message definitions */\n");
216 for(ndp
= nodehead
; ndp
; ndp
= ndp
->next
)
221 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.comment
+1);
225 fprintf(fp
, "/* %s */\n", cptr
);
234 * Search for an english text.
235 * If not found, then use the first in the list
238 for(i
= 0; i
< ndp
->u
.msg
->nmsgs
; i
++)
240 if(ndp
->u
.msg
->msgs
[i
]->lan
== 0x409)
248 fprintf(fp
, "/* MessageId : 0x%08x */\n", ndp
->u
.msg
->realid
);
249 cptr
= dup_u2c(ndp
->u
.msg
->msgs
[idx_en
]->cp
, ndp
->u
.msg
->msgs
[idx_en
]->msg
);
252 fprintf(fp
, "/* Approximate msg: %s */\n", cptr
);
254 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.msg
->sym
);
256 cast
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.msg
->cast
);
259 switch(ndp
->u
.msg
->base
)
263 fprintf(fp
, "#define %s\t((%s)0%oL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
265 fprintf(fp
, "#define %s\t0%oL\n\n", cptr
, ndp
->u
.msg
->realid
);
269 fprintf(fp
, "#define %s\t((%s)%dL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
271 fprintf(fp
, "#define %s\t%dL\n\n", cptr
, ndp
->u
.msg
->realid
);
275 fprintf(fp
, "#define %s\t((%s)0x%08xL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
277 fprintf(fp
, "#define %s\t0x%08xL\n\n", cptr
, ndp
->u
.msg
->realid
);
280 internal_error(__FILE__
, __LINE__
, "Invalid base for number print\n");
286 internal_error(__FILE__
, __LINE__
, "Invalid node type %d\n", ndp
->type
);
289 fprintf(fp
, "\n#endif\n");
293 static void write_rcbin(FILE *fp
)
300 get_tokentable(&ttab
, &ntab
);
302 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
305 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
306 for(i
= 0; i
< ntab
; i
++)
308 if(ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
311 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
316 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
317 fprintf(fp
, "1 MESSAGETABLE \"%s.bin\"\n", cptr
);
322 static char *make_string(WCHAR
*uc
, int len
, int codepage
)
324 char *str
= xmalloc(7*len
+ 1);
334 for(i
= b
= 0; i
< len
; i
++, uc
++)
338 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
339 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
340 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
341 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
342 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
343 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
344 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
345 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
346 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
348 if (*uc
< 0x100 && isprint(*uc
))
355 int n
= sprintf(cptr
, "\\x%04x", *uc
& 0xffff);
361 if(i
< len
-1 && b
>= 72)
373 len
= (len
+ 1) & ~1;
375 len
= (len
+ 3) & ~3;
392 const union cptable
*cpdef
= find_codepage(codepage
);
395 mlen
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, NULL
, 0, NULL
, NULL
);
397 mlen
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, NULL
, 0);
398 cc
= tmp
= xmalloc(mlen
);
400 if((i
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, tmp
, mlen
, NULL
, NULL
)) < 0)
401 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d\n", i
);
403 if((i
= wine_utf8_wcstombs(0, uc
, unistrlen(uc
)+1, tmp
, mlen
)) < 0)
404 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d\n", i
);
408 for(i
= b
= 0; i
< len
; i
++, cc
++)
412 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
413 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
414 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
415 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
416 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
417 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
418 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
419 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
420 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
429 int n
= sprintf(cptr
, "\\x%02x", *cc
& 0xff);
435 if(i
< len
-1 && b
>= 72)
445 len
= (len
+ 3) & ~3;
460 static void write_rcinline(FILE *fp
)
466 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
468 unsigned offs
= 4 * (lbp
->nblk
* 3 + 1);
469 fprintf(fp
, "\n1 MESSAGETABLE\n");
470 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
472 fprintf(fp
, " /* NBlocks */ 0x%08xL,\n", lbp
->nblk
);
473 for(i
= 0; i
< lbp
->nblk
; i
++)
475 fprintf(fp
, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
479 offs
+= lbp
->blks
[i
].size
;
481 for(i
= 0; i
< lbp
->nblk
; i
++)
483 block_t
*blk
= &lbp
->blks
[i
];
484 for(j
= 0; j
< blk
->nmsg
; j
++)
487 int l
= blk
->msgs
[j
]->len
;
488 const char *comma
= j
== blk
->nmsg
-1 && i
== lbp
->nblk
-1 ? "" : ",";
489 cptr
= make_string(blk
->msgs
[j
]->msg
, l
, unicodeout
? 0 : blk
->msgs
[j
]->cp
);
490 fprintf(fp
, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
492 (unicodeout
? (l
*2+3)&~3 : (l
+3)&~3) + 4,
493 unicodeout
? '1' : '0');
494 fprintf(fp
, "%s%s\n", cptr
, comma
);
502 void write_rc_file(const char *fname
)
507 fp
= fopen(fname
, "w");
515 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
, cptr
);
524 static void output_bin_data( lan_blk_t
*lbp
)
526 unsigned int offs
= 4 * (lbp
->nblk
* 3 + 1);
529 put_dword( lbp
->nblk
); /* NBlocks */
530 for (i
= 0; i
< lbp
->nblk
; i
++)
532 put_dword( lbp
->blks
[i
].idlo
); /* Lo */
533 put_dword( lbp
->blks
[i
].idhi
); /* Hi */
534 put_dword( offs
); /* Offs */
535 offs
+= lbp
->blks
[i
].size
;
537 for (i
= 0; i
< lbp
->nblk
; i
++)
539 block_t
*blk
= &lbp
->blks
[i
];
540 for (j
= 0; j
< blk
->nmsg
; j
++)
542 int len
= (2 * blk
->msgs
[j
]->len
+ 3) & ~3;
545 for (k
= 0; k
< blk
->msgs
[j
]->len
; k
++) put_word( blk
->msgs
[j
]->msg
[k
] );
551 void write_bin_files(void)
558 get_tokentable(&ttab
, &ntab
);
560 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
564 for (i
= 0; i
< ntab
; i
++)
566 if (ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
568 if (ttab
[i
].alias
) cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
573 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x\n", lbp
->lan
);
574 init_output_buffer();
575 output_bin_data( lbp
);
576 cptr
= xrealloc( cptr
, strlen(cptr
) + 5 );
577 strcat( cptr
, ".bin" );
578 flush_output_buffer( cptr
);
583 void write_res_file( const char *name
)
588 init_output_buffer();
590 put_dword( 0 ); /* ResSize */
591 put_dword( 32 ); /* HeaderSize */
592 put_word( 0xffff ); /* ResType */
594 put_word( 0xffff ); /* ResName */
596 put_dword( 0 ); /* DataVersion */
597 put_word( 0 ); /* Memory options */
598 put_word( 0 ); /* Language */
599 put_dword( 0 ); /* Version */
600 put_dword( 0 ); /* Characteristics */
602 for (lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
604 unsigned int data_size
= 4 * (lbp
->nblk
* 3 + 1);
605 unsigned int header_size
= 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
607 for (i
= 0; i
< lbp
->nblk
; i
++)
609 block_t
*blk
= &lbp
->blks
[i
];
610 for (j
= 0; j
< blk
->nmsg
; j
++) data_size
+= 4 + ((blk
->msgs
[j
]->len
* 2 + 3) & ~3);
613 put_dword( data_size
); /* ResSize */
614 put_dword( header_size
); /* HeaderSize */
615 put_word( 0xffff ); /* ResType */
616 put_word( 0x000b /*RT_MESSAGETABLE*/ );
617 put_word( 0xffff ); /* ResName */
620 put_dword( 0 ); /* DataVersion */
621 put_word( 0x30 ); /* Memory options */
622 put_word( lbp
->lan
); /* Language */
623 put_dword( lbp
->version
); /* Version */
624 put_dword( 0 ); /* Characteristics */
626 output_bin_data( lbp
);
628 flush_output_buffer( name
);