winebuild: Simplify the ARM part of output_import_thunk.
[wine] / tools / wmc / write.c
1 /*
2  * Wine Message Compiler output generation
3  *
4  * Copyright 2000 Bertho A. Stultiens (BS)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29
30 #include "wmc.h"
31 #include "utils.h"
32 #include "lang.h"
33 #include "write.h"
34
35 /*
36  * The binary resource layout is as follows:
37  *
38  *         +===============+
39  * Header  |    NBlocks    |
40  *         +===============+
41  * Block 0 |    Low ID     |
42  *         +---------------+
43  *         |    High ID    |
44  *         +---------------+
45  *         |    Offset     |---+
46  *         +===============+   |
47  * Block 1 |    Low ID     |   |
48  *         +---------------+   |
49  *         |    High ID    |   |
50  *         +---------------+   |
51  *         |    Offset     |------+
52  *         +===============+   |  |
53  *         |               |   |  |
54  *        ...             ...  |  |
55  *         |               |   |  |
56  *         +===============+ <-+  |
57  * B0 LoID |  Len  | Flags |      |
58  *         +---+---+---+---+      |
59  *         | b | l | a | b |      |
60  *         +---+---+---+---+      |
61  *         | l | a | \0| \0|      |
62  *         +===============+      |
63  *         |               |      |
64  *        ...             ...     |
65  *         |               |      |
66  *         +===============+      |
67  * B0 HiID |  Len  | Flags |      |
68  *         +---+---+---+---+      |
69  *         | M | o | r | e |      |
70  *         +---+---+---+---+      |
71  *         | b | l | a | \0|      |
72  *         +===============+ <----+
73  * B1 LoID |  Len  | Flags |
74  *         +---+---+---+---+
75  *         | J | u | n | k |
76  *         +---+---+---+---+
77  *         | \0| \0| \0| \0|
78  *         +===============+
79  *         |               |
80  *        ...             ...
81  *         |               |
82  *         +===============+
83  *
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.
88  */
89
90 static const char str_header[] =
91         "/* This file is generated with wmc version " PACKAGE_VERSION ". Do not edit! */\n"
92         "/* Source : %s */\n"
93         "/* Cmdline: %s */\n"
94         "/* Date   : %s */\n"
95         "\n"
96         ;
97
98 static char *dup_u2c(int cp, const WCHAR *uc)
99 {
100         int len;
101         char *cptr;
102         const union cptable *cpdef = find_codepage(cp);
103
104         if(cpdef)
105             len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
106         else
107             len = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, NULL, 0);
108         cptr = xmalloc(len);
109         if (cpdef)
110             len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len, NULL, NULL);
111         else
112             len = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, cptr, len);
113         if (len < 0)
114                 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", len);
115         return cptr;
116 }
117
118 static void killnl(char *s, int ddd)
119 {
120         char *tmp;
121         tmp = strstr(s, "\r\n");
122         if(tmp)
123         {
124                 if(ddd && tmp - s > 3)
125                 {
126                         tmp[0] = tmp[1] = tmp[2] = '.';
127                         tmp[3] = '\0';
128                 }
129                 else
130                         *tmp = '\0';
131         }
132         tmp = strchr(s, '\n');
133         if(tmp)
134         {
135                 if(ddd && tmp - s > 3)
136                 {
137                         tmp[0] = tmp[1] = tmp[2] = '.';
138                         tmp[3] = '\0';
139                 }
140                 else
141                         *tmp = '\0';
142         }
143 }
144
145 static int killcomment(char *s)
146 {
147         char *tmp = s;
148         int b = 0;
149         while((tmp = strstr(tmp, "/*")))
150         {
151                 tmp[1] = 'x';
152                 b++;
153         }
154         tmp = s;
155         while((tmp = strstr(tmp, "*/")))
156         {
157                 tmp[0] = 'x';
158                 b++;
159         }
160         return b;
161 }
162
163 void write_h_file(const char *fname)
164 {
165         node_t *ndp;
166         char *cptr;
167         char *cast;
168         FILE *fp;
169         token_t *ttab;
170         int ntab;
171         int i;
172         int once = 0;
173         int idx_en = 0;
174
175         fp = fopen(fname, "w");
176         if(!fp)
177         {
178                 perror(fname);
179                 exit(1);
180         }
181         cptr = ctime(&now);
182         killnl(cptr, 0);
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);
186         fprintf(fp, "\n");
187
188         /* Write severity and facility aliases */
189         get_tokentable(&ttab, &ntab);
190         fprintf(fp, "/* Severity codes */\n");
191         for(i = 0; i < ntab; i++)
192         {
193                 if(ttab[i].type == tok_severity && ttab[i].alias)
194                 {
195                         cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
196                         fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
197                         free(cptr);
198                 }
199         }
200         fprintf(fp, "\n");
201
202         fprintf(fp, "/* Facility codes */\n");
203         for(i = 0; i < ntab; i++)
204         {
205                 if(ttab[i].type == tok_facility && ttab[i].alias)
206                 {
207                         cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
208                         fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
209                         free(cptr);
210                 }
211         }
212         fprintf(fp, "\n");
213
214         /* Write the message codes */
215         fprintf(fp, "/* Message definitions */\n");
216         for(ndp = nodehead; ndp; ndp = ndp->next)
217         {
218                 switch(ndp->type)
219                 {
220                 case nd_comment:
221                         cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
222                         killnl(cptr, 0);
223                         killcomment(cptr);
224                         if(*cptr)
225                                 fprintf(fp, "/* %s */\n", cptr);
226                         else
227                                 fprintf(fp, "\n");
228                         free(cptr);
229                         break;
230                 case nd_msg:
231                         if(!once)
232                         {
233                                 /*
234                                  * Search for an english text.
235                                  * If not found, then use the first in the list
236                                  */
237                                 once++;
238                                 for(i = 0; i < ndp->u.msg->nmsgs; i++)
239                                 {
240                                         if(ndp->u.msg->msgs[i]->lan == 0x409)
241                                         {
242                                                 idx_en = i;
243                                                 break;
244                                         }
245                                 }
246                                 fprintf(fp, "\n");
247                         }
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);
250                         killnl(cptr, 0);
251                         killcomment(cptr);
252                         fprintf(fp, "/* Approximate msg: %s */\n", cptr);
253                         free(cptr);
254                         cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
255                         if(ndp->u.msg->cast)
256                                 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
257                         else
258                                 cast = NULL;
259                         switch(ndp->u.msg->base)
260                         {
261                         case 8:
262                                 if(cast)
263                                         fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
264                                 else
265                                         fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
266                                 break;
267                         case 10:
268                                 if(cast)
269                                         fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
270                                 else
271                                         fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
272                                 break;
273                         case 16:
274                                 if(cast)
275                                         fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
276                                 else
277                                         fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
278                                 break;
279                         default:
280                                 internal_error(__FILE__, __LINE__, "Invalid base for number print\n");
281                         }
282                         free(cptr);
283                         free(cast);
284                         break;
285                 default:
286                         internal_error(__FILE__, __LINE__, "Invalid node type %d\n", ndp->type);
287                 }
288         }
289         fprintf(fp, "\n#endif\n");
290         fclose(fp);
291 }
292
293 static void write_rcbin(FILE *fp)
294 {
295         lan_blk_t *lbp;
296         token_t *ttab;
297         int ntab;
298         int i;
299
300         get_tokentable(&ttab, &ntab);
301
302         for(lbp = lanblockhead; lbp; lbp = lbp->next)
303         {
304                 char *cptr = NULL;
305                 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
306                 for(i = 0; i < ntab; i++)
307                 {
308                         if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
309                         {
310                                 if(ttab[i].alias)
311                                         cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
312                                 break;
313                         }
314                 }
315                 if(!cptr)
316                         internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
317                 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
318                 free(cptr);
319         }
320 }
321
322 static char *make_string(WCHAR *uc, int len, int codepage)
323 {
324         char *str = xmalloc(7*len + 1);
325         char *cptr = str;
326         int i;
327         int b;
328
329         if(!codepage)
330         {
331                 *cptr++ = ' ';
332                 *cptr++ = 'L';
333                 *cptr++ = '"';
334                 for(i = b = 0; i < len; i++, uc++)
335                 {
336                         switch(*uc)
337                         {
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;
347                         default:
348                             if (*uc < 0x100 && isprint(*uc))
349                             {
350                                 *cptr++ = *uc;
351                                 b++;
352                             }
353                             else
354                             {
355                                 int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
356                                 cptr += n;
357                                 b += n;
358                             }
359                             break;
360                         }
361                         if(i < len-1 && b >= 72)
362                         {
363                                 *cptr++ = '"';
364                                 *cptr++ = ',';
365                                 *cptr++ = '\n';
366                                 *cptr++ = ' ';
367                                 *cptr++ = 'L';
368                                 *cptr++ = '"';
369                                 b = 0;
370                         }
371                 }
372                 if (unicodeout)
373                     len = (len + 1) & ~1;
374                 else
375                     len = (len + 3) & ~3;
376                 for(; i < len; i++)
377                 {
378                         *cptr++ = '\\';
379                         *cptr++ = 'x';
380                         *cptr++ = '0';
381                         *cptr++ = '0';
382                         *cptr++ = '0';
383                         *cptr++ = '0';
384                 }
385                 *cptr++ = '"';
386                 *cptr = '\0';
387         }
388         else
389         {
390                 char *tmp, *cc;
391                 int mlen;
392                 const union cptable *cpdef = find_codepage(codepage);
393
394                 if (cpdef)
395                     mlen = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
396                 else
397                     mlen = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, NULL, 0);
398                 cc = tmp = xmalloc(mlen);
399                 if (cpdef) {
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);
402                 } else {
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);
405                 }
406                 *cptr++ = ' ';
407                 *cptr++ = '"';
408                 for(i = b = 0; i < len; i++, cc++)
409                 {
410                         switch(*cc)
411                         {
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;
421                         default:
422                             if(isprint(*cc))
423                             {
424                                 *cptr++ = *cc;
425                                 b++;
426                             }
427                             else
428                             {
429                                 int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
430                                 cptr += n;
431                                 b += n;
432                             }
433                             break;
434                         }
435                         if(i < len-1 && b >= 72)
436                         {
437                                 *cptr++ = '"';
438                                 *cptr++ = ',';
439                                 *cptr++ = '\n';
440                                 *cptr++ = ' ';
441                                 *cptr++ = '"';
442                                 b = 0;
443                         }
444                 }
445                 len = (len + 3) & ~3;
446                 for(; i < len; i++)
447                 {
448                         *cptr++ = '\\';
449                         *cptr++ = 'x';
450                         *cptr++ = '0';
451                         *cptr++ = '0';
452                 }
453                 *cptr++ = '"';
454                 *cptr = '\0';
455                 free(tmp);
456         }
457         return str;
458 }
459
460 static void write_rcinline(FILE *fp)
461 {
462         lan_blk_t *lbp;
463         int i;
464         int j;
465
466         for(lbp = lanblockhead; lbp; lbp = lbp->next)
467         {
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);
471                 fprintf(fp, "{\n");
472                 fprintf(fp, " /* NBlocks    */ 0x%08xL,\n", lbp->nblk);
473                 for(i = 0; i < lbp->nblk; i++)
474                 {
475                         fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
476                                         lbp->blks[i].idlo,
477                                         lbp->blks[i].idhi,
478                                         offs);
479                         offs += lbp->blks[i].size;
480                 }
481                 for(i = 0; i < lbp->nblk; i++)
482                 {
483                         block_t *blk = &lbp->blks[i];
484                         for(j = 0; j < blk->nmsg; j++)
485                         {
486                                 char *cptr;
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",
491                                         blk->idlo + j,
492                                         (unicodeout ? (l*2+3)&~3 : (l+3)&~3) + 4,
493                                         unicodeout ? '1' : '0');
494                                 fprintf(fp, "%s%s\n", cptr, comma);
495                                 free(cptr);
496                         }
497                 }
498                 fprintf(fp, "}\n");
499         }
500 }
501
502 void write_rc_file(const char *fname)
503 {
504         FILE *fp;
505         char *cptr;
506
507         fp = fopen(fname, "w");
508         if(!fp)
509         {
510                 perror(fname);
511                 exit(1);
512         }
513         cptr = ctime(&now);
514         killnl(cptr, 0);
515         fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
516
517         if(rcinline)
518                 write_rcinline(fp);
519         else
520                 write_rcbin(fp);
521         fclose(fp);
522 }
523
524 static void output_bin_data( lan_blk_t *lbp )
525 {
526     unsigned int offs = 4 * (lbp->nblk * 3 + 1);
527     int i, j, k;
528
529     put_dword( lbp->nblk );  /* NBlocks */
530     for (i = 0; i < lbp->nblk; i++)
531     {
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;
536     }
537     for (i = 0; i < lbp->nblk; i++)
538     {
539         block_t *blk = &lbp->blks[i];
540         for (j = 0; j < blk->nmsg; j++)
541         {
542             int len = (2 * blk->msgs[j]->len + 3) & ~3;
543             put_word( len + 4 );
544             put_word( 1 );
545             for (k = 0; k < blk->msgs[j]->len; k++) put_word( blk->msgs[j]->msg[k] );
546             align_output( 4 );
547         }
548     }
549 }
550
551 void write_bin_files(void)
552 {
553     lan_blk_t *lbp;
554     token_t *ttab;
555     int ntab;
556     int i;
557
558     get_tokentable(&ttab, &ntab);
559
560     for (lbp = lanblockhead; lbp; lbp = lbp->next)
561     {
562         char *cptr = NULL;
563
564         for (i = 0; i < ntab; i++)
565         {
566             if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
567             {
568                 if (ttab[i].alias) cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
569                 break;
570             }
571         }
572         if(!cptr)
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 );
579         free(cptr);
580     }
581 }
582
583 void write_res_file( const char *name )
584 {
585     lan_blk_t *lbp;
586     int i, j;
587
588     init_output_buffer();
589
590     put_dword( 0 );      /* ResSize */
591     put_dword( 32 );     /* HeaderSize */
592     put_word( 0xffff );  /* ResType */
593     put_word( 0x0000 );
594     put_word( 0xffff );  /* ResName */
595     put_word( 0x0000 );
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 */
601
602     for (lbp = lanblockhead; lbp; lbp = lbp->next)
603     {
604         unsigned int data_size = 4 * (lbp->nblk * 3 + 1);
605         unsigned int header_size = 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
606
607         for (i = 0; i < lbp->nblk; i++)
608         {
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);
611         }
612
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 */
618         put_word( 0x0001 );
619         align_output( 4 );
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 */
625
626         output_bin_data( lbp );
627     }
628     flush_output_buffer( name );
629 }