/[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 263 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-2005 Frodo Looijaard <frodo@frodo.looijaard.name> 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
52#endif 52#endif
53 53
54#include "nls.h" 54#include "nls.h"
55 55
56/* 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 '+'
57 mode */ 57 * mode */
58#define NON_OPT 1 58#define NON_OPT 1
59/* 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. */
60#define LONG_OPT 2 60#define LONG_OPT 2
61 61
62/* The shells recognized. */ 62/* The shells recognized. */
63typedef enum {BASH,TCSH} shell_t; 63typedef enum {BASH,TCSH} shell_t;
64 64
65 65
66/* Some global variables that tells us how to parse. */ 66/* Some global variables that tells us how to parse. */
67shell_t shell=BASH; /* The shell we generate output for. */ 67shell_t shell=BASH; /* The shell we generate output for. */
68int quiet_errors=0; /* 0 is not quiet. */ 68int quiet_errors=0; /* 0 is not quiet. */
69int quiet_output=0; /* 0 is not quiet. */ 69int quiet_output=0; /* 0 is not quiet. */
70int quote=1; /* 1 is do quote. */ 70int quote=1; /* 1 is do quote. */
71int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */ 71int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
72 72
73/* Function prototypes */ 73/* Function prototypes */
74void *our_malloc(size_t size); 74void *our_malloc(size_t size);
75void *our_realloc(void *ptr, size_t size); 75void *our_realloc(void *ptr, size_t size);
76const char *normalize(const char *arg); 76const char *normalize(const char *arg);
103 } 103 }
104 return(ret); 104 return(ret);
105} 105}
106 106
107/* 107/*
108 * This function 'normalizes' a single argument: it puts single quotes around 108 * This function 'normalizes' a single argument: it puts single quotes
109 * 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
110 * returns its argument. 110 * just returns its argument.
111 *
111 * Bash only needs special treatment for single quotes; tcsh also recognizes 112 * Bash only needs special treatment for single quotes; tcsh also recognizes
112 * exclamation marks within single quotes, and nukes whitespace. 113 * exclamation marks within single quotes, and nukes whitespace. This
113 * 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.
114 * each call.
115 */ 115 */
116const char *normalize(const char *arg) 116const char *normalize(const char *arg)
117{ 117{
118 static char *BUFFER=NULL; 118 static char *BUFFER=NULL;
119 const char *argptr=arg; 119 const char *argptr=arg;
120 char *bufptr; 120 char *bufptr;
121 121
122 if (BUFFER != NULL) 122 if (BUFFER != NULL)
123 free(BUFFER); 123 free(BUFFER);
124 124
125 if (!quote) { /* Just copy arg */ 125 if (!quote) {
126 /* Just copy arg */
126 BUFFER=our_malloc(strlen(arg)+1); 127 BUFFER=our_malloc(strlen(arg)+1);
127 128
128 strcpy(BUFFER,arg); 129 strcpy(BUFFER,arg);
129 return BUFFER; 130 return BUFFER;
130 } 131 }
131 132
133 /*
132 /* Each character in arg may take upto four characters in the result: 134 * Each character in arg may take upto four characters in the
133 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
134 opening quote! We need also the global opening and closing quote, 136 * and an opening quote! We need also the global opening and closing
135 and one extra character for '\0'. */ 137 * quote, and one extra character for '\0'.
138 */
136 BUFFER=our_malloc(strlen(arg)*4+3); 139 BUFFER=our_malloc(strlen(arg)*4+3);
137 140
138 bufptr=BUFFER; 141 bufptr=BUFFER;
139 *bufptr++='\''; 142 *bufptr++='\'';
140 143
179 * Other settings are found in global variables. 182 * Other settings are found in global variables.
180 */ 183 */
181int generate_output(char * argv[],int argc,const char *optstr, 184int generate_output(char * argv[],int argc,const char *optstr,
182 const struct option *longopts) 185 const struct option *longopts)
183{ 186{
184 int exit_code = 0; /* We assume everything will be OK */ 187 int exit_code = 0; /* Assume everything will be OK */
185 int opt; 188 int opt;
186 int longindex; 189 int longindex;
187 const char *charptr; 190 const char *charptr;
188 191
189 if (quiet_errors) /* No error reporting from getopt(3) */ 192 if (quiet_errors)
193 /* No error reporting from getopt(3) */
190 opterr=0; 194 opterr=0;
191 optind=0; /* Reset getopt(3) */ 195 /* Reset getopt(3) */
196 optind=0;
192 197
193 while ((opt = (alternative? 198 while ((opt = (alternative?
194 getopt_long_only(argc,argv,optstr,longopts,&longindex): 199 getopt_long_only(argc,argv,optstr,longopts,&longindex):
195 getopt_long(argc,argv,optstr,longopts,&longindex))) 200 getopt_long(argc,argv,optstr,longopts,&longindex)))
196 != EOF) 201 != EOF)
222 } 227 }
223 return exit_code; 228 return exit_code;
224} 229}
225 230
226/* 231/*
227 * Report an error when parsing getopt's own arguments. 232 * Report an error when parsing getopt's own arguments. If message is NULL,
228 * 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.
229 * hint.
230 */ 234 */
231void parse_error(const char *message) 235void parse_error(const char *message)
232{ 236{
233 if (message) 237 if (message)
234 fprintf(stderr,"getopt: %s\n",message); 238 fprintf(stderr,"getopt: %s\n",message);
244 248
245/* Register a long option. The contents of name is copied. */ 249/* Register a long option. The contents of name is copied. */
246void add_longopt(const char *name,int has_arg) 250void add_longopt(const char *name,int has_arg)
247{ 251{
248 char *tmp; 252 char *tmp;
249 if (!name) { /* init */ 253 if (!name) {
254 /* init */
250 free(long_options); 255 free(long_options);
251 long_options=NULL; 256 long_options=NULL;
252 long_options_length=0; 257 long_options_length=0;
253 long_options_nr=0; 258 long_options_nr=0;
254 } 259 }
263 long_options[long_options_nr].name=NULL; 268 long_options[long_options_nr].name=NULL;
264 long_options[long_options_nr].has_arg=0; 269 long_options[long_options_nr].has_arg=0;
265 long_options[long_options_nr].flag=NULL; 270 long_options[long_options_nr].flag=NULL;
266 long_options[long_options_nr].val=0; 271 long_options[long_options_nr].val=0;
267 272
268 if (long_options_nr) { /* Not for init! */ 273 if (long_options_nr) {
274 /* Not for init! */
269 long_options[long_options_nr-1].has_arg=has_arg; 275 long_options[long_options_nr-1].has_arg=has_arg;
270 long_options[long_options_nr-1].flag=NULL; 276 long_options[long_options_nr-1].flag=NULL;
271 long_options[long_options_nr-1].val=LONG_OPT; 277 long_options[long_options_nr-1].val=LONG_OPT;
272 tmp = our_malloc(strlen(name)+1); 278 tmp = our_malloc(strlen(name)+1);
273 strcpy(tmp,name); 279 strcpy(tmp,name);
276 long_options_nr++; 282 long_options_nr++;
277} 283}
278 284
279 285
280/* 286/*
281 * Register several long options. options is a string of long options, 287 * Register several long options. options is a string of long options,
282 * separated by commas or whitespace. 288 * separated by commas or whitespace. This nukes options!
283 * This nukes options!
284 */ 289 */
285void add_long_options(char *options) 290void add_long_options(char *options)
286{ 291{
287 int arg_opt; 292 int arg_opt;
288 char *tokptr=strtok(options,", \t\n"); 293 char *tokptr=strtok(options,", \t\n");
386 compatible=1; 391 compatible=1;
387 392
388 if (argc == 1) 393 if (argc == 1)
389 { 394 {
390 if (compatible) { 395 if (compatible) {
396 /*
391 /* For some reason, the original getopt gave no error 397 * For some reason, the original getopt gave no error
392 when there were no arguments. */ 398 * when there were no arguments.
399 */
393 printf(" --\n"); 400 printf(" --\n");
394 exit(0); 401 exit(0);
395 } 402 }
396 else 403 else
397 parse_error(_("missing optstring argument")); 404 parse_error(_("missing optstring argument"));

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

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