write.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include "wmc.h"
  28. #include "utils.h"
  29. #include "lang.h"
  30. #include "write.h"
  31. /*
  32. * The binary resource layout is as follows:
  33. *
  34. * +===============+
  35. * Header | NBlocks |
  36. * +===============+
  37. * Block 0 | Low ID |
  38. * +---------------+
  39. * | High ID |
  40. * +---------------+
  41. * | Offset |---+
  42. * +===============+ |
  43. * Block 1 | Low ID | |
  44. * +---------------+ |
  45. * | High ID | |
  46. * +---------------+ |
  47. * | Offset |------+
  48. * +===============+ | |
  49. * | | | |
  50. * ... ... | |
  51. * | | | |
  52. * +===============+ <-+ |
  53. * B0 LoID | Len | Flags | |
  54. * +---+---+---+---+ |
  55. * | b | l | a | b | |
  56. * +---+---+---+---+ |
  57. * | l | a | \0| \0| |
  58. * +===============+ |
  59. * | | |
  60. * ... ... |
  61. * | | |
  62. * +===============+ |
  63. * B0 HiID | Len | Flags | |
  64. * +---+---+---+---+ |
  65. * | M | o | r | e | |
  66. * +---+---+---+---+ |
  67. * | b | l | a | \0| |
  68. * +===============+ <----+
  69. * B1 LoID | Len | Flags |
  70. * +---+---+---+---+
  71. * | J | u | n | k |
  72. * +---+---+---+---+
  73. * | \0| \0| \0| \0|
  74. * +===============+
  75. * | |
  76. * ... ...
  77. * | |
  78. * +===============+
  79. *
  80. * All Fields are aligned on their natural boundaries. The length
  81. * field (Len) covers both the length of the string and the header
  82. * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
  83. * for normal character strings and 1 for unicode strings.
  84. */
  85. static const char str_header[] =
  86. "/* This file is generated with wmc version " PACKAGE_VERSION ". Do not edit! */\n"
  87. "/* Source : %s */\n"
  88. "/* Cmdline: %s */\n"
  89. "\n"
  90. ;
  91. static char *dup_u2c(const WCHAR *uc)
  92. {
  93. int i;
  94. char *cptr = xmalloc( unistrlen(uc)+1 );
  95. for (i = 0; *uc; i++, uc++) cptr[i] = (*uc <= 0xff) ? *uc : '_';
  96. cptr[i] = 0;
  97. return cptr;
  98. }
  99. static void killnl(char *s, int ddd)
  100. {
  101. char *tmp;
  102. tmp = strstr(s, "\r\n");
  103. if(tmp)
  104. {
  105. if(ddd && tmp - s > 3)
  106. {
  107. tmp[0] = tmp[1] = tmp[2] = '.';
  108. tmp[3] = '\0';
  109. }
  110. else
  111. *tmp = '\0';
  112. }
  113. tmp = strchr(s, '\n');
  114. if(tmp)
  115. {
  116. if(ddd && tmp - s > 3)
  117. {
  118. tmp[0] = tmp[1] = tmp[2] = '.';
  119. tmp[3] = '\0';
  120. }
  121. else
  122. *tmp = '\0';
  123. }
  124. }
  125. static int killcomment(char *s)
  126. {
  127. char *tmp = s;
  128. int b = 0;
  129. while((tmp = strstr(tmp, "/*")))
  130. {
  131. tmp[1] = 'x';
  132. b++;
  133. }
  134. tmp = s;
  135. while((tmp = strstr(tmp, "*/")))
  136. {
  137. tmp[0] = 'x';
  138. b++;
  139. }
  140. return b;
  141. }
  142. void write_h_file(const char *fname)
  143. {
  144. node_t *ndp;
  145. char *cptr;
  146. char *cast;
  147. FILE *fp;
  148. token_t *ttab;
  149. int ntab;
  150. int i;
  151. int once = 0;
  152. int idx_en = 0;
  153. fp = fopen(fname, "w");
  154. if(!fp)
  155. {
  156. perror(fname);
  157. exit(1);
  158. }
  159. fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
  160. fprintf(fp, "#ifndef __WMCGENERATED_H\n");
  161. fprintf(fp, "#define __WMCGENERATED_H\n");
  162. fprintf(fp, "\n");
  163. /* Write severity and facility aliases */
  164. get_tokentable(&ttab, &ntab);
  165. fprintf(fp, "/* Severity codes */\n");
  166. for(i = 0; i < ntab; i++)
  167. {
  168. if(ttab[i].type == tok_severity && ttab[i].alias)
  169. {
  170. cptr = dup_u2c(ttab[i].alias);
  171. fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
  172. free(cptr);
  173. }
  174. }
  175. fprintf(fp, "\n");
  176. fprintf(fp, "/* Facility codes */\n");
  177. for(i = 0; i < ntab; i++)
  178. {
  179. if(ttab[i].type == tok_facility && ttab[i].alias)
  180. {
  181. cptr = dup_u2c(ttab[i].alias);
  182. fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
  183. free(cptr);
  184. }
  185. }
  186. fprintf(fp, "\n");
  187. /* Write the message codes */
  188. fprintf(fp, "/* Message definitions */\n");
  189. for(ndp = nodehead; ndp; ndp = ndp->next)
  190. {
  191. switch(ndp->type)
  192. {
  193. case nd_comment:
  194. cptr = dup_u2c(ndp->u.comment+1);
  195. killnl(cptr, 0);
  196. killcomment(cptr);
  197. if(*cptr)
  198. fprintf(fp, "/* %s */\n", cptr);
  199. else
  200. fprintf(fp, "\n");
  201. free(cptr);
  202. break;
  203. case nd_msg:
  204. if(!once)
  205. {
  206. /*
  207. * Search for an English text.
  208. * If not found, then use the first in the list
  209. */
  210. once++;
  211. for(i = 0; i < ndp->u.msg->nmsgs; i++)
  212. {
  213. if(ndp->u.msg->msgs[i]->lan == 0x409)
  214. {
  215. idx_en = i;
  216. break;
  217. }
  218. }
  219. fprintf(fp, "\n");
  220. }
  221. fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
  222. cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->msg);
  223. killnl(cptr, 0);
  224. killcomment(cptr);
  225. fprintf(fp, "/* Approximate msg: %s */\n", cptr);
  226. free(cptr);
  227. cptr = dup_u2c(ndp->u.msg->sym);
  228. if(ndp->u.msg->cast)
  229. cast = dup_u2c(ndp->u.msg->cast);
  230. else
  231. cast = NULL;
  232. switch(ndp->u.msg->base)
  233. {
  234. case 8:
  235. if(cast)
  236. fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
  237. else
  238. fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
  239. break;
  240. case 10:
  241. if(cast)
  242. fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
  243. else
  244. fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
  245. break;
  246. case 16:
  247. if(cast)
  248. fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
  249. else
  250. fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
  251. break;
  252. default:
  253. internal_error(__FILE__, __LINE__, "Invalid base for number print\n");
  254. }
  255. free(cptr);
  256. free(cast);
  257. break;
  258. default:
  259. internal_error(__FILE__, __LINE__, "Invalid node type %d\n", ndp->type);
  260. }
  261. }
  262. fprintf(fp, "\n#endif\n");
  263. fclose(fp);
  264. }
  265. static void write_rcbin(FILE *fp)
  266. {
  267. lan_blk_t *lbp;
  268. token_t *ttab;
  269. int ntab;
  270. int i;
  271. get_tokentable(&ttab, &ntab);
  272. for(lbp = lanblockhead; lbp; lbp = lbp->next)
  273. {
  274. char *cptr = NULL;
  275. fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
  276. for(i = 0; i < ntab; i++)
  277. {
  278. if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
  279. {
  280. if(ttab[i].alias)
  281. cptr = dup_u2c(ttab[i].alias);
  282. break;
  283. }
  284. }
  285. if(!cptr)
  286. internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
  287. fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
  288. free(cptr);
  289. }
  290. }
  291. static char *make_string(WCHAR *uc, int len)
  292. {
  293. char *str = xmalloc(7*len + 12);
  294. char *cptr = str;
  295. int i;
  296. int b;
  297. *cptr++ = ' ';
  298. *cptr++ = 'L';
  299. *cptr++ = '"';
  300. for(i = b = 0; i < len; i++, uc++)
  301. {
  302. switch(*uc)
  303. {
  304. case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
  305. case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
  306. case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
  307. case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
  308. case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
  309. case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
  310. case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
  311. case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
  312. case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
  313. default:
  314. if (*uc < 0x100 && isprint(*uc))
  315. {
  316. *cptr++ = *uc;
  317. b++;
  318. }
  319. else
  320. {
  321. int n = sprintf(cptr, "\\x%04x", *uc);
  322. cptr += n;
  323. b += n;
  324. }
  325. break;
  326. }
  327. if(i < len-1 && b >= 72)
  328. {
  329. *cptr++ = '"';
  330. *cptr++ = ',';
  331. *cptr++ = '\n';
  332. *cptr++ = ' ';
  333. *cptr++ = 'L';
  334. *cptr++ = '"';
  335. b = 0;
  336. }
  337. }
  338. len = (len + 1) & ~1;
  339. for(; i < len; i++)
  340. {
  341. *cptr++ = '\\';
  342. *cptr++ = 'x';
  343. *cptr++ = '0';
  344. *cptr++ = '0';
  345. *cptr++ = '0';
  346. *cptr++ = '0';
  347. }
  348. *cptr++ = '"';
  349. *cptr = '\0';
  350. return str;
  351. }
  352. static void write_rcinline(FILE *fp)
  353. {
  354. lan_blk_t *lbp;
  355. int i;
  356. int j;
  357. for(lbp = lanblockhead; lbp; lbp = lbp->next)
  358. {
  359. unsigned offs = 4 * (lbp->nblk * 3 + 1);
  360. fprintf(fp, "\n1 MESSAGETABLE\n");
  361. fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
  362. fprintf(fp, "{\n");
  363. fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
  364. for(i = 0; i < lbp->nblk; i++)
  365. {
  366. fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
  367. lbp->blks[i].idlo,
  368. lbp->blks[i].idhi,
  369. offs);
  370. offs += lbp->blks[i].size;
  371. }
  372. for(i = 0; i < lbp->nblk; i++)
  373. {
  374. block_t *blk = &lbp->blks[i];
  375. for(j = 0; j < blk->nmsg; j++)
  376. {
  377. char *cptr;
  378. int l = blk->msgs[j]->len;
  379. const char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
  380. cptr = make_string(blk->msgs[j]->msg, l);
  381. fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x0001,\n",
  382. blk->idlo + j, ((l * 2 + 3) & ~3) + 4 );
  383. fprintf(fp, "%s%s\n", cptr, comma);
  384. free(cptr);
  385. }
  386. }
  387. fprintf(fp, "}\n");
  388. }
  389. }
  390. void write_rc_file(const char *fname)
  391. {
  392. FILE *fp = fopen(fname, "w");
  393. if(!fp)
  394. {
  395. perror(fname);
  396. exit(1);
  397. }
  398. fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
  399. if(rcinline)
  400. write_rcinline(fp);
  401. else
  402. write_rcbin(fp);
  403. fclose(fp);
  404. }
  405. static void output_bin_data( lan_blk_t *lbp )
  406. {
  407. unsigned int offs = 4 * (lbp->nblk * 3 + 1);
  408. int i, j, k;
  409. put_dword( lbp->nblk ); /* NBlocks */
  410. for (i = 0; i < lbp->nblk; i++)
  411. {
  412. put_dword( lbp->blks[i].idlo ); /* Lo */
  413. put_dword( lbp->blks[i].idhi ); /* Hi */
  414. put_dword( offs ); /* Offs */
  415. offs += lbp->blks[i].size;
  416. }
  417. for (i = 0; i < lbp->nblk; i++)
  418. {
  419. block_t *blk = &lbp->blks[i];
  420. for (j = 0; j < blk->nmsg; j++)
  421. {
  422. int len = (2 * blk->msgs[j]->len + 3) & ~3;
  423. put_word( len + 4 );
  424. put_word( 1 );
  425. for (k = 0; k < blk->msgs[j]->len; k++) put_word( blk->msgs[j]->msg[k] );
  426. align_output( 4 );
  427. }
  428. }
  429. }
  430. void write_bin_files(void)
  431. {
  432. lan_blk_t *lbp;
  433. token_t *ttab;
  434. int ntab;
  435. int i;
  436. get_tokentable(&ttab, &ntab);
  437. for (lbp = lanblockhead; lbp; lbp = lbp->next)
  438. {
  439. char *cptr = NULL;
  440. for (i = 0; i < ntab; i++)
  441. {
  442. if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
  443. {
  444. if (ttab[i].alias) cptr = dup_u2c(ttab[i].alias);
  445. break;
  446. }
  447. }
  448. if(!cptr)
  449. internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
  450. init_output_buffer();
  451. output_bin_data( lbp );
  452. cptr = xrealloc( cptr, strlen(cptr) + 5 );
  453. strcat( cptr, ".bin" );
  454. flush_output_buffer( cptr );
  455. free(cptr);
  456. }
  457. }
  458. void write_res_file( const char *name )
  459. {
  460. lan_blk_t *lbp;
  461. int i, j;
  462. init_output_buffer();
  463. put_dword( 0 ); /* ResSize */
  464. put_dword( 32 ); /* HeaderSize */
  465. put_word( 0xffff ); /* ResType */
  466. put_word( 0x0000 );
  467. put_word( 0xffff ); /* ResName */
  468. put_word( 0x0000 );
  469. put_dword( 0 ); /* DataVersion */
  470. put_word( 0 ); /* Memory options */
  471. put_word( 0 ); /* Language */
  472. put_dword( 0 ); /* Version */
  473. put_dword( 0 ); /* Characteristics */
  474. for (lbp = lanblockhead; lbp; lbp = lbp->next)
  475. {
  476. unsigned int data_size = 4 * (lbp->nblk * 3 + 1);
  477. unsigned int header_size = 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
  478. for (i = 0; i < lbp->nblk; i++)
  479. {
  480. block_t *blk = &lbp->blks[i];
  481. for (j = 0; j < blk->nmsg; j++) data_size += 4 + ((blk->msgs[j]->len * 2 + 3) & ~3);
  482. }
  483. put_dword( data_size ); /* ResSize */
  484. put_dword( header_size ); /* HeaderSize */
  485. put_word( 0xffff ); /* ResType */
  486. put_word( 0x000b /*RT_MESSAGETABLE*/ );
  487. put_word( 0xffff ); /* ResName */
  488. put_word( 0x0001 );
  489. align_output( 4 );
  490. put_dword( 0 ); /* DataVersion */
  491. put_word( 0x30 ); /* Memory options */
  492. put_word( lbp->lan ); /* Language */
  493. put_dword( lbp->version ); /* Version */
  494. put_dword( 0 ); /* Characteristics */
  495. output_bin_data( lbp );
  496. }
  497. flush_output_buffer( name );
  498. }