- /* do shell variable substitution */
- if(*prog->argv[argc_l - 1] == '$') {
- if ((var = getenv(prog->argv[argc_l - 1] + 1))) {
- prog->argv[argc_l - 1] = var;
- }
-#ifdef BB_FEATURE_SH_ENVIRONMENT
- else {
- switch(*(prog->argv[argc_l - 1] + 1)) {
- case '?':
- prog->argv[argc_l - 1] = itoa(lastReturnCode);
- break;
- case '$':
- prog->argv[argc_l - 1] = itoa(getpid());
- break;
- case '#':
- prog->argv[argc_l - 1] = itoa(argc-1);
- break;
- case '!':
- if (lastBgPid==-1)
- *(prog->argv[argc_l - 1])='\0';
- else
- prog->argv[argc_l - 1] = itoa(lastBgPid);
- break;
- case '0':case '1':case '2':case '3':case '4':
- case '5':case '6':case '7':case '8':case '9':
- {
- int index=*(prog->argv[argc_l - 1] + 1)-48;
- if (index >= argc) {
- *(prog->argv[argc_l - 1])='\0';
- } else {
- prog->argv[argc_l - 1] = argv[index];
- }
- }
- break;
+
+ /* Find the end of any whitespace trailing behind
+ * the token and let that be part of the token */
+ while( string && string[*ix] && isspace(string[*ix]) ) {
+ (*ix)++;
+ }
+
+ if (! string && *ix==0) {
+ /* Nothing useful was found */
+ return NULL;
+ }
+
+ token = xmalloc(*ix+1);
+ token[*ix] = '\0';
+ strncpy(token, string, *ix);
+
+ return token;
+}
+
+static int expand_arguments(char *command)
+{
+ int total_length=0, length, i, retval, ix = 0;
+ expand_t expand_result;
+ char *tmpcmd, *cmd, *cmd_copy;
+ char *src, *dst, *var;
+ const char *out_of_space = "out of space during expansion";
+ int flags = GLOB_NOCHECK
+#ifdef GLOB_BRACE
+ | GLOB_BRACE
+#endif
+#ifdef GLOB_TILDE
+ | GLOB_TILDE
+#endif
+ ;
+
+ /* get rid of the terminating \n */
+ chomp(command);
+
+ /* Fix up escape sequences to be the Real Thing(tm) */
+ while( command && command[ix]) {
+ if (command[ix] == '\\') {
+ const char *tmp = command+ix+1;
+ command[ix] = process_escape_sequence( &tmp );
+ memmove(command+ix + 1, tmp, strlen(tmp)+1);
+ }
+ ix++;
+ }
+ /* Use glob and then fixup environment variables and such */
+
+ /* It turns out that glob is very stupid. We have to feed it one word at a
+ * time since it can't cope with a full string. Here we convert command
+ * (char*) into cmd (char**, one word per string) */
+
+ /* We need a clean copy, so strsep can mess up the copy while
+ * we write stuff into the original (in a minute) */
+ cmd = cmd_copy = strdup(command);
+ *command = '\0';
+ for (ix = 0, tmpcmd = cmd;
+ (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
+ if (*tmpcmd == '\0')
+ break;
+ /* we need to trim() the result for glob! */
+ trim(tmpcmd);
+ retval = glob(tmpcmd, flags, NULL, &expand_result);
+ free(tmpcmd); /* Free mem allocated by strsep_space */
+ if (retval == GLOB_NOSPACE) {
+ /* Mem may have been allocated... */
+ globfree (&expand_result);
+ error_msg(out_of_space);
+ return FALSE;
+ } else if (retval != 0) {
+ /* Some other error. GLOB_NOMATCH shouldn't
+ * happen because of the GLOB_NOCHECK flag in
+ * the glob call. */
+ error_msg("syntax error");
+ return FALSE;
+ } else {
+ /* Convert from char** (one word per string) to a simple char*,
+ * but don't overflow command which is BUFSIZ in length */
+ for (i=0; i < expand_result.gl_pathc; i++) {
+ length=strlen(expand_result.gl_pathv[i]);
+ if (total_length+length+1 >= BUFSIZ) {
+ error_msg(out_of_space);
+ return FALSE;
+ }
+ strcat(command+total_length, " ");
+ total_length+=1;
+ strcat(command+total_length, expand_result.gl_pathv[i]);
+ total_length+=length;