From 4f025234ab41a881988978b2928f4f762e116349 Mon Sep 17 00:00:00 2001 From: CaS Date: Wed, 26 Oct 2005 13:13:29 +0000 Subject: [PATCH] Settle on shell style config file git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@21876 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 + Source/NSPathUtilities.m | 245 ++------------------------------------- 2 files changed, 13 insertions(+), 237 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8552707b5..18c6915a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-10-26 Richard Frith-Macdonald + + * Source/NSPathUtilities.m: Drop 'make' style config file parsing + and setting on 'sh' style (so we can source the config file). + 2005-10-25 Richard Frith-Macdonald * Source/NSPathUtilities.m: Try to ensure that user root and diff --git a/Source/NSPathUtilities.m b/Source/NSPathUtilities.m index 2192a4068..952b6c090 100644 --- a/Source/NSPathUtilities.m +++ b/Source/NSPathUtilities.m @@ -584,21 +584,21 @@ static void ShutdownPathUtilities(void) Win32Finalise(); } -#if 0 - /** * Reads a file and expects it to be in basic unix "conf" style format with * one key = value per line (the format a unix shell can 'source' in order * to define shell variables).
- * Attempts to mimic the escape sequence and quoting conventions of standard - * shells, so that a config file sourced by the make package will produce - * the same results as one parsed by this function.
- * The value may be any quoted string (or an unquoted string containing no + * This attempts to mimic the escape sequence and quoting conventions of + * the standard bourne shell, so that a config file sourced by the make + * package will produce the same results as one parsed by this function.
+ * Keys, by convention, consiste of uppercase letters, digits, + * and underscores, and must not begin with a digit.
+ * A value may be any quoted string (or an unquoted string containing no * white space).
* Lines beginning with a hash '#' are deemed comment lines and ignored.
* The backslash character may be used as an escape character anywhere * in the file except within a singly quoted string - * (where it is taken literally).<
+ * (where it is taken literally).
* A backslash followed immediately by a newline (except in a singly * quoted string) is removed completely along with the newline ... it * thus serves to join lines so that they are treated as a single line.
@@ -827,7 +827,7 @@ ParseConfigurationFile(NSString *fileName, NSMutableDictionary *dict) continue; // escaped newline is removed. } } - if (*spos <= ' ' || *spos == '=') + if (isspace(*spos) || *spos == '=') { break; } @@ -870,235 +870,6 @@ ParseConfigurationFile(NSString *fileName, NSMutableDictionary *dict) return YES; } -#else - -/* - * Parse config file in gnumake format ... different quoting conventions
- * - *

Empty lines (or lines containing only spaces) are ignored. - *

- *

A '#' introduces a comment that extends up to the end of the line. - * Anything from the '#' up to the end of the line is ignored (unless the '#' - * if found in a variable value, and escaped by a preceding '\' as explained - * below). - *

- *

A variable (which must be one in the allowed set listed in the - * documentation) is defined using a line of the form:
- * name = value - *

- *

name must be from the predefined list (where names in the list only - * contains uppercase letters and '_'). - *

- *

The '=' might (or might not) be preceded / followed by spaces that are - * ignored. - *

- *

value is read literally until the end of the line with the following - * exceptions:
- * to insert a '$' character you must insert '$$'. - * A single '$' can never appear in the value.
- * a '#' would start a comment that ends at the end of the line. - * To insert a literal '#' character you must insert '\#'.
- * to insert a '\' character at the end of the line or before the '#' - * initiating a comment, you need to insert '\\#'.
- * value might be empty to mean ''.
- * Spaces at the end or at the beginning of value are stripped. - *

- */ -static BOOL -ParseConfigurationFile(NSString *fileName, NSMutableDictionary *dict) -{ - NSDictionary *attributes; - NSString *file; - unsigned l; - unichar *src; - unichar *dst; - unichar *end; - unichar *spos; - unichar *dpos; - unichar *tmp; - - if ([MGR() isReadableFileAtPath: fileName] == NO) - { - return NO; - } - - attributes = [MGR() fileAttributesAtPath: fileName traverseLink: YES]; - if (([attributes filePosixPermissions] & (0022 & ATTRMASK)) != 0) - { -#if defined(__WIN32__) - fprintf(stderr, "The file '%S' is writable by someone other than" - " its owner (permissions 0%lo).\nIgnoring it.\n", - (const unichar*)[fileName fileSystemRepresentation], - [attributes filePosixPermissions]); -#else - fprintf(stderr, "The file '%s' is writable by someone other than" - " its owner (permissions 0%lo).\nIgnoring it.\n", - [fileName fileSystemRepresentation], - [attributes filePosixPermissions]); -#endif - return NO; - } - - if (dict == nil) - { - [NSException raise: NSInvalidArgumentException - format: @"No destination dictionary supplied"]; - } - - file = [NSString stringWithContentsOfFile: fileName]; - l = [file length]; - src = (unichar*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(unichar) * l); - spos = src; - end = src + l; - dst = (unichar*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(unichar) * l); - dpos = dst; - [file getCharacters: src]; - - while (spos < end) - { - NSString *key = nil; - NSString *val = nil; - - /* - * Step past any whitespace ... including blank lines - */ - while (spos < end) - { - if (*spos == '#') - { - // Got comment ... skip to end of line - while (++spos < end) - { - if (*spos == '\r' || *spos == '\n') - { - break; - } - } - } - if (*spos == '\\') - { - spos++; - if (spos >= end) - { - break; // At end of file ... odd but not fatal - } - } - if (*spos > ' ') - { - break; // OK ... found a non space character. - } - spos++; - } - if (spos == end) - { - return YES; // Completed parsing. - } - - dpos = dst; - while (spos < end && *spos > ' ' && *spos != '=') - { - if (*spos == '#') - { - key = [NSString stringWithCharacters: dst length: dpos - dst]; - fprintf(stderr, "Unexpected '#' after '%s' in config file '%s'\n", - [key UTF8String], [fileName UTF8String]); - goto end_of_line; - } - *dpos++ = *spos++; - } - key = [NSString stringWithCharacters: dst length: dpos - dst]; - for (tmp = dst; tmp < dpos; tmp++) - { - if (*tmp != '_' && (*tmp < 'A' || *tmp > 'Z')) - { - fprintf(stderr, "Bad variable name '%s' in config file '%s'\n", - [key UTF8String], [fileName UTF8String]); - goto end_of_line; - } - } - - while (spos < end && *spos <= ' ') - { - spos++; - } - if (spos >= end || *spos != '=') - { - fprintf(stderr, "Expected '=' after '%s' in config file '%s'\n", - [key UTF8String], [fileName UTF8String]); - goto end_of_line; - } - spos++; // Step past '=' - - /* - * Skip any space, but *not* beyond the end of line. - */ - while (spos < end && *spos <= ' ') - { - if (*spos == '\r' || *spos == '\n') - { - break; - } - spos++; - } - - /* - * Capture value. - */ - dpos = dst; - while (spos < end && *spos != '\r' && *spos != '\n') - { - if (*spos == '\\' && spos < end - 1) - { - if (spos[1] == '#' || spos[1] == '\\') - { - spos++; // Escape hash or backslash - } - else if (spos[1] == '\r' || spos[1] == '\n') - { - spos += 2; - continue; // Join lines. - } - } - else if (*spos == '#') - { - // Comment ... skip to end of line - while (spos < end && *spos != '\r' && *spos != '\n') - { - spos++; - } - break; - } - else if (*spos == '$') - { - spos++; - if (spos >= end || *spos != '$') - { - fprintf(stderr, "Unexpected '$' in value after '%s'" - " in config file '%s'\n", - [key UTF8String], [fileName UTF8String]); - goto end_of_line; - } - } - *dpos++ = *spos++; - } - val = [NSString stringWithCharacters: dst length: dpos - dst]; - - [dict setObject: val forKey: key]; - -end_of_line: - while (spos < end && *spos != '\r' && *spos != '\n') - { - spos++; - } - } - NSZoneFree(NSDefaultMallocZone(), src); - NSZoneFree(NSDefaultMallocZone(), dst); - - return YES; -} - -#endif - /* See NSPathUtilities.h for description */ void