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 <arena.h>
#include <debugconsole.h>
#include <filereader.h>
#include <in_gb.h>
#include <in_input.h>
#include <malloc.h>
#include <mprintf.h>
#include <path.h>
#include <printf.h>
#include <process_self.h>
#include <status.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
@@ -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);
}
}
}