-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt.c
98 lines (91 loc) · 2.51 KB
/
getopt.c
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
/*
* Simplistic getopt() function for EFI
*
* This function provides the basic functionality of the POSIX getopt() function.
* No long options are supported.
*
* This code is meant for EFI programs and therefore deals with Unicode characters.
*
* Copyright (C) 2000 Hewlett-Packard Co
* Copyright (C) 2000 Stephane Eranian <[email protected]>
*
* ELILO is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* ELILO 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.
*
* You should have received a copy of the GNU General Public License
* along with ELILO; see the file COPYING. If not, write to the Free
* Software Foundation, 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <efi.h>
#include <efilib.h>
#define DASH (CHAR16)'-'
#define COLON (CHAR16)':'
#define EOS (CHAR16)'\0'
#define BADCH (INTN)'?'
#define BADARG (INTN)':'
extern CHAR16 * StrChr(IN const CHAR16 *s, CHAR16 c);
CHAR16 *Optarg;
INTN Optind = 1;
INTN Optopt;
/*
* This simple version of getopt supports:
* - option with no argument (no :)
* - option with REQUIRED argument (single :)
* it does not support:
* - long options
* - optional arguments to options
* - optreset
*/
INTN
Getopt(INTN argc, CHAR16 *const argv[], const CHAR16 *optstring)
{
static CHAR16 *cur_chr = NULL;
CHAR16 *opt;
if (Optind >= argc) { /* no option or end of argument list */
cur_chr = NULL;
return -1;
}
if (cur_chr == NULL || *cur_chr == EOS) {
cur_chr = argv[Optind];
if (*cur_chr++ != DASH) { /* missing DASH */
cur_chr = NULL;
return -1;
}
if (*cur_chr == DASH) {
cur_chr = NULL;
Optind++;
return -1; /* -- case, we're done */
}
}
Optopt = *cur_chr++;
opt = StrChr(optstring, Optopt);
if (!opt) {
Print(L"%s: illegal option -- %c\n", argv[0], Optopt);
if (*cur_chr == EOS) Optind++;
return BADCH;
}
if (*(opt+1) != COLON) {
Optarg = NULL;
if (*cur_chr == EOS) Optind++;
} else {
if (*cur_chr) {
Optarg = cur_chr;
} else if ( ++Optind >= argc ) {
Print(L"%s: option `%s' requires an argument\n", argv[0], argv[Optind-1]),
cur_chr = NULL;
return BADARG;
} else {
Optarg = argv[Optind];
}
Optind++;
}
return Optopt;
}