summaryrefslogtreecommitdiffstats
path: root/lib/getopt.c
blob: fd12a886ec99d24aaa8fa44dbbba981cdcf3c5db (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
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
/*
 * getopt.c - a simple getopt(3) implementation. See getopt.h for explanation.
 *
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <common.h>
#include <module.h>
#include <getopt.h>

int opterr = 1;
int optind = 1;
int optopt;
char *optarg;

EXPORT_SYMBOL(optind);
EXPORT_SYMBOL(opterr);
EXPORT_SYMBOL(optopt);
EXPORT_SYMBOL(optarg);

static int optindex = 1; /* option index in the current argv[] element */
static int nonopts = 0;  /* number of nonopts found */

void getopt_context_store(struct getopt_context *gc)
{
	gc->optind = optind;
	gc->opterr = opterr;
	gc->optopt = optopt;
	gc->optarg = optarg;
	gc->nonopts = nonopts;
	gc->optindex = optindex;

	optind = opterr = optindex = 1;
	nonopts = 0;
}
EXPORT_SYMBOL(getopt_context_store);

void getopt_context_restore(struct getopt_context *gc)
{
	optind = gc->optind;
	opterr = gc->opterr;
	optopt = gc->optopt;
	optarg = gc->optarg;
	nonopts = gc->nonopts;
	optindex = gc->optindex;
}
EXPORT_SYMBOL(getopt_context_restore);

int getopt(int argc, char *argv[], const char *optstring)
{
	char curopt;   /* current option character */
	const char *curoptp; /* pointer to the current option in optstring */

	while(1) {
		debug("optindex: %d nonopts: %d optind: %d\n", optindex, nonopts, optind);

		/* first put nonopts to the end */
		while (optind + nonopts < argc && *argv[optind] != '-') {
			int i;
			char *tmp;

			nonopts++;
			tmp = argv[optind];
			for (i = optind; i + 1 < argc; i++)
				argv[i] = argv[i + 1];
			argv[argc - 1] = tmp;
		}

		if (optind + nonopts >= argc)
			return -1;

		/* We have found an option */
		curopt = argv[optind][optindex];
		if (curopt)
			break;
		/* no more options in current argv[] element. Start
		 * over with looking for nonopts
		 */
		optind++;
		optindex = 1;
	}

	/* look up current option in optstring */
	curoptp = strchr(optstring, curopt);

	if (!curoptp) {
		if (opterr)
			printf("%s: invalid option -- %c\n", argv[0], curopt);
		optopt = curopt;
		optindex++;
		return '?';
	}

	if (*(curoptp + 1) != ':') {
		/* option with no argument. Just return it */
		optarg = NULL;
		optindex++;
		return curopt;
	}

	if (*(curoptp + 1) && *(curoptp + 2) == ':') {
		/* optional argument */
		if (argv[optind][optindex + 1]) {
			/* optional argument with directly following optarg */
			optarg = argv[optind] + optindex + 1;
			optindex = 1;
			optind++;
			return curopt;
		}
		if (optind + nonopts  + 1 == argc) {
			/* We are at the last argv[] element */
			optarg = NULL;
			optind++;
			return curopt;
		}
		if (*argv[optind + 1] != '-') {
			/* optional argument with optarg in next argv[] element
			 */
			optind++;
			optarg = argv[optind];
			optind++;
			optindex = 1;
			return curopt;
		}

		/* no optional argument found */
		optarg = NULL;
		optindex = 1;
		optind++;
		return curopt;
	}

	if (argv[optind][optindex + 1]) {
		/* required argument with directly following optarg */
		optarg = argv[optind] + optindex + 1;
		optind++;
		optindex = 1;
		return curopt;
	}

	optind++;
	optindex = 1;

	if (optind + nonopts >= argc || argv[optind][0] == '-') {
		if (opterr)
			printf("option requires an argument -- %c\n",
				curopt);
		optopt = curopt;
		return ':';
	}

	optarg = argv[optind];
	optind++;
	return curopt;
}
EXPORT_SYMBOL(getopt);