1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00

- 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
This commit is contained in:
Olaf Barthel
2005-03-03 09:32:09 +00:00
parent 2aba208878
commit 7690bb8399
6 changed files with 106 additions and 71 deletions

View File

@ -83,6 +83,14 @@
- Added atoll(), ffs(), ftw(), nftw(), lstat() and uname() code - Added atoll(), ffs(), ftw(), nftw(), lstat() and uname() code
contributed by Peter Bengtsson. Thank you very much! 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) c.lib 1.188 (7.2.2005)

View File

@ -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 * :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_pool_size;
extern int NOCOMMON __default_puddle_size; extern int NOCOMMON __default_puddle_size;

View File

@ -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 * :ts=4
* *
@ -105,8 +105,8 @@ extern void * __get_sp(void);
/****************************************************************************/ /****************************************************************************/
/* stdlib_wildcard_expand.c */ /* stdlib_wildcard_expand.c */
extern UBYTE * __allocate_quote_vector(int num_parameters); extern int __wildcard_quote_parameter(unsigned int parameter);
extern int __wildcard_expand_init(void); extern int __wildcard_expand_init(void);
/****************************************************************************/ /****************************************************************************/

View File

@ -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 * :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 STATIC BOOL
is_escape_character(UBYTE c) is_escape_character(unsigned char c)
{ {
BOOL result; BOOL result;
@ -103,11 +117,11 @@ is_escape_character(UBYTE c)
/****************************************************************************/ /****************************************************************************/
STATIC BOOL STATIC BOOL
is_final_quote_character(const char * str) is_final_quote_character(const unsigned char * str)
{ {
BOOL result; 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); return(result);
} }
@ -122,32 +136,31 @@ __startup_init(void)
/* Shell startup? */ /* Shell startup? */
if(__WBenchMsg == NULL) if(__WBenchMsg == NULL)
{ {
int number_of_arguments; size_t number_of_arguments;
char * command_name; unsigned char * command_name;
int command_name_len; size_t command_name_len;
char * arg_str; unsigned char * arg_str;
int arg_len; size_t arg_len;
char * command_line; unsigned char * command_line;
char * str; unsigned char * str;
/* Make a copy of the current command name string. */ /* 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; __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) if(command_name == NULL)
goto out; goto out;
if(CANNOT GetProgramName(command_name,command_name_len+1)) if(CANNOT GetProgramName(command_name,command_name_len + 1))
goto out; goto out;
__program_name = (char *)command_name; __program_name = (char *)command_name;
/* Get the shell parameter string and find out /* Get the shell parameter string and find out
* how long it is, stripping a trailing line how long it is, stripping a trailing line
* feed and blank spaces if necessary. feed and blank spaces if necessary. */
*/
arg_str = GetArgStr(); arg_str = GetArgStr();
while((*arg_str) == ' ' || (*arg_str) == '\t') while((*arg_str) == ' ' || (*arg_str) == '\t')
@ -155,20 +168,19 @@ __startup_init(void)
arg_len = strlen(arg_str); 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--; arg_len--;
/* Make a copy of the shell parameter string. */ /* 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) if(command_line == NULL)
goto out; goto out;
memmove(command_line,arg_str,(size_t)arg_len); memmove(command_line,arg_str,arg_len);
command_line[arg_len] = '\0'; command_line[arg_len] = '\0';
/* If we have a valid command line string and a command /* 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; str = command_line;
/* Count the number of arguments. */ /* Count the number of arguments. */
@ -177,7 +189,7 @@ __startup_init(void)
while(TRUE) while(TRUE)
{ {
/* Skip leading blank space. */ /* Skip leading blank space. */
while((*str) != '\0' && isspace(*str)) while((*str) != '\0' && is_space(*str))
str++; str++;
if((*str) == '\0') if((*str) == '\0')
@ -214,7 +226,7 @@ __startup_init(void)
else else
{ {
/* Skip the parameter. */ /* Skip the parameter. */
while((*str) != '\0' && NOT isspace(*str)) while((*str) != '\0' && NOT is_space(*str))
str++; str++;
if((*str) == '\0') if((*str) == '\0')
@ -223,18 +235,12 @@ __startup_init(void)
} }
/* Put all this together into an argument vector. /* Put all this together into an argument vector.
* We allocate one extra slot to put a NULL pointer We allocate one extra slot to put a NULL pointer
* into. into. */
*/ __argv = (char **)malloc((number_of_arguments + 1) * sizeof(*__argv));
__argv = (char **)malloc((number_of_arguments+1) * sizeof(*__argv));
if(__argv == NULL) if(__argv == NULL)
goto out; 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. */ /* The first parameter is the program name. */
__argv[0] = command_name; __argv[0] = command_name;
@ -245,7 +251,7 @@ __startup_init(void)
while(TRUE) while(TRUE)
{ {
/* Skip leading blank space. */ /* Skip leading blank space. */
while((*str) != '\0' && isspace(*str)) while((*str) != '\0' && is_space(*str))
str++; str++;
if((*str) == '\0') if((*str) == '\0')
@ -257,8 +263,8 @@ __startup_init(void)
char * arg; char * arg;
/* If necessary, indicate that this parameter was quoted. */ /* If necessary, indicate that this parameter was quoted. */
if(__quote_vector != NULL) if(__wildcard_quote_parameter(__argc) < 0)
__quote_vector[__argc / 8] |= 1 << (7 - (__argc & 7)); goto out;
str++; str++;
@ -291,9 +297,7 @@ __startup_init(void)
case '\0': case '\0':
/* NOTE: the termination is handled /* NOTE: the termination is handled down below. */
* down below.
*/
break; break;
default: default:
@ -323,7 +327,7 @@ __startup_init(void)
{ {
__argv[__argc++] = str; __argv[__argc++] = str;
while((*str) != '\0' && NOT isspace(*str)) while((*str) != '\0' && NOT is_space(*str))
str++; str++;
if((*str) == '\0') 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] ); assert( str <= &command_line[arg_len] );
__argv[__argc] = NULL; __argv[__argc] = NULL;
@ -428,8 +432,7 @@ CLIB_DESTRUCTOR(__startup_exit)
PROFILE_OFF(); PROFILE_OFF();
/* Now clean up after the streams set up for the Workbench /* Now clean up after the streams set up for the Workbench
* startup... startup... */
*/
if(restore_console_task) if(restore_console_task)
{ {
SetConsoleTask((struct MsgPort *)old_console_task); SetConsoleTask((struct MsgPort *)old_console_task);

View File

@ -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 * :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); int WEAK __wildcard_expand_init(void);
/****************************************************************************/ /****************************************************************************/
UBYTE * int
__allocate_quote_vector(int UNUSED num_parameters) __wildcard_quote_parameter(unsigned int parameter UNUSED)
{ {
return(NULL); return(0);
} }
/****************************************************************************/ /****************************************************************************/

View File

@ -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 * :ts=4
* *
@ -57,21 +57,47 @@
/****************************************************************************/ /****************************************************************************/
UBYTE * static size_t quote_vector_size;
__allocate_quote_vector(int num_parameters) static UBYTE * quote_vector;
/****************************************************************************/
int
__wildcard_quote_parameter(unsigned int parameter)
{ {
UBYTE * vector;
size_t num_bytes; 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); /* Set the bits in the new buffer portion to 0. */
if(vector != NULL) memset(&new_quote_vector[quote_vector_size],0,new_quote_vector_size - quote_vector_size);
memset(vector,0,num_bytes);
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); old_window_pointer = __set_process_window((APTR)-1);
/* No work to be done? */ /* No work to be done? */
if(__quote_vector == NULL || __argc == 0 || __argv == NULL) if(quote_vector == NULL || __argc == 0 || __argv == NULL)
{ {
error = OK; error = OK;
goto out; goto out;
@ -204,7 +230,7 @@ __wildcard_expand_init(void)
/* Does this string contain a wildcard pattern? We also check if the /* Does this string contain a wildcard pattern? We also check if the
string is quoted. Quoted strings are never expanded. */ 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; BOOL is_first = TRUE;
LONG rc; LONG rc;
@ -411,8 +437,10 @@ __wildcard_expand_init(void)
abort(); abort();
} }
free(__quote_vector); free(quote_vector);
__quote_vector = NULL; quote_vector = NULL;
quote_vector_size = 0;
PROFILE_ON(); PROFILE_ON();