| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397 |
- /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
- Commands are spawned as separate processes for preemptive multitasking. */
- #include "io.h"
- #include "dbg.h"
- #include "kexec.h"
- #include "kmalloc.h"
- #include "mm.h"
- #include "process.h"
- #include "terminal.h"
- #include "timer.h"
- extern void program_reboot_main(void);
- extern void program_poweroff_main(void);
- extern void program_run_main(const char *filename);
- extern void program_display_main(void);
- extern void program_gfxbench_main(void);
- extern void program_serial_loop_main(const char *arg);
- extern void program_serial_recv_main(const char *arg);
- extern void program_run_serial_main(const char *arg);
- extern void program_run_embedded_main(const char *name);
- extern void program_lsusb_main(void);
- extern void program_format_main(const char *arg);
- extern void program_lspci_main(const char *filter);
- extern void program_peek_main(const char *arg);
- extern void program_poke_main(const char *arg);
- /* 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();
- }
- /* ---- Command registry ----
- *
- * One table drives command dispatch, `help` and Tab completion. Most commands
- * are spawned as separate kernel processes; the CK_CUSTOM handlers are the
- * shell builtins, the commands that must run inline (ps/kill/kexec/...) and
- * `run`, the one command still unavailable in a windowed terminal. */
- enum cmd_kind {
- CK_SPAWN = 0, /* spawn, no arguments */
- CK_SPAWN_ARG, /* spawn with the raw remainder of the line */
- CK_CUSTOM /* handled by a cmd_*() handler below */
- };
- #define CMD_REQARG 0x01 /* the argument must be non-empty */
- #define CMD_BUILTIN 0x04 /* handled inline, before the PATH lookup */
- struct kcmd {
- const char *name; /* dispatch token */
- const char *usage; /* `help` line (and completion name) */
- unsigned char kind;
- unsigned char flags;
- void (*fn)(void); /* spawned entry, no argument */
- void (*fn_arg)(const char *); /* spawned entry with an argument */
- const char *fixed; /* CK_SPAWN: fixed argument, if any */
- int (*custom)(struct term *t, const char *args);
- };
- /* The terminal's spawn/path helpers are defined later in this file. */
- static void term_cd(struct term *t, const char *arg);
- static void term_spawn(struct term *t, const char *name, void (*entry)(void));
- static void term_spawn_arg(struct term *t, const char *name,
- void (*entry)(const char *), const char *arg);
- static int term_path_spawn(struct term *t, const char *word, const char *args);
- static int cmd_cd(struct term *t, const char *args);
- static int cmd_clear(struct term *t, const char *args);
- static int cmd_dbg(struct term *t, const char *args);
- static int cmd_format(struct term *t, const char *args);
- static int cmd_help(struct term *t, const char *args);
- static int cmd_path(struct term *t, const char *args);
- static int cmd_kexec(struct term *t, const char *args);
- static int cmd_kill(struct term *t, const char *args);
- static int cmd_poweroff(struct term *t, const char *args);
- static int cmd_ps(struct term *t, const char *args);
- static int cmd_reboot(struct term *t, const char *args);
- static int cmd_run(struct term *t, const char *args);
- static int cmd_serial(struct term *t, const char *args);
- /* Table order defines the `help` layout (printed as two columns). Ring-3
- * commands are not listed here: they are files on the PATH (see cmd_path). */
- static const struct kcmd cmds[] = {
- { "cd", "cd [path]", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_cd },
- { "path", "path [dir[:dir...]]", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_path },
- { "help", "help", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_help },
- { "clear", "clear", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_clear },
- { "run", "run <name>|<file>", CK_CUSTOM, 0, 0, 0, 0, cmd_run },
- { "space", "space", CK_SPAWN, 0, 0, program_run_embedded_main, "space", 0 },
- { "prog", "prog", CK_SPAWN, 0, 0, program_run_embedded_main, "prog", 0 },
- { "reboot", "reboot", CK_CUSTOM, 0, 0, 0, 0, cmd_reboot },
- { "poweroff", "poweroff", CK_CUSTOM, 0, 0, 0, 0, cmd_poweroff },
- { "display", "display", CK_SPAWN, 0, program_display_main, 0, 0, 0 },
- { "gfxbench", "gfxbench", CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
- { "lsusb", "lsusb", CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
- { "format", "format <device>", CK_CUSTOM, 0, 0, 0, 0, cmd_format },
- { "lspci", "lspci [id]", CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
- { "peek", "peek <addr> [len]", CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
- { "poke", "poke <addr> <val>", CK_SPAWN_ARG, CMD_REQARG, 0, program_poke_main, 0, 0 },
- { "ps", "ps", CK_CUSTOM, 0, 0, 0, 0, cmd_ps },
- { "kill", "kill <pid>", CK_CUSTOM, 0, 0, 0, 0, cmd_kill },
- { "kexec", "kexec", CK_CUSTOM, 0, 0, 0, 0, cmd_kexec },
- { "dbg", "dbg", CK_CUSTOM, 0, 0, 0, 0, cmd_dbg },
- { "serial", "serial test|recv", CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
- };
- #define NCMDS (sizeof(cmds) / sizeof(cmds[0]))
- /* Find the registry entry whose name matches the first word of the line. */
- static const struct kcmd *cmd_lookup(const char *line, unsigned int line_len)
- {
- unsigned int i = 0, j, k, m;
- while (i < line_len && line[i] == ' ')
- i++;
- for (j = 0; i + j < line_len && line[i + j] != ' '; j++)
- ;
- if (j == 0)
- return 0;
- for (k = 0; k < NCMDS; k++) {
- const char *n = cmds[k].name;
- for (m = 0; m < j && n[m]; m++)
- if (line[i + m] != n[m])
- break;
- if (m == j && n[m] == '\0')
- return &cmds[k];
- }
- return 0;
- }
- static void cmd_usage(const struct kcmd *c)
- {
- io_putstr(c->name);
- io_putstr(": usage ");
- io_putstr(c->usage);
- io_putchar('\n');
- }
- /* Split the line into the command word and the trimmed remainder. */
- static void term_split_line(struct term *t, char *word, unsigned int wsize,
- char *args, unsigned int asize)
- {
- unsigned int i = 0, n = 0;
- while (i < t->line_len && t->line[i] == ' ')
- i++;
- while (i < t->line_len && t->line[i] != ' ' && n + 1 < wsize)
- word[n++] = t->line[i++];
- word[n] = '\0';
- while (i < t->line_len && t->line[i] == ' ')
- i++;
- n = 0;
- while (i < t->line_len && n + 1 < asize)
- args[n++] = t->line[i++];
- while (n > 0 && args[n - 1] == ' ')
- n--;
- args[n] = '\0';
- }
- /* Dispatch one registry entry. Returns 1 when the handler already printed the
- * next prompt (clear), 0 otherwise. */
- static int term_run_command(struct term *t, const struct kcmd *c,
- const char *args)
- {
- if ((c->flags & CMD_REQARG) && args[0] == '\0') {
- cmd_usage(c);
- return 0;
- }
- switch (c->kind) {
- case CK_SPAWN:
- if (c->fixed)
- term_spawn_arg(t, c->name, c->fn_arg, c->fixed);
- else
- term_spawn(t, c->name, c->fn);
- break;
- case CK_SPAWN_ARG:
- term_spawn_arg(t, c->name, c->fn_arg, args);
- break;
- case CK_CUSTOM:
- return c->custom(t, args);
- }
- return 0;
- }
- /* `help`: printed as two 32-column columns so it fits the 64-column terminal
- * without scrolling. */
- static void help_item_pad(const char *s)
- {
- unsigned int n = 0;
- while (s[n]) {
- io_putchar(s[n]);
- n++;
- }
- while (n < 32) {
- io_putchar(' ');
- n++;
- }
- }
- static void help_print(void)
- {
- unsigned int nitems = NCMDS;
- unsigned int half = (nitems + 1) / 2;
- unsigned int k;
- for (k = 0; k < half; k++) {
- if (k + half < nitems) {
- help_item_pad(cmds[k].usage);
- io_putstr(cmds[k + half].usage);
- } else {
- io_putstr(cmds[k].usage);
- }
- io_putchar('\n');
- }
- }
- /* ---- Command handlers that run inline in the terminal ---- */
- static int cmd_cd(struct term *t, const char *args)
- {
- term_cd(t, args);
- return 0;
- }
- static int cmd_clear(struct term *t, const char *args)
- {
- (void)args;
- io_clear_screen();
- term_print_prompt(t, 1);
- return 1; /* prompt already printed */
- }
- static int cmd_dbg(struct term *t, const char *args)
- {
- (void)t;
- (void)args;
- program_dbg_main();
- return 0;
- }
- static int cmd_format(struct term *t, const char *args)
- {
- char dev[16];
- unsigned int n = 0;
- const char *p;
- (void)t;
- while (args[n] && args[n] != ' ' && n + 1 < sizeof(dev))
- dev[n] = args[n], n++;
- dev[n] = '\0';
- p = args + n;
- while (*p == ' ')
- p++;
- if (n == 0 || *p) {
- io_putstr("format: usage format <device>\n");
- return 0;
- }
- program_format_main(dev);
- return 0;
- }
- /* `path` builtin: show or set the command search path. */
- static int path_entry_ok(const char *entry)
- {
- unsigned int size = 0;
- int is_dir = 0;
- if (entry[0] != '/')
- return 0;
- return vfs_stat(entry, &size, &is_dir) == 0 && is_dir;
- }
- static int cmd_path(struct term *t, const char *args)
- {
- if (args[0] == '\0') {
- io_putstr("path=");
- io_putstr(t->path);
- io_putchar('\n');
- return 0;
- }
- /* Validate every colon-separated entry (absolute, existing dir) and
- * rebuild the path without stray spaces. */
- {
- char entry[VFS_PATH_MAX];
- char normalized[VFS_PATH_MAX];
- unsigned int i = 0, n = 0, o = 0;
- for (;;) {
- char c = args[i];
- if (c == ':' || c == '\0') {
- entry[n] = '\0';
- if (n > 0) {
- unsigned int k;
- if (!path_entry_ok(entry)) {
- io_putstr("path: not a directory: ");
- io_putstr(entry);
- io_putchar('\n');
- return 0;
- }
- if (o > 0) {
- if (o + 1 >= sizeof(normalized)) {
- io_putstr("path: too long\n");
- return 0;
- }
- normalized[o++] = ':';
- }
- for (k = 0; entry[k]; k++) {
- if (o + 1 >= sizeof(normalized)) {
- io_putstr("path: too long\n");
- return 0;
- }
- normalized[o++] = entry[k];
- }
- }
- n = 0;
- if (c == '\0')
- break;
- i++;
- continue;
- }
- if (!(c == ' ' && n == 0)) {
- if (n + 1 >= sizeof(entry)) {
- io_putstr("path: entry too long\n");
- return 0;
- }
- entry[n++] = c;
- }
- i++;
- }
- if (o == 0) {
- io_putstr("path: empty\n");
- return 0;
- }
- normalized[o] = '\0';
- for (i = 0; i + 1 < sizeof(t->path) && normalized[i]; i++)
- t->path[i] = normalized[i];
- t->path[i] = '\0';
- }
- io_putstr("path set to ");
- io_putstr(t->path);
- io_putchar('\n');
- return 0;
- }
- /* List the command files found on the search path. */
- static void help_print_path(struct term *t)
- {
- const char *p = t->path;
- while (*p) {
- char dir[VFS_PATH_MAX];
- unsigned int n = 0, col = 0, i;
- while (*p && *p != ':' && n + 1 < sizeof(dir))
- dir[n++] = *p++;
- dir[n] = '\0';
- if (*p == ':')
- p++;
- if (dir[0] == '\0')
- continue;
- io_putstr(dir);
- io_putstr(":\n");
- for (i = 0; ; i++) {
- struct vfs_dirent d;
- unsigned int len = 0;
- int r = vfs_list_dir(dir, i, &d);
- if (r != 1)
- break;
- if (d.is_dir)
- continue;
- while (d.name[len])
- len++;
- if (col > 0 && col + 2 + len > t->screen.cols) {
- io_putchar('\n');
- col = 0;
- }
- if (col > 0) {
- io_putstr(" ");
- col += 2;
- }
- io_putstr(d.name);
- col += len;
- }
- io_putchar('\n');
- }
- }
- static int cmd_help(struct term *t, const char *args)
- {
- (void)args;
- help_print();
- help_print_path(t);
- return 0;
- }
- static int cmd_kexec(struct term *t, const char *args)
- {
- (void)t;
- (void)args;
- if (kexec_busy) {
- io_putstr("kexec: already receiving\n");
- return 0;
- }
- kexec_busy = 1;
- if (process_create("kexec", program_kexec_main) < 0) {
- kexec_busy = 0;
- io_putstr("error: too many processes\n");
- }
- return 0;
- }
- static int cmd_kill(struct term *t, const char *args)
- {
- unsigned int pid = 0;
- int has_digit = 0;
- (void)t;
- while (*args >= '0' && *args <= '9') {
- pid = pid * 10 + (unsigned int)(*args - '0');
- args++;
- has_digit = 1;
- }
- while (*args == ' ')
- args++;
- if (!has_digit || *args) {
- io_putstr("kill: usage kill <pid>\n");
- return 0;
- }
- if (process_kill(pid) < 0)
- io_putstr("kill: no such process\n");
- else
- io_putstr("killed\n");
- return 0;
- }
- static int cmd_poweroff(struct term *t, const char *args)
- {
- (void)t;
- (void)args;
- program_poweroff_main();
- return 0;
- }
- static int cmd_ps(struct term *t, const char *args)
- {
- (void)t;
- (void)args;
- process_ps();
- return 0;
- }
- static int cmd_reboot(struct term *t, const char *args)
- {
- (void)t;
- (void)args;
- program_reboot_main();
- return 0;
- }
- static int cmd_run(struct term *t, const char *args)
- {
- (void)t;
- if (args[0] == 's' && args[1] == 'e' && args[2] == 'r' &&
- args[3] == 'i' && args[4] == 'a' && args[5] == 'l' &&
- args[6] == '\0') {
- if (process_create_arg("run_serial", program_run_serial_main, "serial") < 0)
- io_putstr("error: too many processes\n");
- } else {
- io_putstr("run: not available in windowed mode yet\n");
- }
- return 0;
- }
- static int cmd_serial(struct term *t, const char *args)
- {
- (void)t;
- if (args[0] == 't' && args[1] == 'e' && args[2] == 's' && args[3] == 't' &&
- args[4] == '\0') {
- if (process_create_arg("serial_loop", program_serial_loop_main, "test") < 0)
- io_putstr("error: too many processes\n");
- } else if (args[0] == 'r' && args[1] == 'e' && args[2] == 'c' &&
- args[3] == 'v' && args[4] == '\0') {
- program_serial_recv_main("recv");
- } else {
- io_putstr("serial: usage serial test|recv\n");
- }
- return 0;
- }
- /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
- static void redraw_line_from(const char *line, unsigned int line_len,
- unsigned int line_pos, unsigned int line_start, unsigned int from)
- {
- unsigned int i;
- io_set_cursor(line_start + from);
- for (i = from; i < line_len; i++)
- io_putchar(line[i]);
- io_putchar(' ');
- io_set_cursor(line_start + line_pos);
- }
- /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
- static void redraw_line_clear(const char *line, unsigned int line_len,
- unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
- {
- unsigned int i;
- io_set_cursor(line_start);
- for (i = 0; i < line_len; i++)
- io_putchar(line[i]);
- for (i = line_len; i <= clear_through; i++)
- io_putchar(' ');
- io_set_cursor(line_start + line_pos);
- }
- /* Copy history entry (1 = newest) into t->line, redraw from start. */
- static void history_load_into_line(struct term *t, unsigned int browse)
- {
- unsigned int slot, len, old_len;
- const char *src;
- if (browse < 1 || browse > t->history_count)
- return;
- old_len = t->line_len;
- slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
- src = t->history[slot];
- for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
- t->line[len] = src[len];
- t->line[len] = '\0';
- t->line_len = len;
- t->line_pos = len;
- redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
- }
- /* ---- Tab completion ---- */
- static char lower_ch(char c)
- {
- return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
- }
- static int prefix_ci(const char *s, const char *prefix, unsigned int plen)
- {
- unsigned int i;
- for (i = 0; i < plen; i++)
- if (lower_ch(s[i]) != lower_ch(prefix[i]))
- return 0;
- return 1;
- }
- /* "HELLO TXT" -> "hello.txt" (a completion-ready 8.3 display name). */
- static void tab_display_name(const char *n, char *out)
- {
- unsigned int bl = 0, el = 0, i, j = 0;
- for (i = 0; i < 8 && n[i] && n[i] != ' '; i++)
- bl = i + 1;
- for (i = 8; i < 11 && n[i] && n[i] != ' '; i++)
- el = i - 8 + 1;
- for (i = 0; i < bl; i++)
- out[j++] = lower_ch(n[i]);
- if (el) {
- out[j++] = '.';
- for (i = 0; i < el; i++)
- out[j++] = lower_ch(n[8 + i]);
- }
- out[j] = '\0';
- }
- /* Shared completion state (fat_list_dir takes no context pointer). */
- static char tab_prefix[16];
- static unsigned int tab_prefix_len;
- static unsigned int tab_count;
- static unsigned int tab_lcp_len;
- static char tab_lcp[16];
- static int tab_single_dir;
- static int tab_raw_83; /* current dir holds raw 8.3 names */
- static char tab_scan_path[VFS_PATH_MAX]; /* dir of the last tab_scan_dir() */
- static unsigned int tab_col;
- static unsigned int tab_width;
- /* Display form of a name: FAT's raw 8.3 gets "NAME EXT" -> "name.ext",
- * everything else (ramfs) is just lowercased. */
- static void tab_disp_name(const char *name, char *out)
- {
- unsigned int i;
- if (tab_raw_83) {
- tab_display_name(name, out);
- return;
- }
- for (i = 0; name[i] && i < 15; i++)
- out[i] = lower_ch(name[i]);
- out[i] = '\0';
- }
- static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
- {
- char disp[16];
- unsigned int i, n;
- (void)size;
- if (name[0] == '.') /* "." / ".." */
- return;
- tab_disp_name(name, disp);
- if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
- return;
- n = 0;
- while (disp[n])
- n++;
- if (tab_count == 0) {
- for (i = 0; i < n; i++)
- tab_lcp[i] = disp[i];
- tab_lcp_len = n;
- tab_single_dir = is_dir;
- } else {
- i = 0;
- while (i < tab_lcp_len && i < n && tab_lcp[i] == disp[i])
- i++;
- tab_lcp_len = i;
- }
- tab_count++;
- }
- static void tab_print_one(const char *name, int is_dir)
- {
- unsigned int n = 0, w;
- while (name[n])
- n++;
- w = n + (is_dir ? 1 : 0);
- if (tab_col > 0 && tab_col + 2 + w > tab_width) {
- io_putchar('\n');
- tab_col = 0;
- }
- if (tab_col > 0) {
- io_putstr(" ");
- tab_col += 2;
- }
- io_putstr(name);
- if (is_dir)
- io_putchar('/');
- tab_col += w;
- }
- static void tab_print_cb(const char *name, int is_dir, unsigned int size)
- {
- char disp[16];
- (void)size;
- if (name[0] == '.')
- return;
- tab_disp_name(name, disp);
- if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
- return;
- tab_print_one(disp, is_dir);
- }
- static int tab_scan_dir(const char *abs)
- {
- struct vfs_mount *m;
- char rel[VFS_PATH_MAX];
- unsigned int i;
- if (vfs_resolve(abs, &m, rel, sizeof(rel)) != 0)
- return 0;
- tab_raw_83 = m->ops->raw_83;
- {
- unsigned int j;
- for (j = 0; abs[j] && j + 1 < sizeof(tab_scan_path); j++)
- tab_scan_path[j] = abs[j];
- tab_scan_path[j] = '\0';
- }
- tab_count = 0;
- tab_lcp_len = 0;
- for (i = 0; ; i++) {
- struct vfs_dirent d;
- int r = m->ops->list_dir(m, rel, i, &d);
- if (r != 1)
- break;
- tab_collect_cb(d.name, (int)d.is_dir, d.size);
- }
- return (int)tab_count;
- }
- static int tab_scan_mounts(void)
- {
- unsigned int i;
- tab_count = 0;
- tab_lcp_len = 0;
- for (i = 0; i < vfs_mount_count; i++) {
- const char *name = vfs_mounts[i].name;
- unsigned int n = 0, j;
- while (name[n])
- n++;
- if (!prefix_ci(name, tab_prefix, tab_prefix_len))
- continue;
- if (tab_count == 0) {
- for (j = 0; j < n; j++)
- tab_lcp[j] = name[j];
- tab_lcp_len = n;
- } else {
- j = 0;
- while (j < tab_lcp_len && j < n && tab_lcp[j] == name[j])
- j++;
- tab_lcp_len = j;
- }
- tab_count++;
- }
- return (int)tab_count;
- }
- /* Replace line[start,end) with ins, keep the tail and redraw. */
- static void term_replace(struct term *t, unsigned int start, unsigned int end,
- const char *ins)
- {
- unsigned int inslen = 0, tail, i, old_len = t->line_len;
- while (ins[inslen])
- inslen++;
- tail = old_len - end;
- if (start + inslen + tail >= LINE_MAX)
- return;
- for (i = 0; i < tail; i++)
- t->line[start + inslen + i] = t->line[end + i];
- for (i = 0; i < inslen; i++)
- t->line[start + i] = ins[i];
- t->line_len = start + inslen + tail;
- t->line[t->line_len] = '\0';
- t->line_pos = start + inslen;
- redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start,
- old_len > t->line_len ? old_len : t->line_len);
- }
- /* List the ambiguous matches below the line, then redraw prompt + line. */
- static void term_tab_ambiguous(struct term *t, int mounts)
- {
- unsigned int i;
- io_putchar('\n');
- tab_col = 0;
- tab_width = t->screen.cols;
- if (mounts) {
- for (i = 0; i < vfs_mount_count; i++) {
- const char *name = vfs_mounts[i].name;
- if (prefix_ci(name, tab_prefix, tab_prefix_len))
- tab_print_one(name, 1);
- }
- } else {
- for (i = 0; ; i++) {
- struct vfs_dirent d;
- int r = vfs_list_dir(tab_scan_path, i, &d);
- if (r != 1)
- break;
- tab_print_cb(d.name, (int)d.is_dir, d.size);
- }
- }
- io_putchar('\n');
- term_print_prompt(t, 1);
- redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
- }
- /* Command-name completion state: fed from the registry rows and the PATH. */
- struct compl_state {
- const char *token;
- unsigned int tl;
- unsigned int count;
- unsigned int lcp_len;
- char lcp[16];
- char best[16];
- };
- static void compl_add(struct compl_state *st, const char *name)
- {
- unsigned int nl = 0, j;
- while (name[nl])
- nl++;
- if (nl < st->tl || !prefix_ci(name, st->token, st->tl))
- return;
- if (st->count == 0) {
- for (j = 0; j < nl && j < sizeof(st->lcp) - 1; j++)
- st->lcp[j] = name[j];
- st->lcp[j] = '\0';
- st->lcp_len = j;
- for (j = 0; j < nl && j < sizeof(st->best) - 1; j++)
- st->best[j] = name[j];
- st->best[j] = '\0';
- } else {
- for (j = 0; j < st->lcp_len && j < nl && st->lcp[j] == name[j]; j++)
- ;
- st->lcp_len = j;
- }
- st->count++;
- }
- static void compl_print_one(const char *name, unsigned int tl, const char *token)
- {
- char buf[16];
- unsigned int nl = 0, j;
- while (name[nl])
- nl++;
- if (nl < tl || !prefix_ci(name, token, tl))
- return;
- for (j = 0; j < nl && j < 15; j++)
- buf[j] = name[j];
- buf[j] = '\0';
- tab_print_one(buf, 0);
- }
- static void compl_consider(struct compl_state *st, const char *name, int print)
- {
- if (print)
- compl_print_one(name, st->tl, st->token);
- else
- compl_add(st, name);
- }
- /* Visit every command-name candidate: registry rows + files on the PATH. */
- static void compl_visit(struct term *t, struct compl_state *st, int print)
- {
- const char *p;
- unsigned int i;
- for (i = 0; i < NCMDS; i++)
- compl_consider(st, cmds[i].name, print);
- p = t->path;
- while (*p) {
- char dir[VFS_PATH_MAX];
- unsigned int n = 0;
- while (*p && *p != ':' && n + 1 < sizeof(dir))
- dir[n++] = *p++;
- dir[n] = '\0';
- if (*p == ':')
- p++;
- if (dir[0] == '\0')
- continue;
- for (i = 0; ; i++) {
- struct vfs_dirent d;
- int r = vfs_list_dir(dir, i, &d);
- if (r != 1)
- break;
- if (!d.is_dir)
- compl_consider(st, d.name, print);
- }
- }
- }
- /* Complete the token at the cursor: command name in the first token, a VFS
- * path afterwards. One match completes, several extend to the common
- * prefix or are listed below the line. */
- static void term_complete(struct term *t)
- {
- char token[LINE_MAX + 1];
- char ins[LINE_MAX + 1];
- char dir[VFS_PATH_MAX];
- unsigned int start = t->line_pos, end, tl, i;
- while (start > 0 && t->line[start - 1] != ' ')
- start--;
- end = t->line_pos;
- while (end < t->line_len && t->line[end] != ' ')
- end++;
- tl = end - start;
- for (i = 0; i < tl; i++)
- token[i] = t->line[start + i];
- token[tl] = '\0';
- /* First token -> command name. */
- {
- int first = 1;
- for (i = 0; i < start; i++)
- if (t->line[i] != ' ') {
- first = 0;
- break;
- }
- if (first) {
- struct compl_state st;
- unsigned int j;
- st.token = token;
- st.tl = tl;
- st.count = 0;
- st.lcp_len = 0;
- st.lcp[0] = '\0';
- st.best[0] = '\0';
- compl_visit(t, &st, 0);
- if (st.count == 0)
- return;
- if (st.count == 1) {
- unsigned int nl = 0;
- while (st.best[nl])
- nl++;
- for (j = 0; j < nl && j < LINE_MAX - 1; j++)
- ins[j] = st.best[j];
- ins[j] = ' ';
- ins[j + 1] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- if (st.lcp_len > tl) {
- for (j = 0; j < st.lcp_len; j++)
- ins[j] = st.lcp[j];
- ins[st.lcp_len] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- io_putchar('\n');
- tab_col = 0;
- tab_width = t->screen.cols;
- compl_visit(t, &st, 1);
- io_putchar('\n');
- term_print_prompt(t, 1);
- redraw_line_clear(t->line, t->line_len, t->line_pos,
- t->line_start, t->line_len);
- return;
- }
- }
-
- /* Argument token -> VFS path. */
- {
- char dirpart[LINE_MAX + 1];
- const char *last = 0, *p;
- unsigned int dp, n;
- int mounts = 0;
- for (p = token; *p; p++)
- if (*p == '/')
- last = p;
- if (last) {
- dp = (unsigned int)(last - token) + 1;
- for (i = 0; i < dp; i++)
- dirpart[i] = token[i];
- dirpart[dp] = '\0';
- p = last + 1;
- } else {
- dp = 0;
- dirpart[0] = '\0';
- p = token;
- }
- {
- unsigned int k = 0;
- while (*p && k < 15)
- tab_prefix[k++] = *p++;
- tab_prefix[k] = '\0';
- tab_prefix_len = k;
- }
- if (dirpart[0] == '/' && dirpart[1] == '\0')
- mounts = 1;
- if (dirpart[0] == '/') {
- if (dp >= VFS_PATH_MAX)
- return;
- for (i = 0; dirpart[i]; i++)
- dir[i] = dirpart[i];
- dir[i] = '\0';
- } else {
- if (vfs_join_path(t->cwd, dirpart, dir, sizeof(dir)) != 0)
- return;
- }
- n = mounts ? (unsigned int)tab_scan_mounts()
- : (unsigned int)tab_scan_dir(dir);
- if (n == 0)
- return;
- if (dp + tab_lcp_len + 2 > LINE_MAX)
- return;
- if (n == 1) {
- for (i = 0; i < dp; i++)
- ins[i] = dirpart[i];
- for (i = 0; i < tab_lcp_len; i++)
- ins[dp + i] = tab_lcp[i];
- ins[dp + tab_lcp_len] = (mounts || tab_single_dir) ? '/' : ' ';
- ins[dp + tab_lcp_len + 1] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- if (tab_lcp_len > tab_prefix_len) {
- for (i = 0; i < dp; i++)
- ins[i] = dirpart[i];
- for (i = 0; i < tab_lcp_len; i++)
- ins[dp + i] = tab_lcp[i];
- ins[dp + tab_lcp_len] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- term_tab_ambiguous(t, mounts);
- }
- }
- /* Push current line to history if non-empty (and not duplicate of newest). */
- static void history_push(struct term *t, const char *line, unsigned int line_len)
- {
- unsigned int i, slot;
- int all_spaces = 1;
- if (line_len == 0)
- return;
- for (i = 0; i < line_len; i++) {
- if (line[i] != ' ') {
- all_spaces = 0;
- break;
- }
- }
- if (all_spaces)
- return;
- if (t->history_count > 0) {
- slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
- for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
- if (t->history[slot][i] != line[i])
- break;
- }
- if (i == line_len && t->history[slot][i] == '\0')
- return; /* duplicate of newest */
- }
- slot = t->history_next;
- for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
- t->history[slot][i] = line[i];
- t->history[slot][i] = '\0';
- t->history_next = (t->history_next + 1) % HISTORY_SIZE;
- if (t->history_count < HISTORY_SIZE)
- 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];
- unsigned int size = 0;
- int is_dir = 0;
- 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;
- }
- r = vfs_stat(abs, &size, &is_dir);
- if (r == VFS_EIO) {
- io_putstr("cd: disk error\n");
- return;
- }
- if (r != 0 || !is_dir) {
- 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';
- }
- /* 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
- t->fg_pid = pid;
- }
- 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
- t->fg_pid = pid;
- }
- /* PATH search: try each directory in t->path for `word`; the first hit is
- * loaded and spawned as a ring-3 process. Returns 1 when the command was
- * found (spawned, or reported as bad), 0 when no directory has it. */
- static int term_path_spawn(struct term *t, const char *word, const char *args)
- {
- const char *p = t->path;
- while (*p) {
- char dir[VFS_PATH_MAX];
- char full[VFS_PATH_MAX + LINE_MAX];
- struct vfs_file f;
- struct vfs_mount *m = 0;
- unsigned int n = 0;
- int r, pid;
- while (*p && *p != ':' && n + 1 < sizeof(dir))
- dir[n++] = *p++;
- dir[n] = '\0';
- if (*p == ':')
- p++;
- if (dir[0] == '\0')
- continue;
- if (vfs_join_path(dir, word, full, sizeof(full)) != 0)
- continue;
- r = vfs_load_file(full, &f, &m);
- if (r != 0)
- continue;
- pid = process_create_user(word, f.data, f.size, args, t->cwd);
- vfs_release_file(m, &f);
- if (pid == -2) {
- io_putstr(word);
- io_putstr(": not an executable\n");
- } else if (pid < 0) {
- io_putstr("error: too many processes\n");
- } else {
- t->fg_pid = pid;
- }
- return 1;
- }
- return 0;
- }
- void term_run(struct term *t)
- {
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- t->history_browse = 0;
- t->fg_pid = -1;
- if (t->path[0] == '\0') {
- const char *def = "/ramfs/bin";
- unsigned int i;
- for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
- t->path[i] = def[i];
- t->path[i] = '\0';
- }
- 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 {
- process_sleep_ms(10);
- }
- continue;
- }
- {
- int k = term_key_pop(t);
- if (k) {
- 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);
- }
- } else if (k == IO_KEY_RIGHT) {
- if (t->line_pos < t->line_len) {
- t->line_pos++;
- io_set_cursor(t->line_start + t->line_pos);
- }
- } else if (k == IO_KEY_UP) {
- if (t->history_count > 0) {
- if (t->history_browse < t->history_count)
- t->history_browse++;
- history_load_into_line(t, t->history_browse);
- }
- } else if (k == IO_KEY_DOWN) {
- if (t->history_browse > 0) {
- t->history_browse--;
- if (t->history_browse == 0) {
- unsigned int clear_len = t->line_len;
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
- } else {
- history_load_into_line(t, t->history_browse);
- }
- }
- } else if (k == '\b') {
- if (t->line_pos > 0) {
- unsigned int i;
- t->line_pos--;
- for (i = t->line_pos; i < t->line_len; i++)
- t->line[i] = t->line[i + 1];
- t->line_len--;
- t->line[t->line_len] = '\0';
- redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
- }
- } else if (k == '\t') {
- term_complete(t);
- } else if (k == '\n') {
- int skip_prompt = 0;
- io_putchar('\n');
- history_push(t, t->line, t->line_len);
- t->history_browse = 0;
- {
- char word[LINE_MAX + 1];
- char args[LINE_MAX + 1];
- const struct kcmd *c;
- term_split_line(t, word, sizeof(word),
- args, sizeof(args));
- c = cmd_lookup(t->line, t->line_len);
- if (c && (c->flags & CMD_BUILTIN)) {
- skip_prompt = term_run_command(t, c, args);
- } else if (word[0] != '\0' &&
- term_path_spawn(t, word, args)) {
- /* spawned from the search path */
- } else if (c) {
- skip_prompt = term_run_command(t, c, args);
- } else if (word[0] != '\0') {
- io_putstr("command not found\n");
- }
- }
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- if (!skip_prompt && t->fg_pid < 0) {
- term_print_prompt(t, 0);
- }
- } else {
- unsigned char u = (unsigned char)k;
- if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
- unsigned int i;
- t->history_browse = 0;
- for (i = t->line_len; i > t->line_pos; i--)
- t->line[i] = t->line[i - 1];
- t->line[t->line_pos] = (char)k;
- t->line_len++;
- t->line[t->line_len] = '\0';
- t->line_pos++;
- redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
- }
- }
- } else {
- process_sleep_ms(10);
- }
- }
- }
- }
- struct kcmd_ctx {
- char name[16];
- char args[VFS_PATH_MAX];
- };
- static const struct kcmd *kcmd_find(const char *name)
- {
- unsigned int i;
- for (i = 0; i < NCMDS; i++)
- if (cmds[i].name[0] == name[0]) {
- unsigned int j = 0;
- while (cmds[i].name[j] && name[j] && cmds[i].name[j] == name[j])
- j++;
- if (!cmds[i].name[j] && !name[j])
- return &cmds[i];
- }
- return 0;
- }
- static void kcmd_entry(void *arg)
- {
- struct kcmd_ctx *ctx = (struct kcmd_ctx *)arg;
- const struct kcmd *c = kcmd_find(ctx->name);
- if (c) {
- if (c->kind == CK_CUSTOM) {
- struct term *t = (struct term *)mm_alloc_pages(2);
- unsigned int i;
- if (!t) {
- io_putstr("kcmd: out of memory\n");
- } else {
- unsigned char *p = (unsigned char *)t;
- for (i = 0; i < 2 * PAGE_SIZE; i++)
- p[i] = 0;
- t->fg_pid = -1;
- for (i = 0; i + 1 < sizeof(t->cwd) &&
- procs[current_pid].cwd[i]; i++)
- t->cwd[i] = procs[current_pid].cwd[i];
- t->cwd[i] = '\0';
- {
- const char *def = "/ramfs/bin";
- for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
- t->path[i] = def[i];
- t->path[i] = '\0';
- }
- c->custom(t, ctx->args);
- mm_free_page((unsigned int)t);
- mm_free_page((unsigned int)t + PAGE_SIZE);
- }
- } else if (c->kind == CK_SPAWN) {
- if (c->fixed)
- c->fn_arg(c->fixed);
- else
- c->fn();
- } else if (c->kind == CK_SPAWN_ARG) {
- c->fn_arg(ctx->args);
- }
- } else {
- io_putstr(ctx->name);
- io_putstr(": no such kernel command\n");
- }
- kfree(ctx);
- process_exit();
- }
- int term_kcmd_spawn(const char *name, const char *args, unsigned int tty)
- {
- const struct kcmd *c = kcmd_find(name);
- struct kcmd_ctx *ctx;
- unsigned int i;
- int pid;
- if (!c || (c->flags & CMD_BUILTIN))
- return -1;
- ctx = (struct kcmd_ctx *)kmalloc(sizeof(*ctx));
- if (!ctx)
- return -1;
- for (i = 0; i + 1 < sizeof(ctx->name) && name[i]; i++)
- ctx->name[i] = name[i];
- ctx->name[i] = '\0';
- for (i = 0; i + 1 < sizeof(ctx->args) && args[i]; i++)
- ctx->args[i] = args[i];
- ctx->args[i] = '\0';
- pid = process_create_ptr("kcmd", kcmd_entry, ctx);
- if (pid < 0) {
- kfree(ctx);
- return -1;
- }
- if (tty)
- process_set_tty((unsigned int)pid, (int)tty);
- return pid;
- }
- void term_entry(void *arg)
- {
- struct term *t = (struct term *)arg;
- process_set_screen(&t->screen);
- term_run(t);
- }
|