|
|
1.1 root 1: /*
2: * Reimplementation of SysVr3 sh builtin command "getopts" for S5R2 shell.
3: *
4: * created by Arnold Robbins
5: * modified by Doug Gwyn
6: * modified for PD ksh by Eric Gisin
7: */
8:
9: static char *RCSid = "$Header: getopts.c,v 3.1 88/11/03 09:16:01 egisin Exp $";
10:
11: #include <stddef.h>
12: #include <stdlib.h>
13: #include <string.h>
14: #include <errno.h>
15: #include <setjmp.h>
16: #include "sh.h"
17: #include "table.h"
18:
19: /*
20: * The following is derived from getopt() source placed into the public
21: * domain by AT&T (the only time they're known to have done that).
22: *
23: * It has been modified somewhat to fit into the context of the shell.
24: *
25: * -D"FASCIST" if you really want to strictly enforce ALL the
26: * Command Syntax Standard rules (not recommended).
27: */
28:
29: #define GETOPTEOF (-1)
30: #define ERR(S, C) shellf("%s%c\n", (S), (C))
31:
32: int optind;
33: static char *optarg;
34: static int sp;
35:
36: static int
37: getopt(argc, argv, opts)
38: int argc;
39: register char **argv, *opts;
40: {
41: register int c;
42: register char *cp;
43:
44: if (sp == 1)
45: if (optind >= argc ||
46: argv[optind][0] != '-' || argv[optind][1] == '\0')
47: return(GETOPTEOF);
48: else if (strcmp(argv[optind], "--") == 0) {
49: optind++;
50: return(GETOPTEOF);
51: }
52: c = argv[optind][sp];
53: if (c == ':' || (cp=strchr(opts, c)) == NULL) {
54: ERR("illegal option -- ", c);
55: if (argv[optind][++sp] == '\0') {
56: optind++;
57: sp = 1;
58: }
59: optarg = NULL;
60: return('?');
61: }
62: if (*++cp == ':') {
63: #if FASCIST
64: if (sp != 1) {
65: ERR("option must not be grouped -- ", c );
66: optind++;
67: sp = 1;
68: optarg = NULL;
69: return('?');
70: } else
71: #endif
72: if (argv[optind][sp+1] != '\0') {
73: #if FASCIST
74: ERR("option must be followed by whitespace -- ", c );
75: optind++;
76: sp = 1;
77: optarg = NULL;
78: return('?');
79: #else
80: optarg = &argv[optind++][sp+1];
81: #endif
82: } else if (++optind >= argc) {
83: ERR("option requires an argument -- ", c);
84: sp = 1;
85: optarg = NULL;
86: return('?');
87: } else
88: optarg = argv[optind++];
89: sp = 1;
90: } else {
91: if (argv[optind][++sp] == '\0') {
92: sp = 1;
93: optind++;
94: }
95: optarg = NULL;
96: }
97: return(c);
98: }
99:
100: /*
101: * The following were created by Arnold Robbins.
102: */
103:
104: /* resetopts --- magic code for when OPTIND is reset to 1 */
105:
106: void
107: resetopts ()
108: {
109: optind = 1;
110: sp = 1;
111: }
112:
113: int
114: c_getopts(wp)
115: char **wp;
116: {
117: int ret;
118: register int argc;
119: char temp[2];
120: char *optstr; /* list of options */
121: char *name; /* variable to get flag val */
122:
123: if ((optstr = *++wp) == NULL || (name = *++wp) == NULL)
124: errorf("missing arguments\n");
125:
126: for (argc = 1; wp[argc] != NULL; argc++)
127: ;
128:
129: if (argc > 1)
130: ret = getopt(argc, wp, optstr);
131: else
132: ret = getopt(e.loc->argc+1, e.loc->argv, optstr);
133:
134: /*
135: * set the OPTIND variable in any case, to handle "--" skipping
136: */
137:
138: setint(global("OPTIND"), (long)optind);
139:
140: if (ret == GETOPTEOF) /* end of args */
141: return (1);
142:
143: /*
144: * else, got an arg, set the various shell variables
145: */
146:
147: if (optarg != NULL)
148: setstr(global("OPTARG"), optarg);
149:
150: temp[0] = (char) ret;
151: temp[1] = '\0';
152: setstr(global(name), temp);
153:
154: return (0);
155: }
156:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.