]> git.saurik.com Git - apple/xnu.git/blame - pexpert/gen/bootargs.c
xnu-344.tar.gz
[apple/xnu.git] / pexpert / gen / bootargs.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22#include <pexpert/pexpert.h>
23
24extern boolean_t isargsep( char c);
25extern int argstrcpy(char *from, char *to);
26extern int getval(char *s, int *val);
27
28#define NUM 0
29#define STR 1
30
31boolean_t
32PE_parse_boot_arg(
33 char *arg_string,
34 void *arg_ptr)
35{
36 char *args;
37 char *cp, c;
38 int i;
39 int val;
40 boolean_t arg_boolean;
41 boolean_t arg_found;
42
43 args = PE_boot_args();
9bccf70c
A
44 if (*args == '\0') return FALSE;
45
1c79356b
A
46 arg_found = FALSE;
47
48 while(isargsep(*args)) args++;
49
50 while (*args)
51 {
52 if (*args == '-')
53 arg_boolean = TRUE;
54 else
55 arg_boolean = FALSE;
56
57 cp = args;
58 while (!isargsep (*cp) && *cp != '=')
59 cp++;
60 if (*cp != '=' && !arg_boolean)
61 goto gotit;
62
63 c = *cp;
64
65 i = cp-args;
66 if (strncmp(args, arg_string, i) ||
67 (i!=strlen(arg_string)))
68 goto gotit;
69 if (arg_boolean) {
70 *(unsigned int *)arg_ptr = TRUE;
71 arg_found = TRUE;
72 break;
73 } else {
74 while (isargsep (*cp))
75 cp++;
76 if (*cp == '=' && c != '=') {
77 args = cp+1;
78 goto gotit;
79 }
80
81 switch (getval(cp, &val))
82 {
83 case NUM:
84 *(unsigned int *)arg_ptr = val;
85 arg_found = TRUE;
86 break;
87 case STR:
88 argstrcpy(++cp, (char *)arg_ptr);
89 arg_found = TRUE;
90 break;
91 }
92 goto gotit;
93 }
94gotit:
95 /* Skip over current arg */
96 while(!isargsep(*args)) args++;
97
98 /* Skip leading white space (catch end of args) */
99 while(*args && isargsep(*args)) args++;
100 }
101
102 return(arg_found);
103}
104
105boolean_t isargsep(
106 char c)
107{
108 if (c == ' ' || c == '\0' || c == '\t')
109 return(TRUE);
110 else
111 return(FALSE);
112}
113
114int
115argstrcpy(
116 char *from,
117 char *to)
118{
119 int i = 0;
120
121 while (!isargsep(*from)) {
122 i++;
123 *to++ = *from++;
124 }
125 *to = 0;
126 return(i);
127}
128
129int
130getval(
131 char *s,
132 int *val)
133{
134 register unsigned radix, intval;
135 register unsigned char c;
136 int sign = 1;
137
138 if (*s == '=') {
139 s++;
140 if (*s == '-')
141 sign = -1, s++;
142 intval = *s++-'0';
143 radix = 10;
144 if (intval == 0)
145 switch(*s) {
146
147 case 'x':
148 radix = 16;
149 s++;
150 break;
151
152 case 'b':
153 radix = 2;
154 s++;
155 break;
156
157 case '0': case '1': case '2': case '3':
158 case '4': case '5': case '6': case '7':
159 intval = *s-'0';
160 s++;
161 radix = 8;
162 break;
163
164 default:
165 if (!isargsep(*s))
166 return (STR);
167 }
168 for(;;) {
169 if (((c = *s++) >= '0') && (c <= '9'))
170 c -= '0';
171 else if ((c >= 'a') && (c <= 'f'))
172 c -= 'a' - 10;
173 else if ((c >= 'A') && (c <= 'F'))
174 c -= 'A' - 10;
175 else if (isargsep(c))
176 break;
177 else
178 return (STR);
179 if (c >= radix)
180 return (STR);
181 intval *= radix;
182 intval += c;
183 }
184 *val = intval * sign;
185 return (NUM);
186 }
187 *val = 1;
188 return (NUM);
189}