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