|
@@ -792,25 +792,25 @@ static void term_resolve_first(struct term *t, const char *args,
|
|
|
out[j] = '\0';
|
|
out[j] = '\0';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/* Spawn a foreground command process and wait for it to finish before
|
|
|
|
|
- * printing the next prompt (so its output doesn't overwrite the prompt). */
|
|
|
|
|
-static void term_spawn(const char *name, void (*entry)(void))
|
|
|
|
|
|
|
+/* Spawn a foreground command process; the caller (term_run's wait loop)
|
|
|
|
|
+ * waits for it to finish and handles Ctrl+C. */
|
|
|
|
|
+static void term_spawn(struct term *t, const char *name, void (*entry)(void))
|
|
|
{
|
|
{
|
|
|
int pid = process_create(name, entry);
|
|
int pid = process_create(name, entry);
|
|
|
if (pid < 0)
|
|
if (pid < 0)
|
|
|
io_putstr("error: too many processes\n");
|
|
io_putstr("error: too many processes\n");
|
|
|
else
|
|
else
|
|
|
- process_wait((unsigned int)pid);
|
|
|
|
|
|
|
+ t->fg_pid = pid;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void term_spawn_arg(const char *name, void (*entry)(const char *),
|
|
|
|
|
- const char *arg)
|
|
|
|
|
|
|
+static void term_spawn_arg(struct term *t, const char *name,
|
|
|
|
|
+ void (*entry)(const char *), const char *arg)
|
|
|
{
|
|
{
|
|
|
int pid = process_create_arg(name, entry, arg);
|
|
int pid = process_create_arg(name, entry, arg);
|
|
|
if (pid < 0)
|
|
if (pid < 0)
|
|
|
io_putstr("error: too many processes\n");
|
|
io_putstr("error: too many processes\n");
|
|
|
else
|
|
else
|
|
|
- process_wait((unsigned int)pid);
|
|
|
|
|
|
|
+ t->fg_pid = pid;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void term_run(struct term *t)
|
|
void term_run(struct term *t)
|
|
@@ -819,15 +819,41 @@ void term_run(struct term *t)
|
|
|
t->line_pos = 0;
|
|
t->line_pos = 0;
|
|
|
t->line[0] = '\0';
|
|
t->line[0] = '\0';
|
|
|
t->history_browse = 0;
|
|
t->history_browse = 0;
|
|
|
|
|
+ t->fg_pid = -1;
|
|
|
|
|
|
|
|
io_clear_screen();
|
|
io_clear_screen();
|
|
|
term_print_prompt(t, 1);
|
|
term_print_prompt(t, 1);
|
|
|
|
|
|
|
|
for (;;) {
|
|
for (;;) {
|
|
|
|
|
+ if (t->fg_pid >= 0) {
|
|
|
|
|
+ /* Waiting on a foreground child: poll keys for Ctrl+C
|
|
|
|
|
+ * and watch for the child to exit. */
|
|
|
|
|
+ int k = term_key_pop(t);
|
|
|
|
|
+ if (k == IO_KEY_CTRL_C) {
|
|
|
|
|
+ int killed = (process_kill((unsigned int)t->fg_pid) == 0);
|
|
|
|
|
+ t->fg_pid = -1;
|
|
|
|
|
+ if (killed)
|
|
|
|
|
+ io_putstr("^C\n");
|
|
|
|
|
+ term_print_prompt(t, 0);
|
|
|
|
|
+ } else if (!process_is_alive((unsigned int)t->fg_pid)) {
|
|
|
|
|
+ t->fg_pid = -1;
|
|
|
|
|
+ term_print_prompt(t, 0);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ cpu_idle();
|
|
|
|
|
+ }
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
{
|
|
{
|
|
|
int k = term_key_pop(t);
|
|
int k = term_key_pop(t);
|
|
|
if (k) {
|
|
if (k) {
|
|
|
- if (k == IO_KEY_LEFT) {
|
|
|
|
|
|
|
+ if (k == IO_KEY_CTRL_C) {
|
|
|
|
|
+ /* Ctrl+C at the prompt: discard the line. */
|
|
|
|
|
+ io_putstr("^C\n");
|
|
|
|
|
+ t->line_len = 0;
|
|
|
|
|
+ t->line_pos = 0;
|
|
|
|
|
+ t->line[0] = '\0';
|
|
|
|
|
+ term_print_prompt(t, 0);
|
|
|
|
|
+ } else if (k == IO_KEY_LEFT) {
|
|
|
if (t->line_pos > 0) {
|
|
if (t->line_pos > 0) {
|
|
|
t->line_pos--;
|
|
t->line_pos--;
|
|
|
io_set_cursor(t->line_start + t->line_pos);
|
|
io_set_cursor(t->line_start + t->line_pos);
|
|
@@ -880,7 +906,7 @@ void term_run(struct term *t)
|
|
|
history_push(t, t->line, t->line_len);
|
|
history_push(t, t->line, t->line_len);
|
|
|
t->history_browse = 0;
|
|
t->history_browse = 0;
|
|
|
if (is_cmd_hello(t->line, t->line_len)) {
|
|
if (is_cmd_hello(t->line, t->line_len)) {
|
|
|
- term_spawn("hello", program_hello_main);
|
|
|
|
|
|
|
+ term_spawn(t, "hello", program_hello_main);
|
|
|
} else if (is_cmd_reboot(t->line, t->line_len)) {
|
|
} else if (is_cmd_reboot(t->line, t->line_len)) {
|
|
|
program_reboot_main();
|
|
program_reboot_main();
|
|
|
} else if (is_cmd_poweroff(t->line, t->line_len)) {
|
|
} else if (is_cmd_poweroff(t->line, t->line_len)) {
|
|
@@ -889,29 +915,29 @@ void term_run(struct term *t)
|
|
|
term_cd(t, ls_path);
|
|
term_cd(t, ls_path);
|
|
|
} else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
|
|
} else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
|
|
|
if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
|
|
if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
|
|
|
- term_spawn_arg("ls", program_ls_main, resolved);
|
|
|
|
|
|
|
+ term_spawn_arg(t, "ls", program_ls_main, resolved);
|
|
|
} else {
|
|
} else {
|
|
|
io_putstr("ls: path too long\n");
|
|
io_putstr("ls: path too long\n");
|
|
|
}
|
|
}
|
|
|
} else if (is_cmd_lsusb(t->line, t->line_len)) {
|
|
} else if (is_cmd_lsusb(t->line, t->line_len)) {
|
|
|
- term_spawn("lsusb", program_lsusb_main);
|
|
|
|
|
|
|
+ term_spawn(t, "lsusb", program_lsusb_main);
|
|
|
} else if (is_cmd_lspci(t->line, t->line_len)) {
|
|
} else if (is_cmd_lspci(t->line, t->line_len)) {
|
|
|
- term_spawn("lspci", program_lspci_main);
|
|
|
|
|
|
|
+ term_spawn(t, "lspci", program_lspci_main);
|
|
|
} else if (is_cmd_netinfo(t->line, t->line_len)) {
|
|
} else if (is_cmd_netinfo(t->line, t->line_len)) {
|
|
|
- term_spawn("netinfo", program_netinfo_main);
|
|
|
|
|
|
|
+ term_spawn(t, "netinfo", program_netinfo_main);
|
|
|
} else if (is_cmd_netrecv(t->line, t->line_len)) {
|
|
} else if (is_cmd_netrecv(t->line, t->line_len)) {
|
|
|
- term_spawn("netrecv", program_netrecv_main);
|
|
|
|
|
|
|
+ term_spawn(t, "netrecv", program_netrecv_main);
|
|
|
} else if (is_cmd_netsend(t->line, t->line_len)) {
|
|
} else if (is_cmd_netsend(t->line, t->line_len)) {
|
|
|
- term_spawn("netsend", program_netsend_main);
|
|
|
|
|
|
|
+ term_spawn(t, "netsend", program_netsend_main);
|
|
|
} else if (is_cmd_storage(t->line, t->line_len)) {
|
|
} else if (is_cmd_storage(t->line, t->line_len)) {
|
|
|
- term_spawn("storage", program_storage_main);
|
|
|
|
|
|
|
+ term_spawn(t, "storage", program_storage_main);
|
|
|
} else if (is_cmd_clock(t->line, t->line_len)) {
|
|
} else if (is_cmd_clock(t->line, t->line_len)) {
|
|
|
io_putstr("clock: not available in windowed mode yet\n");
|
|
io_putstr("clock: not available in windowed mode yet\n");
|
|
|
} else if (is_cmd_colors(t->line, t->line_len)) {
|
|
} else if (is_cmd_colors(t->line, t->line_len)) {
|
|
|
io_putstr("colors: not available in windowed mode yet\n");
|
|
io_putstr("colors: not available in windowed mode yet\n");
|
|
|
} else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
|
|
} else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
|
|
|
if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
|
|
if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
|
|
|
- term_spawn_arg("cat", program_cat_main, resolved);
|
|
|
|
|
|
|
+ term_spawn_arg(t, "cat", program_cat_main, resolved);
|
|
|
} else {
|
|
} else {
|
|
|
io_putstr("cat: path too long\n");
|
|
io_putstr("cat: path too long\n");
|
|
|
}
|
|
}
|
|
@@ -927,33 +953,33 @@ void term_run(struct term *t)
|
|
|
}
|
|
}
|
|
|
} else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
|
|
} else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
|
|
|
term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
|
|
term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
|
|
|
- term_spawn_arg("save", program_save_main, save_resolved);
|
|
|
|
|
|
|
+ term_spawn_arg(t, "save", program_save_main, save_resolved);
|
|
|
} else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
|
|
} else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
|
|
|
if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
|
|
if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
|
|
|
- term_spawn_arg("mkdir", program_mkdir_main, resolved);
|
|
|
|
|
|
|
+ term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
|
|
|
} else {
|
|
} else {
|
|
|
io_putstr("mkdir: path too long\n");
|
|
io_putstr("mkdir: path too long\n");
|
|
|
}
|
|
}
|
|
|
} else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
|
|
} else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
|
|
|
if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
|
|
if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
|
|
|
- term_spawn_arg("rmdir", program_rmdir_main, resolved);
|
|
|
|
|
|
|
+ term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
|
|
|
} else {
|
|
} else {
|
|
|
io_putstr("rmdir: path too long\n");
|
|
io_putstr("rmdir: path too long\n");
|
|
|
}
|
|
}
|
|
|
} else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
|
|
} else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
|
|
|
if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
|
|
if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
|
|
|
- term_spawn_arg("rm", program_rm_main, resolved);
|
|
|
|
|
|
|
+ term_spawn_arg(t, "rm", program_rm_main, resolved);
|
|
|
} else {
|
|
} else {
|
|
|
io_putstr("rm: path too long\n");
|
|
io_putstr("rm: path too long\n");
|
|
|
}
|
|
}
|
|
|
} else if (is_cmd_space(t->line, t->line_len)) {
|
|
} else if (is_cmd_space(t->line, t->line_len)) {
|
|
|
io_putstr("space: not available in windowed mode yet\n");
|
|
io_putstr("space: not available in windowed mode yet\n");
|
|
|
} else if (is_cmd_display(t->line, t->line_len)) {
|
|
} else if (is_cmd_display(t->line, t->line_len)) {
|
|
|
- term_spawn("display", program_display_main);
|
|
|
|
|
|
|
+ term_spawn(t, "display", program_display_main);
|
|
|
} else if (is_cmd_cpuinfo(t->line, t->line_len)) {
|
|
} else if (is_cmd_cpuinfo(t->line, t->line_len)) {
|
|
|
- term_spawn("cpuinfo", program_cpuinfo_main);
|
|
|
|
|
|
|
+ term_spawn(t, "cpuinfo", program_cpuinfo_main);
|
|
|
} else if (is_cmd_mouseinfo(t->line, t->line_len)) {
|
|
} else if (is_cmd_mouseinfo(t->line, t->line_len)) {
|
|
|
- term_spawn("mouseinfo", program_mouseinfo_main);
|
|
|
|
|
|
|
+ term_spawn(t, "mouseinfo", program_mouseinfo_main);
|
|
|
} else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
|
|
} else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
|
|
|
program_format_main(format_dev);
|
|
program_format_main(format_dev);
|
|
|
} else if (is_cmd_ps(t->line, t->line_len)) {
|
|
} else if (is_cmd_ps(t->line, t->line_len)) {
|
|
@@ -1036,7 +1062,7 @@ void term_run(struct term *t)
|
|
|
t->line_len = 0;
|
|
t->line_len = 0;
|
|
|
t->line_pos = 0;
|
|
t->line_pos = 0;
|
|
|
t->line[0] = '\0';
|
|
t->line[0] = '\0';
|
|
|
- if (!skip_prompt) {
|
|
|
|
|
|
|
+ if (!skip_prompt && t->fg_pid < 0) {
|
|
|
term_print_prompt(t, 0);
|
|
term_print_prompt(t, 0);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|