source: buchla-68k/orig/LATTICE/GETOPT.C

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

Imported original source code.

  • Property mode set to 100755
File size: 3.1 KB
Line 
1/*
2 =============================================================================
3 getopt.c -- process the options on a Unix{tm} style command line
4 Version 1 -- 1987-10-16 -- 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.
33 =============================================================================
34*/
35
36/*
37
38*/
39
40#include "stdio.h"
41#include "stddefs.h"
42
43#define ARGCH (int)':'
44#define BADCH (int)'?'
45#define EMSG ""
46#define ENDARGS "--"
47
48int opterr; /* set to non-zero to get error message output */
49int optind; /* index of current option string in parent argv */
50int optopt; /* current option letter */
51
52char *optarg; /* pointer to the option argument */
53
54static
55char *
56index(s, c)
57register char *s;
58register int c;
59{
60 while (*s)
61 if (c EQ *s)
62 return(s);
63 else
64 s++;
65
66 return(NULL);
67}
68
69/*
70
71*/
72
73int
74getopt(nargc, nargv, ostr)
75int nargc;
76char *nargv[], *ostr;
77{
78 static char *place = EMSG; /* private scan pointer */
79 register char *oli;
80
81 if (optind EQ 0) /* make sure optind starts out non-zero */
82 ++optind;
83
84 if (!*place) { /* update the scan pointer */
85
86 if ((optind GE nargc)
87 OR (*(place = nargv[optind]) NE '-')
88 OR (!*++place))
89 return(EOF);
90
91 if (*place EQ '-') { /* found "--" */
92
93 ++optind;
94 return(EOF);
95 }
96 }
97
98 if (((optopt = (int)*place++) EQ ARGCH)
99 OR (!(oli = index(ostr, optopt)))) { /* option letter OK ? */
100
101 if (!*place)
102 ++optind;
103
104 if (opterr) {
105
106 fputs(*nargv, stderr);
107 fputs(": unknown argument \042", stderr);
108 fputc(optopt, stderr);
109 fputs("\042\n", stderr);
110 }
111
112 return(BADCH);
113 }
114
115 if (*++oli NE ARGCH) { /* check for required argument */
116
117 optarg = NULL; /* no argument needed */
118
119 if (!*place)
120 ++optind;
121
122 } else { /* we need an arguement */
123
124 if (*place)
125 optarg = place;
126 else if (nargc LE ++optind) {
127
128 place = EMSG;
129
130 if (opterr) {
131
132 fputs(*nargv, stderr);
133 fputs(": option \042", stderr);
134 fputc(optopt, stderr);
135 fputs("\042 requires an argument.\n", stderr);
136 }
137
138 return(BADCH);
139 } else
140 optarg = nargv[optind];
141
142 place = EMSG;
143 ++optind;
144 }
145
146 return(optopt);
147}
Note: See TracBrowser for help on using the repository browser.