Implement waiting for process, CE add command cancelation, rctx many cpus
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m27s

This commit is contained in:
2026-03-01 22:59:04 +01:00
parent 858e55118b
commit 9043c4f9ec
18 changed files with 308 additions and 89 deletions

117
ce/ce.c
View File

@@ -14,6 +14,9 @@
#include <system.h>
#include <terminal_cursor.h>
#define CTRL(x) ((x) - '@')
#define CPRINTF_BUF_MAX (1024 * 16)
static struct arena arena;
struct strbuf {
@@ -45,8 +48,6 @@ struct context {
struct strbuf strbuf;
};
#define CPRINTF_BUF_MAX 4096
void cprintf (struct context* context, const char* fmt, ...) {
va_list args;
va_start (args, fmt);
@@ -264,8 +265,26 @@ static int e_pgid;
static bool run = true;
static void putch (char ch) { mail_send (e_pgid, &ch, 1); }
void putchar_ (char ch) { putch (ch); }
void putchar_ (char ch) { (void)ch; }
void mprintf (const char* fmt, ...) {
va_list args;
va_start (args, fmt);
char* buf = malloc (CPRINTF_BUF_MAX);
if (buf == NULL) {
va_end (args);
return;
}
int len = vsnprintf (buf, CPRINTF_BUF_MAX, fmt, args);
va_end (args);
mail_send (e_pgid, buf, len);
free (buf);
}
static void tokenize (struct list_node_link** tokens, const char* text) {
const char* p = text;
@@ -311,7 +330,7 @@ static void tokenize (struct list_node_link** tokens, const char* text) {
continue;
}
printf ("ERROR unknown character '%c'\n", *p);
mprintf ("ERROR unknown character '%c'\r\n", *p);
p++;
}
}
@@ -331,7 +350,7 @@ static void parse_and_execute (struct list_node_link* tokens) {
execute (root, &context);
if (context.strbuf.items != NULL)
printf ("%.*s", (int)context.strbuf.count, context.strbuf.items);
mprintf ("%.*s", (int)context.strbuf.count, context.strbuf.items);
}
}
}
@@ -350,12 +369,12 @@ static void mkfile (struct context* context, char** file_paths, size_t files_cou
const char* file_path = file_paths[i];
if (!path_parse (file_path, volume, &path)) {
cprintf (context, "ERROR bad path '%s'\n", file_path);
cprintf (context, "ERROR bad path '%s'\r\n", file_path);
continue;
}
if ((ret = volume_open (volume)) < 0) {
cprintf (context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]);
cprintf (context, "ERROR could not open volume '%s': %s\r\n", volume, str_status[-ret]);
continue;
}
@@ -375,12 +394,12 @@ static void cat (struct context* context, char** file_paths, size_t files_count)
const char* file_path = file_paths[i];
if (!path_parse (file_path, volume, &path)) {
cprintf (context, "ERROR bad path '%s'\n", file_path);
cprintf (context, "ERROR bad path '%s'\r\n", file_path);
continue;
}
if ((ret = volume_open (volume)) < 0) {
cprintf (context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]);
cprintf (context, "ERROR could not open volume '%s': %s\r\n", volume, str_status[-ret]);
continue;
}
@@ -396,7 +415,7 @@ static void cat (struct context* context, char** file_paths, size_t files_count)
memset (buffer, 0, desc.size + 1);
read_file (path, 0, (uint8_t*)buffer, desc.size);
cprintf (context, "%s\n", buffer);
cprintf (context, "%s\r\n", buffer);
close:
if (buffer != NULL)
@@ -413,26 +432,26 @@ static void ls (struct context* context, const char* path_string) {
struct dir_entry entry;
if (!path_parse (path_string, volume, &path)) {
cprintf (context, "ERROR bad path '%s'\n", path_string);
cprintf (context, "ERROR bad path '%s'\r\n", path_string);
return;
}
if ((ret = volume_open (volume)) < 0) {
cprintf (context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]);
cprintf (context, "ERROR could not open volume '%s': %s\r\n", volume, str_status[-ret]);
return;
}
describe (path, &desc);
if (desc.type != FS_DIR) {
cprintf (context, "ERROR '%s' is not a directory\n", path_string);
cprintf (context, "ERROR '%s' is not a directory\r\n", path_string);
volume_close ();
return;
}
size_t entries = desc.size;
cprintf (context, "total entries: %zu\n", entries);
cprintf (context, "\n");
cprintf (context, "total entries: %zu\r\n", entries);
cprintf (context, "\r\n");
for (size_t entry_num = 0; entry_num < entries; entry_num++) {
memset (&desc, 0, sizeof (desc));
@@ -441,7 +460,7 @@ static void ls (struct context* context, const char* path_string) {
read_dir_entry (path, &entry, entry_num);
describe (entry.path, &desc);
cprintf (context, "%c %-40s %-40zu\n", (desc.type == FS_DIR ? 'D' : 'F'), entry.path,
cprintf (context, "%c %-40s %-40zu\r\n", (desc.type == FS_DIR ? 'D' : 'F'), entry.path,
desc.size);
}
@@ -450,17 +469,33 @@ static void ls (struct context* context, const char* path_string) {
static void quit1 (struct context* context) {
run = false;
cprintf (context, "Goodbye!\n");
cprintf (context, "Goodbye!\r\n");
}
static void help (struct context* context) {
cprintf (context, "Available commands:\n");
cprintf (context, "echo <word1> <word2> <word3> ...\n");
cprintf (context, "help\n");
cprintf (context, "cat <file path>\n");
cprintf (context, "ls <directory path>\n");
cprintf (context, "mkfile <file path>\n");
cprintf (context, "quit\n");
cprintf (context, "Available commands:\r\n");
cprintf (context, "echo <word1> <word2> <word3> ...\r\n");
cprintf (context, "help\r\n");
cprintf (context, "cat <file path>\r\n");
cprintf (context, "ls <directory path>\r\n");
cprintf (context, "mkfile <file path>\r\n");
cprintf (context, "quit\r\n");
}
static void cmd_cancel_proc (void) {
int pid = *(int*)argument_ptr ();
char ch = 0;
for (;;) {
mail_receive (&ch, 1);
if (ch == CTRL ('C'))
break;
}
kill (pid);
quit ();
}
static void execute_cmd (struct ast_cmd* cmd, struct context* context) {
@@ -477,7 +512,27 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) {
} else if (strcmp (cmd->name, "mkfile") == 0) {
mkfile (context, cmd->args, cmd->arg_count);
} else {
cprintf (context, "ERROR unknown command '%s'\n", cmd->name);
/* cprintf (context, "ERROR unknown command '%s'\r\n", cmd->name); */
char volume[VOLUME_MAX];
const char* path;
if (!(path_parse (cmd->name, volume, &path))) {
cprintf (context, "ERROR bad path '%s'\r\n", cmd->name);
return;
}
int pid = exec (volume, path);
if (pid < 0) {
cprintf (context, "ERROR could not run '%s': %s\r\n", cmd->name, str_status[-pid]);
return;
}
int cancel_pid = process_spawn (&cmd_cancel_proc, &pid);
wait_for_pid (pid);
kill (cancel_pid);
}
}
@@ -487,12 +542,12 @@ static void execute_redir (struct ast_redir* redir, struct context* context) {
int ret;
if (!(path_parse (redir->file_path, volume, &path))) {
cprintf (context, "ERROR bad path '%s'\n", redir->file_path);
cprintf (context, "ERROR bad path '%s'\r\n", redir->file_path);
return;
}
if ((ret = volume_open (volume)) < 0) {
cprintf (context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]);
cprintf (context, "ERROR could not open volume '%s': %s\r\n", volume, str_status[-ret]);
return;
}
@@ -543,7 +598,7 @@ void app_main (void) {
memset (line_buffer, 0, sizeof (line_buffer));
while (run) {
printf (PROMPT);
mprintf (PROMPT);
char ch = 0;
for (;;) {
@@ -554,11 +609,11 @@ void app_main (void) {
if (line_cursor < LINE_BUFFER_MAX) {
line_buffer[line_cursor++] = ch;
printf ("%c", ch);
mprintf ("%c", ch);
}
}
printf ("\n");
mprintf ("\r\n");
exec_line (line_buffer);
line_cursor = 0;