From 7690bb8399a4cbfaa0a9c685fe6287ecc0a26339 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Thu, 3 Mar 2005 09:32:09 +0000 Subject: [PATCH] - Reworked the code that handles quoting for the wildcard expansion routine. We no longer allocate memory and then modify it, but call a function for each quoted parameter which does whatever is necessary. - The shell command parameter parser now considers the non-breaking space character (ISO code 160) to be a blank space character, too. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14851 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/changes | 8 +++ library/stdlib_headers.h | 6 +-- library/stdlib_protos.h | 6 +-- library/stdlib_startup.c | 91 +++++++++++++++++--------------- library/stdlib_wildcard_expand.c | 10 ++-- library/unistd_wildcard_expand.c | 56 +++++++++++++++----- 6 files changed, 106 insertions(+), 71 deletions(-) diff --git a/library/changes b/library/changes index b5a7b17..82544bf 100644 --- a/library/changes +++ b/library/changes @@ -83,6 +83,14 @@ - Added atoll(), ffs(), ftw(), nftw(), lstat() and uname() code contributed by Peter Bengtsson. Thank you very much! +- Reworked the code that handles quoting for the wildcard expansion + routine. We no longer allocate memory and then modify it, but + call a function for each quoted parameter which does whatever is + necessary. + +- The shell command parameter parser now considers the non-breaking + space character (ISO code 160) to be a blank space character, too. + c.lib 1.188 (7.2.2005) diff --git a/library/stdlib_headers.h b/library/stdlib_headers.h index 3ca7860..8f2cbbc 100644 --- a/library/stdlib_headers.h +++ b/library/stdlib_headers.h @@ -1,5 +1,5 @@ /* - * $Id: stdlib_headers.h,v 1.11 2005-02-28 10:07:32 obarthel Exp $ + * $Id: stdlib_headers.h,v 1.12 2005-03-03 09:32:08 obarthel Exp $ * * :ts=4 * @@ -253,10 +253,6 @@ extern int NOCOMMON __argc; /****************************************************************************/ -extern UBYTE * NOCOMMON __quote_vector; - -/****************************************************************************/ - extern int NOCOMMON __default_pool_size; extern int NOCOMMON __default_puddle_size; diff --git a/library/stdlib_protos.h b/library/stdlib_protos.h index 68d5130..d96a3da 100644 --- a/library/stdlib_protos.h +++ b/library/stdlib_protos.h @@ -1,5 +1,5 @@ /* - * $Id: stdlib_protos.h,v 1.9 2005-02-28 10:07:32 obarthel Exp $ + * $Id: stdlib_protos.h,v 1.10 2005-03-03 09:32:09 obarthel Exp $ * * :ts=4 * @@ -105,8 +105,8 @@ extern void * __get_sp(void); /****************************************************************************/ /* stdlib_wildcard_expand.c */ -extern UBYTE * __allocate_quote_vector(int num_parameters); -extern int __wildcard_expand_init(void); +extern int __wildcard_quote_parameter(unsigned int parameter); +extern int __wildcard_expand_init(void); /****************************************************************************/ diff --git a/library/stdlib_startup.c b/library/stdlib_startup.c index 4d7ac4d..49f71a9 100644 --- a/library/stdlib_startup.c +++ b/library/stdlib_startup.c @@ -1,5 +1,5 @@ /* - * $Id: stdlib_startup.c,v 1.7 2005-02-25 10:14:21 obarthel Exp $ + * $Id: stdlib_startup.c,v 1.8 2005-03-03 09:32:09 obarthel Exp $ * * :ts=4 * @@ -86,12 +86,26 @@ int __argc; /****************************************************************************/ -UBYTE * __quote_vector; +STATIC BOOL +is_space(unsigned char c) +{ + BOOL result; + + result = (BOOL)(c == '\t' || /* tab */ + c == '\r' || /* carriage return */ + c == '\n' || /* line feed */ + c == '\v' || /* vertical tab */ + c == '\f' || /* form feed */ + c == ' ' || /* blank space */ + c == '\240'); /* non-breaking space */ + + return(result); +} /****************************************************************************/ STATIC BOOL -is_escape_character(UBYTE c) +is_escape_character(unsigned char c) { BOOL result; @@ -103,11 +117,11 @@ is_escape_character(UBYTE c) /****************************************************************************/ STATIC BOOL -is_final_quote_character(const char * str) +is_final_quote_character(const unsigned char * str) { BOOL result; - result = (BOOL)(str[0] == '\"' && (str[1] == '\0' || isspace(str[1]))); + result = (BOOL)(str[0] == '\"' && (str[1] == '\0' || is_space(str[1]))); return(result); } @@ -122,32 +136,31 @@ __startup_init(void) /* Shell startup? */ if(__WBenchMsg == NULL) { - int number_of_arguments; - char * command_name; - int command_name_len; - char * arg_str; - int arg_len; - char * command_line; - char * str; + size_t number_of_arguments; + unsigned char * command_name; + size_t command_name_len; + unsigned char * arg_str; + size_t arg_len; + unsigned char * command_line; + unsigned char * str; /* Make a copy of the current command name string. */ - command_name_len = (int)((UBYTE *)BADDR(Cli()->cli_CommandName))[0]; + command_name_len = (size_t)((UBYTE *)BADDR(Cli()->cli_CommandName))[0]; __free_program_name = TRUE; - command_name = AllocVec((ULONG)(command_name_len+1),MEMF_ANY); + command_name = AllocVec((ULONG)(command_name_len + 1),MEMF_ANY); if(command_name == NULL) goto out; - if(CANNOT GetProgramName(command_name,command_name_len+1)) + if(CANNOT GetProgramName(command_name,command_name_len + 1)) goto out; __program_name = (char *)command_name; /* Get the shell parameter string and find out - * how long it is, stripping a trailing line - * feed and blank spaces if necessary. - */ + how long it is, stripping a trailing line + feed and blank spaces if necessary. */ arg_str = GetArgStr(); while((*arg_str) == ' ' || (*arg_str) == '\t') @@ -155,20 +168,19 @@ __startup_init(void) arg_len = strlen(arg_str); - while(arg_len > 0 && (arg_str[arg_len-1] == '\n' || arg_str[arg_len-1] == ' ' || arg_str[arg_len-1] == '\t')) + while(arg_len > 0 && (arg_str[arg_len - 1] == '\n' || arg_str[arg_len - 1] == ' ' || arg_str[arg_len - 1] == '\t')) arg_len--; /* Make a copy of the shell parameter string. */ - command_line = malloc((size_t)(arg_len+1)); + command_line = malloc(arg_len + 1); if(command_line == NULL) goto out; - memmove(command_line,arg_str,(size_t)arg_len); + memmove(command_line,arg_str,arg_len); command_line[arg_len] = '\0'; /* If we have a valid command line string and a command - * name, proceed to set up the argument vector. - */ + name, proceed to set up the argument vector. */ str = command_line; /* Count the number of arguments. */ @@ -177,7 +189,7 @@ __startup_init(void) while(TRUE) { /* Skip leading blank space. */ - while((*str) != '\0' && isspace(*str)) + while((*str) != '\0' && is_space(*str)) str++; if((*str) == '\0') @@ -214,7 +226,7 @@ __startup_init(void) else { /* Skip the parameter. */ - while((*str) != '\0' && NOT isspace(*str)) + while((*str) != '\0' && NOT is_space(*str)) str++; if((*str) == '\0') @@ -223,18 +235,12 @@ __startup_init(void) } /* Put all this together into an argument vector. - * We allocate one extra slot to put a NULL pointer - * into. - */ - __argv = (char **)malloc((number_of_arguments+1) * sizeof(*__argv)); + We allocate one extra slot to put a NULL pointer + into. */ + __argv = (char **)malloc((number_of_arguments + 1) * sizeof(*__argv)); if(__argv == NULL) goto out; - /* If necessary, allocate a bit vector to keep track of - * which parameters are quoted and which ones are not. - */ - __quote_vector = __allocate_quote_vector(number_of_arguments+1); - /* The first parameter is the program name. */ __argv[0] = command_name; @@ -245,7 +251,7 @@ __startup_init(void) while(TRUE) { /* Skip leading blank space. */ - while((*str) != '\0' && isspace(*str)) + while((*str) != '\0' && is_space(*str)) str++; if((*str) == '\0') @@ -257,8 +263,8 @@ __startup_init(void) char * arg; /* If necessary, indicate that this parameter was quoted. */ - if(__quote_vector != NULL) - __quote_vector[__argc / 8] |= 1 << (7 - (__argc & 7)); + if(__wildcard_quote_parameter(__argc) < 0) + goto out; str++; @@ -291,9 +297,7 @@ __startup_init(void) case '\0': - /* NOTE: the termination is handled - * down below. - */ + /* NOTE: the termination is handled down below. */ break; default: @@ -323,7 +327,7 @@ __startup_init(void) { __argv[__argc++] = str; - while((*str) != '\0' && NOT isspace(*str)) + while((*str) != '\0' && NOT is_space(*str)) str++; if((*str) == '\0') @@ -333,7 +337,7 @@ __startup_init(void) } } - assert( __argc == number_of_arguments ); + assert( __argc == (int)number_of_arguments ); assert( str <= &command_line[arg_len] ); __argv[__argc] = NULL; @@ -428,8 +432,7 @@ CLIB_DESTRUCTOR(__startup_exit) PROFILE_OFF(); /* Now clean up after the streams set up for the Workbench - * startup... - */ + startup... */ if(restore_console_task) { SetConsoleTask((struct MsgPort *)old_console_task); diff --git a/library/stdlib_wildcard_expand.c b/library/stdlib_wildcard_expand.c index 55ce63d..28b9ebe 100644 --- a/library/stdlib_wildcard_expand.c +++ b/library/stdlib_wildcard_expand.c @@ -1,5 +1,5 @@ /* - * $Id: stdlib_wildcard_expand.c,v 1.3 2005-01-02 09:07:19 obarthel Exp $ + * $Id: stdlib_wildcard_expand.c,v 1.4 2005-03-03 09:32:09 obarthel Exp $ * * :ts=4 * @@ -37,15 +37,15 @@ /****************************************************************************/ -UBYTE * WEAK __allocate_quote_vector(int num_parameters UNUSED); +int WEAK __wildcard_quote_parameter(unsigned int parameter UNUSED); int WEAK __wildcard_expand_init(void); /****************************************************************************/ -UBYTE * -__allocate_quote_vector(int UNUSED num_parameters) +int +__wildcard_quote_parameter(unsigned int parameter UNUSED) { - return(NULL); + return(0); } /****************************************************************************/ diff --git a/library/unistd_wildcard_expand.c b/library/unistd_wildcard_expand.c index 9aa4985..e642b06 100644 --- a/library/unistd_wildcard_expand.c +++ b/library/unistd_wildcard_expand.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_wildcard_expand.c,v 1.9 2005-02-26 09:05:54 obarthel Exp $ + * $Id: unistd_wildcard_expand.c,v 1.10 2005-03-03 09:32:09 obarthel Exp $ * * :ts=4 * @@ -57,21 +57,47 @@ /****************************************************************************/ -UBYTE * -__allocate_quote_vector(int num_parameters) +static size_t quote_vector_size; +static UBYTE * quote_vector; + +/****************************************************************************/ + +int +__wildcard_quote_parameter(unsigned int parameter) { - UBYTE * vector; size_t num_bytes; + int result = -1; - assert( num_parameters > 0 ); + /* Can we mark this parameter as quoted or do we need more + room in the buffer? */ + num_bytes = (parameter + 7) / 8; + if(num_bytes >= quote_vector_size) + { + size_t new_quote_vector_size = quote_vector_size + 8; + UBYTE * new_quote_vector; - num_bytes = (num_parameters + 7) / 8; + /* Allocate a larger buffer. */ + new_quote_vector = realloc(quote_vector,new_quote_vector_size); + if(new_quote_vector == NULL) + goto out; - vector = malloc(num_bytes); - if(vector != NULL) - memset(vector,0,num_bytes); + /* Set the bits in the new buffer portion to 0. */ + memset(&new_quote_vector[quote_vector_size],0,new_quote_vector_size - quote_vector_size); - return(vector); + quote_vector_size = new_quote_vector_size; + quote_vector = new_quote_vector; + } + + assert( quote_vector != NULL ); + assert( num_bytes < quote_vector_size ); + + quote_vector[parameter / 8] |= 1 << (7 - (parameter & 7)); + + result = 0; + + out: + + return(result); } /****************************************************************************/ @@ -146,7 +172,7 @@ __wildcard_expand_init(void) old_window_pointer = __set_process_window((APTR)-1); /* No work to be done? */ - if(__quote_vector == NULL || __argc == 0 || __argv == NULL) + if(quote_vector == NULL || __argc == 0 || __argv == NULL) { error = OK; goto out; @@ -204,7 +230,7 @@ __wildcard_expand_init(void) /* Does this string contain a wildcard pattern? We also check if the string is quoted. Quoted strings are never expanded. */ - if(i > 0 && (__quote_vector[i / 8] & (1 << (7 - (i & 7)))) == 0 && ParsePatternNoCase(argv[i],ap->ap_Buf,2 * MAXPATHLEN) > 0) + if(i > 0 && (quote_vector[i / 8] & (1 << (7 - (i & 7)))) == 0 && ParsePatternNoCase(argv[i],ap->ap_Buf,2 * MAXPATHLEN) > 0) { BOOL is_first = TRUE; LONG rc; @@ -411,8 +437,10 @@ __wildcard_expand_init(void) abort(); } - free(__quote_vector); - __quote_vector = NULL; + free(quote_vector); + quote_vector = NULL; + + quote_vector_size = 0; PROFILE_ON();