OpenVAS Scanner  7.0.0~git
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 64 of file nasl_lex_ctxt.c.

65 {
66  int i;
67  named_nasl_var *v;
68 
69  printf ("--------<CTXT>--------\n");
70  if (c->fct_ctxt)
71  printf ("Is a function context\n");
72  if (c->up_ctxt == NULL)
73  printf ("Is the top level context\n");
74  if (c->ret_val)
75  {
76  printf ("Return value\n");
78  }
79 
80  printf ("Variables:\n");
81  for (i = 0; i < VAR_NAME_HASH; i++)
82  for (v = c->ctx_vars.hash_elt[i]; v != NULL; v = v->next_var)
83  printf ("%s\t", v->var_name);
84  putchar ('\n');
85 
86  printf ("----------------------\n");
87 }

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 55 of file nasl_lex_ctxt.c.

56 {
57  deref_cell (c->ret_val);
58  free_array (&c->ctx_vars);
59  g_hash_table_destroy (c->functions);
60  g_free (c);
61 }

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 ( )
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 32 of file nasl_lex_ctxt.c.

33 {
34  lex_ctxt *c = g_malloc0 (sizeof (lex_ctxt));
35 
36  c->ctx_vars.hash_elt = g_malloc0 (sizeof (named_nasl_var *) * VAR_NAME_HASH);
37  c->ctx_vars.num_elt = NULL;
38  c->ctx_vars.max_idx = 0;
39  c->functions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
40  (GDestroyNotify) free_func);
41  c->oid = NULL;
42  c->ret_val = NULL;
43  c->fct_ctxt = 0;
44 
50 
51  return c;
52 }

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 446 of file nasl_init.c.

447 {
448  tree_cell tc;
449  unsigned i;
450 
451  memset (&tc, 0, sizeof (tc));
452 
453  // Initialize constant integer terms
454  tc.type = CONST_INT;
455  for (i = 0; i < sizeof (libivars) / sizeof (libivars[0]) - 1; i++)
456  {
457  tc.x.i_val = libivars[i].val;
458  if (add_named_var_to_ctxt (lexic, libivars[i].name, &tc) == NULL)
459  {
460  nasl_perror (lexic, "init_nasl_library: could not define var '%s'\n",
461  libivars[i].name);
462  continue;
463  }
464  }
465 
466  // Initialize constant string terms
467  tc.type = CONST_DATA;
468  for (i = 0; i < sizeof (libsvars) / sizeof (libsvars[0]) - 1; i++)
469  {
470  tc.x.str_val = (char *) libsvars[i].val;
471  tc.size = strlen (libsvars[i].val);
472  if (add_named_var_to_ctxt (lexic, libsvars[i].name, &tc) == NULL)
473  {
474  nasl_perror (lexic, "init_nasl_library: could not define var '%s'\n",
475  libsvars[i].name);
476  continue;
477  }
478  }
479 
480  // Add the "NULL" variable
481  if (add_named_var_to_ctxt (lexic, "NULL", NULL) == NULL)
482  nasl_perror (lexic, "init_nasl_library: could not define var 'NULL'\n");
483 }

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:354
struct_lex_ctxt::ctx_vars
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:46
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:93
add_named_var_to_ctxt
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:825
libivars
static struct @1 libivars[]
libsvars
static struct @0 libsvars[]
TC::str_val
char * str_val
Definition: nasl_tree.h:112
st_n_nasl_var
Definition: nasl_var.h:65
nasl_dump_tree
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:377
st_nasl_array::max_idx
int max_idx
Definition: nasl_var.h:45
st_nasl_array::hash_elt
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:47
struct_lex_ctxt::functions
GHashTable * functions
Definition: nasl_lex_ctxt.h:48
TC::x
union TC::@2 x
st_n_nasl_var::var_name
char * var_name
Definition: nasl_var.h:69
name
const char * name
Definition: nasl_init.c:377
struct_lex_ctxt::up_ctxt
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:35
st_n_nasl_var::next_var
struct st_n_nasl_var * next_var
Definition: nasl_var.h:73
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
TC::size
int size
Definition: nasl_tree.h:109
struct_lex_ctxt::oid
const char * oid
Definition: nasl_lex_ctxt.h:42
struct_lex_ctxt::fct_ctxt
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:37
free_func
void free_func(nasl_func *f)
Definition: nasl_func.c:269
init_nasl_library
void init_nasl_library(lex_ctxt *)
Adds "built-in" variable and function definitions to a context.
Definition: nasl_init.c:446
TC
Definition: nasl_tree.h:104
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:33
TC::type
short type
Definition: nasl_tree.h:106
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:90
val
const char * val
Definition: nasl_init.c:378
deref_cell
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
VAR_NAME_HASH
#define VAR_NAME_HASH
Definition: nasl_var.h:33
struct_lex_ctxt::ret_val
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:36
TC::i_val
long int i_val
Definition: nasl_tree.h:113
st_nasl_array::num_elt
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:46