--- pgp/src/config.c 2018/04/24 16:42:25 1.1.1.6 +++ pgp/src/config.c 2018/04/24 16:43:44 1.1.1.7 @@ -1,27 +1,27 @@ /* config.c - config file parser by Peter Gutmann Parses config file for PGP - + (c) Copyright 1990-1994 by Philip Zimmermann. All rights reserved. The author assumes no liability for damages resulting from the use of this software, even if the damage results from defects in this software. No warranty is expressed or implied. - + Note that while most PGP source modules bear Philip Zimmermann's copyright notice, many of them have been revised or entirely written by contributors who frequently failed to put their names in their code. Code that has been incorporated into PGP from other authors was either originally published in the public domain or is used with permission from the various authors. - + PGP is available for free to the public under certain restrictions. See the PGP User's Guide (included in the release package) for important information about licensing, patent restrictions on certain algorithms, trademarks, copyrights, and export controls. - + Modified 24 Jun 92 - HAJK Misc fixes for VAX C restrictions. - -*/ + + */ #include #include @@ -36,7 +36,8 @@ static int lookup( char *key, int keyLength, char *keyWords[], int range ); static int extractToken( char *buffer, int *endIndex, int *length ); static int getaString( char *buffer, int *endIndex ); -static int getAssignment( char *buffer, int *endIndex, INPUT_TYPE settingType ); +static int getAssignment( char *buffer, int *endIndex, + INPUT_TYPE settingType ); static void processAssignment( int intrinsicIndex ); /* The external config variables we can set here are referenced in pgp.h */ @@ -52,13 +53,13 @@ enum { NO_ERROR, ILLEGAL_CHAR_ERROR, LIN #define CPM_EOF 0x1A /* ^Z = CPM EOF char */ -#define MAX_ERRORS 3 /* Max.no.errors before we give up */ +#define MAX_ERRORS 3 /* Max.no.errors before we give up */ #define LINEBUF_SIZE 100 /* Size of input buffer */ -static int line; /* The line on which an error occurred */ +static int line; /* The line on which an error occurred */ static int errCount; /* Total error count */ -static boolean hasError; /* Whether this line has an error in it */ +static boolean hasError; /* Whether this line has an error in it */ /* The settings parsed out by getAssignment() */ @@ -73,8 +74,8 @@ static char optstr[100]; /* option being - Leading spaces/tabs (whitespace) are ignored. - - Lines with a '#' as the first non-whitespace character are treated as - comment lines. + - Lines with a '#' as the first non-whitespace character are treated + as comment lines. - All other lines are treated as config options for the program. @@ -139,7 +140,6 @@ static char *settings[] = { "OFF", "ON" /* Search a list of keywords for a match */ - static int lookup( char *key, int keyLength, char *keyWords[], int range ) { int indx, pos = 0, matches = 0; @@ -160,12 +160,14 @@ static int lookup( char *key, int keyLen switch (matches) { case 0: - fprintf(stderr, "%s: unknown keyword: \"%s\"\n", errtag, optstr); + fprintf(stderr, "%s: unknown keyword: \"%s\"\n", + errtag, optstr); break; case 1: return pos; default: - fprintf(stderr, "%s: \"%s\" is ambiguous\n", errtag, optstr); + fprintf(stderr, "%s: \"%s\" is ambiguous\n", + errtag, optstr); } return ERROR; } @@ -177,12 +179,14 @@ static int extractToken( char *buffer, i char ch; /* Skip whitespace */ - for( ch = buffer[ indx ]; ch && ( ch == ' ' || ch == '\t' ); ch = buffer[ indx ] ) + for( ch = buffer[ indx ]; ch && ( ch == ' ' || ch == '\t' ); + ch = buffer[ indx ] ) indx++; tokenStart = indx; /* Find end of setting */ - while( indx < LINEBUF_SIZE && ( ch = buffer[ indx ] ) != '\0' && ch != ' ' && ch != '\t' ) + while( indx < LINEBUF_SIZE && ( ch = buffer[ indx ] ) != '\0' + && ch != ' ' && ch != '\t' ) indx++; *endIndex += indx; *length = indx - tokenStart; @@ -242,7 +246,9 @@ static int getaString( char *buffer, int /* Check for missing string terminator */ if( ch != '\"' && !noQuote ) { if (line) - fprintf(stderr, "%s: unterminated string in line %d\n", errtag, line ); + fprintf(stderr, + "%s: unterminated string in line %d\n", + errtag, line ); else fprintf(stderr, "unterminated string: '\"%s'\n", str ); hasError = TRUE; @@ -263,9 +269,13 @@ static int getAssignment( char *buffer, /* Check for an assignment operator */ if ( *buffer != '=' ) { if (line) - fprintf(stderr, "%s: expected '=' in line %d\n", errtag, line ); + fprintf(stderr, + "%s: expected '=' in line %d\n", + errtag, line ); else - fprintf(stderr, "%s: expected '=' after \"%s\"\n", errtag, optstr); + fprintf(stderr, + "%s: expected '=' after \"%s\"\n", + errtag, optstr); hasError = TRUE; errCount++; return ERROR; @@ -278,7 +288,9 @@ static int getAssignment( char *buffer, case BOOL: /* Check for known intrinsic - really more general than just checking for TRUE or FALSE */ - if( ( settingIndex = lookup( buffer, length, settings, NO_SETTINGS ) ) == ERROR ) + if( ( settingIndex = lookup( buffer, + length, settings, + NO_SETTINGS ) ) == ERROR ) { hasError = TRUE; errCount++; @@ -304,7 +316,6 @@ static int getAssignment( char *buffer, } /* Process an assignment */ - static void processAssignment( int intrinsicIndex ) { if( !hasError ) @@ -415,7 +426,7 @@ static void processAssignment( int intri case FORCE: force_flag = flag; break; case PKCS_COMPAT: pkcs_compat = value; break; - /* Undocumented flag to test contractually-mandated time bomb */ + /* Undocumented flag to test contractually-mandated time bomb */ case LEGAL_KLUDGE: legal_kludge = value; break; @@ -443,7 +454,6 @@ static void processAssignment( int intri /* Process an option on a line by itself. This expects options which are taken from the command-line, and is less finicky about errors than the config-file version */ - int processConfigLine( char *option ) { int indx, intrinsicIndex; @@ -458,7 +468,9 @@ int processConfigLine( char *option ) indx < LINEBUF_SIZE && ( ch = option[ indx ] ) != '\0' && ch != ' ' && ch != '\t' && ch != '='; indx++ ); - if( ( intrinsicIndex = lookup( ( char * ) option, indx, intrinsics, NO_INTRINSICS ) ) == ERROR ) + if( ( intrinsicIndex = lookup( ( char * ) option, + indx, intrinsics, + NO_INTRINSICS ) ) == ERROR ) return -1; if (option[indx] == '\0' && intrinsicType[intrinsicIndex] == BOOL) { @@ -467,7 +479,9 @@ int processConfigLine( char *option ) processAssignment(intrinsicIndex); } else /* Get the value to set to, either as a string, a numeric value, or a boolean flag */ - if (getAssignment( ( char * ) option + indx, &indx, intrinsicType[ intrinsicIndex ] ) != ERROR) + if (getAssignment( ( char * ) option + indx, + &indx, intrinsicType[ intrinsicIndex ] ) + != ERROR) processAssignment( intrinsicIndex ); return errCount ? -1 : 0; } @@ -486,14 +500,16 @@ int processConfigFile( char *configFileN errtag = "config.txt"; if( ( configFilePtr = fopen( configFileName, FOPRTXT ) ) == NULL ) { - fprintf(stderr, "Cannot open configuration file %s\n", configFileName ); + fprintf(stderr, "Cannot open configuration file %s\n", + configFileName ); return OK; /* treat like empty config file */ } /* Process each line in the configFile */ while( ch != EOF ) { /* Skip whitespace */ - while( ( ( ch = getc( configFilePtr ) ) == ' ' || ch == '\t' ) && ch != EOF ) + while( ( ( ch = getc( configFilePtr ) ) == ' ' || ch == '\t' ) + && ch != EOF ) ; /* Get a line into the inBuffer */ @@ -503,18 +519,22 @@ int processConfigFile( char *configFileN while( ch != '\r' && ch != '\n' && ch != CPM_EOF && ch != EOF ) { /* Check for an illegal char in the data */ - if( ( ch < ' ' || ch > '~' ) && ch != '\r' && ch != '\n' && - ch != ' ' && ch != '\t' && ch != CPM_EOF && ch != EOF ) + if( ( ch < ' ' || ch > '~' ) && + ch != '\r' && ch != '\n' && + ch != ' ' && ch != '\t' && ch != CPM_EOF + && ch != EOF ) { if( errType == NO_ERROR ) - /* Save position of first illegal char */ + /* Save position of first illegal char */ errPos = lineBufCount; errType = ILLEGAL_CHAR_ERROR; } - /* Make sure the path is of the correct length. Note that the - code is ordered so that a LINELENGTH_ERROR takes precedence over - an ILLEGAL_CHAR_ERROR */ + /* Make sure the path is of the correct + length. Note that the code is ordered + so that a LINELENGTH_ERROR takes + precedence over an ILLEGAL_CHAR_ERROR */ + if( lineBufCount > LINEBUF_SIZE ) errType = LINELENGTH_ERROR; else @@ -522,16 +542,20 @@ int processConfigFile( char *configFileN if( ( ch = getc( configFilePtr ) ) == '#' ) { - /* Skip comment section and trailing whitespace */ - while( ch != '\r' && ch != '\n' && ch != CPM_EOF && ch != EOF ) - ch = getc( configFilePtr ); + /* Skip comment section and trailing + whitespace */ + while( ch != '\r' && ch != '\n' + && ch != CPM_EOF && ch != EOF ) + ch = getc( configFilePtr ); break; } } /* Skip trailing whitespace and add der terminador */ - while(lineBufCount && (( theCh = inBuffer[ lineBufCount - 1 ] ) == ' ' || theCh == '\t' )) - lineBufCount--; + while(lineBufCount && + (( theCh = inBuffer[ lineBufCount - 1 ] ) + == ' ' || theCh == '\t' )) + lineBufCount--; inBuffer[ lineBufCount ] = '\0'; @@ -541,36 +565,46 @@ int processConfigFile( char *configFileN switch( errType ) { case LINELENGTH_ERROR: - fprintf(stderr, "%s: line '%.30s...' too long\n", errtag, inBuffer ); + fprintf(stderr, + "%s: line '%.30s...' too long\n", + errtag, inBuffer ); errCount++; break; case ILLEGAL_CHAR_ERROR: fprintf(stderr, "> %s\n ", inBuffer ); fprintf(stderr, "%*s^\n", errPos, ""); - fprintf(stderr, "%s: bad character in command on line %d\n", errtag, line ); + fprintf(stderr, + "%s: bad character in command on line %d\n", + errtag, line ); errCount++; break; default: for( indx = 0; - indx < LINEBUF_SIZE && ( ch = inBuffer[ indx ] ) != '\0' - && ch != ' ' && ch != '\t' && ch != '='; - indx++ ) - ; - if( ( intrinsicIndex = lookup( inBuffer, indx, intrinsics, CONFIG_INTRINSICS ) ) == ERROR ) + indx < LINEBUF_SIZE && + ( ch = inBuffer[ indx ] ) != '\0' + && ch != ' ' && ch != '\t' && ch != '='; + indx++ ); + + if( ( intrinsicIndex = + lookup( inBuffer, indx, intrinsics, + CONFIG_INTRINSICS ) ) == ERROR ) { errCount++; } else { - /* Get the value to set to, either as a string, a - numeric value, or a boolean flag */ - getAssignment( inBuffer + indx, &indx, intrinsicType[ intrinsicIndex ] ); - processAssignment( intrinsicIndex ); + /* Get the value to set to, either as a + string, a numeric value, or a boolean + flag */ + getAssignment(inBuffer + indx, + &indx, + intrinsicType[intrinsicIndex]); + processAssignment( intrinsicIndex ); } } } - /* Handle special-case of ^Z if configFile came off an MSDOS system */ + /* Handle special-case of ^Z if configFile came off an MSDOS system */ if( ch == CPM_EOF ) ch = EOF; @@ -585,8 +619,10 @@ int processConfigFile( char *configFileN /* Exit if there were errors */ if( errCount ) { - fprintf(stderr, "%s: %s%d error(s) detected\n\n", configFileName, ( errCount >= MAX_ERRORS ) ? - "Maximum level of " : "", errCount ); + fprintf(stderr, + "%s: %s%d error(s) detected\n\n", + configFileName, ( errCount >= MAX_ERRORS ) ? + "Maximum level of " : "", errCount ); return ERROR; }