Go to the source code of this file.
Classes | |
| struct | option |
Defines | |
| #define | no_argument (0) |
| #define | required_argument (1) |
| #define | optional_argument (2) |
Functions | |
| int | spamc_getopt (int argc, char *const *argv, const char *optstr) |
| int | spamc_getopt_long (int argc, char *const argv[], const char *optstring, struct option *longopts, int *longindex) |
| int | spamc_getopt_long_only (int argc, char *const *argv, const char *optstr, const struct option *longoptions, int *longopt) |
Variables | |
| char * | spamc_optarg |
| int | spamc_optreset |
| int | spamc_optind |
| int | spamc_opterr |
| int | spamc_optopt |
|
|
Definition at line 46 of file getopt.h. Referenced by read_args(). |
|
|
Definition at line 48 of file getopt.h. Referenced by read_args(), and spamc_getopt_long(). |
|
|
Definition at line 47 of file getopt.h. Referenced by read_args(), and spamc_getopt_long(). |
|
||||||||||||||||
|
Definition at line 104 of file getopt.c. References OPTERRARG, OPTERRCOLON, OPTERRNF, optiserr(), spamc_optarg, spamc_optind, and spamc_optreset. 00105 { 00106 static int optchr = 0; 00107 static int dash = 0; /* have already seen the - */ 00108 00109 char *cp; 00110 00111 if (spamc_optreset) 00112 spamc_optreset = optchr = dash = 0; 00113 if(spamc_optind >= argc) 00114 return(EOF); 00115 if(!dash && (argv[spamc_optind][0] != '-')) 00116 return(EOF); 00117 if(!dash && (argv[spamc_optind][0] == '-') && !argv[spamc_optind][1]) 00118 { 00119 /* 00120 * use to specify stdin. Need to let pgm process this and 00121 * the following args 00122 */ 00123 return(EOF); 00124 } 00125 if((argv[spamc_optind][0] == '-') && (argv[spamc_optind][1] == '-')) 00126 { 00127 /* -- indicates end of args */ 00128 spamc_optind++; 00129 return(EOF); 00130 } 00131 if(!dash) 00132 { 00133 assert((argv[spamc_optind][0] == '-') && argv[spamc_optind][1]); 00134 dash = 1; 00135 optchr = 1; 00136 } 00137 00138 /* Check if the guy tries to do a -: kind of flag */ 00139 assert(dash); 00140 if(argv[spamc_optind][optchr] == ':') 00141 { 00142 dash = 0; 00143 spamc_optind++; 00144 return(optiserr(argc, argv, spamc_optind-1, optstr, optchr, OPTERRCOLON)); 00145 } 00146 cp = strchr(optstr, argv[spamc_optind][optchr]); 00147 if(!cp) 00148 { 00149 int errind = spamc_optind; 00150 int errchr = optchr; 00151 00152 if(!argv[spamc_optind][optchr+1]) 00153 { 00154 dash = 0; 00155 spamc_optind++; 00156 } 00157 else 00158 optchr++; 00159 return(optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); 00160 } 00161 if(cp[1] == ':') 00162 { 00163 dash = 0; 00164 spamc_optind++; 00165 if(spamc_optind == argc) 00166 return(optiserr(argc, argv, spamc_optind-1, optstr, optchr, OPTERRARG)); 00167 spamc_optarg = argv[spamc_optind++]; 00168 return(*cp); 00169 } 00170 else 00171 { 00172 if(!argv[spamc_optind][optchr+1]) 00173 { 00174 dash = 0; 00175 spamc_optind++; 00176 } 00177 else 00178 optchr++; 00179 return(*cp); 00180 } 00181 assert(0); 00182 return(0); 00183 }
|
|
||||||||||||||||||||||||
|
Definition at line 186 of file getopt.c. References option::flag, option::has_arg, longoptiserr(), option::name, OPTERRARG, OPTERRCOLON, OPTERRNF, optional_argument, optiserr(), required_argument, spamc_optarg, spamc_optind, spamc_optreset, and option::val. Referenced by read_args(). 00189 { 00190 static int optchr = 0; 00191 static int dash = 0; 00192 char *cp, *longopt; 00193 char *bp, *opt = NULL; 00194 int i, longoptlen;; 00195 00196 spamc_optarg = NULL; /* clear any left over state from previous option */ 00197 if(spamc_optreset) 00198 spamc_optreset = optchr = dash = 0; 00199 if(spamc_optind >= argc) { 00200 return(EOF); 00201 } 00202 if(!dash && (argv[spamc_optind][0] != '-')) { 00203 return(EOF); 00204 } 00205 if(!dash && (argv[spamc_optind][0] == '-') && !argv[spamc_optind][1]) { 00206 /* used to specify stdin */ 00207 return(EOF); 00208 } 00209 if((argv[spamc_optind][0] == '-') && (argv[spamc_optind][1] == '-') 00210 && !argv[spamc_optind][2]) { 00211 /* used to specify end of args */ 00212 return(EOF); 00213 } 00214 if((argv[spamc_optind][0] == '-') && argv[spamc_optind][1] && 00215 (argv[spamc_optind][1] != '-')) { 00216 /* short option */ 00217 optchr = 1; 00218 if(argv[spamc_optind][optchr] == ':') 00219 return(optiserr(argc, argv, spamc_optind++, optstring, optchr, OPTERRCOLON)); 00220 00221 cp = strchr(optstring, argv[spamc_optind++][optchr]); 00222 if(cp == NULL) 00223 return(optiserr(argc, argv, spamc_optind-1, optstring, optchr, OPTERRNF)); 00224 if(cp[1] == ':') { 00225 /* requires an argument */ 00226 if(!argv[spamc_optind] || (argv[spamc_optind][0] == '-') || 00227 (spamc_optind >= argc)) { 00228 return(optiserr(argc, argv, spamc_optind-1, optstring, optchr, OPTERRARG)); 00229 } 00230 spamc_optarg = argv[spamc_optind++]; 00231 return(*cp); 00232 } else { 00233 dash = 0; 00234 return(*cp); 00235 } 00236 } 00237 if((argv[spamc_optind][0] == '-') && (argv[spamc_optind][1] == '-') && 00238 argv[spamc_optind][2]) { 00239 /* long option */ 00240 optchr = 2; 00241 longopt = argv[spamc_optind++]; 00242 if(longopt[2] == ':') 00243 return(longoptiserr(argc, argv, spamc_optind, OPTERRCOLON)); 00244 longoptlen = strlen(longopt) - 2; 00245 if((bp = strchr(longopt, '='))) { 00246 opt = strdup(bp+1); 00247 longoptlen -= strlen(bp); 00248 } 00249 00250 for(i=1; ; i++) { 00251 if((longopts[i].name == NULL) || (longopts[i].name == 0)) 00252 return(longoptiserr(argc, argv, spamc_optind-1, OPTERRNF)); 00253 if((memcmp(longopt+2, longopts[i].name, longoptlen)) == 0) { 00254 *longindex = i; 00255 if(longopts[i].has_arg == required_argument) { 00256 if(((spamc_optind >= argc) || (!argv[spamc_optind]) || (argv[spamc_optind][0] == '-')) && 00257 (opt == NULL)) 00258 return(longoptiserr(argc, argv, spamc_optind-1, OPTERRARG)); 00259 if(opt != NULL) { 00260 spamc_optarg = opt; 00261 } else { 00262 spamc_optarg = argv[spamc_optind++]; 00263 } 00264 } else if(longopts[i].has_arg == optional_argument) { 00265 if(((spamc_optind < argc) && (argv[spamc_optind]) && (argv[spamc_optind][0] != '-')) || 00266 (opt != NULL)) { 00267 if(opt != NULL) { 00268 spamc_optarg = opt; 00269 } else { 00270 spamc_optarg = argv[spamc_optind++]; 00271 } 00272 } 00273 } 00274 if(longopts[i].flag == NULL) { 00275 return(longopts[i].val); 00276 } else { 00277 *longopts[i].flag = longopts[i].val; 00278 return(0); 00279 } 00280 } 00281 } 00282 } 00283 return(0); /* should never reach here */ 00284 }
|
|
||||||||||||||||||||||||
|
|
|
|
Definition at line 42 of file getopt.c. Referenced by read_args(), spamc_getopt(), and spamc_getopt_long(). |
|
|
Definition at line 45 of file getopt.c. Referenced by longoptiserr(), and optiserr(). |
|
|
Definition at line 44 of file getopt.c. Referenced by read_args(), spamc_getopt(), and spamc_getopt_long(). |
|
|
Definition at line 46 of file getopt.c. Referenced by optiserr(). |
|
|
Definition at line 43 of file getopt.c. Referenced by spamc_getopt(), and spamc_getopt_long(). |