From bef3b79502da6a8a84d344d44e5b3df4e33ccf77 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 7 Apr 2026 15:47:27 +0200 Subject: [PATCH] CE implement running commands from script files --- ce/ce.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++----- etc/doc.txt | 1 + etc/init.cmd | 1 + init/init.c | 29 +++++++++++++++++++++---- make/dist.mk | 2 +- 5 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 etc/doc.txt create mode 100644 etc/init.cmd diff --git a/ce/ce.c b/ce/ce.c index 9849c25..c6b74eb 100644 --- a/ce/ce.c +++ b/ce/ce.c @@ -4,12 +4,15 @@ #include "strbuf.h" #include #include +#include #include #include #include #include +#include #include #include +#include #include #include #include @@ -59,17 +62,65 @@ static void exec_line (const char* line) { 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) { libprocess_self_init (); + char scriptpathbuf[PATH_MAX]; char line[1024]; - while (interp_is_running ()) { - memset (line, 0, sizeof (line)); + memset (scriptpathbuf, 0, sizeof (scriptpathbuf)); + 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') - exec_line (line); + if (!path_parse (scriptpathbuf, volume, &path)) { + 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); + } } } diff --git a/etc/doc.txt b/etc/doc.txt new file mode 100644 index 0000000..652719f --- /dev/null +++ b/etc/doc.txt @@ -0,0 +1 @@ +Other files for the system disk go here... diff --git a/etc/init.cmd b/etc/init.cmd new file mode 100644 index 0000000..46f6723 --- /dev/null +++ b/etc/init.cmd @@ -0,0 +1 @@ +echo HELLO! diff --git a/init/init.c b/init/init.c index 416fdfc..2278a66 100644 --- a/init/init.c +++ b/init/init.c @@ -12,7 +12,7 @@ static int ce_pgid; -void receiver (void* arg) { +static void receiver (void* arg) { (void)arg; char recv[RECV_MAX]; @@ -27,9 +27,7 @@ void receiver (void* arg) { } } -void app_main (void) { - debug_printf ("Init process is running. Starting user shell...\n"); - +static void run_ce_interactive (void) { int ce_pid = exec ("sys", "/ce"); ce_pgid = get_procgroup (ce_pid); @@ -44,3 +42,26 @@ void app_main (void) { 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 (); +} diff --git a/make/dist.mk b/make/dist.mk index 59d3aee..c7ce9e3 100644 --- a/make/dist.mk +++ b/make/dist.mk @@ -1,5 +1,5 @@ exe := $(foreach d,$(apps),$(wildcard $(d)/$(d))) -extra := LICENSE.txt +extra := LICENSE.txt $(wildcard etc/*) all_dist: mop3dist.tar.lz4