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