summaryrefslogtreecommitdiff
path: root/fpcsrc/utils/sim_pasc/options.c
blob: 9f166766807f06d6c53c07b45fb959574482d609 (plain)
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
/*	This file is part of the software similarity tester SIM.
	Written by Dick Grune, Vrije Universiteit, Amsterdam.
	$Id: options.c,v 1.3 2001/11/13 12:55:53 dick Exp $
*/

#include	<stdio.h>
#include	<stdlib.h>

#include	"options.h"

static char options[128];

static void bad_option(
	const char *progname, const struct option *optlist, char *msg, int c
);
static int opt_value(const struct option *op, const char *arg, char *argv[]);

static int do_arg(
	const char *progname, const struct option *optlist,
	const char *arg, char *argv[]
);

int
do_options(
	const char *progname, const struct option *optlist,
	int argc, char *argv[]
) {
	int skips = 0;

	while (argc > 0 && argv[0][0] == '-' && argv[0][1] != '\0') {
		int consumed = do_arg(progname, optlist, &argv[0][1], argv);

		argc -= consumed, argv += consumed, skips += consumed;
	}

	return skips;
}

int
option_set(char ch) {
	return options[(int)ch];
}

static int
do_arg(
	const char *progname, const struct option *optlist,
	const char *arg, char *argv[]
) {
	int consumed = 0;

	while (*arg) {
		/* treat argument character */
		register char opc = *arg++;
		register const struct option *op;

		for (op = optlist; op->op_char; op++) {
			/* for every allowed option */
			if (opc == op->op_char) {
				options[(int)opc]++;
				if (op->op_indicator != ' ') {
					consumed = opt_value(op, arg, argv);
					if (consumed < 0) {
						bad_option(progname, (struct option *)0,
							" option -%c requires another argument",
							op->op_char
						);
						/*NOTREACHED*/
					}
				}
				break;
			}
		}
		if (!op->op_char) {
			bad_option(progname, optlist,
				"*option -%c unknown", opc
			);
			/*NOTREACHED*/
		}
		if (consumed) break;
	}
	if (!consumed) {
		consumed = 1;
	}
	
	return consumed;
}

static int
opt_value(const struct option *op, const char *arg, char *argv[]) {
	/* locate the option value */
	if (*arg) {
		/* argument is continuation of option */
		*op->op_stringp = arg;
		return 1;
	}
	else {
		/* argument follows option */
		if (!argv[1]) return -1;
		*op->op_stringp = argv[1];
		return 2;
	}
}

static void
bad_option(
	const char *progname, const struct option *optlist, char *msg, int c
) {
	fprintf(stderr, "%s: ", progname);
	fprintf(stderr, &msg[1], c);
	fprintf(stderr, "\n");

	if (msg[0] != ' ') {
		register const struct option *op;

		fprintf(stderr, "Possible options are:\n");
		for (op = optlist; op->op_char; op++) {
			fprintf(stderr, "\t-%c%c\t%s\n",
				op->op_char, op->op_indicator, op->op_text
			);
		}
	}
	exit(1);
}