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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 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
);
104 internal_error(__FILE__
, __LINE__
, "Codepage %d not found (vanished?)", cp
);
105 len
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, NULL
, 0, NULL
, NULL
);
107 if((len
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, cptr
, len
, NULL
, NULL
)) < 0)
108 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d.", len
);
112 static void killnl(char *s
, int ddd
)
115 tmp
= strstr(s
, "\r\n");
118 if(ddd
&& tmp
- s
> 3)
120 tmp
[0] = tmp
[1] = tmp
[2] = '.';
126 tmp
= strchr(s
, '\n');
129 if(ddd
&& tmp
- s
> 3)
131 tmp
[0] = tmp
[1] = tmp
[2] = '.';
139 static int killcomment(char *s
)
143 while((tmp
= strstr(tmp
, "/*")))
149 while((tmp
= strstr(tmp
, "*/")))
157 void write_h_file(const char *fname
)
169 fp
= fopen(fname
, "w");
177 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
, cptr
);
178 fprintf(fp
, "#ifndef __WMCGENERATED_%08lx_H\n", (long)now
);
179 fprintf(fp
, "#define __WMCGENERATED_%08lx_H\n", (long)now
);
182 /* Write severity and facility aliases */
183 get_tokentable(&ttab
, &ntab
);
184 fprintf(fp
, "/* Severity codes */\n");
185 for(i
= 0; i
< ntab
; i
++)
187 if(ttab
[i
].type
== tok_severity
&& ttab
[i
].alias
)
189 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
190 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
196 fprintf(fp
, "/* Facility codes */\n");
197 for(i
= 0; i
< ntab
; i
++)
199 if(ttab
[i
].type
== tok_facility
&& ttab
[i
].alias
)
201 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
202 fprintf(fp
, "#define %s\t0x%x\n", cptr
, ttab
[i
].token
);
208 /* Write the message codes */
209 fprintf(fp
, "/* Message definitions */\n");
210 for(ndp
= nodehead
; ndp
; ndp
= ndp
->next
)
215 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.comment
+1);
219 fprintf(fp
, "/* %s */\n", cptr
);
228 * Search for an english text.
229 * If not found, then use the first in the list
232 for(i
= 0; i
< ndp
->u
.msg
->nmsgs
; i
++)
234 if(ndp
->u
.msg
->msgs
[i
]->lan
== 0x409)
242 fprintf(fp
, "/* MessageId : 0x%08x */\n", ndp
->u
.msg
->realid
);
243 cptr
= dup_u2c(ndp
->u
.msg
->msgs
[idx_en
]->cp
, ndp
->u
.msg
->msgs
[idx_en
]->msg
);
246 fprintf(fp
, "/* Approx. msg: %s */\n", cptr
);
248 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.msg
->sym
);
250 cast
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ndp
->u
.msg
->cast
);
253 switch(ndp
->u
.msg
->base
)
257 fprintf(fp
, "#define %s\t((%s)0%oL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
259 fprintf(fp
, "#define %s\t0%oL\n\n", cptr
, ndp
->u
.msg
->realid
);
263 fprintf(fp
, "#define %s\t((%s)%dL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
265 fprintf(fp
, "#define %s\t%dL\n\n", cptr
, ndp
->u
.msg
->realid
);
269 fprintf(fp
, "#define %s\t((%s)0x%08xL)\n\n", cptr
, cast
, ndp
->u
.msg
->realid
);
271 fprintf(fp
, "#define %s\t0x%08xL\n\n", cptr
, ndp
->u
.msg
->realid
);
274 internal_error(__FILE__
, __LINE__
, "Invalid base for number print");
281 internal_error(__FILE__
, __LINE__
, "Invalid node type %d", ndp
->type
);
284 fprintf(fp
, "\n#endif\n");
288 static void write_rcbin(FILE *fp
)
295 get_tokentable(&ttab
, &ntab
);
297 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
300 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
301 for(i
= 0; i
< ntab
; i
++)
303 if(ttab
[i
].type
== tok_language
&& ttab
[i
].token
== lbp
->lan
)
306 cptr
= dup_u2c(WMC_DEFAULT_CODEPAGE
, ttab
[i
].alias
);
311 internal_error(__FILE__
, __LINE__
, "Filename vanished for language 0x%0x", lbp
->lan
);
312 fprintf(fp
, "1 MESSAGETABLE \"%s.bin\"\n", cptr
);
317 static char *make_string(WCHAR
*uc
, int len
, int codepage
)
319 char *str
= xmalloc(7*len
+ 1);
329 for(i
= b
= 0; i
< len
; i
++, uc
++)
333 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
334 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
335 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
336 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
337 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
338 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
339 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
340 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
341 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
343 if (*uc
< 0x100 && isprint(*uc
))
350 int n
= sprintf(cptr
, "\\x%04x", *uc
& 0xffff);
356 if(i
< len
-1 && b
>= 72)
368 len
= (len
+ 1) & ~1;
370 len
= (len
+ 3) & ~3;
387 const union cptable
*cpdef
= find_codepage(codepage
);
389 assert(cpdef
!= NULL
);
390 mlen
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, NULL
, 0, NULL
, NULL
);
391 cc
= tmp
= xmalloc(mlen
);
392 if((i
= wine_cp_wcstombs(cpdef
, 0, uc
, unistrlen(uc
)+1, tmp
, mlen
, NULL
, NULL
)) < 0)
393 internal_error(__FILE__
, __LINE__
, "Buffer overflow? code %d.", i
);
396 for(i
= b
= 0; i
< len
; i
++, cc
++)
400 case '\a': *cptr
++ = '\\'; *cptr
++ = 'a'; b
+= 2; break;
401 case '\b': *cptr
++ = '\\'; *cptr
++ = 'b'; b
+= 2; break;
402 case '\f': *cptr
++ = '\\'; *cptr
++ = 'f'; b
+= 2; break;
403 case '\n': *cptr
++ = '\\'; *cptr
++ = 'n'; b
+= 2; break;
404 case '\r': *cptr
++ = '\\'; *cptr
++ = 'r'; b
+= 2; break;
405 case '\t': *cptr
++ = '\\'; *cptr
++ = 't'; b
+= 2; break;
406 case '\v': *cptr
++ = '\\'; *cptr
++ = 'v'; b
+= 2; break;
407 case '\\': *cptr
++ = '\\'; *cptr
++ = '\\'; b
+= 2; break;
408 case '"': *cptr
++ = '\\'; *cptr
++ = '"'; b
+= 2; break;
417 int n
= sprintf(cptr
, "\\x%02x", *cc
& 0xff);
423 if(i
< len
-1 && b
>= 72)
433 len
= (len
+ 3) & ~3;
448 static void write_rcinline(FILE *fp
)
454 for(lbp
= lanblockhead
; lbp
; lbp
= lbp
->next
)
456 unsigned offs
= 4 * (lbp
->nblk
* 3 + 1);
457 fprintf(fp
, "\n1 MESSAGETABLE\n");
458 fprintf(fp
, "LANGUAGE 0x%x, 0x%x\n", lbp
->lan
& 0x3ff, lbp
->lan
>> 10);
460 fprintf(fp
, " /* NBlocks */ 0x%08xL,\n", lbp
->nblk
);
461 for(i
= 0; i
< lbp
->nblk
; i
++)
463 fprintf(fp
, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
467 offs
+= lbp
->blks
[i
].size
;
469 for(i
= 0; i
< lbp
->nblk
; i
++)
471 block_t
*blk
= &lbp
->blks
[i
];
472 for(j
= 0; j
< blk
->nmsg
; j
++)
475 int l
= blk
->msgs
[j
]->len
;
476 const char *comma
= j
== blk
->nmsg
-1 && i
== lbp
->nblk
-1 ? "" : ",";
477 cptr
= make_string(blk
->msgs
[j
]->msg
, l
, unicodeout
? 0 : blk
->msgs
[j
]->cp
);
478 fprintf(fp
, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
480 (unicodeout
? (l
*2+3)&~3 : (l
+3)&~3) + 4,
481 unicodeout
? '1' : '0');
482 fprintf(fp
, "%s%s\n", cptr
, comma
);
490 void write_rc_file(const char *fname
)
495 fp
= fopen(fname
, "w");
503 fprintf(fp
, str_header
, input_name
? input_name
: "<stdin>", cmdline
, cptr
);
512 void write_bin_files(void)
514 assert(rcinline
== 0);