2 * Simplistic getopt() function for EFI
4 * This function provides the basic functionality of the POSIX getopt() function.
5 * No long options are supported.
7 * This code is meant for EFI programs and therefore deals with Unicode characters.
9 * Copyright (C) 2000 Hewlett-Packard Co
10 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
12 * ELILO is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * ELILO is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with ELILO; see the file COPYING. If not, write to the Free
24 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
32 #define DASH (CHAR16)'-'
33 #define COLON (CHAR16)':'
34 #define EOS (CHAR16)'\0'
35 #define BADCH (INTN)'?'
36 #define BADARG (INTN)':'
38 extern CHAR16 * StrChr(IN const CHAR16 *s, CHAR16 c);
45 * This simple version of getopt supports:
46 * - option with no argument (no :)
47 * - option with REQUIRED argument (single :)
48 * it does not support:
50 * - optional arguments to options
54 Getopt(INTN argc, CHAR16 *const argv[], const CHAR16 *optstring)
56 static CHAR16 *cur_chr = NULL;
59 if (Optind >= argc) { /* no option or end of argument list */
63 if (cur_chr == NULL || *cur_chr == EOS) {
64 cur_chr = argv[Optind];
65 if (*cur_chr++ != DASH) { /* missing DASH */
69 if (*cur_chr == DASH) {
72 return -1; /* -- case, we're done */
76 opt = StrChr(optstring, Optopt);
78 Print(L"%s: illegal option -- %c\n", argv[0], Optopt);
79 if (*cur_chr == EOS) Optind++;
82 if (*(opt+1) != COLON) {
84 if (*cur_chr == EOS) Optind++;
88 } else if ( ++Optind >= argc ) {
89 Print(L"%s: option `%s' requires an argument\n", argv[0], argv[Optind-1]),
93 Optarg = argv[Optind];