orphan
[debian/elilo] / getopt.c
1 /*
2  * Simplistic getopt() function for EFI
3  *
4  * This function provides the basic functionality of the POSIX getopt() function.
5  * No long options are supported.
6  *
7  * This code  is meant for EFI programs and therefore deals with Unicode characters.
8  *
9  * Copyright (C) 2000 Hewlett-Packard Co
10  * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
11  *
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)
15  * any later version.
16  *
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.
21  *
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
25  * 02111-1307, USA.
26  */
27
28 #include <efi.h>
29 #include <efilib.h>
30
31
32 #define DASH    (CHAR16)'-'
33 #define COLON   (CHAR16)':'
34 #define EOS     (CHAR16)'\0'
35 #define BADCH   (INTN)'?'
36 #define BADARG  (INTN)':'
37
38 extern CHAR16 * StrChr(IN const CHAR16 *s, CHAR16 c);
39
40 CHAR16 *Optarg;
41 INTN    Optind = 1;
42 INTN    Optopt;
43
44 /*
45  * This simple version of getopt supports:
46  *      - option with no argument (no :)
47  *      - option with REQUIRED argument (single :)
48  * it does not support:
49  *      - long options
50  *      - optional arguments to options
51  *      - optreset
52  */
53 INTN
54 Getopt(INTN argc, CHAR16 *const argv[], const CHAR16 *optstring)
55 {
56         static CHAR16 *cur_chr = NULL;
57         CHAR16 *opt;
58
59         if (Optind >= argc) { /* no option or end of argument list */
60                 cur_chr = NULL;
61                 return -1;
62         }
63         if (cur_chr == NULL || *cur_chr == EOS) {
64                 cur_chr = argv[Optind];
65                 if (*cur_chr++ != DASH) { /* missing DASH */
66                         cur_chr = NULL;
67                         return -1;
68                 }
69                 if (*cur_chr == DASH) {
70                         cur_chr = NULL;
71                         Optind++;
72                         return -1; /* -- case, we're done */
73                 }
74         }
75         Optopt = *cur_chr++;
76         opt = StrChr(optstring, Optopt);
77         if (!opt) {
78                 Print(L"%s: illegal option -- %c\n", argv[0], Optopt);
79                 if (*cur_chr == EOS) Optind++;
80                 return BADCH;
81         } 
82         if (*(opt+1) != COLON) {
83                 Optarg = NULL;
84                 if (*cur_chr == EOS) Optind++;
85         } else {
86                 if (*cur_chr) {
87                         Optarg = cur_chr;
88                 } else if ( ++Optind >= argc ) {
89                            Print(L"%s: option `%s' requires an argument\n", argv[0], argv[Optind-1]),
90                            cur_chr = NULL;
91                            return BADARG;
92                 } else {
93                         Optarg = argv[Optind];
94                 }
95                 Optind++;
96         }
97         return Optopt;
98 }