Browse Source

give terminals a cwd

Johannes Zellner 1 month ago
parent
commit
a1ebd3c072
14 changed files with 307 additions and 51 deletions
  1. 8 3
      AGENTS.md
  2. 1 1
      Makefile
  3. 4 1
      cat.c
  4. 4 1
      ls.c
  5. 1 1
      mkdir.c
  6. 1 1
      rm.c
  7. 1 1
      rmdir.c
  8. 1 1
      save.c
  9. 12 15
      storage.c
  10. 169 26
      terminal.c
  11. 2 0
      terminal.h
  12. 90 0
      vfs.c
  13. 11 0
      vfs.h
  14. 2 0
      wm.c

+ 8 - 3
AGENTS.md

@@ -87,11 +87,16 @@ make clean
 
 ## Shell commands
 
-`hello`, `reboot`, `poweroff`, `ls [/dev[/dir]]`, `cat /dev/file`, `save /dev/file <text>`, `rm /dev/file`,
-`mkdir /dev/dir`, `rmdir /dev/dir`, `run /dev/file`, `space`, `clock`,
-`colors`, `display`, `cpuinfo`, `mouseinfo`, `lsusb`, `storage`, `format <device>`,
+`cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
+`mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`, `run <file>`, `space`,
+`clock`, `colors`, `display`, `cpuinfo`, `mouseinfo`, `lsusb`, `storage`, `format <device>`,
 `ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 
+Each terminal has a current working directory (shown in the prompt as `[dir] > `),
+changed with `cd`; paths may be absolute (`/floppy1/…`) or relative (`.`, `..`,
+nested). `/` is the read-only VFS root listing the mounted filesystems (the
+filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
+
 ## Serial hot-reload (kexec) workflow
 
 Develop without reflashing the floppy. The serial link (COM1 ↔ `/dev/ttyUSB0`)

+ 1 - 1
Makefile

@@ -35,7 +35,7 @@ font_8x8.o: font_8x8.c font_8x8.h
 fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-terminal.o: terminal.c terminal.h screen.h io.h dbg.h idle.h kexec.h process.h timer.h
+terminal.o: terminal.c terminal.h screen.h vfs.h fat.h blkdev.h io.h dbg.h idle.h kexec.h process.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 wm.o: wm.c wm.h terminal.h screen.h fbtext.h idle.h io.h mouse.h process.h rtc.h timer.h version.h vga.h

+ 4 - 1
cat.c

@@ -23,7 +23,10 @@ void program_cat_main(const char *filename)
 
 	vfs_mount_all();
 	if (vfs_resolve(filename, &fs, &fs_path) != 0) {
-		io_putstr("cat: unknown device or invalid path\n");
+		if (vfs_device_known(filename))
+			io_putstr("cat: no filesystem on device\n");
+		else
+			io_putstr("cat: no such device\n");
 		return;
 	}
 

+ 4 - 1
ls.c

@@ -53,7 +53,10 @@ void program_ls_main(const char *path)
 	}
 
 	if (vfs_resolve(path, &fs, &fs_path) != 0) {
-		io_putstr("ls: unknown device or invalid path\n");
+		if (vfs_device_known(path))
+			io_putstr("ls: no filesystem on device\n");
+		else
+			io_putstr("ls: no such device\n");
 		return;
 	}
 

+ 1 - 1
mkdir.c

@@ -19,7 +19,7 @@ void program_mkdir_main(const char *arg)
 
 	vfs_mount_all();
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
-		io_putstr("mkdir: unknown device or invalid path\n");
+		io_putstr("mkdir: read-only filesystem\n");
 		return;
 	}
 

+ 1 - 1
rm.c

@@ -19,7 +19,7 @@ void program_rm_main(const char *arg)
 
 	vfs_mount_all();
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
-		io_putstr("rm: unknown device or invalid path\n");
+		io_putstr("rm: read-only filesystem\n");
 		return;
 	}
 

+ 1 - 1
rmdir.c

@@ -19,7 +19,7 @@ void program_rmdir_main(const char *arg)
 
 	vfs_mount_all();
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
-		io_putstr("rmdir: unknown device or invalid path\n");
+		io_putstr("rmdir: read-only filesystem\n");
 		return;
 	}
 

+ 1 - 1
save.c

@@ -50,7 +50,7 @@ void program_save_main(const char *arg)
 
 	vfs_mount_all();
 	if (vfs_resolve(fn, &fs, &fs_path) != 0) {
-		io_putstr("save: unknown device or invalid path\n");
+		io_putstr("save: read-only filesystem\n");
 		return;
 	}
 

+ 12 - 15
storage.c

@@ -56,16 +56,16 @@ static const char *fat_name(fat_type_t t)
 	}
 }
 
-static void report_fs(const char *name, const fat_info_t *info)
+static void report_fs(const char *name, const fat_fs_t *fs)
 {
 	io_putstr(name);
 	io_putstr(": ");
-	io_putstr(fat_name(info->type));
+	io_putstr(fat_name(fs->type));
 	io_putchar(' ');
-	put_size(info->total_sectors * info->bytes_per_sector);
-	if (info->label[0]) {
+	put_size(fs->total_sectors * fs->bytes_per_sector);
+	if (fs->label[0]) {
 		io_putstr("  \"");
-		io_putstr(info->label);
+		io_putstr(fs->label);
 		io_putchar('"');
 	}
 	io_putchar('\n');
@@ -86,14 +86,13 @@ static void part_name(char *out, const char *base, unsigned int idx)
 void program_storage_main(void)
 {
 	static unsigned char buf[512];
-	static unsigned char pbuf[512];
 	unsigned int i;
 
 	blk_scan();
 
 	for (i = 0; i < blk_dev_count; i++) {
 		blk_dev_t *d = &blk_devs[i];
-		fat_info_t info;
+		fat_fs_t fs;
 
 		if (d->kind == BLK_IDE && !d->present) {
 			io_putstr(d->name);
@@ -108,8 +107,8 @@ void program_storage_main(void)
 		}
 
 		if (d->kind == BLK_FLOPPY) {
-			if (fat_detect(buf, &info) == 0)
-				report_fs(d->name, &info);
+			if (fat_mount(d, 0, &fs) == 0)
+				report_fs(d->name, &fs);
 			else {
 				io_putstr(d->name);
 				io_putstr(": no valid FAT filesystem\n");
@@ -125,18 +124,16 @@ void program_storage_main(void)
 				if (!parts[j].is_fat || parts[j].start_lba == 0 ||
 				    parts[j].sectors == 0)
 					continue;
-				if (blk_read(d, parts[j].start_lba, pbuf, 1) != 0)
-					continue;
-				if (fat_detect(pbuf, &info) == 0) {
+				if (fat_mount(d, parts[j].start_lba, &fs) == 0) {
 					part_name(pname, d->name, parts[j].index);
-					report_fs(pname, &info);
+					report_fs(pname, &fs);
 					found = 1;
 				}
 			}
 
 			if (!found) {
-				if (fat_detect(buf, &info) == 0)
-					report_fs(d->name, &info);
+				if (fat_mount(d, 0, &fs) == 0)
+					report_fs(d->name, &fs);
 				else {
 					io_putstr(d->name);
 					io_putstr(": no valid FAT filesystem\n");

+ 169 - 26
terminal.c

@@ -31,8 +31,16 @@ extern void program_lsusb_main(void);
 extern void program_storage_main(void);
 extern void program_format_main(const char *arg);
 
-static const char prompt[] __attribute__((section(".text"))) = "\n> ";
-static const char prompt_first[] __attribute__((section(".text"))) = "> ";
+/* Print the prompt (with the current directory) and set line_start. */
+static void term_print_prompt(struct term *t, int first)
+{
+	if (!first)
+		io_putchar('\n');
+	io_putchar('[');
+	io_putstr(t->cwd);
+	io_putstr("] > ");
+	t->line_start = io_get_cursor();
+}
 
 static int is_cmd_hello(const char *line, unsigned int line_len)
 {
@@ -107,6 +115,33 @@ static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, un
 	return (i == line_len);
 }
 
+static int is_cmd_cd(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 2 > line_len)
+		return 0;
+	if (line[i] != 'c' || line[i+1] != 'd')
+		return 0;
+	i += 2;
+	if (i < line_len && line[i] != ' ')
+		return 0;  /* "cd" must be a whole word */
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			path_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		path_buf[j++] = line[i++];
+	path_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_clock(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -602,6 +637,91 @@ static void history_push(struct term *t, const char *line, unsigned int line_len
 		t->history_count++;
 }
 
+/* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
+ * validate it is an existing directory. Updates t->cwd on success. */
+static void term_cd(struct term *t, const char *arg)
+{
+	char abs[VFS_PATH_MAX];
+	fat_fs_t *fs;
+	const char *fs_path;
+	unsigned int dir;
+	int r;
+	unsigned int i;
+
+	if (arg[0] == '\0') {
+		t->cwd[0] = '/';
+		t->cwd[1] = '\0';
+		return;
+	}
+
+	if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
+		io_putstr("cd: path too long\n");
+		return;
+	}
+
+	if (abs[0] == '/' && abs[1] == '\0') {
+		t->cwd[0] = '/';
+		t->cwd[1] = '\0';
+		return;
+	}
+
+	vfs_mount_all();
+	if (vfs_resolve(abs, &fs, &fs_path) != 0) {
+		io_putstr("cd: no such directory\n");
+		return;
+	}
+	r = fat_resolve_dir(fs, fs_path, &dir);
+	if (r == -2) {
+		io_putstr("cd: disk error\n");
+		return;
+	}
+	if (r != 0) {
+		io_putstr("cd: no such directory\n");
+		return;
+	}
+
+	for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
+		t->cwd[i] = abs[i];
+	t->cwd[i] = '\0';
+}
+
+/* Resolve the first token (path) of `args` against t->cwd, rebuilding
+ * "abs-path rest-of-args" into out. Used by `save`. */
+static void term_resolve_first(struct term *t, const char *args,
+			       char *out, unsigned int out_size)
+{
+	char path[VFS_PATH_MAX];
+	char abs[VFS_PATH_MAX];
+	unsigned int i = 0, j = 0;
+
+	while (args[i] == ' ')
+		i++;
+	while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
+		path[j++] = args[i++];
+	path[j] = '\0';
+
+	if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
+		abs[0] = '/';
+		abs[1] = '\0';
+	}
+	while (args[i] == ' ')
+		i++;
+
+	j = 0;
+	{
+		unsigned int k = 0;
+		while (abs[k] && j + 1 < out_size)
+			out[j++] = abs[k++];
+	}
+	if (args[i]) {
+		if (j + 1 < out_size)
+			out[j++] = ' ';
+		while (args[i] && j + 1 < out_size)
+			out[j++] = args[i++];
+	}
+	out[j] = '\0';
+}
+
 void term_run(struct term *t)
 {
 	t->line_len         = 0;
@@ -610,8 +730,7 @@ void term_run(struct term *t)
 	t->history_browse   = 0;
 
 	io_clear_screen();
-	io_putstr(prompt_first);
-	t->line_start = io_get_cursor();
+	term_print_prompt(t, 1);
 
 	for (;;) {
 		{
@@ -662,6 +781,8 @@ void term_run(struct term *t)
 					char rm_fname[RM_FNAME_MAX];
 					char ls_path[LS_PATH_MAX];
 					char format_dev[FORMAT_DEV_MAX];
+					char resolved[VFS_PATH_MAX];
+					char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
 					unsigned int kill_pid;
 					int skip_prompt = 0;
 					io_putchar('\n');
@@ -674,9 +795,15 @@ void term_run(struct term *t)
 						program_reboot_main();
 					} else if (is_cmd_poweroff(t->line, t->line_len)) {
 						program_poweroff_main();
+					} else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
+						term_cd(t, ls_path);
 					} else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
-						if (process_create_arg("ls", program_ls_main, ls_path) < 0)
-							io_putstr("error: too many processes\n");
+						if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
+							if (process_create_arg("ls", program_ls_main, resolved) < 0)
+								io_putstr("error: too many processes\n");
+						} else {
+							io_putstr("ls: path too long\n");
+						}
 					} else if (is_cmd_lsusb(t->line, t->line_len)) {
 						if (process_create("lsusb", program_lsusb_main) < 0)
 							io_putstr("error: too many processes\n");
@@ -688,8 +815,12 @@ void term_run(struct term *t)
 					} 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 (process_create_arg("cat", program_cat_main, cat_fname) < 0)
-							io_putstr("error: too many processes\n");
+						if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
+							if (process_create_arg("cat", program_cat_main, resolved) < 0)
+								io_putstr("error: too many processes\n");
+						} else {
+							io_putstr("cat: path too long\n");
+						}
 					} else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
 						if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
 						    cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
@@ -701,17 +832,30 @@ void term_run(struct term *t)
 							io_putstr("run: not available in windowed mode yet\n");
 						}
 					} else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
-						if (process_create_arg("save", program_save_main, save_args) < 0)
+						term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
+						if (process_create_arg("save", program_save_main, save_resolved) < 0)
 							io_putstr("error: too many processes\n");
 					} else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
-						if (process_create_arg("mkdir", program_mkdir_main, rm_fname) < 0)
-							io_putstr("error: too many processes\n");
+						if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
+							if (process_create_arg("mkdir", program_mkdir_main, resolved) < 0)
+								io_putstr("error: too many processes\n");
+						} else {
+							io_putstr("mkdir: path too long\n");
+						}
 					} else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
-						if (process_create_arg("rmdir", program_rmdir_main, rm_fname) < 0)
-							io_putstr("error: too many processes\n");
+						if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
+							if (process_create_arg("rmdir", program_rmdir_main, resolved) < 0)
+								io_putstr("error: too many processes\n");
+						} else {
+							io_putstr("rmdir: path too long\n");
+						}
 					} else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
-						if (process_create_arg("rm", program_rm_main, rm_fname) < 0)
-							io_putstr("error: too many processes\n");
+						if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
+							if (process_create_arg("rm", program_rm_main, resolved) < 0)
+								io_putstr("error: too many processes\n");
+						} 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)) {
@@ -745,16 +889,17 @@ void term_run(struct term *t)
 					} else if (is_cmd_dbg(t->line, t->line_len)) {
 						program_dbg_main();
 					} else if (is_cmd_help(t->line, t->line_len)) {
+						io_putstr("cd [path]\n");
+						io_putstr("ls [path]\n");
+						io_putstr("cat <path>\n");
+						io_putstr("save <path> <text>\n");
+						io_putstr("rm <path>\n");
+						io_putstr("mkdir <path>\n");
+						io_putstr("rmdir <path>\n");
 						io_putstr("hello\n");
 						io_putstr("reboot\n");
 						io_putstr("poweroff\n");
-						io_putstr("ls [/dev[/dir]]\n");
-						io_putstr("cat /dev/file\n");
-						io_putstr("save /dev/file <text>\n");
-						io_putstr("rm /dev/file\n");
-						io_putstr("mkdir /dev/dir\n");
-						io_putstr("rmdir /dev/dir\n");
-						io_putstr("run /dev/file\n");
+						io_putstr("run <file>\n");
 						io_putstr("space\n");
 						io_putstr("clock\n");
 						io_putstr("colors\n");
@@ -772,8 +917,7 @@ void term_run(struct term *t)
 						io_putstr("clear\n");
 				} else if (is_cmd_clear(t->line, t->line_len)) {
 					io_clear_screen();
-					io_putstr(prompt_first);
-					t->line_start = io_get_cursor();
+					term_print_prompt(t, 1);
 					skip_prompt = 1;
 				} else {
 					char serial_arg[8];
@@ -802,8 +946,7 @@ void term_run(struct term *t)
 				t->line_pos = 0;
 				t->line[0]  = '\0';
 				if (!skip_prompt) {
-					io_putstr(prompt);
-					t->line_start = io_get_cursor();
+					term_print_prompt(t, 0);
 				}
 			} else {
 				unsigned char u = (unsigned char)k;

+ 2 - 0
terminal.h

@@ -3,6 +3,7 @@
 #define TERMINAL_H
 
 #include "screen.h"
+#include "vfs.h"
 
 #define CURSOR_MS    500
 #define LINE_MAX     64
@@ -19,6 +20,7 @@ struct term {
 	unsigned int line_pos;
 	unsigned int line_start;
 	unsigned int history_browse;
+	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];
 	unsigned int key_head;

+ 90 - 0
vfs.c

@@ -192,3 +192,93 @@ int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path)
 	*fs_path = path + i;
 	return 0;
 }
+
+int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
+{
+	char buf[VFS_PATH_MAX * 2 + 2];
+	unsigned int bi = 0;
+	unsigned int len;
+	const char *p;
+
+	/* Build the combined (un-normalized) path. */
+	if (arg[0] == '/') {
+		p = arg;
+	} else {
+		p = cwd;
+		while (*p && bi < sizeof(buf) - 1)
+			buf[bi++] = *p++;
+		if (bi > 0 && buf[bi - 1] != '/') {
+			if (bi < sizeof(buf) - 1)
+				buf[bi++] = '/';
+		}
+		p = arg;
+	}
+	while (*p && bi < sizeof(buf) - 1)
+		buf[bi++] = *p++;
+	buf[bi] = '\0';
+
+	if (out_size < 2)
+		return -1;
+
+	/* Normalize into out (always begins with "/"). */
+	out[0] = '/';
+	out[1] = '\0';
+	len = 1;
+
+	p = buf;
+	while (*p) {
+		unsigned int clen = 0;
+		const char *comp;
+
+		while (*p == '/')
+			p++;
+		if (!*p)
+			break;
+		comp = p;
+		while (*p && *p != '/') {
+			p++;
+			clen++;
+		}
+
+		if (clen == 1 && comp[0] == '.')
+			continue;
+		if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
+			/* Pop the previous component (may leave "/"). */
+			while (len > 1 && out[len - 1] != '/')
+				len--;
+			if (len > 1)
+				len--;
+			out[len] = '\0';
+			continue;
+		}
+		/* Append "/component". */
+		if (len + 1 + clen + 1 > out_size)
+			return -1;
+		if (len > 1)
+			out[len++] = '/';
+		{
+			unsigned int k;
+			for (k = 0; k < clen; k++)
+				out[len++] = comp[k];
+		}
+		out[len] = '\0';
+	}
+
+	return 0;
+}
+
+int vfs_device_known(const char *path)
+{
+	unsigned int i = 0, j = 0;
+	char name[16];
+
+	if (!path || path[0] != '/')
+		return 0;
+	i = 1;
+	while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
+		name[j++] = path[i++];
+	name[j] = '\0';
+	if (j == 0)
+		return 0;
+	return blk_find(name) != 0;
+}

+ 11 - 0
vfs.h

@@ -13,6 +13,7 @@ typedef struct {
 } vfs_mount_t;
 
 #define VFS_MAX_MOUNTS 12
+#define VFS_PATH_MAX   64
 extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 extern unsigned int vfs_mount_count;
 
@@ -29,4 +30,14 @@ void vfs_list_root(void);
  * points into `path` (no copy). Returns 0 on success, -1 on error. */
 int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path);
 
+/* Join `arg` onto `cwd` (arg may be absolute or relative) and normalize the
+ * result into `out` as an absolute VFS path. "." and duplicate slashes are
+ * dropped; ".." pops the previous component (popping the mount name yields
+ * "/", and ".." at "/" stays "/"). Returns 0 on success, -1 if it doesn't fit. */
+int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size);
+
+/* After a failed vfs_resolve, returns 1 if the path's first component names a
+ * known block device (i.e. it exists but has no mounted filesystem). */
+int vfs_device_known(const char *path);
+
 #endif

+ 2 - 0
wm.c

@@ -86,6 +86,8 @@ int wm_spawn_terminal(void)
 	t->key_head = t->key_tail = 0;
 	t->history_count = 0;
 	t->history_next = 0;
+	t->cwd[0] = '/';
+	t->cwd[1] = '\0';
 
 	w->used = 1;
 	w->term = t;