Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / 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 #include "definitions.h"
49
50 #ifdef HAVE_STRINGS_H
51 #include <strings.h>
52 #endif
53
54 #ifdef HAVE_STRING_H
55 #include <string.h>
56 #endif
57
58 /*LINTLIBRARY*/
59 #ifndef NULL
60 #define NULL    0
61 #endif
62 #define EOF     (-1)
63
64
65 int     my_opterr = 1;
66 int     my_optind = 1;
67 int     my_optopt;
68 char    *my_optarg;
69
70 void ERR(const char *s, char c, char **argv)  
71 {
72         if(my_opterr)
73         {
74                 char errbuf[2];
75                 errbuf[0] = c;
76                 errbuf[1] = '\n';
77                 /* (void) write(2, argv[0], (unsigned)strlen(argv[0]));
78                 (void) write(2, s, (unsigned)strlen(s));
79                 (void) write(2, errbuf, 2); */
80                 (void) fwrite(argv[0], (unsigned)strlen(argv[0]),1,stderr);
81                 (void) fwrite(s, (unsigned)strlen(s),1,stderr);
82                 (void) fwrite(errbuf, 2, 1,stderr); 
83         }
84 }
85
86 char *index2(const char *str, char c)
87 {
88         char *ret;
89         ret=strchr((char *) str,c);
90         return(ret);
91 }
92
93
94 int get_options(int argc, char **argv, const char *opts)
95 {
96         static int sp = 1;
97         /* register int c;
98          register char *cp; */
99         int c;
100         char tmp;
101         char *cp;
102         cp=&tmp;
103
104         if(sp == 1)
105         {
106                 if(my_optind >= argc ||
107                    argv[my_optind][0] != '-' || argv[my_optind][1] == '\0')
108                         return(EOF);
109                 else if(strcmp(argv[my_optind], "--") == 0) {
110                         my_optind++;
111                         return(EOF);
112                 }
113         }
114         my_optopt = c = argv[my_optind][sp];
115         if(c == ':' || (cp=index2((char *) opts, c)) == NULL) {
116                 ERR(": illegal option -- ", c,argv);
117                 if(argv[my_optind][++sp] == '\0') {
118                         my_optind++;
119                         sp = 1;
120                 }
121                 return('?');
122         }
123         if(*++cp == ':') {
124                 if(argv[my_optind][sp+1] != '\0')
125                         my_optarg = &argv[my_optind++][sp+1];
126                 else if(++my_optind >= argc) {
127                         ERR(": option requires an argument -- ", c,argv);
128                         sp = 1;
129                         return('?');
130                 } else
131                         my_optarg = argv[my_optind++];
132                 sp = 1;
133         } else {
134                 if(argv[my_optind][++sp] == '\0') {
135                         sp = 1;
136                         my_optind++;
137                 }
138                 my_optarg = NULL;
139         }
140         return(c);
141 }