1/* plural-exp.c - Expression parsing for plural form selection. */ 2 3/* Copyright (C) 2000, 2001, 2005-2009 Free Software Foundation, Inc. 4 Written by Ulrich Drepper <drepper@cygnus.com>, 2000. 5 6 This file is part of GNU Bash. 7 8 Bash is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 Bash is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with Bash. If not, see <http://www.gnu.org/licenses/>. 20*/ 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". */ 37static const struct expression plvar = 38{ 39 .nargs = 0, 40 .operation = var, 41}; 42static const struct expression plone = 43{ 44 .nargs = 0, 45 .operation = num, 46 .val = 47 { 48 .num = 1 49 } 50}; 51struct 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 72static struct expression plvar; 73static struct expression plone; 74struct expression GERMANIC_PLURAL; 75 76static void 77init_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 99void 100internal_function 101EXTRACT_PLURAL_EXPRESSION (nullentry, pluralp, npluralsp) 102 const char *nullentry; 103 struct expression **pluralp; 104 unsigned long int *npluralsp; 105{ 106 if (nullentry != NULL) 107 { 108 const char *plural; 109 const char *nplurals; 110 111 plural = strstr (nullentry, "plural="); 112 nplurals = strstr (nullentry, "nplurals="); 113 if (plural == NULL || nplurals == NULL) 114 goto no_plural; 115 else 116 { 117 char *endp; 118 unsigned long int n; 119 struct parse_args args; 120 121 /* First get the number. */ 122 nplurals += 9; 123 while (*nplurals != '\0' && isspace ((unsigned char) *nplurals)) 124 ++nplurals; 125 if (!(*nplurals >= '0' && *nplurals <= '9')) 126 goto no_plural; 127#if defined HAVE_STRTOUL || defined _LIBC 128 n = strtoul (nplurals, &endp, 10); 129#else 130 for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++) 131 n = n * 10 + (*endp - '0'); 132#endif 133 if (nplurals == endp) 134 goto no_plural; 135 *npluralsp = n; 136 137 /* Due to the restrictions bison imposes onto the interface of the 138 scanner function we have to put the input string and the result 139 passed up from the parser into the same structure which address 140 is passed down to the parser. */ 141 plural += 7; 142 args.cp = plural; 143 if (PLURAL_PARSE (&args) != 0) 144 goto no_plural; 145 *pluralp = args.res; 146 } 147 } 148 else 149 { 150 /* By default we are using the Germanic form: singular form only 151 for `one', the plural form otherwise. Yes, this is also what 152 English is using since English is a Germanic language. */ 153 no_plural: 154 INIT_GERMANIC_PLURAL (); 155 *pluralp = &GERMANIC_PLURAL; 156 *npluralsp = 2; 157 } 158} 159