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/*
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 *newStringFromList(char **list, int *size)
139{
140char *begin = *list, *end;
141char *newstr;
142int newsize = *size;
143int bufsize;
144
145while (*begin && newsize && isspace(*begin)) {
146begin++;
147newsize--;
148}
149end = begin;
150while (*end && newsize && !isspace(*end)) {
151end++;
152newsize--;
153}
154if (begin == end) {
155return 0;
156}
157bufsize = end - begin + 1;
158newstr = malloc(bufsize);
159strlcpy(newstr, begin, bufsize);
160*list = end;
161*size = newsize;
162return newstr;
163}
164
165#endif
166
167/*
168 * compress == compress escaped characters to one character
169 */
170int stringLength(const char *table, int compress)
171{
172int ret = 0;
173
174while (*table) {
175if (*table == '\\') {
176table += 2;
177ret += 1 + (compress ? 0 : 1);
178} else {
179if (*table == '\"') {
180return ret;
181}
182ret++;
183table++;
184}
185}
186return ret;
187}
188
189
190bool getValueForConfigTableKey(config_file_t *config, const char *key, const char **val, int *size)
191{
192if (config->dictionary != 0 ) {
193// Look up key in XML dictionary
194TagPtr value;
195value = XMLGetProperty(config->dictionary, key);
196if (value != 0) {
197if (value->type != kTagTypeString) {
198error("Non-string tag '%s' found in config file\n", key);
199return false;
200}
201*val = value->string;
202*size = strlen(value->string);
203return true;
204}
205} else {
206
207// Legacy plist-style table
208
209}
210
211return false;
212}
213
214#if UNUSED
215
216/*
217 * Returns a new malloc'ed string if one is found
218 * in the string table matching 'key'. Also translates
219 * \n escapes in the string.
220 */
221char *newStringForStringTableKey(char *table, char *key, config_file_t *config)
222{
223const char *val;
224char *newstr, *p;
225int size;
226
227if (getValueForConfigTableKey(config, key, &val, &size)) {
228newstr = (char *)malloc(size+1);
229for (p = newstr; size; size--, p++, val++) {
230if ((*p = *val) == '\\') {
231switch (*++val) {
232case 'r':
233*p = '\r';
234break;
235case 'n':
236*p = '\n';
237break;
238case 't':
239*p = '\t';
240break;
241default:
242*p = *val;
243break;
244}
245size--;
246}
247}
248*p = '\0';
249return newstr;
250} else {
251return 0;
252}
253}
254
255#endif
256
257char *newStringForKey(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;
510return 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"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
616"/OS X Install Data/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)
650{
651char *dirspec[] = {
652"rd(0,0)/Extra/org.chameleon.Boot.plist",
653"/Extra/org.chameleon.Boot.plist",
654"bt(0,0)/Extra/org.chameleon.Boot.plist",
655
656"rd(0,0)/Extra/com.apple.Boot.plist", /* DEPRECIATED */
657"/Extra/com.apple.Boot.plist", /* DEPRECIATED */
658"bt(0,0)/Extra/com.apple.Boot.plist", /* DEPRECIATED */
659};
660
661int i, fd, count, ret=-1;
662
663for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {
664if ((fd = open(dirspec[i], 0)) >= 0) {
665// Check for depreciated file names and annoy the user about it.
666if(strstr(dirspec[i], "com.apple.Boot.plist")) {
667printf("%s is depreciated.\n", dirspec[i]);
668dirspec[i][strlen(dirspec[i]) - strlen("com.apple.Boot.plist")] = 0;
669printf("Please use the file %sorg.chameleon.Boot.plist instead.\n", dirspec[i]);
670pause();
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++) {
702if ((fd = open(dirspec[i], 0)) >= 0) {
703// read file
704count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);
705close(fd);
706
707// build xml dictionary
708ParseXMLFile(config->plist, &config->dictionary);
709sysConfigValid = true;
710ret=0;
711break;
712}
713}
714return ret;
715}
716
717char * newString(const char * oldString)
718{
719if ( oldString ) {
720return strcpy(malloc(strlen(oldString)+1), oldString);
721} else {
722return NULL;
723}
724}
725
726/*
727 * Extracts the next argument from the command line, double quotes are allowed here.
728 */
729char * getNextArg(char ** argPtr, char * val)
730{
731char * ptr = *argPtr;
732const char * strStart;
733int len = 0;
734bool isQuoted = false;
735
736*val = '\0';
737
738// Scan for the next non-whitespace character.
739while ( *ptr && (*ptr == ' ' || *ptr == '=') ) {
740ptr++;
741}
742
743strStart = ptr;
744
745// Skip the leading double quote character.
746if (*ptr == '\"') {
747isQuoted = true;
748ptr++;
749strStart++;
750}
751
752// Scan for the argument terminator character.
753// This can be either a NULL character - in case we reach the end of the string,
754// a double quote in case of quoted argument,
755// or a whitespace character (' ' or '=') for non-quoted argument.
756while (*ptr && !( (isQuoted && (*ptr == '\"')) ||
757 (!isQuoted && (*ptr == ' ' || *ptr == '=')) )
758 ) {
759ptr++;
760}
761
762len = ptr - strStart;
763
764// Skip the closing double quote character and adjust
765// the starting pointer for the next getNextArg call.
766if (*ptr && isQuoted && *ptr == '\"') {
767ptr++;
768}
769
770// Copy the extracted argument to val.
771strncat(val, strStart, len);
772
773// Set command line pointer.
774*argPtr = ptr;
775
776return ptr;
777}
778

Archive Download this file

Revision: 2379