Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / sgmls / getopt.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: getopt.c /main/3 1996/06/19 17:15:14 drk $ */
24 /* getopt.c -
25    getopt() for those systems that don't have it.
26
27    Derived from comp.sources.unix/volume3/att_getopt.
28    Modified by James Clark (jjc@jclark.com).
29 */
30
31 #include "config.h"
32
33 #ifndef HAVE_GETOPT
34
35 #include "std.h"
36 #include "getopt.h"
37
38 #ifdef SWITCHAR
39 #include <dos.h>
40 #endif
41
42 int     opterr = 1;
43 int     optind = 1;
44 int     optopt;
45 char *optarg;
46
47 #ifndef OPTION_CHAR
48 #define OPTION_CHAR '-'
49 #endif
50
51 int getopt(argc, argv, opts)
52 int argc;
53 char **argv;
54 char *opts;
55 {
56 #ifdef SWITCHAR
57         union REGS regs;
58         static char switchar = '\0';
59 #endif
60         static int sp = 1;
61         register int c;
62         register char *cp;
63         char *message;
64 #ifdef SWITCHAR
65         if (switchar == '\0') {
66                 regs.x.ax = 0x3700;
67                 intdos(&regs, &regs);
68                 if (!regs.x.cflag)
69                         switchar = regs.h.dl;
70                 else
71                         switchar = '/';
72         }
73 #endif
74         if (sp == 1) {
75                 if (optind >= argc)
76                         return EOF;
77                 if ((
78 #ifdef SWITCHAR
79                         argv[optind][0] != switchar &&
80 #endif
81                         argv[optind][0] != OPTION_CHAR) || argv[optind][1] == '\0') {
82 #ifdef REORDER_ARGS
83                         int i;
84                         for (i = optind; i < argc; i++)
85                                 if ((
86 #ifdef SWITCHAR
87                                          argv[i][0] == switchar ||
88 #endif
89                                          argv[i][0] == OPTION_CHAR) && argv[i][1] != '\0')
90                                         break;
91                         if (i < argc) {
92                                 c = argv[i][1];
93 #ifdef CASE_INSENSITIVE_OPTIONS
94                                 if (isupper(c))
95                                         c = tolower(c);
96 #endif
97                                 if (c != ':' && c != OPTION_CHAR && (cp = strchr(opts, c)) != NULL
98                                         && cp[1] == ':' && argv[i][2] == 0 && i < argc - 1) {
99                                         int j;
100                                         char *temp1 = argv[i];
101                                         char *temp2 = argv[i+1];
102                                         for (j = i - 1; j >= optind; j--)
103                                                 argv[j+2] = argv[j];
104                                         argv[optind] = temp1;
105                                         argv[optind+1] = temp2;
106                                 }
107                                 else {
108                                         int j;
109                                         char *temp = argv[i];
110                                         for (j = i - 1; j >= optind; j--)
111                                                 argv[j+1] = argv[j];
112                                         argv[optind] = temp;
113                                 }
114                         }
115                         else
116 #endif
117                                 return EOF;
118                 }
119                 if ((argv[optind][0] == OPTION_CHAR && argv[optind][1] == OPTION_CHAR
120                                   && argv[optind][2] == '\0')
121 #ifdef SWITCHAR
122                         || (argv[optind][0] == switchar && argv[optind][1] == switchar
123                                 && argv[optind][2] == '\0')
124 #endif
125                         ) {
126                         optind++;
127                         return(EOF);
128                 }
129         }
130         optopt = c = argv[optind][sp];
131 #ifdef CASE_INSENSITIVE_OPTIONS
132         if (
133 #ifdef USE_ISASCII
134                 isascii(c) &&
135 #endif /* USE_ISASCII */
136                 isupper((unsigned char)c))
137                 optopt = c = tolower((unsigned char)c);
138 #endif /* CASE_INSENSITIVE_OPTIONS */
139         if (c == ':' || (cp = strchr(opts, c)) == NULL) {
140                 if (argv[optind][++sp] == '\0') {
141                         optind++;
142                         sp = 1;
143                 }
144                 message = ": illegal option -- ";
145                 goto bad;
146         }
147         if (*++cp == ':') {
148                 if (argv[optind][sp+1] != '\0')
149                         optarg = &argv[optind++][sp+1];
150                 else if (++optind >= argc) {
151                         sp = 1;
152                         message = ": option requires an argument -- ";
153                         goto bad;
154                 }
155                 else
156                         optarg = argv[optind++];
157                 sp = 1;
158         } 
159         else {
160                 if (argv[optind][++sp] == '\0') {
161                         sp = 1;
162                         optind++;
163                 }
164                 optarg = NULL;
165         }
166         return c;
167 bad:
168         if (opterr) {
169                 fputs(argv[0], stderr);
170                 fputs(message, stderr);
171                 fputc(optopt, stderr);
172                 fputc('\n', stderr);
173         }
174         return '?';
175 }
176
177 #endif /* not HAVE_GETOPT */
178
179 /*
180 Local Variables:
181 c-indent-level: 4
182 c-continued-statement-offset: 4
183 c-brace-offset: 4
184 c-argdecl-indent: 4
185 c-label-offset: -4
186 tab-width: 4
187 End:
188 */
189