| 1 |
/* getopt.c - |
| 2 |
getopt() for those systems that don't have it. |
| 3 |
|
| 4 |
Derived from comp.sources.unix/volume3/att_getopt. |
| 5 |
Modified by James Clark ([email protected]). |
| 6 |
*/ |
| 7 |
|
| 8 |
/* Notes about this implementation of getopt(): |
| 9 |
* |
| 10 |
* The following #defines exist: |
| 11 |
* SWITCHAR - signifies that MSDOS-specific functions should be used to |
| 12 |
* determine the switch character |
| 13 |
* REORDER_ARGS - ?? |
| 14 |
* CASE_INSENSITIVE_OPTIONS, USE_ISASCII - self-explanatory |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifdef HAVE_CONFIG_H |
| 18 |
#include "config.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
#ifndef HAVE_GETOPT |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
#include "arch.h" |
| 26 |
|
| 27 |
#ifdef SWITCHAR |
| 28 |
#include <dos.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
int opterr = 1; |
| 32 |
int optind = 1; |
| 33 |
int optopt; |
| 34 |
char *optarg; |
| 35 |
|
| 36 |
#ifndef OPTION_CHAR |
| 37 |
#define OPTION_CHAR '-' |
| 38 |
#endif |
| 39 |
|
| 40 |
int getopt(argc, argv, opts) |
| 41 |
int argc; |
| 42 |
char **argv; |
| 43 |
char *opts; |
| 44 |
{ |
| 45 |
#ifdef SWITCHAR |
| 46 |
union REGS regs; |
| 47 |
static char switchar = '\0'; |
| 48 |
#endif |
| 49 |
static int sp = 1; |
| 50 |
register int c; |
| 51 |
register char *cp; |
| 52 |
char *message; |
| 53 |
#ifdef SWITCHAR |
| 54 |
if (switchar == '\0') { |
| 55 |
regs.x.ax = 0x3700; |
| 56 |
intdos(®s, ®s); |
| 57 |
if (!regs.x.cflag) |
| 58 |
switchar = regs.h.dl; |
| 59 |
else |
| 60 |
switchar = '/'; |
| 61 |
} |
| 62 |
#endif |
| 63 |
if (sp == 1) { |
| 64 |
if (optind >= argc) |
| 65 |
return EOF; |
| 66 |
if (( |
| 67 |
#ifdef SWITCHAR |
| 68 |
argv[optind][0] != switchar && |
| 69 |
#endif |
| 70 |
argv[optind][0] != OPTION_CHAR) || argv[optind][1] == '\0') { |
| 71 |
#ifdef REORDER_ARGS |
| 72 |
int i; |
| 73 |
for (i = optind; i < argc; i++) |
| 74 |
if (( |
| 75 |
#ifdef SWITCHAR |
| 76 |
argv[i][0] == switchar || |
| 77 |
#endif |
| 78 |
argv[i][0] == OPTION_CHAR) && argv[i][1] != '\0') |
| 79 |
break; |
| 80 |
if (i < argc) { |
| 81 |
c = argv[i][1]; |
| 82 |
#ifdef CASE_INSENSITIVE_OPTIONS |
| 83 |
if (isupper(c)) |
| 84 |
c = tolower(c); |
| 85 |
#endif |
| 86 |
if (c != ':' && c != OPTION_CHAR && (cp = strchr(opts, c)) != NULL |
| 87 |
&& cp[1] == ':' && argv[i][2] == 0 && i < argc - 1) { |
| 88 |
int j; |
| 89 |
char *temp1 = argv[i]; |
| 90 |
char *temp2 = argv[i+1]; |
| 91 |
for (j = i - 1; j >= optind; j--) |
| 92 |
argv[j+2] = argv[j]; |
| 93 |
argv[optind] = temp1; |
| 94 |
argv[optind+1] = temp2; |
| 95 |
} |
| 96 |
else { |
| 97 |
int j; |
| 98 |
char *temp = argv[i]; |
| 99 |
for (j = i - 1; j >= optind; j--) |
| 100 |
argv[j+1] = argv[j]; |
| 101 |
argv[optind] = temp; |
| 102 |
} |
| 103 |
} |
| 104 |
else |
| 105 |
#endif /* REORDER_OPTIONS */ |
| 106 |
return EOF; |
| 107 |
} |
| 108 |
if ((argv[optind][0] == OPTION_CHAR && argv[optind][1] == OPTION_CHAR |
| 109 |
&& argv[optind][2] == '\0') |
| 110 |
#ifdef SWITCHAR |
| 111 |
|| (argv[optind][0] == switchar && argv[optind][1] == switchar |
| 112 |
&& argv[optind][2] == '\0') |
| 113 |
#endif |
| 114 |
) { |
| 115 |
optind++; |
| 116 |
return(EOF); |
| 117 |
} |
| 118 |
} |
| 119 |
optopt = c = argv[optind][sp]; |
| 120 |
#ifdef CASE_INSENSITIVE_OPTIONS |
| 121 |
if ( |
| 122 |
#ifdef USE_ISASCII |
| 123 |
isascii(c) && |
| 124 |
#endif /* USE_ISASCII */ |
| 125 |
isupper((unsigned char)c)) |
| 126 |
optopt = c = tolower((unsigned char)c); |
| 127 |
#endif /* CASE_INSENSITIVE_OPTIONS */ |
| 128 |
if (c == ':' || (cp = strchr(opts, c)) == NULL) { |
| 129 |
if (argv[optind][++sp] == '\0') { |
| 130 |
optind++; |
| 131 |
sp = 1; |
| 132 |
} |
| 133 |
message = ": illegal option -- "; |
| 134 |
goto bad; |
| 135 |
} |
| 136 |
if (*++cp == ':') { |
| 137 |
if (argv[optind][sp+1] != '\0') |
| 138 |
optarg = &argv[optind++][sp+1]; |
| 139 |
else if (++optind >= argc) { |
| 140 |
sp = 1; |
| 141 |
message = ": option requires an argument -- "; |
| 142 |
goto bad; |
| 143 |
} |
| 144 |
else |
| 145 |
optarg = argv[optind++]; |
| 146 |
sp = 1; |
| 147 |
} |
| 148 |
else { |
| 149 |
if (argv[optind][++sp] == '\0') { |
| 150 |
sp = 1; |
| 151 |
optind++; |
| 152 |
} |
| 153 |
optarg = NULL; |
| 154 |
} |
| 155 |
return c; |
| 156 |
bad: |
| 157 |
if (opterr) { |
| 158 |
fputs(argv[0], stderr); |
| 159 |
fputs(message, stderr); |
| 160 |
fputc(optopt, stderr); |
| 161 |
fputc('\n', stderr); |
| 162 |
} |
| 163 |
return '?'; |
| 164 |
} |
| 165 |
|
| 166 |
#endif /* HAVE_GETOPT */ |
| 167 |
|