source: buchla-68k/orig/GP/GETOPT.C@ cbe2c15

Last change on this file since cbe2c15 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago

Imported original source code.

  • Property mode set to 100755
File size: 3.9 KB
RevLine 
[3ae31e9]1/*
2 =============================================================================
3 getopt.c -- process the options on a Unix{tm} style command line
4 Version 2 -- 1988-05-04 -- D.N. Lynx Crowe
5
6 Adapted from public domain code by Henry Specner, et al,
7 from Usenet "net.sources".
8
9 extern int optind; Index into nargv
10 extern int opterr; Error output flag
11 extern int optopt; Most recently parsed option letter
12 extern char *optarg; Pointer to option argument or NULL
13
14 int
15 getopt(nargc, nargv, optstr)
16 int nargc;
17 char *nargv[];
18 char *optstr;
19
20 Parses nargv[] according to optstr. Returns the next
21 option letter in argv that matches a letter in optstr.
22 'optstr' is a string of recognized option letters; if a
23 letter is followed by a colon, the option is expected
24 to have an argument that may or may not be separated
25 from it by whitespace. 'optarg' is set to point to the
26 start of the option argument on return from getopt.
27 Assumes argv[0] is the program name. Outputs error
28 messages to stderr if 'opterr' is non-zero. 'optopt' is
29 the most recently parsed option letter.
30
31 Note: this code is not ROM-able due to the use of an
32 initialized static variable, but this is probably not
33 too important, as it's designed to be used as a command
34 line processor.
35
36 int
37 getarg(nargc, nargv)
38 int nargc;
39 char *nargv[];
40
41 Returns the index of the next argument, or EOF if the
42 argument string is exhausted. Used to get the file
43 arguments after getopt() is finished. Updates optarg
44 to point at the argument. Increments optind to point
45 at the next argument.
46 =============================================================================
47*/
48
49/*
50
51*/
52
53#include "stdio.h"
54#include "stddefs.h"
55
56#define ARGCH (int)':'
57#define BADCH (int)'?'
58#define EMSG ""
59#define ENDARGS "--"
60
61int opterr; /* set to non-zero to get error message output */
62int optind; /* index of current option string in parent argv */
63int optopt; /* current option letter */
64
65char *optarg; /* pointer to the option argument */
66
67/*
68
69*/
70
71int
72getarg(nargc, nargv)
73int nargc;
74char *nargv[];
75{
76 if (optind EQ 0) /* in case getopt() wasn't called */
77 optind++;
78
79 if (optind GE nargc)
80 return(EOF); /* out of arguments */
81
82 optarg = nargv[optind]; /* set optarg to point at the argument */
83 return(optind++); /* return the argument index */
84}
85
86static
87char *
88index(s, c)
89register char *s;
90register int c;
91{
92 while (*s)
93 if (c EQ *s)
94 return(s);
95 else
96 s++;
97
98 return(NULL);
99}
100
101/*
102
103*/
104
105int
106getopt(nargc, nargv, ostr)
107int nargc;
108char *nargv[], *ostr;
109{
110 static char *place = EMSG; /* private scan pointer */
111 register char *oli;
112
113 if (optind EQ 0) /* make sure optind starts out non-zero */
114 ++optind;
115
116 if (!*place) { /* update the scan pointer */
117
118 if ((optind GE nargc)
119 OR (*(place = nargv[optind]) NE '-')
120 OR (!*++place))
121 return(EOF);
122
123 if (*place EQ '-') { /* found "--" */
124
125 ++optind;
126 return(EOF);
127 }
128 }
129
130 if (((optopt = (int)*place++) EQ ARGCH)
131 OR (!(oli = index(ostr, optopt)))) { /* option letter OK ? */
132
133 if (!*place)
134 ++optind;
135
136 if (opterr) {
137
138 fputs(*nargv, stderr);
139 fputs(": unknown argument \042", stderr);
140 fputc(optopt, stderr);
141 fputs("\042\n", stderr);
142 }
143
144 return(BADCH);
145 }
146/*
147
148*/
149 if (*++oli NE ARGCH) { /* check for required argument */
150
151 optarg = NULL; /* no argument needed */
152
153 if (!*place)
154 ++optind;
155
156 } else { /* we need an arguement */
157
158 if (*place)
159 optarg = place;
160 else if (nargc LE ++optind) {
161
162 place = EMSG;
163
164 if (opterr) {
165
166 fputs(*nargv, stderr);
167 fputs(": option \042", stderr);
168 fputc(optopt, stderr);
169 fputs("\042 requires an argument.\n", stderr);
170 }
171
172 return(BADCH);
173
174 } else
175 optarg = nargv[optind];
176
177 place = EMSG;
178 ++optind;
179 }
180
181 return(optopt);
182}
183
Note: See TracBrowser for help on using the repository browser.