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