Procházet zdrojové kódy

Add ctrl+c control flow

Johannes Zellner před 1 měsícem
rodič
revize
3fc7482fe4
6 změnil soubory, kde provedl 85 přidání a 41 odebrání
  1. 14 1
      io.c
  2. 5 4
      io.h
  3. 13 11
      process.c
  4. 1 0
      process.h
  5. 51 25
      terminal.c
  6. 1 0
      terminal.h

+ 14 - 1
io.c

@@ -44,6 +44,7 @@ static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodat
 };
 
 static int shift_pressed;
+static int ctrl_pressed;
 static unsigned char last_scancode;
 static int kbd_extended;  /* 1 after 0xE0, next byte is extended scancode */
 
@@ -196,17 +197,29 @@ static int kbd_process_byte(unsigned char sc)
 	}
 	if (sc & 0x80) {
 		if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
+		if (sc == 0x9D) ctrl_pressed = 0;
 		return 0;
 	}
 	if (sc == 0x2A || sc == 0x36) {
 		shift_pressed = 1;
 		return 0;
 	}
+	if (sc == 0x1D) {
+		ctrl_pressed = 1;
+		return 0;
+	}
 	{
 		char c = (char)scancode_to_ascii[sc];
 		if (c) {
 			last_scancode = sc;
-			return (unsigned char)apply_shift(c);
+			c = apply_shift(c);
+			if (ctrl_pressed) {
+				/* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
+				if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
+					return (unsigned char)(c & 0x1F);
+				return 0;
+			}
+			return (unsigned char)c;
 		}
 	}
 	return 0;

+ 5 - 4
io.h

@@ -29,10 +29,11 @@ void io_vga_write(unsigned int pos, unsigned short value);
 void io_drain_keyboard(void);
 
 /* Poll keyboard; returns 0 = no key, 1..255 = ASCII, or IO_KEY_LEFT/IO_KEY_RIGHT/IO_KEY_UP/IO_KEY_DOWN. */
-#define IO_KEY_LEFT  0x100
-#define IO_KEY_RIGHT 0x101
-#define IO_KEY_UP    0x102
-#define IO_KEY_DOWN  0x103
+#define IO_KEY_CTRL_C 0x03  /* Ctrl+C (ASCII ETX), kills the foreground job */
+#define IO_KEY_LEFT   0x100
+#define IO_KEY_RIGHT  0x101
+#define IO_KEY_UP     0x102
+#define IO_KEY_DOWN   0x103
 int io_poll_key(void);
 /* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
 int io_poll_key_with_hw(void);

+ 13 - 11
process.c

@@ -253,21 +253,23 @@ int process_kill(unsigned int pid)
 	return -1;
 }
 
+/* Return 1 if a process with `pid` is still runnable (READY/RUNNING). */
+int process_is_alive(unsigned int pid)
+{
+	unsigned int i;
+	for (i = 0; i < MAX_PROCS; i++)
+		if (procs[i].pid == pid &&
+		    (procs[i].state == PROC_READY ||
+		     procs[i].state == PROC_RUNNING))
+			return 1;
+	return 0;
+}
+
 /* Wait (yield) until the process with `pid` has exited.  The scheduler
  * preempts us every timeslice so the child runs and process_exit() reaps
  * it to PROC_UNUSED. */
 void process_wait(unsigned int pid)
 {
-	unsigned int i;
-	for (;;) {
-		int alive = 0;
-		for (i = 0; i < MAX_PROCS; i++)
-			if (procs[i].pid == pid &&
-			    (procs[i].state == PROC_READY ||
-			     procs[i].state == PROC_RUNNING))
-				alive = 1;
-		if (!alive)
-			return;
+	while (process_is_alive(pid))
 		cpu_idle();
-	}
 }

+ 1 - 0
process.h

@@ -38,6 +38,7 @@ void process_exit(void);
 void schedule(void);
 void process_ps(void);
 int  process_kill(unsigned int pid);
+int  process_is_alive(unsigned int pid);
 void process_wait(unsigned int pid);
 
 #endif

+ 51 - 25
terminal.c

@@ -792,25 +792,25 @@ static void term_resolve_first(struct term *t, const char *args,
 	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);
 	if (pid < 0)
 		io_putstr("error: too many processes\n");
 	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);
 	if (pid < 0)
 		io_putstr("error: too many processes\n");
 	else
-		process_wait((unsigned int)pid);
+		t->fg_pid = pid;
 }
 
 void term_run(struct term *t)
@@ -819,15 +819,41 @@ void term_run(struct term *t)
 	t->line_pos         = 0;
 	t->line[0]          = '\0';
 	t->history_browse   = 0;
+	t->fg_pid           = -1;
 
 	io_clear_screen();
 	term_print_prompt(t, 1);
 
 	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);
 			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) {
 						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);
 					t->history_browse = 0;
 					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)) {
 						program_reboot_main();
 					} 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);
 					} 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) {
-							term_spawn_arg("ls", program_ls_main, resolved);
+							term_spawn_arg(t, "ls", program_ls_main, resolved);
 						} else {
 							io_putstr("ls: path too long\n");
 						}
 					} 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)) {
-						term_spawn("lspci", program_lspci_main);
+						term_spawn(t, "lspci", program_lspci_main);
 					} 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)) {
-						term_spawn("netrecv", program_netrecv_main);
+						term_spawn(t, "netrecv", program_netrecv_main);
 					} 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)) {
-						term_spawn("storage", program_storage_main);
+						term_spawn(t, "storage", program_storage_main);
 					} else if (is_cmd_clock(t->line, t->line_len)) {
 						io_putstr("clock: not available in windowed mode yet\n");
 					} else if (is_cmd_colors(t->line, t->line_len)) {
 						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)) {
 						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 {
 							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)) {
 						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)) {
 						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 {
 							io_putstr("mkdir: path too long\n");
 						}
 					} 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) {
-							term_spawn_arg("rmdir", program_rmdir_main, resolved);
+							term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
 						} else {
 							io_putstr("rmdir: path too long\n");
 						}
 					} 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) {
-							term_spawn_arg("rm", program_rm_main, resolved);
+							term_spawn_arg(t, "rm", program_rm_main, resolved);
 						} else {
 							io_putstr("rm: path too long\n");
 						}
 					} else if (is_cmd_space(t->line, t->line_len)) {
 						io_putstr("space: not available in windowed mode yet\n");
 					} 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)) {
-						term_spawn("cpuinfo", program_cpuinfo_main);
+						term_spawn(t, "cpuinfo", program_cpuinfo_main);
 					} 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)) {
 						program_format_main(format_dev);
 					} 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_pos = 0;
 				t->line[0]  = '\0';
-				if (!skip_prompt) {
+				if (!skip_prompt && t->fg_pid < 0) {
 					term_print_prompt(t, 0);
 				}
 			} else {

+ 1 - 0
terminal.h

@@ -20,6 +20,7 @@ struct term {
 	unsigned int line_pos;
 	unsigned int line_start;
 	unsigned int history_browse;
+	int fg_pid;                 /* foreground child pid, or -1 when none */
 	char cwd[VFS_PATH_MAX];     /* absolute current working directory */
 	/* Per-terminal key queue: WM (slot 0) pushes, terminal process pops. */
 	int keys[KEY_BUF_SIZE];