OpenVAS Libraries  9.0.3
nasl_lex_ctxt.h File Reference
#include "nasl_tree.h"
#include "nasl_var.h"
#include "nasl_func.h"
Include dependency graph for nasl_lex_ctxt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  struct_lex_ctxt
 

Macros

#define NASL_COMPAT_LEX_CTXT   "NASL compat lex context"
 

Typedefs

typedef struct struct_lex_ctxt lex_ctxt
 

Functions

lex_ctxtinit_empty_lex_ctxt (void)
 
void free_lex_ctxt (lex_ctxt *)
 
void dump_ctxt (lex_ctxt *)
 
nasl_funcget_func_ref_by_name (lex_ctxt *, const char *)
 
tree_celldecl_nasl_func (lex_ctxt *, tree_cell *)
 
nasl_funcinsert_nasl_func (lex_ctxt *, const char *, tree_cell *)
 
tree_cellnasl_func_call (lex_ctxt *, const nasl_func *, tree_cell *)
 
tree_cellget_variable_by_name (lex_ctxt *, const char *)
 
tree_cellget_array_elem (lex_ctxt *, const char *, tree_cell *)
 
anon_nasl_varadd_numbered_var_to_ctxt (lex_ctxt *, int, tree_cell *)
 
named_nasl_varadd_named_var_to_ctxt (lex_ctxt *, const char *, tree_cell *)
 
tree_cellnasl_read_var_ref (lex_ctxt *, tree_cell *)
 
tree_cellnasl_incr_variable (lex_ctxt *, tree_cell *, int, int)
 
tree_cellnasl_return (lex_ctxt *, tree_cell *)
 
tree_celldecl_local_variables (lex_ctxt *, tree_cell *)
 
tree_celldecl_global_variables (lex_ctxt *, tree_cell *)
 
tree_cellcell2atom (lex_ctxt *, tree_cell *)
 
long int get_int_var_by_num (lex_ctxt *, int, int)
 
char * get_str_var_by_num (lex_ctxt *, int)
 
long int get_int_var_by_name (lex_ctxt *, const char *, int)
 
long int get_int_local_var_by_name (lex_ctxt *, const char *, int)
 
char * get_str_var_by_name (lex_ctxt *, const char *)
 
char * get_str_local_var_by_name (lex_ctxt *, const char *)
 
int get_var_size_by_name (lex_ctxt *, const char *)
 
int get_local_var_size_by_name (lex_ctxt *, const char *)
 
int get_local_var_type_by_name (lex_ctxt *, const char *)
 
int get_var_size_by_num (lex_ctxt *, int)
 
int get_var_type_by_num (lex_ctxt *, int)
 Returns NASL variable/cell type, VAR2_UNDEF if value is NULL. More...
 

Macro Definition Documentation

◆ NASL_COMPAT_LEX_CTXT

#define NASL_COMPAT_LEX_CTXT   "NASL compat lex context"

Definition at line 49 of file nasl_lex_ctxt.h.

Typedef Documentation

◆ lex_ctxt

typedef struct struct_lex_ctxt lex_ctxt

Function Documentation

◆ add_named_var_to_ctxt()

named_nasl_var* add_named_var_to_ctxt ( lex_ctxt ,
const char *  ,
tree_cell  
)

Definition at line 908 of file nasl_var.c.

Referenced by decl_local_variables(), and exec_nasl_script().

909 {
910  int h = hash_str (name);
911  named_nasl_var *v;
912 
913  /* Duplicated code ? */
914  for (v = lexic->ctx_vars.hash_elt[h]; v != NULL; v = v->next_var)
915  if (v->var_name != NULL && strcmp (name, v->var_name) == 0)
916  {
917  if (val != NULL)
918  nasl_perror (lexic, "Cannot add existing variable %s\n", name);
919 #if NASL_DEBUG > 0
920  else
921  nasl_perror (lexic, "Will not clear existing variable %s\n", name);
922 #endif
923  return NULL;
924  }
925  v = create_named_var (name, val);
926  if (v == NULL)
927  return NULL;
928  v->next_var = lexic->ctx_vars.hash_elt[h];
929  lexic->ctx_vars.hash_elt[h] = v;
930  return v;
931 }
struct st_n_nasl_var * next_var
Definition: nasl_var.h:74
char * var_name
Definition: nasl_var.h:70
const char * val
Definition: nasl_init.c:525
const char * name
Definition: nasl_init.c:524
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
Here is the caller graph for this function:

◆ add_numbered_var_to_ctxt()

anon_nasl_var* add_numbered_var_to_ctxt ( lex_ctxt ,
int  ,
tree_cell  
)

Definition at line 876 of file nasl_var.c.

References struct_lex_ctxt::ctx_vars, st_nasl_array::max_idx, nasl_perror(), st_nasl_array::num_elt, val, VAR2_UNDEF, and st_a_nasl_var::var_type.

877 {
878  anon_nasl_var *v;
879  nasl_array *a = &lexic->ctx_vars;
880 
881  if (a->max_idx > num)
882  {
883  v = a->num_elt[num];
884  if (v != NULL && v->var_type != VAR2_UNDEF)
885  {
886  if (val != NULL)
887  nasl_perror (lexic, "Cannot add existing variable %d\n", num);
888 #if NASL_DEBUG > 0
889  else
890  nasl_perror (lexic, "Will not clear existing variable %d\n", num);
891 #endif
892  return NULL;
893  }
894  free_anon_var (a->num_elt[num]);
895  }
896  else
897  {
898  a->num_elt = g_realloc (a->num_elt, (num + 1) * sizeof (anon_nasl_var));
899  bzero (a->num_elt + a->max_idx,
900  sizeof (anon_nasl_var *) * (num + 1 - a->max_idx));
901  a->max_idx = num + 1;
902  }
903  a->num_elt[num] = v = create_anon_var (val);
904  return v;
905 }
const char * val
Definition: nasl_init.c:525
int var_type
Definition: nasl_var.h:54
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
Here is the call graph for this function:

◆ cell2atom()

tree_cell* cell2atom ( lex_ctxt lexic,
tree_cell c1 
)
Returns
A 'referenced' cell.

Definition at line 216 of file exec.c.

References cell2atom(), CONST_DATA, CONST_INT, CONST_STR, deref_cell(), DYN_ARRAY, FAKE_CELL, nasl_exec(), REF_ARRAY, ref_cell(), and TC::type.

Referenced by cell2atom(), cell_cmp(), and nasl_return().

217 {
218  tree_cell *c2 = NULL, *ret = NULL;
219  if (c1 == NULL || c1 == FAKE_CELL)
220  return c1;
221 
222  switch (c1->type)
223  {
224  case CONST_INT:
225  case CONST_STR:
226  case CONST_DATA:
227  case REF_ARRAY:
228  case DYN_ARRAY:
229  ref_cell (c1);
230  return c1;
231  default:
232  c2 = nasl_exec (lexic, c1);
233  ret = cell2atom (lexic, c2);
234  deref_cell (c2);
235  return ret;
236  }
237 }
#define FAKE_CELL
Definition: nasl_tree.h:120
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:188
short type
Definition: nasl_tree.h:107
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:216
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
Definition: nasl_tree.h:105
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition: exec.c:800
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decl_global_variables()

tree_cell* decl_global_variables ( lex_ctxt ,
tree_cell  
)

Definition at line 866 of file nasl_var.c.

References decl_local_variables(), and struct_lex_ctxt::up_ctxt.

867 {
868  lex_ctxt *c = lexic;
869 
870  while (c->up_ctxt != NULL)
871  c = c->up_ctxt;
872  return decl_local_variables (c, vars);
873 }
tree_cell * decl_local_variables(lex_ctxt *lexic, tree_cell *vars)
Definition: nasl_var.c:853
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:33
Here is the call graph for this function:

◆ decl_local_variables()

tree_cell* decl_local_variables ( lex_ctxt ,
tree_cell  
)

Definition at line 853 of file nasl_var.c.

References add_named_var_to_ctxt(), FAKE_CELL, TC::link, nasl_perror(), TC::str_val, and TC::x.

Referenced by decl_global_variables().

854 {
855  tree_cell *t;
856 
857  for (t = vars; t != NULL; t = t->link[0])
858  if (t->x.str_val == NULL)
859  nasl_perror (lexic, "decl_local_variables: null name!\n");
860  else
861  add_named_var_to_ctxt (lexic, t->x.str_val, NULL);
862  return FAKE_CELL;
863 }
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
char * str_val
Definition: nasl_tree.h:113
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *lexic, const char *name, tree_cell *val)
Definition: nasl_var.c:908
union TC::@7 x
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decl_nasl_func()

tree_cell* decl_nasl_func ( lex_ctxt ,
tree_cell  
)

Definition at line 111 of file nasl_func.c.

References FAKE_CELL, insert_nasl_func(), nasl_perror(), TC::str_val, and TC::x.

Referenced by nasl_lint().

112 {
113  if (decl_node == NULL || decl_node == FAKE_CELL)
114  {
115  nasl_perror (lexic, "Cannot insert NULL or FAKE cell as function\n");
116  return NULL;
117  }
118 
119  if (insert_nasl_func (lexic, decl_node->x.str_val, decl_node) == NULL)
120  return NULL;
121  else
122  return FAKE_CELL;
123 }
#define FAKE_CELL
Definition: nasl_tree.h:120
nasl_func * insert_nasl_func(lex_ctxt *lexic, const char *fname, tree_cell *decl_node)
Definition: nasl_func.c:65
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump_ctxt()

void dump_ctxt ( lex_ctxt )

Definition at line 64 of file nasl_lex_ctxt.c.

References struct_lex_ctxt::ctx_vars, struct_lex_ctxt::fct_ctxt, st_nasl_func::func_name, FUNC_NAME_HASH, struct_lex_ctxt::functions, st_nasl_array::hash_elt, nasl_dump_tree(), st_nasl_func::next_func, 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().

65 {
66  int i;
67  named_nasl_var *v;
68  nasl_func *f;
69 
70  printf ("--------<CTXT>--------\n");
71  if (c->fct_ctxt)
72  printf ("Is a function context\n");
73  if (c->up_ctxt == NULL)
74  printf ("Is the top level context\n");
75  if (c->ret_val)
76  {
77  printf ("Return value\n");
78  nasl_dump_tree (c->ret_val);
79  }
80 
81  printf ("Variables:\n");
82  for (i = 0; i < VAR_NAME_HASH; i++)
83  for (v = c->ctx_vars.hash_elt[i]; v != NULL; v = v->next_var)
84  printf ("%s\t", v->var_name);
85  putchar ('\n');
86 
87  printf ("Functions:\n");
88  for (i = 0; i < FUNC_NAME_HASH; i++)
89  for (f = c->functions[i]; f != NULL; f = f->next_func)
90  printf ("%s\t", f->func_name);
91  putchar ('\n');
92 
93  printf ("----------------------\n");
94 }
struct st_n_nasl_var * next_var
Definition: nasl_var.h:74
char * var_name
Definition: nasl_var.h:70
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:439
#define FUNC_NAME_HASH
Definition: nasl_func.h:22
char * func_name
Definition: nasl_func.h:32
struct st_nasl_func * next_func
Definition: nasl_func.h:38
#define VAR_NAME_HASH
Definition: nasl_var.h:31
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 )

Definition at line 46 of file nasl_lex_ctxt.c.

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

Referenced by exec_nasl_script().

47 {
48  int i;
49 
50 #if 0
51  if (c->exit_flag && c->up_ctxt != NULL)
52  ((lex_ctxt *) c->up_ctxt)->exit_flag = 1;
53 #endif
54  deref_cell (c->ret_val);
55  free_array (&c->ctx_vars);
56  for (i = 0; i < FUNC_NAME_HASH; i++)
57  {
58  free_func_chain (c->functions[i]);
59  }
60  g_free (c);
61 }
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
#define FUNC_NAME_HASH
Definition: nasl_func.h:22
void free_func_chain(nasl_func *f)
Definition: nasl_func.c:375
void free_array(nasl_array *a)
Definition: nasl_var.c:366
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_array_elem()

tree_cell* get_array_elem ( lex_ctxt ,
const char *  ,
tree_cell  
)

Definition at line 238 of file nasl_var.c.

References struct_lex_ctxt::ctx_vars, struct_lex_ctxt::fct_ctxt, st_nasl_array::hash_elt, name, struct_lex_ctxt::up_ctxt, st_a_nasl_var::v, st_a_nasl_var::v_arr, VAR2_ARRAY, and st_a_nasl_var::var_type.

239 {
240  named_nasl_var *nv;
241  anon_nasl_var *u, *av, fake_var;
242  tree_cell *tc, idx0;
243 
244 
245  /* Fake variable */
246  if (strcmp (name, "_FCT_ANON_ARGS") == 0)
247  {
248  lex_ctxt *c;
249  for (c = ctxt; c != NULL && !c->fct_ctxt; c = c->up_ctxt)
250  ;
251  if (c == NULL)
252  return NULL;
253  fake_var.var_type = VAR2_ARRAY;
254  fake_var.v.v_arr = c->ctx_vars;
255  fake_var.v.v_arr.hash_elt = NULL; /* mask named elements */
256  u = &fake_var;
257  }
258  else
259  {
260  named_nasl_var *v = get_var_ref_by_name (ctxt, name, 1);
261  u = &v->u;
262  }
263 
264  if (idx == NULL)
265  {
266 #if NASL_DEBUG > 0
267  nasl_perror (ctxt, "get_array_elem: NULL index\n");
268 #endif
269  /* Treat it as zero */
270  idx = &idx0;
271  idx->x.i_val = 0;
272  idx->type = CONST_INT;
273  }
274 
275  switch (u->var_type)
276  {
277  case VAR2_UNDEF:
278  /* We define the array here */
279  u->var_type = VAR2_ARRAY;
280  case VAR2_ARRAY:
281  switch (idx->type)
282  {
283  case CONST_INT:
284  av = nasl_get_var_by_num (ctxt, &u->v.v_arr, idx->x.i_val,
285  /* avoid dangling pointers */
286  strcmp (name, "_FCT_ANON_ARGS"));
287  return var2cell (av);
288 
289  case CONST_STR:
290  case CONST_DATA:
291  nv = get_var_by_name (&u->v.v_arr, idx->x.str_val);
292  return var2cell (nv != NULL ? &nv->u : NULL);
293 
294  default:
295  nasl_perror (ctxt, "get_array_elem: unhandled index type 0x%x for "
296  "variable %s\n", idx->type, name);
297  return NULL;
298  }
299  /*NOTREACHED*/ break;
300 
301  case VAR2_INT:
302  nasl_perror (ctxt, "get_array_elem: variable %s is an integer\n", name);
303  return NULL;
304 
305  case VAR2_STRING:
306  case VAR2_DATA:
307  if (idx->type == CONST_INT)
308  {
309  int l = u->v.v_str.s_siz;
310 
311  if (idx->x.i_val >= l)
312  {
313  nasl_perror (ctxt,
314  "get_array_elem: requesting character after end "
315  "of string %s (%d >= %d)\n", name, idx->x.i_val, l);
316  tc = alloc_expr_cell (idx->line_nb, CONST_DATA /*CONST_STR */ ,
317  NULL, NULL);
318  tc->x.str_val = g_strdup ("");
319  tc->size = 0;
320  return tc;
321  }
322  else
323  {
324  if (idx->x.i_val < 0)
325  {
326  nasl_perror (ctxt,
327  "get_array_elem: Negative index (%d) passed to "
328  "\"%s\"!\n", idx->x.i_val, name);
329  return NULL;
330  }
331  tc = alloc_expr_cell (idx->line_nb, CONST_DATA /*CONST_STR */ ,
332  NULL, NULL);
333  tc->x.str_val = g_malloc0 (2);
334  tc->x.str_val[0] = u->v.v_str.s_val[idx->x.i_val];
335  tc->x.str_val[1] = '\0';
336  tc->size = 1;
337  return tc;
338  }
339  }
340  else
341  {
342  nasl_perror (ctxt,
343  "get_array_elem: Cannot use a non integer index"
344  " (type 0x%x) in string. Variable: %s\n",
345  idx->type, name);
346  return NULL;
347  }
348  /*NOTREACHED*/ break;
349 
350  default:
351  nasl_perror (ctxt, "Severe bug: unknown variable type 0x%x %s\n",
352  u->var_type, get_line_nb (idx));
353  return NULL;
354  }
355  /*NOTREACHED*/ return NULL;
356 }
anon_nasl_var * nasl_get_var_by_num(void *ctxt, nasl_array *a, int num, int create)
Definition: nasl_var.c:71
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
struct st_a_nasl_var u
Definition: nasl_var.h:68
nasl_string_t v_str
Definition: nasl_var.h:60
nasl_array v_arr
Definition: nasl_var.h:62
union TC::@7 x
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:35
const char * name
Definition: nasl_init.c:524
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
tree_cell * var2cell(anon_nasl_var *v)
Definition: nasl_var.c:196
long int i_val
Definition: nasl_tree.h:114
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
unsigned char * s_val
Definition: nasl_var.h:35
char * get_line_nb(const tree_cell *c)
Definition: nasl_tree.c:452
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition: nasl_tree.c:86
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:33
int size
Definition: nasl_tree.h:110

◆ get_func_ref_by_name()

nasl_func* get_func_ref_by_name ( lex_ctxt ,
const char *   
)

Definition at line 126 of file nasl_func.c.

Referenced by exec_nasl_script(), nasl_defined_func(), nasl_func_has_arg(), nasl_func_named_args(), nasl_func_unnamed_args(), and nasl_lint().

127 {
128  int h = hash_str (name);
129  nasl_func *f;
130 
131  if ((f = get_func (ctxt, name, h)) != NULL)
132  return f;
133  else
134  return NULL;
135 }
const char * name
Definition: nasl_init.c:524
Here is the caller graph for this function:

◆ get_int_local_var_by_name()

long int get_int_local_var_by_name ( lex_ctxt ,
const char *  ,
int   
)

Definition at line 1240 of file nasl_var.c.

Referenced by forge_icmp_packet(), forge_icmp_v6_packet(), forge_igmp_packet(), forge_igmp_v6_packet(), forge_ip_packet(), forge_ipv6_packet(), forge_tcp_packet(), forge_tcp_v6_packet(), forge_udp_packet(), forge_udp_v6_packet(), get_port_transport(), insert_ip_options(), insert_ipv6_options(), nasl_bn_random(), nasl_crap(), nasl_dec2str(), nasl_egrep(), nasl_ereg(), nasl_ereg_replace(), nasl_eregmatch(), nasl_file_read(), nasl_file_seek(), nasl_file_write(), nasl_ftp_get_pasv_address(), nasl_ftp_log_in(), nasl_get_sock_info(), nasl_localtime(), nasl_match(), nasl_mktime(), nasl_open_sock_tcp_bufsz(), nasl_pcap_next(), nasl_recv(), nasl_recv_line(), nasl_same_host(), nasl_scanner_add_port(), nasl_send(), nasl_send_capture(), nasl_send_packet(), nasl_send_v6packet(), nasl_smb_close(), nasl_smb_file_group_sid(), nasl_smb_file_owner_sid(), nasl_smb_file_SDDL(), nasl_smb_file_trustee_rights(), nasl_socket_get_cert(), nasl_socket_get_ssl_ciphersuite(), nasl_socket_get_ssl_compression(), nasl_socket_get_ssl_session_id(), nasl_socket_get_ssl_version(), nasl_socket_negotiate_ssl(), nasl_split(), nasl_ssh_connect(), nasl_str_replace(), nasl_tcp_ping(), nasl_tcp_v6_ping(), nasl_wmi_close(), nasl_wmi_query(), nasl_wmi_query_rsop(), nasl_wmi_reg_create_key(), nasl_wmi_reg_delete_key(), nasl_wmi_reg_enum_key(), nasl_wmi_reg_enum_value(), nasl_wmi_reg_get_bin_val(), nasl_wmi_reg_get_dword_val(), nasl_wmi_reg_get_ex_string_val(), nasl_wmi_reg_get_mul_string_val(), nasl_wmi_reg_get_qword_val(), nasl_wmi_reg_get_sz(), nasl_wmi_reg_set_dword_val(), nasl_wmi_reg_set_ex_string_val(), nasl_wmi_reg_set_qword_val(), nasl_wmi_reg_set_string_val(), replace_kb_item(), set_ip_elements(), set_ipv6_elements(), set_kb_item(), set_tcp_elements(), set_tcp_v6_elements(), and set_udp_v6_elements().

1241 {
1242  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1243  return var2int (&v->u, defval);
1244 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524

◆ get_int_var_by_name()

long int get_int_var_by_name ( lex_ctxt ,
const char *  ,
int   
)

Definition at line 1233 of file nasl_var.c.

Referenced by nasl_get_sign(), nasl_ntlm_response(), nasl_ntlmv2_hash(), and nasl_ntlmv2_response().

1234 {
1235  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1236  return var2int (&v->u, defval);
1237 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524
Here is the caller graph for this function:

◆ get_int_var_by_num()

◆ get_local_var_size_by_name()

int get_local_var_size_by_name ( lex_ctxt ,
const char *   
)

◆ get_local_var_type_by_name()

int get_local_var_type_by_name ( lex_ctxt ,
const char *   
)

Definition at line 1322 of file nasl_var.c.

Referenced by nasl_open_sock_tcp_bufsz(), replace_kb_item(), and set_kb_item().

1323 {
1324  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1325  return v == NULL ? VAR2_UNDEF : v->u.var_type;
1326 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
int var_type
Definition: nasl_var.h:54
const char * name
Definition: nasl_init.c:524
Here is the caller graph for this function:

◆ get_str_local_var_by_name()

char* get_str_local_var_by_name ( lex_ctxt ,
const char *   
)

Definition at line 1262 of file nasl_var.c.

Referenced by forge_icmp_packet(), forge_icmp_v6_packet(), forge_igmp_packet(), forge_igmp_v6_packet(), forge_ip_packet(), forge_ipv6_packet(), forge_tcp_packet(), forge_tcp_v6_packet(), forge_udp_packet(), forge_udp_v6_packet(), get_icmp_element(), get_icmp_v6_element(), get_ip_element(), get_ipv6_element(), get_tcp_element(), get_tcp_v6_element(), get_udp_element(), get_udp_v6_element(), insert_ip_options(), insert_ipv6_options(), nasl_bf_cbc(), nasl_crap(), nasl_egrep(), nasl_ereg(), nasl_ereg_replace(), nasl_eregmatch(), nasl_file_open(), nasl_file_write(), nasl_ftp_log_in(), nasl_fwrite(), nasl_gunzip(), nasl_gzip(), nasl_match(), nasl_open_sock_tcp_bufsz(), nasl_pcap_next(), nasl_rsa_sign(), nasl_scanner_add_port(), nasl_send(), nasl_send_capture(), nasl_send_packet(), nasl_send_v6packet(), nasl_smb_connect(), nasl_smb_file_group_sid(), nasl_smb_file_owner_sid(), nasl_smb_file_SDDL(), nasl_smb_file_trustee_rights(), nasl_split(), nasl_str_replace(), nasl_wmi_query(), nasl_wmi_query_rsop(), nasl_wmi_reg_create_key(), nasl_wmi_reg_delete_key(), nasl_wmi_reg_enum_key(), nasl_wmi_reg_enum_value(), nasl_wmi_reg_get_bin_val(), nasl_wmi_reg_get_dword_val(), nasl_wmi_reg_get_ex_string_val(), nasl_wmi_reg_get_mul_string_val(), nasl_wmi_reg_get_qword_val(), nasl_wmi_reg_get_sz(), nasl_wmi_reg_set_dword_val(), nasl_wmi_reg_set_ex_string_val(), nasl_wmi_reg_set_qword_val(), nasl_wmi_reg_set_string_val(), replace_kb_item(), script_add_preference(), set_ip_elements(), set_ipv6_elements(), set_kb_item(), set_tcp_elements(), set_tcp_v6_elements(), set_udp_elements(), and set_udp_v6_elements().

1263 {
1264  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1265  return (char *) var2str (&v->u);
1266 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524
const char * var2str(const anon_nasl_var *v)
Definition: nasl_var.c:1189

◆ get_str_var_by_name()

char* get_str_var_by_name ( lex_ctxt ,
const char *   
)

Definition at line 1255 of file nasl_var.c.

Referenced by nasl_get_sign(), nasl_get_smb2_sign(), nasl_hmac_sha256(), nasl_insert_hexzeros(), nasl_keyexchg(), nasl_ntlm2_response(), nasl_ntlm_response(), nasl_ntlmv1_hash(), nasl_ntlmv2_hash(), nasl_ntlmv2_response(), nasl_ntv2_owf_gen(), nasl_rsa_private_decrypt(), nasl_rsa_public_encrypt(), script_mandatory_keys(), script_tag(), and script_xref().

1256 {
1257  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1258  return (char *) var2str (&v->u);
1259 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524
const char * var2str(const anon_nasl_var *v)
Definition: nasl_var.c:1189
Here is the caller graph for this function:

◆ get_str_var_by_num()

char* get_str_var_by_num ( lex_ctxt ,
int   
)

◆ get_var_size_by_name()

◆ get_var_size_by_num()

◆ get_var_type_by_num()

int get_var_type_by_num ( lex_ctxt ,
int   
)

Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.

Definition at line 1315 of file nasl_var.c.

Referenced by nasl_func_has_arg(), nasl_get_sock_info(), nasl_isnull(), nasl_isotime_is_valid(), nasl_isotime_scan(), nasl_rawstring(), nasl_string(), and nasl_substr().

1316 {
1317  anon_nasl_var *v = get_var_ref_by_num (lexic, num);
1318  return v == NULL ? VAR2_UNDEF : v->var_type;
1319 }
int var_type
Definition: nasl_var.h:54
Here is the caller graph for this function:

◆ get_variable_by_name()

tree_cell* get_variable_by_name ( lex_ctxt ,
const char *   
)

Definition at line 206 of file nasl_var.c.

References alloc_typed_cell(), DYN_ARRAY, name, TC::ref_val, and TC::x.

207 {
208  if (name == NULL)
209  return NULL;
210  /* Broken: Need also code in get_array_elem */
211  if (strcmp (name, "_FCT_ANON_ARGS") == 0)
212  {
214  nasl_array *a = retc->x.ref_val = g_malloc0 (sizeof (nasl_array));
215  copy_array (a, &ctxt->ctx_vars, 0);
216  return retc;
217  }
218  else
219  {
220  named_nasl_var *v = get_var_ref_by_name (ctxt, name, 1);
221  return var2cell (&v->u);
222  }
223  /*NOTREACHED*/}
struct st_a_nasl_var u
Definition: nasl_var.h:68
void * ref_val
Definition: nasl_tree.h:115
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
const char * name
Definition: nasl_init.c:524
tree_cell * var2cell(anon_nasl_var *v)
Definition: nasl_var.c:196
Here is the call graph for this function:

◆ init_empty_lex_ctxt()

lex_ctxt* init_empty_lex_ctxt ( void  )

Definition at line 29 of file nasl_lex_ctxt.c.

References struct_lex_ctxt::ctx_vars, struct_lex_ctxt::fct_ctxt, FUNC_NAME_HASH, struct_lex_ctxt::functions, st_nasl_array::hash_elt, 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(), and nasl_func_call().

30 {
31  lex_ctxt *c = g_malloc0 (sizeof (lex_ctxt));
32  int i;
33 
34  c->ctx_vars.hash_elt = g_malloc0 (sizeof (named_nasl_var) * VAR_NAME_HASH);
35  c->ctx_vars.num_elt = NULL;
36  c->ctx_vars.max_idx = 0;
37  for (i = 0; i < FUNC_NAME_HASH; i++)
38  c->functions[i] = NULL;
39  c->oid = NULL;
40  c->ret_val = NULL;
41  c->fct_ctxt = 0;
42  return c;
43 }
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
#define FUNC_NAME_HASH
Definition: nasl_func.h:22
nasl_func * functions[FUNC_NAME_HASH]
Definition: nasl_lex_ctxt.h:46
#define VAR_NAME_HASH
Definition: nasl_var.h:31
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:35
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:34
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
const char * oid
Definition: nasl_lex_ctxt.h:40
Here is the caller graph for this function:

◆ insert_nasl_func()

nasl_func* insert_nasl_func ( lex_ctxt ,
const char *  ,
tree_cell  
)

Definition at line 65 of file nasl_func.c.

Referenced by decl_nasl_func().

66 {
67  int h = hash_str (fname);
68  int i;
69  nasl_func *pf;
70  tree_cell *pc;
71 
72  if (get_func (lexic, fname, h) != NULL)
73  {
74  nasl_perror (lexic,
75  "insert_nasl_func: function '%s' is already defined\n",
76  fname);
77  return NULL;
78  }
79  pf = g_malloc0 (sizeof (nasl_func));
80  pf->func_name = g_strdup (fname);
81 
82  if (decl_node != NULL && decl_node != FAKE_CELL)
83  {
84  for (pc = decl_node->link[0]; pc != NULL; pc = pc->link[0])
85  if (pc->x.str_val == NULL)
86  pf->nb_unnamed_args++;
87  else
88  pf->nb_named_args++;
89 
90  pf->args_names = g_malloc0 (sizeof (char *) * pf->nb_named_args);
91  for (i = 0, pc = decl_node->link[0]; pc != NULL; pc = pc->link[0])
92  if (pc->x.str_val != NULL)
93  pf->args_names[i++] = g_strdup (pc->x.str_val);
94  /* Sort argument names */
95  qsort (pf->args_names, pf->nb_named_args, sizeof (pf->args_names[0]),
96  (qsortcmp)strcmp);
97 
98  pf->block = decl_node->link[1];
99  ref_cell (pf->block);
100  }
101  /* Allow variable number of arguments for user defined functions */
102  if (decl_node != NULL)
103  pf->nb_unnamed_args = 9999;
104 
105  pf->next_func = lexic->functions[h];
106  lexic->functions[h] = pf;
107  return pf;
108 }
#define FAKE_CELL
Definition: nasl_tree.h:120
void * block
Definition: nasl_func.h:36
struct TC * link[4]
Definition: nasl_tree.h:117
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:188
char * str_val
Definition: nasl_tree.h:113
char * func_name
Definition: nasl_func.h:32
struct st_nasl_func * next_func
Definition: nasl_func.h:38
char ** args_names
Definition: nasl_func.h:35
int nb_named_args
Definition: nasl_func.h:34
union TC::@7 x
Definition: nasl_tree.h:105
int nb_unnamed_args
Definition: nasl_func.h:34
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
int(* qsortcmp)(const void *, const void *)
Definition: nasl_func.c:62
Here is the caller graph for this function:

◆ nasl_func_call()

tree_cell* nasl_func_call ( lex_ctxt ,
const nasl_func ,
tree_cell  
)

Definition at line 147 of file nasl_func.c.

References st_nasl_func::args_names, FAKE_CELL, struct_lex_ctxt::fct_ctxt, st_nasl_func::flags, FUNC_FLAG_COMPAT, st_nasl_func::func_name, init_empty_lex_ctxt(), TC::link, nasl_dump_tree(), nasl_trace_fp, st_nasl_func::nb_named_args, struct_lex_ctxt::oid, struct_lex_ctxt::recv_timeout, struct_lex_ctxt::script_infos, TC::str_val, TRACE_BUF_SZ, and TC::x.

Referenced by exec_nasl_script().

148 {
149 #if 0
150  return FAKE_CELL;
151 #else
152  int nb_u = 0, nb_n = 0, nb_a = 0;
153  tree_cell *pc = NULL, *pc2 = NULL, *retc = NULL;
154  lex_ctxt *lexic2 = NULL;
155  char *trace_buf = NULL;
156  char *temp_funname = NULL, *tmp_filename = NULL;
157  int trace_buf_len = 0, tn;
158 #define TRACE_BUF_SZ 255
159 
160 #if 0
161  nasl_dump_tree (arg_list);
162 #endif
163 
164  /* 1. Create a new context */
165  lexic2 = init_empty_lex_ctxt ();
166  lexic2->script_infos = lexic->script_infos;
167  lexic2->oid = lexic->oid;
168  lexic2->recv_timeout = lexic->recv_timeout;
169  lexic2->fct_ctxt = 1;
170 
171  if (nasl_trace_fp != NULL)
172  {
173  trace_buf = g_malloc0 (TRACE_BUF_SZ);
174  tn = snprintf (trace_buf, TRACE_BUF_SZ, "Call %s(", f->func_name);
175  if (tn > 0)
176  trace_buf_len += tn;
177  }
178 
179  if (!(f->flags & FUNC_FLAG_COMPAT))
180  {
181  for (pc = arg_list; pc != NULL; pc = pc->link[1])
182  if (pc->x.str_val == NULL)
183  nb_u++;
184  else
185  {
186  size_t num = f->nb_named_args;
187  if (lfind
188  (&pc->x.str_val, f->args_names, &num, sizeof (char *),
189  stringcompare) != NULL)
190  nb_n++;
191  }
192 
193  if (nb_n + nb_u > f->nb_unnamed_args + f->nb_named_args)
194  nasl_perror (lexic,
195  "Too many args for function '%s' [%dN+%dU > %dN+%dU]\n",
196  f->func_name, nb_n, nb_u, f->nb_unnamed_args,
197  f->nb_named_args);
198  /*
199  * I should look exactly how unnamed arguments works...
200  * Or maybe I should remove this feature?
201  */
202 
203  for (nb_u = 0, pc = arg_list; pc != NULL; pc = pc->link[1])
204  {
205 #if 0
206  pc2 = pc->link[0];
207  ref_cell (pc2);
208  do
209  {
210  pc22 = nasl_exec (lexic, pc2);
211  deref_cell (pc2);
212  pc2 = pc22;
213  }
214  while (!nasl_is_leaf (pc2));
215 #else
216  pc2 = cell2atom (lexic, pc->link[0]);
217 #endif
218  if (pc->x.str_val == NULL)
219  {
220  /* 2. Add unnamed (numbered) variables for unnamed args */
221  if (add_numbered_var_to_ctxt (lexic2, nb_u, pc2) == NULL)
222  goto error;
223  nb_u++;
224  if (nasl_trace_fp != NULL && trace_buf_len < TRACE_BUF_SZ)
225  {
226  tn = snprintf (trace_buf + trace_buf_len,
227  TRACE_BUF_SZ - trace_buf_len, "%s%d: %s",
228  nb_a > 0 ? ", " : "", nb_u,
229  dump_cell_val (pc2));
230  if (tn > 0)
231  trace_buf_len += tn;
232  }
233  nb_a++;
234  }
235  else
236  {
237  /* 3. and add named variables for named args */
238  if (add_named_var_to_ctxt (lexic2, pc->x.str_val, pc2) == NULL)
239  goto error;
240  if (nasl_trace_fp != NULL && trace_buf_len < TRACE_BUF_SZ)
241  {
242  tn = snprintf (trace_buf + trace_buf_len,
243  TRACE_BUF_SZ - trace_buf_len, "%s%s: %s",
244  nb_a > 0 ? ", " : "", pc->x.str_val,
245  dump_cell_val (pc2));
246  if (tn > 0)
247  trace_buf_len += tn;
248  }
249  nb_a++;
250  }
251  deref_cell (pc2);
252  }
253 
254  if (nasl_trace_fp != NULL)
255  {
256  if (trace_buf_len < TRACE_BUF_SZ)
257  nasl_trace (lexic, "NASL> %s)\n", trace_buf);
258  else
259  nasl_trace (lexic, "NASL> %s ...)\n", trace_buf);
260  g_free (trace_buf);
261  }
262 
263  /* 4. Chain new context to old (lexic) */
264  lexic2->up_ctxt = lexic;
265  /* 5. Execute */
266  tmp_filename = g_strdup (nasl_get_filename (NULL));
267  nasl_set_filename (nasl_get_filename (f->func_name));
268  if (f->flags & FUNC_FLAG_INTERNAL)
269  {
270  tree_cell *(*pf2) (lex_ctxt *) = f->block;
271  retc = pf2 (lexic2);
272  }
273  else
274  {
275  temp_funname = g_strdup (nasl_get_function_name());
276  nasl_set_function_name (f->func_name);
277  retc = nasl_exec (lexic2, f->block);
278  deref_cell (retc);
279  retc = FAKE_CELL;
280  nasl_set_function_name (temp_funname);
281  g_free (temp_funname);
282  }
283 
284  nasl_set_filename (tmp_filename);
285  g_free (tmp_filename);
286  if ((retc == NULL || retc == FAKE_CELL)
287  && (lexic2->ret_val != NULL && lexic2->ret_val != FAKE_CELL))
288  {
289 #if 0
290  nasl_perror (lexic,
291  "nasl_func_call: nasl_exec(%s) returns NULL or FAKE value, but context disagrees. Fixing...\n",
292  f->func_name);
293  nasl_dump_tree (retc);
294 #endif
295  retc = lexic2->ret_val;
296  ref_cell (retc);
297  }
298 
299  if (nasl_trace_enabled ())
300  nasl_trace (lexic, "NASL> Return %s: %s\n", f->func_name,
301  dump_cell_val (retc));
302 #if 1
303  if (!nasl_is_leaf (retc))
304  {
305  nasl_perror (lexic,
306  "nasl_func_call: return value from %s is not atomic!\n",
307  f->func_name);
308  nasl_dump_tree (retc);
309  }
310 #endif
311 
312  free_lex_ctxt (lexic2);
313  lexic2 = NULL;
314  return retc;
315  }
316 
317 
318 error:
319  free_lex_ctxt (lexic2);
320  return NULL;
321 #endif
322 }
int nasl_trace_enabled(void)
Checks if the nasl_trace_fp is set.
Definition: nasl_debug.c:151
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
const char * nasl_get_filename(const char *function)
Definition: nasl_debug.c:43
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:188
FILE * nasl_trace_fp
Definition: exec.c:386
char * str_val
Definition: nasl_tree.h:113
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:439
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:216
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:71
const char * nasl_get_function_name()
Definition: nasl_debug.c:65
union TC::@7 x
#define FUNC_FLAG_COMPAT
Definition: nasl_func.h:24
void free_lex_ctxt(lex_ctxt *c)
Definition: nasl_lex_ctxt.c:46
void nasl_trace(lex_ctxt *lexic, char *msg,...)
Prints debug message in printf fashion to nasl_trace_fp if it exists.
Definition: nasl_debug.c:165
#define FUNC_FLAG_INTERNAL
Definition: nasl_func.h:25
lex_ctxt * init_empty_lex_ctxt()
Definition: nasl_lex_ctxt.c:29
int nasl_is_leaf(const tree_cell *pc)
Definition: nasl_tree.c:463
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:908
Definition: nasl_tree.h:105
char * dump_cell_val(const tree_cell *c)
Definition: nasl_tree.c:301
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:35
#define TRACE_BUF_SZ
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
void nasl_set_function_name(const char *funname)
Definition: nasl_debug.c:56
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:34
anon_nasl_var * add_numbered_var_to_ctxt(lex_ctxt *, int, tree_cell *)
Definition: nasl_var.c:876
const char * oid
Definition: nasl_lex_ctxt.h:40
struct arglist * script_infos
Definition: nasl_lex_ctxt.h:39
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:33
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition: exec.c:800
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_incr_variable()

tree_cell* nasl_incr_variable ( lex_ctxt ,
tree_cell ,
int  ,
int   
)

Definition at line 1034 of file nasl_var.c.

References alloc_tree_cell(), clear_anon_var(), CONST_INT, get_line_nb(), TC::i_val, nasl_perror(), TC::ref_val, REF_VAR, st_nasl_string::s_val, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_int, st_a_nasl_var::v_str, val, VAR2_DATA, VAR2_INT, VAR2_STRING, VAR2_UNDEF, st_a_nasl_var::var_type, and TC::x.

1035 {
1036  anon_nasl_var *v;
1037  int old_val = 0, new_val;
1038  tree_cell *retc;
1039 
1040  if (tc->type != REF_VAR)
1041  {
1042  nasl_perror (lexic,
1043  "nasl_incr_variable: argument (type=%d) is not REF_VAR %s\n",
1044  tc->type, get_line_nb (tc));
1045  return NULL;
1046  }
1047 
1048  v = tc->x.ref_val;
1049 
1050  switch (v->var_type)
1051  {
1052  case VAR2_INT:
1053  old_val = v->v.v_int;
1054  break;
1055  case VAR2_STRING:
1056  case VAR2_DATA:
1057 #if NASL_DEBUG > 0
1058  nasl_perror (lexic,
1059  "nasl_incr_variable: variable %s is a STRING %s - converting to integer\n",
1060  "", get_line_nb (tc));
1061 #endif
1062  old_val = v->v.v_str.s_val == NULL ? 0 : atoi ((char *) v->v.v_str.s_val);
1063  break;
1064  case VAR2_UNDEF:
1065 #if NASL_DEBUG > 0
1066  nasl_perror (lexic, "nasl_incr_variable: variable %s is undefined %s\n",
1067  "", get_line_nb (tc));
1068 #endif
1069  old_val = 0;
1070  break;
1071 
1072  default:
1073  nasl_perror (lexic,
1074  "nasl_incr_variable: variable %s has bad type %d %s\n",
1075  /*get_var_name(v) */ "", get_line_nb (tc));
1076  return NULL;
1077  }
1078  new_val = old_val + val;
1079 
1080  clear_anon_var (v);
1081  v->var_type = VAR2_INT;
1082  v->v.v_int = new_val;
1083 
1084  retc = alloc_tree_cell (0, NULL);
1085  retc->type = CONST_INT;
1086  retc->x.i_val = pre ? new_val : old_val;
1087 
1088  return retc;
1089 }
const char * val
Definition: nasl_init.c:525
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
void clear_anon_var(anon_nasl_var *v)
Definition: nasl_var.c:429
nasl_string_t v_str
Definition: nasl_var.h:60
union TC::@7 x
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
unsigned char * s_val
Definition: nasl_var.h:35
char * get_line_nb(const tree_cell *c)
Definition: nasl_tree.c:452
long int v_int
Definition: nasl_var.h:61
Here is the call graph for this function:

◆ nasl_read_var_ref()

tree_cell* nasl_read_var_ref ( lex_ctxt ,
tree_cell  
)

Definition at line 935 of file nasl_var.c.

References alloc_tree_cell(), CONST_INT, FAKE_CELL, get_line_nb(), TC::i_val, TC::line_nb, name, nasl_perror(), nasl_trace(), nasl_trace_enabled(), TC::ref_val, REF_VAR, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_int, VAR2_INT, st_a_nasl_var::var_type, and TC::x.

936 {
937  tree_cell *ret;
938  anon_nasl_var *v;
939 #if NASL_DEBUG > 0
940  const char *name;
941 #endif
942 
943  if (tc == NULL || tc == FAKE_CELL)
944  {
945  nasl_perror (lexic, "nasl_read_var_ref: cannot read NULL or FAKE cell\n");
946  return NULL;
947  }
948  if (tc->type != REF_VAR)
949  {
950  nasl_perror (lexic,
951  "nasl_read_var_ref: argument (type=%d) is not REF_VAR %s\n",
952  tc->type, get_line_nb (tc));
953  return NULL;
954  }
955 
956  v = tc->x.ref_val;
957  if (v == NULL)
958  {
959 #if NASL_DEBUG > 0
960  nasl_perror (lexic, "nasl_read_var_ref: NULL variable in REF_VAR\n");
961 #endif
962  return NULL;
963  }
964 
965  ret = alloc_tree_cell (tc->line_nb, NULL);
966 
967  switch (v->var_type)
968  {
969  case VAR2_INT:
970  ret->type = CONST_INT;
971  ret->x.i_val = v->v.v_int;
972  if (nasl_trace_enabled ())
973  nasl_trace (lexic, "NASL> %s -> %d\n", get_var_name (v), ret->x.i_val);
974  return ret;
975 
976  case VAR2_STRING:
977  ret->type = CONST_STR;
978  /* Fix bad string length */
979  if (v->v.v_str.s_siz <= 0 && v->v.v_str.s_val[0] != '\0')
980  {
981  v->v.v_str.s_siz = strlen ((char *) v->v.v_str.s_val);
982  nasl_perror (lexic, "nasl_read_var_ref: Bad string length fixed\n");
983  }
984  /* Go on next case */
985  case VAR2_DATA:
986  ret->type = v->var_type == VAR2_STRING ? CONST_STR : CONST_DATA;
987  if (v->v.v_str.s_val == NULL)
988  {
989  ret->x.str_val = NULL;
990  ret->size = 0;
991  }
992  else
993  {
994  ret->x.str_val = g_malloc0 (v->v.v_str.s_siz + 1);
995  memcpy (ret->x.str_val, v->v.v_str.s_val, v->v.v_str.s_siz);
996  ret->size = v->v.v_str.s_siz;
997  }
998  if (nasl_trace_enabled ())
999  nasl_trace (lexic, "NASL> %s -> \"%s\"\n", get_var_name (v),
1000  ret->x.str_val);
1001  return ret;
1002 
1003  case VAR2_ARRAY:
1004  ret->type = REF_ARRAY;
1005  ret->x.ref_val = &v->v.v_arr;
1006  return ret;
1007 
1008  case VAR2_UNDEF:
1009 #if NASL_DEBUG > 0
1010  name = get_var_name (v);
1011  if (strcmp (name, "NULL") != 0) /* special case */
1012  nasl_perror (lexic, "nasl_read_var_ref: variable %s is undefined %s\n",
1013  name, get_line_nb (tc));
1014 #endif
1015  if (nasl_trace_enabled ())
1016  nasl_trace (lexic, "NASL> %s -> undef\n", get_var_name (v),
1017  v->var_type);
1018  break;
1019 
1020  default:
1021  nasl_perror (lexic, "nasl_read_var_ref: unhandled variable type %d\n",
1022  v->var_type);
1023  if (nasl_trace_enabled ())
1024  nasl_trace (lexic, "NASL> %s -> ???? (Var type %d)\n", get_var_name (v),
1025  v->var_type);
1026  break;
1027  }
1028  deref_cell (ret);
1029  return NULL;
1030 }
int nasl_trace_enabled(void)
Checks if the nasl_trace_fp is set.
Definition: nasl_debug.c:151
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
void * ref_val
Definition: nasl_tree.h:115
nasl_string_t v_str
Definition: nasl_var.h:60
nasl_array v_arr
Definition: nasl_var.h:62
union TC::@7 x
void nasl_trace(lex_ctxt *lexic, char *msg,...)
Prints debug message in printf fashion to nasl_trace_fp if it exists.
Definition: nasl_debug.c:165
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
const char * name
Definition: nasl_init.c:524
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
unsigned char * s_val
Definition: nasl_var.h:35
char * get_line_nb(const tree_cell *c)
Definition: nasl_tree.c:452
long int v_int
Definition: nasl_var.h:61
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_return()

tree_cell* nasl_return ( lex_ctxt ,
tree_cell  
)

Definition at line 325 of file nasl_func.c.

References cell2atom(), copy_ref_array(), deref_cell(), FAKE_CELL, struct_lex_ctxt::fct_ctxt, REF_ARRAY, ref_cell(), struct_lex_ctxt::ret_val, TC::type, and struct_lex_ctxt::up_ctxt.

326 {
327  tree_cell *c;
328 
329  retv = cell2atom (ctxt, retv);
330  if (retv == NULL)
331  retv = FAKE_CELL;
332 
333  if (retv != FAKE_CELL && retv->type == REF_ARRAY)
334  /* We have to "copy" it as the referenced array will be freed */
335  {
336  c = copy_ref_array (retv);
337  deref_cell (retv);
338  retv = c;
339  }
340 
341  while (ctxt != NULL)
342  {
343  ctxt->ret_val = retv;
344  ref_cell (retv);
345  if (ctxt->fct_ctxt)
346  break;
347  ctxt = ctxt->up_ctxt;
348  }
349  /* Bug? Do not return NULL, as we may test it to break the control flow */
350  deref_cell (retv);
351  return FAKE_CELL;
352 }
#define FAKE_CELL
Definition: nasl_tree.h:120
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:188
tree_cell * copy_ref_array(const tree_cell *c1)
Definition: nasl_var.c:566
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:216
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
Definition: nasl_tree.h:105
Here is the call graph for this function: