nco/nco_getopt.c File Reference

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nco_getopt.h"

Include dependency graph for nco_getopt.c:

Go to the source code of this file.

Functions

int my_getopt (int argc, char *argv[], const char *opts)
int _my_getopt_internal (int argc, char *argv[], const char *shortopts, const struct option *longopts, int *longind, int long_only)
int my_getopt_long (int argc, char *argv[], const char *shortopts, const struct option *longopts, int *longind)
int my_getopt_long_only (int argc, char *argv[], const char *shortopts, const struct option *longopts, int *longind)

Variables

int my_optind = 1
int my_opterr = 1
int my_optopt = 0
char * my_optarg = 0


Function Documentation

int _my_getopt_internal int  argc,
char *  argv[],
const char *  shortopts,
const struct option longopts,
int *  longind,
int  long_only
 

Definition at line 146 of file nco_getopt.c.

References _my_getopt_internal(), option::flag, option::has_arg, my_getopt(), my_optarg, my_opterr, my_optind, my_optopt, option::name, and option::val.

Referenced by _my_getopt_internal(), my_getopt_long(), and my_getopt_long_only().

00149 {
00150   char mode, colon_mode = *shortopts;
00151   int shortoff = 0, opt = -1;
00152 
00153   if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+';
00154   else {
00155     if((colon_mode = *shortopts) == ':') shortoff ++;
00156     if(((mode = shortopts[shortoff]) == '+') || (mode == '-')) {
00157       shortoff++;
00158       if((colon_mode != ':') && ((colon_mode = shortopts[shortoff]) == ':'))
00159         shortoff ++;
00160     }
00161   }
00162   my_optarg = 0;
00163   if((my_optind >= argc) ||
00164       ((argv[my_optind][0] == '-') &&
00165        (argv[my_optind][1] == '-') &&
00166        (argv[my_optind][2] == '\0'))) {
00167     my_optind++;
00168     opt = -1;
00169   } else if((argv[my_optind][0] != '-') ||
00170             (argv[my_optind][1] == '\0')) {
00171     char *tmp;
00172     int i, j, k;
00173 
00174     opt = -1;
00175     if(mode == '+') return -1;
00176     else if(mode == '-') {
00177       my_optarg = argv[my_optind++];
00178       return 1;
00179     }
00180     for(i=j=my_optind; i<argc; i++) if((argv[i][0] == '-') &&
00181                                     (argv[i][1] != '\0')) {
00182       my_optind=i;
00183       opt=_my_getopt_internal(argc, argv, shortopts,
00184                               longopts, longind,
00185                               long_only);
00186       while(i > j) {
00187         tmp=argv[--i];
00188         for(k=i; k+1<my_optind; k++)
00189           argv[k]=argv[k+1];
00190         argv[--my_optind]=tmp;
00191       }
00192       break;
00193     }
00194   } else if((!long_only) && (argv[my_optind][1] != '-'))
00195     opt = my_getopt(argc, argv, shortopts);
00196   else {
00197     int charind, offset;
00198     int found = 0, ind, hits = 0;
00199 
00200     if(((my_optopt = argv[my_optind][1]) != '-') && ! argv[my_optind][2]) {
00201       int c;
00202       
00203       ind = shortoff;
00204       while((c = shortopts[ind++])) {
00205         if(((shortopts[ind] == ':') ||
00206             ((c == 'W') && (shortopts[ind] == ';'))) &&
00207            (shortopts[++ind] == ':'))
00208           ind ++;
00209         if(my_optopt == c) return my_getopt(argc, argv, shortopts);
00210       }
00211     }
00212     offset = 2 - (argv[my_optind][1] != '-');
00213     for(charind = offset;
00214         (argv[my_optind][charind] != '\0') &&
00215           (argv[my_optind][charind] != '=');
00216         charind++);
00217     for(ind = 0; longopts[ind].name && !hits; ind++)
00218       if((strlen(longopts[ind].name) == (size_t) (charind - offset)) &&
00219          (strncmp(longopts[ind].name,
00220                   argv[my_optind] + offset, charind - offset) == 0))
00221         found = ind, hits++;
00222     if(!hits) for(ind = 0; longopts[ind].name; ind++)
00223       if(strncmp(longopts[ind].name,
00224                  argv[my_optind] + offset, charind - offset) == 0)
00225         found = ind, hits++;
00226     if(hits == 1) {
00227       opt = 0;
00228 
00229       if(argv[my_optind][charind] == '=') {
00230         if(longopts[found].has_arg == 0) {
00231           opt = '?';
00232           if(my_opterr) fprintf(stderr,
00233                              "%s: option `--%s' doesn't allow an argument\n",
00234                              argv[0], longopts[found].name);
00235         } else {
00236           my_optarg = argv[my_optind] + ++charind;
00237           charind = 0;
00238         }
00239       } else if(longopts[found].has_arg == 1) {
00240         if(++my_optind >= argc) {
00241           opt = (colon_mode == ':') ? ':' : '?';
00242           if(my_opterr) fprintf(stderr,
00243                              "%s: option `--%s' requires an argument\n",
00244                              argv[0], longopts[found].name);
00245         } else my_optarg = argv[my_optind];
00246       }
00247       if(!opt) {
00248         if (longind) *longind = found;
00249         if(!longopts[found].flag) opt = longopts[found].val;
00250         else *(longopts[found].flag) = longopts[found].val;
00251       }
00252       my_optind++;
00253     } else if(!hits) {
00254       if(offset == 1) opt = my_getopt(argc, argv, shortopts);
00255       else {
00256         opt = '?';
00257         if(my_opterr) fprintf(stderr,
00258                            "%s: unrecognized option `%s'\n",
00259                            argv[0], argv[my_optind++]);
00260       }
00261     } else {
00262       opt = '?';
00263       if(my_opterr) fprintf(stderr,
00264                          "%s: option `%s' is ambiguous\n",
00265                          argv[0], argv[my_optind++]);
00266     }
00267   }
00268   if (my_optind > argc) my_optind = argc;
00269   return opt;
00270 }

int my_getopt int  argc,
char *  argv[],
const char *  opts
 

Definition at line 50 of file nco_getopt.c.

References my_getopt(), my_optarg, my_opterr, my_optind, and my_optopt.

Referenced by _my_getopt_internal(), and my_getopt().

00051 {
00052   static int charind=0;
00053   const char *s;
00054   char mode, colon_mode;
00055   int off = 0, opt = -1;
00056 
00057   if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+';
00058   else {
00059     if((colon_mode = *opts) == ':') off ++;
00060     if(((mode = opts[off]) == '+') || (mode == '-')) {
00061       off++;
00062       if((colon_mode != ':') && ((colon_mode = opts[off]) == ':'))
00063         off ++;
00064     }
00065   }
00066   my_optarg = 0;
00067   if(charind) {
00068     my_optopt = argv[my_optind][charind];
00069     for(s=opts+off; *s; s++) if(my_optopt == *s) {
00070       charind++;
00071       if((*(++s) == ':') || ((my_optopt == 'W') && (*s == ';'))) {
00072         if(argv[my_optind][charind]) {
00073           my_optarg = &(argv[my_optind++][charind]);
00074           charind = 0;
00075         } else if(*(++s) != ':') {
00076           charind = 0;
00077           if(++my_optind >= argc) {
00078             if(my_opterr) fprintf(stderr,
00079                                 "%s: option requires an argument -- %c\n",
00080                                 argv[0], my_optopt);
00081             opt = (colon_mode == ':') ? ':' : '?';
00082             goto my_getopt_ok;
00083           }
00084           my_optarg = argv[my_optind++];
00085         }
00086       }
00087       opt = my_optopt;
00088       goto my_getopt_ok;
00089     }
00090     if(my_opterr) fprintf(stderr,
00091                         "%s: illegal option -- %c\n",
00092                         argv[0], my_optopt);
00093     opt = '?';
00094     if(argv[my_optind][++charind] == '\0') {
00095       my_optind++;
00096       charind = 0;
00097     }
00098   my_getopt_ok:
00099     if(charind && ! argv[my_optind][charind]) {
00100       my_optind++;
00101       charind = 0;
00102     }
00103   } else if((my_optind >= argc) ||
00104              ((argv[my_optind][0] == '-') &&
00105               (argv[my_optind][1] == '-') &&
00106               (argv[my_optind][2] == '\0'))) {
00107     my_optind++;
00108     opt = -1;
00109   } else if((argv[my_optind][0] != '-') ||
00110              (argv[my_optind][1] == '\0')) {
00111     char *tmp;
00112     int i, j, k;
00113 
00114     if(mode == '+') opt = -1;
00115     else if(mode == '-') {
00116       my_optarg = argv[my_optind++];
00117       charind = 0;
00118       opt = 1;
00119     } else {
00120       for(i=j=my_optind; i<argc; i++) if((argv[i][0] == '-') &&
00121                                         (argv[i][1] != '\0')) {
00122         my_optind=i;
00123         opt=my_getopt(argc, argv, opts);
00124         while(i > j) {
00125           tmp=argv[--i];
00126           for(k=i; k+1<my_optind; k++) argv[k]=argv[k+1];
00127           argv[--my_optind]=tmp;
00128         }
00129         break;
00130       }
00131       if(i == argc) opt = -1;
00132     }
00133   } else {
00134     charind++;
00135     opt = my_getopt(argc, argv, opts);
00136   }
00137   if (my_optind > argc) my_optind = argc;
00138   return opt;
00139 }

int my_getopt_long int  argc,
char *  argv[],
const char *  shortopts,
const struct option longopts,
int *  longind
 

Definition at line 272 of file nco_getopt.c.

References _my_getopt_internal().

00274 {
00275   return _my_getopt_internal(argc, argv, shortopts, longopts, longind, 0);
00276 }

int my_getopt_long_only int  argc,
char *  argv[],
const char *  shortopts,
const struct option longopts,
int *  longind
 

Definition at line 278 of file nco_getopt.c.

References _my_getopt_internal().

00280 {
00281   return _my_getopt_internal(argc, argv, shortopts, longopts, longind, 1);
00282 }


Variable Documentation

char* my_optarg = 0
 

Definition at line 44 of file nco_getopt.c.

Referenced by _my_getopt_internal(), and my_getopt().

int my_opterr = 1
 

Definition at line 43 of file nco_getopt.c.

Referenced by _my_getopt_internal(), and my_getopt().

int my_optind = 1
 

Definition at line 43 of file nco_getopt.c.

Referenced by _my_getopt_internal(), and my_getopt().

int my_optopt = 0
 

Definition at line 43 of file nco_getopt.c.

Referenced by _my_getopt_internal(), and my_getopt().


Generated on Thu Mar 16 18:15:39 2006 for nco by  doxygen 1.4.4