OpenVAS Scanner  22.7.9
nasl_lex_ctxt.c File Reference
#include "nasl_lex_ctxt.h"
#include "nasl_func.h"
#include "nasl_global_ctxt.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <glib.h>
Include dependency graph for nasl_lex_ctxt.c:

Go to the source code of this file.

Functions

void init_nasl_library (lex_ctxt *)
 Adds "built-in" variable and function definitions to a context. More...
 
lex_ctxtinit_empty_lex_ctxt ()
 
void free_lex_ctxt (lex_ctxt *c)
 
void dump_ctxt (lex_ctxt *c)
 

Function Documentation

◆ dump_ctxt()

void dump_ctxt ( lex_ctxt c)

Definition at line 52 of file nasl_lex_ctxt.c.

53 {
54  int i;
55  named_nasl_var *v;
56 
57  printf ("--------<CTXT>--------\n");
58  if (c->fct_ctxt)
59  printf ("Is a function context\n");
60  if (c->up_ctxt == NULL)
61  printf ("Is the top level context\n");
62  if (c->ret_val)
63  {
64  printf ("Return value\n");
66  }
67 
68  printf ("Variables:\n");
69  for (i = 0; i < VAR_NAME_HASH; i++)
70  for (v = c->ctx_vars.hash_elt[i]; v != NULL; v = v->next_var)
71  printf ("%s\t", v->var_name);
72  putchar ('\n');
73 
74  printf ("----------------------\n");
75 }

References struct_lex_ctxt::ctx_vars, struct_lex_ctxt::fct_ctxt, st_nasl_array::hash_elt, nasl_dump_tree(), st_n_nasl_var::next_var, struct_lex_ctxt::ret_val, struct_lex_ctxt::up_ctxt, st_n_nasl_var::var_name, and VAR_NAME_HASH.

Referenced by nasl_dump_ctxt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_lex_ctxt()

void free_lex_ctxt ( lex_ctxt c)

Definition at line 43 of file nasl_lex_ctxt.c.

44 {
45  deref_cell (c->ret_val);
46  free_array (&c->ctx_vars);
47  g_hash_table_destroy (c->functions);
48  g_free (c);
49 }

References struct_lex_ctxt::ctx_vars, deref_cell(), free_array(), struct_lex_ctxt::functions, and struct_lex_ctxt::ret_val.

Referenced by exec_nasl_script(), nasl_func_call(), and nasl_lint().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_empty_lex_ctxt()

lex_ctxt* init_empty_lex_ctxt ( void  )
Todo:
Initialization of the library seems intuitively be necessary only once (involves "linking" the nasl functions to c code). Consider a "prototype" context that has to be created only once and of which copies are made when needed.

Definition at line 20 of file nasl_lex_ctxt.c.

21 {
22  lex_ctxt *c = g_malloc0 (sizeof (lex_ctxt));
23 
24  c->ctx_vars.hash_elt = g_malloc0 (sizeof (named_nasl_var *) * VAR_NAME_HASH);
25  c->ctx_vars.num_elt = NULL;
26  c->ctx_vars.max_idx = 0;
27  c->functions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
28  (GDestroyNotify) free_func);
29  c->oid = NULL;
30  c->ret_val = NULL;
31  c->fct_ctxt = 0;
32 
38 
39  return c;
40 }

References struct_lex_ctxt::ctx_vars, struct_lex_ctxt::fct_ctxt, free_func(), struct_lex_ctxt::functions, st_nasl_array::hash_elt, init_nasl_library(), st_nasl_array::max_idx, st_nasl_array::num_elt, struct_lex_ctxt::oid, struct_lex_ctxt::ret_val, and VAR_NAME_HASH.

Referenced by exec_nasl_script(), nasl_func_call(), and nasl_lint().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_nasl_library()

void init_nasl_library ( lex_ctxt )

Adds "built-in" variable and function definitions to a context.

Definition at line 486 of file nasl_init.c.

487 {
488  tree_cell tc;
489  unsigned i;
490 
491  memset (&tc, 0, sizeof (tc));
492 
493  // Initialize constant integer terms
494  tc.type = CONST_INT;
495  for (i = 0; i < sizeof (libivars) / sizeof (libivars[0]) - 1; i++)
496  {
497  tc.x.i_val = libivars[i].val;
498  if (add_named_var_to_ctxt (lexic, libivars[i].name, &tc) == NULL)
499  {
500  nasl_perror (lexic, "init_nasl_library: could not define var '%s'\n",
501  libivars[i].name);
502  continue;
503  }
504  }
505 
506  // Initialize constant string terms
507  tc.type = CONST_DATA;
508  for (i = 0; i < sizeof (libsvars) / sizeof (libsvars[0]) - 1; i++)
509  {
510  tc.x.str_val = (char *) libsvars[i].val;
511  tc.size = strlen (libsvars[i].val);
512  if (add_named_var_to_ctxt (lexic, libsvars[i].name, &tc) == NULL)
513  {
514  nasl_perror (lexic, "init_nasl_library: could not define var '%s'\n",
515  libsvars[i].name);
516  continue;
517  }
518  }
519 
520  // Add the "NULL" variable
521  if (add_named_var_to_ctxt (lexic, "NULL", NULL) == NULL)
522  nasl_perror (lexic, "init_nasl_library: could not define var 'NULL'\n");
523 }

References add_named_var_to_ctxt(), CONST_DATA, CONST_INT, TC::i_val, libivars, libsvars, name, nasl_perror(), TC::size, TC::str_val, TC::type, val, and TC::x.

Referenced by init_empty_lex_ctxt().

Here is the call graph for this function:
Here is the caller graph for this function:
free_array
void free_array(nasl_array *a)
Definition: nasl_var.c:342
struct_lex_ctxt::ctx_vars
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:35
libivars
static struct @4 libivars[]
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:82
add_named_var_to_ctxt
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:813
TC::str_val
char * str_val
Definition: nasl_tree.h:103
st_n_nasl_var
Definition: nasl_var.h:55
nasl_dump_tree
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:366
st_nasl_array::max_idx
int max_idx
Definition: nasl_var.h:34
TC::x
union TC::@5 x
st_nasl_array::hash_elt
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:36
struct_lex_ctxt::functions
GHashTable * functions
Definition: nasl_lex_ctxt.h:37
st_n_nasl_var::var_name
char * var_name
Definition: nasl_var.h:58
libsvars
static struct @3 libsvars[]
name
const char * name
Definition: nasl_init.c:411
struct_lex_ctxt::up_ctxt
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:24
st_n_nasl_var::next_var
struct st_n_nasl_var * next_var
Definition: nasl_var.h:62
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:111
TC::size
int size
Definition: nasl_tree.h:99
struct_lex_ctxt::oid
const char * oid
Definition: nasl_lex_ctxt.h:31
struct_lex_ctxt::fct_ctxt
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:26
free_func
void free_func(nasl_func *f)
Definition: nasl_func.c:266
init_nasl_library
void init_nasl_library(lex_ctxt *)
Adds "built-in" variable and function definitions to a context.
Definition: nasl_init.c:486
TC
Definition: nasl_tree.h:94
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:23
TC::type
short type
Definition: nasl_tree.h:95
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:79
val
const char * val
Definition: nasl_init.c:412
deref_cell
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:181
VAR_NAME_HASH
#define VAR_NAME_HASH
Definition: nasl_var.h:22
struct_lex_ctxt::ret_val
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:25
TC::i_val
long int i_val
Definition: nasl_tree.h:104
st_nasl_array::num_elt
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:35