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