Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2469