Imported Upstream version 4.6.0
[debian/atlc] / tools / src / get_options.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 */
24
25
26 /*
27  * Here's something you've all been waiting for:  the AT&T public domain
28  * source for getopt(3).  It is the code which was given out at the 1985
29  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
30  * directly from AT&T.  The people there assure me that it is indeed
31  * in the public domain.
32  * 
33  * There is no manual page.  That is because the one they gave out at
34  * UNIFORUM was slightly different from the current System V Release 2
35  * manual page.  The difference apparently involved a note about the
36  * famous rules 5 and 6, recommending using white space between an option
37  * and its first argument, and not grouping options that have arguments.
38  * Getopt itself is currently lenient about both of these things White
39  * space is allowed, but not mandatory, and the last option in a group can
40  * have an argument.  That particular package_version of the man page evidently
41  * has no official existence, and my source at AT&T did not send a copy.
42  * The current SVR2 man page reflects the actual behavor of this getopt.
43  * However, I am not about to post a copy of anything licensed by AT&T.
44  */
45
46 #include "config.h"
47
48 #ifdef HAVE_STRINGS_H
49 #include <strings.h>
50 #endif
51
52 #ifdef HAVE_STRING_H
53 #include <string.h>
54 #endif
55
56 #ifdef HAVE_STDIO_H
57 #include <stdio.h>
58 #endif
59
60 /*LINTLIBRARY*/
61 #ifndef NULL
62 #define NULL    0
63 #endif
64 #define EOF     (-1)
65
66
67 int     my_opterr = 1;
68 int     my_optind = 1;
69 int     my_optopt;
70 char    *my_optarg;
71
72 void ERR(const char *s, char c, char **argv)  
73 {
74         if(my_opterr)
75         {
76                 char errbuf[2];
77                 errbuf[0] = c;
78                 errbuf[1] = '\n';
79                 /* (void) write(2, argv[0], (unsigned)strlen(argv[0]));
80                 (void) write(2, s, (unsigned)strlen(s));
81                 (void) write(2, errbuf, 2); */
82                 (void) fwrite(argv[0], (unsigned)strlen(argv[0]),1,stderr);
83                 (void) fwrite(s, (unsigned)strlen(s),1,stderr);
84                 (void) fwrite(errbuf, 2, 1,stderr); 
85         }
86 }
87
88 char *index2(const char *str, char c)
89 {
90         char *ret;
91         ret=strchr((char *) str,c);
92         return(ret);
93 }
94
95
96 int get_options(int argc, char **argv, const char *opts)
97 {
98         static int sp = 1;
99         /* register int c;
100          register char *cp; */
101         int c;
102         char tmp;
103         char *cp;
104         cp=&tmp;
105
106         if(sp == 1)
107         {
108                 if(my_optind >= argc ||
109                    argv[my_optind][0] != '-' || argv[my_optind][1] == '\0')
110                         return(EOF);
111                 else if(strcmp(argv[my_optind], "--") == 0) {
112                         my_optind++;
113                         return(EOF);
114                 }
115         }
116         my_optopt = c = argv[my_optind][sp];
117         if(c == ':' || (cp=index2((char *) opts, c)) == NULL) {
118                 ERR(": illegal option -- ", c,argv);
119                 if(argv[my_optind][++sp] == '\0') {
120                         my_optind++;
121                         sp = 1;
122                 }
123                 return('?');
124         }
125         if(*++cp == ':') {
126                 if(argv[my_optind][sp+1] != '\0')
127                         my_optarg = &argv[my_optind++][sp+1];
128                 else if(++my_optind >= argc) {
129                         ERR(": option requires an argument -- ", c,argv);
130                         sp = 1;
131                         return('?');
132                 } else
133                         my_optarg = argv[my_optind++];
134                 sp = 1;
135         } else {
136                 if(argv[my_optind][++sp] == '\0') {
137                         sp = 1;
138                         my_optind++;
139                 }
140                 my_optarg = NULL;
141         }
142         return(c);
143 }