Main Page | Class List | Directories | File List | Class Members | File Members

getopt.c File Reference

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "getopt.h"

Go to the source code of this file.

Defines

#define OPTERRCOLON   (1)
#define OPTERRNF   (2)
#define OPTERRARG   (3)

Functions

static int optiserr (int argc, char *const *argv, int oint, const char *optstr, int optchr, int err)
static int longoptiserr (int argc, char *const *argv, int oint, int err)
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)

Variables

char * spamc_optarg
int spamc_optreset = 0
int spamc_optind = 1
int spamc_opterr = 1
int spamc_optopt


Define Documentation

#define OPTERRARG   (3)
 

Definition at line 40 of file getopt.c.

Referenced by longoptiserr(), optiserr(), spamc_getopt(), and spamc_getopt_long().

#define OPTERRCOLON   (1)
 

Definition at line 38 of file getopt.c.

Referenced by longoptiserr(), optiserr(), spamc_getopt(), and spamc_getopt_long().

#define OPTERRNF   (2)
 

Definition at line 39 of file getopt.c.

Referenced by longoptiserr(), optiserr(), spamc_getopt(), and spamc_getopt_long().


Function Documentation

static int longoptiserr int  argc,
char *const *  argv,
int  oint,
int  err
[static]
 

Definition at line 78 of file getopt.c.

References OPTERRARG, OPTERRCOLON, OPTERRNF, and spamc_opterr.

Referenced by spamc_getopt_long().

00079 {
00080     (void) argc;  /* not used */
00081     if(spamc_opterr)
00082     {
00083         fprintf(stderr, "Error in argument %d : ", oint);
00084         switch(err)
00085         {
00086         case OPTERRCOLON:
00087             fprintf(stderr, ": in flags\n");
00088             break;
00089         case OPTERRNF:
00090             fprintf(stderr, "option not found %s\n", argv[oint]);
00091             break;
00092         case OPTERRARG:
00093             fprintf(stderr, "argument required for option %s\n", argv[oint]);
00094             break;
00095         default:
00096             fprintf(stderr, "unknown\n");
00097             break;
00098         }
00099     }
00100     return('?');
00101 }

static int optiserr int  argc,
char *const *  argv,
int  oint,
const char *  optstr,
int  optchr,
int  err
[static]
 

Definition at line 49 of file getopt.c.

References OPTERRARG, OPTERRCOLON, OPTERRNF, spamc_opterr, and spamc_optopt.

Referenced by spamc_getopt(), and spamc_getopt_long().

00051 {
00052     (void) argc;  /* not used */
00053     (void) optstr; /* not used */
00054     if(spamc_opterr)
00055     {
00056         fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
00057         switch(err)
00058         {
00059         case OPTERRCOLON:
00060             fprintf(stderr, ": in flags\n");
00061             break;
00062         case OPTERRNF:
00063             fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
00064             break;
00065         case OPTERRARG:
00066             fprintf(stderr, "argument required for option %c\n", argv[oint][optchr]);
00067             break;
00068         default:
00069             fprintf(stderr, "unknown\n");
00070             break;
00071         }
00072     }
00073     spamc_optopt = argv[oint][optchr];
00074     return('?');
00075 }

int spamc_getopt int  argc,
char *const *  argv,
const char *  optstr
 

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 }

int spamc_getopt_long int  argc,
char *const   argv[],
const char *  optstring,
struct option longopts,
int *  longindex
 

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 }


Variable Documentation

char* spamc_optarg
 

Definition at line 42 of file getopt.c.

Referenced by read_args(), spamc_getopt(), and spamc_getopt_long().

int spamc_opterr = 1
 

Definition at line 45 of file getopt.c.

Referenced by longoptiserr(), and optiserr().

int spamc_optind = 1
 

Definition at line 44 of file getopt.c.

Referenced by read_args(), spamc_getopt(), and spamc_getopt_long().

int spamc_optopt
 

Definition at line 46 of file getopt.c.

Referenced by optiserr().

int spamc_optreset = 0
 

Definition at line 43 of file getopt.c.

Referenced by spamc_getopt(), and spamc_getopt_long().


© sourcejam.com 2005-2008