Finished layout code.
[binutils.git] / gold / options.cc
blobad0ac838325108df8c7382e61f0855ce2f7e2dd1
1 // options.c -- handle command line options for gold
3 #include <iostream>
5 #include "gold.h"
6 #include "options.h"
8 // The information we keep for a single command line option.
10 struct gold::options::One_option
12 // The single character option name, or '\0' if this is only a long
13 // option.
14 char short_option;
16 // The long option name, or NULL if this is only a short option.
17 const char* long_option;
19 // Description of the option for --help output, or NULL if there is none.
20 const char* doc;
22 // How to print the option name in --help output, or NULL to use the
23 // default.
24 const char* help_output;
26 // Long option dash control. This is ignored if long_option is
27 // NULL.
28 enum
30 // Long option normally takes one dash; two dashes are also
31 // accepted.
32 ONE_DASH,
33 // Long option normally takes two dashes; one dash is also
34 // accepted.
35 TWO_DASHES,
36 // Long option always takes two dashes.
37 EXACTLY_TWO_DASHES
38 } dash;
40 // Function for special handling, or NULL. Returns the number of
41 // arguments to skip. This will normally be at least 1, but it may
42 // be 0 if this function changes *argv. ARG points to the location
43 // in *ARGV where the option starts, which may be helpful for a
44 // short option.
45 int (*special)(int argc, char** argv, char *arg, gold::Command_line*);
47 // If this is a position independent option which does not take an
48 // argument, this is the member function to call to record it.
49 void (gold::General_options::*general_noarg)();
51 // If this is a position independent function which takes an
52 // argument, this is the member function to call to record it.
53 void (gold::General_options::*general_arg)(const char*);
55 // If this is a position dependent option which does not take an
56 // argument, this is the member function to call to record it.
57 void (gold::Position_dependent_options::*dependent_noarg)();
59 // If this is a position dependent option which takes an argument,
60 // this is the member function to record it.
61 void (gold::Position_dependent_options::*dependent_arg)(const char*);
63 // Return whether this option takes an argument.
64 bool
65 takes_argument() const
66 { return this->general_arg != NULL || this->dependent_arg != NULL; }
69 class gold::options::Command_line_options
71 public:
72 static const One_option options[];
73 static const int options_size;
76 namespace
79 // Report usage information for ld --help, and exit.
81 int
82 help(int, char**, char*, gold::Command_line*)
84 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
86 const int options_size = gold::options::Command_line_options::options_size;
87 const gold::options::One_option* options =
88 gold::options::Command_line_options::options;
89 for (int i = 0; i < options_size; ++i)
91 if (options[i].doc == NULL)
92 continue;
94 printf(" ");
95 int len = 2;
96 bool comma = false;
98 int j = i;
101 if (options[j].help_output != NULL)
103 if (comma)
105 printf(", ");
106 len += 2;
108 printf(options[j].help_output);
109 len += std::strlen(options[i].help_output);
111 else
113 if (options[j].short_option != '\0')
115 if (comma)
117 printf(", ");
118 len += 2;
120 printf("-%c", options[j].short_option);
121 len += 2;
124 if (options[j].long_option != NULL)
126 if (comma)
128 printf(", ");
129 len += 2;
131 if (options[j].dash == gold::options::One_option::ONE_DASH)
133 printf("-");
134 ++len;
136 else
138 printf("--");
139 len += 2;
141 printf("%s", options[j].long_option);
142 len += std::strlen(options[j].long_option);
145 ++j;
147 while (j < options_size && options[j].doc == NULL);
149 if (len > 30)
151 printf("\n");
152 len = 0;
154 for (; len < 30; ++len)
155 std::putchar(' ');
157 std::puts(options[i].doc);
160 gold::gold_exit(true);
162 return 0;
165 } // End empty namespace.
167 // Helper macros used to specify the options. We could also do this
168 // using constructors, but then g++ would generate code to initialize
169 // the array. We want the array to be initialized statically so that
170 // we get better startup time.
172 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
173 { short_option, long_option, doc, help, gold::options::One_option::dash, \
174 NULL, func, NULL, NULL, NULL }
175 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
176 { short_option, long_option, doc, help, gold::options::One_option::dash, \
177 NULL, NULL, func, NULL, NULL }
178 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
179 { short_option, long_option, doc, help, gold::options::One_option::dash, \
180 NULL, NULL, NULL, func, NULL }
181 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
182 { short_option, long_option, doc, help, gold::options::One_option::dash, \
183 NULL, NULL, NULL, NULL, func }
184 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
185 { short_option, long_option, doc, help, gold::options::One_option::dash, \
186 func, NULL, NULL, NULL, NULL }
188 // Here is the actual list of options which we accept.
190 const gold::options::One_option
191 gold::options::Command_line_options::options[] =
193 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
194 N_("-L DIR, --library-path DIR"), TWO_DASHES,
195 &gold::General_options::add_to_search_path),
196 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
197 ONE_DASH, &gold::General_options::set_relocatable),
198 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
199 NULL, ONE_DASH, &gold::General_options::set_static),
200 SPECIAL('\0', "help", N_("Report usage information"), NULL,
201 TWO_DASHES, &help)
204 const int gold::options::Command_line_options::options_size =
205 sizeof (options) / sizeof (options[0]);
207 // The default values for the general options.
209 gold::General_options::General_options()
210 : is_relocatable_(false)
214 // The default values for the position dependent options.
216 gold::Position_dependent_options::Position_dependent_options()
217 : do_static_search_(false)
221 // Construct a Command_line.
223 gold::Command_line::Command_line()
227 // Process the command line options.
229 void
230 gold::Command_line::process(int argc, char** argv)
232 const int options_size = gold::options::Command_line_options::options_size;
233 const gold::options::One_option* options =
234 gold::options::Command_line_options::options;
235 bool no_more_options = false;
236 int i = 0;
237 while (i < argc)
239 if (argv[i][0] != '-' || no_more_options)
241 this->inputs_.push_back(Input_argument(argv[i],
242 this->position_options_));
243 ++i;
244 continue;
247 // Option starting with '-'.
248 int dashes = 1;
249 if (argv[i][1] == '-')
251 dashes = 2;
252 if (argv[i][2] == '\0')
254 no_more_options = true;
255 continue;
259 // Look for a long option match.
260 char* opt = argv[i] + dashes;
261 char first = opt[0];
262 int skiparg = 0;
263 char* arg = strchr(opt, '=');
264 if (arg != NULL)
265 *arg = '\0';
266 else if (i + 1 < argc)
268 arg = argv[i + 1];
269 skiparg = 1;
272 int j;
273 for (j = 0; j < options_size; ++j)
275 if (options[j].long_option != NULL
276 && (dashes == 2
277 || (options[j].dash
278 != gold::options::One_option::EXACTLY_TWO_DASHES))
279 && first == options[j].long_option[0]
280 && strcmp(opt, options[j].long_option) == 0)
282 if (options[j].special)
283 i += options[j].special(argc - 1, argv + i, opt, this);
284 else
286 if (!options[j].takes_argument())
288 arg = NULL;
289 skiparg = 0;
291 else
293 if (arg == NULL)
294 this->usage(_("missing argument"), argv[i]);
296 this->apply_option(options[j], arg);
297 i += skiparg + 1;
299 break;
302 if (j < options_size)
303 continue;
305 // If we saw two dashes, we need to see a long option.
306 if (dashes == 2)
307 this->usage(_("unknown option"), argv[i]);
309 // Look for a short option match. There may be more than one
310 // short option in a given argument.
311 bool done = false;
312 char* s = argv[i] + 1;
313 ++i;
314 while (*s != '\0' && !done)
316 char opt = *s;
317 int j;
318 for (j = 0; j < options_size; ++j)
320 if (options[j].short_option == opt)
322 if (options[j].special)
324 // Undo the argument skip done above.
325 --i;
326 i += options[j].special(argc - i, argv + i, s, this);
327 done = true;
329 else
331 arg = NULL;
332 if (options[j].takes_argument())
334 if (s[1] != '\0')
336 arg = s + 1;
337 done = true;
339 else if (i < argc)
341 arg = argv[i];
342 ++i;
344 else
345 this->usage(_("missing argument"), opt);
347 this->apply_option(options[j], arg);
349 break;
353 if (j >= options_size)
354 this->usage(_("unknown option"), *s);
356 ++s;
361 // Apply a command line option.
363 void
364 gold::Command_line::apply_option(const gold::options::One_option& opt,
365 const char* arg)
367 if (arg == NULL)
369 if (opt.general_noarg)
370 (this->options_.*(opt.general_noarg))();
371 else if (opt.dependent_noarg)
372 (this->position_options_.*(opt.dependent_noarg))();
373 else
374 gold::gold_unreachable();
376 else
378 if (opt.general_arg)
379 (this->options_.*(opt.general_arg))(arg);
380 else if (opt.dependent_arg)
381 (this->position_options_.*(opt.dependent_arg))(arg);
382 else
383 gold::gold_unreachable();
387 // Report a usage error. */
389 void
390 gold::Command_line::usage()
392 fprintf(stderr,
393 _("%s: use the --help option for usage information\n"),
394 gold::program_name);
395 gold::gold_exit(false);
398 void
399 gold::Command_line::usage(const char* msg, const char *opt)
401 fprintf(stderr,
402 _("%s: %s: %s\n"),
403 gold::program_name, opt, msg);
404 this->usage();
407 void
408 gold::Command_line::usage(const char* msg, char opt)
410 fprintf(stderr,
411 _("%s: -%c: %s\n"),
412 gold::program_name, opt, msg);
413 this->usage();