Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/trunkAutoResolution/i386/libsaio/stringTable.c

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

Archive Download this file

Revision: 1336