discover/grub2: Add support for for-loops
authorJeremy Kerr <jk@ozlabs.org>
Fri, 24 Jan 2014 06:40:46 +0000 (14:40 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 30 Jan 2014 13:59:10 +0000 (21:59 +0800)
GRUB2 syntax allows for for-loops; this change adds supoprt in the
parser grammar and script execution code to implement them. In the
execution code, we simply update the for-loop variable and re-execute
the body statements.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/grub2/grub2.h
discover/grub2/parser.y
discover/grub2/script.c

index 61662892f615e5a1fc0069a29180faa0a2772680..e79bf41d4b655598a3019394a0c2f5ccbbac0ca3 100644 (file)
@@ -83,6 +83,13 @@ struct grub2_statement_function {
        struct grub2_statements *body;
 };
 
        struct grub2_statements *body;
 };
 
+struct grub2_statement_for {
+       struct grub2_statement  st;
+       struct grub2_word       *var;
+       struct grub2_argv       *list;
+       struct grub2_statements *body;
+};
+
 struct grub2_script {
        struct grub2_statements         *statements;
        struct list                     environment;
 struct grub2_script {
        struct grub2_statements         *statements;
        struct list                     environment;
@@ -125,6 +132,10 @@ struct grub2_statement *create_statement_block(struct grub2_parser *parser,
 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
                struct grub2_word *name, struct grub2_statements *body);
 
 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
                struct grub2_word *name, struct grub2_statements *body);
 
+struct grub2_statement *create_statement_for(struct grub2_parser *parser,
+               struct grub2_word *var, struct grub2_argv *list,
+               struct grub2_statements *body);
+
 struct grub2_word *create_word_text(struct grub2_parser *parser,
                const char *text);
 
 struct grub2_word *create_word_text(struct grub2_parser *parser,
                const char *text);
 
@@ -153,6 +164,8 @@ int statement_menuentry_execute(struct grub2_script *script,
                struct grub2_statement *statement);
 int statement_function_execute(struct grub2_script *script,
                struct grub2_statement *statement);
                struct grub2_statement *statement);
 int statement_function_execute(struct grub2_script *script,
                struct grub2_statement *statement);
+int statement_for_execute(struct grub2_script *script,
+               struct grub2_statement *statement);
 
 struct grub2_script *create_script(struct grub2_parser *parser,
                struct discover_context *ctx);
 
 struct grub2_script *create_script(struct grub2_parser *parser,
                struct discover_context *ctx);
index 0beddd6fc3e28252af96acb74cd12a34a0186ebd..23bf4276818932613b3df07e14a9392861a1e2eb 100644 (file)
@@ -130,6 +130,12 @@ statement:
                /* we just flatten everything */
                $$ = create_statement_block(parser, $6);
        }
                /* we just flatten everything */
                $$ = create_statement_block(parser, $6);
        }
+       | "for" TOKEN_DELIM word TOKEN_DELIM "in" TOKEN_DELIM words TOKEN_EOL
+               "do" sep
+               statements
+               "done" {
+               $$ = create_statement_for(parser, $3, $7, $11);
+       }
 
 words: word {
                $$ = create_argv(parser);
 
 words: word {
                $$ = create_argv(parser);
@@ -243,6 +249,19 @@ struct grub2_statement *create_statement_function(struct grub2_parser *parser,
        return &stmt->st;
 }
 
        return &stmt->st;
 }
 
+struct grub2_statement *create_statement_for(struct grub2_parser *parser,
+               struct grub2_word *var, struct grub2_argv *list,
+               struct grub2_statements *body)
+{
+       struct grub2_statement_for *stmt =
+               talloc(parser, struct grub2_statement_for);
+       stmt->st.exec = statement_for_execute;
+       stmt->var = var;
+       stmt->list = list;
+       stmt->body = body;
+       return &stmt->st;
+}
+
 void statement_append(struct grub2_statements *stmts,
                struct grub2_statement *stmt)
 {
 void statement_append(struct grub2_statements *stmts,
                struct grub2_statement *stmt)
 {
index bfca607e1bf3c9266240d6b40873daee94faf56f..0618297784499f5d60ac0e597e03ff8602ee05d7 100644 (file)
@@ -19,6 +19,8 @@
        container_of(stmt, struct grub2_statement_menuentry, st)
 #define to_stmt_function(stmt) \
        container_of(stmt, struct grub2_statement_function, st)
        container_of(stmt, struct grub2_statement_menuentry, st)
 #define to_stmt_function(stmt) \
        container_of(stmt, struct grub2_statement_function, st)
+#define to_stmt_for(stmt) \
+       container_of(stmt, struct grub2_statement_for, st)
 #define to_stmt_conditional(stmt) \
        container_of(stmt, struct grub2_statement_conditional, st)
 
 #define to_stmt_conditional(stmt) \
        container_of(stmt, struct grub2_statement_conditional, st)
 
@@ -401,6 +403,27 @@ int statement_function_execute(struct grub2_script *script,
        return 0;
 }
 
        return 0;
 }
 
+int statement_for_execute(struct grub2_script *script,
+               struct grub2_statement *statement)
+{
+       struct grub2_statement_for *st = to_stmt_for(statement);
+       const char *varname;
+       int i, rc = 0;
+
+       if (st->var->type == GRUB2_WORD_VAR)
+               expand_var(script, st->var);
+       varname = st->var->text;
+
+       process_expansions(script, st->list);
+
+       for (i = 0; i < st->list->argc; ++i) {
+               script_env_set(script, varname, st->list->argv[i]);
+               rc = statements_execute(script, st->body);
+       }
+
+       return rc;
+}
+
 static void init_env(struct grub2_script *script)
 {
        struct env_entry *env;
 static void init_env(struct grub2_script *script)
 {
        struct env_entry *env;