| 1 |
/* Input routines. |
| 2 |
Copyright (C) 1989-1998, 2002-2004, 2011, 2017-2018, 2025 Free Software Foundation, Inc. |
| 3 |
Written by Douglas C. Schmidt <[email protected]> |
| 4 |
and Bruno Haible <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU GPERF. |
| 7 |
|
| 8 |
This program 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 |
This program 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 this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
/* Specification. */ |
| 24 |
#include "input.h" |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> /* declares exit(), free() */ |
| 28 |
#include <string.h> /* declares strncpy(), strchr() */ |
| 29 |
#include <limits.h> /* defines UCHAR_MAX etc. */ |
| 30 |
#include "options.h" |
| 31 |
#include "read-file.h" |
| 32 |
|
| 33 |
template <class KT> |
| 34 |
Input<KT>::Input (FILE *stream, Keyword_Factory<KT> *keyword_factory) |
| 35 |
: _stream (stream), _factory (keyword_factory) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
/* Returns a pretty representation of the input file name, for error and |
| 40 |
warning messages. */ |
| 41 |
static const char * |
| 42 |
pretty_input_file_name () |
| 43 |
{ |
| 44 |
if (option.get_input_file_name ()) |
| 45 |
return option.get_input_file_name (); |
| 46 |
else |
| 47 |
return "(standard input)"; |
| 48 |
} |
| 49 |
|
| 50 |
/* Returns true if the given line contains a "%DECL" declaration. */ |
| 51 |
static bool |
| 52 |
is_declaration (const char *line, const char *line_end, unsigned int lineno, |
| 53 |
const char *decl) |
| 54 |
{ |
| 55 |
/* Skip '%'. */ |
| 56 |
line++; |
| 57 |
|
| 58 |
/* Skip DECL. */ |
| 59 |
for (const char *d = decl; *d; d++) |
| 60 |
{ |
| 61 |
if (!(line < line_end)) |
| 62 |
return false; |
| 63 |
if (!(*line == *d || (*d == '-' && *line == '_'))) |
| 64 |
return false; |
| 65 |
line++; |
| 66 |
} |
| 67 |
if (line < line_end |
| 68 |
&& ((*line >= 'A' && *line <= 'Z') |
| 69 |
|| (*line >= 'a' && *line <= 'z') |
| 70 |
|| *line == '-' || *line == '_')) |
| 71 |
return false; |
| 72 |
|
| 73 |
/* OK, found DECL. */ |
| 74 |
|
| 75 |
/* Skip whitespace. */ |
| 76 |
while (line < line_end && (*line == ' ' || *line == '\t')) |
| 77 |
line++; |
| 78 |
|
| 79 |
/* Expect end of line. */ |
| 80 |
if (line < line_end && *line != '\n') |
| 81 |
{ |
| 82 |
fprintf (stderr, "%s:%u: junk after declaration\n", |
| 83 |
pretty_input_file_name (), lineno); |
| 84 |
exit (1); |
| 85 |
} |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
/* Tests if the given line contains a "%DECL=ARG" declaration. |
| 91 |
If yes, it sets *ARGP to the argument, and returns true. |
| 92 |
Otherwise, it returns false. */ |
| 93 |
static bool |
| 94 |
is_declaration_with_arg (const char *line, const char *line_end, |
| 95 |
unsigned int lineno, |
| 96 |
const char *decl, char **argp) |
| 97 |
{ |
| 98 |
/* Skip '%'. */ |
| 99 |
line++; |
| 100 |
|
| 101 |
/* Skip DECL. */ |
| 102 |
for (const char *d = decl; *d; d++) |
| 103 |
{ |
| 104 |
if (!(line < line_end)) |
| 105 |
return false; |
| 106 |
if (!(*line == *d || (*d == '-' && *line == '_'))) |
| 107 |
return false; |
| 108 |
line++; |
| 109 |
} |
| 110 |
if (line < line_end |
| 111 |
&& ((*line >= 'A' && *line <= 'Z') |
| 112 |
|| (*line >= 'a' && *line <= 'z') |
| 113 |
|| *line == '-' || *line == '_')) |
| 114 |
return false; |
| 115 |
|
| 116 |
/* OK, found DECL. */ |
| 117 |
|
| 118 |
/* Skip '='. */ |
| 119 |
if (!(line < line_end && *line == '=')) |
| 120 |
{ |
| 121 |
fprintf (stderr, "%s:%u: missing argument in %%%s=ARG declaration.\n", |
| 122 |
pretty_input_file_name (), lineno, decl); |
| 123 |
exit (1); |
| 124 |
} |
| 125 |
line++; |
| 126 |
|
| 127 |
/* The next word is the argument. */ |
| 128 |
char *arg = new char[line_end - line + 1]; |
| 129 |
char *p = arg; |
| 130 |
while (line < line_end && !(*line == ' ' || *line == '\t' || *line == '\n')) |
| 131 |
*p++ = *line++; |
| 132 |
*p = '\0'; |
| 133 |
|
| 134 |
/* Skip whitespace. */ |
| 135 |
while (line < line_end && (*line == ' ' || *line == '\t')) |
| 136 |
line++; |
| 137 |
|
| 138 |
/* Expect end of line. */ |
| 139 |
if (line < line_end && *line != '\n') |
| 140 |
{ |
| 141 |
fprintf (stderr, "%s:%u: junk after declaration\n", |
| 142 |
pretty_input_file_name (), lineno); |
| 143 |
exit (1); |
| 144 |
} |
| 145 |
|
| 146 |
*argp = arg; |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
/* Tests if the given line contains a "%define DECL ARG" declaration. |
| 151 |
If yes, it sets *ARGP to the argument, and returns true. |
| 152 |
Otherwise, it returns false. */ |
| 153 |
static bool |
| 154 |
is_define_declaration (const char *line, const char *line_end, |
| 155 |
unsigned int lineno, |
| 156 |
const char *decl, char **argp) |
| 157 |
{ |
| 158 |
/* Skip '%'. */ |
| 159 |
line++; |
| 160 |
|
| 161 |
/* Skip "define". */ |
| 162 |
{ |
| 163 |
for (const char *d = "define"; *d; d++) |
| 164 |
{ |
| 165 |
if (!(line < line_end)) |
| 166 |
return false; |
| 167 |
if (!(*line == *d)) |
| 168 |
return false; |
| 169 |
line++; |
| 170 |
} |
| 171 |
if (!(line < line_end && (*line == ' ' || *line == '\t'))) |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
/* Skip whitespace. */ |
| 176 |
while (line < line_end && (*line == ' ' || *line == '\t')) |
| 177 |
line++; |
| 178 |
|
| 179 |
/* Skip DECL. */ |
| 180 |
for (const char *d = decl; *d; d++) |
| 181 |
{ |
| 182 |
if (!(line < line_end)) |
| 183 |
return false; |
| 184 |
if (!(*line == *d || (*d == '-' && *line == '_'))) |
| 185 |
return false; |
| 186 |
line++; |
| 187 |
} |
| 188 |
if (line < line_end |
| 189 |
&& ((*line >= 'A' && *line <= 'Z') |
| 190 |
|| (*line >= 'a' && *line <= 'z') |
| 191 |
|| *line == '-' || *line == '_')) |
| 192 |
return false; |
| 193 |
|
| 194 |
/* OK, found DECL. */ |
| 195 |
|
| 196 |
/* Skip whitespace. */ |
| 197 |
if (!(line < line_end && (*line == ' ' || *line == '\t'))) |
| 198 |
{ |
| 199 |
fprintf (stderr, "%s:%u:" |
| 200 |
" missing argument in %%define %s ARG declaration.\n", |
| 201 |
pretty_input_file_name (), lineno, decl); |
| 202 |
exit (1); |
| 203 |
} |
| 204 |
do |
| 205 |
line++; |
| 206 |
while (line < line_end && (*line == ' ' || *line == '\t')); |
| 207 |
|
| 208 |
/* The next word is the argument. */ |
| 209 |
char *arg = new char[line_end - line + 1]; |
| 210 |
char *p = arg; |
| 211 |
while (line < line_end && !(*line == ' ' || *line == '\t' || *line == '\n')) |
| 212 |
*p++ = *line++; |
| 213 |
*p = '\0'; |
| 214 |
|
| 215 |
/* Skip whitespace. */ |
| 216 |
while (line < line_end && (*line == ' ' || *line == '\t')) |
| 217 |
line++; |
| 218 |
|
| 219 |
/* Expect end of line. */ |
| 220 |
if (line < line_end && *line != '\n') |
| 221 |
{ |
| 222 |
fprintf (stderr, "%s:%u: junk after declaration\n", |
| 223 |
pretty_input_file_name (), lineno); |
| 224 |
exit (1); |
| 225 |
} |
| 226 |
|
| 227 |
*argp = arg; |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
/* Reads the entire input file. */ |
| 232 |
template <class KT> |
| 233 |
void |
| 234 |
Input<KT>::read_input () |
| 235 |
{ |
| 236 |
/* The input file has the following structure: |
| 237 |
DECLARATIONS |
| 238 |
%% |
| 239 |
KEYWORDS |
| 240 |
%% |
| 241 |
ADDITIONAL_CODE |
| 242 |
Since the DECLARATIONS and the ADDITIONAL_CODE sections are optional, |
| 243 |
we have to read the entire file in the case there is only one %% |
| 244 |
separator line, in order to determine whether the structure is |
| 245 |
DECLARATIONS |
| 246 |
%% |
| 247 |
KEYWORDS |
| 248 |
or |
| 249 |
KEYWORDS |
| 250 |
%% |
| 251 |
ADDITIONAL_CODE |
| 252 |
When the option -t is given or when the first section contains |
| 253 |
declaration lines starting with %, we go for the first interpretation, |
| 254 |
otherwise for the second interpretation. */ |
| 255 |
|
| 256 |
size_t input_length; |
| 257 |
char *input = fread_file (_stream, 0, &input_length); |
| 258 |
if (input == NULL) |
| 259 |
{ |
| 260 |
if (ferror (_stream)) |
| 261 |
fprintf (stderr, "%s: error while reading input file\n", |
| 262 |
pretty_input_file_name ()); |
| 263 |
else |
| 264 |
fprintf (stderr, "%s: The input file is empty!\n", |
| 265 |
pretty_input_file_name ()); |
| 266 |
exit (1); |
| 267 |
} |
| 268 |
|
| 269 |
/* Convert CR/LF line terminators (Windows) to LF line terminators (Unix). |
| 270 |
GCC 3.3 and newer support CR/LF line terminators in C sources on Unix, |
| 271 |
so we do the same. |
| 272 |
The so-called "text mode" in stdio on Windows translates CR/LF to \n |
| 273 |
automatically, but here we also need this conversion on Unix. As a side |
| 274 |
effect, on Windows we also parse CR/CR/LF into a single \n, but this |
| 275 |
is not a problem. */ |
| 276 |
{ |
| 277 |
char *p = input; |
| 278 |
char *p_end = input + input_length; |
| 279 |
/* Converting the initial segment without CRs is a no-op. */ |
| 280 |
while (p < p_end && *p != '\r') |
| 281 |
p++; |
| 282 |
/* Then start the conversion for real. */ |
| 283 |
char *q = p; |
| 284 |
while (p < p_end) |
| 285 |
{ |
| 286 |
if (p[0] == '\r' && p + 1 < p_end && p[1] == '\n') |
| 287 |
p++; |
| 288 |
*q++ = *p++; |
| 289 |
} |
| 290 |
input_length = q - input; |
| 291 |
} |
| 292 |
|
| 293 |
/* We use input_end as a limit, in order to cope with NUL bytes in the |
| 294 |
input. But note that one trailing NUL byte has been added after |
| 295 |
input_end, for convenience. */ |
| 296 |
char *input_end = input + input_length; |
| 297 |
|
| 298 |
const char *declarations; |
| 299 |
const char *declarations_end; |
| 300 |
const char *keywords; |
| 301 |
const char *keywords_end; |
| 302 |
unsigned int keywords_lineno; |
| 303 |
|
| 304 |
/* Break up the input into the three sections. */ |
| 305 |
{ |
| 306 |
const char *separator[2] = { NULL, NULL }; |
| 307 |
unsigned int separator_lineno[2] = { 0, 0 }; |
| 308 |
int separators = 0; |
| 309 |
{ |
| 310 |
unsigned int lineno = 1; |
| 311 |
for (const char *p = input; p < input_end; ) |
| 312 |
{ |
| 313 |
if (p[0] == '%' && p[1] == '%') |
| 314 |
{ |
| 315 |
separator[separators] = p; |
| 316 |
separator_lineno[separators] = lineno; |
| 317 |
if (++separators == 2) |
| 318 |
break; |
| 319 |
} |
| 320 |
lineno++; |
| 321 |
p = (const char *) memchr (p, '\n', input_end - p); |
| 322 |
if (p != NULL) |
| 323 |
p++; |
| 324 |
else |
| 325 |
p = input_end; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
bool has_declarations; |
| 330 |
if (separators == 1) |
| 331 |
{ |
| 332 |
if (option[TYPE]) |
| 333 |
has_declarations = true; |
| 334 |
else |
| 335 |
{ |
| 336 |
has_declarations = false; |
| 337 |
for (const char *p = input; p < separator[0]; ) |
| 338 |
{ |
| 339 |
if (p[0] == '%') |
| 340 |
{ |
| 341 |
has_declarations = true; |
| 342 |
break; |
| 343 |
} |
| 344 |
p = (const char *) memchr (p, '\n', separator[0] - p); |
| 345 |
if (p != NULL) |
| 346 |
p++; |
| 347 |
else |
| 348 |
p = separator[0]; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
else |
| 353 |
has_declarations = (separators > 0); |
| 354 |
|
| 355 |
if (has_declarations) |
| 356 |
{ |
| 357 |
declarations = input; |
| 358 |
declarations_end = separator[0]; |
| 359 |
/* Give a warning if the separator line is nonempty. */ |
| 360 |
bool nonempty_line = false; |
| 361 |
const char *p; |
| 362 |
for (p = declarations_end + 2; p < input_end; ) |
| 363 |
{ |
| 364 |
if (*p == '\n') |
| 365 |
{ |
| 366 |
p++; |
| 367 |
break; |
| 368 |
} |
| 369 |
if (!(*p == ' ' || *p == '\t')) |
| 370 |
nonempty_line = true; |
| 371 |
p++; |
| 372 |
} |
| 373 |
if (nonempty_line) |
| 374 |
fprintf (stderr, "%s:%u: warning: junk after %%%% is ignored\n", |
| 375 |
pretty_input_file_name (), separator_lineno[0]); |
| 376 |
keywords = p; |
| 377 |
keywords_lineno = separator_lineno[0] + 1; |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
declarations = NULL; |
| 382 |
declarations_end = NULL; |
| 383 |
keywords = input; |
| 384 |
keywords_lineno = 1; |
| 385 |
} |
| 386 |
|
| 387 |
if (separators > (has_declarations ? 1 : 0)) |
| 388 |
{ |
| 389 |
keywords_end = separator[separators-1]; |
| 390 |
_verbatim_code = separator[separators-1] + 2; |
| 391 |
_verbatim_code_end = input_end; |
| 392 |
_verbatim_code_lineno = separator_lineno[separators-1]; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
keywords_end = input_end; |
| 397 |
_verbatim_code = NULL; |
| 398 |
_verbatim_code_end = NULL; |
| 399 |
_verbatim_code_lineno = 0; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/* Parse the declarations section. */ |
| 404 |
|
| 405 |
_verbatim_declarations = NULL; |
| 406 |
_verbatim_declarations_end = NULL; |
| 407 |
_verbatim_declarations_lineno = 0; |
| 408 |
_struct_decl = NULL; |
| 409 |
_struct_decl_lineno = 0; |
| 410 |
_return_type = NULL; |
| 411 |
_struct_tag = NULL; |
| 412 |
{ |
| 413 |
unsigned int lineno = 1; |
| 414 |
char *struct_decl = NULL; |
| 415 |
unsigned int *struct_decl_linenos = NULL; |
| 416 |
unsigned int struct_decl_linecount = 0; |
| 417 |
for (const char *line = declarations; line < declarations_end; ) |
| 418 |
{ |
| 419 |
const char *line_end; |
| 420 |
line_end = (const char *) memchr (line, '\n', declarations_end - line); |
| 421 |
if (line_end != NULL) |
| 422 |
line_end++; |
| 423 |
else |
| 424 |
line_end = declarations_end; |
| 425 |
|
| 426 |
if (*line == '%') |
| 427 |
{ |
| 428 |
if (line[1] == '{') |
| 429 |
{ |
| 430 |
/* Handle %{. */ |
| 431 |
if (_verbatim_declarations != NULL) |
| 432 |
{ |
| 433 |
fprintf (stderr, "%s:%u:\n%s:%u:" |
| 434 |
" only one %%{...%%} section is allowed\n", |
| 435 |
pretty_input_file_name (), |
| 436 |
_verbatim_declarations_lineno, |
| 437 |
pretty_input_file_name (), lineno); |
| 438 |
exit (1); |
| 439 |
} |
| 440 |
_verbatim_declarations = line + 2; |
| 441 |
_verbatim_declarations_lineno = lineno; |
| 442 |
} |
| 443 |
else if (line[1] == '}') |
| 444 |
{ |
| 445 |
/* Handle %}. */ |
| 446 |
if (_verbatim_declarations == NULL) |
| 447 |
{ |
| 448 |
fprintf (stderr, "%s:%u:" |
| 449 |
" %%} outside of %%{...%%} section\n", |
| 450 |
pretty_input_file_name (), lineno); |
| 451 |
exit (1); |
| 452 |
} |
| 453 |
if (_verbatim_declarations_end != NULL) |
| 454 |
{ |
| 455 |
fprintf (stderr, "%s:%u:" |
| 456 |
" %%{...%%} section already closed\n", |
| 457 |
pretty_input_file_name (), lineno); |
| 458 |
exit (1); |
| 459 |
} |
| 460 |
_verbatim_declarations_end = line; |
| 461 |
/* Give a warning if the rest of the line is nonempty. */ |
| 462 |
bool nonempty_line = false; |
| 463 |
const char *q; |
| 464 |
for (q = line + 2; q < line_end; q++) |
| 465 |
{ |
| 466 |
if (*q == '\n') |
| 467 |
{ |
| 468 |
q++; |
| 469 |
break; |
| 470 |
} |
| 471 |
if (!(*q == ' ' || *q == '\t')) |
| 472 |
nonempty_line = true; |
| 473 |
} |
| 474 |
if (nonempty_line) |
| 475 |
fprintf (stderr, "%s:%u:" |
| 476 |
" warning: junk after %%} is ignored\n", |
| 477 |
pretty_input_file_name (), lineno); |
| 478 |
} |
| 479 |
else if (_verbatim_declarations != NULL |
| 480 |
&& _verbatim_declarations_end == NULL) |
| 481 |
{ |
| 482 |
fprintf (stderr, "%s:%u:" |
| 483 |
" warning: %% directives are ignored" |
| 484 |
" inside the %%{...%%} section\n", |
| 485 |
pretty_input_file_name (), lineno); |
| 486 |
} |
| 487 |
else |
| 488 |
{ |
| 489 |
char *arg; |
| 490 |
|
| 491 |
if (is_declaration_with_arg (line, line_end, lineno, |
| 492 |
"delimiters", &arg)) |
| 493 |
option.set_delimiters (arg); |
| 494 |
else |
| 495 |
|
| 496 |
if (is_declaration (line, line_end, lineno, "struct-type")) |
| 497 |
option.set (TYPE); |
| 498 |
else |
| 499 |
|
| 500 |
if (is_declaration (line, line_end, lineno, "ignore-case")) |
| 501 |
option.set (UPPERLOWER); |
| 502 |
else |
| 503 |
|
| 504 |
if (is_declaration_with_arg (line, line_end, lineno, |
| 505 |
"language", &arg)) |
| 506 |
option.set_language (arg); |
| 507 |
else |
| 508 |
|
| 509 |
if (is_define_declaration (line, line_end, lineno, |
| 510 |
"slot-name", &arg)) |
| 511 |
option.set_slot_name (arg); |
| 512 |
else |
| 513 |
|
| 514 |
if (is_define_declaration (line, line_end, lineno, |
| 515 |
"initializer-suffix", &arg)) |
| 516 |
option.set_initializer_suffix (arg); |
| 517 |
else |
| 518 |
|
| 519 |
if (is_define_declaration (line, line_end, lineno, |
| 520 |
"hash-function-name", &arg)) |
| 521 |
option.set_hash_name (arg); |
| 522 |
else |
| 523 |
|
| 524 |
if (is_define_declaration (line, line_end, lineno, |
| 525 |
"lookup-function-name", &arg)) |
| 526 |
option.set_function_name (arg); |
| 527 |
else |
| 528 |
|
| 529 |
if (is_define_declaration (line, line_end, lineno, |
| 530 |
"class-name", &arg)) |
| 531 |
option.set_class_name (arg); |
| 532 |
else |
| 533 |
|
| 534 |
if (is_declaration (line, line_end, lineno, "7bit")) |
| 535 |
option.set (SEVENBIT); |
| 536 |
else |
| 537 |
|
| 538 |
if (is_declaration (line, line_end, lineno, "compare-lengths")) |
| 539 |
option.set (LENTABLE); |
| 540 |
else |
| 541 |
|
| 542 |
if (is_declaration (line, line_end, lineno, "compare-strncmp")) |
| 543 |
option.set (COMP); |
| 544 |
else |
| 545 |
|
| 546 |
if (is_declaration (line, line_end, lineno, "readonly-tables")) |
| 547 |
option.set (CONST); |
| 548 |
else |
| 549 |
|
| 550 |
if (is_declaration (line, line_end, lineno, "enum")) |
| 551 |
option.set (ENUM); |
| 552 |
else |
| 553 |
|
| 554 |
if (is_declaration (line, line_end, lineno, "includes")) |
| 555 |
option.set (INCLUDE); |
| 556 |
else |
| 557 |
|
| 558 |
if (is_declaration (line, line_end, lineno, "global-table")) |
| 559 |
option.set (GLOBAL); |
| 560 |
else |
| 561 |
|
| 562 |
if (is_declaration (line, line_end, lineno, "pic")) |
| 563 |
option.set (SHAREDLIB); |
| 564 |
else |
| 565 |
|
| 566 |
if (is_define_declaration (line, line_end, lineno, |
| 567 |
"string-pool-name", &arg)) |
| 568 |
option.set_stringpool_name (arg); |
| 569 |
else |
| 570 |
|
| 571 |
if (is_declaration (line, line_end, lineno, "null-strings")) |
| 572 |
option.set (NULLSTRINGS); |
| 573 |
else |
| 574 |
|
| 575 |
if (is_define_declaration (line, line_end, lineno, |
| 576 |
"constants-prefix", &arg)) |
| 577 |
option.set_constants_prefix (arg); |
| 578 |
else |
| 579 |
|
| 580 |
if (is_define_declaration (line, line_end, lineno, |
| 581 |
"word-array-name", &arg)) |
| 582 |
option.set_wordlist_name (arg); |
| 583 |
else |
| 584 |
|
| 585 |
if (is_define_declaration (line, line_end, lineno, |
| 586 |
"length-table-name", &arg)) |
| 587 |
option.set_lengthtable_name (arg); |
| 588 |
else |
| 589 |
|
| 590 |
if (is_declaration_with_arg (line, line_end, lineno, |
| 591 |
"switch", &arg)) |
| 592 |
{ |
| 593 |
option.set_total_switches (atoi (arg)); |
| 594 |
if (option.get_total_switches () <= 0) |
| 595 |
{ |
| 596 |
fprintf (stderr, "%s:%u: number of switches %s" |
| 597 |
" must be a positive number\n", |
| 598 |
pretty_input_file_name (), lineno, arg); |
| 599 |
exit (1); |
| 600 |
} |
| 601 |
} |
| 602 |
else |
| 603 |
|
| 604 |
if (is_declaration (line, line_end, lineno, "omit-struct-type")) |
| 605 |
option.set (NOTYPE); |
| 606 |
else |
| 607 |
|
| 608 |
{ |
| 609 |
fprintf (stderr, "%s:%u: unrecognized %% directive\n", |
| 610 |
pretty_input_file_name (), lineno); |
| 611 |
exit (1); |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
else if (!(_verbatim_declarations != NULL |
| 616 |
&& _verbatim_declarations_end == NULL)) |
| 617 |
{ |
| 618 |
/* Append the line to struct_decl. */ |
| 619 |
size_t old_len = (struct_decl ? strlen (struct_decl) : 0); |
| 620 |
size_t line_len = line_end - line; |
| 621 |
size_t new_len = old_len + line_len + 1; |
| 622 |
char *new_struct_decl = new char[new_len]; |
| 623 |
if (old_len > 0) |
| 624 |
memcpy (new_struct_decl, struct_decl, old_len); |
| 625 |
memcpy (new_struct_decl + old_len, line, line_len); |
| 626 |
new_struct_decl[old_len + line_len] = '\0'; |
| 627 |
if (struct_decl) |
| 628 |
delete[] struct_decl; |
| 629 |
struct_decl = new_struct_decl; |
| 630 |
/* Append the lineno to struct_decl_linenos. */ |
| 631 |
unsigned int *new_struct_decl_linenos = |
| 632 |
new unsigned int[struct_decl_linecount + 1]; |
| 633 |
if (struct_decl_linecount > 0) |
| 634 |
memcpy (new_struct_decl_linenos, struct_decl_linenos, |
| 635 |
struct_decl_linecount * sizeof (unsigned int)); |
| 636 |
new_struct_decl_linenos[struct_decl_linecount] = lineno; |
| 637 |
if (struct_decl_linenos) |
| 638 |
delete[] struct_decl_linenos; |
| 639 |
struct_decl_linenos = new_struct_decl_linenos; |
| 640 |
/* Increment struct_decl_linecount. */ |
| 641 |
struct_decl_linecount++; |
| 642 |
} |
| 643 |
lineno++; |
| 644 |
line = line_end; |
| 645 |
} |
| 646 |
if (_verbatim_declarations != NULL && _verbatim_declarations_end == NULL) |
| 647 |
{ |
| 648 |
fprintf (stderr, "%s:%u: unterminated %%{ section\n", |
| 649 |
pretty_input_file_name (), _verbatim_declarations_lineno); |
| 650 |
exit (1); |
| 651 |
} |
| 652 |
|
| 653 |
/* Determine _struct_decl, _return_type, _struct_tag. */ |
| 654 |
if (option[TYPE]) |
| 655 |
{ |
| 656 |
if (struct_decl) |
| 657 |
{ |
| 658 |
/* Drop leading whitespace and comments. */ |
| 659 |
{ |
| 660 |
char *p = struct_decl; |
| 661 |
unsigned int *l = struct_decl_linenos; |
| 662 |
for (;;) |
| 663 |
{ |
| 664 |
if (p[0] == ' ' || p[0] == '\t') |
| 665 |
{ |
| 666 |
p++; |
| 667 |
continue; |
| 668 |
} |
| 669 |
if (p[0] == '\n') |
| 670 |
{ |
| 671 |
l++; |
| 672 |
p++; |
| 673 |
continue; |
| 674 |
} |
| 675 |
if (p[0] == '/') |
| 676 |
{ |
| 677 |
if (p[1] == '*') |
| 678 |
{ |
| 679 |
/* Skip over ANSI C style comment. */ |
| 680 |
p += 2; |
| 681 |
while (p[0] != '\0') |
| 682 |
{ |
| 683 |
if (p[0] == '*' && p[1] == '/') |
| 684 |
{ |
| 685 |
p += 2; |
| 686 |
break; |
| 687 |
} |
| 688 |
if (p[0] == '\n') |
| 689 |
l++; |
| 690 |
p++; |
| 691 |
} |
| 692 |
continue; |
| 693 |
} |
| 694 |
if (p[1] == '/') |
| 695 |
{ |
| 696 |
/* Skip over ISO C99 or C++ style comment. */ |
| 697 |
p += 2; |
| 698 |
while (p[0] != '\0' && p[0] != '\n') |
| 699 |
p++; |
| 700 |
if (p[0] == '\n') |
| 701 |
{ |
| 702 |
l++; |
| 703 |
p++; |
| 704 |
} |
| 705 |
continue; |
| 706 |
} |
| 707 |
} |
| 708 |
break; |
| 709 |
} |
| 710 |
if (p != struct_decl) |
| 711 |
{ |
| 712 |
size_t len = strlen (p); |
| 713 |
char *new_struct_decl = new char[len + 1]; |
| 714 |
memcpy (new_struct_decl, p, len + 1); |
| 715 |
delete[] struct_decl; |
| 716 |
struct_decl = new_struct_decl; |
| 717 |
} |
| 718 |
_struct_decl_lineno = *l; |
| 719 |
} |
| 720 |
/* Drop trailing whitespace. */ |
| 721 |
for (char *p = struct_decl + strlen (struct_decl); p > struct_decl;) |
| 722 |
if (p[-1] == '\n' || p[-1] == ' ' || p[-1] == '\t') |
| 723 |
*--p = '\0'; |
| 724 |
else |
| 725 |
break; |
| 726 |
} |
| 727 |
if (struct_decl == NULL || struct_decl[0] == '\0') |
| 728 |
{ |
| 729 |
fprintf (stderr, "%s: missing struct declaration" |
| 730 |
" for option --struct-type\n", |
| 731 |
pretty_input_file_name ()); |
| 732 |
exit (1); |
| 733 |
} |
| 734 |
{ |
| 735 |
/* Ensure trailing semicolon. */ |
| 736 |
size_t old_len = strlen (struct_decl); |
| 737 |
if (struct_decl[old_len - 1] != ';') |
| 738 |
{ |
| 739 |
char *new_struct_decl = new char[old_len + 2]; |
| 740 |
memcpy (new_struct_decl, struct_decl, old_len); |
| 741 |
new_struct_decl[old_len] = ';'; |
| 742 |
new_struct_decl[old_len + 1] = '\0'; |
| 743 |
delete[] struct_decl; |
| 744 |
struct_decl = new_struct_decl; |
| 745 |
} |
| 746 |
} |
| 747 |
/* Set _struct_decl to the entire declaration. */ |
| 748 |
_struct_decl = struct_decl; |
| 749 |
/* Set _struct_tag to the naked "struct something". */ |
| 750 |
const char *p; |
| 751 |
for (p = struct_decl; *p && *p != '{' && *p != ';' && *p != '\n'; p++) |
| 752 |
; |
| 753 |
for (; p > struct_decl;) |
| 754 |
if (p[-1] == '\n' || p[-1] == ' ' || p[-1] == '\t') |
| 755 |
--p; |
| 756 |
else |
| 757 |
break; |
| 758 |
size_t struct_tag_length = p - struct_decl; |
| 759 |
char *struct_tag = new char[struct_tag_length + 1]; |
| 760 |
memcpy (struct_tag, struct_decl, struct_tag_length); |
| 761 |
struct_tag[struct_tag_length] = '\0'; |
| 762 |
_struct_tag = struct_tag; |
| 763 |
/* The return type of the lookup function is "struct something *". |
| 764 |
No "const" here, because if !option[CONST], some user code might |
| 765 |
want to modify the structure. */ |
| 766 |
char *return_type = new char[struct_tag_length + 3]; |
| 767 |
memcpy (return_type, struct_decl, struct_tag_length); |
| 768 |
return_type[struct_tag_length] = ' '; |
| 769 |
return_type[struct_tag_length + 1] = '*'; |
| 770 |
return_type[struct_tag_length + 2] = '\0'; |
| 771 |
_return_type = return_type; |
| 772 |
} |
| 773 |
|
| 774 |
if (struct_decl_linenos) |
| 775 |
delete[] struct_decl_linenos; |
| 776 |
} |
| 777 |
|
| 778 |
/* Parse the keywords section. */ |
| 779 |
{ |
| 780 |
Keyword_List<KT> **list_tail = &_head; |
| 781 |
const char *delimiters = option.get_delimiters (); |
| 782 |
unsigned int lineno = keywords_lineno; |
| 783 |
bool charset_dependent = false; |
| 784 |
for (const char *line = keywords; line < keywords_end; ) |
| 785 |
{ |
| 786 |
const char *line_end; |
| 787 |
line_end = (const char *) memchr (line, '\n', keywords_end - line); |
| 788 |
if (line_end != NULL) |
| 789 |
line_end++; |
| 790 |
else |
| 791 |
line_end = keywords_end; |
| 792 |
|
| 793 |
if (line[0] == '#') |
| 794 |
; /* Comment line. */ |
| 795 |
else if (line[0] == '%') |
| 796 |
{ |
| 797 |
fprintf (stderr, "%s:%u:" |
| 798 |
" declarations are not allowed in the keywords section.\n" |
| 799 |
"To declare a keyword starting with %%, enclose it in" |
| 800 |
" double-quotes.\n", |
| 801 |
pretty_input_file_name (), lineno); |
| 802 |
exit (1); |
| 803 |
} |
| 804 |
else |
| 805 |
{ |
| 806 |
/* An input line carrying a keyword. */ |
| 807 |
const char *keyword; |
| 808 |
size_t keyword_length; |
| 809 |
const char *rest; |
| 810 |
|
| 811 |
if (line[0] == '"') |
| 812 |
{ |
| 813 |
/* Parse a string in ANSI C syntax. */ |
| 814 |
char *kp = new char[line_end-line]; |
| 815 |
keyword = kp; |
| 816 |
const char *lp = line + 1; |
| 817 |
|
| 818 |
for (;;) |
| 819 |
{ |
| 820 |
if (lp == line_end) |
| 821 |
{ |
| 822 |
fprintf (stderr, "%s:%u: unterminated string\n", |
| 823 |
pretty_input_file_name (), lineno); |
| 824 |
exit (1); |
| 825 |
} |
| 826 |
|
| 827 |
char c = *lp; |
| 828 |
if (c == '\\') |
| 829 |
{ |
| 830 |
c = *++lp; |
| 831 |
switch (c) |
| 832 |
{ |
| 833 |
case '0': case '1': case '2': case '3': |
| 834 |
case '4': case '5': case '6': case '7': |
| 835 |
{ |
| 836 |
int code = 0; |
| 837 |
int count = 0; |
| 838 |
while (count < 3 && *lp >= '0' && *lp <= '7') |
| 839 |
{ |
| 840 |
code = (code << 3) + (*lp - '0'); |
| 841 |
lp++; |
| 842 |
count++; |
| 843 |
} |
| 844 |
if (code > UCHAR_MAX) |
| 845 |
fprintf (stderr, |
| 846 |
"%s:%u: octal escape out of range\n", |
| 847 |
pretty_input_file_name (), lineno); |
| 848 |
*kp = static_cast<char>(code); |
| 849 |
break; |
| 850 |
} |
| 851 |
case 'x': |
| 852 |
{ |
| 853 |
int code = 0; |
| 854 |
int count = 0; |
| 855 |
lp++; |
| 856 |
while ((*lp >= '0' && *lp <= '9') |
| 857 |
|| (*lp >= 'A' && *lp <= 'F') |
| 858 |
|| (*lp >= 'a' && *lp <= 'f')) |
| 859 |
{ |
| 860 |
code = (code << 4) |
| 861 |
+ (*lp >= 'A' && *lp <= 'F' |
| 862 |
? *lp - 'A' + 10 : |
| 863 |
*lp >= 'a' && *lp <= 'f' |
| 864 |
? *lp - 'a' + 10 : |
| 865 |
*lp - '0'); |
| 866 |
lp++; |
| 867 |
count++; |
| 868 |
} |
| 869 |
if (count == 0) |
| 870 |
fprintf (stderr, "%s:%u: hexadecimal escape" |
| 871 |
" without any hex digits\n", |
| 872 |
pretty_input_file_name (), lineno); |
| 873 |
if (code > UCHAR_MAX) |
| 874 |
fprintf (stderr, "%s:%u: hexadecimal escape" |
| 875 |
" out of range\n", |
| 876 |
pretty_input_file_name (), lineno); |
| 877 |
*kp = static_cast<char>(code); |
| 878 |
break; |
| 879 |
} |
| 880 |
case '\\': case '\'': case '"': |
| 881 |
*kp = c; |
| 882 |
lp++; |
| 883 |
charset_dependent = true; |
| 884 |
break; |
| 885 |
case 'n': |
| 886 |
*kp = '\n'; |
| 887 |
lp++; |
| 888 |
charset_dependent = true; |
| 889 |
break; |
| 890 |
case 't': |
| 891 |
*kp = '\t'; |
| 892 |
lp++; |
| 893 |
charset_dependent = true; |
| 894 |
break; |
| 895 |
case 'r': |
| 896 |
*kp = '\r'; |
| 897 |
lp++; |
| 898 |
charset_dependent = true; |
| 899 |
break; |
| 900 |
case 'f': |
| 901 |
*kp = '\f'; |
| 902 |
lp++; |
| 903 |
charset_dependent = true; |
| 904 |
break; |
| 905 |
case 'b': |
| 906 |
*kp = '\b'; |
| 907 |
lp++; |
| 908 |
charset_dependent = true; |
| 909 |
break; |
| 910 |
case 'a': |
| 911 |
*kp = '\a'; |
| 912 |
lp++; |
| 913 |
charset_dependent = true; |
| 914 |
break; |
| 915 |
case 'v': |
| 916 |
*kp = '\v'; |
| 917 |
lp++; |
| 918 |
charset_dependent = true; |
| 919 |
break; |
| 920 |
default: |
| 921 |
fprintf (stderr, "%s:%u: invalid escape sequence" |
| 922 |
" in string\n", |
| 923 |
pretty_input_file_name (), lineno); |
| 924 |
exit (1); |
| 925 |
} |
| 926 |
} |
| 927 |
else if (c == '"') |
| 928 |
break; |
| 929 |
else |
| 930 |
{ |
| 931 |
*kp = c; |
| 932 |
lp++; |
| 933 |
charset_dependent = true; |
| 934 |
} |
| 935 |
kp++; |
| 936 |
} |
| 937 |
lp++; |
| 938 |
if (lp < line_end && *lp != '\n') |
| 939 |
{ |
| 940 |
if (strchr (delimiters, *lp) == NULL) |
| 941 |
{ |
| 942 |
fprintf (stderr, "%s:%u: string not followed" |
| 943 |
" by delimiter\n", |
| 944 |
pretty_input_file_name (), lineno); |
| 945 |
exit (1); |
| 946 |
} |
| 947 |
lp++; |
| 948 |
} |
| 949 |
keyword_length = kp - keyword; |
| 950 |
if (option[TYPE]) |
| 951 |
{ |
| 952 |
char *line_rest = new char[line_end - lp + 1]; |
| 953 |
memcpy (line_rest, lp, line_end - lp); |
| 954 |
line_rest[line_end - lp - |
| 955 |
(line_end > lp && line_end[-1] == '\n' ? 1 : 0)] |
| 956 |
= '\0'; |
| 957 |
rest = line_rest; |
| 958 |
} |
| 959 |
else |
| 960 |
rest = empty_string; |
| 961 |
} |
| 962 |
else |
| 963 |
{ |
| 964 |
/* Not a string. Look for the delimiter. */ |
| 965 |
const char *lp = line; |
| 966 |
for (;;) |
| 967 |
{ |
| 968 |
if (!(lp < line_end && *lp != '\n')) |
| 969 |
{ |
| 970 |
keyword = line; |
| 971 |
keyword_length = lp - line; |
| 972 |
rest = empty_string; |
| 973 |
break; |
| 974 |
} |
| 975 |
if (strchr (delimiters, *lp) != NULL) |
| 976 |
{ |
| 977 |
keyword = line; |
| 978 |
keyword_length = lp - line; |
| 979 |
lp++; |
| 980 |
if (option[TYPE]) |
| 981 |
{ |
| 982 |
char *line_rest = new char[line_end - lp + 1]; |
| 983 |
memcpy (line_rest, lp, line_end - lp); |
| 984 |
line_rest[line_end - lp - |
| 985 |
(line_end > lp && line_end[-1] == '\n' |
| 986 |
? 1 : 0)] |
| 987 |
= '\0'; |
| 988 |
rest = line_rest; |
| 989 |
} |
| 990 |
else |
| 991 |
rest = empty_string; |
| 992 |
break; |
| 993 |
} |
| 994 |
lp++; |
| 995 |
} |
| 996 |
if (keyword_length > 0) |
| 997 |
charset_dependent = true; |
| 998 |
} |
| 999 |
|
| 1000 |
/* Allocate Keyword and add it to the list. */ |
| 1001 |
KT *new_kw = _factory->create_keyword (keyword, keyword_length, |
| 1002 |
rest, lineno); |
| 1003 |
*list_tail = new Keyword_List<KT> (new_kw); |
| 1004 |
list_tail = &(*list_tail)->rest(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
lineno++; |
| 1008 |
line = line_end; |
| 1009 |
} |
| 1010 |
*list_tail = NULL; |
| 1011 |
|
| 1012 |
if (_head == NULL) |
| 1013 |
{ |
| 1014 |
fprintf (stderr, "%s: No keywords in input file!\n", |
| 1015 |
pretty_input_file_name ()); |
| 1016 |
exit (1); |
| 1017 |
} |
| 1018 |
|
| 1019 |
_charset_dependent = charset_dependent; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* To be freed in the destructor. */ |
| 1023 |
_input = input; |
| 1024 |
_input_end = input_end; |
| 1025 |
} |
| 1026 |
|
| 1027 |
template <class KT> |
| 1028 |
Input<KT>::~Input () |
| 1029 |
{ |
| 1030 |
/* Free allocated memory. */ |
| 1031 |
delete[] const_cast<char*>(_return_type); |
| 1032 |
delete[] const_cast<char*>(_struct_tag); |
| 1033 |
delete[] const_cast<char*>(_struct_decl); |
| 1034 |
free (_input); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* ------------------------------------------------------------------------- */ |
| 1038 |
|
| 1039 |
/* Explicit template instantiations. Needed to avoid link-time errors. |
| 1040 |
|
| 1041 |
C++ is just misdesigned: The most important aspect in building large |
| 1042 |
software packages is information hiding. (That's the point of having the |
| 1043 |
implementation of a .h file in a .cc file, isn't it? And of having |
| 1044 |
classes with private fields and methods, isn't it?) The fact that we |
| 1045 |
need the instantiation of the Input<KT> class only for KT = KeywordExt |
| 1046 |
comes from the code in main.cc. It is ugly that implementation details |
| 1047 |
of main.cc have an influence into this file here. */ |
| 1048 |
|
| 1049 |
template class Input<KeywordExt>; |
| 1050 |
|
| 1051 |
/* ------------------------------------------------------------------------- */ |