1 /*****************************************************************
2 * SQUID - a library of functions for biological sequence analysis
3 * Copyright (C) 1992-2002 Washington University School of Medicine
5 * This source code is freely distributed under the terms of the
6 * GNU General Public License. See the files COPYRIGHT and LICENSE
8 *****************************************************************/
10 /* RCS $Id: getopt.c 217 2011-03-19 10:27:10Z andreas $ (Original squid RCS Id: getopt.c,v 1.7 2001/02/21 21:09:10 eddy Exp)
21 * Purpose: Portable command line option parsing with abbreviated
22 * option switches. Replaces UNIX getopt(). Using UNIX getopt()
23 * hinders portability to non-UNIX platforms, and getopt()
24 * is also limited to single letter options.
26 * Getopt() implements a superset of UNIX getopt().
27 * All of getopt()'s single-character switch behavior
28 * is emulated, and "--" by itself terminates the options.
29 * Additionally, Getopt() provides extended switches
30 * like "--youroptionhere", and Getopt() type checks
33 * Extended options must start with "--", as in "--option1".
34 * Normal options must start with "-", as in "-o".
35 * Normal options may be concatenated, as in "-a -b" == "-ab".
37 * See bottom of this .c file after #fdef GETOPT_TESTDRIVER
38 * for an example of calling Getopt().
40 * Args: argc - from main(). number of elems in argv.
41 * argv - from main(). argv[0] is the name of the command.
42 * opt - array of opt_s structures, defining option switches
43 * nopts - number of switches in opt
44 * usage - a (possibly long) string to print if usage error.
45 * ret_optind - RETURN: the index in argv[] of the next
46 * valid command-line token.
47 * ret_optname- RETURN: ptr to the name of option switch
48 * seen, or NULL if no option was seen.
49 * ret_optarg - RETURN: ptr to the optional argument, if any;
50 * NULL if option takes no argument.
52 * Return: 1 if a valid option was parsed.
53 * 0 if no option was found, and command-line parsing is complete.
54 * Die()'s here if an error is detected.
57 Getopt(int argc, char **argv, struct opt_s *opt, int nopts, char *usage,
58 int *ret_optind, char **ret_optname, char **ret_optarg)
63 static int optind = 1; /* init to 1 on first call */
64 static char *optptr = NULL; /* ptr to next valid switch */
65 int opti = 0; /* init only to silence gcc uninit warnings */
67 /* Check to see if we've run out of options.
68 * A '-' by itself is an argument (e.g. "read from stdin")
71 if (optind >= argc || argv[optind][0] != '-' || strcmp(argv[optind], "-") == 0)
79 /* Check to see if we're being told that this is the end
80 * of the options with the special "--" flag.
82 if (strcmp(argv[optind], "--") == 0)
91 /* We have a real option. Find which one it is.
92 * We handle single letter switches "-o" separately
93 * from full switches "--option", based on the "-" vs. "--"
94 * prefix -- single letter switches can be concatenated
95 * as long as they don't have arguments.
98 if (optptr == NULL && strncmp(argv[optind], "--", 2) == 0)
100 /* Use optptr to parse argument in options of form "--foo=666"
102 if ((optptr = strchr(argv[optind], '=')) != NULL)
103 { *optptr = '\0'; optptr++; }
105 arglen = strlen(argv[optind]);
107 for (i = 0; i < nopts; i++)
108 if (opt[i].single == FALSE &&
109 strncmp(opt[i].name, argv[optind], arglen) == 0)
113 if (arglen == strlen(opt[i].name)) break; /* exact match, stop now */
115 if (nmatch > 1 && arglen != strlen(opt[i].name))
116 Die("Option \"%s\" is ambiguous; please be more specific.\n%s",
117 argv[optind], usage);
119 Die("No such option \"%s\".\n%s", argv[optind], usage);
121 *ret_optname = opt[opti].name;
123 /* Set the argument, if there is one
125 if (opt[opti].argtype != sqdARG_NONE)
128 { /* --foo=666 style */
129 *ret_optarg = optptr;
133 else if (optind+1 >= argc)
134 Die("Option %s requires an argument\n%s", opt[opti].name, usage);
135 else /* "--foo 666" style */
137 *ret_optarg = argv[optind+1];
141 else /* sqdARG_NONE */
144 Die("Option %s does not take an argument\n%s", opt[opti].name, usage);
149 else /* else, a single letter option "-o" */
151 /* find the option */
153 optptr = argv[optind]+1;
154 for (opti = -1, i = 0; i < nopts; i++)
155 if (opt[i].single == TRUE && *optptr == opt[i].name[1])
158 Die("No such option \"%c\".\n%s", *optptr, usage);
159 *ret_optname = opt[opti].name;
161 /* set the argument, if there is one */
162 if (opt[opti].argtype != sqdARG_NONE)
164 if (*(optptr+1) != '\0') /* attached argument */
166 *ret_optarg = optptr+1;
169 else if (optind+1 < argc) /* unattached argument */
171 *ret_optarg = argv[optind+1];
174 else Die("Option %s requires an argument\n%s", opt[opti].name, usage);
176 optptr = NULL; /* can't concatenate after an argument */
178 else /* sqdARG_NONE */
181 if (*(optptr+1) != '\0') /* concatenation */
185 optind++; /* move to next field */
192 /* Type check the argument, if there is one
194 if (opt[opti].argtype != sqdARG_NONE)
196 if (opt[opti].argtype == sqdARG_INT && ! IsInt(*ret_optarg))
197 Die("Option %s requires an integer argument\n%s",
198 opt[opti].name, usage);
199 else if (opt[opti].argtype == sqdARG_FLOAT && ! IsReal(*ret_optarg))
200 Die("Option %s requires a numerical argument\n%s",
201 opt[opti].name, usage);
202 else if (opt[opti].argtype == sqdARG_CHAR && strlen(*ret_optarg) != 1)
203 Die("Option %s requires a single-character argument\n%s",
204 opt[opti].name, usage);
205 /* sqdARG_STRING is always ok, no type check necessary */
208 *ret_optind = optind;
214 #ifdef GETOPT_TESTDRIVER
215 /* cc -DGETOPT_TESTDRIVER -L ~/lib/squid.linux/ getopt.c -lsquid
217 struct opt_s OPTIONS[] = {
218 { "--test1", FALSE, sqdARG_INT },
219 { "--test2", FALSE, sqdARG_FLOAT },
220 { "--test3", FALSE, sqdARG_STRING },
221 { "--test4", FALSE, sqdARG_CHAR },
222 { "-a", TRUE, sqdARG_NONE },
223 { "-b", TRUE, sqdARG_INT },
225 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
228 main(int argc, char **argv)
234 while (Getopt(argc, argv, OPTIONS, NOPTIONS, "Usage/help here",
235 &optind, &optname, &optarg))
237 printf("Option: index: %d name: %s argument: %s\n",
238 optind, optname, optarg);
240 while (optind < argc)
242 printf("Argument: index: %d name: %s\n", optind, argv[optind]);
250 #endif /*GETOPT_TESTDRIVER*/