xref: /netbsd-src/external/gpl2/gettext/dist/gettext-runtime/intl/plural-exp.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Expression parsing for plural form selection.
2*946379e7Schristos    Copyright (C) 2000-2001, 2003, 2005 Free Software Foundation, Inc.
3*946379e7Schristos    Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
4*946379e7Schristos 
5*946379e7Schristos    This program is free software; you can redistribute it and/or modify it
6*946379e7Schristos    under the terms of the GNU Library General Public License as published
7*946379e7Schristos    by 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 GNU
13*946379e7Schristos    Library General Public License for more details.
14*946379e7Schristos 
15*946379e7Schristos    You should have received a copy of the GNU Library General Public
16*946379e7Schristos    License along with this program; if not, write to the Free Software
17*946379e7Schristos    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18*946379e7Schristos    USA.  */
19*946379e7Schristos 
20*946379e7Schristos #ifdef HAVE_CONFIG_H
21*946379e7Schristos # include <config.h>
22*946379e7Schristos #endif
23*946379e7Schristos 
24*946379e7Schristos #include <ctype.h>
25*946379e7Schristos #include <stdlib.h>
26*946379e7Schristos #include <string.h>
27*946379e7Schristos 
28*946379e7Schristos #include "plural-exp.h"
29*946379e7Schristos 
30*946379e7Schristos #if (defined __GNUC__ && !(__APPLE_CC__ > 1)) \
31*946379e7Schristos     || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
32*946379e7Schristos 
33*946379e7Schristos /* These structs are the constant expression for the germanic plural
34*946379e7Schristos    form determination.  It represents the expression  "n != 1".  */
35*946379e7Schristos static const struct expression plvar =
36*946379e7Schristos {
37*946379e7Schristos   .nargs = 0,
38*946379e7Schristos   .operation = var,
39*946379e7Schristos };
40*946379e7Schristos static const struct expression plone =
41*946379e7Schristos {
42*946379e7Schristos   .nargs = 0,
43*946379e7Schristos   .operation = num,
44*946379e7Schristos   .val =
45*946379e7Schristos   {
46*946379e7Schristos     .num = 1
47*946379e7Schristos   }
48*946379e7Schristos };
49*946379e7Schristos struct expression GERMANIC_PLURAL =
50*946379e7Schristos {
51*946379e7Schristos   .nargs = 2,
52*946379e7Schristos   .operation = not_equal,
53*946379e7Schristos   .val =
54*946379e7Schristos   {
55*946379e7Schristos     .args =
56*946379e7Schristos     {
57*946379e7Schristos       [0] = (struct expression *) &plvar,
58*946379e7Schristos       [1] = (struct expression *) &plone
59*946379e7Schristos     }
60*946379e7Schristos   }
61*946379e7Schristos };
62*946379e7Schristos 
63*946379e7Schristos # define INIT_GERMANIC_PLURAL()
64*946379e7Schristos 
65*946379e7Schristos #else
66*946379e7Schristos 
67*946379e7Schristos /* For compilers without support for ISO C 99 struct/union initializers:
68*946379e7Schristos    Initialization at run-time.  */
69*946379e7Schristos 
70*946379e7Schristos static struct expression plvar;
71*946379e7Schristos static struct expression plone;
72*946379e7Schristos struct expression GERMANIC_PLURAL;
73*946379e7Schristos 
74*946379e7Schristos static void
init_germanic_plural()75*946379e7Schristos init_germanic_plural ()
76*946379e7Schristos {
77*946379e7Schristos   if (plone.val.num == 0)
78*946379e7Schristos     {
79*946379e7Schristos       plvar.nargs = 0;
80*946379e7Schristos       plvar.operation = var;
81*946379e7Schristos 
82*946379e7Schristos       plone.nargs = 0;
83*946379e7Schristos       plone.operation = num;
84*946379e7Schristos       plone.val.num = 1;
85*946379e7Schristos 
86*946379e7Schristos       GERMANIC_PLURAL.nargs = 2;
87*946379e7Schristos       GERMANIC_PLURAL.operation = not_equal;
88*946379e7Schristos       GERMANIC_PLURAL.val.args[0] = &plvar;
89*946379e7Schristos       GERMANIC_PLURAL.val.args[1] = &plone;
90*946379e7Schristos     }
91*946379e7Schristos }
92*946379e7Schristos 
93*946379e7Schristos # define INIT_GERMANIC_PLURAL() init_germanic_plural ()
94*946379e7Schristos 
95*946379e7Schristos #endif
96*946379e7Schristos 
97*946379e7Schristos void
98*946379e7Schristos internal_function
EXTRACT_PLURAL_EXPRESSION(const char * nullentry,struct expression ** pluralp,unsigned long int * npluralsp)99*946379e7Schristos EXTRACT_PLURAL_EXPRESSION (const char *nullentry, struct expression **pluralp,
100*946379e7Schristos 			   unsigned long int *npluralsp)
101*946379e7Schristos {
102*946379e7Schristos   if (nullentry != NULL)
103*946379e7Schristos     {
104*946379e7Schristos       const char *plural;
105*946379e7Schristos       const char *nplurals;
106*946379e7Schristos 
107*946379e7Schristos       plural = strstr (nullentry, "plural=");
108*946379e7Schristos       nplurals = strstr (nullentry, "nplurals=");
109*946379e7Schristos       if (plural == NULL || nplurals == NULL)
110*946379e7Schristos 	goto no_plural;
111*946379e7Schristos       else
112*946379e7Schristos 	{
113*946379e7Schristos 	  char *endp;
114*946379e7Schristos 	  unsigned long int n;
115*946379e7Schristos 	  struct parse_args args;
116*946379e7Schristos 
117*946379e7Schristos 	  /* First get the number.  */
118*946379e7Schristos 	  nplurals += 9;
119*946379e7Schristos 	  while (*nplurals != '\0' && isspace ((unsigned char) *nplurals))
120*946379e7Schristos 	    ++nplurals;
121*946379e7Schristos 	  if (!(*nplurals >= '0' && *nplurals <= '9'))
122*946379e7Schristos 	    goto no_plural;
123*946379e7Schristos #if defined HAVE_STRTOUL || defined _LIBC
124*946379e7Schristos 	  n = strtoul (nplurals, &endp, 10);
125*946379e7Schristos #else
126*946379e7Schristos 	  for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++)
127*946379e7Schristos 	    n = n * 10 + (*endp - '0');
128*946379e7Schristos #endif
129*946379e7Schristos 	  if (nplurals == endp)
130*946379e7Schristos 	    goto no_plural;
131*946379e7Schristos 	  *npluralsp = n;
132*946379e7Schristos 
133*946379e7Schristos 	  /* Due to the restrictions bison imposes onto the interface of the
134*946379e7Schristos 	     scanner function we have to put the input string and the result
135*946379e7Schristos 	     passed up from the parser into the same structure which address
136*946379e7Schristos 	     is passed down to the parser.  */
137*946379e7Schristos 	  plural += 7;
138*946379e7Schristos 	  args.cp = plural;
139*946379e7Schristos 	  if (PLURAL_PARSE (&args) != 0)
140*946379e7Schristos 	    goto no_plural;
141*946379e7Schristos 	  *pluralp = args.res;
142*946379e7Schristos 	}
143*946379e7Schristos     }
144*946379e7Schristos   else
145*946379e7Schristos     {
146*946379e7Schristos       /* By default we are using the Germanic form: singular form only
147*946379e7Schristos          for `one', the plural form otherwise.  Yes, this is also what
148*946379e7Schristos          English is using since English is a Germanic language.  */
149*946379e7Schristos     no_plural:
150*946379e7Schristos       INIT_GERMANIC_PLURAL ();
151*946379e7Schristos       *pluralp = &GERMANIC_PLURAL;
152*946379e7Schristos       *npluralsp = 2;
153*946379e7Schristos     }
154*946379e7Schristos }
155