gencat.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 1996.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  14. #ifdef HAVE_CONFIG_H
  15. # include "config.h"
  16. #endif
  17. #include <argp.h>
  18. #include <assert.h>
  19. #include <ctype.h>
  20. #include <endian.h>
  21. #include <errno.h>
  22. #include <error.h>
  23. #include <fcntl.h>
  24. #include <iconv.h>
  25. #include <langinfo.h>
  26. #include <locale.h>
  27. #include <libintl.h>
  28. #include <limits.h>
  29. #include <nl_types.h>
  30. #include <obstack.h>
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include <wchar.h>
  37. #include "version.h"
  38. #include "catgetsinfo.h"
  39. #define SWAPU32(w) \
  40. (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
  41. struct message_list
  42. {
  43. int number;
  44. const char *message;
  45. const char *fname;
  46. size_t line;
  47. const char *symbol;
  48. struct message_list *next;
  49. };
  50. struct set_list
  51. {
  52. int number;
  53. int deleted;
  54. struct message_list *messages;
  55. int last_message;
  56. const char *fname;
  57. size_t line;
  58. const char *symbol;
  59. struct set_list *next;
  60. };
  61. struct catalog
  62. {
  63. struct set_list *all_sets;
  64. struct set_list *current_set;
  65. size_t total_messages;
  66. wint_t quote_char;
  67. int last_set;
  68. struct obstack mem_pool;
  69. };
  70. /* If non-zero force creation of new file, not using existing one. */
  71. static int force_new;
  72. /* Name of output file. */
  73. static const char *output_name;
  74. /* Name of generated C header file. */
  75. static const char *header_name;
  76. /* Name and version of program. */
  77. static void print_version (FILE *stream, struct argp_state *state);
  78. void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
  79. #define OPT_NEW 1
  80. /* Definitions of arguments for argp functions. */
  81. static const struct argp_option options[] =
  82. {
  83. { "header", 'H', N_("NAME"), 0,
  84. N_("Create C header file NAME containing symbol definitions") },
  85. { "new", OPT_NEW, NULL, 0,
  86. N_("Do not use existing catalog, force new output file") },
  87. { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
  88. { NULL, 0, NULL, 0, NULL }
  89. };
  90. /* Short description of program. */
  91. static const char doc[] = N_("Generate message catalog.\
  92. \vIf INPUT-FILE is -, input is read from standard input. If OUTPUT-FILE\n\
  93. is -, output is written to standard output.\n");
  94. /* Strings for arguments in help texts. */
  95. static const char args_doc[] = N_("\
  96. -o OUTPUT-FILE [INPUT-FILE]...\n[OUTPUT-FILE [INPUT-FILE]...]");
  97. /* Prototype for option handler. */
  98. static error_t parse_opt (int key, char *arg, struct argp_state *state);
  99. /* Function to print some extra text in the help message. */
  100. static char *more_help (int key, const char *text, void *input);
  101. /* Data structure to communicate with argp functions. */
  102. static struct argp argp =
  103. {
  104. options, parse_opt, args_doc, doc, NULL, more_help
  105. };
  106. /* Wrapper functions with error checking for standard functions. */
  107. #include <programs/xmalloc.h>
  108. /* Prototypes for local functions. */
  109. static void error_print (void);
  110. static struct catalog *read_input_file (struct catalog *current,
  111. const char *fname);
  112. static void write_out (struct catalog *result, const char *output_name,
  113. const char *header_name);
  114. static struct set_list *find_set (struct catalog *current, int number);
  115. static void normalize_line (const char *fname, size_t line, iconv_t cd,
  116. wchar_t *string, wchar_t quote_char,
  117. wchar_t escape_char);
  118. static void read_old (struct catalog *catalog, const char *file_name);
  119. static int open_conversion (const char *codesetp, iconv_t *cd_towcp,
  120. iconv_t *cd_tombp, wchar_t *escape_charp);
  121. int
  122. main (int argc, char *argv[])
  123. {
  124. struct catalog *result;
  125. int remaining;
  126. /* Set program name for messages. */
  127. error_print_progname = error_print;
  128. /* Set locale via LC_ALL. */
  129. setlocale (LC_ALL, "");
  130. /* Set the text message domain. */
  131. textdomain (PACKAGE);
  132. /* Initialize local variables. */
  133. result = NULL;
  134. /* Parse and process arguments. */
  135. argp_parse (&argp, argc, argv, 0, &remaining, NULL);
  136. /* Determine output file. */
  137. if (output_name == NULL)
  138. output_name = remaining < argc ? argv[remaining++] : "-";
  139. /* Process all input files. */
  140. setlocale (LC_CTYPE, "C");
  141. if (remaining < argc)
  142. do
  143. result = read_input_file (result, argv[remaining]);
  144. while (++remaining < argc);
  145. else
  146. result = read_input_file (NULL, "-");
  147. /* Write out the result. */
  148. if (result != NULL)
  149. write_out (result, output_name, header_name);
  150. return error_message_count != 0;
  151. }
  152. /* Handle program arguments. */
  153. static error_t
  154. parse_opt (int key, char *arg, struct argp_state *state)
  155. {
  156. switch (key)
  157. {
  158. case 'H':
  159. header_name = arg;
  160. break;
  161. case OPT_NEW:
  162. force_new = 1;
  163. break;
  164. case 'o':
  165. output_name = arg;
  166. break;
  167. default:
  168. return ARGP_ERR_UNKNOWN;
  169. }
  170. return 0;
  171. }
  172. static char *
  173. more_help (int key, const char *text, void *input)
  174. {
  175. char *tp = NULL;
  176. switch (key)
  177. {
  178. case ARGP_KEY_HELP_EXTRA:
  179. /* We print some extra information. */
  180. if (asprintf (&tp, gettext ("\
  181. For bug reporting instructions, please see:\n\
  182. %s.\n"), REPORT_BUGS_TO) < 0)
  183. return NULL;
  184. return tp;
  185. default:
  186. break;
  187. }
  188. return (char *) text;
  189. }
  190. /* Print the version information. */
  191. static void
  192. print_version (FILE *stream, struct argp_state *state)
  193. {
  194. fprintf (stream, "gencat %s%s\n", PKGVERSION, VERSION);
  195. fprintf (stream, gettext ("\
  196. Copyright (C) %s Free Software Foundation, Inc.\n\
  197. This is free software; see the source for copying conditions. There is NO\n\
  198. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
  199. "), "2019");
  200. fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
  201. }
  202. /* The address of this function will be assigned to the hook in the
  203. error functions. */
  204. static void
  205. error_print (void)
  206. {
  207. /* We don't want the program name to be printed in messages. Emacs'
  208. compile.el does not like this. */
  209. }
  210. static struct catalog *
  211. read_input_file (struct catalog *current, const char *fname)
  212. {
  213. FILE *fp;
  214. char *buf;
  215. size_t len;
  216. size_t line_number;
  217. wchar_t *wbuf;
  218. size_t wbufsize;
  219. iconv_t cd_towc = (iconv_t) -1;
  220. iconv_t cd_tomb = (iconv_t) -1;
  221. wchar_t escape_char = L'\\';
  222. char *codeset = NULL;
  223. if (strcmp (fname, "-") == 0 || strcmp (fname, "/dev/stdin") == 0)
  224. {
  225. fp = stdin;
  226. fname = gettext ("*standard input*");
  227. }
  228. else
  229. fp = fopen (fname, "r");
  230. if (fp == NULL)
  231. {
  232. error (0, errno, gettext ("cannot open input file `%s'"), fname);
  233. return current;
  234. }
  235. /* If we haven't seen anything yet, allocate result structure. */
  236. if (current == NULL)
  237. {
  238. current = (struct catalog *) xcalloc (1, sizeof (*current));
  239. #define obstack_chunk_alloc malloc
  240. #define obstack_chunk_free free
  241. obstack_init (&current->mem_pool);
  242. current->current_set = find_set (current, NL_SETD);
  243. }
  244. buf = NULL;
  245. len = 0;
  246. line_number = 0;
  247. wbufsize = 1024;
  248. wbuf = (wchar_t *) xmalloc (wbufsize);
  249. while (!feof (fp))
  250. {
  251. int continued;
  252. int used;
  253. size_t start_line = line_number + 1;
  254. char *this_line;
  255. do
  256. {
  257. int act_len;
  258. act_len = getline (&buf, &len, fp);
  259. if (act_len <= 0)
  260. break;
  261. ++line_number;
  262. /* It the line continued? */
  263. continued = 0;
  264. if (buf[act_len - 1] == '\n')
  265. {
  266. --act_len;
  267. /* There might be more than one backslash at the end of
  268. the line. Only if there is an odd number of them is
  269. the line continued. */
  270. if (act_len > 0 && buf[act_len - 1] == '\\')
  271. {
  272. int temp_act_len = act_len;
  273. do
  274. {
  275. --temp_act_len;
  276. continued = !continued;
  277. }
  278. while (temp_act_len > 0 && buf[temp_act_len - 1] == '\\');
  279. if (continued)
  280. --act_len;
  281. }
  282. }
  283. /* Append to currently selected line. */
  284. obstack_grow (&current->mem_pool, buf, act_len);
  285. }
  286. while (continued);
  287. obstack_1grow (&current->mem_pool, '\0');
  288. this_line = (char *) obstack_finish (&current->mem_pool);
  289. used = 0;
  290. if (this_line[0] == '$')
  291. {
  292. if (isblank (this_line[1]))
  293. {
  294. int cnt = 1;
  295. while (isblank (this_line[cnt]))
  296. ++cnt;
  297. if (strncmp (&this_line[cnt], "codeset=", 8) != 0)
  298. /* This is a comment line. Do nothing. */;
  299. else if (codeset != NULL)
  300. /* Ignore multiple codeset. */;
  301. else
  302. {
  303. int start = cnt + 8;
  304. cnt = start;
  305. while (this_line[cnt] != '\0' && !isspace (this_line[cnt]))
  306. ++cnt;
  307. if (cnt != start)
  308. {
  309. int len = cnt - start;
  310. codeset = xmalloc (len + 1);
  311. *((char *) mempcpy (codeset, &this_line[start], len))
  312. = '\0';
  313. }
  314. }
  315. }
  316. else if (strncmp (&this_line[1], "set", 3) == 0)
  317. {
  318. int cnt = sizeof ("set");
  319. int set_number;
  320. const char *symbol = NULL;
  321. while (isspace (this_line[cnt]))
  322. ++cnt;
  323. if (isdigit (this_line[cnt]))
  324. {
  325. set_number = atol (&this_line[cnt]);
  326. /* If the given number for the character set is
  327. higher than any we used for symbolic set names
  328. avoid clashing by using only higher numbers for
  329. the following symbolic definitions. */
  330. if (set_number > current->last_set)
  331. current->last_set = set_number;
  332. }
  333. else
  334. {
  335. /* See whether it is a reasonable identifier. */
  336. int start = cnt;
  337. while (isalnum (this_line[cnt]) || this_line[cnt] == '_')
  338. ++cnt;
  339. if (cnt == start)
  340. {
  341. /* No correct character found. */
  342. error_at_line (0, 0, fname, start_line,
  343. gettext ("illegal set number"));
  344. set_number = 0;
  345. }
  346. else
  347. {
  348. /* We have found seomthing that looks like a
  349. correct identifier. */
  350. struct set_list *runp;
  351. this_line[cnt] = '\0';
  352. used = 1;
  353. symbol = &this_line[start];
  354. /* Test whether the identifier was already used. */
  355. runp = current->all_sets;
  356. while (runp != 0)
  357. if (runp->symbol != NULL
  358. && strcmp (runp->symbol, symbol) == 0)
  359. break;
  360. else
  361. runp = runp->next;
  362. if (runp != NULL)
  363. {
  364. /* We cannot allow duplicate identifiers for
  365. message sets. */
  366. error_at_line (0, 0, fname, start_line,
  367. gettext ("duplicate set definition"));
  368. error_at_line (0, 0, runp->fname, runp->line,
  369. gettext ("\
  370. this is the first definition"));
  371. set_number = 0;
  372. }
  373. else
  374. /* Allocate next free message set for identifier. */
  375. set_number = ++current->last_set;
  376. }
  377. }
  378. if (set_number != 0)
  379. {
  380. /* We found a legal set number. */
  381. current->current_set = find_set (current, set_number);
  382. if (symbol != NULL)
  383. used = 1;
  384. current->current_set->symbol = symbol;
  385. current->current_set->fname = fname;
  386. current->current_set->line = start_line;
  387. }
  388. }
  389. else if (strncmp (&this_line[1], "delset", 6) == 0)
  390. {
  391. int cnt = sizeof ("delset");
  392. while (isspace (this_line[cnt]))
  393. ++cnt;
  394. if (isdigit (this_line[cnt]))
  395. {
  396. size_t set_number = atol (&this_line[cnt]);
  397. struct set_list *set;
  398. /* Mark the message set with the given number as
  399. deleted. */
  400. set = find_set (current, set_number);
  401. set->deleted = 1;
  402. }
  403. else
  404. {
  405. /* See whether it is a reasonable identifier. */
  406. int start = cnt;
  407. while (isalnum (this_line[cnt]) || this_line[cnt] == '_')
  408. ++cnt;
  409. if (cnt == start)
  410. error_at_line (0, 0, fname, start_line,
  411. gettext ("illegal set number"));
  412. else
  413. {
  414. const char *symbol;
  415. struct set_list *runp;
  416. this_line[cnt] = '\0';
  417. used = 1;
  418. symbol = &this_line[start];
  419. /* We have a symbolic set name. This name must
  420. appear somewhere else in the catalogs read so
  421. far. */
  422. for (runp = current->all_sets; runp != NULL;
  423. runp = runp->next)
  424. {
  425. if (strcmp (runp->symbol, symbol) == 0)
  426. {
  427. runp->deleted = 1;
  428. break;
  429. }
  430. }
  431. if (runp == NULL)
  432. /* Name does not exist before. */
  433. error_at_line (0, 0, fname, start_line,
  434. gettext ("unknown set `%s'"), symbol);
  435. }
  436. }
  437. }
  438. else if (strncmp (&this_line[1], "quote", 5) == 0)
  439. {
  440. char buf[2];
  441. char *bufptr;
  442. size_t buflen;
  443. char *wbufptr;
  444. size_t wbuflen;
  445. int cnt;
  446. cnt = sizeof ("quote");
  447. while (isspace (this_line[cnt]))
  448. ++cnt;
  449. /* We need the conversion. */
  450. if (cd_towc == (iconv_t) -1
  451. && open_conversion (codeset, &cd_towc, &cd_tomb,
  452. &escape_char) != 0)
  453. /* Something is wrong. */
  454. goto out;
  455. /* Yes, the quote char can be '\0'; this means no quote
  456. char. The function using the information works on
  457. wide characters so we have to convert it here. */
  458. buf[0] = this_line[cnt];
  459. buf[1] = '\0';
  460. bufptr = buf;
  461. buflen = 2;
  462. wbufptr = (char *) wbuf;
  463. wbuflen = wbufsize;
  464. /* Flush the state. */
  465. iconv (cd_towc, NULL, NULL, NULL, NULL);
  466. iconv (cd_towc, &bufptr, &buflen, &wbufptr, &wbuflen);
  467. if (buflen != 0 || (wchar_t *) wbufptr != &wbuf[2])
  468. error_at_line (0, 0, fname, start_line,
  469. gettext ("invalid quote character"));
  470. else
  471. /* Use the converted wide character. */
  472. current->quote_char = wbuf[0];
  473. }
  474. else
  475. {
  476. int cnt;
  477. cnt = 2;
  478. while (this_line[cnt] != '\0' && !isspace (this_line[cnt]))
  479. ++cnt;
  480. this_line[cnt] = '\0';
  481. error_at_line (0, 0, fname, start_line,
  482. gettext ("unknown directive `%s': line ignored"),
  483. &this_line[1]);
  484. }
  485. }
  486. else if (isalnum (this_line[0]) || this_line[0] == '_')
  487. {
  488. const char *ident = this_line;
  489. char *line = this_line;
  490. int message_number;
  491. do
  492. ++line;
  493. while (line[0] != '\0' && !isspace (line[0]));
  494. if (line[0] != '\0')
  495. *line++ = '\0'; /* Terminate the identifier. */
  496. /* Now we found the beginning of the message itself. */
  497. if (isdigit (ident[0]))
  498. {
  499. struct message_list *runp;
  500. struct message_list *lastp;
  501. message_number = atoi (ident);
  502. /* Find location to insert the new message. */
  503. runp = current->current_set->messages;
  504. lastp = NULL;
  505. while (runp != NULL)
  506. if (runp->number == message_number)
  507. break;
  508. else
  509. {
  510. lastp = runp;
  511. runp = runp->next;
  512. }
  513. if (runp != NULL)
  514. {
  515. /* Oh, oh. There is already a message with this
  516. number in the message set. */
  517. if (runp->symbol == NULL)
  518. {
  519. /* The existing message had its number specified
  520. by the user. Fatal collision type uh, oh. */
  521. error_at_line (0, 0, fname, start_line,
  522. gettext ("duplicated message number"));
  523. error_at_line (0, 0, runp->fname, runp->line,
  524. gettext ("this is the first definition"));
  525. message_number = 0;
  526. }
  527. else
  528. {
  529. /* Collision was with number auto-assigned to a
  530. symbolic. Change existing symbolic number
  531. and move to end the list (if not already there). */
  532. runp->number = ++current->current_set->last_message;
  533. if (runp->next != NULL)
  534. {
  535. struct message_list *endp;
  536. if (lastp == NULL)
  537. current->current_set->messages=runp->next;
  538. else
  539. lastp->next=runp->next;
  540. endp = runp->next;
  541. while (endp->next != NULL)
  542. endp = endp->next;
  543. endp->next = runp;
  544. runp->next = NULL;
  545. }
  546. }
  547. }
  548. ident = NULL; /* We don't have a symbol. */
  549. if (message_number != 0
  550. && message_number > current->current_set->last_message)
  551. current->current_set->last_message = message_number;
  552. }
  553. else if (ident[0] != '\0')
  554. {
  555. struct message_list *runp;
  556. /* Test whether the symbolic name was not used for
  557. another message in this message set. */
  558. runp = current->current_set->messages;
  559. while (runp != NULL)
  560. if (runp->symbol != NULL && strcmp (ident, runp->symbol) == 0)
  561. break;
  562. else
  563. runp = runp->next;
  564. if (runp != NULL)
  565. {
  566. /* The name is already used. */
  567. error_at_line (0, 0, fname, start_line, gettext ("\
  568. duplicated message identifier"));
  569. error_at_line (0, 0, runp->fname, runp->line,
  570. gettext ("this is the first definition"));
  571. message_number = 0;
  572. }
  573. else
  574. /* Give the message the next unused number. */
  575. message_number = ++current->current_set->last_message;
  576. }
  577. else
  578. message_number = 0;
  579. if (message_number != 0)
  580. {
  581. char *inbuf;
  582. size_t inlen;
  583. char *outbuf;
  584. size_t outlen;
  585. struct message_list *newp;
  586. size_t line_len = strlen (line) + 1;
  587. size_t ident_len = 0;
  588. /* We need the conversion. */
  589. if (cd_towc == (iconv_t) -1
  590. && open_conversion (codeset, &cd_towc, &cd_tomb,
  591. &escape_char) != 0)
  592. /* Something is wrong. */
  593. goto out;
  594. /* Convert to a wide character string. We have to
  595. interpret escape sequences which will be impossible
  596. without doing the conversion if the codeset of the
  597. message is stateful. */
  598. while (1)
  599. {
  600. inbuf = line;
  601. inlen = line_len;
  602. outbuf = (char *) wbuf;
  603. outlen = wbufsize;
  604. /* Flush the state. */
  605. iconv (cd_towc, NULL, NULL, NULL, NULL);
  606. iconv (cd_towc, &inbuf, &inlen, &outbuf, &outlen);
  607. if (inlen == 0)
  608. {
  609. /* The string is converted. */
  610. assert (outlen < wbufsize);
  611. assert (wbuf[(wbufsize - outlen) / sizeof (wchar_t) - 1]
  612. == L'\0');
  613. break;
  614. }
  615. if (outlen != 0)
  616. {
  617. /* Something is wrong with this string, we ignore it. */
  618. error_at_line (0, 0, fname, start_line, gettext ("\
  619. invalid character: message ignored"));
  620. goto ignore;
  621. }
  622. /* The output buffer is too small. */
  623. wbufsize *= 2;
  624. wbuf = (wchar_t *) xrealloc (wbuf, wbufsize);
  625. }
  626. /* Strip quote characters, change escape sequences into
  627. correct characters etc. */
  628. normalize_line (fname, start_line, cd_towc, wbuf,
  629. current->quote_char, escape_char);
  630. if (ident)
  631. ident_len = line - this_line;
  632. /* Now the string is free of escape sequences. Convert it
  633. back into a multibyte character string. First free the
  634. memory allocated for the original string. */
  635. obstack_free (&current->mem_pool, this_line);
  636. used = 1; /* Yes, we use the line. */
  637. /* Now fill in the new string. It should never happen that
  638. the replaced string is longer than the original. */
  639. inbuf = (char *) wbuf;
  640. inlen = (wcslen (wbuf) + 1) * sizeof (wchar_t);
  641. outlen = obstack_room (&current->mem_pool);
  642. obstack_blank (&current->mem_pool, outlen);
  643. this_line = (char *) obstack_base (&current->mem_pool);
  644. outbuf = this_line + ident_len;
  645. outlen -= ident_len;
  646. /* Flush the state. */
  647. iconv (cd_tomb, NULL, NULL, NULL, NULL);
  648. iconv (cd_tomb, &inbuf, &inlen, &outbuf, &outlen);
  649. if (inlen != 0)
  650. {
  651. error_at_line (0, 0, fname, start_line,
  652. gettext ("invalid line"));
  653. goto ignore;
  654. }
  655. assert (outbuf[-1] == '\0');
  656. /* Free the memory in the obstack we don't use. */
  657. obstack_blank (&current->mem_pool, -(int) outlen);
  658. line = obstack_finish (&current->mem_pool);
  659. newp = (struct message_list *) xmalloc (sizeof (*newp));
  660. newp->number = message_number;
  661. newp->message = line + ident_len;
  662. /* Remember symbolic name; is NULL if no is given. */
  663. newp->symbol = ident ? line : NULL;
  664. /* Remember where we found the character. */
  665. newp->fname = fname;
  666. newp->line = start_line;
  667. /* Find place to insert to message. We keep them in a
  668. sorted single linked list. */
  669. if (current->current_set->messages == NULL
  670. || current->current_set->messages->number > message_number)
  671. {
  672. newp->next = current->current_set->messages;
  673. current->current_set->messages = newp;
  674. }
  675. else
  676. {
  677. struct message_list *runp;
  678. runp = current->current_set->messages;
  679. while (runp->next != NULL)
  680. if (runp->next->number > message_number)
  681. break;
  682. else
  683. runp = runp->next;
  684. newp->next = runp->next;
  685. runp->next = newp;
  686. }
  687. }
  688. ++current->total_messages;
  689. }
  690. else
  691. {
  692. size_t cnt;
  693. cnt = 0;
  694. /* See whether we have any non-white space character in this
  695. line. */
  696. while (this_line[cnt] != '\0' && isspace (this_line[cnt]))
  697. ++cnt;
  698. if (this_line[cnt] != '\0')
  699. /* Yes, some unknown characters found. */
  700. error_at_line (0, 0, fname, start_line,
  701. gettext ("malformed line ignored"));
  702. }
  703. ignore:
  704. /* We can save the memory for the line if it was not used. */
  705. if (!used)
  706. obstack_free (&current->mem_pool, this_line);
  707. }
  708. /* Close the conversion modules. */
  709. iconv_close (cd_towc);
  710. iconv_close (cd_tomb);
  711. free (codeset);
  712. out:
  713. free (wbuf);
  714. if (fp != stdin)
  715. fclose (fp);
  716. return current;
  717. }
  718. static void
  719. write_out (struct catalog *catalog, const char *output_name,
  720. const char *header_name)
  721. {
  722. /* Computing the "optimal" size. */
  723. struct set_list *set_run;
  724. size_t best_total, best_size, best_depth;
  725. size_t act_size, act_depth;
  726. struct catalog_obj obj;
  727. struct obstack string_pool;
  728. const char *strings;
  729. size_t strings_size;
  730. uint32_t *array1, *array2;
  731. size_t cnt;
  732. int fd;
  733. /* If not otherwise told try to read file with existing
  734. translations. */
  735. if (!force_new)
  736. read_old (catalog, output_name);
  737. /* Initialize best_size with a very high value. */
  738. best_total = best_size = best_depth = UINT_MAX;
  739. /* We need some start size for testing. Let's start with
  740. TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
  741. 5. */
  742. act_size = 1 + catalog->total_messages / 5;
  743. /* We determine the size of a hash table here. Because the message
  744. numbers can be chosen arbitrary by the programmer we cannot use
  745. the simple method of accessing the array using the message
  746. number. The algorithm is based on the trivial hash function
  747. NUMBER % TABLE_SIZE, where collisions are stored in a second
  748. dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
  749. the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
  750. while (act_size <= best_total)
  751. {
  752. size_t deep[act_size];
  753. act_depth = 1;
  754. memset (deep, '\0', act_size * sizeof (size_t));
  755. set_run = catalog->all_sets;
  756. while (set_run != NULL)
  757. {
  758. struct message_list *message_run;
  759. message_run = set_run->messages;
  760. while (message_run != NULL)
  761. {
  762. size_t idx = (message_run->number * set_run->number) % act_size;
  763. ++deep[idx];
  764. if (deep[idx] > act_depth)
  765. {
  766. act_depth = deep[idx];
  767. if (act_depth * act_size > best_total)
  768. break;
  769. }
  770. message_run = message_run->next;
  771. }
  772. set_run = set_run->next;
  773. }
  774. if (act_depth * act_size <= best_total)
  775. {
  776. /* We have found a better solution. */
  777. best_total = act_depth * act_size;
  778. best_size = act_size;
  779. best_depth = act_depth;
  780. }
  781. ++act_size;
  782. }
  783. /* let's be prepared for an empty message file. */
  784. if (best_size == UINT_MAX)
  785. {
  786. best_size = 1;
  787. best_depth = 1;
  788. }
  789. /* OK, now we have the size we will use. Fill in the header, build
  790. the table and the second one with swapped byte order. */
  791. obj.magic = CATGETS_MAGIC;
  792. obj.plane_size = best_size;
  793. obj.plane_depth = best_depth;
  794. /* Allocate room for all needed arrays. */
  795. array1 =
  796. (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
  797. memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
  798. array2
  799. = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
  800. obstack_init (&string_pool);
  801. set_run = catalog->all_sets;
  802. while (set_run != NULL)
  803. {
  804. struct message_list *message_run;
  805. message_run = set_run->messages;
  806. while (message_run != NULL)
  807. {
  808. size_t idx = (((message_run->number * set_run->number) % best_size)
  809. * 3);
  810. /* Determine collision depth. */
  811. while (array1[idx] != 0)
  812. idx += best_size * 3;
  813. /* Store set number, message number and pointer into string
  814. space, relative to the first string. */
  815. array1[idx + 0] = set_run->number;
  816. array1[idx + 1] = message_run->number;
  817. array1[idx + 2] = obstack_object_size (&string_pool);
  818. /* Add current string to the continuous space containing all
  819. strings. */
  820. obstack_grow0 (&string_pool, message_run->message,
  821. strlen (message_run->message));
  822. message_run = message_run->next;
  823. }
  824. set_run = set_run->next;
  825. }
  826. strings_size = obstack_object_size (&string_pool);
  827. strings = obstack_finish (&string_pool);
  828. /* Compute ARRAY2 by changing the byte order. */
  829. for (cnt = 0; cnt < best_size * best_depth * 3; ++cnt)
  830. array2[cnt] = SWAPU32 (array1[cnt]);
  831. /* Now we can write out the whole data. */
  832. if (strcmp (output_name, "-") == 0
  833. || strcmp (output_name, "/dev/stdout") == 0)
  834. fd = STDOUT_FILENO;
  835. else
  836. {
  837. fd = creat (output_name, 0666);
  838. if (fd < 0)
  839. error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
  840. output_name);
  841. }
  842. /* Write out header. */
  843. write (fd, &obj, sizeof (obj));
  844. /* We always write out the little endian version of the index
  845. arrays. */
  846. #if __BYTE_ORDER == __LITTLE_ENDIAN
  847. write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
  848. write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
  849. #elif __BYTE_ORDER == __BIG_ENDIAN
  850. write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
  851. write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
  852. #else
  853. # error Cannot handle __BYTE_ORDER byte order
  854. #endif
  855. /* Finally write the strings. */
  856. write (fd, strings, strings_size);
  857. if (fd != STDOUT_FILENO)
  858. close (fd);
  859. /* If requested now write out the header file. */
  860. if (header_name != NULL)
  861. {
  862. int first = 1;
  863. FILE *fp;
  864. /* Open output file. "-" or "/dev/stdout" means write to
  865. standard output. */
  866. if (strcmp (header_name, "-") == 0
  867. || strcmp (header_name, "/dev/stdout") == 0)
  868. fp = stdout;
  869. else
  870. {
  871. fp = fopen (header_name, "w");
  872. if (fp == NULL)
  873. error (EXIT_FAILURE, errno,
  874. gettext ("cannot open output file `%s'"), header_name);
  875. }
  876. /* Iterate over all sets and all messages. */
  877. set_run = catalog->all_sets;
  878. while (set_run != NULL)
  879. {
  880. struct message_list *message_run;
  881. /* If the current message set has a symbolic name write this
  882. out first. */
  883. if (set_run->symbol != NULL)
  884. fprintf (fp, "%s#define %sSet %#x\t/* %s:%Zu */\n",
  885. first ? "" : "\n", set_run->symbol, set_run->number - 1,
  886. set_run->fname, set_run->line);
  887. first = 0;
  888. message_run = set_run->messages;
  889. while (message_run != NULL)
  890. {
  891. /* If the current message has a symbolic name write
  892. #define out. But we have to take care for the set
  893. not having a symbolic name. */
  894. if (message_run->symbol != NULL)
  895. {
  896. if (set_run->symbol == NULL)
  897. fprintf (fp, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
  898. set_run->number, message_run->symbol,
  899. message_run->number, message_run->fname,
  900. message_run->line);
  901. else
  902. fprintf (fp, "#define %s%s %#x\t/* %s:%Zu */\n",
  903. set_run->symbol, message_run->symbol,
  904. message_run->number, message_run->fname,
  905. message_run->line);
  906. }
  907. message_run = message_run->next;
  908. }
  909. set_run = set_run->next;
  910. }
  911. if (fp != stdout)
  912. fclose (fp);
  913. }
  914. }
  915. static struct set_list *
  916. find_set (struct catalog *current, int number)
  917. {
  918. struct set_list *result = current->all_sets;
  919. /* We must avoid set number 0 because a set of this number signals
  920. in the tables that the entry is not occupied. */
  921. ++number;
  922. while (result != NULL)
  923. if (result->number == number)
  924. return result;
  925. else
  926. result = result->next;
  927. /* Prepare new message set. */
  928. result = (struct set_list *) xcalloc (1, sizeof (*result));
  929. result->number = number;
  930. result->next = current->all_sets;
  931. current->all_sets = result;
  932. return result;
  933. }
  934. /* Normalize given string *in*place* by processing escape sequences
  935. and quote characters. */
  936. static void
  937. normalize_line (const char *fname, size_t line, iconv_t cd, wchar_t *string,
  938. wchar_t quote_char, wchar_t escape_char)
  939. {
  940. int is_quoted;
  941. wchar_t *rp = string;
  942. wchar_t *wp = string;
  943. if (quote_char != L'\0' && *rp == quote_char)
  944. {
  945. is_quoted = 1;
  946. ++rp;
  947. }
  948. else
  949. is_quoted = 0;
  950. while (*rp != L'\0')
  951. if (*rp == quote_char)
  952. /* We simply end the string when we find the first time an
  953. not-escaped quote character. */
  954. break;
  955. else if (*rp == escape_char)
  956. {
  957. ++rp;
  958. if (quote_char != L'\0' && *rp == quote_char)
  959. /* This is an extension to XPG. */
  960. *wp++ = *rp++;
  961. else
  962. /* Recognize escape sequences. */
  963. switch (*rp)
  964. {
  965. case L'n':
  966. *wp++ = L'\n';
  967. ++rp;
  968. break;
  969. case L't':
  970. *wp++ = L'\t';
  971. ++rp;
  972. break;
  973. case L'v':
  974. *wp++ = L'\v';
  975. ++rp;
  976. break;
  977. case L'b':
  978. *wp++ = L'\b';
  979. ++rp;
  980. break;
  981. case L'r':
  982. *wp++ = L'\r';
  983. ++rp;
  984. break;
  985. case L'f':
  986. *wp++ = L'\f';
  987. ++rp;
  988. break;
  989. case L'0' ... L'7':
  990. {
  991. int number;
  992. char cbuf[2];
  993. char *cbufptr;
  994. size_t cbufin;
  995. wchar_t wcbuf[2];
  996. char *wcbufptr;
  997. size_t wcbufin;
  998. number = *rp++ - L'0';
  999. while (number <= (255 / 8) && *rp >= L'0' && *rp <= L'7')
  1000. {
  1001. number *= 8;
  1002. number += *rp++ - L'0';
  1003. }
  1004. cbuf[0] = (char) number;
  1005. cbuf[1] = '\0';
  1006. cbufptr = cbuf;
  1007. cbufin = 2;
  1008. wcbufptr = (char *) wcbuf;
  1009. wcbufin = sizeof (wcbuf);
  1010. /* Flush the state. */
  1011. iconv (cd, NULL, NULL, NULL, NULL);
  1012. iconv (cd, &cbufptr, &cbufin, &wcbufptr, &wcbufin);
  1013. if (cbufptr != &cbuf[2] || (wchar_t *) wcbufptr != &wcbuf[2])
  1014. error_at_line (0, 0, fname, line,
  1015. gettext ("invalid escape sequence"));
  1016. else
  1017. *wp++ = wcbuf[0];
  1018. }
  1019. break;
  1020. default:
  1021. if (*rp == escape_char)
  1022. {
  1023. *wp++ = escape_char;
  1024. ++rp;
  1025. }
  1026. else
  1027. /* Simply ignore the backslash character. */;
  1028. break;
  1029. }
  1030. }
  1031. else
  1032. *wp++ = *rp++;
  1033. /* If we saw a quote character at the beginning we expect another
  1034. one at the end. */
  1035. if (is_quoted && *rp != quote_char)
  1036. error_at_line (0, 0, fname, line, gettext ("unterminated message"));
  1037. /* Terminate string. */
  1038. *wp = L'\0';
  1039. return;
  1040. }
  1041. static void
  1042. read_old (struct catalog *catalog, const char *file_name)
  1043. {
  1044. struct catalog_info old_cat_obj;
  1045. struct set_list *set = NULL;
  1046. int last_set = -1;
  1047. size_t cnt;
  1048. /* Try to open catalog, but don't look through the NLSPATH. */
  1049. if (__open_catalog (file_name, NULL, NULL, &old_cat_obj) != 0)
  1050. {
  1051. if (errno == ENOENT)
  1052. /* No problem, the catalog simply does not exist. */
  1053. return;
  1054. else
  1055. error (EXIT_FAILURE, errno,
  1056. gettext ("while opening old catalog file"));
  1057. }
  1058. /* OK, we have the catalog loaded. Now read all messages and merge
  1059. them. When set and message number clash for any message the new
  1060. one is used. If the new one is empty it indicates that the
  1061. message should be deleted. */
  1062. for (cnt = 0; cnt < old_cat_obj.plane_size * old_cat_obj.plane_depth; ++cnt)
  1063. {
  1064. struct message_list *message, *last;
  1065. if (old_cat_obj.name_ptr[cnt * 3 + 0] == 0)
  1066. /* No message in this slot. */
  1067. continue;
  1068. if (old_cat_obj.name_ptr[cnt * 3 + 0] - 1 != (uint32_t) last_set)
  1069. {
  1070. last_set = old_cat_obj.name_ptr[cnt * 3 + 0] - 1;
  1071. set = find_set (catalog, old_cat_obj.name_ptr[cnt * 3 + 0] - 1);
  1072. }
  1073. last = NULL;
  1074. message = set->messages;
  1075. while (message != NULL)
  1076. {
  1077. if ((uint32_t) message->number >= old_cat_obj.name_ptr[cnt * 3 + 1])
  1078. break;
  1079. last = message;
  1080. message = message->next;
  1081. }
  1082. if (message == NULL
  1083. || (uint32_t) message->number > old_cat_obj.name_ptr[cnt * 3 + 1])
  1084. {
  1085. /* We have found a message which is not yet in the catalog.
  1086. Insert it at the right position. */
  1087. struct message_list *newp;
  1088. newp = (struct message_list *) xmalloc (sizeof(*newp));
  1089. newp->number = old_cat_obj.name_ptr[cnt * 3 + 1];
  1090. newp->message =
  1091. &old_cat_obj.strings[old_cat_obj.name_ptr[cnt * 3 + 2]];
  1092. newp->fname = NULL;
  1093. newp->line = 0;
  1094. newp->symbol = NULL;
  1095. newp->next = message;
  1096. if (last == NULL)
  1097. set->messages = newp;
  1098. else
  1099. last->next = newp;
  1100. ++catalog->total_messages;
  1101. }
  1102. else if (*message->message == '\0')
  1103. {
  1104. /* The new empty message has overridden the old one thus
  1105. "deleting" it as required. Now remove the empty remains. */
  1106. if (last == NULL)
  1107. set->messages = message->next;
  1108. else
  1109. last->next = message->next;
  1110. }
  1111. }
  1112. }
  1113. static int
  1114. open_conversion (const char *codeset, iconv_t *cd_towcp, iconv_t *cd_tombp,
  1115. wchar_t *escape_charp)
  1116. {
  1117. char buf[2];
  1118. char *bufptr;
  1119. size_t bufsize;
  1120. wchar_t wbuf[2];
  1121. char *wbufptr;
  1122. size_t wbufsize;
  1123. /* If the input file does not specify the codeset use the locale's. */
  1124. if (codeset == NULL)
  1125. {
  1126. setlocale (LC_ALL, "");
  1127. codeset = nl_langinfo (CODESET);
  1128. setlocale (LC_ALL, "C");
  1129. }
  1130. /* Get the conversion modules. */
  1131. *cd_towcp = iconv_open ("WCHAR_T", codeset);
  1132. *cd_tombp = iconv_open (codeset, "WCHAR_T");
  1133. if (*cd_towcp == (iconv_t) -1 || *cd_tombp == (iconv_t) -1)
  1134. {
  1135. error (0, 0, gettext ("conversion modules not available"));
  1136. if (*cd_towcp != (iconv_t) -1)
  1137. iconv_close (*cd_towcp);
  1138. return 1;
  1139. }
  1140. /* One special case for historical reasons is the backslash
  1141. character. In some codesets the byte value 0x5c is not mapped to
  1142. U005c in Unicode. These charsets then don't have a backslash
  1143. character at all. Therefore we have to live with whatever the
  1144. codeset provides and recognize, instead of the U005c, the character
  1145. the byte value 0x5c is mapped to. */
  1146. buf[0] = '\\';
  1147. buf[1] = '\0';
  1148. bufptr = buf;
  1149. bufsize = 2;
  1150. wbufptr = (char *) wbuf;
  1151. wbufsize = sizeof (wbuf);
  1152. iconv (*cd_towcp, &bufptr, &bufsize, &wbufptr, &wbufsize);
  1153. if (bufsize != 0 || wbufsize != 0)
  1154. {
  1155. /* Something went wrong, we couldn't convert the byte 0x5c. Go
  1156. on with using U005c. */
  1157. error (0, 0, gettext ("cannot determine escape character"));
  1158. *escape_charp = L'\\';
  1159. }
  1160. else
  1161. *escape_charp = wbuf[0];
  1162. return 0;
  1163. }