/* * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights * Reserved. This file contains Original Code and/or Modifications of * Original Code as defined in and that are subject to the Apple Public * Source License Version 1.1 (the "License"). You may not use this file * except in compliance with the License. Please obtain a copy of the * License at http://www.apple.com/publicsource and read it before using * this file. * * The Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * Copyright 1993 NeXT, Inc. * All rights reserved. */ //#import "libsaio.h" //#import "bootstruct.h" //#import extern char *Language; extern char *LoadableFamilies; static void eatThru(char val, char **table_p); static __inline INTN isspace(char c) { return (c == ' ' || c == '\t'); } /* * Compare a string to a key with quoted characters */ static __inline int keyncmp(char *str, char *key, INTN n) { INTN c; while (n--) { c = *key++; if (c == '\\') { switch(c = *key++) { case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; default: break; } } else if (c == '\"') { /* Premature end of key */ return 1; } if (c != *str++) { return 1; } } return 0; } static void eatThru(char val, char **table_p) { register char *table = *table_p; register BOOL found = NO; while (*table && !found) { if (*table == '\\') table += 2; else { if (*table == val) found = YES; table++; } } *table_p = table; } char * newStringFromList( char **list, INTN* size ) { char *begin = *list, *end; char *newstr; INTN newsize = *size; while (*begin && newsize && isspace(*begin)) { begin++; newsize--; } end = begin; while (*end && newsize && !isspace(*end)) { end++; newsize--; } if (begin == end) return 0; newstr = malloc(end - begin + 1); strncpy(newstr, begin, end - begin); *list = end; *size = newsize; return newstr; } /* * compress == compress escaped characters to one character */ int stringLength(char *table, INTN compress) { int ret = 0; while (*table) { if (*table == '\\') { table += 2; ret += 1 + (compress ? 0 : 1); } else { if (*table == '\"') return ret; ret++; table++; } } return ret; } // looks in table for strings of format << "key" = "value"; >> // or << "key"; >> BOOL getValueForStringTableKey(char *table, char *key, char **val, INTN *size) { INTN keyLength; char *tableKey; do { eatThru('\"',&table); tableKey = table; keyLength = strlen(key); if (keyLength && (stringLength(table,1) == keyLength) && (keyncmp(key, table, keyLength) == 0)) { INTN c; /* found the key; now look for either * '=' or ';' */ while (c = *table) { ++table; if (c == '\\') { ++table; continue; } else if (c == '=' || c == ';') { break; } } if (c == ';') { table = tableKey; } else { eatThru('\"',&table); } *val = table; *size = stringLength(table,0); return YES; } eatThru(';',&table); } while (*table); return NO; } /* * Returns a new malloc'ed string if one is found * in the string table matching 'key'. Also translates * \n escapes in the string. */ char *newStringForStringTableKey( char *table, char *key ) { char *val, *newstr, *p; INTN size; if (getValueForStringTableKey(table, key, &val, &size)) { newstr = malloc(size+1); for (p = newstr; size; size--, p++, val++) { if ((*p = *val) == '\\') { switch (*++val) { case 'r': *p = '\r'; break; case 'n': *p = '\n'; break; case 't': *p = '\t'; break; default: *p = *val; break; } size--; } } *p = '\0'; return newstr; } else { return 0; } } char * newStringForKey(char *key) { char *val, *newstr; INTN size; if (getValueForKey(key, &val, &size) && size) { newstr = malloc(size + 1); strncpy(newstr, val, size); return newstr; } else { return 0; } } /* parse a command line * in the form: [ ...] [