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