/[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 259 Revision 317
1/* 1/*
2 getopt.c - Enhanced implementation of BSD getopt(1) 2 * getopt.c - Enhanced implementation of BSD getopt(1)
3 Copyright (c) 1997, 1998, 1999, 2000 Frodo Looijaard <frodol@dds.nl> 3 * Copyright (c) 1997-2005 Frodo Looijaard <frodo@frodo.looijaard.name>
4 4 *
5 This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 * (at your option) any later version.
9 9 *
10 This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 13 * GNU General Public License for more details.
14 14 *
15 You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 16 * along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/ 18 */
19 19
20/* 20/*
21 * Version 1.0-b4: Tue Sep 23 1997. First public release. 21 * Version 1.0-b4: Tue Sep 23 1997. First public release.
22 * Version 1.0: Wed Nov 19 1997. 22 * Version 1.0: Wed Nov 19 1997.
23 * Bumped up the version number to 1.0 23 * Bumped up the version number to 1.0
33 * Version 1.0.6: Tue Jun 27 2000 33 * Version 1.0.6: Tue Jun 27 2000
34 * No important changes 34 * No important changes
35 * Version 1.1.0: Tue Jun 30 2000 35 * Version 1.1.0: Tue Jun 30 2000
36 * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz 36 * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz
37 * <misiek@pld.org.pl>) 37 * <misiek@pld.org.pl>)
38 * Version 1.1.4: Mon Nov 7 2005
39 * Fixed a few type's in the manpage
38 */ 40 */
39 41
40#include <stdio.h> 42#include <stdio.h>
41#include <stdlib.h> 43#include <stdlib.h>
42#include <string.h> 44#include <string.h>
50#endif 52#endif
51 53
52#include "nls.h" 54#include "nls.h"
53 55
54/* NON_OPT is the code that is returned when a non-option is found in '+' 56/* NON_OPT is the code that is returned when a non-option is found in '+'
55 mode */ 57 * mode */
56#define NON_OPT 1 58#define NON_OPT 1
57/* LONG_OPT is the code that is returned when a long option is found. */ 59/* LONG_OPT is the code that is returned when a long option is found. */
58#define LONG_OPT 2 60#define LONG_OPT 2
59 61
60/* The shells recognized. */ 62/* The shells recognized. */
61typedef enum {BASH,TCSH} shell_t; 63typedef enum {BASH,TCSH} shell_t;
62 64
63 65
64/* Some global variables that tells us how to parse. */ 66/* Some global variables that tells us how to parse. */
65shell_t shell=BASH; /* The shell we generate output for. */ 67shell_t shell=BASH; /* The shell we generate output for. */
66int quiet_errors=0; /* 0 is not quiet. */ 68int quiet_errors=0; /* 0 is not quiet. */
67int quiet_output=0; /* 0 is not quiet. */ 69int quiet_output=0; /* 0 is not quiet. */
68int quote=1; /* 1 is do quote. */ 70int quote=1; /* 1 is do quote. */
69int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */ 71int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
70 72
71/* Function prototypes */ 73/* Function prototypes */
72void *our_malloc(size_t size); 74void *our_malloc(size_t size);
73void *our_realloc(void *ptr, size_t size); 75void *our_realloc(void *ptr, size_t size);
74const char *normalize(const char *arg); 76const char *normalize(const char *arg);
101 } 103 }
102 return(ret); 104 return(ret);
103} 105}
104 106
105/* 107/*
106 * This function 'normalizes' a single argument: it puts single quotes around 108 * This function 'normalizes' a single argument: it puts single quotes
107 * it and escapes other special characters. If quote is false, it just 109 * around it and escapes other special characters. If quote is false, it
108 * returns its argument. 110 * just returns its argument.
111 *
109 * Bash only needs special treatment for single quotes; tcsh also recognizes 112 * Bash only needs special treatment for single quotes; tcsh also recognizes
110 * exclamation marks within single quotes, and nukes whitespace. 113 * exclamation marks within single quotes, and nukes whitespace. This
111 * This function returns a pointer to a buffer that is overwritten by 114 * function returns a pointer to a buffer that is overwritten by each call.
112 * each call.
113 */ 115 */
114const char *normalize(const char *arg) 116const char *normalize(const char *arg)
115{ 117{
116 static char *BUFFER=NULL; 118 static char *BUFFER=NULL;
117 const char *argptr=arg; 119 const char *argptr=arg;
118 char *bufptr; 120 char *bufptr;
119 121
120 if (BUFFER != NULL) 122 if (BUFFER != NULL)
121 free(BUFFER); 123 free(BUFFER);
122 124
123 if (!quote) { /* Just copy arg */ 125 if (!quote) {
126 /* Just copy arg */
124 BUFFER=our_malloc(strlen(arg)+1); 127 BUFFER=our_malloc(strlen(arg)+1);
125 128
126 strcpy(BUFFER,arg); 129 strcpy(BUFFER,arg);
127 return BUFFER; 130 return BUFFER;
128 } 131 }
129 132
133 /*
130 /* Each character in arg may take upto four characters in the result: 134 * Each character in arg may take upto four characters in the
131 For a quote we need a closing quote, a backslash, a quote and an 135 * result: For a quote we need a closing quote, a backslash, a quote
132 opening quote! We need also the global opening and closing quote, 136 * and an opening quote! We need also the global opening and closing
133 and one extra character for '\0'. */ 137 * quote, and one extra character for '\0'.
138 */
134 BUFFER=our_malloc(strlen(arg)*4+3); 139 BUFFER=our_malloc(strlen(arg)*4+3);
135 140
136 bufptr=BUFFER; 141 bufptr=BUFFER;
137 *bufptr++='\''; 142 *bufptr++='\'';
138 143
177 * Other settings are found in global variables. 182 * Other settings are found in global variables.
178 */ 183 */
179int generate_output(char * argv[],int argc,const char *optstr, 184int generate_output(char * argv[],int argc,const char *optstr,
180 const struct option *longopts) 185 const struct option *longopts)
181{ 186{
182 int exit_code = 0; /* We assume everything will be OK */ 187 int exit_code = 0; /* Assume everything will be OK */
183 int opt; 188 int opt;
184 int longindex; 189 int longindex;
185 const char *charptr; 190 const char *charptr;
186 191
187 if (quiet_errors) /* No error reporting from getopt(3) */ 192 if (quiet_errors)
193 /* No error reporting from getopt(3) */
188 opterr=0; 194 opterr=0;
189 optind=0; /* Reset getopt(3) */ 195 /* Reset getopt(3) */
196 optind=0;
190 197
191 while ((opt = (alternative? 198 while ((opt = (alternative?
192 getopt_long_only(argc,argv,optstr,longopts,&longindex): 199 getopt_long_only(argc,argv,optstr,longopts,&longindex):
193 getopt_long(argc,argv,optstr,longopts,&longindex))) 200 getopt_long(argc,argv,optstr,longopts,&longindex)))
194 != EOF) 201 != EOF)
220 } 227 }
221 return exit_code; 228 return exit_code;
222} 229}
223 230
224/* 231/*
225 * Report an error when parsing getopt's own arguments. 232 * Report an error when parsing getopt's own arguments. If message is NULL,
226 * If message is NULL, we already sent a message, we just exit with a helpful 233 * we already sent a message, we just exit with a helpful hint.
227 * hint.
228 */ 234 */
229void parse_error(const char *message) 235void parse_error(const char *message)
230{ 236{
231 if (message) 237 if (message)
232 fprintf(stderr,"getopt: %s\n",message); 238 fprintf(stderr,"getopt: %s\n",message);
242 248
243/* Register a long option. The contents of name is copied. */ 249/* Register a long option. The contents of name is copied. */
244void add_longopt(const char *name,int has_arg) 250void add_longopt(const char *name,int has_arg)
245{ 251{
246 char *tmp; 252 char *tmp;
247 if (!name) { /* init */ 253 if (!name) {
254 /* init */
248 free(long_options); 255 free(long_options);
249 long_options=NULL; 256 long_options=NULL;
250 long_options_length=0; 257 long_options_length=0;
251 long_options_nr=0; 258 long_options_nr=0;
252 } 259 }
261 long_options[long_options_nr].name=NULL; 268 long_options[long_options_nr].name=NULL;
262 long_options[long_options_nr].has_arg=0; 269 long_options[long_options_nr].has_arg=0;
263 long_options[long_options_nr].flag=NULL; 270 long_options[long_options_nr].flag=NULL;
264 long_options[long_options_nr].val=0; 271 long_options[long_options_nr].val=0;
265 272
266 if (long_options_nr) { /* Not for init! */ 273 if (long_options_nr) {
274 /* Not for init! */
267 long_options[long_options_nr-1].has_arg=has_arg; 275 long_options[long_options_nr-1].has_arg=has_arg;
268 long_options[long_options_nr-1].flag=NULL; 276 long_options[long_options_nr-1].flag=NULL;
269 long_options[long_options_nr-1].val=LONG_OPT; 277 long_options[long_options_nr-1].val=LONG_OPT;
270 tmp = our_malloc(strlen(name)+1); 278 tmp = our_malloc(strlen(name)+1);
271 strcpy(tmp,name); 279 strcpy(tmp,name);
274 long_options_nr++; 282 long_options_nr++;
275} 283}
276 284
277 285
278/* 286/*
279 * Register several long options. options is a string of long options, 287 * Register several long options. options is a string of long options,
280 * separated by commas or whitespace. 288 * separated by commas or whitespace. This nukes options!
281 * This nukes options!
282 */ 289 */
283void add_long_options(char *options) 290void add_long_options(char *options)
284{ 291{
285 int arg_opt; 292 int arg_opt;
286 char *tokptr=strtok(options,", \t\n"); 293 char *tokptr=strtok(options,", \t\n");
384 compatible=1; 391 compatible=1;
385 392
386 if (argc == 1) 393 if (argc == 1)
387 { 394 {
388 if (compatible) { 395 if (compatible) {
396 /*
389 /* For some reason, the original getopt gave no error 397 * For some reason, the original getopt gave no error
390 when there were no arguments. */ 398 * when there were no arguments.
399 */
391 printf(" --\n"); 400 printf(" --\n");
392 exit(0); 401 exit(0);
393 } 402 }
394 else 403 else
395 parse_error(_("missing optstring argument")); 404 parse_error(_("missing optstring argument"));
439 exit(4); 448 exit(4);
440 case 'u': 449 case 'u':
441 quote=0; 450 quote=0;
442 break; 451 break;
443 case 'V': 452 case 'V':
444 printf(_("getopt (enhanced) 1.1.3\n")); 453 printf(_("getopt (enhanced) 1.1.4\n"));
445 exit(0); 454 exit(0);
446 case '?': 455 case '?':
447 case ':': 456 case ':':
448 parse_error(NULL); 457 parse_error(NULL);
449 default: 458 default:

Legend:
Removed from v.259  
changed lines
  Added in v.317

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