1 /* xgettext YCP backend.
2 Copyright (C) 2001-2003, 2005-2009, 2011, 2018-2023 Free Software Foundation, Inc.
3
4 This file was written by Bruno Haible <haible@clisp.cons.org>, 2001.
5
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 3 of the License, or
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 /* Specification. */
24 #include "x-ycp.h"
25
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "attribute.h"
33 #include "message.h"
34 #include "rc-str-list.h"
35 #include "xgettext.h"
36 #include "xg-pos.h"
37 #include "xg-arglist-context.h"
38 #include "xg-message.h"
39 #include "error.h"
40 #include "error-progname.h"
41 #include "xalloc.h"
42 #include "gettext.h"
43
44 #define _(s) gettext(s)
45
46 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
47
48
49 /* The YCP syntax is defined in libycp/doc/syntax.html.
50 See also libycp/src/scanner.ll.
51 Both are part of the yast2-core package in SuSE Linux distributions. */
52
53
54 void
55 init_flag_table_ycp ()
56 {
57 xgettext_record_flag ("sformat:1:ycp-format");
58 xgettext_record_flag ("y2debug:1:ycp-format");
59 xgettext_record_flag ("y2milestone:1:ycp-format");
60 xgettext_record_flag ("y2warning:1:ycp-format");
61 xgettext_record_flag ("y2error:1:ycp-format");
62 xgettext_record_flag ("y2security:1:ycp-format");
63 xgettext_record_flag ("y2internal:1:ycp-format");
64 }
65
66
67 /* ======================== Reading of characters. ======================== */
68
69 /* Position in the current line. */
70 static int char_in_line;
71
72 /* The input file stream. */
73 static FILE *fp;
74
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;
79
80
81 /* 1. line_number handling. */
82
83 static int
84 phase1_getc ()
85 {
86 int c = getc (fp);
87
88 if (c == EOF)
89 {
90 if (ferror (fp))
91 error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
92 real_file_name);
93 return EOF;
94 }
95
96 if (c == '\n')
97 {
98 line_number++;
99 char_in_line = 0;
100 }
101 else
102 char_in_line++;
103
104 return c;
105 }
106
107 /* Supports only one pushback character. */
108 static void
109 phase1_ungetc (int c)
110 {
111 if (c != EOF)
112 {
113 if (c == '\n')
114 {
115 --line_number;
116 char_in_line = INT_MAX;
117 }
118 else
119 --char_in_line;
120
121 ungetc (c, fp);
122 }
123 }
124
125
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. */
131
132 static unsigned char phase2_pushback[1];
133 static int phase2_pushback_length;
134
135 static int
136 phase2_getc ()
137 {
138 static char *buffer;
139 static size_t bufmax;
140 size_t buflen;
141 int lineno;
142 int c;
143 bool last_was_star;
144
145 if (phase2_pushback_length)
146 return phase2_pushback[--phase2_pushback_length];
147
148 if (char_in_line == 0)
149 {
150 /* Eat whitespace, to recognize ^[\t ]*# pattern. */
151 do
152 c = phase1_getc ();
153 while (c == '\t' || c == ' ');
154
155 if (c == '#')
156 {
157 /* sh comment. */
158 buflen = 0;
159 lineno = line_number;
160 for (;;)
161 {
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')))
167 {
168 if (buflen >= bufmax)
169 {
170 bufmax = 2 * bufmax + 10;
171 buffer = xrealloc (buffer, bufmax);
172 }
173 buffer[buflen++] = c;
174 }
175 }
176 if (buflen >= bufmax)
177 {
178 bufmax = 2 * bufmax + 10;
179 buffer = xrealloc (buffer, bufmax);
180 }
181 buffer[buflen] = '\0';
182 savable_comment_add (buffer);
183 last_comment_line = lineno;
184 return '\n';
185 }
186 }
187 else
188 c = phase1_getc ();
189
190 if (c == '/')
191 {
192 c = phase1_getc ();
193
194 switch (c)
195 {
196 default:
197 phase1_ungetc (c);
198 return '/';
199
200 case '*':
201 /* C comment. */
202 buflen = 0;
203 lineno = line_number;
204 last_was_star = false;
205 for (;;)
206 {
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)
214 {
215 bufmax = 2 * bufmax + 10;
216 buffer = xrealloc (buffer, bufmax);
217 }
218 buffer[buflen++] = c;
219 switch (c)
220 {
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 savable_comment_add (buffer);
229 buflen = 0;
230 lineno = line_number;
231 last_was_star = false;
232 continue;
233
234 case '*':
235 last_was_star = true;
236 continue;
237
238 case '/':
239 if (last_was_star)
240 {
241 buflen -= 2;
242 while (buflen >= 1
243 && (buffer[buflen - 1] == ' '
244 || buffer[buflen - 1] == '\t'))
245 --buflen;
246 buffer[buflen] = '\0';
247 savable_comment_add (buffer);
248 break;
249 }
250 FALLTHROUGH;
251
252 default:
253 last_was_star = false;
254 continue;
255 }
256 break;
257 }
258 last_comment_line = lineno;
259 return ' ';
260
261 case '/':
262 /* C++ comment. */
263 buflen = 0;
264 lineno = line_number;
265 for (;;)
266 {
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')))
272 {
273 if (buflen >= bufmax)
274 {
275 bufmax = 2 * bufmax + 10;
276 buffer = xrealloc (buffer, bufmax);
277 }
278 buffer[buflen++] = c;
279 }
280 }
281 if (buflen >= bufmax)
282 {
283 bufmax = 2 * bufmax + 10;
284 buffer = xrealloc (buffer, bufmax);
285 }
286 buffer[buflen] = '\0';
287 savable_comment_add (buffer);
288 last_comment_line = lineno;
289 return '\n';
290 }
291 }
292 else
293 return c;
294 }
295
296 /* Supports only one pushback character. */
297 static void
298 phase2_ungetc (int c)
299 {
300 if (c != EOF)
301 {
302 if (phase2_pushback_length == SIZEOF (phase2_pushback))
303 abort ();
304 phase2_pushback[phase2_pushback_length++] = c;
305 }
306 }
307
308
309 /* ========================== Reading of tokens. ========================== */
310
311
312 enum token_type_ty
313 {
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 */
322 };
323 typedef enum token_type_ty token_type_ty;
324
325 typedef struct token_ty token_ty;
326 struct token_ty
327 {
328 token_type_ty type;
329 char *string; /* for token_type_string_literal, token_type_symbol */
330 refcounted_string_list_ty *comment; /* for token_type_string_literal */
331 int line_number;
332 };
333
334
335 /* 7. Replace escape sequences within character strings with their
336 single character equivalents. */
337
338 #define P7_QUOTES (1000 + '"')
339
340 static int
341 phase7_getc ()
342 {
343 int c;
344
345 for (;;)
346 {
347 /* Use phase 1, because phase 2 elides comments. */
348 c = phase1_getc ();
349
350 if (c == '"')
351 return P7_QUOTES;
352 if (c != '\\')
353 return c;
354 c = phase1_getc ();
355 if (c != '\n')
356 switch (c)
357 {
358 case 'b':
359 return '\b';
360 case 'f':
361 return '\f';
362 case 'n':
363 return '\n';
364 case 'r':
365 return '\r';
366 case 't':
367 return '\t';
368
369 /* FIXME: What is the octal escape syntax?
370 syntax.html says: [0] [0-7]+
371 scanner.ll says: [0-7] [0-7] [0-7]
372 */
373 #if 0
374 case '0': case '1': case '2': case '3':
375 case '4': case '5': case '6': case '7':
376 {
377 int n, j;
378
379 n = 0;
380 for (j = 0; j < 3; ++j)
381 {
382 n = n * 8 + c - '0';
383 c = phase1_getc ();
384 switch (c)
385 {
386 default:
387 break;
388
389 case '0': case '1': case '2': case '3':
390 case '4': case '5': case '6': case '7':
391 continue;
392 }
393 break;
394 }
395 phase1_ungetc (c);
396 return n;
397 }
398 #endif
399
400 default:
401 return c;
402 }
403 }
404 }
405
406
407 /* Free the memory pointed to by a 'struct token_ty'. */
408 static inline void
409 free_token (token_ty *tp)
410 {
411 if (tp->type == token_type_string_literal || tp->type == token_type_symbol)
412 free (tp->string);
413 if (tp->type == token_type_string_literal)
414 drop_reference (tp->comment);
415 }
416
417
418 /* Combine characters into tokens. Discard whitespace. */
419
420 static token_ty phase5_pushback[1];
421 static int phase5_pushback_length;
422
423 static void
424 phase5_get (token_ty *tp)
425 {
426 static char *buffer;
427 static int bufmax;
428 int bufpos;
429 int c;
430
431 if (phase5_pushback_length)
432 {
433 *tp = phase5_pushback[--phase5_pushback_length];
434 return;
435 }
436 for (;;)
437 {
438 tp->line_number = line_number;
439 c = phase2_getc ();
440
441 switch (c)
442 {
443 case EOF:
444 tp->type = token_type_eof;
445 return;
446
447 case '\n':
448 if (last_non_comment_line > last_comment_line)
449 savable_comment_reset ();
450 FALLTHROUGH;
451 case '\r':
452 case '\t':
453 case ' ':
454 /* Ignore whitespace and comments. */
455 continue;
456 }
457
458 last_non_comment_line = tp->line_number;
459
460 switch (c)
461 {
462 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
463 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
464 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
465 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
466 case 'Y': case 'Z':
467 case '_':
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 '0': case '1': case '2': case '3': case '4':
474 case '5': case '6': case '7': case '8': case '9':
475 /* Symbol, or part of a number. */
476 bufpos = 0;
477 for (;;)
478 {
479 if (bufpos >= bufmax)
480 {
481 bufmax = 2 * bufmax + 10;
482 buffer = xrealloc (buffer, bufmax);
483 }
484 buffer[bufpos++] = c;
485 c = phase2_getc ();
486 switch (c)
487 {
488 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
489 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
490 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
491 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
492 case 'Y': case 'Z':
493 case '_':
494 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
495 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
496 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
497 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
498 case 'y': case 'z':
499 case '0': case '1': case '2': case '3': case '4':
500 case '5': case '6': case '7': case '8': case '9':
501 continue;
502 default:
503 if (bufpos == 1 && buffer[0] == '_' && c == '(')
504 {
505 tp->type = token_type_i18n;
506 return;
507 }
508 phase2_ungetc (c);
509 break;
510 }
511 break;
512 }
513 if (bufpos >= bufmax)
514 {
515 bufmax = 2 * bufmax + 10;
516 buffer = xrealloc (buffer, bufmax);
517 }
518 buffer[bufpos] = '\0';
519 tp->string = xstrdup (buffer);
520 tp->type = token_type_symbol;
521 return;
522
523 case '"':
524 bufpos = 0;
525 for (;;)
526 {
527 c = phase7_getc ();
528 if (c == EOF || c == P7_QUOTES)
529 break;
530 if (bufpos >= bufmax)
531 {
532 bufmax = 2 * bufmax + 10;
533 buffer = xrealloc (buffer, bufmax);
534 }
535 buffer[bufpos++] = c;
536 }
537 if (bufpos >= bufmax)
538 {
539 bufmax = 2 * bufmax + 10;
540 buffer = xrealloc (buffer, bufmax);
541 }
542 buffer[bufpos] = '\0';
543 tp->string = xstrdup (buffer);
544 tp->type = token_type_string_literal;
545 tp->comment = add_reference (savable_comment);
546 return;
547
548 case '(':
549 tp->type = token_type_lparen;
550 return;
551
552 case ')':
553 tp->type = token_type_rparen;
554 return;
555
556 case ',':
557 tp->type = token_type_comma;
558 return;
559
560 default:
561 /* We could carefully recognize each of the 2 and 3 character
562 operators, but it is not necessary, as we only need to recognize
563 gettext invocations. Don't bother. */
564 tp->type = token_type_other;
565 return;
566 }
567 }
568 }
569
570 /* Supports only one pushback token. */
571 static void
572 phase5_unget (token_ty *tp)
573 {
574 if (tp->type != token_type_eof)
575 {
576 if (phase5_pushback_length == SIZEOF (phase5_pushback))
577 abort ();
578 phase5_pushback[phase5_pushback_length++] = *tp;
579 }
580 }
581
582
583 /* Concatenate adjacent string literals to form single string literals.
584 (See libycp/src/parser.yy, rule 'string' vs. terminal 'STRING'.) */
585
586 static token_ty phase8_pushback[1];
587 static int phase8_pushback_length;
588
589 static void
590 phase8_get (token_ty *tp)
591 {
592 if (phase8_pushback_length)
593 {
594 *tp = phase8_pushback[--phase8_pushback_length];
595 return;
596 }
597 phase5_get (tp);
598 if (tp->type != token_type_string_literal)
599 return;
600 for (;;)
601 {
602 token_ty tmp;
603 size_t len;
604
605 phase5_get (&tmp);
606 if (tmp.type != token_type_string_literal)
607 {
608 phase5_unget (&tmp);
609 return;
610 }
611 len = strlen (tp->string);
612 tp->string = xrealloc (tp->string, len + strlen (tmp.string) + 1);
613 strcpy (tp->string + len, tmp.string);
614 free_token (&tmp);
615 }
616 }
617
618 /* Supports only one pushback token. */
619 static void
620 phase8_unget (token_ty *tp)
621 {
622 if (tp->type != token_type_eof)
623 {
624 if (phase8_pushback_length == SIZEOF (phase8_pushback))
625 abort ();
626 phase8_pushback[phase8_pushback_length++] = *tp;
627 }
628 }
629
630
631 /* ========================= Extracting strings. ========================== */
632
633
634 /* Context lookup table. */
635 static flag_context_list_table_ty *flag_context_list_table;
636
637
638 /* Maximum supported nesting depth. */
639 #define MAX_NESTING_DEPTH 1000
640
641 /* Current nesting depth. */
642 static int nesting_depth;
643
644
645 /* The file is broken into tokens.
646
647 Normal handling: Look for
648 [A] _( [B] msgid ... )
649 Plural handling: Look for
650 [A] _( [B] msgid [C] , [D] msgid_plural ... )
651 At point [A]: state == 0.
652 At point [B]: state == 1, plural_mp == NULL.
653 At point [C]: state == 2, plural_mp != NULL.
654 At point [D]: state == 1, plural_mp != NULL.
655
656 We use recursion because we have to set the context according to the given
657 flags. */
658
659
660 /* Extract messages until the next balanced closing parenthesis.
661 Extracted messages are added to MLP.
662 Return true upon eof, false upon closing parenthesis. */
663 static bool
664 extract_parenthesized (message_list_ty *mlp,
665 flag_context_ty outer_context,
666 flag_context_list_iterator_ty context_iter,
667 bool in_i18n)
668 {
669 int state; /* 1 or 2 inside _( ... ), otherwise 0 */
670 int plural_state = 0; /* defined only when in states 1 and 2 */
671 message_ty *plural_mp = NULL; /* defined only when in states 1 and 2 */
672 /* Context iterator that will be used if the next token is a '('. */
673 flag_context_list_iterator_ty next_context_iter =
674 passthrough_context_list_iterator;
675 /* Current context. */
676 flag_context_ty inner_context =
677 inherited_context (outer_context,
678 flag_context_list_iterator_advance (&context_iter));
679
680 /* Start state is 0 or 1. */
681 state = (in_i18n ? 1 : 0);
682
683 for (;;)
684 {
685 token_ty token;
686
687 if (in_i18n)
688 phase8_get (&token);
689 else
690 phase5_get (&token);
691
692 switch (token.type)
693 {
694 case token_type_i18n:
695 if (++nesting_depth > MAX_NESTING_DEPTH)
696 {
697 error_with_progname = false;
698 error (EXIT_FAILURE, 0, _("%s:%d: error: too many open parentheses"),
699 logical_file_name, line_number);
700 }
701 if (extract_parenthesized (mlp, inner_context, next_context_iter,
702 true))
703 return true;
704 nesting_depth--;
705 next_context_iter = null_context_list_iterator;
706 state = 0;
707 continue;
708
709 case token_type_string_literal:
710 if (state == 1)
711 {
712 lex_pos_ty pos;
713 pos.file_name = logical_file_name;
714 pos.line_number = token.line_number;
715
716 if (plural_state == 0)
717 {
718 /* Seen an msgid. */
719 token_ty token2;
720
721 if (in_i18n)
722 phase8_get (&token2);
723 else
724 phase5_get (&token2);
725
726 plural_mp =
727 remember_a_message (mlp, NULL, token.string, false,
728 token2.type == token_type_comma,
729 inner_context, &pos,
730 NULL, token.comment, false);
731
732 if (in_i18n)
733 phase8_unget (&token2);
734 else
735 phase5_unget (&token2);
736
737 plural_state = 1;
738 state = 2;
739 }
740 else
741 {
742 /* Seen an msgid_plural. */
743 if (plural_mp != NULL)
744 remember_a_message_plural (plural_mp, token.string, false,
745 inner_context, &pos,
746 token.comment, false);
747 state = 0;
748 }
749 drop_reference (token.comment);
750 }
751 else
752 {
753 free_token (&token);
754 state = 0;
755 }
756 next_context_iter = null_context_list_iterator;
757 continue;
758
759 case token_type_symbol:
760 next_context_iter =
761 flag_context_list_iterator (
762 flag_context_list_table_lookup (
763 flag_context_list_table,
764 token.string, strlen (token.string)));
765 free_token (&token);
766 state = 0;
767 continue;
768
769 case token_type_lparen:
770 if (++nesting_depth > MAX_NESTING_DEPTH)
771 {
772 error_with_progname = false;
773 error (EXIT_FAILURE, 0, _("%s:%d: error: too many open parentheses"),
774 logical_file_name, line_number);
775 }
776 if (extract_parenthesized (mlp, inner_context, next_context_iter,
777 false))
778 return true;
779 nesting_depth--;
780 next_context_iter = null_context_list_iterator;
781 state = 0;
782 continue;
783
784 case token_type_rparen:
785 return false;
786
787 case token_type_comma:
788 if (state == 2)
789 state = 1;
790 else
791 state = 0;
792 inner_context =
793 inherited_context (outer_context,
794 flag_context_list_iterator_advance (
795 &context_iter));
796 next_context_iter = passthrough_context_list_iterator;
797 continue;
798
799 case token_type_other:
800 next_context_iter = null_context_list_iterator;
801 state = 0;
802 continue;
803
804 case token_type_eof:
805 return true;
806
807 default:
808 abort ();
809 }
810 }
811 }
812
813
814 void
815 extract_ycp (FILE *f,
816 const char *real_filename, const char *logical_filename,
817 flag_context_list_table_ty *flag_table,
818 msgdomain_list_ty *mdlp)
819 {
820 message_list_ty *mlp = mdlp->item[0]->messages;
821
822 fp = f;
823 real_file_name = real_filename;
824 logical_file_name = xstrdup (logical_filename);
825 line_number = 1;
826 char_in_line = 0;
827
828 last_comment_line = -1;
829 last_non_comment_line = -1;
830
831 phase2_pushback_length = 0;
832 phase5_pushback_length = 0;
833 phase8_pushback_length = 0;
834
835 flag_context_list_table = flag_table;
836 nesting_depth = 0;
837
838 /* Eat tokens until eof is seen. When extract_parenthesized returns
839 due to an unbalanced closing parenthesis, just restart it. */
840 while (!extract_parenthesized (mlp, null_context, null_context_list_iterator,
841 false))
842 ;
843
844 fp = NULL;
845 real_file_name = NULL;
846 logical_file_name = NULL;
847 line_number = 0;
848 char_in_line = 0;
849 }