File: xgetopt.c

package info (click to toggle)
bsmtpd 2.3pl8b-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 220 kB
  • ctags: 236
  • sloc: ansic: 1,928; makefile: 178; sh: 31
file content (198 lines) | stat: -rw-r--r-- 4,420 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * xgetopt.c v1.0 fifi@veeble.uucp
 */
 
#include	<stdio.h>
#include	<ctype.h>
#include	<string.h>
#ifdef __GNUC__
# ifdef __STDC__
#  if defined(__NeXT__) || defined(__linux__) || defined(__svr4__)
#   include	<varargs.h>
#  else
#   include	<stdargs.h>
#  endif
#  if !defined(va_list) && !defined(__NeXT__)
#   define va_list _G_va_list
#  endif
# else
#  include	"/usr/local/lib/gcc-include/varargs.h"
# endif
#else
# include	<varargs.h>
#endif

char	*xoptarg = NULL;
int	xoptind = 0;
char	*xoptpnt = NULL;
char	xoptflag = '\0';
char	*xoptnull = "\0";

int
xgetopt(argc, argv, descr)
register int	argc;
register char	**argv;
register char	*descr;
{
	/*
	 * verarbeitung wie 'getopt', jedoch werden auch angaben
	 * like '+abc' akzeptiert. werden flags mit + uebergeben,
	 * so erscheint das flag im return als capital letter,
	 * bei '-' als small letter.
	 */
	char	arg;

	while(xoptind < argc) {
		/* pruefen, ob evtl in einer argumentkette */
		if (!xoptpnt || !*xoptpnt) xoptflag = '\0';
		if (xoptflag) {
			/* ok... wir durchsuchen die +/- - liste */
			arg = *xoptpnt++;
		} else {
			/* keine liste, naechsten index waehlen */
			xoptarg = xoptpnt = argv[++xoptind];
			if (xoptpnt && ((*xoptpnt == '+') || (*xoptpnt == '-'))) {
				xoptflag = *xoptpnt++;
				if (!(arg = *xoptpnt++)) {
					/* kein argument: +/- ist das argument */
					xoptpnt = NULL;
					arg = xoptflag;
					xoptflag = 0;
				}
			} else {
				/* kein argument mehr da oder NULL-pointer oder kein +/- */
				return(EOF);
			}
		}
		while (descr && *descr && (*descr != arg)) descr++;
		if (!descr || !*descr) {
			/* argument nicht in der arglist */
			return('?');
		} else {
			/* argument erwartet ? */
			if (*(descr+1) == ':') {
				/* direkt dahinter ? */
				if (xoptpnt && *xoptpnt) {
					xoptarg = xoptpnt;
				} else {
					/* ansonsten... weiteres argument vorhanden? */
					if ((xoptind+1) < argc) {
						xoptarg = argv[(xoptind+1)];
						if ((*xoptarg != '+') && (*xoptarg != '-')) {
							/* ok... */
							xoptind++;
						} else {
							xoptarg = xoptnull;
						}
					} else {
						xoptarg = xoptnull;
					}
				}
				xoptpnt = NULL;
			}
			/* kein argument erwartet...  oder argument in xoptarg */
			if (xoptflag == '+') {
				return(arg+256);
			} else {
				return(arg);
			}
		}
	}
	return(EOF);
}

#ifndef __svr4__ /* { */
int
/* VARARGS */
xgetoptstrg(argc, argv, va_alist)
register int	argc;
register char	**argv;
va_dcl
{
	/*
	 * aufruf: xgetoptstrg(argc, argv, "optstrg:idchar[:]" ..., null)
	 */
	va_list	va;
	static char descr[30];
	register char *xoptcp;
	register int i=0;

	while(xoptind < argc) {
		/* keine liste moeglich , naechsten index waehlen */
		xoptarg = xoptpnt = argv[++xoptind];
		if (xoptpnt && ((*xoptpnt == '+') || (*xoptpnt == '-'))) {
			xoptflag = *xoptpnt++;
			if (!(*xoptpnt)) {
				/* kein argument: +/- ist das argument */
				xoptpnt--;
				xoptflag = 0;
			}
		} else {
			/* kein argument mehr da oder NULL-pointer oder kein +/- */
			/* if (!xoptpnt) */
				return(EOF);
			/* xoptflag = 0; */
		}
		/* soll-argumente durchsuchen */
		va_start(va);
		while((xoptcp = va_arg(va, char *))) {
			xoptcp = (char *)strcpy(descr,xoptcp);
			while(*xoptcp && (*xoptcp != ':')) xoptcp++;
			if ((i = strlen(xoptcp)) < 2) {
				va_end(va);
				/* fehler */
				return(EOF);
			}
			*xoptcp++ = '\0';
			if (*xoptcp == ':') {
				xoptcp++;
				/* argument darf nicht folgen */
				if (!strcmp(descr,xoptpnt)) {
					xoptpnt = NULL;
					break;
				}
			} else {
				/* argument darf folgen */
				if (!strncmp(descr,xoptpnt,strlen(descr))) {
					xoptpnt += strlen(descr);
					break;
				}
			}
		}
		va_end(va);
		if (!xoptcp) {
			/* argument nicht in der arglist */
			return('?');
		} else {
			/* argument erwartet ? */
			if (i > 2) {
				/* direkt dahinter ? */
				if (xoptpnt && *xoptpnt) {
					xoptarg = xoptpnt;
				} else {
					/* ansonsten... weiteres argument vorhanden? */
					if ((xoptind+1) < argc) {
						xoptarg = argv[(xoptind+1)];
						if ((*xoptarg != '+') && (*xoptarg != '-')) {
							/* ok... */
							xoptind++;
						} else {
							xoptarg = xoptnull;
						}
					} else {
						xoptarg = xoptnull;
					}
				}
				xoptpnt = NULL;
			}
			/* kein argument erwartet...  oder argument in xoptarg */
			if (xoptflag == '+') {
				return(*xoptcp+256);
			} else {
				return(*xoptcp);
			}
		}
	}
	return(EOF);
}
#endif /* } __svr4__ */