No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / src / x-ycp.c
blob6ab49a6d9929950d6e66d5c66591af92e501dedf
1 /* xgettext YCP backend.
2 Copyright (C) 2001-2003 Free Software Foundation, Inc.
4 This file was written by Bruno Haible <haible@clisp.cons.org>, 2001.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "message.h"
32 #include "xgettext.h"
33 #include "x-ycp.h"
34 #include "error.h"
35 #include "xalloc.h"
36 #include "exit.h"
37 #include "gettext.h"
39 #define _(s) gettext(s)
41 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
44 /* The YCP syntax is defined in libycp/doc/syntax.html.
45 See also libycp/src/scanner.ll. */
48 void
49 init_flag_table_ycp ()
51 xgettext_record_flag ("sformat:1:ycp-format");
52 xgettext_record_flag ("y2debug:1:ycp-format");
53 xgettext_record_flag ("y2milestone:1:ycp-format");
54 xgettext_record_flag ("y2warning:1:ycp-format");
55 xgettext_record_flag ("y2error:1:ycp-format");
56 xgettext_record_flag ("y2security:1:ycp-format");
57 xgettext_record_flag ("y2internal:1:ycp-format");
61 /* ======================== Reading of characters. ======================== */
64 /* Real filename, used in error messages about the input file. */
65 static const char *real_file_name;
67 /* Logical filename and line number, used to label the extracted messages. */
68 static char *logical_file_name;
69 static int line_number;
70 static int char_in_line;
72 /* The input file stream. */
73 static FILE *fp;
75 /* These are for tracking whether comments count as immediately before
76 keyword. */
77 static int last_comment_line;
78 static int last_non_comment_line;
81 /* 1. line_number handling. */
83 static int
84 phase1_getc ()
86 int c = getc (fp);
88 if (c == EOF)
90 if (ferror (fp))
91 error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
92 real_file_name);
93 return EOF;
96 if (c == '\n')
98 line_number++;
99 char_in_line = 0;
101 else
102 char_in_line++;
104 return c;
107 /* Supports only one pushback character. */
108 static void
109 phase1_ungetc (int c)
111 if (c != EOF)
113 if (c == '\n')
115 --line_number;
116 char_in_line = INT_MAX;
118 else
119 --char_in_line;
121 ungetc (c, fp);
126 /* 2. Replace each comment that is not inside a character constant or
127 string literal with a space character. We need to remember the
128 comment for later, because it may be attached to a keyword string.
129 YCP comments can be in C comment syntax, C++ comment syntax or sh
130 comment syntax. */
132 static unsigned char phase2_pushback[1];
133 static int phase2_pushback_length;
135 static int
136 phase2_getc ()
138 static char *buffer;
139 static size_t bufmax;
140 size_t buflen;
141 int lineno;
142 int c;
143 bool last_was_star;
145 if (phase2_pushback_length)
146 return phase2_pushback[--phase2_pushback_length];
148 if (char_in_line == 0)
150 /* Eat whitespace, to recognize ^[\t ]*# pattern. */
152 c = phase1_getc ();
153 while (c == '\t' || c == ' ');
155 if (c == '#')
157 /* sh comment. */
158 buflen = 0;
159 lineno = line_number;
160 for (;;)
162 c = phase1_getc ();
163 if (c == '\n' || c == EOF)
164 break;
165 /* We skip all leading white space, but not EOLs. */
166 if (!(buflen == 0 && (c == ' ' || c == '\t')))
168 if (buflen >= bufmax)
170 bufmax = 2 * bufmax + 10;
171 buffer = xrealloc (buffer, bufmax);
173 buffer[buflen++] = c;
176 if (buflen >= bufmax)
178 bufmax = 2 * bufmax + 10;
179 buffer = xrealloc (buffer, bufmax);
181 buffer[buflen] = '\0';
182 xgettext_comment_add (buffer);
183 last_comment_line = lineno;
184 return '\n';
187 else
188 c = phase1_getc ();
190 if (c == '/')
192 c = phase1_getc ();
194 switch (c)
196 default:
197 phase1_ungetc (c);
198 return '/';
200 case '*':
201 /* C comment. */
202 buflen = 0;
203 lineno = line_number;
204 last_was_star = false;
205 for (;;)
207 c = phase1_getc ();
208 if (c == EOF)
209 break;
210 /* We skip all leading white space, but not EOLs. */
211 if (buflen == 0 && (c == ' ' || c == '\t'))
212 continue;
213 if (buflen >= bufmax)
215 bufmax = 2 * bufmax + 10;
216 buffer = xrealloc (buffer, bufmax);
218 buffer[buflen++] = c;
219 switch (c)
221 case '\n':
222 --buflen;
223 while (buflen >= 1
224 && (buffer[buflen - 1] == ' '
225 || buffer[buflen - 1] == '\t'))
226 --buflen;
227 buffer[buflen] = '\0';
228 xgettext_comment_add (buffer);
229 buflen = 0;
230 lineno = line_number;
231 last_was_star = false;
232 continue;
234 case '*':
235 last_was_star = true;
236 continue;
238 case '/':
239 if (last_was_star)
241 buflen -= 2;
242 while (buflen >= 1
243 && (buffer[buflen - 1] == ' '
244 || buffer[buflen - 1] == '\t'))
245 --buflen;
246 buffer[buflen] = '\0';
247 xgettext_comment_add (buffer);
248 break;
250 /* FALLTHROUGH */
252 default:
253 last_was_star = false;
254 continue;
256 break;
258 last_comment_line = lineno;
259 return ' ';
261 case '/':
262 /* C++ comment. */
263 buflen = 0;
264 lineno = line_number;
265 for (;;)
267 c = phase1_getc ();
268 if (c == '\n' || c == EOF)
269 break;
270 /* We skip all leading white space, but not EOLs. */
271 if (!(buflen == 0 && (c == ' ' || c == '\t')))
273 if (buflen >= bufmax)
275 bufmax = 2 * bufmax + 10;
276 buffer = xrealloc (buffer, bufmax);
278 buffer[buflen++] = c;
281 if (buflen >= bufmax)
283 bufmax = 2 * bufmax + 10;
284 buffer = xrealloc (buffer, bufmax);
286 buffer[buflen] = '\0';
287 xgettext_comment_add (buffer);
288 last_comment_line = lineno;
289 return '\n';
292 else
293 return c;
296 /* Supports only one pushback character. */
297 static void
298 phase2_ungetc (int c)
300 if (c != EOF)
302 if (phase2_pushback_length == SIZEOF (phase2_pushback))
303 abort ();
304 phase2_pushback[phase2_pushback_length++] = c;
309 /* ========================== Reading of tokens. ========================== */
312 enum token_type_ty
314 token_type_eof,
315 token_type_lparen, /* ( */
316 token_type_rparen, /* ) */
317 token_type_comma, /* , */
318 token_type_i18n, /* _( */
319 token_type_string_literal, /* "abc" */
320 token_type_symbol, /* symbol, number */
321 token_type_other /* misc. operator */
323 typedef enum token_type_ty token_type_ty;
325 typedef struct token_ty token_ty;
326 struct token_ty
328 token_type_ty type;
329 char *string; /* for token_type_string_literal, token_type_symbol */
330 int line_number;
334 /* 7. Replace escape sequences within character strings with their
335 single character equivalents. */
337 #define P7_QUOTES (1000 + '"')
339 static int
340 phase7_getc ()
342 int c;
344 for (;;)
346 /* Use phase 1, because phase 2 elides comments. */
347 c = phase1_getc ();
349 if (c == '"')
350 return P7_QUOTES;
351 if (c != '\\')
352 return c;
353 c = phase1_getc ();
354 if (c != '\n')
355 switch (c)
357 case 'b':
358 return '\b';
359 case 'f':
360 return '\f';
361 case 'n':
362 return '\n';
363 case 'r':
364 return '\r';
365 case 't':
366 return '\t';
368 /* FIXME: What is the octal escape syntax?
369 syntax.html says: [0] [0-7]+
370 scanner.ll says: [0-7] [0-7] [0-7]
372 #if 0
373 case '0': case '1': case '2': case '3':
374 case '4': case '5': case '6': case '7':
376 int n, j;
378 n = 0;
379 for (j = 0; j < 3; ++j)
381 n = n * 8 + c - '0';
382 c = phase1_getc ();
383 switch (c)
385 default:
386 break;
388 case '0': case '1': case '2': case '3':
389 case '4': case '5': case '6': case '7':
390 continue;
392 break;
394 phase1_ungetc (c);
395 return n;
397 #endif
399 default:
400 return c;
406 /* Combine characters into tokens. Discard whitespace. */
408 static void
409 x_ycp_lex (token_ty *tp)
411 static char *buffer;
412 static int bufmax;
413 int bufpos;
414 int c;
416 for (;;)
418 tp->line_number = line_number;
419 c = phase2_getc ();
421 switch (c)
423 case EOF:
424 tp->type = token_type_eof;
425 return;
427 case '\n':
428 if (last_non_comment_line > last_comment_line)
429 xgettext_comment_reset ();
430 /* FALLTHROUGH */
431 case '\r':
432 case '\t':
433 case ' ':
434 /* Ignore whitespace and comments. */
435 continue;
438 last_non_comment_line = tp->line_number;
440 switch (c)
442 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
443 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
444 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
445 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
446 case 'Y': case 'Z':
447 case '_':
448 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
449 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
450 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
451 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
452 case 'y': case 'z':
453 case '0': case '1': case '2': case '3': case '4':
454 case '5': case '6': case '7': case '8': case '9':
455 /* Symbol, or part of a number. */
456 bufpos = 0;
457 for (;;)
459 if (bufpos >= bufmax)
461 bufmax = 2 * bufmax + 10;
462 buffer = xrealloc (buffer, bufmax);
464 buffer[bufpos++] = c;
465 c = phase2_getc ();
466 switch (c)
468 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
469 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
470 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
471 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
472 case 'Y': case 'Z':
473 case '_':
474 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
475 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
476 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
477 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
478 case 'y': case 'z':
479 case '0': case '1': case '2': case '3': case '4':
480 case '5': case '6': case '7': case '8': case '9':
481 continue;
482 default:
483 if (bufpos == 1 && buffer[0] == '_' && c == '(')
485 tp->type = token_type_i18n;
486 return;
488 phase2_ungetc (c);
489 break;
491 break;
493 if (bufpos >= bufmax)
495 bufmax = 2 * bufmax + 10;
496 buffer = xrealloc (buffer, bufmax);
498 buffer[bufpos] = '\0';
499 tp->string = xstrdup (buffer);
500 tp->type = token_type_symbol;
501 return;
503 case '"':
504 bufpos = 0;
505 for (;;)
507 c = phase7_getc ();
508 if (c == EOF || c == P7_QUOTES)
509 break;
510 if (bufpos >= bufmax)
512 bufmax = 2 * bufmax + 10;
513 buffer = xrealloc (buffer, bufmax);
515 buffer[bufpos++] = c;
517 if (bufpos >= bufmax)
519 bufmax = 2 * bufmax + 10;
520 buffer = xrealloc (buffer, bufmax);
522 buffer[bufpos] = '\0';
523 tp->string = xstrdup (buffer);
524 tp->type = token_type_string_literal;
525 return;
527 case '(':
528 tp->type = token_type_lparen;
529 return;
531 case ')':
532 tp->type = token_type_rparen;
533 return;
535 case ',':
536 tp->type = token_type_comma;
537 return;
539 default:
540 /* We could carefully recognize each of the 2 and 3 character
541 operators, but it is not necessary, as we only need to recognize
542 gettext invocations. Don't bother. */
543 tp->type = token_type_other;
544 return;
550 /* ========================= Extracting strings. ========================== */
553 /* Context lookup table. */
554 static flag_context_list_table_ty *flag_context_list_table;
557 /* The file is broken into tokens.
559 Normal handling: Look for
560 [A] _( [B] msgid ... )
561 Plural handling: Look for
562 [A] _( [B] msgid [C] , [D] msgid_plural ... )
563 At point [A]: state == 0.
564 At point [B]: state == 1, plural_mp == NULL.
565 At point [C]: state == 2, plural_mp != NULL.
566 At point [D]: state == 1, plural_mp != NULL.
568 We use recursion because we have to set the context according to the given
569 flags. */
572 /* Extract messages until the next balanced closing parenthesis.
573 Extracted messages are added to MLP.
574 Return true upon eof, false upon closing parenthesis. */
575 static bool
576 extract_parenthesized (message_list_ty *mlp,
577 flag_context_ty outer_context,
578 flag_context_list_iterator_ty context_iter,
579 bool in_i18n)
581 int state; /* 1 or 2 inside _( ... ), otherwise 0 */
582 message_ty *plural_mp = NULL; /* defined only when in states 1 and 2 */
583 /* Context iterator that will be used if the next token is a '('. */
584 flag_context_list_iterator_ty next_context_iter =
585 passthrough_context_list_iterator;
586 /* Current context. */
587 flag_context_ty inner_context =
588 inherited_context (outer_context,
589 flag_context_list_iterator_advance (&context_iter));
591 /* Start state is 0 or 1. */
592 state = (in_i18n ? 1 : 0);
594 for (;;)
596 token_ty token;
598 x_ycp_lex (&token);
599 switch (token.type)
601 case token_type_i18n:
602 if (extract_parenthesized (mlp, inner_context, next_context_iter,
603 true))
604 return true;
605 next_context_iter = null_context_list_iterator;
606 state = 0;
607 continue;
609 case token_type_string_literal:
610 if (state == 1)
612 lex_pos_ty pos;
613 pos.file_name = logical_file_name;
614 pos.line_number = token.line_number;
616 if (plural_mp == NULL)
618 /* Seen an msgid. */
619 plural_mp = remember_a_message (mlp, token.string,
620 inner_context, &pos);
621 state = 2;
623 else
625 /* Seen an msgid_plural. */
626 remember_a_message_plural (plural_mp, token.string,
627 inner_context, &pos);
628 state = 0;
631 else
633 free (token.string);
634 state = 0;
636 next_context_iter = null_context_list_iterator;
637 continue;
639 case token_type_symbol:
640 next_context_iter =
641 flag_context_list_iterator (
642 flag_context_list_table_lookup (
643 flag_context_list_table,
644 token.string, strlen (token.string)));
645 free (token.string);
646 state = 0;
647 continue;
649 case token_type_lparen:
650 if (extract_parenthesized (mlp, inner_context, next_context_iter,
651 false))
652 return true;
653 next_context_iter = null_context_list_iterator;
654 state = 0;
655 continue;
657 case token_type_rparen:
658 return false;
660 case token_type_comma:
661 if (state == 2)
662 state = 1;
663 else
664 state = 0;
665 inner_context =
666 inherited_context (outer_context,
667 flag_context_list_iterator_advance (
668 &context_iter));
669 next_context_iter = passthrough_context_list_iterator;
670 continue;
672 case token_type_other:
673 next_context_iter = null_context_list_iterator;
674 state = 0;
675 continue;
677 case token_type_eof:
678 return true;
680 default:
681 abort ();
687 void
688 extract_ycp (FILE *f,
689 const char *real_filename, const char *logical_filename,
690 flag_context_list_table_ty *flag_table,
691 msgdomain_list_ty *mdlp)
693 message_list_ty *mlp = mdlp->item[0]->messages;
695 fp = f;
696 real_file_name = real_filename;
697 logical_file_name = xstrdup (logical_filename);
698 line_number = 1;
699 char_in_line = 0;
701 last_comment_line = -1;
702 last_non_comment_line = -1;
704 flag_context_list_table = flag_table;
706 /* Eat tokens until eof is seen. When extract_parenthesized returns
707 due to an unbalanced closing parenthesis, just restart it. */
708 while (!extract_parenthesized (mlp, null_context, null_context_list_iterator,
709 false))
712 fp = NULL;
713 real_file_name = NULL;
714 logical_file_name = NULL;
715 line_number = 0;
716 char_in_line = 0;