Debian Codegraph @

src/options.cc

raw
1 /* Handles parsing the Options provided to the user.
2 Copyright (C) 1989-1998, 2000, 2002-2004, 2006-2009, 2011, 2016-2018, 2022-2023, 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 "options.h"
25
26 #include <stdio.h>
27 #include <stdlib.h> /* declares atoi(), abs(), exit() */
28 #include <string.h> /* declares strcmp() */
29 #include <ctype.h> /* declares isdigit() */
30 #include <limits.h> /* defines CHAR_MAX */
31 #include "filename.h"
32 #include "getopt.h"
33 #include "version.h"
34
35 /* Global option coordinator for the entire program. */
36 Options option;
37
38 /* Records the program name. */
39 const char *program_name;
40
41 /* Size to jump on a collision. */
42 static const int DEFAULT_JUMP_VALUE = 5;
43
44 /* Default name for generated lookup function. */
45 static const char *const DEFAULT_FUNCTION_NAME = "in_word_set";
46
47 /* Default name for the key component. */
48 static const char *const DEFAULT_SLOT_NAME = "name";
49
50 /* Default struct initializer suffix. */
51 static const char *const DEFAULT_INITIALIZER_SUFFIX = "";
52
53 /* Default name for the generated class. */
54 static const char *const DEFAULT_CLASS_NAME = "Perfect_Hash";
55
56 /* Default name for generated hash function. */
57 static const char *const DEFAULT_HASH_NAME = "hash";
58
59 /* Default name for generated hash table array. */
60 static const char *const DEFAULT_WORDLIST_NAME = "wordlist";
61
62 /* Default name for generated length table array. */
63 static const char *const DEFAULT_LENGTHTABLE_NAME = "lengthtable";
64
65 /* Default name for string pool. */
66 static const char *const DEFAULT_STRINGPOOL_NAME = "stringpool";
67
68 /* Default prefix for constants. */
69 static const char *const DEFAULT_CONSTANTS_PREFIX = "";
70
71 /* Default delimiters that separate keywords from their attributes. */
72 static const char *const DEFAULT_DELIMITERS = ",";
73
74 /* Prints program usage to given stream. */
75
76 void
77 Options::short_usage (FILE * stream)
78 {
79 fprintf (stream,
80 "Try '%s --help' for more information.\n", program_name);
81 }
82
83 void
84 Options::long_usage (FILE * stream)
85 {
86 fprintf (stream,
87 "GNU 'gperf' generates perfect hash functions.\n");
88 fprintf (stream, "\n");
89 fprintf (stream,
90 "Usage: %s [OPTION]... [INPUT-FILE]\n",
91 program_name);
92 fprintf (stream, "\n");
93 fprintf (stream,
94 "If a long option shows an argument as mandatory, then it is mandatory\n"
95 "for the equivalent short option also.\n");
96 fprintf (stream, "\n");
97 fprintf (stream,
98 "Output file location:\n");
99 fprintf (stream,
100 " --output-file=FILE Write output to specified file.\n");
101 fprintf (stream,
102 "The results are written to standard output if no output file is specified\n"
103 "or if it is -.\n");
104 fprintf (stream, "\n");
105 fprintf (stream,
106 "Input file interpretation:\n");
107 fprintf (stream,
108 " -e, --delimiters=DELIMITER-LIST\n"
109 " Allow user to provide a string containing delimiters\n"
110 " used to separate keywords from their attributes.\n"
111 " Default is \",\".\n");
112 fprintf (stream,
113 " -t, --struct-type Allows the user to include a structured type\n"
114 " declaration for generated code. Any text before %%%%\n"
115 " is considered part of the type declaration. Key\n"
116 " words and additional fields may follow this, one\n"
117 " group of fields per line.\n");
118 fprintf (stream,
119 " --ignore-case Consider upper and lower case ASCII characters as\n"
120 " equivalent. Note that locale dependent case mappings\n"
121 " are ignored.\n");
122 fprintf (stream, "\n");
123 fprintf (stream,
124 "Language for the output code:\n");
125 fprintf (stream,
126 " -L, --language=LANGUAGE-NAME\n"
127 " Generates code in the specified language. Languages\n"
128 " handled are currently C++, ANSI-C, C, and KR-C. The\n"
129 " default is ANSI-C.\n");
130 fprintf (stream, "\n");
131 fprintf (stream,
132 "Details in the output code:\n");
133 fprintf (stream,
134 " -K, --slot-name=NAME Select name of the keyword component in the keyword\n"
135 " structure.\n");
136 fprintf (stream,
137 " -F, --initializer-suffix=INITIALIZERS\n"
138 " Initializers for additional components in the keyword\n"
139 " structure.\n");
140 fprintf (stream,
141 " -H, --hash-function-name=NAME\n"
142 " Specify name of generated hash function. Default is\n"
143 " 'hash'.\n");
144 fprintf (stream,
145 " -N, --lookup-function-name=NAME\n"
146 " Specify name of generated lookup function. Default\n"
147 " name is 'in_word_set'.\n");
148 fprintf (stream,
149 " -Z, --class-name=NAME Specify name of generated C++ class. Default name is\n"
150 " 'Perfect_Hash'.\n");
151 fprintf (stream,
152 " -7, --seven-bit Assume 7-bit characters.\n");
153 fprintf (stream,
154 " -l, --compare-lengths Compare key lengths before trying a string\n"
155 " comparison. This is necessary if the keywords\n"
156 " contain NUL bytes. It also helps cut down on the\n"
157 " number of string comparisons made during the lookup.\n");
158 fprintf (stream,
159 " -c, --compare-strncmp Generate comparison code using strncmp rather than\n"
160 " strcmp.\n");
161 fprintf (stream,
162 " -C, --readonly-tables Make the contents of generated lookup tables\n"
163 " constant, i.e., readonly.\n");
164 fprintf (stream,
165 " -E, --enum Define constant values using an enum local to the\n"
166 " lookup function rather than with defines.\n");
167 fprintf (stream,
168 " -I, --includes Include the necessary system include file <string.h>\n"
169 " at the beginning of the code.\n");
170 fprintf (stream,
171 " -G, --global-table Generate the static table of keywords as a static\n"
172 " global variable, rather than hiding it inside of the\n"
173 " lookup function (which is the default behavior).\n");
174 fprintf (stream,
175 " -P, --pic Optimize the generated table for inclusion in shared\n"
176 " libraries. This reduces the startup time of programs\n"
177 " using a shared library containing the generated code.\n");
178 fprintf (stream,
179 " -Q, --string-pool-name=NAME\n"
180 " Specify name of string pool generated by option --pic.\n"
181 " Default name is 'stringpool'.\n");
182 fprintf (stream,
183 " --null-strings Use NULL strings instead of empty strings for empty\n"
184 " keyword table entries.\n");
185 fprintf (stream,
186 " --constants-prefix=PREFIX\n"
187 " Specify prefix for the constants like TOTAL_KEYWORDS.\n");
188 fprintf (stream,
189 " -W, --word-array-name=NAME\n"
190 " Specify name of word list array. Default name is\n"
191 " 'wordlist'.\n");
192 fprintf (stream,
193 " --length-table-name=NAME\n"
194 " Specify name of length table array. Default name is\n"
195 " 'lengthtable'.\n");
196 fprintf (stream,
197 " -S, --switch=COUNT Causes the generated C code to use a switch\n"
198 " statement scheme, rather than an array lookup table.\n"
199 " This can lead to a reduction in both time and space\n"
200 " requirements for some keyfiles. The COUNT argument\n"
201 " determines how many switch statements are generated.\n"
202 " A value of 1 generates 1 switch containing all the\n"
203 " elements, a value of 2 generates 2 tables with 1/2\n"
204 " the elements in each table, etc. If COUNT is very\n"
205 " large, say 1000000, the generated C code does a\n"
206 " binary search.\n");
207 fprintf (stream,
208 " -T, --omit-struct-type\n"
209 " Prevents the transfer of the type declaration to the\n"
210 " output file. Use this option if the type is already\n"
211 " defined elsewhere.\n");
212 fprintf (stream, "\n");
213 fprintf (stream,
214 "Algorithm employed by gperf:\n");
215 fprintf (stream,
216 " -k, --key-positions=KEYS\n"
217 " Select the key positions used in the hash function.\n"
218 " The allowable choices range between 1-%d, inclusive.\n"
219 " The positions are separated by commas, ranges may be\n"
220 " used, and key positions may occur in any order.\n"
221 " Also, the meta-character '*' causes the generated\n"
222 " hash function to consider ALL key positions, and $\n"
223 " indicates the \"final character\" of a key, e.g.,\n"
224 " $,1,2,4,6-10.\n",
225 Positions::MAX_KEY_POS);
226 fprintf (stream,
227 " -D, --duplicates Handle keywords that hash to duplicate values. This\n"
228 " is useful for certain highly redundant keyword sets.\n");
229 fprintf (stream,
230 " -m, --multiple-iterations=ITERATIONS\n"
231 " Perform multiple choices of the -i and -j values,\n"
232 " and choose the best results. This increases the\n"
233 " running time by a factor of ITERATIONS but does a\n"
234 " good job minimizing the generated table size.\n");
235 fprintf (stream,
236 " -i, --initial-asso=N Provide an initial value for the associate values\n"
237 " array. Default is 0. Setting this value larger helps\n"
238 " inflate the size of the final table.\n");
239 fprintf (stream,
240 " -j, --jump=JUMP-VALUE Affects the \"jump value\", i.e., how far to advance\n"
241 " the associated character value upon collisions. Must\n"
242 " be an odd number, default is %d.\n",
243 DEFAULT_JUMP_VALUE);
244 fprintf (stream,
245 " -n, --no-strlen Do not include the length of the keyword when\n"
246 " computing the hash function.\n");
247 fprintf (stream,
248 " -r, --random Utilizes randomness to initialize the associated\n"
249 " values table.\n");
250 fprintf (stream,
251 " -s, --size-multiple=N Affects the size of the generated hash table. The\n"
252 " numeric argument N indicates \"how many times larger\n"
253 " or smaller\" the associated value range should be,\n"
254 " in relationship to the number of keys, e.g. a value\n"
255 " of 3 means \"allow the maximum associated value to\n"
256 " be about 3 times larger than the number of input\n"
257 " keys\". Conversely, a value of 1/3 means \"make the\n"
258 " maximum associated value about 3 times smaller than\n"
259 " the number of input keys\". A larger table should\n"
260 " decrease the time required for an unsuccessful\n"
261 " search, at the expense of extra table space. Default\n"
262 " value is 1.\n");
263 fprintf (stream, "\n");
264 fprintf (stream,
265 "Informative output:\n"
266 " -h, --help Print this message.\n"
267 " -v, --version Print the gperf version number.\n"
268 " -d, --debug Enables the debugging option (produces verbose\n"
269 " output to the standard error).\n");
270 fprintf (stream, "\n");
271 fprintf (stream,
272 "Report bugs to <[email protected]>.\n");
273 }
274
275 /* Prints the given options. */
276
277 void
278 Options::print_options () const
279 {
280 printf ("/* Command-line: ");
281
282 for (int i = 0; i < _argument_count; i++)
283 {
284 char *arg = _argument_vector[i];
285
286 if (i == 0)
287 {
288 /* _argument_vector[0] is the program name. Print only its base name,
289 and also drop its ".exe" suffix on native Windows.
290 This is useful for reproducible builds. */
291 {
292 char *p = arg + strlen (arg);
293 while (p > arg && ! ISSLASH (p[-1]))
294 p--;
295 arg = p;
296 }
297 {
298 char *p = arg + strlen (arg);
299 if (p - arg > 4
300 && p[-4] == '.'
301 && (p[-3] == 'E' || p[-3] == 'e')
302 && (p[-2] == 'X' || p[-2] == 'x')
303 && (p[-1] == 'E' || p[-1] == 'e'))
304 p[-4] = '\0';
305 }
306 }
307
308 /* Escape arg if it contains shell metacharacters. */
309 if (*arg == '-')
310 {
311 putchar (*arg);
312 arg++;
313 if ((*arg >= 'A' && *arg <= 'Z') || (*arg >= 'a' && *arg <= 'z'))
314 {
315 putchar (*arg);
316 arg++;
317 }
318 else if (*arg == '-')
319 {
320 do
321 {
322 putchar (*arg);
323 arg++;
324 }
325 while ((*arg >= 'A' && *arg <= 'Z') || (*arg >= 'a' && *arg <= 'z') || *arg == '-');
326 if (*arg == '=')
327 {
328 putchar (*arg);
329 arg++;
330 }
331 }
332 }
333 if (strpbrk (arg, "\t\n !\"#$&'()*;<>?[\\]`{|}~") != NULL)
334 {
335 if (strchr (arg, '\'') != NULL)
336 {
337 putchar ('"');
338 for (; *arg; arg++)
339 {
340 if (*arg == '\"' || *arg == '\\' || *arg == '$' || *arg == '`')
341 putchar ('\\');
342 putchar (*arg);
343 }
344 putchar ('"');
345 }
346 else
347 {
348 putchar ('\'');
349 for (; *arg; arg++)
350 {
351 if (*arg == '\\')
352 putchar ('\\');
353 putchar (*arg);
354 }
355 putchar ('\'');
356 }
357 }
358 else
359 printf ("%s", arg);
360
361 printf (" ");
362 }
363
364 printf (" */");
365 }
366
367 /* ------------------------------------------------------------------------- */
368
369 /* Parses a string denoting key positions. */
370
371 class PositionStringParser
372 {
373 public:
374 /* Initializes a key position string parser for string STR. */
375 PositionStringParser (const char *str,
376 int low_bound, int high_bound,
377 int end_word_marker, int error_value, int end_marker);
378 /* Returns the next key position from the given string. */
379 int nextPosition ();
380 private:
381 /* A pointer to the string provided by the user. */
382 const char * _str;
383 /* Smallest possible value, inclusive. */
384 int const _low_bound;
385 /* Greatest possible value, inclusive. */
386 int const _high_bound;
387 /* A value marking the abstract "end of word" ( usually '$'). */
388 int const _end_word_marker;
389 /* Error value returned when input is syntactically erroneous. */
390 int const _error_value;
391 /* Value returned after last key is processed. */
392 int const _end_marker;
393 /* Intermediate state for producing a range of positions. */
394 bool _in_range; /* True while producing a range of positions. */
395 int _range_upper_bound; /* Upper bound (inclusive) of the range. */
396 int _range_curr_value; /* Last value returned. */
397 };
398
399 /* Initializes a key position strng parser for string STR. */
400 PositionStringParser::PositionStringParser (const char *str,
401 int low_bound, int high_bound,
402 int end_word_marker, int error_value, int end_marker)
403 : _str (str),
404 _low_bound (low_bound),
405 _high_bound (high_bound),
406 _end_word_marker (end_word_marker),
407 _error_value (error_value),
408 _end_marker (end_marker),
409 _in_range (false)
410 {
411 }
412
413 /* Returns the next key position from the given string. */
414 int
415 PositionStringParser::nextPosition ()
416 {
417 if (_in_range)
418 {
419 /* We are inside a range. Return the next value from the range. */
420 if (++_range_curr_value >= _range_upper_bound)
421 _in_range = false;
422 return _range_curr_value;
423 }
424 else
425 {
426 /* Continue parsing the given string. */
427 while (*_str)
428 switch (*_str)
429 {
430 case ',':
431 /* Skip the comma. */
432 _str++;
433 break;
434 case '$':
435 /* Valid key position. */
436 _str++;
437 return _end_word_marker;
438 case '0': case '1': case '2': case '3': case '4':
439 case '5': case '6': case '7': case '8': case '9':
440 /* Valid key position. */
441 {
442 int curr_value;
443 for (curr_value = 0; isdigit (static_cast<unsigned char>(*_str)); _str++)
444 curr_value = curr_value * 10 + (*_str - '0');
445
446 if (*_str == '-')
447 {
448 _str++;
449 /* Starting a range of key positions. */
450 _in_range = true;
451
452 for (_range_upper_bound = 0;
453 isdigit (static_cast<unsigned char>(*_str));
454 _str++)
455 _range_upper_bound = _range_upper_bound * 10 + (*_str - '0');
456
457 /* Verify range's upper bound. */
458 if (!(_range_upper_bound > curr_value && _range_upper_bound <= _high_bound))
459 return _error_value;
460 _range_curr_value = curr_value;
461 }
462
463 /* Verify range's lower bound. */
464 if (!(curr_value >= _low_bound && curr_value <= _high_bound))
465 return _error_value;
466 return curr_value;
467 }
468 default:
469 /* Invalid syntax. */
470 return _error_value;
471 }
472
473 return _end_marker;
474 }
475 }
476
477 /* ------------------------------------------------------------------------- */
478
479 /* Sets the default Options. */
480
481 Options::Options ()
482 : _option_word (ANSIC),
483 _input_file_name (NULL),
484 _output_file_name (NULL),
485 _language (NULL),
486 _jump (DEFAULT_JUMP_VALUE),
487 _initial_asso_value (0),
488 _asso_iterations (0),
489 _total_switches (1),
490 _size_multiple (1),
491 _function_name (DEFAULT_FUNCTION_NAME),
492 _slot_name (DEFAULT_SLOT_NAME),
493 _initializer_suffix (DEFAULT_INITIALIZER_SUFFIX),
494 _class_name (DEFAULT_CLASS_NAME),
495 _hash_name (DEFAULT_HASH_NAME),
496 _wordlist_name (DEFAULT_WORDLIST_NAME),
497 _lengthtable_name (DEFAULT_LENGTHTABLE_NAME),
498 _stringpool_name (DEFAULT_STRINGPOOL_NAME),
499 _constants_prefix (DEFAULT_CONSTANTS_PREFIX),
500 _delimiters (DEFAULT_DELIMITERS),
501 _key_positions ()
502 {
503 }
504
505 /* Dumps option status when debugging is enabled. */
506
507 Options::~Options ()
508 {
509 if (_option_word & DEBUG)
510 {
511 fprintf (stderr, "\ndumping Options:"
512 "\nTYPE is........: %s"
513 "\nUPPERLOWER is..: %s"
514 "\nKRC is.........: %s"
515 "\nC is...........: %s"
516 "\nANSIC is.......: %s"
517 "\nCPLUSPLUS is...: %s"
518 "\nSEVENBIT is....: %s"
519 "\nLENTABLE is....: %s"
520 "\nCOMP is........: %s"
521 "\nCONST is.......: %s"
522 "\nENUM is........: %s"
523 "\nINCLUDE is.....: %s"
524 "\nGLOBAL is......: %s"
525 "\nNULLSTRINGS is.: %s"
526 "\nSHAREDLIB is...: %s"
527 "\nSWITCH is......: %s"
528 "\nNOTYPE is......: %s"
529 "\nDUP is.........: %s"
530 "\nNOLENGTH is....: %s"
531 "\nRANDOM is......: %s"
532 "\nDEBUG is.......: %s"
533 "\nlookup function name = %s"
534 "\nhash function name = %s"
535 "\nword list name = %s"
536 "\nlength table name = %s"
537 "\nstring pool name = %s"
538 "\nslot name = %s"
539 "\ninitializer suffix = %s"
540 "\nasso_values iterations = %d"
541 "\njump value = %d"
542 "\nhash table size multiplier = %g"
543 "\ninitial associated value = %d"
544 "\ndelimiters = %s"
545 "\nnumber of switch statements = %d\n",
546 _option_word & TYPE ? "enabled" : "disabled",
547 _option_word & UPPERLOWER ? "enabled" : "disabled",
548 _option_word & KRC ? "enabled" : "disabled",
549 _option_word & C ? "enabled" : "disabled",
550 _option_word & ANSIC ? "enabled" : "disabled",
551 _option_word & CPLUSPLUS ? "enabled" : "disabled",
552 _option_word & SEVENBIT ? "enabled" : "disabled",
553 _option_word & LENTABLE ? "enabled" : "disabled",
554 _option_word & COMP ? "enabled" : "disabled",
555 _option_word & CONST ? "enabled" : "disabled",
556 _option_word & ENUM ? "enabled" : "disabled",
557 _option_word & INCLUDE ? "enabled" : "disabled",
558 _option_word & GLOBAL ? "enabled" : "disabled",
559 _option_word & NULLSTRINGS ? "enabled" : "disabled",
560 _option_word & SHAREDLIB ? "enabled" : "disabled",
561 _option_word & SWITCH ? "enabled" : "disabled",
562 _option_word & NOTYPE ? "enabled" : "disabled",
563 _option_word & DUP ? "enabled" : "disabled",
564 _option_word & NOLENGTH ? "enabled" : "disabled",
565 _option_word & RANDOM ? "enabled" : "disabled",
566 _option_word & DEBUG ? "enabled" : "disabled",
567 _function_name, _hash_name, _wordlist_name, _lengthtable_name,
568 _stringpool_name, _slot_name, _initializer_suffix,
569 _asso_iterations, _jump, _size_multiple, _initial_asso_value,
570 _delimiters, _total_switches);
571 if (_key_positions.is_useall())
572 fprintf (stderr, "all characters are used in the hash function\n");
573 else
574 {
575 fprintf (stderr, "maximum keysig size = %d\nkey positions are: \n",
576 _key_positions.get_size());
577
578 PositionIterator iter = _key_positions.iterator();
579 for (int pos; (pos = iter.next()) != PositionIterator::EOS; )
580 if (pos == Positions::LASTCHAR)
581 fprintf (stderr, "$\n");
582 else
583 fprintf (stderr, "%d\n", pos + 1);
584 }
585
586 fprintf (stderr, "finished dumping Options\n");
587 }
588 }
589
590
591 /* Sets the output language, if not already set. */
592 void
593 Options::set_language (const char *language)
594 {
595 if (_language == NULL)
596 {
597 _language = language;
598 _option_word &= ~(KRC | C | ANSIC | CPLUSPLUS);
599 if (!strcmp (language, "KR-C"))
600 _option_word |= KRC;
601 else if (!strcmp (language, "C"))
602 _option_word |= C;
603 else if (!strcmp (language, "ANSI-C"))
604 _option_word |= ANSIC;
605 else if (!strcmp (language, "C++"))
606 _option_word |= CPLUSPLUS;
607 else
608 {
609 fprintf (stderr,
610 "unsupported language option %s, defaulting to ANSI-C\n",
611 language);
612 _option_word |= ANSIC;
613 }
614 }
615 }
616
617 /* Sets the total number of switch statements, if not already set. */
618 void
619 Options::set_total_switches (int total_switches)
620 {
621 if (!(_option_word & SWITCH))
622 {
623 _option_word |= SWITCH;
624 _total_switches = total_switches;
625 }
626 }
627
628 /* Sets the generated function name, if not already set. */
629 void
630 Options::set_function_name (const char *name)
631 {
632 if (_function_name == DEFAULT_FUNCTION_NAME)
633 _function_name = name;
634 }
635
636 /* Sets the keyword key name, if not already set. */
637 void
638 Options::set_slot_name (const char *name)
639 {
640 if (_slot_name == DEFAULT_SLOT_NAME)
641 _slot_name = name;
642 }
643
644 /* Returns true if the struct initializer suffix has been set. */
645 bool
646 Options::has_initializer_suffix () const
647 {
648 return _initializer_suffix != DEFAULT_INITIALIZER_SUFFIX;
649 }
650
651 /* Sets the struct initializer suffix, if not already set. */
652 void
653 Options::set_initializer_suffix (const char *initializers)
654 {
655 if (_initializer_suffix == DEFAULT_INITIALIZER_SUFFIX)
656 _initializer_suffix = initializers;
657 }
658
659 /* Sets the generated class name, if not already set. */
660 void
661 Options::set_class_name (const char *name)
662 {
663 if (_class_name == DEFAULT_CLASS_NAME)
664 _class_name = name;
665 }
666
667 /* Sets the hash function name, if not already set. */
668 void
669 Options::set_hash_name (const char *name)
670 {
671 if (_hash_name == DEFAULT_HASH_NAME)
672 _hash_name = name;
673 }
674
675 /* Sets the hash table array name, if not already set. */
676 void
677 Options::set_wordlist_name (const char *name)
678 {
679 if (_wordlist_name == DEFAULT_WORDLIST_NAME)
680 _wordlist_name = name;
681 }
682
683 /* Sets the length table array name, if not already set. */
684 void
685 Options::set_lengthtable_name (const char *name)
686 {
687 if (_lengthtable_name == DEFAULT_LENGTHTABLE_NAME)
688 _lengthtable_name = name;
689 }
690
691 /* Sets the prefix for the constants, if not already set. */
692 void
693 Options::set_constants_prefix (const char *prefix)
694 {
695 if (_constants_prefix == DEFAULT_CONSTANTS_PREFIX)
696 _constants_prefix = prefix;
697 }
698
699 /* Sets the string pool name, if not already set. */
700 void
701 Options::set_stringpool_name (const char *name)
702 {
703 if (_stringpool_name == DEFAULT_STRINGPOOL_NAME)
704 _stringpool_name = name;
705 }
706
707 /* Sets the delimiters string, if not already set. */
708 void
709 Options::set_delimiters (const char *delimiters)
710 {
711 if (_delimiters == DEFAULT_DELIMITERS)
712 _delimiters = delimiters;
713 }
714
715
716 /* Parses the command line Options and sets appropriate flags in option_word. */
717
718 static const struct option long_options[] =
719 {
720 { "output-file", required_argument, NULL, CHAR_MAX + 1 },
721 { "ignore-case", no_argument, NULL, CHAR_MAX + 2 },
722 { "delimiters", required_argument, NULL, 'e' },
723 { "struct-type", no_argument, NULL, 't' },
724 { "language", required_argument, NULL, 'L' },
725 { "slot-name", required_argument, NULL, 'K' },
726 { "initializer-suffix", required_argument, NULL, 'F' },
727 { "hash-fn-name", required_argument, NULL, 'H' }, /* backward compatibility */
728 { "hash-function-name", required_argument, NULL, 'H' },
729 { "lookup-fn-name", required_argument, NULL, 'N' }, /* backward compatibility */
730 { "lookup-function-name", required_argument, NULL, 'N' },
731 { "class-name", required_argument, NULL, 'Z' },
732 { "seven-bit", no_argument, NULL, '7' },
733 { "compare-strncmp", no_argument, NULL, 'c' },
734 { "readonly-tables", no_argument, NULL, 'C' },
735 { "enum", no_argument, NULL, 'E' },
736 { "includes", no_argument, NULL, 'I' },
737 { "global-table", no_argument, NULL, 'G' },
738 { "constants-prefix", required_argument, NULL, CHAR_MAX + 5 },
739 { "word-array-name", required_argument, NULL, 'W' },
740 { "length-table-name", required_argument, NULL, CHAR_MAX + 4 },
741 { "switch", required_argument, NULL, 'S' },
742 { "omit-struct-type", no_argument, NULL, 'T' },
743 { "key-positions", required_argument, NULL, 'k' },
744 { "compare-strlen", no_argument, NULL, 'l' }, /* backward compatibility */
745 { "compare-lengths", no_argument, NULL, 'l' },
746 { "duplicates", no_argument, NULL, 'D' },
747 { "fast", required_argument, NULL, 'f' },
748 { "initial-asso", required_argument, NULL, 'i' },
749 { "jump", required_argument, NULL, 'j' },
750 { "multiple-iterations", required_argument, NULL, 'm' },
751 { "no-strlen", no_argument, NULL, 'n' },
752 { "occurrence-sort", no_argument, NULL, 'o' },
753 { "optimized-collision-resolution", no_argument, NULL, 'O' },
754 { "pic", no_argument, NULL, 'P' },
755 { "string-pool-name", required_argument, NULL, 'Q' },
756 { "null-strings", no_argument, NULL, CHAR_MAX + 3 },
757 { "random", no_argument, NULL, 'r' },
758 { "size-multiple", required_argument, NULL, 's' },
759 { "help", no_argument, NULL, 'h' },
760 { "version", no_argument, NULL, 'v' },
761 { "debug", no_argument, NULL, 'd' },
762 { NULL, no_argument, NULL, 0 }
763 };
764
765 void
766 Options::parse_options (int argc, char *argv[])
767 {
768 int option_char;
769
770 program_name = argv[0];
771 _argument_count = argc;
772 _argument_vector = argv;
773
774 while ((option_char =
775 getopt_long (_argument_count, _argument_vector,
776 "acCdDe:Ef:F:gGhH:i:Ij:k:K:lL:m:nN:oOpPQ:rs:S:tTvW:Z:7",
777 long_options, NULL))
778 != -1)
779 {
780 switch (option_char)
781 {
782 case 'a': /* Generated code uses the ANSI prototype format. */
783 break; /* This is now the default. */
784 case 'c': /* Generate strncmp rather than strcmp. */
785 {
786 _option_word |= COMP;
787 break;
788 }
789 case 'C': /* Make the generated tables readonly (const). */
790 {
791 _option_word |= CONST;
792 break;
793 }
794 case 'd': /* Enable debugging option. */
795 {
796 _option_word |= DEBUG;
797 fprintf (stderr, "Starting program %s, version %s, with debugging on.\n",
798 program_name, version_string);
799 break;
800 }
801 case 'D': /* Enable duplicate option. */
802 {
803 _option_word |= DUP;
804 break;
805 }
806 case 'e': /* Specify keyword/attribute separator */
807 {
808 _delimiters = /*getopt*/optarg;
809 break;
810 }
811 case 'E':
812 {
813 _option_word |= ENUM;
814 break;
815 }
816 case 'f': /* Generate the hash table "fast". */
817 break; /* Not needed any more. */
818 case 'F':
819 {
820 _initializer_suffix = /*getopt*/optarg;
821 break;
822 }
823 case 'g': /* Use the 'inline' keyword for generated sub-routines, ifdef __GNUC__. */
824 break; /* This is now the default. */
825 case 'G': /* Make the keyword table a global variable. */
826 {
827 _option_word |= GLOBAL;
828 break;
829 }
830 case 'h': /* Displays a list of helpful Options to the user. */
831 {
832 long_usage (stdout);
833 exit (0);
834 }
835 case 'H': /* Sets the name for the hash function. */
836 {
837 _hash_name = /*getopt*/optarg;
838 break;
839 }
840 case 'i': /* Sets the initial value for the associated values array. */
841 {
842 if ((_initial_asso_value = atoi (/*getopt*/optarg)) < 0)
843 fprintf (stderr, "Initial value %d should be non-zero, ignoring and continuing.\n", _initial_asso_value);
844 if (option[RANDOM])
845 fprintf (stderr, "warning, -r option superceeds -i, ignoring -i option and continuing\n");
846 break;
847 }
848 case 'I': /* Enable #include statements. */
849 {
850 _option_word |= INCLUDE;
851 break;
852 }
853 case 'j': /* Sets the jump value, must be odd for later algorithms. */
854 {
855 if ((_jump = atoi (/*getopt*/optarg)) < 0)
856 {
857 fprintf (stderr, "Jump value %d must be a positive number.\n", _jump);
858 short_usage (stderr);
859 exit (1);
860 }
861 else if (_jump && ((_jump % 2) == 0))
862 fprintf (stderr, "Jump value %d should be odd, adding 1 and continuing...\n", _jump++);
863 break;
864 }
865 case 'k': /* Sets key positions used for hash function. */
866 {
867 _option_word |= POSITIONS;
868 const int BAD_VALUE = -3;
869 const int EOS = PositionIterator::EOS;
870 int value;
871 PositionStringParser sparser (/*getopt*/optarg, 1, Positions::MAX_KEY_POS, Positions::LASTCHAR, BAD_VALUE, EOS);
872
873 if (/*getopt*/optarg [0] == '*') /* Use all the characters for hashing!!!! */
874 _key_positions.set_useall(true);
875 else
876 {
877 _key_positions.set_useall(false);
878 int *key_positions = _key_positions.pointer();
879 int *key_pos;
880
881 for (key_pos = key_positions; (value = sparser.nextPosition()) != EOS; key_pos++)
882 {
883 if (value == BAD_VALUE)
884 {
885 fprintf (stderr, "Invalid position value or range, use 1,2,3-%d,'$' or '*'.\n",
886 Positions::MAX_KEY_POS);
887 short_usage (stderr);
888 exit (1);
889 }
890 if (key_pos - key_positions == Positions::MAX_SIZE)
891 {
892 /* More than Positions::MAX_SIZE key positions.
893 Since all key positions are in the range
894 0..Positions::MAX_KEY_POS-1 or == Positions::LASTCHAR,
895 there must be duplicates. */
896 fprintf (stderr, "Duplicate key positions selected\n");
897 short_usage (stderr);
898 exit (1);
899 }
900 if (value != Positions::LASTCHAR)
901 /* We use 0-based indices in the class Positions. */
902 value = value - 1;
903 *key_pos = value;
904 }
905
906 unsigned int total_keysig_size = key_pos - key_positions;
907 if (total_keysig_size == 0)
908 {
909 fprintf (stderr, "No key positions selected.\n");
910 short_usage (stderr);
911 exit (1);
912 }
913 _key_positions.set_size (total_keysig_size);
914
915 /* Sorts the key positions *IN REVERSE ORDER!!*
916 This makes further routines more efficient. Especially
917 when generating code. */
918 if (! _key_positions.sort())
919 {
920 fprintf (stderr, "Duplicate key positions selected\n");
921 short_usage (stderr);
922 exit (1);
923 }
924 }
925 break;
926 }
927 case 'K': /* Make this the keyname for the keyword component field. */
928 {
929 _slot_name = /*getopt*/optarg;
930 break;
931 }
932 case 'l': /* Create length table to avoid extra string compares. */
933 {
934 _option_word |= LENTABLE;
935 break;
936 }
937 case 'L': /* Deal with different generated languages. */
938 {
939 _language = NULL;
940 set_language (/*getopt*/optarg);
941 break;
942 }
943 case 'm': /* Multiple iterations for finding good asso_values. */
944 {
945 if ((_asso_iterations = atoi (/*getopt*/optarg)) < 0)
946 {
947 fprintf (stderr, "asso_iterations value must not be negative, assuming 0\n");
948 _asso_iterations = 0;
949 }
950 break;
951 }
952 case 'n': /* Don't include the length when computing hash function. */
953 {
954 _option_word |= NOLENGTH;
955 break;
956 }
957 case 'N': /* Make generated lookup function name be optarg. */
958 {
959 _function_name = /*getopt*/optarg;
960 break;
961 }
962 case 'o': /* Order input by frequency of key set occurrence. */
963 break; /* Not needed any more. */
964 case 'O': /* Optimized choice during collision resolution. */
965 break; /* Not needed any more. */
966 case 'p': /* Generated lookup function a pointer instead of int. */
967 break; /* This is now the default. */
968 case 'P': /* Optimize for position-independent code. */
969 {
970 _option_word |= SHAREDLIB;
971 break;
972 }
973 case 'Q': /* Sets the name for the string pool. */
974 {
975 _stringpool_name = /*getopt*/optarg;
976 break;
977 }
978 case 'r': /* Utilize randomness to initialize the associated values table. */
979 {
980 _option_word |= RANDOM;
981 if (_initial_asso_value != 0)
982 fprintf (stderr, "warning, -r option supersedes -i, disabling -i option and continuing\n");
983 break;
984 }
985 case 's': /* Range of associated values, determines size of final table. */
986 {
987 float numerator;
988 float denominator = 1;
989 bool invalid = false;
990 char *endptr;
991
992 numerator = strtod (/*getopt*/optarg, &endptr);
993 if (endptr == /*getopt*/optarg)
994 invalid = true;
995 else if (*endptr != '\0')
996 {
997 if (*endptr == '/')
998 {
999 char *denomptr = endptr + 1;
1000 denominator = strtod (denomptr, &endptr);
1001 if (endptr == denomptr || *endptr != '\0')
1002 invalid = true;
1003 }
1004 else
1005 invalid = true;
1006 }
1007 if (invalid)
1008 {
1009 fprintf (stderr, "Invalid value for option -s.\n");
1010 short_usage (stderr);
1011 exit (1);
1012 }
1013 _size_multiple = numerator / denominator;
1014 /* Backward compatibility: -3 means 1/3. */
1015 if (_size_multiple < 0)
1016 _size_multiple = 1 / (-_size_multiple);
1017 /* Catch stupid users. */
1018 if (_size_multiple == 0)
1019 _size_multiple = 1;
1020 /* Warnings. */
1021 if (_size_multiple > 50)
1022 fprintf (stderr, "Size multiple %g is excessive, did you really mean this?! (try '%s --help' for help)\n", _size_multiple, program_name);
1023 else if (_size_multiple < 0.01f)
1024 fprintf (stderr, "Size multiple %g is extremely small, did you really mean this?! (try '%s --help' for help)\n", _size_multiple, program_name);
1025 break;
1026 }
1027 case 'S': /* Generate switch statement output, rather than lookup table. */
1028 {
1029 _option_word |= SWITCH;
1030 _total_switches = atoi (/*getopt*/optarg);
1031 if (_total_switches <= 0)
1032 {
1033 fprintf (stderr, "number of switches %s must be a positive number\n", /*getopt*/optarg);
1034 short_usage (stderr);
1035 exit (1);
1036 }
1037 break;
1038 }
1039 case 't': /* Enable the TYPE mode, allowing arbitrary user structures. */
1040 {
1041 _option_word |= TYPE;
1042 break;
1043 }
1044 case 'T': /* Don't print structure definition. */
1045 {
1046 _option_word |= NOTYPE;
1047 break;
1048 }
1049 case 'v': /* Print out the version and quit. */
1050 fprintf (stdout, "GNU gperf %s\n", version_string);
1051 fprintf (stdout, "Copyright (C) %s Free Software Foundation, Inc.\n\
1052 License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n\
1053 This is free software: you are free to change and redistribute it.\n\
1054 There is NO WARRANTY, to the extent permitted by law.\n\
1055 ",
1056 "2025");
1057 fprintf (stdout, "Written by %s and %s.\n",
1058 "Douglas C. Schmidt", "Bruno Haible");
1059 exit (0);
1060 case 'W': /* Sets the name for the hash table array. */
1061 {
1062 _wordlist_name = /*getopt*/optarg;
1063 break;
1064 }
1065 case 'Z': /* Set the class name. */
1066 {
1067 _class_name = /*getopt*/optarg;
1068 break;
1069 }
1070 case '7': /* Assume 7-bit characters. */
1071 {
1072 _option_word |= SEVENBIT;
1073 break;
1074 }
1075 case CHAR_MAX + 1: /* Set the output file name. */
1076 {
1077 _output_file_name = /*getopt*/optarg;
1078 break;
1079 }
1080 case CHAR_MAX + 2: /* Case insignificant. */
1081 {
1082 _option_word |= UPPERLOWER;
1083 break;
1084 }
1085 case CHAR_MAX + 3: /* Use NULL instead of "". */
1086 {
1087 _option_word |= NULLSTRINGS;
1088 break;
1089 }
1090 case CHAR_MAX + 4: /* Sets the name for the length table array. */
1091 {
1092 _lengthtable_name = /*getopt*/optarg;
1093 break;
1094 }
1095 case CHAR_MAX + 5: /* Sets the prefix for the constants. */
1096 {
1097 _constants_prefix = /*getopt*/optarg;
1098 break;
1099 }
1100 default:
1101 short_usage (stderr);
1102 exit (1);
1103 }
1104
1105 }
1106
1107 if (/*getopt*/optind < argc)
1108 _input_file_name = argv[/*getopt*/optind++];
1109
1110 if (/*getopt*/optind < argc)
1111 {
1112 fprintf (stderr, "Extra trailing arguments to %s.\n", program_name);
1113 short_usage (stderr);
1114 exit (1);
1115 }
1116 }
1117
1118 /* ------------------------------------------------------------------------- */
1119
1120 #ifndef __OPTIMIZE__
1121
1122 #define INLINE /* not inline */
1123 #include "options.icc"
1124 #undef INLINE
1125
1126 #endif /* not defined __OPTIMIZE__ */