2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 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 1.1 (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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1993 NeXT, Inc.
26 * All rights reserved.
29 #include "bootstruct.h"
31 #include "stringConstants.h"
32 #include "legacy/configTablePrivate.h"
35 extern char *Language
;
36 extern char *LoadableFamilies
;
38 static TagPtr gConfigDict
;
40 static void eatThru(char val
, const char **table_p
);
42 static inline int isspace(char c
)
44 return (c
== ' ' || c
== '\t');
48 * Compare a string to a key with quoted characters
51 keyncmp(const char *str
, const char *key
, int n
)
70 } else if (c
== '\"') {
71 /* Premature end of key */
81 static void eatThru(char val
, const char **table_p
)
83 register const char *table
= *table_p
;
84 register BOOL found
= NO
;
86 while (*table
&& !found
)
88 if (*table
== '\\') table
+= 2;
91 if (*table
== val
) found
= YES
;
98 /* Remove key and its associated value from the table. */
101 removeKeyFromTable(const char *key
, char *table
)
109 buf
= (char *)malloc(len
+ 3);
111 sprintf(buf
, "\"%s\"", key
);
115 if(strncmp(buf
, tab
, len
) == 0) {
118 while((c
= *(tab
+ len
)) != ';') {
126 if(*(tab
+ len
) == '\n') len
++;
135 if(len
== -1) return NO
;
137 while((*tab
= *(tab
+ len
))) {
150 char *begin
= *list
, *end
;
155 while (*begin
&& newsize
&& isspace(*begin
)) {
160 while (*end
&& newsize
&& !isspace(*end
)) {
166 bufsize
= end
- begin
+ 1;
167 newstr
= malloc(bufsize
);
168 strlcpy(newstr
, begin
, bufsize
);
175 * compress == compress escaped characters to one character
177 int stringLength(const char *table
, int compress
)
186 ret
+= 1 + (compress
? 0 : 1);
190 if (*table
== '\"') return ret
;
198 BOOL
getValueForConfigTableKey(const char *table
, const char *key
, const char **val
, int *size
)
201 const char *tableKey
;
203 if (gConfigDict
!= 0 ) {
204 /* Look up key in XML dictionary */
206 value
= XMLGetProperty(gConfigDict
, key
);
208 if (value
->type
!= kTagTypeString
) {
209 error("Non-string tag '%s' found in config file\n",
213 *val
= value
->string
;
214 *size
= strlen(value
->string
);
218 /* Legacy plist-style table */
221 eatThru('\"',&table
);
223 keyLength
= strlen(key
);
225 (stringLength(table
,1) == keyLength
) &&
226 (keyncmp(key
, table
, keyLength
) == 0))
230 /* found the key; now look for either
238 } else if (c
== '=' || c
== ';') {
245 eatThru('\"',&table
);
248 *size
= stringLength(table
,0);
261 * Returns a new malloc'ed string if one is found
262 * in the string table matching 'key'. Also translates
263 * \n escapes in the string.
265 char *newStringForStringTableKey(
274 if (getValueForConfigTableKey(table
, key
, &val
, &size
)) {
275 newstr
= (char *)malloc(size
+1);
276 for (p
= newstr
; size
; size
--, p
++, val
++) {
277 if ((*p
= *val
) == '\\') {
303 newStringForKey(char *key
)
309 if (getValueForKey(key
, &val
, &size
) && size
) {
310 newstr
= (char *)malloc(size
+ 1);
311 strlcpy(newstr
, val
, size
+ 1);
318 /* parse a command line
319 * in the form: [<argument> ...] [<option>=<value> ...]
320 * both <option> and <value> must be either composed of
321 * non-whitespace characters, or enclosed in quotes.
324 static const char *getToken(const char *line
, const char **begin
, int *len
)
328 while (*line
&& *line
!= '\"')
330 *len
= line
++ - *begin
;
333 while (*line
&& !isspace(*line
) && *line
!= '=')
335 *len
= line
- *begin
;
340 BOOL
getValueForBootKey(const char *line
, const char *match
, const char **matchval
, int *len
)
342 const char *key
, *value
;
343 int key_len
, value_len
;
346 /* look for keyword or argument */
347 while (isspace(*line
)) line
++;
349 /* now look for '=' or whitespace */
350 line
= getToken(line
, &key
, &key_len
);
351 /* line now points to '=' or space */
352 if (*line
&& !isspace(*line
)) {
353 line
= getToken(++line
, &value
, &value_len
);
358 if ((strlen(match
) == key_len
)
359 && strncmp(match
, key
, key_len
) == 0) {
375 if (getValueForKey(key
, &val
, &size
) && (size
>= 1) &&
376 val
[0] == 'Y' || val
[0] == 'y')
389 if (getValueForKey(key
, &val
, &size
)) {
390 for (sum
= 0; size
> 0; size
--) {
391 sum
= (sum
* 10) + (*val
++ - '0');
405 if (getValueForBootKey(bootArgs
->bootString
, key
, val
, size
))
407 else if (getValueForConfigTableKey(bootArgs
->config
, key
, val
, size
))
415 #define TABLE_EXPAND_SIZE 192
418 * Returns 0 if file loaded OK,
419 * -1 if file was not loaded
420 * Does not print error messages.
421 * Returns pointer to table in memory in *table.
422 * Allocates an extra number of bytes for table expansion.
425 loadConfigFile(const char *configFile
, const char **table
, BOOL allocTable
)
427 char *configPtr
= bootArgs
->configEnd
;
430 /* Read config file into memory */
431 if ((fd
= open(configFile
, 0)) >= 0)
434 configPtr
= malloc(file_size(fd
)+2+TABLE_EXPAND_SIZE
);
436 if ((configPtr
- bootArgs
->config
) > CONFIG_SIZE
) {
437 error("No room in memory for config files\n");
441 verbose("Reading configuration file '%s'.\n",configFile
);
443 if (table
) *table
= configPtr
;
444 count
= read(fd
, configPtr
, IO_CONFIG_DATA_SIZE
);
451 bootArgs
->configEnd
= configPtr
;
459 /* Returns 0 if requested config files were loaded,
460 * 1 if default files were loaded,
461 * -1 if no files were loaded.
462 * Prints error message if files cannot be loaded.
467 const char *bundleName
, // bundle directory name (e.g. "System")
468 BOOL useDefault
, // use Default.table instead of instance tables
469 const char **table
, // returns pointer to config table
470 BOOL allocTable
// malloc the table and return in *table
475 const char *device_dir
= usrDevices();
480 // load up to 99 instance tables
485 for (i
=0; i
< max
; i
++) {
486 sprintf(buf
, "%s/%s.config/Instance%d.table",
489 if (useDefault
|| (loadConfigFile(buf
, table
, allocTable
) != 0)) {
491 // couldn't load first instance table;
492 // try the default table
493 sprintf(buf
, "%s/%s.config/%s",
496 IO_DEFAULT_TABLE_FILENAME
);
497 if (loadConfigFile(buf
, table
, allocTable
) == 0) {
501 error("Config file \"%s\" not found\n", buf
);
502 sleep(1); // let the message be seen!
516 #define USR_SYSTEM_CONFIG \
517 USR_DEVICES "/System.config"
518 #define USR_SYSTEM_DEFAULT_FILE \
519 USR_SYSTEM_CONFIG "/Default.table"
520 #define ARCH_SYSTEM_CONFIG \
521 ARCH_DEVICES "/System.config"
522 #define ARCH_SYSTEM_DEFAULT_FILE \
523 ARCH_SYSTEM_CONFIG "/Default.table"
524 #define SYSTEM_CONFIG "System"
528 #define SYSTEM_CONFIG_DIR "/Library/Preferences/SystemConfiguration"
529 #define SYSTEM_CONFIG_FILE "/com.apple.Boot.plist"
530 #define SYSTEM_CONFIG_PATH SYSTEM_CONFIG_DIR SYSTEM_CONFIG_FILE
531 #define CONFIG_EXT ".plist"
535 printSystemConfig(void)
537 char *p1
= bootArgs
->config
;
540 while (*p1
!= '\0') {
541 while (*p2
!= '\0' && *p2
!= '\n') p2
++;
546 if (tmp
== '\0') break;
552 static int sysconfig_dev
;
554 //==========================================================================
556 // Modifies the input buffer.
557 // Expects to see one dictionary in the XML file.
558 // Puts the first dictionary it finds in the
559 // tag pointer and returns 0, or returns -1 if not found
560 // (and does not modify dict pointer).
563 ParseXMLFile( char * buffer
, TagPtr
* dict
)
570 configBuffer
= malloc(strlen(buffer
)+1);
571 strcpy(configBuffer
, buffer
);
575 length
= XMLParseNextTag(configBuffer
+ pos
, &tag
);
576 if (length
== -1) break;
580 if (tag
== 0) continue;
581 if (tag
->type
== kTagTypeDict
) break;
593 /* Returns 0 if requested config files were loaded,
594 * 1 if default files were loaded,
595 * -1 if no files were loaded.
596 * Prints error message if files cannot be loaded.
606 int ret
, len
, doDefault
=0;
608 buf
= bp
= malloc(512);
610 for(cp
= which
, len
= size
; len
&& *cp
&& *cp
!= LP
; cp
++, len
--) ;
612 while (len
-- && *cp
&& *cp
++ != RP
) ;
613 /* cp now points past device */
614 strlcpy(buf
,which
,cp
- which
);
621 strcpy(bp
, systemConfigDir());
623 strncat(bp
, cp
, len
);
624 if (strncmp(cp
+ len
- strlen(CONFIG_EXT
),
625 CONFIG_EXT
, strlen(CONFIG_EXT
)) != 0)
626 strcat(bp
, CONFIG_EXT
);
628 strlcpy(bp
, cp
, len
);
630 if ((strcmp(bp
, SYSTEM_CONFIG_PATH
) == 0)) {
633 ret
= loadConfigFile(bp
= buf
, 0, 0);
635 strcpy(bp
, systemConfigDir());
636 strcat(bp
, SYSTEM_CONFIG_FILE
);
637 ret
= loadConfigFile(bp
, 0, 0);
639 ret
= loadConfigDir((bp
= SYSTEM_CONFIG
), 0, 0, 0);
642 sysconfig_dev
= currentdev();
644 error("System config file '%s' not found\n", bp
);
648 // Check for XML file;
649 // if not XML, gConfigDict will remain 0.
650 ParseXMLFile(bootArgs
->config
, &gConfigDict
);
653 return (ret
< 0 ? ret
: doDefault
);
657 char * newString(const char * oldString
)
660 return strcpy(malloc(strlen(oldString
)+1), oldString
);