CE Implement positional arguments
This commit is contained in:
20
ce/ce.c
20
ce/ce.c
@@ -8,6 +8,7 @@
|
|||||||
#include <in_gb.h>
|
#include <in_gb.h>
|
||||||
#include <in_input.h>
|
#include <in_input.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
#include <minmax.h>
|
||||||
#include <mprintf.h>
|
#include <mprintf.h>
|
||||||
#include <path.h>
|
#include <path.h>
|
||||||
#include <printf.h>
|
#include <printf.h>
|
||||||
@@ -77,8 +78,23 @@ static bool split_lines_cb (void* ctx, const char* start, size_t len) {
|
|||||||
return true;
|
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) {
|
void app_main (void) {
|
||||||
char scriptpathbuf[PATH_MAX];
|
char scriptpathbuf[PATH_MAX];
|
||||||
|
char posvarsbuf[2048];
|
||||||
char line[1024];
|
char line[1024];
|
||||||
|
|
||||||
memset (scriptpathbuf, 0, sizeof (scriptpathbuf));
|
memset (scriptpathbuf, 0, sizeof (scriptpathbuf));
|
||||||
@@ -87,6 +103,10 @@ void app_main (void) {
|
|||||||
if (has_script == ST_OK) {
|
if (has_script == ST_OK) {
|
||||||
interactive_mode = false;
|
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];
|
char volume[VOLUME_MAX];
|
||||||
const char* path;
|
const char* path;
|
||||||
int ret;
|
int ret;
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ static bool run = true;
|
|||||||
|
|
||||||
bool interactive_mode;
|
bool interactive_mode;
|
||||||
|
|
||||||
|
struct posvar posvars[POSVAR_MAX];
|
||||||
|
int posvar_count = 0;
|
||||||
|
|
||||||
bool interp_is_running (void) { return run; }
|
bool interp_is_running (void) { return run; }
|
||||||
|
|
||||||
void interp_shutdown (void) { run = false; }
|
void interp_shutdown (void) { run = false; }
|
||||||
|
|||||||
10
ce/interp.h
10
ce/interp.h
@@ -3,8 +3,15 @@
|
|||||||
|
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include <list.h>
|
||||||
#include <stdbool.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);
|
void execute (struct ast_node* root, struct context* context, bool run_bg);
|
||||||
|
|
||||||
bool interp_is_running (void);
|
bool interp_is_running (void);
|
||||||
@@ -13,4 +20,7 @@ void interp_shutdown (void);
|
|||||||
|
|
||||||
extern bool interactive_mode;
|
extern bool interactive_mode;
|
||||||
|
|
||||||
|
extern struct posvar posvars[POSVAR_MAX];
|
||||||
|
extern int posvar_count;
|
||||||
|
|
||||||
#endif // _INTERP_H
|
#endif // _INTERP_H
|
||||||
|
|||||||
34
ce/parser.c
34
ce/parser.c
@@ -5,6 +5,7 @@
|
|||||||
#include <arena.h>
|
#include <arena.h>
|
||||||
#include <list.h>
|
#include <list.h>
|
||||||
#include <mprintf.h>
|
#include <mprintf.h>
|
||||||
|
#include <strconv.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static struct parse_rule parse_rules[] = {
|
static struct parse_rule parse_rules[] = {
|
||||||
@@ -162,6 +163,29 @@ void tokenize (struct list_node_link** tokens, const char* text) {
|
|||||||
if (*p == '#')
|
if (*p == '#')
|
||||||
return;
|
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 == '"') {
|
if (*p == '"') {
|
||||||
p++;
|
p++;
|
||||||
struct token* token = arena_malloc (&arena, sizeof (*token));
|
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));
|
memset (token, 0, sizeof (*token));
|
||||||
size_t i = 0;
|
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)
|
if (i < TOKEN_MAX - 1)
|
||||||
token->buffer[i++] = *p;
|
token->buffer[i++] = *p;
|
||||||
p++;
|
p++;
|
||||||
|
|||||||
Reference in New Issue
Block a user