1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * Copyright 1993 NeXT, Inc.␊ |
26 | * All rights reserved.␊ |
27 | */␊ |
28 | ␊ |
29 | #include "bootstruct.h"␊ |
30 | #include "libsaio.h"␊ |
31 | #include "xml.h"␊ |
32 | ␊ |
33 | extern char *Language;␊ |
34 | extern char *LoadableFamilies;␊ |
35 | ␊ |
36 | bool sysConfigValid;␊ |
37 | ␊ |
38 | /*␊ |
39 | * Compare a string to a key with quoted characters␊ |
40 | */␊ |
41 | static inline int␊ |
42 | keyncmp(const char *str, const char *key, int n)␊ |
43 | {␊ |
44 | int c;␊ |
45 | while (n--) {␊ |
46 | ␉c = *key++;␊ |
47 | ␉if (c == '\\') {␊ |
48 | ␉ switch(c = *key++) {␊ |
49 | ␉ case 'n':␊ |
50 | ␉␉c = '\n';␊ |
51 | ␉␉break;␊ |
52 | ␉ case 'r':␊ |
53 | ␉␉c = '\r';␊ |
54 | ␉␉break;␊ |
55 | ␉ case 't':␊ |
56 | ␉␉c = '\t';␊ |
57 | ␉␉break;␊ |
58 | ␉ default:␊ |
59 | ␉␉break;␊ |
60 | ␉ }␊ |
61 | ␉} else if (c == '\"') {␊ |
62 | ␉ /* Premature end of key */␊ |
63 | ␉ return 1;␊ |
64 | ␉}␊ |
65 | ␉if (c != *str++) {␊ |
66 | ␉ return 1;␊ |
67 | ␉}␊ |
68 | }␊ |
69 | return 0;␊ |
70 | }␊ |
71 | ␊ |
72 | #if UNUSED␊ |
73 | ␊ |
74 | static void eatThru(char val, const char **table_p)␊ |
75 | {␊ |
76 | ␉register const char *table = *table_p;␊ |
77 | ␉register bool found = false;␊ |
78 | ␊ |
79 | ␉while (*table && !found)␊ |
80 | ␉{␊ |
81 | ␉␉if (*table == '\\') table += 2;␊ |
82 | ␉␉else␊ |
83 | ␉␉{␊ |
84 | ␉␉␉if (*table == val) found = true;␊ |
85 | ␉␉␉table++;␊ |
86 | ␉␉}␊ |
87 | ␉}␊ |
88 | ␉*table_p = table;␊ |
89 | }␊ |
90 | ␊ |
91 | /* Remove key and its associated value from the table. */␊ |
92 | ␊ |
93 | bool␊ |
94 | removeKeyFromTable(const char *key, char *table)␊ |
95 | {␊ |
96 | register int len;␊ |
97 | register char *tab;␊ |
98 | char *buf;␊ |
99 | ␊ |
100 | len = strlen(key);␊ |
101 | tab = (char *)table;␊ |
102 | buf = (char *)malloc(len + 3);␊ |
103 | ␊ |
104 | sprintf(buf, "\"%s\"", key);␊ |
105 | len = strlen(buf);␊ |
106 | ␊ |
107 | while(*tab) {␊ |
108 | if(strncmp(buf, tab, len) == 0) {␊ |
109 | char c;␊ |
110 | ␊ |
111 | while((c = *(tab + len)) != ';') {␊ |
112 | if(c == 0) {␊ |
113 | len = -1;␊ |
114 | goto out;␊ |
115 | }␊ |
116 | len++;␊ |
117 | }␊ |
118 | len++;␊ |
119 | if(*(tab + len) == '\n') len++;␊ |
120 | goto out;␊ |
121 | }␊ |
122 | tab++;␊ |
123 | }␊ |
124 | len = -1;␊ |
125 | out:␊ |
126 | free(buf);␊ |
127 | ␊ |
128 | if(len == -1) return false;␊ |
129 | ␊ |
130 | while((*tab = *(tab + len))) {␊ |
131 | tab++;␊ |
132 | }␊ |
133 | ␊ |
134 | return true;␊ |
135 | }␊ |
136 | ␊ |
137 | char *␊ |
138 | newStringFromList(␊ |
139 | char **list,␊ |
140 | int *size␊ |
141 | )␊ |
142 | {␊ |
143 | char *begin = *list, *end;␊ |
144 | char *newstr;␊ |
145 | int newsize = *size;␊ |
146 | int bufsize;␊ |
147 | ␊ |
148 | while (*begin && newsize && isspace(*begin)) {␊ |
149 | ␉begin++;␊ |
150 | ␉newsize--;␊ |
151 | }␊ |
152 | end = begin;␊ |
153 | while (*end && newsize && !isspace(*end)) {␊ |
154 | ␉end++;␊ |
155 | ␉newsize--;␊ |
156 | }␊ |
157 | if (begin == end)␊ |
158 | ␉return 0;␊ |
159 | bufsize = end - begin + 1;␊ |
160 | newstr = malloc(bufsize);␊ |
161 | strlcpy(newstr, begin, bufsize);␊ |
162 | *list = end;␊ |
163 | *size = newsize;␊ |
164 | return newstr;␊ |
165 | }␊ |
166 | ␊ |
167 | #endif␊ |
168 | ␊ |
169 | /* ␊ |
170 | * compress == compress escaped characters to one character␊ |
171 | */␊ |
172 | int stringLength(const char *table, int compress)␊ |
173 | {␊ |
174 | ␉int ret = 0;␊ |
175 | ␊ |
176 | ␉while (*table)␊ |
177 | ␉{␊ |
178 | ␉␉if (*table == '\\')␊ |
179 | ␉␉{␊ |
180 | ␉␉␉table += 2;␊ |
181 | ␉␉␉ret += 1 + (compress ? 0 : 1);␊ |
182 | ␉␉}␊ |
183 | ␉␉else␊ |
184 | ␉␉{␊ |
185 | ␉␉␉if (*table == '\"') return ret;␊ |
186 | ␉␉␉ret++;␊ |
187 | ␉␉␉table++;␊ |
188 | ␉␉}␊ |
189 | ␉}␊ |
190 | ␉return ret;␊ |
191 | }␊ |
192 | ␊ |
193 | ␊ |
194 | bool getValueForConfigTableKey(config_file_t *config, const char *key, const char **val, int *size)␊ |
195 | {␊ |
196 | ␉if (config->dictionary != 0 ) {␊ |
197 | ␉␉// Look up key in XML dictionary␊ |
198 | ␉␉TagPtr value;␊ |
199 | ␉␉value = XMLGetProperty(config->dictionary, key);␊ |
200 | ␉␉if (value != 0) {␊ |
201 | ␉␉␉if (value->type != kTagTypeString) {␊ |
202 | ␉␉␉␉error("Non-string tag '%s' found in config file\n",␊ |
203 | ␉␉␉␉␉ key);␊ |
204 | ␉␉␉␉return false;␊ |
205 | ␉␉␉}␊ |
206 | ␉␉␉*val = value->string;␊ |
207 | ␉␉␉*size = strlen(value->string);␊ |
208 | ␉␉␉return true;␊ |
209 | ␉␉}␊ |
210 | ␉} else {␊ |
211 | ␉␊ |
212 | ␉␉// Legacy plist-style table␊ |
213 | ␊ |
214 | ␉}␊ |
215 | ␊ |
216 | ␉return false;␊ |
217 | }␊ |
218 | ␊ |
219 | #if UNUSED␊ |
220 | ␊ |
221 | /*␊ |
222 | * Returns a new malloc'ed string if one is found␊ |
223 | * in the string table matching 'key'. Also translates␊ |
224 | * \n escapes in the string.␊ |
225 | */␊ |
226 | char *newStringForStringTableKey(␊ |
227 | ␉char *table,␊ |
228 | ␉char *key,␊ |
229 | ␉config_file_t *config␊ |
230 | )␊ |
231 | {␊ |
232 | const char *val;␊ |
233 | char *newstr, *p;␊ |
234 | int size;␊ |
235 | ␊ |
236 | if (getValueForConfigTableKey(config, key, &val, &size)) {␊ |
237 | ␉newstr = (char *)malloc(size+1);␊ |
238 | ␉for (p = newstr; size; size--, p++, val++) {␊ |
239 | ␉ if ((*p = *val) == '\\') {␊ |
240 | ␉␉switch (*++val) {␊ |
241 | ␉␉case 'r':␊ |
242 | ␉␉ *p = '\r';␊ |
243 | ␉␉ break;␊ |
244 | ␉␉case 'n':␊ |
245 | ␉␉ *p = '\n';␊ |
246 | ␉␉ break;␊ |
247 | ␉␉case 't':␊ |
248 | ␉␉ *p = '\t';␊ |
249 | ␉␉ break;␊ |
250 | ␉␉default:␊ |
251 | ␉␉ *p = *val;␊ |
252 | ␉␉ break;␊ |
253 | ␉␉}␊ |
254 | ␉␉size--;␊ |
255 | ␉ }␊ |
256 | ␉}␊ |
257 | ␉*p = '\0';␊ |
258 | ␉return newstr;␊ |
259 | } else {␊ |
260 | ␉return 0;␊ |
261 | }␊ |
262 | }␊ |
263 | ␊ |
264 | #endif␊ |
265 | ␊ |
266 | char *␊ |
267 | newStringForKey(char *key, config_file_t *config)␊ |
268 | {␊ |
269 | const char *val;␊ |
270 | char *newstr;␊ |
271 | int size;␊ |
272 | ␊ |
273 | if (getValueForKey(key, &val, &size, config) && size) {␊ |
274 | ␉newstr = (char *)malloc(size + 1);␊ |
275 | ␉strlcpy(newstr, val, size + 1);␊ |
276 | ␉return newstr;␊ |
277 | } else {␊ |
278 | ␉return 0;␊ |
279 | }␊ |
280 | }␊ |
281 | ␊ |
282 | /* parse a command line␊ |
283 | * in the form: [<argument> ...] [<option>=<value> ...]␊ |
284 | * both <option> and <value> must be either composed of␊ |
285 | * non-whitespace characters, or enclosed in quotes.␊ |
286 | */␊ |
287 | ␊ |
288 | static const char *getToken(const char *line, const char **begin, int *len)␊ |
289 | {␊ |
290 | if (*line == '\"') {␊ |
291 | ␉*begin = ++line;␊ |
292 | ␉while (*line && *line != '\"')␊ |
293 | ␉ line++;␊ |
294 | ␉*len = line++ - *begin;␊ |
295 | } else {␊ |
296 | ␉*begin = line;␊ |
297 | ␉while (*line && !isspace(*line) && *line != '=')␊ |
298 | ␉ line++;␊ |
299 | ␉*len = line - *begin;␊ |
300 | }␊ |
301 | return line;␊ |
302 | }␊ |
303 | ␊ |
304 | bool getValueForBootKey(const char *line, const char *match, const char **matchval, int *len)␊ |
305 | {␊ |
306 | const char *key, *value;␊ |
307 | int key_len, value_len;␊ |
308 | bool retval = false;␊ |
309 | ␊ |
310 | while (*line) {␊ |
311 | ␉/* look for keyword or argument */␊ |
312 | ␉while (isspace(*line)) line++;␊ |
313 | ␊ |
314 | ␉/* now look for '=' or whitespace */␊ |
315 | ␉line = getToken(line, &key, &key_len);␊ |
316 | ␉/* line now points to '=' or space */␊ |
317 | ␉if (*line && !isspace(*line)) {␊ |
318 | ␉ line = getToken(++line, &value, &value_len);␊ |
319 | ␉} else {␊ |
320 | ␉ value = line;␊ |
321 | ␉ value_len = 0;␊ |
322 | ␉}␊ |
323 | ␉if ((strlen(match) == key_len)␊ |
324 | ␉ && strncmp(match, key, key_len) == 0) {␊ |
325 | ␉ *matchval = value;␊ |
326 | ␉ *len = value_len;␊ |
327 | ␉ retval = true;␊ |
328 | /* Continue to look for this key; last one wins. */␊ |
329 | ␉}␊ |
330 | }␊ |
331 | return retval;␊ |
332 | }␊ |
333 | ␊ |
334 | /* Return NULL if no option has been successfully retrieved, or the string otherwise */␊ |
335 | const char * getStringForKey(const char * key, config_file_t *config)␊ |
336 | {␊ |
337 | static const char* value =0;␊ |
338 | int len=0;␊ |
339 | if(!getValueForKey(key, &value, &len, config)) value = 0;␊ |
340 | return value;␊ |
341 | }␊ |
342 | ␊ |
343 | ␊ |
344 | /* Returns TRUE if a value was found, FALSE otherwise.␊ |
345 | * The boolean value of the key is stored in 'val'.␊ |
346 | */␊ |
347 | ␊ |
348 | bool getBoolForKey( const char *key, bool *result_val, config_file_t *config )␊ |
349 | {␊ |
350 | const char *key_val;␊ |
351 | int size;␊ |
352 | ␊ |
353 | if (getValueForKey(key, &key_val, &size, config)) {␊ |
354 | if ( (size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y') ) {␊ |
355 | *result_val = true;␊ |
356 | } else {␊ |
357 | *result_val = false;␊ |
358 | }␊ |
359 | return true;␊ |
360 | }␊ |
361 | return false;␊ |
362 | }␊ |
363 | ␊ |
364 | bool getIntForKey( const char *key, int *value, config_file_t *config )␊ |
365 | {␊ |
366 | const char *val;␊ |
367 | int size, sum;␊ |
368 | bool negative = false;␊ |
369 | ␊ |
370 | if (getValueForKey(key, &val, &size, config))␊ |
371 | ␉{␊ |
372 | ␉␉if ( size )␊ |
373 | ␉␉{␊ |
374 | ␉␉␉if (*val == '-')␊ |
375 | ␉␉␉{␊ |
376 | ␉␉␉␉negative = true;␊ |
377 | ␉␉␉␉val++;␊ |
378 | ␉␉␉␉size--;␊ |
379 | ␉␉␉}␊ |
380 | ␉␉␉␊ |
381 | ␉␉␉for (sum = 0; size > 0; size--)␊ |
382 | ␉␉␉{␊ |
383 | ␉␉␉␉if (*val < '0' || *val > '9')␊ |
384 | ␉␉␉␉␉return false;␊ |
385 | ␉␉␉␉␊ |
386 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
387 | ␉␉␉}␊ |
388 | ␉␉␉␊ |
389 | ␉␉␉if (negative)␊ |
390 | ␉␉␉␉sum = -sum;␊ |
391 | ␉␉␉␊ |
392 | ␉␉␉*value = sum;␊ |
393 | ␉␉␉return true;␊ |
394 | ␉␉}␊ |
395 | ␉}␊ |
396 | return false;␊ |
397 | }␊ |
398 | ␊ |
399 | ␊ |
400 | // valv: experimenting with float␊ |
401 | double getFloatForKey(const char * key, config_file_t *config)␊ |
402 | {␊ |
403 | static const char* value =0;␊ |
404 | char *val;␊ |
405 | int size;␊ |
406 | if (!(getValueForKey(key, &value, &size, config)) || (size == 0)) value = 0;␊ |
407 | val = (char *)malloc(size + 1);␊ |
408 | strlcpy(val, value, size + 1);␊ |
409 | char **end = 0;␊ |
410 | double result = strtod(val, end);␊ |
411 | return result;␊ |
412 | }␊ |
413 | ␊ |
414 | /*␊ |
415 | int getIntForFloat( const char *key, char *value, config_file_t *config )␊ |
416 | {␊ |
417 | int size, sum, i;␊ |
418 | bool isfloat = false;␊ |
419 | sum = 0;␊ |
420 | const char *val;␊ |
421 | ␊ |
422 | if (getValueForKey(key, &val, &size, config))␊ |
423 | ␉{␊ |
424 | ␉␉if ( size )␊ |
425 | ␉␉{␊ |
426 | ␊ |
427 | ␉␉␉for(i = 0; i < size; i++)␊ |
428 | ␉␉␉{␊ |
429 | ␉␉␉␉if (val[i] == '.')␊ |
430 | ␉␉␉␉{␊ |
431 | ␉␉␉␉␉isfloat = true;␊ |
432 | ␉␉␉␉␉i++;␊ |
433 | ␉␉␉␉}␊ |
434 | ␊ |
435 | ␉␉␉␉if (isdigit(val[i]) != 0)␊ |
436 | ␉␉␉␉␉sum = (sum * 10) + val[i];␊ |
437 | ␉␉␉}␊ |
438 | ␊ |
439 | ␉␉␉if (!isfloat)␊ |
440 | ␉␉␉␉sum = (sum * 10);␊ |
441 | ␊ |
442 | ␉␉␉return sum;␊ |
443 | ␉␉}␊ |
444 | ␉}␊ |
445 | return 0;␊ |
446 | }␊ |
447 | */␊ |
448 | ␊ |
449 | bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )␊ |
450 | {␊ |
451 | ␉const char *val;␊ |
452 | ␉␊ |
453 | int size = 0;␊ |
454 | ␉int sum = 0;␊ |
455 | ␊ |
456 | ␉bool negative = false;␊ |
457 | ␉bool percentage = false;␊ |
458 | ␉␊ |
459 | if (getValueForKey(key, &val, &size, config))␊ |
460 | ␉{␊ |
461 | ␉␉if ( size )␊ |
462 | ␉␉{␊ |
463 | ␉␉␉if (*val == '-')␊ |
464 | ␉␉␉{␊ |
465 | ␉␉␉␉negative = true;␊ |
466 | ␉␉␉␉val++;␊ |
467 | ␉␉␉␉size--;␊ |
468 | ␉␉␉}␊ |
469 | ␉␉␉␊ |
470 | ␉␉␉if (val[size-1] == '%')␊ |
471 | ␉␉␉{␊ |
472 | ␉␉␉␉percentage = true;␊ |
473 | ␉␉␉␉size--;␊ |
474 | ␉␉␉}␊ |
475 | ␉␉␉␊ |
476 | ␉␉␉// convert string to integer␊ |
477 | ␉␉␉for (sum = 0; size > 0; size--)␊ |
478 | ␉␉␉{␊ |
479 | ␉␉␉␉if (*val < '0' || *val > '9')␊ |
480 | ␉␉␉␉␉return false;␊ |
481 | ␉␉␉␉␊ |
482 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
483 | ␉␉␉}␊ |
484 | ␉␉␉␊ |
485 | ␉␉␉if (percentage)␊ |
486 | ␉␉␉␉sum = ( dimension_max * sum ) / 100;␊ |
487 | ␉␉␉␊ |
488 | ␉␉␉// calculate offset from opposite origin␊ |
489 | ␉␉␉if (negative)␊ |
490 | ␉␉␉␉sum = ( ( dimension_max - object_size ) - sum );␊ |
491 | ␉␉␉␊ |
492 | ␉␉} else {␊ |
493 | ␉␉␉␊ |
494 | ␉␉␉// null value calculate center␊ |
495 | ␉␉␉sum = ( dimension_max - object_size ) / 2;␊ |
496 | ␉␉␉␊ |
497 | ␉␉}␊ |
498 | ␉␉␊ |
499 | ␉␉*value = (uint16_t) sum;␊ |
500 | ␉␉return true;␊ |
501 | ␉}␊ |
502 | ␉␊ |
503 | ␉// key not found␊ |
504 | return false;␊ |
505 | }␊ |
506 | ␊ |
507 | /*␊ |
508 | *␉get color value from plist format #RRGGBB␊ |
509 | */␊ |
510 | ␊ |
511 | bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )␊ |
512 | {␊ |
513 | const char *val;␊ |
514 | int size;␊ |
515 | ␊ |
516 | if (getValueForKey(key, &val, &size, config))␊ |
517 | ␉{␊ |
518 | ␉␉if (*val == '#')␊ |
519 | ␉␉{␊ |
520 | val++;␊ |
521 | ␉␉␉*value = strtol(val, NULL, 16);␊ |
522 | ␉␉␉return true;␊ |
523 | }␊ |
524 | }␊ |
525 | return false;␊ |
526 | }␊ |
527 | ␊ |
528 | bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )␊ |
529 | {␊ |
530 | const char *overrideVal;␊ |
531 | int overrideSize;␊ |
532 | bool override, ret;␊ |
533 | ␊ |
534 | if (getValueForBootKey(bootArgs->CommandLine, key, val, size))␊ |
535 | return true;␊ |
536 | ␊ |
537 | ret = getValueForConfigTableKey(config, key, val, size);␊ |
538 | ␊ |
539 | // Try to find alternate keys in bootInfo->overrideConfig␊ |
540 | // and prefer its values with the exceptions for␊ |
541 | // "Kernel"="mach_kernel" and "Kernel Flags"="".␊ |
542 | ␊ |
543 | if (config->canOverride)␊ |
544 | {␊ |
545 | if (getValueForConfigTableKey(&bootInfo->overrideConfig, key, &overrideVal, &overrideSize))␊ |
546 | {␊ |
547 | override = true;␊ |
548 | ␊ |
549 | if (ret && (strcmp(key, "Kernel") == 0) && (strcmp(overrideVal, "mach_kernel") == 0))␊ |
550 | override = false;␊ |
551 | ␊ |
552 | if (ret && (strcmp(key, "Kernel Flags") == 0) && (overrideSize == 0))␊ |
553 | override = false;␊ |
554 | ␊ |
555 | if (override)␊ |
556 | {␊ |
557 | *val = overrideVal;␊ |
558 | *size = overrideSize;␊ |
559 | return true;␊ |
560 | }␊ |
561 | }␊ |
562 | }␊ |
563 | ␊ |
564 | return ret;␊ |
565 | }␊ |
566 | ␊ |
567 | ␊ |
568 | #if UNUSED␊ |
569 | void␊ |
570 | printSystemConfig(char *p1)␊ |
571 | {␊ |
572 | char *p2 = p1, tmp;␊ |
573 | ␊ |
574 | while (*p1 != '\0') {␊ |
575 | ␉while (*p2 != '\0' && *p2 != '\n') p2++;␊ |
576 | ␉tmp = *p2;␊ |
577 | ␉*p2 = '\0';␊ |
578 | ␉printf("%s\n", p1);␊ |
579 | ␉*p2 = tmp;␊ |
580 | ␉if (tmp == '\0') break;␊ |
581 | ␉p1 = ++p2;␊ |
582 | }␊ |
583 | }␊ |
584 | #endif␊ |
585 | ␊ |
586 | //==========================================================================␊ |
587 | // ParseXMLFile␊ |
588 | // Modifies the input buffer.␊ |
589 | // Expects to see one dictionary in the XML file.␊ |
590 | // Puts the first dictionary it finds in the␊ |
591 | // tag pointer and returns 0, or returns -1 if not found␊ |
592 | // (and does not modify dict pointer).␊ |
593 | // Prints an error message if there is a parsing error.␊ |
594 | //␊ |
595 | int ParseXMLFile( char * buffer, TagPtr * dict )␊ |
596 | {␊ |
597 | long length, pos;␊ |
598 | TagPtr tag;␊ |
599 | pos = 0;␊ |
600 | char *configBuffer;␊ |
601 | ␊ |
602 | configBuffer = malloc(strlen(buffer)+1);␊ |
603 | strcpy(configBuffer, buffer);␊ |
604 | ␊ |
605 | while (1)␊ |
606 | {␊ |
607 | length = XMLParseNextTag(configBuffer + pos, &tag);␊ |
608 | if (length == -1) break;␊ |
609 | ␊ |
610 | pos += length;␊ |
611 | ␊ |
612 | if (tag == 0) continue;␊ |
613 | if (tag->type == kTagTypeDict) break;␊ |
614 | ␊ |
615 | XMLFreeTag(tag);␊ |
616 | }␊ |
617 | free(configBuffer);␊ |
618 | if (length < 0) {␊ |
619 | error ("Error parsing plist file\n");␊ |
620 | return -1;␊ |
621 | }␊ |
622 | *dict = tag;␊ |
623 | return 0;␊ |
624 | }␊ |
625 | ␊ |
626 | /* loadConfigFile␊ |
627 | *␊ |
628 | * Returns 0 - successful.␊ |
629 | *␉␉ -1 - unsuccesful.␊ |
630 | */␊ |
631 | int loadConfigFile (const char *configFile, config_file_t *config)␊ |
632 | {␊ |
633 | ␉int fd, count;␊ |
634 | ␊ |
635 | ␉if ((fd = open_bvdev("bt(0,0)", configFile, 0)) < 0) {␊ |
636 | ␉␉return -1;␊ |
637 | ␉}␊ |
638 | ␉// read file␊ |
639 | ␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
640 | ␉close(fd);␊ |
641 | ␉␊ |
642 | ␉// build xml dictionary␊ |
643 | ␉ParseXMLFile(config->plist, &config->dictionary);␊ |
644 | ␉return 0;␊ |
645 | }␊ |
646 | ␊ |
647 | ␊ |
648 | /* loadSystemConfig␊ |
649 | *␊ |
650 | * Returns 0 - successful.␊ |
651 | *␉␉ -1 - unsuccesful.␊ |
652 | */␊ |
653 | int loadSystemConfig(config_file_t *config)␊ |
654 | {␊ |
655 | ␉char *dirspec[] = {␊ |
656 | ␉␉"bt(0,0)/Extra/com.apple.Boot.plist",␊ |
657 | ␉␉"/Extra/com.apple.Boot.plist",␊ |
658 | ␉␉"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
659 | ␉␉"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
660 | ␉␉"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
661 | ␉␉"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
662 | ␉};␊ |
663 | ␊ |
664 | ␉int i, fd, count, ret=-1;␊ |
665 | ␊ |
666 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)␊ |
667 | ␉{␊ |
668 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0)␊ |
669 | ␉␉{␊ |
670 | ␉␉␉// read file␊ |
671 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
672 | ␉␉␉close(fd);␊ |
673 | ␉␉␉␊ |
674 | ␉␉␉// build xml dictionary␊ |
675 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
676 | ␉␉␉sysConfigValid = true;␉␊ |
677 | ␉␉␉ret=0;␊ |
678 | ␉␉␉␊ |
679 | ␉␉␉// enable canOverride flag␊ |
680 | ␉␉␉config->canOverride = true;␊ |
681 | ␊ |
682 | ␉␉␉break;␊ |
683 | ␉␉}␊ |
684 | ␉}␊ |
685 | ␉return ret;␊ |
686 | }␊ |
687 | ␊ |
688 | /* loadOverrideConfig␊ |
689 | *␊ |
690 | * Returns 0 - successful.␊ |
691 | *␉␉ -1 - unsuccesful.␊ |
692 | */␊ |
693 | int loadOverrideConfig(config_file_t *config)␊ |
694 | {␊ |
695 | ␉char *dirspec[] = {␊ |
696 | ␉␉"rd(0,0)/Extra/com.apple.Boot.plist",␊ |
697 | ␉␉"/Extra/com.apple.Boot.plist",␊ |
698 | ␉␉"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
699 | ␉␉"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
700 | ␉␉"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
701 | ␉␉"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
702 | ␉};␊ |
703 | ␊ |
704 | ␉int i, fd, count, ret=-1;␊ |
705 | ␊ |
706 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)␊ |
707 | ␉{␊ |
708 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0)␊ |
709 | ␉␉{␊ |
710 | ␉␉␉// read file␊ |
711 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
712 | ␉␉␉close(fd);␊ |
713 | ␉␉␉␊ |
714 | ␉␉␉// build xml dictionary␊ |
715 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
716 | ␉␉␉sysConfigValid = true;␉␊ |
717 | ␉␉␉ret=0;␊ |
718 | ␉␉␉break;␊ |
719 | ␉␉}␊ |
720 | ␉}␊ |
721 | ␉return ret;␊ |
722 | }␊ |
723 | ␊ |
724 | /* loadHelperConfig␊ |
725 | *␊ |
726 | * Returns 0 - successful.␊ |
727 | *␉␉ -1 - unsuccesful.␊ |
728 | */␊ |
729 | int loadHelperConfig(config_file_t *config)␊ |
730 | {␊ |
731 | ␉char *dirspec[] = {␊ |
732 | ␉␉"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
733 | ␉␉"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
734 | ␉␉"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
735 | ␉};␊ |
736 | ␊ |
737 | ␉int i, fd, count, ret=-1;␊ |
738 | ␊ |
739 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)␊ |
740 | ␉{␊ |
741 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0)␊ |
742 | ␉␉{␊ |
743 | ␉␉␉// read file␊ |
744 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
745 | ␉␉␉close(fd);␊ |
746 | ␉␉␉␊ |
747 | ␉␉␉// build xml dictionary␊ |
748 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
749 | ␉␉␉sysConfigValid = true;␉␊ |
750 | ␉␉␉ret=0;␊ |
751 | ␉␉␉break;␊ |
752 | ␉␉}␊ |
753 | ␉}␊ |
754 | ␉return ret;␊ |
755 | }␊ |
756 | ␊ |
757 | char * newString(const char * oldString)␊ |
758 | {␊ |
759 | if ( oldString )␊ |
760 | return strcpy(malloc(strlen(oldString)+1), oldString);␊ |
761 | else␊ |
762 | return NULL;␊ |
763 | }␊ |
764 | ␊ |
765 | /*␊ |
766 | * Extracts the next argument from the command line, double quotes are allowed here.␊ |
767 | */␊ |
768 | char * getNextArg(char ** argPtr, char * val)␊ |
769 | {␊ |
770 | char * ptr = *argPtr;␊ |
771 | const char * strStart;␊ |
772 | int len = 0;␊ |
773 | bool isQuoted = false;␊ |
774 | ␊ |
775 | *val = '\0';␊ |
776 | ␊ |
777 | // Scan for the next non-whitespace character.␊ |
778 | while ( *ptr && (*ptr == ' ' || *ptr == '=') )␊ |
779 | {␊ |
780 | ptr++;␊ |
781 | }␊ |
782 | ␊ |
783 | strStart = ptr;␊ |
784 | ␊ |
785 | // Skip the leading double quote character.␊ |
786 | if (*ptr == '\"')␊ |
787 | {␊ |
788 | isQuoted = true;␊ |
789 | ptr++;␊ |
790 | strStart++;␊ |
791 | }␊ |
792 | ␊ |
793 | // Scan for the argument terminator character.␊ |
794 | // This can be either a NULL character - in case we reach the end of the string,␊ |
795 | // a double quote in case of quoted argument,␊ |
796 | // or a whitespace character (' ' or '=') for non-quoted argument.␊ |
797 | while (*ptr && !( (isQuoted && (*ptr == '\"')) ||␊ |
798 | (!isQuoted && (*ptr == ' ' || *ptr == '=')) )␊ |
799 | )␊ |
800 | {␊ |
801 | ptr++;␊ |
802 | }␊ |
803 | ␊ |
804 | len = ptr - strStart;␊ |
805 | ␊ |
806 | // Skip the closing double quote character and adjust␊ |
807 | // the starting pointer for the next getNextArg call.␊ |
808 | if (*ptr && isQuoted && *ptr == '\"')␊ |
809 | ptr++;␊ |
810 | ␊ |
811 | // Copy the extracted argument to val.␊ |
812 | strncat(val, strStart, len);␊ |
813 | ␊ |
814 | // Set command line pointer.␊ |
815 | *argPtr = ptr;␊ |
816 | ␊ |
817 | return ptr;␊ |
818 | }␊ |
819 | |