Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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/*
42 * Compare a string to a key with quoted characters
43 */
44static inline int keyncmp(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. */
92bool removeKeyFromTable(const char *key, char *table)
93{
94 register int len;
95 register char *tab;
96 char *buf;
97
98 len = strlen(key);
99 tab = (char *)table;
100 buf = (char *)malloc(len + 3);
101
102 snprintf(buf, len + 3,"\"%s\"", key);
103 len = strlen(buf);
104
105 while(*tab) {
106 if(strncmp(buf, tab, len) == 0) {
107 char c;
108
109 while((c = *(tab + len)) != ';') {
110 if(c == 0) {
111 len = -1;
112 goto out;
113 }
114 len++;
115 }
116 len++;
117 if(*(tab + len) == '\n') len++;
118 goto out;
119 }
120 tab++;
121 }
122 len = -1;
123out:
124 free(buf);
125
126 if(len == -1) return false;
127
128 while((*tab = *(tab + len))) {
129 tab++;
130 }
131
132 return true;
133}
134
135char *newStringFromList(char **list, int *size)
136{
137char *begin = *list, *end;
138char *newstr;
139int newsize = *size;
140int bufsize;
141
142while (*begin && newsize && isspace(*begin)) {
143begin++;
144newsize--;
145}
146end = begin;
147while (*end && newsize && !isspace(*end)) {
148end++;
149newsize--;
150}
151if (begin == end) {
152return 0;
153}
154bufsize = end - begin + 1;
155newstr = malloc(bufsize);
156if (!newstr)
157{
158return 0;
159}
160strlcpy(newstr, begin, bufsize);
161*list = end;
162*size = newsize;
163return newstr;
164}
165
166#endif
167
168/*
169 * compress == compress escaped characters to one character
170 */
171int stringLength(const char *table, int compress)
172{
173int ret = 0;
174
175while (*table) {
176if (*table == '\\') {
177table += 2;
178ret += 1 + (compress ? 0 : 1);
179}
180else
181{
182if (*table == '\"')
183{
184return ret;
185}
186ret++;
187table++;
188}
189}
190return ret;
191}
192
193
194bool getValueForConfigTableKey(config_file_t *config, const char *key, const char **val, int *size)
195{
196if (config->dictionary != 0 ) {
197// Look up key in XML dictionary
198TagPtr value;
199value = XMLGetProperty(config->dictionary, key);
200if (value != 0) {
201if (value->type != kTagTypeString) {
202error("Non-string tag '%s' found in config file\n", key);
203return false;
204}
205*val = value->string;
206*size = strlen(value->string);
207return true;
208}
209} else {
210
211// Legacy plist-style table
212
213}
214
215return false;
216}
217
218#if UNUSED
219
220/*
221 * Returns a new malloc'ed string if one is found
222 * in the string table matching 'key'. Also translates
223 * \n escapes in the string.
224 */
225char *newStringForStringTableKey(char *table, char *key, config_file_t *config)
226{
227const char *val;
228char *newstr, *p;
229int size;
230
231if (getValueForConfigTableKey(config, key, &val, &size))
232{
233newstr = (char *)malloc(size+1);
234if (!newstr)
235{
236return 0;
237}
238for (p = newstr; size; size--, p++, val++) {
239if ((*p = *val) == '\\') {
240switch (*++val) {
241case 'r':
242*p = '\r';
243break;
244case 'n':
245*p = '\n';
246break;
247case 't':
248*p = '\t';
249break;
250default:
251*p = *val;
252break;
253}
254size--;
255}
256}
257*p = '\0';
258return newstr;
259} else {
260return 0;
261}
262}
263
264#endif
265
266char *newStringForKey(char *key, config_file_t *config)
267{
268const char *val;
269char *newstr;
270int size;
271
272if (getValueForKey(key, &val, &size, config) && size) {
273newstr = (char *)malloc(size + 1);
274if (!newstr)
275{
276return 0;
277}
278strlcpy(newstr, val, size + 1);
279return newstr;
280} else {
281return 0;
282}
283}
284
285/* parse a command line
286 * in the form: [<argument> ...] [<option>=<value> ...]
287 * both <option> and <value> must be either composed of
288 * non-whitespace characters, or enclosed in quotes.
289 */
290static const char *getToken(const char *line, const char **begin, int *len)
291{
292if (*line == '\"') {
293*begin = ++line;
294while (*line && *line != '\"') {
295line++;
296}
297*len = line++ - *begin;
298} else {
299*begin = line;
300while (*line && !isspace(*line) && *line != '=') {
301line++;
302}
303*len = line - *begin;
304}
305return line;
306}
307
308bool getValueForBootKey(const char *line, const char *match, const char **matchval, int *len)
309{
310const char *key, *value;
311int key_len, value_len;
312bool retval = false;
313
314while (*line) {
315/* look for keyword or argument */
316while (isspace(*line))
317{
318line++;
319}
320
321/* now look for '=' or whitespace */
322line = getToken(line, &key, &key_len);
323/* line now points to '=' or space */
324if (*line && !isspace(*line)) {
325line = getToken(++line, &value, &value_len);
326} else {
327value = line;
328value_len = 0;
329}
330if ((strlen(match) == key_len) && strncmp(match, key, key_len) == 0)
331{
332// create a new string
333char* newstr = malloc(value_len + 1);
334strncpy(newstr, value, value_len);
335newstr[value_len] = 0;
336
337*matchval = newstr;
338*len = value_len;
339retval = true;
340/* Continue to look for this key; last one wins. */
341}
342}
343
344return retval;
345}
346
347/* Return NULL if no option has been successfully retrieved, or the string otherwise */
348const char *getStringForKey(const char *key, config_file_t *config)
349{
350static const char *value = 0;
351int len = 0;
352if (!getValueForKey(key, &value, &len, config))
353{
354value = 0;
355}
356return value;
357}
358
359
360/* Returns TRUE if a value was found, FALSE otherwise.
361 * The boolean value of the key is stored in 'val'.
362 */
363
364bool getBoolForKey(const char *key, bool *result_val, config_file_t *config)
365{
366 const char *key_val;
367 int size;
368
369if (getValueForKey(key, &key_val, &size, config))
370{
371if ((size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y'))
372{
373*result_val = true;
374}
375else
376{
377*result_val = false;
378}
379return true;
380}
381return false;
382}
383
384bool getIntForKey(const char *key, int *value, config_file_t *config)
385{
386const char *val;
387int size, sum;
388bool negative = false;
389
390if (getValueForKey(key, &val, &size, config))
391{
392if (size)
393{
394if (*val == '-')
395{
396negative = true;
397val++;
398size--;
399}
400
401for (sum = 0; size > 0; size--)
402{
403if (*val < '0' || *val > '9')
404{
405return false;
406}
407
408sum = (sum * 10) + (*val++ - '0');
409}
410
411if (negative)
412{
413sum = -sum;
414}
415*value = sum;
416return true;
417}
418}
419return false;
420}
421/*
422 *
423 */
424bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )
425{
426const char *val;
427
428int size = 0;
429int sum = 0;
430
431bool negative = false;
432bool percentage = false;
433
434if (getValueForKey(key, &val, &size, config))
435{
436if ( size )
437{
438if (*val == '-')
439{
440negative = true;
441val++;
442size--;
443}
444
445if (val[size-1] == '%')
446{
447percentage = true;
448size--;
449}
450
451// convert string to integer
452for (sum = 0; size > 0; size--)
453{
454if (*val < '0' || *val > '9')
455{
456return false;
457}
458
459sum = (sum * 10) + (*val++ - '0');
460}
461
462if (percentage)
463{
464sum = ( dimension_max * sum ) / 100;
465}
466
467// calculate offset from opposite origin
468if (negative)
469{
470sum = ( ( dimension_max - object_size ) - sum );
471}
472
473}
474else
475{
476
477// null value calculate center
478sum = ( dimension_max - object_size ) / 2;
479
480}
481
482*value = (uint16_t) sum;
483return true;
484}
485
486// key not found
487return false;
488}
489
490/*
491 *get color value from plist format #RRGGBB
492 */
493bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )
494{
495const char *val;
496int size;
497
498if (getValueForKey(key, &val, &size, config))
499{
500if (*val == '#') {
501val++;
502*value = strtol(val, NULL, 16);
503return true;
504}
505}
506return false;
507}
508
509bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )
510{
511const char *overrideVal;
512int overrideSize;
513bool override, ret;
514
515if (getValueForBootKey(bootArgs->CommandLine, key, val, size))
516{
517return true;
518}
519
520ret = getValueForConfigTableKey(config, key, val, size);
521
522// Try to find alternate keys in bootInfo->chameleonConfig (if config can be overriden)
523// and prefer its values with the exceptions for
524// "Kernel"="mach_kernel" and "Kernel Flags"="".
525
526if (config->canOverride)
527{
528if (getValueForConfigTableKey(&bootInfo->chameleonConfig, key, &overrideVal, &overrideSize))
529{
530override = true;
531
532// NOTE: Values are defined by apple as being in com.apple.Boot.plist
533// kHelperRootUUIDKey, kKernelArchKey, kMKextCacheKey, kKernelCacheKey, kKernelNameKey, kKernelFlagsKey
534if (ret && (strcmp(key, kKernelNameKey) == 0) && (overrideSize == 0))
535{
536override = false;
537}
538
539if (ret && (strcmp(key, kKernelFlagsKey) == 0) && (overrideSize == 0))
540{
541override = false;
542}
543
544if (override)
545{
546*val = overrideVal;
547*size = overrideSize;
548ret = true;
549}
550}
551}
552return ret;
553}
554
555#if UNUSED
556void printSystemConfig(char *p1)
557{
558char *p2 = p1, tmp;
559
560while (*p1 != '\0')
561{
562while (*p2 != '\0' && *p2 != '\n')
563{
564p2++;
565}
566tmp = *p2;
567*p2 = '\0';
568printf("%s\n", p1);
569*p2 = tmp;
570if (tmp == '\0')
571{
572break;
573}
574p1 = ++p2;
575 }
576}
577#endif
578
579//==========================================================================
580// ParseXMLFile
581// Modifies the input buffer.
582// Expects to see one dictionary in the XML file.
583// Puts the first dictionary it finds in the
584// tag pointer and returns 0, or returns -1 if not found
585// (and does not modify dict pointer).
586// Prints an error message if there is a parsing error.
587//
588int ParseXMLFile( char *buffer, TagPtr *dict )
589{
590long length, pos;
591TagPtr tag = 0;
592pos = 0;
593char *configBuffer;
594
595length = strlen(buffer) + 1;
596
597configBuffer = malloc(strlen(buffer)+1);
598if (!configBuffer)
599{
600return -1;
601}
602strlcpy(configBuffer, buffer, length );
603
604while (1)
605{
606length = XMLParseNextTag(configBuffer + pos, &tag);
607if (length == -1)
608{
609break;
610}
611
612pos += length;
613
614if (tag == 0)
615{
616continue;
617}
618if (tag->type == kTagTypeDict)
619{
620break;
621}
622
623XMLFreeTag(tag);
624}
625free(configBuffer);
626if (length < 0) {
627error ("Error parsing plist file\n");
628return -1;
629}
630*dict = tag;
631return 0;
632}
633
634/* loadConfigFile
635 *
636 * Returns 0 - successful.
637 * -1 - unsuccesful.
638 */
639int loadConfigFile(const char *configFile, config_file_t *config)
640{
641int fd, count;
642
643if ((fd = open_bvdev("bt(0,0)", configFile, 0)) < 0)
644{
645return -1;
646}
647// read file
648count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);
649close(fd);
650
651// build xml dictionary
652ParseXMLFile(config->plist, &config->dictionary);
653return 0;
654}
655
656/* loadSystemConfig
657 *
658 * Returns 0 - successful.
659 * -1 - unsuccesful.
660 */
661int loadSystemConfig(config_file_t *config)
662{
663char *dirspec[] = {
664"/com.apple.recovery.boot/com.apple.Boot.plist",// OS X Recovery
665"/OS X Install Data/com.apple.Boot.plist",// OS X Installer (10.8+)
666"/Mac OS X Install Data/com.apple.Boot.plist",// OS X Installer (Lion 10.7)
667//"/.IABootFiles/com.apple.Boot.plist",// OS X Installer
668"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"// com.apple.Boot.plist
669};
670
671int i, fd, count, ret=-1;
672
673for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
674{
675if ((fd = open(dirspec[i], 0)) >= 0)
676{
677// read file
678count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);
679close(fd);
680
681// build xml dictionary
682ParseXMLFile(config->plist, &config->dictionary);
683sysConfigValid = true;
684ret=0;
685
686break;
687}
688}
689
690if(ret == -1) ret = loadHelperConfig(config);
691
692// Always enable canOverride flag (for SystemConfig)
693config->canOverride = true;
694
695return ret;
696}
697
698/* loadChameleonConfig
699 *
700 * Returns 0 - successful.
701 * -1 - unsuccesful.
702 */
703int loadChameleonConfig(config_file_t *config, BVRef chain)
704{
705char *dirspec[] = {
706"/Extra/org.chameleon.Boot.plist",
707"/Extra/com.apple.Boot.plist", /* DEPRECIATED */
708};
709
710int i;
711
712for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
713{
714if ( loadChameleonConfigForDevice(config, "rd(0,0)", dirspec[i]) == 0 )
715{
716return 0;
717}
718
719if ( loadChameleonConfigForDevice(config, "", dirspec[i]) == 0 )
720{
721return 0;
722}
723
724if ( loadChameleonConfigForDevice(config, "bt(0,0)", dirspec[i]) == 0 )
725{
726return 0;
727}
728BVRef bvr;
729for ( bvr = chain; bvr; bvr = bvr->next ) /* C99 Error */
730{
731char device[256];
732getDeviceDescription(bvr, device);
733
734if ( loadChameleonConfigForDevice(config, device, dirspec[i]) == 0 )
735{
736return 0;
737}
738}
739}
740return -1;
741}
742
743/* loadChameleonConfigForDevice
744 *
745 * Returns 0 - successful.
746 * -1 - unsuccesful.
747 */
748int loadChameleonConfigForDevice(config_file_t *config, const char *device, const char *path)
749{
750char full_path[1024];
751int fd;
752
753snprintf(full_path, sizeof(full_path), "%s%s", device, path);
754
755if ((fd = open(full_path, 0)) >= 0)
756{
757// Check for depreciated file names and annoy the user about it.
758if(strstr(full_path, "com.apple.Boot.plist")) {
759printf("%s is depreciated.\n", full_path);
760full_path[strlen(full_path) - strlen("com.apple.Boot.plist")] = 0;
761printf("Please use the file %sorg.chameleon.Boot.plist instead.\n", full_path);
762pause();
763}
764// read file
765read(fd, config->plist, IO_CONFIG_DATA_SIZE);
766close(fd);
767
768// build xml dictionary
769ParseXMLFile(config->plist, &config->dictionary);
770sysConfigValid = true;
771return 0;
772}
773return -1;
774}
775
776/* loadHelperConfig
777 *
778 * Returns 0 - successful.
779 * -1 - unsuccesful.
780 */
781int loadHelperConfig(config_file_t *config)
782{
783int rfd, pfd, sfd, count, ret=-1, fixedsize;
784
785char *dirspec[] = {
786"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
787"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
788"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"
789};
790
791// This is a simple rock - paper scissors algo. R beats S, P beats R, S beats P
792// If all three, S is used for now. This should be changed to something else (say, timestamp?)
793
794pfd = open(dirspec[0], 0);
795if(pfd >= 0)// com.apple.boot.P exists
796{
797
798sfd = open(dirspec[2], 0); // com.apple.boot.S takes precidence if it also exists
799if(sfd >= 0)
800{
801// Use sfd
802fixedsize = MIN(file_size(sfd), IO_CONFIG_DATA_SIZE);
803count = read(sfd, config->plist, fixedsize);
804close(sfd);
805close(pfd);
806if (count != fixedsize) return -1;
807
808// build xml dictionary
809ParseXMLFile(config->plist, &config->dictionary);
810sysConfigValid = true;
811ret=0;
812
813}
814else
815{
816// used pfd
817fixedsize = MIN(file_size(pfd), IO_CONFIG_DATA_SIZE);
818count = read(pfd, config->plist, fixedsize);
819close(pfd);
820if (count != fixedsize) return -1;
821
822// build xml dictionary
823ParseXMLFile(config->plist, &config->dictionary);
824sysConfigValid = true;
825ret = 0;
826}
827
828}
829else
830{
831rfd = open(dirspec[1], 0); // com.apple.boot.R exists
832if(rfd >= 0)
833{
834pfd = open(dirspec[2], 0); // com.apple.boot.P takes recidence if it exists
835if(pfd >= 0)
836{
837// use sfd
838fixedsize = MIN(file_size(pfd), IO_CONFIG_DATA_SIZE);
839count = read(pfd, config->plist, fixedsize);
840close(pfd);
841close(rfd);
842if (count != fixedsize) return -1;
843
844// build xml dictionary
845ParseXMLFile(config->plist, &config->dictionary);
846sysConfigValid = true;
847ret = 0;
848
849}
850else
851{
852// use rfd
853 fixedsize = MIN(file_size(rfd), IO_CONFIG_DATA_SIZE);
854count = read(rfd, config->plist, fixedsize);
855close(rfd);
856if (count != fixedsize) return -1;
857
858// build xml dictionary
859ParseXMLFile(config->plist, &config->dictionary);
860sysConfigValid = true;
861ret = 0;
862
863}
864
865}
866else
867{
868sfd = open(dirspec[2], 0); // com.apple.boot.S exists, but nothing else does
869if(sfd >= 0)
870{
871// use sfd
872fixedsize = MIN(file_size(sfd), IO_CONFIG_DATA_SIZE);
873count = read(sfd, config->plist, fixedsize);
874close(sfd);
875if (count != fixedsize) return -1;
876
877// build xml dictionary
878ParseXMLFile(config->plist, &config->dictionary);
879sysConfigValid = true;
880ret = 0;
881
882}
883}
884
885}
886
887return ret;
888}
889
890char *newString(const char *oldString)
891{
892if ( oldString )
893{
894return strcpy(malloc(strlen(oldString)+1), oldString);
895}
896else
897{
898return NULL;
899}
900}
901
902/*
903 * Extracts the next argument from the command line, double quotes are allowed here.
904 */
905char *getNextArg(char **argPtr, char *val)
906{
907char * ptr = *argPtr;
908const char * strStart;
909int len = 0;
910bool isQuoted = false;
911
912*val = '\0';
913
914// Scan for the next non-whitespace character.
915while ( *ptr && (*ptr == ' ' || *ptr == '=') )
916{
917ptr++;
918}
919
920strStart = ptr;
921
922// Skip the leading double quote character.
923if (*ptr == '\"')
924{
925isQuoted = true;
926ptr++;
927strStart++;
928}
929
930// Scan for the argument terminator character.
931// This can be either a NULL character - in case we reach the end of the string,
932// a double quote in case of quoted argument,
933// or a whitespace character (' ' or '=') for non-quoted argument.
934while (*ptr && !( (isQuoted && (*ptr == '\"')) ||
935 (!isQuoted && (*ptr == ' ' || *ptr == '=')) )
936 ) {
937ptr++;
938}
939
940len = ptr - strStart;
941
942// Skip the closing double quote character and adjust
943// the starting pointer for the next getNextArg call.
944if (*ptr && isQuoted && *ptr == '\"') {
945ptr++;
946}
947
948// Copy the extracted argument to val.
949strncat(val, strStart, len);
950
951// Set command line pointer.
952*argPtr = ptr;
953
954return ptr;
955}
956

Archive Download this file

Revision: 2758