From 14c44e2ec42a3c973e268add83a11e3e5fb0b80c Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Fri, 13 Mar 1998 17:02:23 +0000 Subject: Update. 1998-03-13 16:55 Ulrich Drepper * string/tester.c (test_strpbrk): Add more strpbrk tests. (test_strsep): Likewise. Correct horrible bugs. * string/bits/string2.h (strcspn): Optimize also reject string of length 2 and 3. (strspn): Likewise. (strpbrk): Likewise. (strsep): Likewise. Correct bug with successive separators and separators at the end of the string. * sysdeps/generic/strsep.c: Correct bug with successive separators and separators at the end of the string. 1998-03-13 13:11 Tim Waugh * posix/wordexp.c (parse_param): Positional parameters ($1, $2 etc) now handled, as well as $$ (pid). * posix/Makefile (tests): Execute wordexp-test.sh for `make check'. (distribute): Add wordexp-tst.sh. * posix/wordexp-tst.sh: New file. * posix/wordexp.c (parse_param): $# (or ${#}) expands to the number of positional parameters. Renamed substitute_length to seen_hash. Don't free(env) is env is NULL. 1998-03-13 16:50 Ulrich Drepper * libc.map: Add pthread_attr_init to GLIBC_2.1. 1998-03-13 15:01 Ulrich Drepper * gmon/gmon.c: Allow GMON_OUT_PREFIX variable to specify filename for output file replacing gmon.out. Patch by Dean Gaudet . 1998-03-12 Andreas Schwab * elf/dl-misc.c (_dl_debug_message): Fix printing of pid. Clean up namespace. Optimize finding end of line. 1998-03-12 Andreas Schwab * elf/rtld.c (process_envvars): Ignore LD_DEBUG_OUTPUT if running securely. Optimized. (process_dl_debug): Add ':' to list of separators. Optimized. 1998-03-13 10:25 Andreas Schwab --- posix/Makefile | 5 +- posix/wordexp-tst.sh | 71 +++++++++++++++++++++++++++ posix/wordexp.c | 135 +++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 178 insertions(+), 33 deletions(-) create mode 100755 posix/wordexp-tst.sh (limited to 'posix') diff --git a/posix/Makefile b/posix/Makefile index a9b78aa606..0fc1787968 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -29,7 +29,7 @@ headers := sys/utsname.h sys/times.h sys/wait.h sys/types.h unistd.h \ bits/sched.h re_comp.h wait.h bits/environments.h cpio.h distribute := confstr.h TESTS TESTS2C.sed testcases.h \ - globtest.c globtest.sh + globtest.c globtest.sh wordexp-tst.sh routines := \ uname \ @@ -64,8 +64,9 @@ before-compile := testcases.h include ../Rules ifeq (no,$(cross-compiling)) -tests: $(objpfx)globtest +tests: $(objpfx)globtest $(objpfx)wordexp-test $(SHELL) -e globtest.sh $(common-objpfx) $(elf-objpfx) $(rtld-installed-name) + $(SHELL) -e wordexp-tst.sh $(common-objpfx) $(elf-objpfx) $(rtld-installed-name) endif CFLAGS-regex.c = -Wno-unused -Wno-strict-prototypes diff --git a/posix/wordexp-tst.sh b/posix/wordexp-tst.sh new file mode 100755 index 0000000000..e341e6435d --- /dev/null +++ b/posix/wordexp-tst.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# Some of these tests may look a little weird. +# The first parameter to wordexp-test is what it gives to wordexp. +# The others are just there to be parameters. + +common_objpfx=$1; shift +elf_objpfx=$1; shift +rtld_installed_name=$1; shift + +: ${TMPDIR=/tmp} +testout=$TMPDIR/wordexp-test-result + +failed=0 +export IFS=$(echo -e " \t\n") + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '$*' > ${testout}1 +cat <<"EOF" | cmp - ${testout}1 || failed=1 +wordexp returned 0 +we_wordv[0] = "$*" +EOF + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '${*}' unquoted > ${testout}2 +cat <<"EOF" | cmp - ${testout}2 || failed=1 +wordexp returned 0 +we_wordv[0] = "${*}" +we_wordv[1] = "unquoted" +EOF + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '$@' unquoted > ${testout}3 +cat <<"EOF" | cmp - ${testout}3 || failed=1 +wordexp returned 0 +we_wordv[0] = "$@" +we_wordv[1] = "unquoted" +EOF + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '"$* quoted"' param > ${testout}4 +cat <<"EOF" | cmp - ${testout}4 || failed=1 +wordexp returned 0 +we_wordv[0] = ""$* quoted" param quoted" +EOF + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '"$@ quoted"' param > ${testout}5 +cat <<"EOF" | cmp - ${testout}5 || failed=1 +wordexp returned 0 +we_wordv[0] = ""$@ quoted"" +we_wordv[1] = "param quoted" +EOF +# Why? Because bash does it that way.. + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '$#' 2 3 4 5 > ${testout}6 +cat <<"EOF" | cmp - ${testout}6 || failed=1 +wordexp returned 0 +we_wordv[0] = "5" +EOF + +${elf_objpfx}${rtld_installed_name} --library-path ${common_objpfx} \ +${common_objpfx}posix/wordexp-test '$2 ${3}' 2nd 3rd > ${testout}6 +cat <<"EOF" | cmp - ${testout}6 || failed=1 +wordexp returned 0 +we_wordv[0] = "2nd" +we_wordv[1] = "3rd" +EOF + +exit $failed diff --git a/posix/wordexp.c b/posix/wordexp.c index a72a677101..0c89e02234 100644 --- a/posix/wordexp.c +++ b/posix/wordexp.c @@ -48,7 +48,8 @@ * This is a recursive-descent-style word expansion routine. */ -/* This variable is defined and initialized in the startup code. */ +/* These variables are defined and initialized in the startup code. */ +extern int __libc_argc; extern char **__libc_argv; /* Some forward declarations */ @@ -1016,9 +1017,8 @@ parse_param (char **word, size_t *word_length, size_t *max_length, enum remove_pattern_enum remove = RP_NONE; int colon_seen = 0; int depth = 0; - int substitute_length = 0; + int seen_hash = 0; int error; - int star = 0; for (; words[*offset]; ++(*offset)) { @@ -1066,7 +1066,14 @@ parse_param (char **word, size_t *word_length, size_t *max_length, goto envsubst; case '#': - /* '#' only has special meaning inside braces */ + /* '#' only has special meaning inside braces or as the very + * first character after $ */ + if (*offset == start) + { + seen_hash = 1; + goto envsubst; + } + if (words[start] != '{') { /* Evaluate */ @@ -1078,10 +1085,10 @@ parse_param (char **word, size_t *word_length, size_t *max_length, /* At the start? (i.e. 'string length') */ if (*offset == start + 1) { - substitute_length = 1; + seen_hash = 1; break; } - else if (substitute_length) + else if (seen_hash) goto syntax; /* Separating variable name from prefix pattern? */ @@ -1158,7 +1165,7 @@ parse_param (char **word, size_t *word_length, size_t *max_length, if (!env || !*env) goto syntax; - if (substitute_length) + if (seen_hash) goto syntax; if (action != '\0' || remove != RP_NONE) @@ -1206,22 +1213,29 @@ parse_param (char **word, size_t *word_length, size_t *max_length, break; } - - star = strchr ("*@", words[*offset]) != NULL; - if (isalnum (words[*offset]) || star) + else { - env = w_addchar (env, &env_length, &env_maxlen, words[*offset]); - if (env == NULL) - goto no_space; + int special = (strchr ("*@$", words[*offset]) != NULL + || isdigit (words[*offset])); - if (star) - goto envsubst; + if (isalpha (words[*offset]) || special) + { + env = w_addchar (env, &env_length, &env_maxlen, + words[*offset]); + if (env == NULL) + goto no_space; - break; - } + if (special && words[start] != '{') + goto envsubst; - --(*offset); - goto envsubst; + /* Keep going (get next char) */ + break; + } + + /* Stop and evaluate, remembering char we stopped at */ + --(*offset); + goto envsubst; + } } } @@ -1235,28 +1249,76 @@ envsubst: if (!env || !*env) { - *offset = start - 1; - *word = w_addchar (*word, word_length, max_length, '$'); - free (env); + if (seen_hash) + { + /* $# expands to the number of positional parameters */ + char buffer[21]; + buffer[20] = '\0'; + *word = w_addstr (*word, word_length, max_length, + _itoa_word (__libc_argc - 1, &buffer[20], 10, 0)); + } + else + { + /* Just $ on its own */ + *offset = start - 1; + *word = w_addchar (*word, word_length, max_length, '$'); + } + + if (env) + free (env); + return *word ? 0 : WRDE_NOSPACE; } - /* Is it `$*' or `$@' ? */ - if (strpbrk (env, "*@") != NULL) + /* Is it a special parameter? */ + if (strpbrk (env, "0123456789*@$")) { - size_t plist_len = 1; - int p; - - if (env[1] != '\0') + if (env[1]) { /* Bad substitution if there is more than one character */ + free (env); fprintf (stderr, "${%s}: bad substitution\n", env); return WRDE_SYNTAX; } - if (!quoted || *env == '*') + /* Is it a digit? */ + if (isdigit(*env)) + { + int n = *env - '0'; + char *param; + + free (env); + if (n >= __libc_argc) + /* Substitute NULL */ + return 0; + + /* Replace with the appropriate positional parameter */ + param = __strdup (__libc_argv[n]); + if (!param) + return WRDE_NOSPACE; + + *word = w_addstr (*word, word_length, max_length, param); + return *word ? 0 : WRDE_NOSPACE; + } + /* Is it `$$' ? */ + else if (*env == '$') + { + char pidstr[21]; + + free (env); + pidstr[20] = '\0'; + *word = w_addstr (*word, word_length, max_length, + _itoa_word (getpid(), &pidstr[20], 10, 0)); + return *word ? 0 : WRDE_NOSPACE; + } + /* Is it `$*' or `$@' (unquoted) ? */ + else if (*env == '*' || (*env == '@' && !quoted)) { + size_t plist_len = 1; + int p; + /* Build up value parameter by parameter (copy them) */ + free (env); for (p = 1; __libc_argv[p]; ++p) { char *old_pointer = value; @@ -1287,8 +1349,15 @@ envsubst: if (value) goto maybe_fieldsplit; + + return 0; } + /* Must be a quoted `$@' */ + assert (*env == '@'); + assert (quoted); + free (env); + /* Each parameter is a separate word ("$@") */ if (__libc_argv[0] == NULL) { @@ -1300,13 +1369,14 @@ envsubst: } else { + int p; + for (p = 1; __libc_argv[p + 1]; p++) { char *copy = __strdup (__libc_argv[p]); if (copy == NULL) return WRDE_NOSPACE; - strcpy (copy, __libc_argv[p]); error = w_addword (pwordexp, copy); if (error) { @@ -1319,6 +1389,9 @@ envsubst: if (__libc_argv[p]) { *word = __strdup (__libc_argv[p]); + if (*word == NULL) + return WRDE_NOSPACE; + *max_length = *word_length = strlen (*word); } } @@ -1584,7 +1657,7 @@ envsubst: return 0; } - if (substitute_length) + if (seen_hash) { char param_length[21]; param_length[20] = '\0'; -- cgit 1.4.1