CE Implement positional arguments
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 36s
Build documentation / build-and-deploy (push) Successful in 1m23s

This commit is contained in:
2026-04-12 22:05:26 +02:00
parent 169fba6ee7
commit 9f216ffc49
4 changed files with 66 additions and 1 deletions

20
ce/ce.c
View File

@@ -8,6 +8,7 @@
#include <in_gb.h>
#include <in_input.h>
#include <malloc.h>
#include <minmax.h>
#include <mprintf.h>
#include <path.h>
#include <printf.h>
@@ -77,8 +78,23 @@ static bool split_lines_cb (void* ctx, const char* start, size_t len) {
return true;
}
static bool split_args_cb (void* ctx, const char* start, size_t len) {
(void)ctx;
if (posvar_count >= POSVAR_MAX)
return false;
struct posvar* posvar = &posvars[posvar_count++];
memset (posvar, 0, sizeof (*posvar));
memcpy (posvar->buf, start, min (len, sizeof (posvar->buf) - 1));
return true;
}
void app_main (void) {
char scriptpathbuf[PATH_MAX];
char posvarsbuf[2048];
char line[1024];
memset (scriptpathbuf, 0, sizeof (scriptpathbuf));
@@ -87,6 +103,10 @@ void app_main (void) {
if (has_script == ST_OK) {
interactive_mode = false;
if (env_get (process_get_pgid (), "args", (void*)posvarsbuf, sizeof (posvarsbuf)) == ST_OK) {
strtokenize (posvarsbuf, ' ', NULL, &split_args_cb);
}
char volume[VOLUME_MAX];
const char* path;
int ret;

View File

@@ -30,6 +30,9 @@ static bool run = true;
bool interactive_mode;
struct posvar posvars[POSVAR_MAX];
int posvar_count = 0;
bool interp_is_running (void) { return run; }
void interp_shutdown (void) { run = false; }

View File

@@ -3,8 +3,15 @@
#include "context.h"
#include "parser.h"
#include <list.h>
#include <stdbool.h>
#define POSVAR_MAX 64
struct posvar {
char buf[0x80];
};
void execute (struct ast_node* root, struct context* context, bool run_bg);
bool interp_is_running (void);
@@ -13,4 +20,7 @@ void interp_shutdown (void);
extern bool interactive_mode;
extern struct posvar posvars[POSVAR_MAX];
extern int posvar_count;
#endif // _INTERP_H

View File

@@ -5,6 +5,7 @@
#include <arena.h>
#include <list.h>
#include <mprintf.h>
#include <strconv.h>
#include <string.h>
static struct parse_rule parse_rules[] = {
@@ -162,6 +163,29 @@ void tokenize (struct list_node_link** tokens, const char* text) {
if (*p == '#')
return;
if (*p == '$') {
p++;
struct token* token = arena_malloc (&arena, sizeof (*token));
memset (token, 0, sizeof (*token));
size_t i = 0;
char numbuf[TOKEN_MAX];
while (*p && isdigit (*p)) {
numbuf[i++] = *p;
p++;
}
numbuf[i] = '\0';
uint32_t idx = str_to_uint32 (numbuf) % posvar_count;
struct posvar* posvar = &posvars[idx];
memcpy (token->buffer, posvar->buf, sizeof (posvar->buf));
token->class = TOKEN_CLASS_WORD;
list_append (*tokens, &token->tokens_link);
continue;
}
if (*p == '"') {
p++;
struct token* token = arena_malloc (&arena, sizeof (*token));
@@ -218,7 +242,15 @@ void tokenize (struct list_node_link** tokens, const char* text) {
memset (token, 0, sizeof (*token));
size_t i = 0;
while (*p && !isspace (*p) && *p != '(' && *p != ')' && *p != ';' && *p != '>' && *p != '"') {
while (*p &&
!isspace (*p) &&
*p != '(' &&
*p != ')' &&
*p != ';' &&
*p != '>' &&
*p != '"' &&
*p != '#' &&
*p != '$') {
if (i < TOKEN_MAX - 1)
token->buffer[i++] = *p;
p++;