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
33 #define DASH (CHAR16)'-'
34 #define COLON (CHAR16)':'
35 #define EOS (CHAR16)'\0'
36 #define BADCH (INTN)'?'
37 #define BADARG (INTN)':'
39 extern CHAR16 * StrChr(IN const CHAR16 *s, INT16 c);
46 * This simple version of getopt supports:
47 * - option with no argument (no :)
48 * - option with REQUIRED argument (single :)
49 * it does not support:
51 * - optional arguments to options
55 Getopt(INTN argc, CHAR16 *const argv[], const CHAR16 *optstring)
57 static CHAR16 *cur_chr = NULL;
60 if (Optind >= argc) { /* no option or end of argument list */
64 if (cur_chr == NULL || *cur_chr == EOS) {
65 cur_chr = argv[Optind];
66 if (*cur_chr++ != DASH) { /* missing DASH */
70 if (*cur_chr == DASH) {
73 return -1; /* -- case, we're done */
77 opt = StrChr(optstring, Optopt);
79 Print(L"%s: illegal option -- %c\n", argv[0], Optopt);
80 if (*cur_chr == EOS) Optind++;
83 if (*(opt+1) != COLON) {
85 if (*cur_chr == EOS) Optind++;
89 } else if ( ++Optind >= argc ) {
90 Print(L"%s: option `%s' requires an argument\n", argv[0], argv[Optind-1]),
94 Optarg = argv[Optind];