|
|
|
@ -1404,25 +1404,26 @@ shell_expand(char *s) |
|
|
|
|
register unsigned long j, k, l = 0; |
|
|
|
|
char new[CONFIG_BUFF]; |
|
|
|
|
unsigned char eval_escape = 1, eval_var = 1, eval_exec = 1, eval_func = 1, in_single = 0, in_double = 0; |
|
|
|
|
unsigned long fsize; |
|
|
|
|
unsigned long fsize, cnt1 = 0, cnt2 = 0; |
|
|
|
|
const unsigned long max = CONFIG_BUFF - 1; |
|
|
|
|
char *Command, *Output, *EnvVar, *OutFile; |
|
|
|
|
FILE *fp; |
|
|
|
|
|
|
|
|
|
ASSERT(s != NULL); |
|
|
|
|
if (!s) |
|
|
|
|
return ((char *) NULL); |
|
|
|
|
ASSERT_RVAL(s != NULL, (char *) NULL); |
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
new = (char *) MALLOC(CONFIG_BUFF); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
for (j = 0; *pbuff && j < CONFIG_BUFF; pbuff++, j++) { |
|
|
|
|
for (j = 0; *pbuff && j < max; pbuff++, j++) { |
|
|
|
|
switch (*pbuff) { |
|
|
|
|
case '~': |
|
|
|
|
D_OPTIONS(("Tilde detected.\n")); |
|
|
|
|
if (eval_var) { |
|
|
|
|
strncpy(new + j, getenv("HOME"), CONFIG_BUFF - j); |
|
|
|
|
j += strlen(getenv("HOME")) - 1; |
|
|
|
|
strncpy(new + j, getenv("HOME"), max - j); |
|
|
|
|
cnt1 = strlen(getenv("HOME")) - 1; |
|
|
|
|
cnt2 = max - j - 1; |
|
|
|
|
j += MIN(cnt1, cnt2); |
|
|
|
|
} else { |
|
|
|
|
new[j] = *pbuff; |
|
|
|
|
} |
|
|
|
@ -1506,8 +1507,9 @@ shell_expand(char *s) |
|
|
|
|
FREE(Command); |
|
|
|
|
if (Output && *Output) { |
|
|
|
|
l = strlen(Output) - 1; |
|
|
|
|
strncpy(new + j, Output, CONFIG_BUFF - j); |
|
|
|
|
j += l; |
|
|
|
|
strncpy(new + j, Output, max - j); |
|
|
|
|
cnt2 = max - j - 1; |
|
|
|
|
j += MIN(l, cnt2); |
|
|
|
|
FREE(Output); |
|
|
|
|
} else { |
|
|
|
|
j--; |
|
|
|
@ -1521,7 +1523,7 @@ shell_expand(char *s) |
|
|
|
|
if (eval_exec) { |
|
|
|
|
Command = (char *) MALLOC(CONFIG_BUFF); |
|
|
|
|
l = 0; |
|
|
|
|
for (pbuff++; *pbuff && *pbuff != '`' && l < CONFIG_BUFF; pbuff++, l++) { |
|
|
|
|
for (pbuff++; *pbuff && *pbuff != '`' && l < max; pbuff++, l++) { |
|
|
|
|
switch (*pbuff) { |
|
|
|
|
case '$': |
|
|
|
|
D_OPTIONS(("Environment variable detected. Evaluating.\n")); |
|
|
|
@ -1542,8 +1544,10 @@ shell_expand(char *s) |
|
|
|
|
} |
|
|
|
|
EnvVar[k] = 0; |
|
|
|
|
if ((tmp = getenv(EnvVar))) { |
|
|
|
|
strncpy(Command + l, tmp, CONFIG_BUFF - l); |
|
|
|
|
l = strlen(Command) - 1; |
|
|
|
|
strncpy(Command + l, tmp, max - l); |
|
|
|
|
cnt1 = strlen(tmp) - 1; |
|
|
|
|
cnt2 = max - l - 1; |
|
|
|
|
l += MIN(cnt1, cnt2); |
|
|
|
|
} |
|
|
|
|
pbuff--; |
|
|
|
|
break; |
|
|
|
@ -1551,10 +1555,11 @@ shell_expand(char *s) |
|
|
|
|
Command[l] = *pbuff; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ASSERT(l < CONFIG_BUFF); |
|
|
|
|
Command[l] = 0; |
|
|
|
|
OutFile = tmpnam(NULL); |
|
|
|
|
if (l + strlen(OutFile) + 8 > CONFIG_BUFF) { |
|
|
|
|
print_error("parse error in file %s, line %lu: Cannot execute command, line too long", |
|
|
|
|
print_error("Parse error in file %s, line %lu: Cannot execute command, line too long", |
|
|
|
|
file_peek_path(), file_peek_line()); |
|
|
|
|
return ((char *) NULL); |
|
|
|
|
} |
|
|
|
@ -1569,18 +1574,20 @@ shell_expand(char *s) |
|
|
|
|
Output = (char *) MALLOC(fsize + 1); |
|
|
|
|
fread(Output, fsize, 1, fp); |
|
|
|
|
Output[fsize] = 0; |
|
|
|
|
D_PARSE(("Command returned \"%s\"\n", Output)); |
|
|
|
|
D_ENL(("Command returned \"%s\". Output length is %lu, j = %lu, max - j == %lu\n", Output, fsize, j, max - j)); |
|
|
|
|
fclose(fp); |
|
|
|
|
remove(OutFile); |
|
|
|
|
Output = CondenseWhitespace(Output); |
|
|
|
|
strncpy(new + j, Output, CONFIG_BUFF - j); |
|
|
|
|
j += strlen(Output) - 1; |
|
|
|
|
strncpy(new + j, Output, max - j); |
|
|
|
|
cnt1 = strlen(Output) - 1; |
|
|
|
|
cnt2 = max - j - 1; |
|
|
|
|
j += MIN(cnt1, cnt2); |
|
|
|
|
FREE(Output); |
|
|
|
|
} else { |
|
|
|
|
print_warning("Command at line %lu of file %s returned no output.", file_peek_line(), file_peek_path()); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
print_warning("Output file %s could not be created. (line %lu of file %s)", (OutFile ? OutFile : "(null)"), |
|
|
|
|
print_warning("Output file %s could not be created. (line %lu of file %s)", NONULL(OutFile), |
|
|
|
|
file_peek_line(), file_peek_path()); |
|
|
|
|
} |
|
|
|
|
FREE(Command); |
|
|
|
@ -1588,7 +1595,7 @@ shell_expand(char *s) |
|
|
|
|
new[j] = *pbuff; |
|
|
|
|
} |
|
|
|
|
#else |
|
|
|
|
print_error("warning: backquote execution support not compiled in, ignoring"); |
|
|
|
|
print_warning("Backquote execution support not compiled in, ignoring"); |
|
|
|
|
new[j] = *pbuff; |
|
|
|
|
#endif |
|
|
|
|
break; |
|
|
|
@ -1612,8 +1619,10 @@ shell_expand(char *s) |
|
|
|
|
} |
|
|
|
|
EnvVar[k] = 0; |
|
|
|
|
if ((tmp = getenv(EnvVar))) { |
|
|
|
|
strncpy(new, tmp, CONFIG_BUFF - j); |
|
|
|
|
j += strlen(tmp) - 1; |
|
|
|
|
strncpy(new, tmp, max - j); |
|
|
|
|
cnt1 = strlen(tmp) - 1; |
|
|
|
|
cnt2 = max - j - 1; |
|
|
|
|
j += MIN(cnt1, cnt2); |
|
|
|
|
} |
|
|
|
|
pbuff--; |
|
|
|
|
} else { |
|
|
|
@ -1654,9 +1663,11 @@ shell_expand(char *s) |
|
|
|
|
new[j] = *pbuff; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ASSERT(j < CONFIG_BUFF); |
|
|
|
|
new[j] = 0; |
|
|
|
|
|
|
|
|
|
D_PARSE(("shell_expand(%s) returning \"%s\"\n", s, new)); |
|
|
|
|
D_PARSE((" strlen(s) == %lu, strlen(new) == %lu, j == %lu\n", strlen(s), strlen(new), j)); |
|
|
|
|
|
|
|
|
|
strcpy(s, new); |
|
|
|
|
#if 0 |
|
|
|
|