/[public]/getopt/trunk/getopt.c
ViewVC logotype

Diff of /getopt/trunk/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 320 Revision 321
74/* The shells recognized. */ 74/* The shells recognized. */
75typedef enum { BASH, TCSH } shell_t; 75typedef 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. */
79shell_t shell = BASH; /* The shell we generate output for. */ 79static shell_t shell = BASH; /* The shell we generate output for. */
80int quiet_errors = 0; /* 0 is not quiet. */ 80static int quiet_errors = 0; /* 0 is not quiet. */
81int quiet_output = 0; /* 0 is not quiet. */ 81static int quiet_output = 0; /* 0 is not quiet. */
82int quote = 1; /* 1 is do quote. */ 82static int quote = 1; /* 1 is do quote. */
83 83
84int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, 84int (*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 */
88void *our_malloc(size_t size); 88void *our_malloc(size_t size);
89void *our_realloc(void *ptr, size_t size); 89void *our_realloc(void *ptr, size_t size);
90const char *normalize(const char *arg); 90static const char *normalize(const char *arg);
91int generate_output(char * argv[], int argc, const char *optstr, 91static int generate_output(char *argv[], int argc, const char *optstr,
92 const struct option *longopts); 92 const struct option *longopts);
93int main(int argc, char *argv[]); 93int main(int argc, char *argv[]);
94void parse_error(const char *message); 94static void parse_error(const char *message);
95void add_long_options(char *options); 95static void add_long_options(char *options);
96void add_longopt(const char *name, int has_arg); 96static void add_longopt(const char *name, int has_arg);
97void print_help(void); 97static void print_help(void);
98void set_shell(const char *new_shell); 98static void set_shell(const char *new_shell);
99void set_initial_shell(void);
100 99
101void *our_malloc(size_t size) 100void *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 */
130const char *normalize(const char *arg) 129static 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 */
198int generate_output(char * argv[], int argc, const char *optstr, 197static 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 */
245void parse_error(const char *message) 244static 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);
255static int long_options_nr = 0; /* Nr of used elements in array */ 254static 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. */
260void add_longopt(const char *name, int has_arg) 259static 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 */
300void add_long_options(char *options) 299static 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
326void set_shell(const char *new_shell) 325static 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
341void print_help(void) 340static 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
360int 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";
361static 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! */
377static const char *shortopts = "+ao:l:n:qQs:TuhV";
378
379int 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);

Legend:
Removed from v.320  
changed lines
  Added in v.321

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26