00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef lint
00038 static const char libbu_getopt_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/getopt.c,v 14.11 2006/08/31 05:50:24 lbutler Exp $ (BRL)";
00039 #endif
00040
00041 #include "common.h"
00042
00043 #include <stdio.h>
00044 #ifdef HAVE_STRING_H
00045 # include <string.h>
00046 #else
00047 # include <strings.h>
00048 #endif
00049
00050 #include "machine.h"
00051 #include "bu.h"
00052
00053
00054 int bu_opterr = 1;
00055 int bu_optind = 1;
00056 int bu_optopt = 0;
00057 char *bu_optarg = NULL;
00058
00059 #define BADCH (int)'?'
00060 #define EMSG ""
00061 #define tell(s) if(bu_opterr) { \
00062 fputs(*nargv,stderr);fputs(s,stderr); \
00063 fputc(bu_optopt,stderr);fputc('\n',stderr); \
00064 } return(BADCH);
00065
00066
00067
00068
00069
00070
00071
00072 int
00073 bu_getopt(int nargc, char *const *nargv, const char *ostr)
00074 {
00075 static char *place = EMSG;
00076 register char *oli;
00077
00078 if(*place=='\0') {
00079 if(bu_optind >= nargc || *(place = nargv[bu_optind]) != '-' ||
00080 !*++place) {
00081 place = EMSG;
00082 return(EOF);
00083 }
00084 if (*place == '-') {
00085 place = EMSG;
00086 ++bu_optind;
00087 return(EOF);
00088 }
00089 }
00090 if ((bu_optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,bu_optopt))) {
00091 #if 0
00092 if(*place == '\0') {
00093 ++bu_optind;
00094 place = EMSG;
00095 }
00096 #else
00097 ++bu_optind;
00098 place = EMSG;
00099 #endif
00100 tell(": illegal option -- ");
00101 }
00102 if (*++oli != ':') {
00103 bu_optarg = NULL;
00104 if (*place == '\0') {
00105 ++bu_optind;
00106 place = EMSG;
00107 }
00108 }
00109 else {
00110 if (*place) bu_optarg = place;
00111 else if (nargc <= ++bu_optind) {
00112 place = EMSG;
00113 tell(": option requires an argument -- ");
00114 }
00115 else bu_optarg = nargv[bu_optind];
00116 place = EMSG;
00117 ++bu_optind;
00118 }
00119 return(bu_optopt);
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130