hush: preparatory work for implementing functions

This commit is contained in:
Denis Vlasenko 2008-10-09 12:58:26 +00:00
parent 9af22c7626
commit c373527e4f

View File

@ -2903,15 +2903,13 @@ static void initialize_context(struct parse_context *ctx)
* case, function, and select are obnoxious, save those for later.
*/
#if HAS_KEYWORDS
static int reserved_word(o_string *word, struct parse_context *ctx)
{
struct reserved_combo {
struct reserved_combo {
char literal[6];
unsigned char res;
unsigned char assignment_flag;
int flag;
};
enum {
};
enum {
FLAG_END = (1 << RES_NONE ),
#if ENABLE_HUSH_IF
FLAG_IF = (1 << RES_IF ),
@ -2933,7 +2931,10 @@ static int reserved_word(o_string *word, struct parse_context *ctx)
FLAG_ESAC = (1 << RES_ESAC ),
#endif
FLAG_START = (1 << RES_XXXX ),
};
};
static const struct reserved_combo* match_reserved_word(o_string *word)
{
/* Mostly a list of accepted follow-up reserved words.
* FLAG_END means we are done with the sequence, and are ready
* to turn the compound list into a command.
@ -2961,6 +2962,16 @@ static int reserved_word(o_string *word, struct parse_context *ctx)
{ "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
#endif
};
const struct reserved_combo *r;
for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
if (strcmp(word->data, r->literal) == 0)
return r;
}
return NULL;
}
static int reserved_word(o_string *word, struct parse_context *ctx)
{
#if ENABLE_HUSH_CASE
static const struct reserved_combo reserved_match = {
"", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
@ -2968,9 +2979,10 @@ static int reserved_word(o_string *word, struct parse_context *ctx)
#endif
const struct reserved_combo *r;
for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
if (strcmp(word->data, r->literal) != 0)
continue;
r = match_reserved_word(word);
if (!r)
return 0;
debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
#if ENABLE_HUSH_CASE
if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE)
@ -3012,8 +3024,6 @@ static int reserved_word(o_string *word, struct parse_context *ctx)
}
word->o_assignment = r->assignment_flag;
return 1;
}
return 0;
}
#endif
@ -3892,6 +3902,17 @@ static int parse_stream(o_string *dest, struct parse_context *ctx,
) {
continue;
}
#endif
#if 0 /* TODO: implement functions */
if (dest->length != 0 /* not just () but word() */
&& dest->nonnull == 0 /* not a"b"c() */
&& ctx->command->argv == NULL /* it's the first word */
&& i_peek(input) == ')'
) {
bb_error_msg("seems like a function definition");
if (match_reserved_word(dest))
bb_error_msg("but '%s' is a reserved word!", dest->data);
}
#endif
case '{':
if (parse_group(dest, ctx, input, ch) != 0) {
@ -3905,7 +3926,9 @@ static int parse_stream(o_string *dest, struct parse_context *ctx,
goto case_semi;
#endif
case '}':
/* proper use of this character is caught by end_trigger */
/* proper use of this character is caught by end_trigger:
* if we see {, we call parse_group(..., end_trigger='}')
* and it will match } earlier (not here). */
syntax("unexpected } or )");
debug_printf_parse("parse_stream return 1: unexpected '}'\n");
return 1;