2026-05-09 19:00:02 +01:00
|
|
|
#include "../syntax.h"
|
|
|
|
|
|
|
|
|
|
#include <fx/encoding.h>
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static bool parse_fword(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
|
2026-05-12 22:58:59 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
if (peek_token_type(ctx) != BSHELL_TOK_WORD_START) {
|
2026-05-12 22:58:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discard_token(ctx);
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_fstring_ast_node *fstring
|
|
|
|
|
= (struct bshell_fstring_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_FSTRING);
|
2026-05-12 22:58:59 +01:00
|
|
|
if (!fstring) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
while (ok) {
|
2026-05-25 10:33:29 +01:00
|
|
|
if (peek_token_type(ctx) == BSHELL_TOK_WORD_END) {
|
2026-05-12 22:58:59 +01:00
|
|
|
discard_token(ctx);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *item = NULL;
|
|
|
|
|
if (peek_token_type(ctx) == BSHELL_TOK_WORD) {
|
|
|
|
|
struct bshell_word_ast_node *n
|
|
|
|
|
= (struct bshell_word_ast_node *)
|
|
|
|
|
bshell_ast_node_create(BSHELL_AST_WORD);
|
2026-05-12 22:58:59 +01:00
|
|
|
if (!n) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
ok = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n->n_value = claim_token(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
item = (struct bshell_ast_node *)n;
|
2026-05-12 22:58:59 +01:00
|
|
|
} else {
|
|
|
|
|
if (!parse_arith_value(ctx, &item)) {
|
|
|
|
|
ok = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_queue_push_back(&fstring->n_elements, &item->n_entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ok) {
|
2026-05-25 10:33:29 +01:00
|
|
|
bshell_ast_node_destroy((struct bshell_ast_node *)fstring);
|
2026-05-12 22:58:59 +01:00
|
|
|
fstring = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)fstring;
|
2026-05-12 22:58:59 +01:00
|
|
|
return ok;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static bool parse_cmdcall_arg(
|
|
|
|
|
struct bshell_parse_ctx *ctx,
|
|
|
|
|
struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-10 14:19:06 +01:00
|
|
|
if (ctx->p_status != BSHELL_SUCCESS) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!tok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *arg = NULL;
|
2026-05-09 19:00:02 +01:00
|
|
|
|
|
|
|
|
switch (tok->tok_type) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_WORD_START:
|
2026-05-12 22:58:59 +01:00
|
|
|
return parse_fword(ctx, out);
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_STR_START:
|
2026-05-12 22:58:59 +01:00
|
|
|
return parse_fstring(ctx, out);
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_WORD: {
|
|
|
|
|
struct bshell_word_ast_node *n
|
|
|
|
|
= (struct bshell_word_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_WORD);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!n) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 21:21:51 +01:00
|
|
|
n->n_value = claim_token(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)n;
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_VAR: {
|
|
|
|
|
struct bshell_var_ast_node *n
|
|
|
|
|
= (struct bshell_var_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_VAR);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!n) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 21:21:51 +01:00
|
|
|
n->n_ident = claim_token(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)n;
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_VAR_SPLAT: {
|
|
|
|
|
struct bshell_var_splat_ast_node *n
|
|
|
|
|
= (struct bshell_var_splat_ast_node *)
|
|
|
|
|
bshell_ast_node_create(BSHELL_AST_VAR_SPLAT);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!n) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 21:21:51 +01:00
|
|
|
n->n_ident = claim_token(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)n;
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_STRING: {
|
|
|
|
|
struct bshell_string_ast_node *n
|
|
|
|
|
= (struct bshell_string_ast_node *)
|
|
|
|
|
bshell_ast_node_create(BSHELL_AST_STRING);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!n) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 21:21:51 +01:00
|
|
|
n->n_value = claim_token(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)n;
|
2026-05-09 21:21:51 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_SYMBOL:
|
2026-05-12 22:58:59 +01:00
|
|
|
switch (tok->tok_symbol) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_LEFT_PAREN:
|
|
|
|
|
case BSHELL_SYM_LEFT_BRACE:
|
|
|
|
|
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
|
|
|
|
|
case BSHELL_SYM_AT_LEFT_BRACE:
|
|
|
|
|
case BSHELL_SYM_AT_LEFT_PAREN:
|
2026-05-12 22:58:59 +01:00
|
|
|
return parse_arith_value(ctx, out);
|
|
|
|
|
default:
|
|
|
|
|
report_error(
|
|
|
|
|
ctx,
|
|
|
|
|
"encountered unsupported command arg");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2026-05-10 14:19:06 +01:00
|
|
|
default:
|
2026-05-12 22:58:59 +01:00
|
|
|
report_error(ctx, "encountered unsupported command arg");
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool parse_redirect_to_fd(
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_parse_ctx *ctx,
|
2026-05-09 19:00:02 +01:00
|
|
|
unsigned int in_fd,
|
|
|
|
|
bool append,
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-10 14:19:06 +01:00
|
|
|
if (ctx->p_status != BSHELL_SUCCESS) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_redirection_ast_node *redirect
|
|
|
|
|
= (struct bshell_redirection_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_REDIRECTION);
|
2026-05-09 19:00:02 +01:00
|
|
|
|
|
|
|
|
redirect->n_in = in_fd;
|
|
|
|
|
redirect->n_append = append;
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
if (!parse_symbol(ctx, BSHELL_SYM_AMPERSAND)) {
|
|
|
|
|
bshell_ast_node_destroy((struct bshell_ast_node *)redirect);
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *out_tok = NULL;
|
|
|
|
|
struct bshell_ast_node *out_expr = NULL;
|
2026-05-10 14:19:06 +01:00
|
|
|
long long out_fd = -1;
|
|
|
|
|
|
|
|
|
|
if (peek_word(ctx, &out_tok)) {
|
|
|
|
|
const char *s = out_tok->tok_str;
|
|
|
|
|
char *ep;
|
|
|
|
|
out_fd = strtoll(s, &ep, 10);
|
|
|
|
|
if (*ep == '\0') {
|
|
|
|
|
discard_token(ctx);
|
|
|
|
|
out_tok = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
out_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
} else if (!parse_cmdcall_arg(ctx, &out_expr)) {
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 14:19:06 +01:00
|
|
|
redirect->n_out_is_fd = (out_fd >= 0) || out_expr;
|
|
|
|
|
redirect->n_out_is_expr = out_expr != NULL;
|
2026-05-09 19:00:02 +01:00
|
|
|
redirect->n_out = (unsigned int)out_fd;
|
2026-05-10 14:19:06 +01:00
|
|
|
redirect->n_out_path_expr = out_expr;
|
|
|
|
|
if (out_tok) {
|
|
|
|
|
redirect->n_out_tok = claim_token(ctx);
|
|
|
|
|
redirect->n_out_path = out_tok->tok_str;
|
|
|
|
|
}
|
2026-05-09 19:00:02 +01:00
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)redirect;
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool parse_redirect_to_file_squashed(
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_parse_ctx *ctx,
|
2026-05-09 19:00:02 +01:00
|
|
|
unsigned int in_fd,
|
|
|
|
|
bool append,
|
|
|
|
|
const char *str,
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-10 14:19:06 +01:00
|
|
|
if (ctx->p_status != BSHELL_SUCCESS) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (*str == '\0') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_redirection_ast_node *redirect
|
|
|
|
|
= (struct bshell_redirection_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_REDIRECTION);
|
2026-05-09 19:00:02 +01:00
|
|
|
|
|
|
|
|
redirect->n_in = in_fd;
|
|
|
|
|
redirect->n_append = append;
|
|
|
|
|
redirect->n_out_is_fd = false;
|
|
|
|
|
redirect->n_out_is_expr = false;
|
|
|
|
|
redirect->n_out_path = str;
|
|
|
|
|
|
2026-05-09 21:21:51 +01:00
|
|
|
redirect->n_out_tok = claim_token(ctx);
|
2026-05-09 19:00:02 +01:00
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)redirect;
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool parse_redirect_to_file_separate(
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_parse_ctx *ctx,
|
2026-05-09 19:00:02 +01:00
|
|
|
unsigned int in_fd,
|
|
|
|
|
bool append,
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-10 14:19:06 +01:00
|
|
|
if (ctx->p_status != BSHELL_SUCCESS) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *out_path = NULL;
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!parse_cmdcall_arg(ctx, &out_path)) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_redirection_ast_node *redirect
|
|
|
|
|
= (struct bshell_redirection_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_REDIRECTION);
|
2026-05-09 19:00:02 +01:00
|
|
|
|
|
|
|
|
redirect->n_in = in_fd;
|
|
|
|
|
redirect->n_append = append;
|
|
|
|
|
redirect->n_out_is_fd = false;
|
|
|
|
|
redirect->n_out_is_expr = true;
|
|
|
|
|
redirect->n_out_path_expr = out_path;
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)redirect;
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
bool parse_redirect(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
|
|
|
|
if (!tok || tok->tok_type != BSHELL_TOK_WORD) {
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int in_fd = 1;
|
|
|
|
|
const char *str = tok->tok_str;
|
|
|
|
|
bool append = false;
|
|
|
|
|
|
|
|
|
|
if (fx_wchar_is_number(*str)) {
|
2026-05-12 22:58:59 +01:00
|
|
|
in_fd = 0;
|
|
|
|
|
while (fx_wchar_is_number(*str)) {
|
|
|
|
|
in_fd *= 10;
|
|
|
|
|
in_fd += *str - '0';
|
|
|
|
|
str++;
|
|
|
|
|
}
|
2026-05-09 19:00:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*str != '>') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
str++;
|
|
|
|
|
if (*str == '>') {
|
|
|
|
|
append = true;
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*str != '\0') {
|
|
|
|
|
return parse_redirect_to_file_squashed(
|
|
|
|
|
ctx,
|
|
|
|
|
in_fd,
|
|
|
|
|
append,
|
|
|
|
|
str,
|
|
|
|
|
out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discard_token(ctx);
|
|
|
|
|
|
|
|
|
|
if (parse_redirect_to_fd(ctx, in_fd, append, out)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parse_redirect_to_file_separate(ctx, in_fd, append, out)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static bool peek_cmdcall_item(struct bshell_parse_ctx *ctx, bool unrestricted)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
|
|
|
|
/* each token type falls into one of three categories:
|
|
|
|
|
* - cmdcall item: the token can be used as part of a command call. the
|
|
|
|
|
* token indicates the start of a command call.
|
|
|
|
|
* - NOT a cmdcall item: the token cannot be used as part of a command
|
|
|
|
|
* call, usually because it as a cmdcall operator like | or &.
|
|
|
|
|
* encountering one of these tokens ends the cmdcall currently being
|
|
|
|
|
* parsed.
|
|
|
|
|
* - RESTRICTED cmdcall item: the token can be used as part of a
|
|
|
|
|
* command, but will not be considered the start of a cmdcall. to run
|
|
|
|
|
* a command with this token as its name, the call operator must be
|
|
|
|
|
* used.
|
|
|
|
|
*/
|
2026-05-09 21:21:51 +01:00
|
|
|
switch (peek_token_type(ctx)) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_KEYWORD:
|
|
|
|
|
case BSHELL_TOK_INT:
|
|
|
|
|
case BSHELL_TOK_DOUBLE:
|
|
|
|
|
case BSHELL_TOK_VAR:
|
|
|
|
|
case BSHELL_TOK_VAR_SPLAT:
|
|
|
|
|
case BSHELL_TOK_STRING:
|
|
|
|
|
case BSHELL_TOK_WORD_START:
|
2026-05-09 19:00:02 +01:00
|
|
|
return unrestricted;
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_SYMBOL:
|
2026-05-09 19:00:02 +01:00
|
|
|
switch (peek_unknown_symbol(ctx)) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_PLUS:
|
|
|
|
|
case BSHELL_SYM_HYPHEN:
|
2026-05-09 19:00:02 +01:00
|
|
|
return unrestricted;
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_PIPE:
|
|
|
|
|
case BSHELL_SYM_AMPERSAND:
|
|
|
|
|
case BSHELL_SYM_SEMICOLON:
|
|
|
|
|
case BSHELL_SYM_RIGHT_PAREN:
|
|
|
|
|
case BSHELL_SYM_RIGHT_BRACE:
|
|
|
|
|
case BSHELL_SYM_RIGHT_BRACKET:
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_TOK_NONE:
|
|
|
|
|
case BSHELL_TOK_LINEFEED:
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
bool parse_cmdcall(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_cmdcall_ast_node *node
|
|
|
|
|
= (struct bshell_cmdcall_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_CMDCALL);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!node) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *child = NULL;
|
2026-05-09 19:00:02 +01:00
|
|
|
bool unrestricted = false;
|
|
|
|
|
bool ok = true;
|
|
|
|
|
bool stop = false;
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
if (parse_symbol(ctx, BSHELL_SYM_AMPERSAND)) {
|
2026-05-09 19:00:02 +01:00
|
|
|
unrestricted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!peek_cmdcall_item(ctx, unrestricted)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!tok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!parse_cmdcall_arg(ctx, &child)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_queue_push_back(&node->n_args, &child->n_entry);
|
|
|
|
|
|
|
|
|
|
while (ok && !stop) {
|
|
|
|
|
if (!peek_cmdcall_item(ctx, true)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!tok) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parse_redirect(ctx, &child)) {
|
|
|
|
|
fx_queue_push_back(&node->n_redirect, &child->n_entry);
|
|
|
|
|
} else if (parse_cmdcall_arg(ctx, &child)) {
|
|
|
|
|
fx_queue_push_back(&node->n_args, &child->n_entry);
|
|
|
|
|
} else {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
|
|
|
|
|
ok = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ok) {
|
2026-05-25 10:33:29 +01:00
|
|
|
bshell_ast_node_destroy((struct bshell_ast_node *)node);
|
2026-05-09 19:00:02 +01:00
|
|
|
node = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)node;
|
2026-05-09 19:00:02 +01:00
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
bool peek_command(struct bshell_parse_ctx *ctx)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
if (peek_symbol(ctx, BSHELL_SYM_AMPERSAND)) {
|
2026-05-09 19:00:02 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return peek_cmdcall_item(ctx, false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
bool parse_command(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
|
2026-05-09 19:00:02 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *cmdcall = NULL;
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!parse_cmdcall(ctx, &cmdcall)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_pipeline_ast_node *pipeline = NULL;
|
2026-05-09 19:00:02 +01:00
|
|
|
|
|
|
|
|
while (1) {
|
2026-05-25 10:33:29 +01:00
|
|
|
if (peek_symbol(ctx, BSHELL_SYM_SEMICOLON)
|
|
|
|
|
|| peek_linefeed(ctx)) {
|
2026-05-09 19:00:02 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
if (!parse_symbol(ctx, BSHELL_SYM_PIPE)) {
|
2026-05-09 19:00:02 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pipeline) {
|
2026-05-25 10:33:29 +01:00
|
|
|
pipeline = (struct bshell_pipeline_ast_node *)
|
|
|
|
|
bshell_ast_node_create(BSHELL_AST_PIPELINE);
|
2026-05-09 19:00:02 +01:00
|
|
|
if (!pipeline) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_NO_MEMORY;
|
2026-05-25 10:33:29 +01:00
|
|
|
bshell_ast_node_destroy(cmdcall);
|
2026-05-09 19:00:02 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_queue_push_back(
|
|
|
|
|
&pipeline->n_stages,
|
|
|
|
|
&cmdcall->n_entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!parse_cmdcall(ctx, &cmdcall)) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_queue_push_back(&pipeline->n_stages, &cmdcall->n_entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pipeline) {
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)pipeline;
|
2026-05-09 19:00:02 +01:00
|
|
|
} else {
|
|
|
|
|
*out = cmdcall;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-05-12 22:58:59 +01:00
|
|
|
|
|
|
|
|
bool parse_pipeline(
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_parse_ctx *ctx,
|
|
|
|
|
struct bshell_ast_node *first_item,
|
|
|
|
|
struct bshell_ast_node **out)
|
2026-05-12 22:58:59 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_pipeline_ast_node *pipeline
|
|
|
|
|
= (struct bshell_pipeline_ast_node *)bshell_ast_node_create(
|
|
|
|
|
BSHELL_AST_PIPELINE);
|
2026-05-12 22:58:59 +01:00
|
|
|
|
|
|
|
|
fx_queue_push_back(&pipeline->n_stages, &first_item->n_entry);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
2026-05-25 10:33:29 +01:00
|
|
|
if (!parse_symbol(ctx, BSHELL_SYM_PIPE)) {
|
2026-05-12 22:58:59 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *cmdcall = NULL;
|
2026-05-12 22:58:59 +01:00
|
|
|
if (!parse_cmdcall(ctx, &cmdcall)) {
|
|
|
|
|
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_queue_push_back(&pipeline->n_stages, &cmdcall->n_entry);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
*out = (struct bshell_ast_node *)pipeline;
|
2026-05-12 22:58:59 +01:00
|
|
|
return true;
|
|
|
|
|
}
|