Allow sh to execute files

This commit is contained in:
K. Lange 2018-07-17 19:44:43 +09:00
parent 0fa8cc4143
commit eb680cd947

View File

@ -389,7 +389,7 @@ void run_cmd(char ** args) {
exit(i); exit(i);
} }
int shell_exec(char * buffer) { int shell_exec(char * buffer, size_t size, FILE * file) {
/* Read previous history entries */ /* Read previous history entries */
if (buffer[0] == '!') { if (buffer[0] == '!') {
@ -545,6 +545,11 @@ int shell_exec(char * buffer) {
goto _new_arg; goto _new_arg;
} }
goto _just_add; goto _just_add;
case '#':
if (!quoted && !backtick && !collected) {
goto _done; /* Support comments; must not be part of an existing arg */
}
goto _just_add;
default: default:
if (backtick) { if (backtick) {
buffer_[collected] = '\\'; buffer_[collected] = '\\';
@ -575,11 +580,14 @@ _next:
_done: _done:
if (quoted) { if (quoted) {
if (shell_interactive) { if (shell_interactive == 1) {
draw_prompt_c(); draw_prompt_c();
read_entry_continued(buffer); read_entry_continued(buffer);
rline_history_append_line(buffer); rline_history_append_line(buffer);
continue; continue;
} else if (shell_interactive == 2) {
fgets(buffer, size, file);
continue;
} else { } else {
fprintf(stderr, "Syntax error: Unterminated quoted string.\n"); fprintf(stderr, "Syntax error: Unterminated quoted string.\n");
return 127; return 127;
@ -896,7 +904,7 @@ int main(int argc, char ** argv) {
switch (c) { switch (c) {
case 'c': case 'c':
shell_interactive = 0; shell_interactive = 0;
return shell_exec(optarg); return shell_exec(optarg, strlen(optarg), NULL);
case 'v': case 'v':
show_version(); show_version();
return 0; return 0;
@ -907,6 +915,23 @@ int main(int argc, char ** argv) {
} }
} }
if (optind < argc) {
shell_interactive = 2;
FILE * f = fopen(argv[optind],"r");
if (!f) {
fprintf(stderr, "%s: %s: file not found\n", argv[0], argv[optind]);
return 1;
}
while (!feof(f)) {
char buf[LINE_LEN] = {0};
fgets(buf, LINE_LEN, f);
last_ret = shell_exec(buf, 4096, f);
}
return 0;
}
shell_interactive = 1; shell_interactive = 1;
while (1) { while (1) {
@ -914,7 +939,7 @@ int main(int argc, char ** argv) {
char buffer[LINE_LEN] = {0}; char buffer[LINE_LEN] = {0};
read_entry(buffer); read_entry(buffer);
last_ret = shell_exec(buffer); last_ret = shell_exec(buffer, LINE_LEN, stdin);
rline_scroll = 0; rline_scroll = 0;
} }