xref: /NextBSD/contrib/gperf/src/options.h (revision 95f7c2f56c7268d6ed9c2a56d357aeeac260363b)
1 /* This may look like C code, but it is really -*- C++ -*- */
2 
3 /* Handles parsing the Options provided to the user.
4 
5    Copyright (C) 1989-1998, 2000, 2002-2004 Free Software Foundation, Inc.
6    Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
7    and Bruno Haible <bruno@clisp.org>.
8 
9    This file is part of GNU GPERF.
10 
11    GNU GPERF is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15 
16    GNU GPERF is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; see the file COPYING.
23    If not, write to the Free Software Foundation, Inc.,
24    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
25 
26 /* This module provides a uniform interface to the various options available
27    to a user of the gperf hash function generator.  */
28 
29 #ifndef options_h
30 #define options_h 1
31 
32 #include <stdio.h>
33 #include "positions.h"
34 
35 /* Enumeration of the possible boolean options.  */
36 
37 enum Option_Type
38 {
39   /* --- Input file interpretation --- */
40 
41   /* Handle user-defined type structured keyword input.  */
42   TYPE         = 1 << 0,
43 
44   /* Ignore case of ASCII characters.  */
45   UPPERLOWER   = 1 << 1,
46 
47   /* --- Language for the output code --- */
48 
49   /* Generate K&R C code: no prototypes, no const.  */
50   KRC          = 1 << 2,
51 
52   /* Generate C code: no prototypes, but const (user can #define it away).  */
53   C            = 1 << 3,
54 
55   /* Generate ISO/ANSI C code: prototypes and const, but no class.  */
56   ANSIC        = 1 << 4,
57 
58   /* Generate C++ code: prototypes, const, class, inline, enum.  */
59   CPLUSPLUS    = 1 << 5,
60 
61   /* --- Details in the output code --- */
62 
63   /* Assume 7-bit, not 8-bit, characters.  */
64   SEVENBIT     = 1 << 6,
65 
66   /* Generate a length table for string comparison.  */
67   LENTABLE     = 1 << 7,
68 
69   /* Generate strncmp rather than strcmp.  */
70   COMP         = 1 << 8,
71 
72   /* Make the generated tables readonly (const).  */
73   CONST        = 1 << 9,
74 
75   /* Use enum for constants.  */
76   ENUM         = 1 << 10,
77 
78   /* Generate #include statements.  */
79   INCLUDE      = 1 << 11,
80 
81   /* Make the keyword table a global variable.  */
82   GLOBAL       = 1 << 12,
83 
84   /* Use NULL strings instead of empty strings for empty table entries.  */
85   NULLSTRINGS  = 1 << 13,
86 
87   /* Optimize for position-independent code.  */
88   SHAREDLIB    = 1 << 14,
89 
90   /* Generate switch output to save space.  */
91   SWITCH       = 1 << 15,
92 
93   /* Don't include user-defined type definition in output -- it's already
94      defined elsewhere.  */
95   NOTYPE       = 1 << 16,
96 
97   /* --- Algorithm employed by gperf --- */
98 
99   /* Use the given key positions.  */
100   POSITIONS    = 1 << 17,
101 
102   /* Handle duplicate hash values for keywords.  */
103   DUP          = 1 << 18,
104 
105   /* Don't include keyword length in hash computations.  */
106   NOLENGTH     = 1 << 19,
107 
108   /* Randomly initialize the associated values table.  */
109   RANDOM       = 1 << 20,
110 
111   /* --- Informative output --- */
112 
113   /* Enable debugging (prints diagnostics to stderr).  */
114   DEBUG        = 1 << 21
115 };
116 
117 /* Class manager for gperf program Options.  */
118 
119 class Options
120 {
121 public:
122   /* Constructor.  */
123                         Options ();
124 
125   /* Destructor.  */
126                         ~Options ();
127 
128   /* Parses the options given in the command-line arguments.  */
129   void                  parse_options (int argc, char *argv[]);
130 
131   /* Prints the given options.  */
132   void                  print_options () const;
133 
134   /* Accessors.  */
135 
136   /* Tests a given boolean option.  Returns true if set, false otherwise.  */
137   bool                  operator[] (Option_Type option) const;
138   /* Sets a given boolean option.  */
139   void                  set (Option_Type option);
140 
141   /* Returns the input file name.  */
142   const char *          get_input_file_name () const;
143 
144   /* Returns the output file name.  */
145   const char *          get_output_file_name () const;
146 
147   /* Sets the output language, if not already set.  */
148   void                  set_language (const char *language);
149 
150   /* Returns the jump value.  */
151   int                   get_jump () const;
152 
153   /* Returns the initial associated character value.  */
154   int                   get_initial_asso_value () const;
155 
156   /* Returns the number of iterations for finding good asso_values.  */
157   int                   get_asso_iterations () const;
158 
159   /* Returns the total number of switch statements to generate.  */
160   int                   get_total_switches () const;
161   /* Sets the total number of switch statements, if not already set.  */
162   void                  set_total_switches (int total_switches);
163 
164   /* Returns the factor by which to multiply the generated table's size.  */
165   float                 get_size_multiple () const;
166 
167   /* Returns the generated function name.  */
168   const char *          get_function_name () const;
169   /* Sets the generated function name, if not already set.  */
170   void                  set_function_name (const char *name);
171 
172   /* Returns the keyword key name.  */
173   const char *          get_slot_name () const;
174   /* Sets the keyword key name, if not already set.  */
175   void                  set_slot_name (const char *name);
176 
177   /* Returns the struct initializer suffix.  */
178   const char *          get_initializer_suffix () const;
179   /* Sets the struct initializer suffix, if not already set.  */
180   void                  set_initializer_suffix (const char *initializers);
181 
182   /* Returns the generated class name.  */
183   const char *          get_class_name () const;
184   /* Sets the generated class name, if not already set.  */
185   void                  set_class_name (const char *name);
186 
187   /* Returns the hash function name.  */
188   const char *          get_hash_name () const;
189   /* Sets the hash function name, if not already set.  */
190   void                  set_hash_name (const char *name);
191 
192   /* Returns the hash table array name.  */
193   const char *          get_wordlist_name () const;
194   /* Sets the hash table array name, if not already set.  */
195   void                  set_wordlist_name (const char *name);
196 
197   /* Returns the length table array name.  */
198   const char *          get_lengthtable_name () const;
199   /* Sets the length table array name, if not already set.  */
200   void                  set_lengthtable_name (const char *name);
201 
202   /* Returns the string pool name.  */
203   const char *          get_stringpool_name () const;
204   /* Sets the string pool name, if not already set.  */
205   void                  set_stringpool_name (const char *name);
206 
207   /* Returns the string used to delimit keywords from other attributes.  */
208   const char *          get_delimiters () const;
209   /* Sets the delimiters string, if not already set.  */
210   void                  set_delimiters (const char *delimiters);
211 
212   /* Returns key positions.  */
213   const Positions&      get_key_positions () const;
214 
215 private:
216   /* Prints program usage to given stream.  */
217   static void           short_usage (FILE * stream);
218 
219   /* Prints program usage to given stream.  */
220   static void           long_usage (FILE * stream);
221 
222   /* Records count of command-line arguments.  */
223   int                   _argument_count;
224 
225   /* Stores a pointer to command-line argument vector.  */
226   char **               _argument_vector;
227 
228   /* Holds the boolean options.  */
229   int                   _option_word;
230 
231   /* Name of input file.  */
232   char *                _input_file_name;
233 
234   /* Name of output file.  */
235   char *                _output_file_name;
236 
237   /* The output language.  */
238   const char *          _language;
239 
240   /* Jump length when trying alternative values.  */
241   int                   _jump;
242 
243   /* Initial value for asso_values table.  */
244   int                   _initial_asso_value;
245 
246   /* Number of attempts at finding good asso_values.  */
247   int                   _asso_iterations;
248 
249   /* Number of switch statements to generate.  */
250   int                   _total_switches;
251 
252   /* Factor by which to multiply the generated table's size.  */
253   float                 _size_multiple;
254 
255   /* Names used for generated lookup function.  */
256   const char *          _function_name;
257 
258   /* Name used for keyword key.  */
259   const char *          _slot_name;
260 
261   /* Suffix for empty struct initializers.  */
262   const char *          _initializer_suffix;
263 
264   /* Name used for generated C++ class.  */
265   const char *          _class_name;
266 
267   /* Name used for generated hash function.  */
268   const char *          _hash_name;
269 
270   /* Name used for hash table array.  */
271   const char *          _wordlist_name;
272 
273   /* Name used for length table array.  */
274   const char *          _lengthtable_name;
275 
276   /* Name used for the string pool.  */
277   const char *          _stringpool_name;
278 
279   /* Separates keywords from other attributes.  */
280   const char *          _delimiters;
281 
282   /* Contains user-specified key choices.  */
283   Positions             _key_positions;
284 };
285 
286 /* Global option coordinator for the entire program.  */
287 extern Options option;
288 
289 #ifdef __OPTIMIZE__
290 
291 #define INLINE inline
292 #include "options.icc"
293 #undef INLINE
294 
295 #endif
296 
297 #endif
298