… | |
… | |
74 | /* The shells recognized. */ |
74 | /* The shells recognized. */ |
75 | typedef enum { BASH, TCSH } shell_t; |
75 | typedef enum { BASH, TCSH } shell_t; |
76 | |
76 | |
77 | |
77 | |
78 | /* Some global variables that tells us how to parse. */ |
78 | /* Some global variables that tells us how to parse. */ |
79 | shell_t shell = BASH; /* The shell we generate output for. */ |
79 | static shell_t shell = BASH; /* The shell we generate output for. */ |
80 | int quiet_errors = 0; /* 0 is not quiet. */ |
80 | static int quiet_errors = 0; /* 0 is not quiet. */ |
81 | int quiet_output = 0; /* 0 is not quiet. */ |
81 | static int quiet_output = 0; /* 0 is not quiet. */ |
82 | int quote = 1; /* 1 is do quote. */ |
82 | static int quote = 1; /* 1 is do quote. */ |
83 | |
83 | |
84 | int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, |
84 | int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, |
85 | const struct option * longopts, int *longindex); |
85 | const struct option * longopts, int *longindex); |
86 | |
86 | |
87 | /* Function prototypes */ |
87 | /* Function prototypes */ |
88 | void *our_malloc(size_t size); |
88 | void *our_malloc(size_t size); |
89 | void *our_realloc(void *ptr, size_t size); |
89 | void *our_realloc(void *ptr, size_t size); |
90 | const char *normalize(const char *arg); |
90 | static const char *normalize(const char *arg); |
91 | int generate_output(char * argv[], int argc, const char *optstr, |
91 | static int generate_output(char *argv[], int argc, const char *optstr, |
92 | const struct option *longopts); |
92 | const struct option *longopts); |
93 | int main(int argc, char *argv[]); |
93 | int main(int argc, char *argv[]); |
94 | void parse_error(const char *message); |
94 | static void parse_error(const char *message); |
95 | void add_long_options(char *options); |
95 | static void add_long_options(char *options); |
96 | void add_longopt(const char *name, int has_arg); |
96 | static void add_longopt(const char *name, int has_arg); |
97 | void print_help(void); |
97 | static void print_help(void); |
98 | void set_shell(const char *new_shell); |
98 | static void set_shell(const char *new_shell); |
99 | void set_initial_shell(void); |
|
|
100 | |
99 | |
101 | void *our_malloc(size_t size) |
100 | void *our_malloc(size_t size) |
102 | { |
101 | { |
103 | void *ret=malloc(size); |
102 | void *ret=malloc(size); |
104 | if (! ret) { |
103 | if (! ret) { |
… | |
… | |
125 | * |
124 | * |
126 | * Bash only needs special treatment for single quotes; tcsh also recognizes |
125 | * Bash only needs special treatment for single quotes; tcsh also recognizes |
127 | * exclamation marks within single quotes, and nukes whitespace. This |
126 | * exclamation marks within single quotes, and nukes whitespace. This |
128 | * function returns a pointer to a buffer that is overwritten by each call. |
127 | * function returns a pointer to a buffer that is overwritten by each call. |
129 | */ |
128 | */ |
130 | const char *normalize(const char *arg) |
129 | static const char *normalize(const char *arg) |
131 | { |
130 | { |
132 | static char *BUFFER = NULL; |
131 | static char *BUFFER = NULL; |
133 | const char *argptr = arg; |
132 | const char *argptr = arg; |
134 | char *bufptr; |
133 | char *bufptr; |
135 | |
134 | |
… | |
… | |
193 | * argv[1..] contains the options to be parsed. argc must be the number of |
192 | * argv[1..] contains the options to be parsed. argc must be the number of |
194 | * elements in argv (ie. 1 if there are no options, only the program name), |
193 | * elements in argv (ie. 1 if there are no options, only the program name), |
195 | * optstr must contain the short options, and longopts the long options. |
194 | * optstr must contain the short options, and longopts the long options. |
196 | * Other settings are found in global variables. |
195 | * Other settings are found in global variables. |
197 | */ |
196 | */ |
198 | int generate_output(char * argv[], int argc, const char *optstr, |
197 | static int generate_output(char *argv[], int argc, const char *optstr, |
199 | const struct option *longopts) |
198 | const struct option *longopts) |
200 | { |
199 | { |
201 | int exit_code = EXIT_SUCCESS; /* Assume everything will be OK */ |
200 | int exit_code = EXIT_SUCCESS; /* Assume everything will be OK */ |
202 | int opt; |
201 | int opt; |
203 | int longindex; |
202 | int longindex; |
204 | const char *charptr; |
203 | const char *charptr; |
… | |
… | |
240 | |
239 | |
241 | /* |
240 | /* |
242 | * Report an error when parsing getopt's own arguments. If message is NULL, |
241 | * Report an error when parsing getopt's own arguments. If message is NULL, |
243 | * we already sent a message, we just exit with a helpful hint. |
242 | * we already sent a message, we just exit with a helpful hint. |
244 | */ |
243 | */ |
245 | void parse_error(const char *message) |
244 | static void __attribute__ ((__noreturn__)) parse_error(const char *message) |
246 | { |
245 | { |
247 | if (message) |
246 | if (message) |
248 | fprintf(stderr, "getopt: %s\n", message); |
247 | fprintf(stderr, "getopt: %s\n", message); |
249 | fputs(_("Try `getopt --help' for more information.\n"), stderr); |
248 | fputs(_("Try `getopt --help' for more information.\n"), stderr); |
250 | exit(PARAMETER_EXIT_CODE); |
249 | exit(PARAMETER_EXIT_CODE); |
… | |
… | |
255 | static int long_options_nr = 0; /* Nr of used elements in array */ |
254 | static int long_options_nr = 0; /* Nr of used elements in array */ |
256 | #define LONG_OPTIONS_INCR 10 |
255 | #define LONG_OPTIONS_INCR 10 |
257 | #define init_longopt() add_longopt(NULL,0) |
256 | #define init_longopt() add_longopt(NULL,0) |
258 | |
257 | |
259 | /* Register a long option. The contents of name is copied. */ |
258 | /* Register a long option. The contents of name is copied. */ |
260 | void add_longopt(const char *name, int has_arg) |
259 | static void add_longopt(const char *name, int has_arg) |
261 | { |
260 | { |
262 | char *tmp; |
261 | char *tmp; |
263 | if (!name) { |
262 | if (!name) { |
264 | /* init */ |
263 | /* init */ |
265 | free(long_options); |
264 | free(long_options); |
… | |
… | |
295 | |
294 | |
296 | /* |
295 | /* |
297 | * Register several long options. options is a string of long options, |
296 | * Register several long options. options is a string of long options, |
298 | * separated by commas or whitespace. This nukes options! |
297 | * separated by commas or whitespace. This nukes options! |
299 | */ |
298 | */ |
300 | void add_long_options(char *options) |
299 | static void add_long_options(char *options) |
301 | { |
300 | { |
302 | int arg_opt; |
301 | int arg_opt; |
303 | char *tokptr = strtok(options, ", \t\n"); |
302 | char *tokptr = strtok(options, ", \t\n"); |
304 | while (tokptr) { |
303 | while (tokptr) { |
305 | arg_opt = no_argument; |
304 | arg_opt = no_argument; |
… | |
… | |
321 | } |
320 | } |
322 | tokptr = strtok(NULL, ", \t\n"); |
321 | tokptr = strtok(NULL, ", \t\n"); |
323 | } |
322 | } |
324 | } |
323 | } |
325 | |
324 | |
326 | void set_shell(const char *new_shell) |
325 | static void set_shell(const char *new_shell) |
327 | { |
326 | { |
328 | if (!strcmp(new_shell, "bash")) |
327 | if (!strcmp(new_shell, "bash")) |
329 | shell = BASH; |
328 | shell = BASH; |
330 | else if (!strcmp(new_shell, "tcsh")) |
329 | else if (!strcmp(new_shell, "tcsh")) |
331 | shell = TCSH; |
330 | shell = TCSH; |
… | |
… | |
336 | else |
335 | else |
337 | parse_error(_ |
336 | parse_error(_ |
338 | ("unknown shell after -s or --shell argument")); |
337 | ("unknown shell after -s or --shell argument")); |
339 | } |
338 | } |
340 | |
339 | |
341 | void print_help(void) |
340 | static void __attribute__ ((__noreturn__)) print_help(void) |
342 | { |
341 | { |
343 | fputs(_("Usage: getopt optstring parameters\n"), stderr); |
342 | fputs(_("Usage: getopt optstring parameters\n"), stderr); |
344 | fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); |
343 | fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); |
345 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); |
344 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); |
346 | fputs(_(" parameters\n"), stderr); |
345 | fputs(_(" parameters\n"), stderr); |
… | |
… | |
356 | fputs(_(" -u, --unqote Do not quote the output\n"), stderr); |
355 | fputs(_(" -u, --unqote Do not quote the output\n"), stderr); |
357 | fputs(_(" -V, --version Output version information\n"), stderr); |
356 | fputs(_(" -V, --version Output version information\n"), stderr); |
358 | exit(PARAMETER_EXIT_CODE); |
357 | exit(PARAMETER_EXIT_CODE); |
359 | } |
358 | } |
360 | |
359 | |
|
|
360 | int main(int argc, char *argv[]) |
|
|
361 | { |
|
|
362 | char *optstr = NULL; |
|
|
363 | char *name = NULL; |
|
|
364 | int opt; |
|
|
365 | int compatible = 0; |
|
|
366 | |
|
|
367 | /* Stop scanning as soon as a non-option argument is found! */ |
|
|
368 | static const char *shortopts = "+ao:l:n:qQs:TuhV"; |
361 | static struct option longopts[] = { |
369 | static const struct option longopts[] = { |
362 | {"options", required_argument, NULL, 'o'}, |
370 | {"options", required_argument, NULL, 'o'}, |
363 | {"longoptions", required_argument, NULL, 'l'}, |
371 | {"longoptions", required_argument, NULL, 'l'}, |
364 | {"quiet", no_argument, NULL, 'q'}, |
372 | {"quiet", no_argument, NULL, 'q'}, |
365 | {"quiet-output", no_argument, NULL, 'Q'}, |
373 | {"quiet-output", no_argument, NULL, 'Q'}, |
366 | {"shell", required_argument, NULL, 's'}, |
374 | {"shell", required_argument, NULL, 's'}, |
… | |
… | |
370 | {"alternative", no_argument, NULL, 'a'}, |
378 | {"alternative", no_argument, NULL, 'a'}, |
371 | {"name", required_argument, NULL, 'n'}, |
379 | {"name", required_argument, NULL, 'n'}, |
372 | {"version", no_argument, NULL, 'V'}, |
380 | {"version", no_argument, NULL, 'V'}, |
373 | {NULL, 0, NULL, 0} |
381 | {NULL, 0, NULL, 0} |
374 | }; |
382 | }; |
375 | |
|
|
376 | /* Stop scanning as soon as a non-option argument is found! */ |
|
|
377 | static const char *shortopts = "+ao:l:n:qQs:TuhV"; |
|
|
378 | |
|
|
379 | int main(int argc, char *argv[]) |
|
|
380 | { |
|
|
381 | char *optstr = NULL; |
|
|
382 | char *name = NULL; |
|
|
383 | int opt; |
|
|
384 | int compatible = 0; |
|
|
385 | |
|
|
386 | #if WITHOUT_GETTEXT |
383 | #if WITHOUT_GETTEXT |
387 | #else |
384 | #else |
388 | setlocale(LC_ALL, ""); |
385 | setlocale(LC_ALL, ""); |
389 | bindtextdomain(PACKAGE, LOCALEDIR); |
386 | bindtextdomain(PACKAGE, LOCALEDIR); |
390 | textdomain(PACKAGE); |
387 | textdomain(PACKAGE); |