summaryrefslogtreecommitdiffstats
path: root/src/po.c
blob: e4b966824a8c4b75a60cc1b3af16da51229bc236 (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
/* GNU gettext - internationalization aids
   Copyright (C) 1995-1996, 1998, 2000, 2001 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 2, 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */


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

#include <stdlib.h>

#include "po.h"
#include "po-charset.h"
#include "po-hash.h"
#include "system.h"

/* Prototypes for local functions.  Needed to ensure compiler checking of
   function argument counts despite of K&R C function definition syntax.  */
static void po_parse_brief PARAMS ((po_ty *pop));
static void po_parse_debrief PARAMS ((po_ty *pop));

/* Methods used indirectly by po_scan.  */
static void po_directive_domain PARAMS ((po_ty *pop, char *name));
static void po_directive_message PARAMS ((po_ty *pop, char *msgid,
					  lex_pos_ty *msgid_pos,
					  char *msgid_plural,
					  char *msgstr, size_t msgstr_len,
					  lex_pos_ty *msgstr_pos,
					  bool obsolete));
static void po_comment PARAMS ((po_ty *pop, const char *s));
static void po_comment_dot PARAMS ((po_ty *pop, const char *s));
static void po_comment_filepos PARAMS ((po_ty *pop, const char *name,
					int line));
static void po_comment_special PARAMS ((po_ty *pop, const char *s));

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


po_ty *
po_alloc (pomp)
     po_method_ty *pomp;
{
  po_ty *pop;

  pop = (po_ty *) xmalloc (pomp->size);
  pop->method = pomp;
  if (pomp->constructor)
    pomp->constructor (pop);
  return pop;
}


void
po_free (pop)
     po_ty *pop;
{
  if (pop->method->destructor)
    pop->method->destructor (pop);
  free (pop);
}


void
po_scan (pop, fp, real_filename, logical_filename)
     po_ty *pop;
     FILE *fp;
     const char *real_filename;
     const char *logical_filename;
{
  extern int po_gram_parse PARAMS ((void));

  /* 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;

  /* Parse the stream's content.  */
  lex_start (fp, real_filename, logical_filename);
  po_parse_brief (pop);
  po_gram_parse ();
  po_parse_debrief (pop);
  lex_end ();
  callback_arg = NULL;
}


void
po_scan_file (pop, filename)
     po_ty *pop;
     const char *filename;
{
  extern int po_gram_parse PARAMS ((void));

  /* 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;

  /* Open the file and parse it.  */
  lex_open (filename);
  po_parse_brief (pop);
  po_gram_parse ();
  po_parse_debrief (pop);
  lex_close ();
  callback_arg = NULL;
}


static void
po_parse_brief (pop)
     po_ty *pop;
{
  if (pop->method->parse_brief)
    pop->method->parse_brief (pop);
}


static void
po_parse_debrief (pop)
     po_ty *pop;
{
  if (pop->method->parse_debrief)
    pop->method->parse_debrief (pop);
}


static void
po_directive_domain (pop, name)
     po_ty *pop;
     char *name;
{
  if (pop->method->directive_domain)
    pop->method->directive_domain (pop, name);
}


void
po_callback_domain (name)
     char *name;
{
  /* assert(callback_arg); */
  po_directive_domain (callback_arg, name);
}


static void
po_directive_message (pop, msgid, msgid_pos, msgid_plural,
		      msgstr, msgstr_len, msgstr_pos, obsolete)
     po_ty *pop;
     char *msgid;
     lex_pos_ty *msgid_pos;
     char *msgid_plural;
     char *msgstr;
     size_t msgstr_len;
     lex_pos_ty *msgstr_pos;
     bool obsolete;
{
  if (pop->method->directive_message)
    pop->method->directive_message (pop, msgid, msgid_pos, msgid_plural,
				    msgstr, msgstr_len, msgstr_pos, obsolete);
}


void
po_callback_message (msgid, msgid_pos, msgid_plural,
		     msgstr, msgstr_len, msgstr_pos,
		     obsolete)
     char *msgid;
     lex_pos_ty *msgid_pos;
     char *msgid_plural;
     char *msgstr;
     size_t msgstr_len;
     lex_pos_ty *msgstr_pos;
     bool obsolete;
{
  /* assert(callback_arg); */

  /* Test for header entry.  Ignore fuzziness of the header entry.  */
  if (msgid[0] == '\0')
    po_lex_charset_set (msgstr, gram_pos.file_name);

  po_directive_message (callback_arg, msgid, msgid_pos, msgid_plural,
			msgstr, msgstr_len, msgstr_pos, obsolete);
}


static void
po_comment_special (pop, s)
     po_ty *pop;
     const char *s;
{
  if (pop->method->comment_special != NULL)
    pop->method->comment_special (pop, s);
}


static void
po_comment (pop, s)
     po_ty *pop;
     const char *s;
{
  if (pop->method->comment != NULL)
    pop->method->comment (pop, s);
}


static void
po_comment_dot (pop, s)
     po_ty *pop;
     const char *s;
{
  if (pop->method->comment_dot != NULL)
    pop->method->comment_dot (pop, s);
}


/* 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
   dispatces it to the appropriate method.  */
void
po_callback_comment (s)
     const char *s;
{
  /* assert(callback_arg); */
  if (*s == '.')
    po_comment_dot (callback_arg, s + 1);
  else if (*s == ':')
    {
      /* Parse the file location string.  If the parse succeeds, the
	 appropriate callback will be invoked.  If the parse fails,
	 the po_hash function will return non-zero - so pretend it was
	 a normal comment.  */
      if (po_hash (s + 1) == 0)
	/* Do nothing, it is a GNU-style file pos line.  */ ;
      else
	po_comment (callback_arg, s + 1);
    }
  else if (*s == ',' || *s == '!')
    {
      /* Get all entries in the special comment line.  */
      po_comment_special (callback_arg, s + 1);
    }
  else
    {
      /* It looks like a plain vanilla comment, but Solaris-style file
	 position lines do, too.  Rather than parse the lot, only look
	 at lines that could start with "# File..." This minimizes
	 memory leaks on failed parses.  If the parse succeeds, the
	 appropriate callback will be invoked.  */
      if (s[0] == ' ' && (s[1] == 'F' || s[1] == 'f') && s[2] == 'i'
	  && po_hash (s) == 0)
	/* Do nothing, it is a Sun-style file pos line.  */ ;
      else
	po_comment (callback_arg, s);
    }
}


static void
po_comment_filepos (pop, name, line)
     po_ty *pop;
     const char *name;
     int line;
{
  if (pop->method->comment_filepos)
    pop->method->comment_filepos (pop, name, line);
}


void
po_callback_comment_filepos (name, line)
     const char *name;
     int line;
{
  /* assert(callback_arg); */
  po_comment_filepos (callback_arg, name, line);
}


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

  *fuzzyp = false;
  for (i = 0; i < NFORMATS; i++)
    formatp[i] = undecided;
  *wrapp = 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 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;
	    }

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