1 /* Expression parsing for plural form selection.
2 Copyright (C) 2000-2016 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 <https://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 HAVE_STRUCT_INITIALIZER
29
30 /* These structs are the constant expression for the germanic plural
31 form determination. It represents the expression "n != 1". */
32 static const struct expression plvar =
33 {
34 .nargs = 0,
35 .operation = var,
36 };
37 static const struct expression plone =
38 {
39 .nargs = 0,
40 .operation = num,
41 .val =
42 {
43 .num = 1
44 }
45 };
46 const struct expression GERMANIC_PLURAL =
47 {
48 .nargs = 2,
49 .operation = not_equal,
50 .val =
51 {
52 .args =
53 {
54 [0] = (struct expression *) &plvar,
55 [1] = (struct expression *) &plone
56 }
57 }
58 };
59
60 # define INIT_GERMANIC_PLURAL()
61
62 #else
63
64 /* For compilers without support for ISO C 99 struct/union initializers:
65 Initialization at run-time. */
66
67 static struct expression plvar;
68 static struct expression plone;
69 struct expression GERMANIC_PLURAL;
70
71 static void
init_germanic_plural(void)72 init_germanic_plural (void)
73 {
74 if (plone.val.num == 0)
75 {
76 plvar.nargs = 0;
77 plvar.operation = var;
78
79 plone.nargs = 0;
80 plone.operation = num;
81 plone.val.num = 1;
82
83 GERMANIC_PLURAL.nargs = 2;
84 GERMANIC_PLURAL.operation = not_equal;
85 GERMANIC_PLURAL.val.args[0] = &plvar;
86 GERMANIC_PLURAL.val.args[1] = &plone;
87 }
88 }
89
90 # define INIT_GERMANIC_PLURAL() init_germanic_plural ()
91
92 #endif
93
94 void
95 internal_function
EXTRACT_PLURAL_EXPRESSION(const char * nullentry,const struct expression ** pluralp,unsigned long int * npluralsp)96 EXTRACT_PLURAL_EXPRESSION (const char *nullentry,
97 const struct expression **pluralp,
98 unsigned long int *npluralsp)
99 {
100 if (nullentry != NULL)
101 {
102 const char *plural;
103 const char *nplurals;
104
105 plural = strstr (nullentry, "plural=");
106 nplurals = strstr (nullentry, "nplurals=");
107 if (plural == NULL || nplurals == NULL)
108 goto no_plural;
109 else
110 {
111 char *endp;
112 unsigned long int n;
113 struct parse_args args;
114
115 /* First get the number. */
116 nplurals += 9;
117 while (*nplurals != '\0' && isspace ((unsigned char) *nplurals))
118 ++nplurals;
119 if (!(*nplurals >= '0' && *nplurals <= '9'))
120 goto no_plural;
121 #if defined HAVE_STRTOUL || defined _LIBC
122 n = strtoul (nplurals, &endp, 10);
123 #else
124 for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++)
125 n = n * 10 + (*endp - '0');
126 #endif
127 if (nplurals == endp)
128 goto no_plural;
129 *npluralsp = n;
130
131 /* Due to the restrictions bison imposes onto the interface of the
132 scanner function we have to put the input string and the result
133 passed up from the parser into the same structure which address
134 is passed down to the parser. */
135 plural += 7;
136 args.cp = plural;
137 if (PLURAL_PARSE (&args) != 0)
138 goto no_plural;
139 *pluralp = args.res;
140 }
141 }
142 else
143 {
144 /* By default we are using the Germanic form: singular form only
145 for `one', the plural form otherwise. Yes, this is also what
146 English is using since English is a Germanic language. */
147 no_plural:
148 INIT_GERMANIC_PLURAL ();
149 *pluralp = &GERMANIC_PLURAL;
150 *npluralsp = 2;
151 }
152 }
153