gencat.c 35 KB

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