2 * Copyright (C) 2013 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
36 #ifndef MINIMUM_DATA_SIZE
37 # define MINIMUM_DATA_SIZE 128
39 #ifndef DEFAULT_MAXIMUM_DATA_SIZE
40 # define DEFAULT_MAXIMUM_DATA_SIZE 32768
47 int parse_utf8_config( struct parsing *parsing)
49 char c, q, acc, overflow, escape;
51 const char *value, *head, *end, *skey, *svar, *bvar;
52 size_t lkey, lvar, ldata, datasz;
55 #define iskeybeg(x) (isalpha(x)||(x)=='_')
56 #define iskey(x) (isalnum(x)||(x)=='_')
57 #define atend ((head-end) >= 0)
59 #define pos(x) ((size_t)((x)-parsing->buffer))
60 #define error(p,x) do{ errors++; \
62 && !parsing->error(parsing,pos(p),x)) \
66 /* alloc data buffer */
67 datasz = parsing->maximum_data_size;
69 datasz = DEFAULT_MAXIMUM_DATA_SIZE;
70 else if (datasz < MINIMUM_DATA_SIZE)
71 datasz = MINIMUM_DATA_SIZE;
72 bdata = alloca(datasz);
75 escape = parsing->should_escape ? 1 : 0;
77 head = parsing->buffer;
78 end = head + parsing->length;
85 initial: /* expecting key, comment or ??? */
101 error(head,_("unexpected character while looking to a key start"));
105 comment: /* skipping a comment */
115 key: /* reading a key */
119 error(head,_("end in a key"));
126 error(head,_("unexpected character while looking to ="));
130 overflow = ldata = 0;
136 if (escape && ldata < datasz)
137 bdata[ldata++] = '\\';
139 add_value: /* add the current char to data */
150 value: /* reading the value */
153 /* end in the value */
157 error(head,_("end in quoted value"));
169 error(head,_("end after escape char"));
191 /* begin of a variable, just after $ */
194 error(head,_("end after $"));
202 error(head,_("end after ${"));
208 error(head,_("invalid character after $ or ${"));
215 if (q || !isspace(c))
221 end_of_value: /* end of the value */
224 error(head,_("value too big"));
225 else if (parsing->put)
226 parsing->put( parsing, skey, lkey, bdata, ldata, pos(skey), pos(head));
231 variable: /* read a variable */
236 error(head,_("unmatched pair { }"));
248 error(head,_("unmatched pair { }"));
252 value = parsing->get( parsing, svar,lvar,pos(bvar),pos(head));
254 error(bvar,_("no value for the variable"));
258 bdata[ldata++] = *value++;
279 void parse_utf8_info(
280 struct parsing *parsing,
281 struct parsinfo *info,
285 const char *buf, *begin, *end;
291 buf = begin = end = parsing->buffer;
292 length = parsing->length;
296 /* search the begin of the line */
297 while ((end - buf) != pos) {
299 colno += ((*end & '\xc0') != '\x80');
300 /* dealing with lines */
308 /* search the end of the line */
309 while ((end - buf) < length && *end != '\n')
312 /* record computed values */
315 info->length = end - begin;