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