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/* Returns TRUE if a value was found, FALSE otherwise.
360 * The boolean value of the key is stored in 'val'.
361 */
362bool getBoolForKey(const char *key, bool *result_val, config_file_t *config)
363{
364const char *key_val;
365int size;
366
367// looking for real boolean (<true/> or <false/>)
368// if is a boolean tag, return immediately
369TagPtr entry = XMLGetProperty(config->dictionary,key);
370
371if(XMLIsBoolean(entry))
372{
373int value = XMLCastBoolean(entry);
374if (value) {
375*result_val = true;
376return true;
377}
378else
379{
380*result_val = false;
381return false;
382}
383}
384
385// check if is a boolean as "string" (Yes/No)
386// (IMHO this should be deprecated soon)
387if (getValueForKey(key, &key_val, &size, config))
388{
389if ((size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y'))
390{
391*result_val = true;
392}
393else
394{
395*result_val = false;
396}
397return true;
398}
399return false;
400}
401
402bool getIntForKey(const char *key, int *value, config_file_t *config)
403{
404const char *val;
405int size, sum;
406bool negative = false;
407
408if (getValueForKey(key, &val, &size, config))
409{
410if (size)
411{
412if (*val == '-')
413{
414negative = true;
415val++;
416size--;
417}
418
419for (sum = 0; size > 0; size--)
420{
421if (*val < '0' || *val > '9')
422{
423return false;
424}
425
426sum = (sum * 10) + (*val++ - '0');
427}
428
429if (negative)
430{
431sum = -sum;
432}
433*value = sum;
434return true;
435}
436}
437return false;
438}
439/*
440 *
441 */
442bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )
443{
444const char *val;
445
446int size = 0;
447int sum = 0;
448
449bool negative = false;
450bool percentage = false;
451
452if (getValueForKey(key, &val, &size, config))
453{
454if ( size )
455{
456if (*val == '-')
457{
458negative = true;
459val++;
460size--;
461}
462
463if (val[size-1] == '%')
464{
465percentage = true;
466size--;
467}
468
469// convert string to integer
470for (sum = 0; size > 0; size--)
471{
472if (*val < '0' || *val > '9')
473{
474return false;
475}
476
477sum = (sum * 10) + (*val++ - '0');
478}
479
480if (percentage)
481{
482sum = ( dimension_max * sum ) / 100;
483}
484
485// calculate offset from opposite origin
486if (negative)
487{
488sum = ( ( dimension_max - object_size ) - sum );
489}
490
491}
492else
493{
494
495// null value calculate center
496sum = ( dimension_max - object_size ) / 2;
497
498}
499
500*value = (uint16_t) sum;
501return true;
502}
503
504// key not found
505return false;
506}
507
508/*
509 *get color value from plist format #RRGGBB
510 */
511bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )
512{
513const char *val;
514int size;
515
516if (getValueForKey(key, &val, &size, config))
517{
518if (*val == '#') {
519val++;
520*value = strtol(val, NULL, 16);
521return true;
522}
523}
524return false;
525}
526
527bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )
528{
529const char *overrideVal;
530int overrideSize;
531bool override, ret;
532
533if (getValueForBootKey(bootArgs->CommandLine, key, val, size))
534{
535return true;
536}
537
538ret = getValueForConfigTableKey(config, key, val, size);
539
540// Try to find alternate keys in bootInfo->chameleonConfig (if config can be overriden)
541// and prefer its values with the exceptions for
542// "Kernel"="mach_kernel" and "Kernel Flags"="".
543
544if (config->canOverride)
545{
546if (getValueForConfigTableKey(&bootInfo->chameleonConfig, key, &overrideVal, &overrideSize))
547{
548override = true;
549
550// NOTE: Values are defined by apple as being in com.apple.Boot.plist
551// kHelperRootUUIDKey, kKernelArchKey, kMKextCacheKey, kKernelCacheKey, kKernelNameKey, kKernelFlagsKey
552if (ret && (strcmp(key, kKernelNameKey) == 0) && (overrideSize == 0))
553{
554override = false;
555}
556
557if (ret && (strcmp(key, kKernelFlagsKey) == 0) && (overrideSize == 0))
558{
559override = false;
560}
561
562if (override)
563{
564*val = overrideVal;
565*size = overrideSize;
566ret = true;
567}
568}
569}
570return ret;
571}
572
573#if UNUSED
574void printSystemConfig(char *p1)
575{
576char *p2 = p1, tmp;
577
578while (*p1 != '\0')
579{
580while (*p2 != '\0' && *p2 != '\n')
581{
582p2++;
583}
584tmp = *p2;
585*p2 = '\0';
586printf("%s\n", p1);
587*p2 = tmp;
588if (tmp == '\0')
589{
590break;
591}
592p1 = ++p2;
593 }
594}
595#endif
596
597//==========================================================================
598// ParseXMLFile
599// Modifies the input buffer.
600// Expects to see one dictionary in the XML file.
601// Puts the first dictionary it finds in the
602// tag pointer and returns 0, or returns -1 if not found
603// (and does not modify dict pointer).
604// Prints an error message if there is a parsing error.
605//
606int ParseXMLFile( char *buffer, TagPtr *dict )
607{
608long length, pos;
609TagPtr tag = 0;
610pos = 0;
611char *configBuffer;
612
613length = strlen(buffer) + 1;
614
615configBuffer = malloc(strlen(buffer)+1);
616if (!configBuffer)
617{
618return -1;
619}
620strlcpy(configBuffer, buffer, length );
621
622while (1)
623{
624length = XMLParseNextTag(configBuffer + pos, &tag);
625if (length == -1)
626{
627break;
628}
629
630pos += length;
631
632if (tag == 0)
633{
634continue;
635}
636if (tag->type == kTagTypeDict)
637{
638break;
639}
640
641XMLFreeTag(tag);
642}
643free(configBuffer);
644if (length < 0) {
645error ("Error parsing plist file\n");
646return -1;
647}
648*dict = tag;
649return 0;
650}
651
652/* loadConfigFile
653 *
654 * Returns 0 - successful.
655 * -1 - unsuccesful.
656 */
657int loadConfigFile (const char *configFile, config_file_t *config)
658{
659int fd, count;
660
661if ((fd = open_bvdev("bt(0,0)", configFile, 0)) < 0)
662{
663return -1;
664}
665// read file
666count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);
667close(fd);
668
669// build xml dictionary
670ParseXMLFile(config->plist, &config->dictionary);
671return 0;
672}
673
674/* loadSystemConfig
675 *
676 * Returns 0 - successful.
677 * -1 - unsuccesful.
678 */
679int loadSystemConfig(config_file_t *config)
680{
681// Micky1979, the order is important
682char *dirspec[] = {
683"/com.apple.recovery.boot/com.apple.Boot.plist",// OS X Recovery
684"/macOS Install Data/com.apple.Boot.plist",// macOS Upgrade (10.12)
685"/macOS Install Data/Locked Files/Boot Files/com.apple.Boot.plist",// macOS Upgrade (10.12)
686"/OS X Install Data/com.apple.Boot.plist",// OS X Upgrade (10.8+)
687"/Mac OS X Install Data/com.apple.Boot.plist",// OS X Upgrade (Lion 10.7)
688"/.IABootFiles/com.apple.Boot.plist",// OS X Installer
689"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"// (Installed System or Installer)
690};
691
692int i, fd, count, ret=-1;
693
694for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
695{
696if ((fd = open(dirspec[i], 0)) >= 0)
697{
698// read file
699count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);
700close(fd);
701
702// build xml dictionary
703ParseXMLFile(config->plist, &config->dictionary);
704sysConfigValid = true;
705ret=0;
706
707break;
708}
709}
710
711if(ret == -1) ret = loadHelperConfig(config);
712
713// Always enable canOverride flag (for SystemConfig)
714config->canOverride = true;
715
716return ret;
717}
718
719/* loadChameleonConfig
720 *
721 * Returns 0 - successful.
722 * -1 - unsuccesful.
723 */
724int loadChameleonConfig(config_file_t *config, BVRef chain)
725{
726char *dirspec[] = {
727"/Extra/org.chameleon.Boot.plist",
728"/Extra/com.apple.Boot.plist", /* DEPRECATED */
729};
730
731int i;
732
733for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)
734{
735if ( loadChameleonConfigForDevice(config, "rd(0,0)", dirspec[i]) == 0 )
736{
737return 0;
738}
739
740if ( loadChameleonConfigForDevice(config, "", dirspec[i]) == 0 )
741{
742return 0;
743}
744
745if ( loadChameleonConfigForDevice(config, "bt(0,0)", dirspec[i]) == 0 )
746{
747return 0;
748}
749BVRef bvr;
750for ( bvr = chain; bvr; bvr = bvr->next ) /* C99 Error */
751{
752char device[256];
753getDeviceDescription(bvr, device);
754
755if ( loadChameleonConfigForDevice(config, device, dirspec[i]) == 0 )
756{
757return 0;
758}
759}
760}
761return -1;
762}
763
764/* loadChameleonConfigForDevice
765 *
766 * Returns 0 - successful.
767 * -1 - unsuccesful.
768 */
769int loadChameleonConfigForDevice(config_file_t *config, const char *device, const char *path)
770{
771char full_path[1024];
772int fd;
773
774snprintf(full_path, sizeof(full_path), "%s%s", device, path);
775
776if ((fd = open(full_path, 0)) >= 0)
777{
778// Check for depreciated file names and annoy the user about it.
779if(strstr(full_path, "com.apple.Boot.plist")) {
780printf("%s is deprecated.\n", full_path);
781full_path[strlen(full_path) - strlen("com.apple.Boot.plist")] = 0;
782printf("Please use the file %sorg.chameleon.Boot.plist instead.\n", full_path);
783pause();
784}
785// read file
786read(fd, config->plist, IO_CONFIG_DATA_SIZE);
787close(fd);
788
789// build xml dictionary
790ParseXMLFile(config->plist, &config->dictionary);
791sysConfigValid = true;
792return 0;
793}
794return -1;
795}
796
797/* loadHelperConfig
798 *
799 * Returns 0 - successful.
800 * -1 - unsuccesful.
801 */
802int loadHelperConfig(config_file_t *config)
803{
804int rfd, pfd, sfd, count, ret=-1, fixedsize;
805
806char *dirspec[] = {
807"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
808"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",
809"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"
810};
811
812// This is a simple rock - paper scissors algo. R beats S, P beats R, S beats P
813// If all three, S is used for now. This should be changed to something else (say, timestamp?)
814
815pfd = open(dirspec[0], 0);
816if(pfd >= 0)// com.apple.boot.P exists
817{
818
819sfd = open(dirspec[2], 0); // com.apple.boot.S takes precidence if it also exists
820if(sfd >= 0)
821{
822// Use sfd
823fixedsize = MIN(file_size(sfd), IO_CONFIG_DATA_SIZE);
824count = read(sfd, config->plist, fixedsize);
825close(sfd);
826close(pfd);
827if (count != fixedsize) return -1;
828
829// build xml dictionary
830ParseXMLFile(config->plist, &config->dictionary);
831sysConfigValid = true;
832ret=0;
833
834}
835else
836{
837// used pfd
838fixedsize = MIN(file_size(pfd), IO_CONFIG_DATA_SIZE);
839count = read(pfd, config->plist, fixedsize);
840close(pfd);
841if (count != fixedsize) return -1;
842
843// build xml dictionary
844ParseXMLFile(config->plist, &config->dictionary);
845sysConfigValid = true;
846ret = 0;
847}
848
849}
850else
851{
852rfd = open(dirspec[1], 0); // com.apple.boot.R exists
853if(rfd >= 0)
854{
855pfd = open(dirspec[2], 0); // com.apple.boot.P takes recidence if it exists
856if(pfd >= 0)
857{
858// use sfd
859fixedsize = MIN(file_size(pfd), IO_CONFIG_DATA_SIZE);
860count = read(pfd, config->plist, fixedsize);
861close(pfd);
862close(rfd);
863if (count != fixedsize) return -1;
864
865// build xml dictionary
866ParseXMLFile(config->plist, &config->dictionary);
867sysConfigValid = true;
868ret = 0;
869
870}
871else
872{
873// use rfd
874fixedsize = MIN(file_size(rfd), IO_CONFIG_DATA_SIZE);
875count = read(rfd, config->plist, fixedsize);
876close(rfd);
877if (count != fixedsize) return -1;
878
879// build xml dictionary
880ParseXMLFile(config->plist, &config->dictionary);
881sysConfigValid = true;
882ret = 0;
883
884}
885
886}
887else
888{
889sfd = open(dirspec[2], 0); // com.apple.boot.S exists, but nothing else does
890if(sfd >= 0)
891{
892// use sfd
893fixedsize = MIN(file_size(sfd), IO_CONFIG_DATA_SIZE);
894count = read(sfd, config->plist, fixedsize);
895close(sfd);
896if (count != fixedsize) return -1;
897
898// build xml dictionary
899ParseXMLFile(config->plist, &config->dictionary);
900sysConfigValid = true;
901ret = 0;
902
903}
904}
905
906}
907
908return ret;
909}
910
911char *newString(const char *oldString)
912{
913if ( oldString )
914{
915return strcpy(malloc(strlen(oldString)+1), oldString);
916}
917else
918{
919return NULL;
920}
921}
922
923/*
924 * Extracts the next argument from the command line, double quotes are allowed here.
925 */
926char *getNextArg(char **argPtr, char *val)
927{
928char * ptr = *argPtr;
929const char * strStart;
930int len = 0;
931bool isQuoted = false;
932
933*val = '\0';
934
935// Scan for the next non-whitespace character.
936while ( *ptr && (*ptr == ' ' || *ptr == '=') )
937{
938ptr++;
939}
940
941strStart = ptr;
942
943// Skip the leading double quote character.
944if (*ptr == '\"')
945{
946isQuoted = true;
947ptr++;
948strStart++;
949}
950
951// Scan for the argument terminator character.
952// This can be either a NULL character - in case we reach the end of the string,
953// a double quote in case of quoted argument,
954// or a whitespace character (' ' or '=') for non-quoted argument.
955while (*ptr && !( (isQuoted && (*ptr == '\"')) ||
956 (!isQuoted && (*ptr == ' ' || *ptr == '=')) )
957 ) {
958ptr++;
959}
960
961len = ptr - strStart;
962
963// Skip the closing double quote character and adjust
964// the starting pointer for the next getNextArg call.
965if (*ptr && isQuoted && *ptr == '\"') {
966ptr++;
967}
968
969// Copy the extracted argument to val.
970strncat(val, strStart, len);
971
972// Set command line pointer.
973*argPtr = ptr;
974
975return ptr;
976}
977

Archive Download this file

Revision: 2881