xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/src/format-pascal.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Object Pascal format strings.
2*946379e7Schristos    Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
3*946379e7Schristos    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4*946379e7Schristos 
5*946379e7Schristos    This program is free software; you can redistribute it and/or modify
6*946379e7Schristos    it under the terms of the GNU General Public License as published by
7*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos    any later version.
9*946379e7Schristos 
10*946379e7Schristos    This program is distributed in the hope that it will be useful,
11*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*946379e7Schristos    GNU General Public License for more details.
14*946379e7Schristos 
15*946379e7Schristos    You should have received a copy of the GNU General Public License
16*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18*946379e7Schristos 
19*946379e7Schristos #ifdef HAVE_CONFIG_H
20*946379e7Schristos # include <config.h>
21*946379e7Schristos #endif
22*946379e7Schristos 
23*946379e7Schristos #include <stdbool.h>
24*946379e7Schristos #include <stdlib.h>
25*946379e7Schristos 
26*946379e7Schristos #include "format.h"
27*946379e7Schristos #include "c-ctype.h"
28*946379e7Schristos #include "xalloc.h"
29*946379e7Schristos #include "xvasprintf.h"
30*946379e7Schristos #include "format-invalid.h"
31*946379e7Schristos #include "gettext.h"
32*946379e7Schristos 
33*946379e7Schristos #define _(str) gettext (str)
34*946379e7Schristos 
35*946379e7Schristos /* Object Pascal format strings are usable with the "format" function in the
36*946379e7Schristos    "sysutils" unit.  They are implemented in fpc-1.0.4/rtl/objpas/sysstr.inc.
37*946379e7Schristos    Another implementation exists in Borland Delphi.  The GNU Pascal's
38*946379e7Schristos    "sysutils" doesn't (yet?) have the "format" function.
39*946379e7Schristos 
40*946379e7Schristos    A directive
41*946379e7Schristos    - starts with '%',
42*946379e7Schristos    - either
43*946379e7Schristos      - is finished with '%', or
44*946379e7Schristos      - - is optionally followed by an index specification: '*' (reads an
45*946379e7Schristos          argument, must be of type integer) or a nonempty digit sequence,
46*946379e7Schristos          followed by ':',
47*946379e7Schristos        - is optionally followed by '-', which acts as a flag,
48*946379e7Schristos        - is optionally followed by a width specification: '*' (reads an
49*946379e7Schristos          argument, must be of type integer) or a nonempty digit sequence,
50*946379e7Schristos        - is optionally followed by '.' and a precision specification: '*'
51*946379e7Schristos          (reads an argument, must be of type integer) or a nonempty digit
52*946379e7Schristos          sequence,
53*946379e7Schristos        - is finished by a case-insensitive specifier. If no index was
54*946379e7Schristos          specified, it reads an argument; otherwise is uses the index-th
55*946379e7Schristos          argument, 0-based.
56*946379e7Schristos          - 'd', needs an 'integer' or 'int64' argument,
57*946379e7Schristos          - 'e', 'f', 'g', 'n', 'm', need an 'extended' floating-point argument,
58*946379e7Schristos          - 's', needs a 'string', 'char', 'pchar' or 'ansistring' argument,
59*946379e7Schristos          - 'p', needs a 'pointer' argument,
60*946379e7Schristos          - 'x', needs an integer argument.
61*946379e7Schristos    Numbered and unnumbered argument specifications can be used in the same
62*946379e7Schristos    string.  Numbered argument specifications have no influence on the
63*946379e7Schristos    "current argument index", that is incremented each time an argument is read.
64*946379e7Schristos  */
65*946379e7Schristos 
66*946379e7Schristos enum format_arg_type
67*946379e7Schristos {
68*946379e7Schristos   FAT_INTEGER,		/* integer */
69*946379e7Schristos   FAT_INTEGER64,	/* integer, int64 */
70*946379e7Schristos   FAT_FLOAT,		/* extended */
71*946379e7Schristos   FAT_STRING,		/* string, char, pchar, ansistring */
72*946379e7Schristos   FAT_POINTER
73*946379e7Schristos };
74*946379e7Schristos 
75*946379e7Schristos struct numbered_arg
76*946379e7Schristos {
77*946379e7Schristos   unsigned int number;
78*946379e7Schristos   enum format_arg_type type;
79*946379e7Schristos };
80*946379e7Schristos 
81*946379e7Schristos struct spec
82*946379e7Schristos {
83*946379e7Schristos   unsigned int directives;
84*946379e7Schristos   unsigned int numbered_arg_count;
85*946379e7Schristos   unsigned int allocated;
86*946379e7Schristos   struct numbered_arg *numbered;
87*946379e7Schristos };
88*946379e7Schristos 
89*946379e7Schristos /* Locale independent test for a decimal digit.
90*946379e7Schristos    Argument can be  'char' or 'unsigned char'.  (Whereas the argument of
91*946379e7Schristos    <ctype.h> isdigit must be an 'unsigned char'.)  */
92*946379e7Schristos #undef isdigit
93*946379e7Schristos #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
94*946379e7Schristos 
95*946379e7Schristos 
96*946379e7Schristos static int
numbered_arg_compare(const void * p1,const void * p2)97*946379e7Schristos numbered_arg_compare (const void *p1, const void *p2)
98*946379e7Schristos {
99*946379e7Schristos   unsigned int n1 = ((const struct numbered_arg *) p1)->number;
100*946379e7Schristos   unsigned int n2 = ((const struct numbered_arg *) p2)->number;
101*946379e7Schristos 
102*946379e7Schristos   return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
103*946379e7Schristos }
104*946379e7Schristos 
105*946379e7Schristos static void *
format_parse(const char * format,bool translated,char ** invalid_reason)106*946379e7Schristos format_parse (const char *format, bool translated, char **invalid_reason)
107*946379e7Schristos {
108*946379e7Schristos   unsigned int directives;
109*946379e7Schristos   unsigned int numbered_arg_count;
110*946379e7Schristos   unsigned int allocated;
111*946379e7Schristos   struct numbered_arg *numbered;
112*946379e7Schristos   unsigned int unnumbered_arg_count;
113*946379e7Schristos   struct spec *result;
114*946379e7Schristos 
115*946379e7Schristos   enum arg_index
116*946379e7Schristos   {
117*946379e7Schristos     index_numbered,	/* index given by a fixed integer */
118*946379e7Schristos     index_unnumbered,	/* index given by unnumbered_arg_count++ */
119*946379e7Schristos     index_unknown	/* index is only known at run time */
120*946379e7Schristos   };
121*946379e7Schristos 
122*946379e7Schristos   directives = 0;
123*946379e7Schristos   numbered_arg_count = 0;
124*946379e7Schristos   allocated = 0;
125*946379e7Schristos   numbered = NULL;
126*946379e7Schristos   unnumbered_arg_count = 0;
127*946379e7Schristos 
128*946379e7Schristos   for (; *format != '\0';)
129*946379e7Schristos     if (*format++ == '%')
130*946379e7Schristos       {
131*946379e7Schristos 	/* A directive.  */
132*946379e7Schristos 	directives++;
133*946379e7Schristos 
134*946379e7Schristos 	if (*format != '%')
135*946379e7Schristos 	  {
136*946379e7Schristos 	    /* A complex directive.  */
137*946379e7Schristos 	    enum arg_index main_arg = index_unnumbered;
138*946379e7Schristos 	    unsigned int main_number = 0;
139*946379e7Schristos 	    enum format_arg_type type;
140*946379e7Schristos 
141*946379e7Schristos 	    if (isdigit (*format))
142*946379e7Schristos 	      {
143*946379e7Schristos 		const char *f = format;
144*946379e7Schristos 		unsigned int m = 0;
145*946379e7Schristos 
146*946379e7Schristos 		do
147*946379e7Schristos 		  {
148*946379e7Schristos 		    m = 10 * m + (*f - '0');
149*946379e7Schristos 		    f++;
150*946379e7Schristos 		  }
151*946379e7Schristos 		while (isdigit (*f));
152*946379e7Schristos 
153*946379e7Schristos 		if (*f == ':')
154*946379e7Schristos 		  {
155*946379e7Schristos 		    main_number = m;
156*946379e7Schristos 		    main_arg = index_numbered;
157*946379e7Schristos 		    format = ++f;
158*946379e7Schristos 		  }
159*946379e7Schristos 	      }
160*946379e7Schristos 	    else if (*format == '*')
161*946379e7Schristos 	      {
162*946379e7Schristos 		if (format[1] == ':')
163*946379e7Schristos 		  {
164*946379e7Schristos 		    main_arg = index_unknown;
165*946379e7Schristos 		    format += 2;
166*946379e7Schristos 		  }
167*946379e7Schristos 	      }
168*946379e7Schristos 
169*946379e7Schristos 	    /* Parse flags.  */
170*946379e7Schristos 	    if (*format == '-')
171*946379e7Schristos 	      format++;
172*946379e7Schristos 
173*946379e7Schristos 	    /* Parse width.  */
174*946379e7Schristos 	    if (isdigit (*format))
175*946379e7Schristos 	      {
176*946379e7Schristos 		do
177*946379e7Schristos 		  format++;
178*946379e7Schristos 		while (isdigit (*format));
179*946379e7Schristos 	      }
180*946379e7Schristos 	    else if (*format == '*')
181*946379e7Schristos 	      {
182*946379e7Schristos 		/* Unnumbered argument of type FAT_INTEGER.   */
183*946379e7Schristos 		if (allocated == numbered_arg_count)
184*946379e7Schristos 		  {
185*946379e7Schristos 		    allocated = 2 * allocated + 1;
186*946379e7Schristos 		    numbered = (struct numbered_arg *) xrealloc (numbered, allocated * sizeof (struct numbered_arg));
187*946379e7Schristos 		  }
188*946379e7Schristos 		numbered[numbered_arg_count].number = unnumbered_arg_count;
189*946379e7Schristos 		numbered[numbered_arg_count].type = FAT_INTEGER;
190*946379e7Schristos 		numbered_arg_count++;
191*946379e7Schristos 		unnumbered_arg_count++;
192*946379e7Schristos 
193*946379e7Schristos 		format++;
194*946379e7Schristos 	      }
195*946379e7Schristos 
196*946379e7Schristos 	    /* Parse precision.  */
197*946379e7Schristos 	    if (*format == '.')
198*946379e7Schristos 	      {
199*946379e7Schristos 		format++;
200*946379e7Schristos 
201*946379e7Schristos 		if (isdigit (*format))
202*946379e7Schristos 		  {
203*946379e7Schristos 		    do
204*946379e7Schristos 		      format++;
205*946379e7Schristos 		    while (isdigit (*format));
206*946379e7Schristos 		  }
207*946379e7Schristos 		else if (*format == '*')
208*946379e7Schristos 		  {
209*946379e7Schristos 		    /* Unnumbered argument of type FAT_INTEGER.   */
210*946379e7Schristos 		    if (allocated == unnumbered_arg_count)
211*946379e7Schristos 		      {
212*946379e7Schristos 			allocated = 2 * allocated + 1;
213*946379e7Schristos 			numbered = (struct numbered_arg *) xrealloc (numbered, allocated * sizeof (struct numbered_arg));
214*946379e7Schristos 		      }
215*946379e7Schristos 		    numbered[numbered_arg_count].number = unnumbered_arg_count;
216*946379e7Schristos 		    numbered[numbered_arg_count].type = FAT_INTEGER;
217*946379e7Schristos 		    numbered_arg_count++;
218*946379e7Schristos 		    unnumbered_arg_count++;
219*946379e7Schristos 
220*946379e7Schristos 		    format++;
221*946379e7Schristos 		  }
222*946379e7Schristos 		else
223*946379e7Schristos 		  --format;	/* will jump to bad_format */
224*946379e7Schristos 	      }
225*946379e7Schristos 
226*946379e7Schristos 	    switch (c_tolower (*format))
227*946379e7Schristos 	      {
228*946379e7Schristos 	      case 'd':
229*946379e7Schristos 		type = FAT_INTEGER64;
230*946379e7Schristos 		break;
231*946379e7Schristos 	      case 'e': case 'f': case 'g': case 'n': case 'm':
232*946379e7Schristos 		type = FAT_FLOAT;
233*946379e7Schristos 		break;
234*946379e7Schristos 	      case 's':
235*946379e7Schristos 		type = FAT_STRING;
236*946379e7Schristos 		break;
237*946379e7Schristos 	      case 'p':
238*946379e7Schristos 		type = FAT_POINTER;
239*946379e7Schristos 		break;
240*946379e7Schristos 	      case 'x':
241*946379e7Schristos 		type = FAT_INTEGER;
242*946379e7Schristos 		break;
243*946379e7Schristos 	      default:
244*946379e7Schristos 		*invalid_reason =
245*946379e7Schristos 		  (*format == '\0'
246*946379e7Schristos 		   ? INVALID_UNTERMINATED_DIRECTIVE ()
247*946379e7Schristos 		   : INVALID_CONVERSION_SPECIFIER (directives, *format));
248*946379e7Schristos 		goto bad_format;
249*946379e7Schristos 	      }
250*946379e7Schristos 
251*946379e7Schristos 	    if (allocated == numbered_arg_count)
252*946379e7Schristos 	      {
253*946379e7Schristos 		allocated = 2 * allocated + 1;
254*946379e7Schristos 		numbered = (struct numbered_arg *) xrealloc (numbered, allocated * sizeof (struct numbered_arg));
255*946379e7Schristos 	      }
256*946379e7Schristos 	    switch (main_arg)
257*946379e7Schristos 	      {
258*946379e7Schristos 	      case index_unnumbered:
259*946379e7Schristos 		numbered[numbered_arg_count].number = unnumbered_arg_count;
260*946379e7Schristos 		numbered[numbered_arg_count].type = type;
261*946379e7Schristos 		unnumbered_arg_count++;
262*946379e7Schristos 		break;
263*946379e7Schristos 	      case index_numbered:
264*946379e7Schristos 		numbered[numbered_arg_count].number = main_number;
265*946379e7Schristos 		numbered[numbered_arg_count].type = type;
266*946379e7Schristos 		break;
267*946379e7Schristos 	      case index_unknown:
268*946379e7Schristos 		numbered[numbered_arg_count].number = unnumbered_arg_count;
269*946379e7Schristos 		numbered[numbered_arg_count].type = FAT_INTEGER;
270*946379e7Schristos 		unnumbered_arg_count++;
271*946379e7Schristos 		break;
272*946379e7Schristos 	      default:
273*946379e7Schristos 		abort ();
274*946379e7Schristos 	      }
275*946379e7Schristos 	    numbered_arg_count++;
276*946379e7Schristos 	  }
277*946379e7Schristos 
278*946379e7Schristos 	format++;
279*946379e7Schristos       }
280*946379e7Schristos 
281*946379e7Schristos   /* Sort the numbered argument array, and eliminate duplicates.  */
282*946379e7Schristos   if (numbered_arg_count > 1)
283*946379e7Schristos     {
284*946379e7Schristos       unsigned int i, j;
285*946379e7Schristos       bool err;
286*946379e7Schristos 
287*946379e7Schristos       qsort (numbered, numbered_arg_count,
288*946379e7Schristos 	     sizeof (struct numbered_arg), numbered_arg_compare);
289*946379e7Schristos 
290*946379e7Schristos       /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i.  */
291*946379e7Schristos       err = false;
292*946379e7Schristos       for (i = j = 0; i < numbered_arg_count; i++)
293*946379e7Schristos 	if (j > 0 && numbered[i].number == numbered[j-1].number)
294*946379e7Schristos 	  {
295*946379e7Schristos 	    enum format_arg_type type1 = numbered[i].type;
296*946379e7Schristos 	    enum format_arg_type type2 = numbered[j-1].type;
297*946379e7Schristos 	    enum format_arg_type type_both;
298*946379e7Schristos 
299*946379e7Schristos 	    if (type1 == type2)
300*946379e7Schristos 	      type_both = type1;
301*946379e7Schristos 	    else if ((type1 == FAT_INTEGER && type2 == FAT_INTEGER64)
302*946379e7Schristos 		     || (type1 == FAT_INTEGER64 && type2 == FAT_INTEGER))
303*946379e7Schristos 	      type_both = FAT_INTEGER;
304*946379e7Schristos 	    else
305*946379e7Schristos 	      {
306*946379e7Schristos 		/* Incompatible types.  */
307*946379e7Schristos 		type_both = type1;
308*946379e7Schristos 		if (!err)
309*946379e7Schristos 		  *invalid_reason =
310*946379e7Schristos 		    INVALID_INCOMPATIBLE_ARG_TYPES (numbered[i].number);
311*946379e7Schristos 		err = true;
312*946379e7Schristos 	      }
313*946379e7Schristos 
314*946379e7Schristos 	    numbered[j-1].type = type_both;
315*946379e7Schristos 	  }
316*946379e7Schristos 	else
317*946379e7Schristos 	  {
318*946379e7Schristos 	    if (j < i)
319*946379e7Schristos 	      {
320*946379e7Schristos 		numbered[j].number = numbered[i].number;
321*946379e7Schristos 		numbered[j].type = numbered[i].type;
322*946379e7Schristos 	      }
323*946379e7Schristos 	    j++;
324*946379e7Schristos 	  }
325*946379e7Schristos       numbered_arg_count = j;
326*946379e7Schristos       if (err)
327*946379e7Schristos 	/* *invalid_reason has already been set above.  */
328*946379e7Schristos 	goto bad_format;
329*946379e7Schristos     }
330*946379e7Schristos 
331*946379e7Schristos   result = (struct spec *) xmalloc (sizeof (struct spec));
332*946379e7Schristos   result->directives = directives;
333*946379e7Schristos   result->numbered_arg_count = numbered_arg_count;
334*946379e7Schristos   result->allocated = allocated;
335*946379e7Schristos   result->numbered = numbered;
336*946379e7Schristos   return result;
337*946379e7Schristos 
338*946379e7Schristos  bad_format:
339*946379e7Schristos   if (numbered != NULL)
340*946379e7Schristos     free (numbered);
341*946379e7Schristos   return NULL;
342*946379e7Schristos }
343*946379e7Schristos 
344*946379e7Schristos static void
format_free(void * descr)345*946379e7Schristos format_free (void *descr)
346*946379e7Schristos {
347*946379e7Schristos   struct spec *spec = (struct spec *) descr;
348*946379e7Schristos 
349*946379e7Schristos   if (spec->numbered != NULL)
350*946379e7Schristos     free (spec->numbered);
351*946379e7Schristos   free (spec);
352*946379e7Schristos }
353*946379e7Schristos 
354*946379e7Schristos static int
format_get_number_of_directives(void * descr)355*946379e7Schristos format_get_number_of_directives (void *descr)
356*946379e7Schristos {
357*946379e7Schristos   struct spec *spec = (struct spec *) descr;
358*946379e7Schristos 
359*946379e7Schristos   return spec->directives;
360*946379e7Schristos }
361*946379e7Schristos 
362*946379e7Schristos static bool
format_check(void * msgid_descr,void * msgstr_descr,bool equality,formatstring_error_logger_t error_logger,const char * pretty_msgstr)363*946379e7Schristos format_check (void *msgid_descr, void *msgstr_descr, bool equality,
364*946379e7Schristos 	      formatstring_error_logger_t error_logger,
365*946379e7Schristos 	      const char *pretty_msgstr)
366*946379e7Schristos {
367*946379e7Schristos   struct spec *spec1 = (struct spec *) msgid_descr;
368*946379e7Schristos   struct spec *spec2 = (struct spec *) msgstr_descr;
369*946379e7Schristos   bool err = false;
370*946379e7Schristos 
371*946379e7Schristos   if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
372*946379e7Schristos     {
373*946379e7Schristos       unsigned int i, j;
374*946379e7Schristos       unsigned int n1 = spec1->numbered_arg_count;
375*946379e7Schristos       unsigned int n2 = spec2->numbered_arg_count;
376*946379e7Schristos 
377*946379e7Schristos       /* Check the argument names are the same.
378*946379e7Schristos 	 Both arrays are sorted.  We search for the first difference.  */
379*946379e7Schristos       for (i = 0, j = 0; i < n1 || j < n2; )
380*946379e7Schristos 	{
381*946379e7Schristos 	  int cmp = (i >= n1 ? 1 :
382*946379e7Schristos 		     j >= n2 ? -1 :
383*946379e7Schristos 		     spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
384*946379e7Schristos 		     spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
385*946379e7Schristos 		     0);
386*946379e7Schristos 
387*946379e7Schristos 	  if (cmp > 0)
388*946379e7Schristos 	    {
389*946379e7Schristos 	      if (error_logger)
390*946379e7Schristos 		error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
391*946379e7Schristos 			      spec2->numbered[j].number, pretty_msgstr);
392*946379e7Schristos 	      err = true;
393*946379e7Schristos 	      break;
394*946379e7Schristos 	    }
395*946379e7Schristos 	  else if (cmp < 0)
396*946379e7Schristos 	    {
397*946379e7Schristos 	      if (equality)
398*946379e7Schristos 		{
399*946379e7Schristos 		  if (error_logger)
400*946379e7Schristos 		    error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
401*946379e7Schristos 				  spec1->numbered[i].number, pretty_msgstr);
402*946379e7Schristos 		  err = true;
403*946379e7Schristos 		  break;
404*946379e7Schristos 		}
405*946379e7Schristos 	      else
406*946379e7Schristos 		i++;
407*946379e7Schristos 	    }
408*946379e7Schristos 	  else
409*946379e7Schristos 	    j++, i++;
410*946379e7Schristos 	}
411*946379e7Schristos       /* Check the argument types are the same.  */
412*946379e7Schristos       if (!err)
413*946379e7Schristos 	for (i = 0, j = 0; j < n2; )
414*946379e7Schristos 	  {
415*946379e7Schristos 	    if (spec1->numbered[i].number == spec2->numbered[j].number)
416*946379e7Schristos 	      {
417*946379e7Schristos 		if (spec1->numbered[i].type != spec2->numbered[j].type)
418*946379e7Schristos 		  {
419*946379e7Schristos 		    if (error_logger)
420*946379e7Schristos 		      error_logger (_("format specifications in 'msgid' and '%s' for argument %u are not the same"),
421*946379e7Schristos 				    pretty_msgstr, spec2->numbered[j].number);
422*946379e7Schristos 		    err = true;
423*946379e7Schristos 		    break;
424*946379e7Schristos 		  }
425*946379e7Schristos 		j++, i++;
426*946379e7Schristos 	      }
427*946379e7Schristos 	    else
428*946379e7Schristos 	      i++;
429*946379e7Schristos 	  }
430*946379e7Schristos     }
431*946379e7Schristos 
432*946379e7Schristos   return err;
433*946379e7Schristos }
434*946379e7Schristos 
435*946379e7Schristos 
436*946379e7Schristos struct formatstring_parser formatstring_pascal =
437*946379e7Schristos {
438*946379e7Schristos   format_parse,
439*946379e7Schristos   format_free,
440*946379e7Schristos   format_get_number_of_directives,
441*946379e7Schristos   NULL,
442*946379e7Schristos   format_check
443*946379e7Schristos };
444*946379e7Schristos 
445*946379e7Schristos 
446*946379e7Schristos #ifdef TEST
447*946379e7Schristos 
448*946379e7Schristos /* Test program: Print the argument list specification returned by
449*946379e7Schristos    format_parse for strings read from standard input.  */
450*946379e7Schristos 
451*946379e7Schristos #include <stdio.h>
452*946379e7Schristos #include "getline.h"
453*946379e7Schristos 
454*946379e7Schristos static void
format_print(void * descr)455*946379e7Schristos format_print (void *descr)
456*946379e7Schristos {
457*946379e7Schristos   struct spec *spec = (struct spec *) descr;
458*946379e7Schristos   unsigned int last;
459*946379e7Schristos   unsigned int i;
460*946379e7Schristos 
461*946379e7Schristos   if (spec == NULL)
462*946379e7Schristos     {
463*946379e7Schristos       printf ("INVALID");
464*946379e7Schristos       return;
465*946379e7Schristos     }
466*946379e7Schristos 
467*946379e7Schristos   printf ("(");
468*946379e7Schristos   last = 0;
469*946379e7Schristos   for (i = 0; i < spec->numbered_arg_count; i++)
470*946379e7Schristos     {
471*946379e7Schristos       unsigned int number = spec->numbered[i].number;
472*946379e7Schristos 
473*946379e7Schristos       if (i > 0)
474*946379e7Schristos 	printf (" ");
475*946379e7Schristos       if (number < last)
476*946379e7Schristos 	abort ();
477*946379e7Schristos       for (; last < number; last++)
478*946379e7Schristos 	printf ("_ ");
479*946379e7Schristos       switch (spec->numbered[i].type)
480*946379e7Schristos 	{
481*946379e7Schristos 	case FAT_INTEGER:
482*946379e7Schristos 	  printf ("i");
483*946379e7Schristos 	  break;
484*946379e7Schristos 	case FAT_INTEGER64:
485*946379e7Schristos 	  printf ("I");
486*946379e7Schristos 	  break;
487*946379e7Schristos 	case FAT_FLOAT:
488*946379e7Schristos 	  printf ("f");
489*946379e7Schristos 	  break;
490*946379e7Schristos 	case FAT_STRING:
491*946379e7Schristos 	  printf ("s");
492*946379e7Schristos 	  break;
493*946379e7Schristos 	case FAT_POINTER:
494*946379e7Schristos 	  printf ("p");
495*946379e7Schristos 	  break;
496*946379e7Schristos 	default:
497*946379e7Schristos 	  abort ();
498*946379e7Schristos 	}
499*946379e7Schristos       last = number + 1;
500*946379e7Schristos     }
501*946379e7Schristos   printf (")");
502*946379e7Schristos }
503*946379e7Schristos 
504*946379e7Schristos int
main()505*946379e7Schristos main ()
506*946379e7Schristos {
507*946379e7Schristos   for (;;)
508*946379e7Schristos     {
509*946379e7Schristos       char *line = NULL;
510*946379e7Schristos       size_t line_size = 0;
511*946379e7Schristos       int line_len;
512*946379e7Schristos       char *invalid_reason;
513*946379e7Schristos       void *descr;
514*946379e7Schristos 
515*946379e7Schristos       line_len = getline (&line, &line_size, stdin);
516*946379e7Schristos       if (line_len < 0)
517*946379e7Schristos 	break;
518*946379e7Schristos       if (line_len > 0 && line[line_len - 1] == '\n')
519*946379e7Schristos 	line[--line_len] = '\0';
520*946379e7Schristos 
521*946379e7Schristos       invalid_reason = NULL;
522*946379e7Schristos       descr = format_parse (line, false, &invalid_reason);
523*946379e7Schristos 
524*946379e7Schristos       format_print (descr);
525*946379e7Schristos       printf ("\n");
526*946379e7Schristos       if (descr == NULL)
527*946379e7Schristos 	printf ("%s\n", invalid_reason);
528*946379e7Schristos 
529*946379e7Schristos       free (invalid_reason);
530*946379e7Schristos       free (line);
531*946379e7Schristos     }
532*946379e7Schristos 
533*946379e7Schristos   return 0;
534*946379e7Schristos }
535*946379e7Schristos 
536*946379e7Schristos /*
537*946379e7Schristos  * For Emacs M-x compile
538*946379e7Schristos  * Local Variables:
539*946379e7Schristos  * compile-command: "/bin/sh ../libtool --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../lib -I../intl -DHAVE_CONFIG_H -DTEST format-pascal.c ../lib/libgettextlib.la"
540*946379e7Schristos  * End:
541*946379e7Schristos  */
542*946379e7Schristos 
543*946379e7Schristos #endif /* TEST */
544