CE implement running commands from script files

This commit is contained in:
2026-04-07 15:47:27 +02:00
parent 3d886f2039
commit bef3b79502
5 changed files with 84 additions and 10 deletions

61
ce/ce.c
View File

@@ -4,12 +4,15 @@
#include "strbuf.h" #include "strbuf.h"
#include <arena.h> #include <arena.h>
#include <debugconsole.h> #include <debugconsole.h>
#include <filereader.h>
#include <in_gb.h> #include <in_gb.h>
#include <in_input.h> #include <in_input.h>
#include <malloc.h> #include <malloc.h>
#include <mprintf.h> #include <mprintf.h>
#include <path.h>
#include <printf.h> #include <printf.h>
#include <process_self.h> #include <process_self.h>
#include <status.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@@ -59,17 +62,65 @@ static void exec_line (const char* line) {
parse_and_execute (tokens); parse_and_execute (tokens);
} }
static bool split_lines_cb (void* ctx, const char* start, size_t len) {
(void)ctx;
char* line = malloc (len + 1);
memcpy (line, start, len);
line[len] = '\0';
if (line[0] != '\0')
exec_line (line);
return true;
}
void app_main (void) { void app_main (void) {
libprocess_self_init (); libprocess_self_init ();
char scriptpathbuf[PATH_MAX];
char line[1024]; char line[1024];
while (interp_is_running ()) { memset (scriptpathbuf, 0, sizeof (scriptpathbuf));
memset (line, 0, sizeof (line)); int has_script = env_get (process_get_pgid (), "s", (void*)scriptpathbuf, sizeof (scriptpathbuf));
in_stream_read_line (PROMPT, line, sizeof (line) - 1); if (has_script == ST_OK) {
char volume[VOLUME_MAX];
const char* path;
int ret;
if (line[0] != '\0') if (!path_parse (scriptpathbuf, volume, &path)) {
exec_line (line); mprintf ("ERROR bad path '%s'\n", scriptpathbuf);
return;
}
struct filereader fr;
if ((ret = filereader_init (&fr, volume, path)) < 0) {
mprintf ("ERROR could not initialize filereader for '%s:%s'\n", volume, path);
return;
}
char* buffer = malloc (fr.file_size + 1);
memset (buffer, 0, fr.file_size + 1);
if ((ret = filereader_read (&fr, (uint8_t*)buffer, fr.file_size)) < 0) {
mprintf ("ERROR could not read script: '%s:%s'\n", volume, path);
free (buffer);
filereader_fini (&fr);
return;
}
str_split_lines (buffer, strlen (buffer), NULL, &split_lines_cb);
free (buffer);
} else {
while (interp_is_running ()) {
memset (line, 0, sizeof (line));
in_stream_read_line (PROMPT, line, sizeof (line) - 1);
if (line[0] != '\0')
exec_line (line);
}
} }
} }

1
etc/doc.txt Normal file
View File

@@ -0,0 +1 @@
Other files for the system disk go here...

1
etc/init.cmd Normal file
View File

@@ -0,0 +1 @@
echo HELLO!

View File

@@ -12,7 +12,7 @@
static int ce_pgid; static int ce_pgid;
void receiver (void* arg) { static void receiver (void* arg) {
(void)arg; (void)arg;
char recv[RECV_MAX]; char recv[RECV_MAX];
@@ -27,9 +27,7 @@ void receiver (void* arg) {
} }
} }
void app_main (void) { static void run_ce_interactive (void) {
debug_printf ("Init process is running. Starting user shell...\n");
int ce_pid = exec ("sys", "/ce"); int ce_pid = exec ("sys", "/ce");
ce_pgid = get_procgroup (ce_pid); ce_pgid = get_procgroup (ce_pid);
@@ -44,3 +42,26 @@ void app_main (void) {
stream_write (ce_pgid, STREAM_IN, &ch, 1); stream_write (ce_pgid, STREAM_IN, &ch, 1);
} }
} }
static void ce_run_init_script (void) {
int ce_pid = exec_partial ("sys", "/ce");
ce_pgid = get_procgroup (ce_pid);
const char* script_path = "sys:/init.cmd";
env_set (ce_pgid, "s", (void*)script_path, strlen (script_path) + 1);
struct process_data* recv_pdata = process_spawn (&receiver, NULL);
exec_partial_fini (ce_pgid);
wait_for_pid (ce_pid);
kill (recv_pdata->pid);
process_data_free (recv_pdata);
}
void app_main (void) {
debug_printf ("Init process is running. Starting user shell...\n");
ce_run_init_script ();
run_ce_interactive ();
}

View File

@@ -1,5 +1,5 @@
exe := $(foreach d,$(apps),$(wildcard $(d)/$(d))) exe := $(foreach d,$(apps),$(wildcard $(d)/$(d)))
extra := LICENSE.txt extra := LICENSE.txt $(wildcard etc/*)
all_dist: mop3dist.tar.lz4 all_dist: mop3dist.tar.lz4