File: getopt.c

package info (click to toggle)
mupdf 1.25.1%2Bds1-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 21,620 kB
  • sloc: ansic: 270,929; python: 20,709; java: 6,916; javascript: 1,865; makefile: 1,130; xml: 550; sh: 430; cpp: 325; cs: 313; awk: 10; sed: 7; lisp: 3
file content (212 lines) | stat: -rw-r--r-- 4,878 bytes parent folder | download | duplicates (3)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * This is a version of the public domain getopt implementation by
 * Henry Spencer originally posted to net.sources.
 *
 * This file is in the public domain.
 */

#include <stdio.h>
#include <string.h>

#include "mupdf/fitz/getopt.h"
#include "mupdf/fitz/string-util.h"

char *fz_optarg; /* Global argument pointer. */
int fz_optind = 0; /* Global argv index. */
const fz_getopt_long_options *fz_optlong = NULL;
int fz_optitem = 0; /* Which item in a long opt list is selected? */

static int
match_long_option(int argc, char * const *argv, const fz_getopt_long_options *longopts, char *opt)
{
	if (longopts == NULL)
	{
		fprintf(stderr, "%s: unknown option --%s\n", argv[0], opt);
		return '?';
	}

	while (longopts->option)
	{
		const char *s = longopts->option;
		const char *eq = strchr(s, '=');
		size_t z = eq ? (size_t)(eq-s) : strlen(s);
		int arg = 0; /* No arg */

		if (s[z] == '=')
			arg = 1; /* arg = 1 => Arg list given */
		else if (z && s[z-1] == ':')
			arg = 2, z--; /* arg = 2 -> argument expected */

		/* If we don't match, try the next one. */
		/* If we aren't expecting an argument, and the given string is longer than expected, try the next one. */
		/* If we are expecting an argument, and the given string doesn't either terminate or end in '=', try the next one. */
		if (strncmp(s, opt, z) || (arg == 0 && opt[z] != 0) || (arg != 0 && opt[z] != 0 && opt[z] != '='))
		{
			longopts++;
			continue;
		}

		/* So we have a match. */
		fz_optind++;
		if (opt[z])
			fz_optarg = &opt[z+1];
		else if (fz_optind < argc)
			fz_optarg = argv[fz_optind++];
		else
		{
			fprintf(stderr, "%s: option requires argument --%s\n", argv[0], s);
			return ':';
		}

		/* If there is an arg from a predefined list, try to match that here. */
		if (arg == 1)
		{
			fz_optitem = fz_opt_from_list(&opt[z+1], &s[z+1]);
			if (longopts->flag)
				*longopts->flag = fz_optitem;
			if (fz_optitem < 0)
				return '?';
		}
		else if (arg == 2 && longopts->flag)
		{
			char *p = strchr(fz_optarg, ',');
			if (*fz_optarg == 0)
			{
				fprintf(stderr, "%s: option requires argument --%s\n", argv[0], s);
				return ':';
			}
			else if (!fz_strcasecmp(fz_optarg, "yes") || !fz_strcasecmp(fz_optarg, "on") || !fz_strcasecmp(fz_optarg, "true"))
				*longopts->flag = 1;
			else if (!fz_strcasecmp(fz_optarg, "no") || !fz_strcasecmp(fz_optarg, "off") || !fz_strcasecmp(fz_optarg, "false"))
				*longopts->flag = 0;
			else
				*longopts->flag = fz_atoi(fz_optarg);
			fz_optarg = p ? p+1 : NULL;
		}
		fz_optlong = longopts;
		return 0;
	}

	fprintf(stderr, "%s: unknown option --%s\n", argv[0], opt);
	return '?';
}

static char *scan = NULL; /* Private scan pointer. */

int
fz_getopt_long(int argc, char * const *argv, const char *optstring, const fz_getopt_long_options *longopts)
{
	int c;
	const char *place;

	fz_optarg = NULL;
	fz_optlong = NULL;
	fz_optitem = -1;

	while (!scan || *scan == '\0')
	{
		if (fz_optind == 0)
			fz_optind++;

		if (fz_optind >= argc || argv[fz_optind][0] != '-' || argv[fz_optind][1] == '\0')
			return EOF;
		if (argv[fz_optind][1] == '-' && argv[fz_optind][2] == '\0') {
			fz_optind++;
			return EOF;
		}
		if (argv[fz_optind][1] == '-')
		{
			scan = NULL;
			return match_long_option(argc, argv, longopts, &argv[fz_optind][2]);
		}

		scan = argv[fz_optind]+1;
		fz_optind++;
	}

	c = *scan++;
	place = strchr(optstring, c);

	if (!place || c == ':') {
		fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
		return '?';
	}

	place++;
	if (*place == ':') {
		if (*scan != '\0') {
			fz_optarg = scan;
			scan = NULL;
		} else if( fz_optind < argc ) {
			fz_optarg = argv[fz_optind];
			fz_optind++;
		} else {
			fprintf(stderr, "%s: option requires argument -%c\n", argv[0], c);
			return ':';
		}
	}

	return c;
}

int
fz_getopt(int argc, char *const *argv, const char *optstring)
{
	return fz_getopt_long(argc, argv, optstring, NULL);
}

int
fz_opt_from_list(char *opt, const char *optlist)
{
	int n = 0;

	while (*optlist)
	{
		const char *optend = optlist;

		if (*optend == '*')
		{
			fz_optarg = opt;
			return n;
		}

		while (*optend != 0 && *optend != '|' && *optend != ':')
			optend++;

		if (fz_strncasecmp(optlist, opt, optend-optlist))
		{
			/* We didn't match. Carry on. */
		}
		else if (opt[optend-optlist] == 0)
		{
			/* We matched, ending in NUL */
			fz_optarg = NULL;
			return n;
		}
		else if (*optend == ':' && opt[optend-optlist] == ':')
		{
			/* We matched, and we both have some arguments and expected some. */
			fz_optarg = &opt[optend-optlist+1];
			return n;
		}

		n++;
		if (*optend == ':')
		{
			optend++;
			if (*optend == '|')
				optend++;
			else if (*optend != 0)
			{
				fprintf(stderr, "Malformed options string");
				return -1;
			}
		}
		if (*optend == '|')
			optend++;
		optlist = optend;
	}

	fprintf(stderr, "Unrecognised option argument: %s\n", opt);
	return -1;
}