Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1468