summaryrefslogtreecommitdiff
path: root/gettext-tools/src/read-catalog-abstract.c
blob: eaa624b81f2d670ba705091d1beef089e4f5a3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/* Reading PO files, abstract class.
   Copyright (C) 1995-1996, 1998, 2000-2009, 2015 Free Software
   Foundation, Inc.

   This file was written by Peter Miller <millerp@canb.auug.org.au>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/* Specification.  */
#include "read-catalog-abstract.h"

#include <limits.h>
#include <stdlib.h>
#include <string.h>

#include "xalloc.h"
#include "xvasprintf.h"
#include "po-xerror.h"
#include "error.h"
#include "gettext.h"

/* Local variables.  */
static abstract_catalog_reader_ty *callback_arg;


/* ========================================================================= */
/* Allocating and freeing instances of abstract_catalog_reader_ty.  */


abstract_catalog_reader_ty *
catalog_reader_alloc (abstract_catalog_reader_class_ty *method_table)
{
  abstract_catalog_reader_ty *pop;

  pop = (abstract_catalog_reader_ty *) xmalloc (method_table->size);
  pop->methods = method_table;
  if (method_table->constructor)
    method_table->constructor (pop);
  return pop;
}


void
catalog_reader_free (abstract_catalog_reader_ty *pop)
{
  if (pop->methods->destructor)
    pop->methods->destructor (pop);
  free (pop);
}


/* ========================================================================= */
/* Inline functions to invoke the methods.  */


static inline void
call_parse_brief (abstract_catalog_reader_ty *pop)
{
  if (pop->methods->parse_brief)
    pop->methods->parse_brief (pop);
}

static inline void
call_parse_debrief (abstract_catalog_reader_ty *pop)
{
  if (pop->methods->parse_debrief)
    pop->methods->parse_debrief (pop);
}

static inline void
call_directive_domain (abstract_catalog_reader_ty *pop, char *name)
{
  if (pop->methods->directive_domain)
    pop->methods->directive_domain (pop, name);
}

static inline void
call_directive_message (abstract_catalog_reader_ty *pop,
                        char *msgctxt,
                        char *msgid,
                        lex_pos_ty *msgid_pos,
                        char *msgid_plural,
                        char *msgstr, size_t msgstr_len,
                        lex_pos_ty *msgstr_pos,
                        char *prev_msgctxt,
                        char *prev_msgid,
                        char *prev_msgid_plural,
                        bool force_fuzzy, bool obsolete)
{
  if (pop->methods->directive_message)
    pop->methods->directive_message (pop, msgctxt,
                                     msgid, msgid_pos, msgid_plural,
                                     msgstr, msgstr_len, msgstr_pos,
                                     prev_msgctxt,
                                     prev_msgid,
                                     prev_msgid_plural,
                                     force_fuzzy, obsolete);
}

static inline void
call_comment (abstract_catalog_reader_ty *pop, const char *s)
{
  if (pop->methods->comment != NULL)
    pop->methods->comment (pop, s);
}

static inline void
call_comment_dot (abstract_catalog_reader_ty *pop, const char *s)
{
  if (pop->methods->comment_dot != NULL)
    pop->methods->comment_dot (pop, s);
}

static inline void
call_comment_filepos (abstract_catalog_reader_ty *pop, const char *name,
                      size_t line)
{
  if (pop->methods->comment_filepos)
    pop->methods->comment_filepos (pop, name, line);
}

static inline void
call_comment_special (abstract_catalog_reader_ty *pop, const char *s)
{
  if (pop->methods->comment_special != NULL)
    pop->methods->comment_special (pop, s);
}


/* ========================================================================= */
/* Exported functions.  */


static inline void
parse_start (abstract_catalog_reader_ty *pop)
{
  /* The parse will call the po_callback_... functions (see below)
     when the various directive are recognised.  The callback_arg
     variable is used to tell these functions which instance is to
     have the relevant method invoked.  */
  callback_arg = pop;

  call_parse_brief (pop);
}

static inline void
parse_end (abstract_catalog_reader_ty *pop)
{
  call_parse_debrief (pop);
  callback_arg = NULL;
}


void
catalog_reader_parse (abstract_catalog_reader_ty *pop, FILE *fp,
                      const char *real_filename, const char *logical_filename,
                      catalog_input_format_ty input_syntax)
{
  error_message_count = 0;

  /* Parse the stream's content.  */
  parse_start (pop);
  input_syntax->parse (pop, fp, real_filename, logical_filename);
  parse_end (pop);

  if (error_message_count > 0)
    po_xerror (PO_SEVERITY_FATAL_ERROR, NULL,
               /*real_filename*/ NULL, (size_t)(-1), (size_t)(-1), false,
               xasprintf (ngettext ("found %d fatal error",
                                    "found %d fatal errors",
                                    error_message_count),
                          error_message_count));
}


/* ========================================================================= */
/* Callbacks used by po-gram.y or po-lex.c, indirectly from
   catalog_reader_parse.  */


/* This function is called by po_gram_lex() whenever a domain directive
   has been seen.  */
void
po_callback_domain (char *name)
{
  /* assert(callback_arg); */
  call_directive_domain (callback_arg, name);
}


/* This function is called by po_gram_lex() whenever a message has been
   seen.  */
void
po_callback_message (char *msgctxt,
                     char *msgid, lex_pos_ty *msgid_pos, char *msgid_plural,
                     char *msgstr, size_t msgstr_len, lex_pos_ty *msgstr_pos,
                     char *prev_msgctxt,
                     char *prev_msgid,
                     char *prev_msgid_plural,
                     bool force_fuzzy, bool obsolete)
{
  /* assert(callback_arg); */
  call_directive_message (callback_arg, msgctxt,
                          msgid, msgid_pos, msgid_plural,
                          msgstr, msgstr_len, msgstr_pos,
                          prev_msgctxt, prev_msgid, prev_msgid_plural,
                          force_fuzzy, obsolete);
}


void
po_callback_comment (const char *s)
{
  /* assert(callback_arg); */
  call_comment (callback_arg, s);
}


void
po_callback_comment_dot (const char *s)
{
  /* assert(callback_arg); */
  call_comment_dot (callback_arg, s);
}


/* This function is called by po_parse_comment_filepos(), once for each
   filename.  */
void
po_callback_comment_filepos (const char *name, size_t line)
{
  /* assert(callback_arg); */
  call_comment_filepos (callback_arg, name, line);
}


void
po_callback_comment_special (const char *s)
{
  /* assert(callback_arg); */
  call_comment_special (callback_arg, s);
}


/* Parse a special comment and put the result in *fuzzyp, formatp, *rangep,
   *wrapp.  */
void
po_parse_comment_special (const char *s,
                          bool *fuzzyp, enum is_format formatp[NFORMATS],
                          struct argument_range *rangep, enum is_wrap *wrapp,
                          enum is_syntax_check scp[NSYNTAXCHECKS])
{
  size_t i;

  *fuzzyp = false;
  for (i = 0; i < NFORMATS; i++)
    formatp[i] = undecided;
  rangep->min = -1;
  rangep->max = -1;
  *wrapp = undecided;
  for (i = 0; i < NSYNTAXCHECKS; i++)
    scp[i] = undecided;

  while (*s != '\0')
    {
      const char *t;

      /* Skip whitespace.  */
      while (*s != '\0' && strchr ("\n \t\r\f\v,", *s) != NULL)
        s++;

      /* Collect a token.  */
      t = s;
      while (*s != '\0' && strchr ("\n \t\r\f\v,", *s) == NULL)
        s++;
      if (s != t)
        {
          size_t len = s - t;

          /* Accept fuzzy flag.  */
          if (len == 5 && memcmp (t, "fuzzy", 5) == 0)
            {
              *fuzzyp = true;
              continue;
            }

          /* Accept format description.  */
          if (len >= 7 && memcmp (t + len - 7, "-format", 7) == 0)
            {
              const char *p;
              size_t n;
              enum is_format value;

              p = t;
              n = len - 7;

              if (n >= 3 && memcmp (p, "no-", 3) == 0)
                {
                  p += 3;
                  n -= 3;
                  value = no;
                }
              else if (n >= 9 && memcmp (p, "possible-", 9) == 0)
                {
                  p += 9;
                  n -= 9;
                  value = possible;
                }
              else if (n >= 11 && memcmp (p, "impossible-", 11) == 0)
                {
                  p += 11;
                  n -= 11;
                  value = impossible;
                }
              else
                value = yes;

              for (i = 0; i < NFORMATS; i++)
                if (strlen (format_language[i]) == n
                    && memcmp (format_language[i], p, n) == 0)
                  {
                    formatp[i] = value;
                    break;
                  }
              if (i < NFORMATS)
                continue;
            }

          /* Accept range description "range: <min>..<max>".  */
          if (len == 6 && memcmp (t, "range:", 6) == 0)
            {
              /* Skip whitespace.  */
              while (*s != '\0' && strchr ("\n \t\r\f\v,", *s) != NULL)
                s++;

              /* Collect a token.  */
              t = s;
              while (*s != '\0' && strchr ("\n \t\r\f\v,", *s) == NULL)
                s++;
              /* Parse it.  */
              if (*t >= '0' && *t <= '9')
                {
                  unsigned int min = 0;

                  for (; *t >= '0' && *t <= '9'; t++)
                    {
                      if (min <= INT_MAX / 10)
                        {
                          min = 10 * min + (*t - '0');
                          if (min > INT_MAX)
                            min = INT_MAX;
                        }
                      else
                        /* Avoid integer overflow.  */
                        min = INT_MAX;
                    }
                  if (*t++ == '.')
                    if (*t++ == '.')
                      if (*t >= '0' && *t <= '9')
                        {
                          unsigned int max = 0;
                          for (; *t >= '0' && *t <= '9'; t++)
                            {
                              if (max <= INT_MAX / 10)
                                {
                                  max = 10 * max + (*t - '0');
                                  if (max > INT_MAX)
                                    max = INT_MAX;
                                }
                              else
                                /* Avoid integer overflow.  */
                                max = INT_MAX;
                            }
                          if (min <= max)
                            {
                              rangep->min = min;
                              rangep->max = max;
                              continue;
                            }
                        }
                }
            }

          /* Accept wrap description.  */
          if (len == 4 && memcmp (t, "wrap", 4) == 0)
            {
              *wrapp = yes;
              continue;
            }
          if (len == 7 && memcmp (t, "no-wrap", 7) == 0)
            {
              *wrapp = no;
              continue;
            }

          /* Accept syntax check description.  */
          if (len >= 6 && memcmp (t + len - 6, "-check", 6) == 0)
            {
              const char *p;
              size_t n;
              enum is_syntax_check value;

              p = t;
              n = len - 6;

              if (n >= 3 && memcmp (p, "no-", 3) == 0)
                {
                  p += 3;
                  n -= 3;
                  value = no;
                }
              else
                value = yes;

              for (i = 0; i < NSYNTAXCHECKS; i++)
                if (strlen (syntax_check_name[i]) == n
                    && memcmp (syntax_check_name[i], p, n) == 0)
                  {
                    scp[i] = value;
                    break;
                  }
              if (i < NSYNTAXCHECKS)
                continue;
            }

          /* Unknown special comment marker.  It may have been generated
             from a future xgettext version.  Ignore it.  */
        }
    }
}


/* Parse a GNU style file comment.
   Syntax: an arbitrary number of
             STRING COLON NUMBER
           or
             STRING
   The latter style, without line number, occurs in PO files converted e.g.
   from Pascal .rst files or from OpenOffice resource files.
   Call po_callback_comment_filepos for each of them.  */
static void
po_parse_comment_filepos (const char *s)
{
  while (*s != '\0')
    {
      while (*s == ' ' || *s == '\t' || *s == '\n')
        s++;
      if (*s != '\0')
        {
          const char *string_start = s;

          do
            s++;
          while (!(*s == '\0' || *s == ' ' || *s == '\t' || *s == '\n'));

          /* See if there is a COLON and NUMBER after the STRING, separated
             through optional spaces.  */
          {
            const char *p = s;

            while (*p == ' ' || *p == '\t' || *p == '\n')
              p++;

            if (*p == ':')
              {
                p++;

                while (*p == ' ' || *p == '\t' || *p == '\n')
                  p++;

                if (*p >= '0' && *p <= '9')
                  {
                    /* Accumulate a number.  */
                    size_t n = 0;

                    do
                      {
                        n = n * 10 + (*p - '0');
                        p++;
                      }
                    while (*p >= '0' && *p <= '9');

                    if (*p == '\0' || *p == ' ' || *p == '\t' || *p == '\n')
                      {
                        /* Parsed a GNU style file comment with spaces.  */
                        const char *string_end = s;
                        size_t string_length = string_end - string_start;
                        char *string = XNMALLOC (string_length + 1, char);

                        memcpy (string, string_start, string_length);
                        string[string_length] = '\0';

                        po_callback_comment_filepos (string, n);

                        free (string);

                        s = p;
                        continue;
                      }
                  }
              }
          }

          /* See if there is a COLON at the end of STRING and a NUMBER after
             it, separated through optional spaces.  */
          if (s[-1] == ':')
            {
              const char *p = s;

              while (*p == ' ' || *p == '\t' || *p == '\n')
                p++;

              if (*p >= '0' && *p <= '9')
                {
                  /* Accumulate a number.  */
                  size_t n = 0;

                  do
                    {
                      n = n * 10 + (*p - '0');
                      p++;
                    }
                  while (*p >= '0' && *p <= '9');

                  if (*p == '\0' || *p == ' ' || *p == '\t' || *p == '\n')
                    {
                      /* Parsed a GNU style file comment with spaces.  */
                      const char *string_end = s - 1;
                      size_t string_length = string_end - string_start;
                      char *string = XNMALLOC (string_length + 1, char);

                      memcpy (string, string_start, string_length);
                      string[string_length] = '\0';

                      po_callback_comment_filepos (string, n);

                      free (string);

                      s = p;
                      continue;
                    }
                }
            }

          /* See if there is a COLON and NUMBER at the end of the STRING,
             without separating spaces.  */
          {
            const char *p = s;

            while (p > string_start)
              {
                p--;
                if (!(*p >= '0' && *p <= '9'))
                  {
                    p++;
                    break;
                  }
              }

            /* p now points to the beginning of the trailing digits segment
               at the end of STRING.  */

            if (p < s
                && p > string_start + 1
                && p[-1] == ':')
              {
                /* Parsed a GNU style file comment without spaces.  */
                const char *string_end = p - 1;

                /* Accumulate a number.  */
                {
                  size_t n = 0;

                  do
                    {
                      n = n * 10 + (*p - '0');
                      p++;
                    }
                  while (p < s);

                  {
                    size_t string_length = string_end - string_start;
                    char *string = XNMALLOC (string_length + 1, char);

                    memcpy (string, string_start, string_length);
                    string[string_length] = '\0';

                    po_callback_comment_filepos (string, n);

                    free (string);

                    continue;
                  }
                }
              }
          }

          /* Parsed a file comment without line number.  */
          {
            const char *string_end = s;
            size_t string_length = string_end - string_start;
            char *string = XNMALLOC (string_length + 1, char);

            memcpy (string, string_start, string_length);
            string[string_length] = '\0';

            po_callback_comment_filepos (string, (size_t)(-1));

            free (string);
          }
        }
    }
}


/* Parse a SunOS or Solaris style file comment.
   Syntax of SunOS style:
     FILE_KEYWORD COLON STRING COMMA LINE_KEYWORD COLON NUMBER
   Syntax of Solaris style:
     FILE_KEYWORD COLON STRING COMMA LINE_KEYWORD NUMBER_KEYWORD COLON NUMBER
   where
     FILE_KEYWORD ::= "file" | "File"
     COLON ::= ":"
     COMMA ::= ","
     LINE_KEYWORD ::= "line"
     NUMBER_KEYWORD ::= "number"
     NUMBER ::= [0-9]+
   Return true if parsed, false if not a comment of this form. */
static bool
po_parse_comment_solaris_filepos (const char *s)
{
  if (s[0] == ' '
      && (s[1] == 'F' || s[1] == 'f')
      && s[2] == 'i' && s[3] == 'l' && s[4] == 'e'
      && s[5] == ':')
    {
      const char *string_start;
      const char *string_end;

      {
        const char *p = s + 6;

        while (*p == ' ' || *p == '\t')
          p++;
        string_start = p;
      }

      for (string_end = string_start; *string_end != '\0'; string_end++)
        {
          const char *p = string_end;

          while (*p == ' ' || *p == '\t')
            p++;

          if (*p == ',')
            {
              p++;

              while (*p == ' ' || *p == '\t')
                p++;

              if (p[0] == 'l' && p[1] == 'i' && p[2] == 'n' && p[3] == 'e')
                {
                  p += 4;

                  while (*p == ' ' || *p == '\t')
                    p++;

                  if (p[0] == 'n' && p[1] == 'u' && p[2] == 'm'
                      && p[3] == 'b' && p[4] == 'e' && p[5] == 'r')
                    {
                      p += 6;
                      while (*p == ' ' || *p == '\t')
                        p++;
                    }

                  if (*p == ':')
                    {
                      p++;

                      if (*p >= '0' && *p <= '9')
                        {
                          /* Accumulate a number.  */
                          size_t n = 0;

                          do
                            {
                              n = n * 10 + (*p - '0');
                              p++;
                            }
                          while (*p >= '0' && *p <= '9');

                          while (*p == ' ' || *p == '\t' || *p == '\n')
                            p++;

                          if (*p == '\0')
                            {
                              /* Parsed a Sun style file comment.  */
                              size_t string_length = string_end - string_start;
                              char *string =
                                XNMALLOC (string_length + 1, char);

                              memcpy (string, string_start, string_length);
                              string[string_length] = '\0';

                              po_callback_comment_filepos (string, n);

                              free (string);
                              return true;
                            }
                        }
                    }
                }
            }
        }
    }

  return false;
}


/* This function is called by po_gram_lex() whenever a comment is
   seen.  It analyzes the comment to see what sort it is, and then
   dispatches it to the appropriate method: call_comment, call_comment_dot,
   call_comment_filepos (via po_parse_comment_filepos), or
   call_comment_special.  */
void
po_callback_comment_dispatcher (const char *s)
{
  if (*s == '.')
    {
      s++;
      /* There is usually a space before the comment.  People don't
         consider it part of the comment, therefore remove it here.  */
      if (*s == ' ')
        s++;
      po_callback_comment_dot (s);
    }
  else if (*s == ':')
    {
      /* Parse the file location string.  The appropriate callback will be
         invoked.  */
      po_parse_comment_filepos (s + 1);
    }
  else if (*s == ',' || *s == '!')
    {
      /* Get all entries in the special comment line.  */
      po_callback_comment_special (s + 1);
    }
  else
    {
      /* It looks like a plain vanilla comment, but Solaris-style file
         position lines do, too.  Try to parse the lot.  If the parse
         succeeds, the appropriate callback will be invoked.  */
      if (po_parse_comment_solaris_filepos (s))
        /* Do nothing, it is a Sun-style file pos line.  */ ;
      else
        {
          /* There is usually a space before the comment.  People don't
             consider it part of the comment, therefore remove it here.  */
          if (*s == ' ')
            s++;
          po_callback_comment (s);
        }
    }
}