Mailing List Archive

eval
Interpreted languages often have a powerful "eval" function.
For those who would like to do a few experiments, here is a quick and
dirty patch that I will never commit into the CVS depository.
It is really ugly:
- it uses a temporary file (we have to change the lexer and the parser
interface in nasl_grammar.y)
- it only accepts "instructions" and not "expressions" (i.e., you cannot
eval "2+3"). I'm not sure there is a simple way to handle this without
duplicating the Bison grammar.
- there should be a way to catch "exceptions" (whatever this means in
NASL)




--- libnasl/nasl/nasl_init.c 21 Mar 2003 00:42:44 -0000 1.43
+++ libnasl/nasl/nasl_init.c 22 Mar 2003 13:38:20 -0000
@@ -173,6 +173,7 @@

"dump_ctxt", nasl_dump_ctxt, 0, { NULL },
"typeof", nasl_typeof, 1, { NULL },
+ "eval", nasl_eval, 1, { NULL },

"exit", nasl_do_exit, 1, { NULL },
"rand", nasl_rand, 0, { NULL },
--- libnasl/nasl/nasl_misc_funcs.c 18 Mar 2003 14:49:20 -0000 1.16
+++ libnasl/nasl/nasl_misc_funcs.c 22 Mar 2003 13:38:21 -0000
@@ -553,7 +553,53 @@
strcpy(retc->x.str_val, s);
return retc;
}
-
+
+tree_cell*
+nasl_eval(lex_ctxt* lexic)
+{
+ tree_cell *retc;
+ const char* s;
+ naslctxt ctx;
+ char *name;
+ FILE *fp;
+
+ s = get_str_var_by_num(lexic, 0);
+ if (s == NULL)
+ {
+ nasl_perror(lexic, "eval: missing parameter\n");
+ return NULL;
+ }
+
+ name = tmpnam(NULL);
+ fp = fopen(name, "w");
+ if (fp == NULL)
+ return NULL;
+
+ if (fputs(s, fp) < 0 || fclose(fp) < 0)
+ {
+ (void) unlink(name);
+ return NULL;
+ }
+
+ if (init_nasl_ctx(&ctx, name) < 0)
+ {
+ unlink(name);
+ return NULL;
+ }
+
+ if (naslparse(&ctx))
+ {
+ nasl_perror(lexic, "eval: parse error at or near line %d\n", ctx.line_nb);
+ nasl_clean_ctx(&ctx);
+ (void) unlink(name);
+ return NULL;
+ }
+ (void) unlink(name);
+
+ retc = nasl_exec(lexic, ctx.tree);
+ return retc;
+}
+
tree_cell*
nasl_defined_func(lex_ctxt* lexic)
{
--- libnasl/nasl/nasl_misc_funcs.h 18 Mar 2003 14:49:20 -0000 1.10
+++ libnasl/nasl/nasl_misc_funcs.h 22 Mar 2003 13:38:21 -0000
@@ -47,5 +47,6 @@
tree_cell* nasl_typeof(lex_ctxt*);
tree_cell* nasl_defined_func(lex_ctxt*);
tree_cell* nasl_sort_array(lex_ctxt*);
+tree_cell* nasl_eval(lex_ctxt*);

#endif