OpenVAS Libraries  9.0.3
nasl_var.c
Go to the documentation of this file.
1 /* Nessus Attack Scripting Language
2  *
3  * Copyright (C) 2002 - 2004 Tenable Network Security
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2,
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 
20 #include <stdlib.h> /* for abort */
21 #include <string.h> /* for strlen */
22 
23 #include <glib.h> /* for g_free */
24 
25 #include "nasl_tree.h"
26 #include "nasl_global_ctxt.h"
27 #include "nasl_func.h"
28 #include "nasl_var.h"
29 #include "nasl_lex_ctxt.h"
30 #include "exec.h"
31 #include "nasl_debug.h"
32 
33 #ifndef NASL_DEBUG
34 #define NASL_DEBUG 0
35 #endif
36 
37 /* Local prototypes */
38 static void copy_array (nasl_array *, const nasl_array *, int);
39 
40 
41 #if 0
42 static int
43 hash_int (int x)
44 {
45  return (unsigned) x % VAR_NAME_HASH;
46 }
47 #endif
48 
50 int
51 hash_str2 (const char *s, int n)
52 {
53  unsigned long h = 0;
54  const char *p;
55 
56  if (s == NULL)
57  return 0;
58 
59  for (p = s; *p != '\0'; p++)
60  h = (h << 3) + (unsigned char) *p;
61  return h % n;
62 }
63 
64 static int
65 hash_str (const char *s)
66 {
67  return hash_str2 (s, VAR_NAME_HASH);
68 }
69 
71 nasl_get_var_by_num (void *ctxt, nasl_array *a, int num, int create)
72 {
73  anon_nasl_var *v = NULL;
74 
75  if (num < 0)
76  {
77  /* TBD: implement a min_index field, just like $[ in Perl */
78  nasl_perror (ctxt,
79  "Negative integer index %d are not supported yet!\n", num);
80  return NULL;
81  }
82 
83  if (num < a->max_idx)
84  v = a->num_elt[num];
85  if (v != NULL || !create)
86  return v;
87 
88  if (num >= a->max_idx)
89  {
90  a->num_elt = g_realloc (a->num_elt, sizeof (anon_nasl_var *) * (num + 1));
91  bzero (a->num_elt + a->max_idx,
92  sizeof (anon_nasl_var *) * (num + 1 - a->max_idx));
93  a->max_idx = num + 1;
94  }
95  v = g_malloc0 (sizeof (anon_nasl_var));
96  v->var_type = VAR2_UNDEF;
97 
98  a->num_elt[num] = v;
99  return v;
100 }
101 
102 static named_nasl_var *
103 get_var_by_name (nasl_array * a, const char *s)
104 {
105  int h = hash_str (s);
106  named_nasl_var *v;
107 
108  if (a->hash_elt == NULL)
109  a->hash_elt = g_malloc0 (VAR_NAME_HASH * sizeof (named_nasl_var *));
110 
111  for (v = a->hash_elt[h]; v != NULL; v = v->next_var)
112  if (v->var_name != NULL && strcmp (s, v->var_name) == 0)
113  return v;
114 
115  v = g_malloc0 (sizeof (named_nasl_var));
116  v->var_name = g_strdup (s);
117  v->u.var_type = VAR2_UNDEF;
118  v->next_var = a->hash_elt[h];
119 
120  a->hash_elt[h] = v;
121  return v;
122 }
123 
124 
128 static named_nasl_var *
129 get_var_ref_by_name (lex_ctxt * ctxt, const char *name, int climb)
130 {
131  named_nasl_var *v;
132  int h = hash_str (name);
133  lex_ctxt *c;
134 
135  if (climb != 0)
136  {
137  for (c = ctxt; c != NULL; c = c->up_ctxt)
138  if (c->ctx_vars.hash_elt != NULL)
139  for (v = c->ctx_vars.hash_elt[h]; v != NULL; v = v->next_var)
140  if (v->var_name != NULL && strcmp (name, v->var_name) == 0)
141  return v;
142  }
143  else
144  {
145  if (ctxt->ctx_vars.hash_elt != NULL)
146  for (v = ctxt->ctx_vars.hash_elt[h]; v != NULL; v = v->next_var)
147  if (v->var_name != NULL && strcmp (name, v->var_name) == 0)
148  return v;
149  }
150 
151  if (ctxt->ctx_vars.hash_elt == NULL)
152  ctxt->ctx_vars.hash_elt =
153  g_malloc0 (sizeof (named_nasl_var *) * VAR_NAME_HASH);
154 
155  v = g_malloc0 (sizeof (named_nasl_var));
156  v->var_name = g_strdup (name);
157  v->u.var_type = VAR2_UNDEF;
158  v->next_var = ctxt->ctx_vars.hash_elt[h];
159  ctxt->ctx_vars.hash_elt[h] = v;
160 
161  return v;
162 }
163 
164 
165 static anon_nasl_var *
166 get_var_ref_by_num (lex_ctxt * ctxt, int num)
167 {
168  anon_nasl_var *v;
169 
170  if (num < 0) /* safer */
171  {
172  nasl_perror (ctxt, "Negative index %d is invalid for array\n", num);
173  return NULL;
174  }
175 
176  if (ctxt->ctx_vars.max_idx <= num)
177  {
178  ctxt->ctx_vars.num_elt =
179  g_realloc (ctxt->ctx_vars.num_elt, sizeof (anon_nasl_var *) * (num + 1));
180  bzero (ctxt->ctx_vars.num_elt + ctxt->ctx_vars.max_idx,
181  sizeof (anon_nasl_var *) * (num + 1 - ctxt->ctx_vars.max_idx));
182  ctxt->ctx_vars.max_idx = num + 1;
183  }
184 
185  v = ctxt->ctx_vars.num_elt[num];
186  if (v != NULL)
187  return v;
188 
189  v = g_malloc0 (sizeof (anon_nasl_var));
190  v->var_type = VAR2_UNDEF;
191  ctxt->ctx_vars.num_elt[num] = v;
192  return v;
193 }
194 
195 tree_cell *
197 {
198  tree_cell *tc = alloc_tree_cell (0, NULL);
199 
200  tc->type = REF_VAR;
201  tc->x.ref_val = v; /* No need to free this later! */
202  return tc;
203 }
204 
205 tree_cell *
206 get_variable_by_name (lex_ctxt * ctxt, const char *name)
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*/}
224 
225 static const char *
226 get_var_name (anon_nasl_var * v)
227 {
228  static char str[16];
229 #ifdef ALL_VARIABLES_NAMED
230  if (v->av_name != NULL)
231  return v->av_name;
232 #endif
233  snprintf (str, sizeof (str), "[%p]", v);
234  return str;
235 }
236 
237 tree_cell *
238 get_array_elem (lex_ctxt * ctxt, const char *name, tree_cell * idx)
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 }
357 
358 static void free_var_chain (named_nasl_var *);
359 static void free_anon_var (anon_nasl_var *);
360 
365 void
367 {
368  int i;
369 
370  if (a == NULL)
371  return;
372  if (a->num_elt != NULL)
373  {
374  for (i = 0; i < a->max_idx; i++)
375  free_anon_var (a->num_elt[i]);
376  g_free (a->num_elt);
377  a->num_elt = NULL;
378  }
379  a->max_idx = 0;
380  if (a->hash_elt != NULL)
381  {
382  for (i = 0; i < VAR_NAME_HASH; i++)
383  free_var_chain (a->hash_elt[i]);
384  g_free (a->hash_elt);
385  a->hash_elt = NULL;
386  }
387 }
388 
389 static void
390 free_var_chain (named_nasl_var * v)
391 {
392 
393  if (v == NULL)
394  return;
395  free_var_chain (v->next_var);
396  g_free (v->var_name);
397  switch (v->u.var_type)
398  {
399  case VAR2_STRING:
400  case VAR2_DATA:
401  g_free (v->u.v.v_str.s_val);
402  break;
403  case VAR2_ARRAY:
404  free_array (&v->u.v.v_arr);
405  break;
406  }
407  g_free (v);
408 }
409 
410 static void
411 free_anon_var (anon_nasl_var * v)
412 {
413  if (v == NULL)
414  return;
415  switch (v->var_type)
416  {
417  case VAR2_STRING:
418  case VAR2_DATA:
419  g_free (v->v.v_str.s_val);
420  break;
421  case VAR2_ARRAY:
422  free_array (&v->v.v_arr);
423  break;
424  }
425  g_free (v);
426 }
427 
428 void
430 {
431 
432  if (v == NULL)
433  return;
434 
435  switch (v->var_type)
436  {
437  case VAR2_INT:
438  v->v.v_int = 0;
439  break;
440  case VAR2_STRING:
441  case VAR2_DATA:
442  g_free (v->v.v_str.s_val);
443  v->v.v_str.s_val = NULL;
444  v->v.v_str.s_siz = 0;
445  break;
446  case VAR2_ARRAY:
447  free_array (&v->v.v_arr);
448  break;
449  }
450  v->var_type = VAR2_UNDEF;
451 }
452 
453 static void
454 copy_anon_var (anon_nasl_var * v1, const anon_nasl_var * v2)
455 {
456  /* TBD: free variable if necessary? */
457  v1->var_type = v2->var_type;
458  switch (v2->var_type)
459  {
460  case VAR2_STRING:
461  case VAR2_DATA:
462  if (v2->v.v_str.s_val != NULL)
463  {
464  v1->v.v_str.s_val = g_malloc0 (v2->v.v_str.s_siz + 1);
465  memcpy (v1->v.v_str.s_val, v2->v.v_str.s_val, v2->v.v_str.s_siz);
466  v1->v.v_str.s_siz = v2->v.v_str.s_siz;
467  }
468  else
469  {
470  v1->v.v_str.s_val = NULL;
471  v1->v.v_str.s_siz = 0;
472  }
473  break;
474 
475  case VAR2_UNDEF:
476  break;
477 
478  case VAR2_INT:
479  v1->v.v_int = v2->v.v_int;
480  break;
481 
482  case VAR2_ARRAY:
483  copy_array (&v1->v.v_arr, &v2->v.v_arr, 1);
484  break;
485 
486  default:
487  nasl_perror (NULL, "copy_anon_var: unhandled type 0x%x\n", v2->var_type);
488  clear_anon_var (v1);
489  }
490 }
491 
492 static anon_nasl_var *
493 dup_anon_var (const anon_nasl_var * v)
494 {
495  anon_nasl_var *v1;
496 
497  if (v == NULL)
498  return NULL;
499 
500  v1 = g_malloc0 (sizeof (anon_nasl_var));
501  copy_anon_var (v1, v);
502  return v1;
503 }
504 
505 static named_nasl_var *
506 dup_named_var (const named_nasl_var * v)
507 {
508  named_nasl_var *v1;
509 
510  if (v == NULL)
511  return NULL;
512 
513  v1 = g_malloc0 (sizeof (named_nasl_var));
514  copy_anon_var (&v1->u, &v->u);
515  v1->var_name = g_strdup (v->var_name);
516  return v1;
517 }
518 
519 static void
520 copy_array (nasl_array * a1, const nasl_array * a2, int copy_named)
521 {
522  int i;
523  named_nasl_var *v1, *v2, *v;
524 
525  if (a1 == a2)
526  {
527 #if NASL_DEBUG > 1
528  nasl_perror (NULL, "copy_array: a1 == a2!\n");
529 #endif
530  return;
531  }
532 
533  if (a1 == NULL || a2 == NULL)
534  {
535  nasl_perror (NULL, "Internal inconsistency - null array\n");
536  abort ();
537  }
538 
539  free_array (a1);
540 
541  if (a2->num_elt != NULL)
542  {
543  a1->max_idx = a2->max_idx;
544  a1->num_elt = g_malloc0 (sizeof (anon_nasl_var *) * a2->max_idx);
545  for (i = 0; i < a2->max_idx; i++)
546  a1->num_elt[i] = dup_anon_var (a2->num_elt[i]);
547  }
548  if (copy_named && a2->hash_elt != NULL)
549  {
550  a1->hash_elt = g_malloc0 (VAR_NAME_HASH * sizeof (named_nasl_var *));
551  for (i = 0; i < VAR_NAME_HASH; i++)
552  {
553  v1 = NULL;
554  for (v2 = a2->hash_elt[i]; v2 != NULL; v2 = v2->next_var)
555  {
556  v = dup_named_var (v2);
557  v->next_var = v1;
558  a1->hash_elt[i] = v;
559  v1 = v;
560  }
561  }
562  }
563 }
564 
565 tree_cell *
567 {
568  tree_cell *c2;
569  nasl_array *a2;
570 
571  if (c1 == NULL || c1 == FAKE_CELL || c1->type != REF_ARRAY)
572  return NULL;
573 
574  c2 = alloc_tree_cell (0, NULL);
575  c2->type = DYN_ARRAY;
576  c2->x.ref_val = a2 = g_malloc0 (sizeof (nasl_array));
577  copy_array (a2, c1->x.ref_val, 1);
578  return c2;
579 }
580 
581 extern FILE *nasl_trace_fp;
582 
583 static tree_cell *
584 affect_to_anon_var (anon_nasl_var * v1, tree_cell * rval)
585 {
586  anon_nasl_var *v2 = NULL, v0;
587  nasl_array *a = NULL;
588  int t1, t2;
589  void *p;
590 
591 
592  if (v1 == NULL || v1 == FAKE_CELL)
593  return NULL;
594 
595  t1 = v1->var_type;
596 
597  if (rval == NULL || rval == FAKE_CELL)
598  {
599 #if NASL_DEBUG > 1
600  nasl_perror (NULL,
601  "nasl_affect: affecting NULL or FAKE cell undefines variable %s %s\n",
602  get_var_name (v1), get_line_nb (rval));
603 #endif
604  clear_anon_var (v1);
605  if (nasl_trace_enabled ())
606  nasl_trace (NULL, "NASL> %s <- undef\n", get_var_name (v1));
607  return NULL;
608  }
609 
610  switch (rval->type)
611  {
612  case CONST_INT:
613  t2 = VAR2_INT;
614  break;
615  case CONST_STR:
616  t2 = VAR2_STRING;
617  break;
618  case CONST_DATA:
619  t2 = VAR2_DATA;
620  break;
621 
622  case REF_VAR:
623  v2 = rval->x.ref_val;
624  if (v2 == NULL)
625  {
626  t2 = 0;
627  a = NULL;
628  break;
629  }
630 
631  if (v2 == v1)
632  {
633 #if NASL_DEBUG > 1
634  nasl_perror (NULL,
635  "Copying variable %s to itself is useless and dangerous!\n",
636  get_var_name (v1));
637 #endif
638  return FAKE_CELL;
639  }
640 
641  t2 = v2->var_type;
642  if (t2 == VAR2_ARRAY)
643  a = &v2->v.v_arr; /* ? */
644  break;
645 
646  case REF_ARRAY:
647  case DYN_ARRAY:
648  a = rval->x.ref_val;
649  t2 = VAR2_ARRAY;
650  if (v1->var_type == VAR2_ARRAY && &v1->v.v_arr == a)
651  {
652 #if NASL_DEBUG > 1
653  nasl_perror (NULL,
654  "Copying array %s to itself is useless and dangerous!\n",
655  get_var_name (v1));
656 #endif
657  return FAKE_CELL;
658  }
659  break;
660 
661  default:
662  nasl_perror (NULL, "Cannot affect rvalue 0x%x to variable\n", rval->type);
663  return NULL;
664  }
665 
666  /*
667  * Bug #146: when executing
668  * x = 'abc'; x = x; or x = make_list(...); x = x[0];
669  * the rvalue will be freed before it is copied to the lvalue
670  */
671  v0 = *v1;
672 
673  if (t1 != VAR2_UNDEF && t2 == VAR2_UNDEF)
674  {
675 #if NASL_DEBUG > 0
676  nasl_perror (NULL, "Warning: Undefining defined variable %s %s\n",
677  get_var_name (v1), get_line_nb (rval));
678 #endif
679  }
680  else if (t1 == VAR2_ARRAY && t2 != VAR2_ARRAY)
681  {
682 #if NASL_DEBUG > 1
683  nasl_perror (NULL,
684  "Warning: affecting non array (0x%x) to array variable %s\n",
685  t2, get_line_nb (rval));
686 #endif
687  }
688  else if ((t1 == VAR2_INT || t1 == VAR2_STRING || t1 == VAR2_DATA)
689  && t2 == VAR2_ARRAY)
690  {
691 #if NASL_DEBUG > 1
692  nasl_perror (NULL,
693  "Warning: affecting array to atomic variable (0x%x) %s\n",
694  t2, get_line_nb (rval));
695 #endif
696  }
697 
698  /* Bug #146: this fake clear is necessary if we copy an array */
699  memset (v1, 0, sizeof (*v1));
700  /* Bug #146: no risk with the type, we already copied it */
701  v1->var_type = t2;
702 
703  if (rval->type != REF_VAR && rval->type != REF_ARRAY
704  && rval->type != DYN_ARRAY)
705  switch (t2)
706  {
707  case VAR2_INT:
708  v1->v.v_int = rval->x.i_val;
709  break;
710  case VAR2_STRING:
711  case VAR2_DATA:
712  if (rval->x.str_val == NULL)
713  {
714  v1->v.v_str.s_val = NULL;
715  v1->v.v_str.s_siz = 0;
716  }
717  else
718  {
719  p = g_malloc0 (rval->size + 1);
720  memcpy (p, rval->x.str_val, rval->size);
721  v1->v.v_str.s_siz = rval->size;
722  v1->v.v_str.s_val = p;
723  }
724  break;
725  }
726  else /* REF_xxx */
727  switch (t2)
728  {
729  case VAR2_INT:
730  v1->v.v_int = v2->v.v_int;
731  break;
732  case VAR2_STRING:
733  case VAR2_DATA:
734  if (v2->v.v_str.s_val == NULL)
735  {
736  v1->v.v_str.s_val = NULL;
737  v1->v.v_str.s_siz = 0;
738  }
739  else
740  {
741  p = g_malloc0 (v2->v.v_str.s_siz + 1);
742  memcpy (p, v2->v.v_str.s_val, v2->v.v_str.s_siz);
743  v1->v.v_str.s_siz = v2->v.v_str.s_siz;
744  v1->v.v_str.s_val = p;
745  }
746  break;
747  case VAR2_ARRAY:
748  copy_array (&v1->v.v_arr, a, 1);
749 #if 0
750  /* MA 2004-08-13: I guess that the code changed somewhere some day and
751  * this part created a memory leak */
752  if (v0.var_type == VAR2_ARRAY)
753  bzero (&v0, sizeof (v0)); /* So that we don't clear the variable twice */
754 #endif
755  break;
756  }
757 
758  if (nasl_trace_fp != NULL)
759  switch (t2)
760  {
761  case VAR2_INT:
762  nasl_trace (NULL, "NASL> %s <- %d\n", get_var_name (v1), v1->v.v_int);
763  break;
764  case VAR2_STRING:
765  case VAR2_DATA:
766  nasl_trace (NULL, "NASL> %s <- \"%s\"\n", get_var_name (v1),
767  v1->v.v_str.s_val);
768  break;
769  case VAR2_ARRAY:
770  nasl_trace (NULL, "NASL> %s <- (VAR2_ARRAY)\n", get_var_name (v1));
771  break;
772  default:
773  nasl_trace (NULL, "NASL> %s <- (Type 0x%x)\n", get_var_name (v1), t2);
774  break;
775  }
776 
777  clear_anon_var (&v0);
778  return FAKE_CELL;
779 }
780 
781 tree_cell *
783 {
784  anon_nasl_var *v1 = NULL;
785 
786  if (lval == NULL)
787  {
788  nasl_perror (NULL, "nasl_effect: invalid lvalue\n");
789  return NULL;
790  }
791 
792  if (lval->type != REF_VAR)
793  {
794  nasl_perror (NULL, "nasl_affect: cannot affect to non variable %s\n",
795  nasl_type_name (lval->type));
796  return NULL;
797  }
798 
799  v1 = lval->x.ref_val;
800  return affect_to_anon_var (v1, rval);
801 }
802 
803 static named_nasl_var *
804 create_named_var (const char *name, tree_cell * val)
805 {
806  named_nasl_var *v = g_malloc0 (sizeof (named_nasl_var));
807  tree_cell *tc;
808 
809  if (name != NULL)
810  v->var_name = g_strdup (name);
811 
812  if (val == NULL || val == FAKE_CELL)
813  {
814 #if NASL_DEBUG > 1
815  nasl_perror (NULL,
816  "create_named_var: affecting NULL or FAKE cell to variable %s\n",
817  get_var_name (v));
818 #endif
819  v->u.var_type = VAR2_UNDEF;
820  return v;
821  }
822 
823  tc = affect_to_anon_var (&v->u, val);
824  /* Here we might test the return value */
825  deref_cell (tc);
826  return v;
827 }
828 
829 static anon_nasl_var *
830 create_anon_var (tree_cell * val)
831 {
832  anon_nasl_var *v = g_malloc0 (sizeof (anon_nasl_var));
833  tree_cell *tc;
834 
835  if (val == NULL || val == FAKE_CELL)
836  {
837 #if NASL_DEBUG > 1
838  nasl_perror (NULL,
839  "create_anon_var: affecting NULL or FAKE cell to variable %s\n",
840  get_var_name (v));
841 #endif
842  v->var_type = VAR2_UNDEF;
843  return v;
844  }
845 
846  tc = affect_to_anon_var (v, val);
847  /* Here we might test the return value */
848  deref_cell (tc);
849  return v;
850 }
851 
852 tree_cell *
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 }
864 
865 tree_cell *
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 }
874 
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 }
906 
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 }
932 
933 
934 tree_cell *
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 }
1031 
1032 
1033 tree_cell *
1034 nasl_incr_variable (lex_ctxt * lexic, tree_cell * tc, int pre, int val)
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 }
1090 
1091 
1092 static long int
1093 var2int (anon_nasl_var * v, int defval)
1094 {
1095  if (v == NULL)
1096  return defval;
1097 
1098  switch (v->var_type)
1099  {
1100  case VAR2_INT:
1101  return v->v.v_int;
1102 
1103  case VAR2_STRING:
1104  case VAR2_DATA:
1105  return atoi ((char *) v->v.v_str.s_val);
1106 
1107  case VAR2_UNDEF:
1108 #if NASL_DEBUG > 1
1109  nasl_perror (NULL, "var2int: variable %s is undefined!\n",
1110  get_var_name (v));
1111 #endif
1112  case VAR2_ARRAY:
1113  default:
1114  return defval;
1115  }
1116  /*NOTREACHED*/}
1117 
1118 char *
1120 {
1121  GString *str;
1122  int i, n1 = 0;
1123  anon_nasl_var *u;
1124  named_nasl_var *v;
1125 
1126  if (a == NULL)
1127  return NULL;
1128 
1129  str = g_string_new ("[ ");
1130  if (a->num_elt != NULL)
1131  for (i = 0; i < a->max_idx; i++)
1132  if ((u = a->num_elt[i]) != NULL && u->var_type != VAR2_UNDEF)
1133  {
1134  if (n1 > 0)
1135  g_string_append (str, ", ");
1136  n1++;
1137  switch (u->var_type)
1138  {
1139  case VAR2_INT:
1140  g_string_append_printf (str, "%d: %ld", i, u->v.v_int);
1141  break;
1142  case VAR2_STRING:
1143  case VAR2_DATA:
1144  if (u->v.v_str.s_siz < 64)
1145  g_string_append_printf (str, "%d: '%s'", i, u->v.v_str.s_val);
1146  else
1147  g_string_append_printf (str, "%d: '%s'...", i, u->v.v_str.s_val);
1148  break;
1149  default:
1150  g_string_append_printf (str, "%d: ????", i);
1151  break;
1152  }
1153  }
1154 
1155  if (a->hash_elt != NULL)
1156  for (i = 0; i < VAR_NAME_HASH; i++)
1157  for (v = a->hash_elt[i]; v != NULL; v = v->next_var)
1158  if (v->u.var_type != VAR2_UNDEF)
1159  {
1160  u = &v->u;
1161  if (n1 > 0)
1162  g_string_append (str, ", ");
1163  n1++;
1164  switch (u->var_type)
1165  {
1166  case VAR2_INT:
1167  g_string_append_printf (str, "%s: %ld", v->var_name, u->v.v_int);
1168  break;
1169  case VAR2_STRING:
1170  case VAR2_DATA:
1171  if (u->v.v_str.s_siz < 64)
1172  g_string_append_printf (str, "%s: '%s'", v->var_name,
1173  u->v.v_str.s_val);
1174  else
1175  g_string_append_printf (str, "%s: '%s'...", v->var_name,
1176  u->v.v_str.s_val);
1177  break;
1178  default:
1179  g_string_append_printf (str, "%s: ????", v->var_name);
1180  break;
1181  }
1182  }
1183 
1184  g_string_append (str, " ]");
1185  return g_string_free (str, FALSE);
1186 }
1187 
1188 const char *
1190 {
1191  static char s1[16];
1192 
1193  if (v == NULL)
1194  return NULL;
1195 
1196  switch (v->var_type)
1197  {
1198  case VAR2_INT:
1199  snprintf (s1, sizeof (s1), "%ld", v->v.v_int);
1200  return s1; /* buggy if called twice in a row */
1201 
1202  case VAR2_STRING:
1203  case VAR2_DATA:
1204  return v->v.v_str.s_val == NULL ? "" : (const char *) v->v.v_str.s_val;
1205 
1206  case VAR2_UNDEF:
1207 #if NASL_DEBUG > 1
1208  nasl_perror (NULL, "var2str: variable %s is undefined!\n",
1209  get_var_name (v));
1210 #endif
1211  return NULL;
1212 
1213  case VAR2_ARRAY:
1214  return array2str (&v->v.v_arr);
1215 
1216  default:
1217 #if NASL_DEBUG > 0
1218  nasl_perror (NULL, "var2str: variable %s has unhandled type %d\n",
1219  get_var_name (v), v->var_type);
1220 #endif
1221  return "";
1222  }
1223 }
1224 
1225 long int
1226 get_int_var_by_num (lex_ctxt * lexic, int num, int defval)
1227 {
1228  anon_nasl_var *v = get_var_ref_by_num (lexic, num);
1229  return var2int (v, defval);
1230 }
1231 
1232 long int
1233 get_int_var_by_name (lex_ctxt * lexic, const char *name, int defval)
1234 {
1235  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1236  return var2int (&v->u, defval);
1237 }
1238 
1239 long int
1240 get_int_local_var_by_name (lex_ctxt * lexic, const char *name, int defval)
1241 {
1242  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1243  return var2int (&v->u, defval);
1244 }
1245 
1246 
1247 char *
1248 get_str_var_by_num (lex_ctxt * lexic, int num)
1249 {
1250  anon_nasl_var *v = get_var_ref_by_num (lexic, num);
1251  return (char *) var2str (v);
1252 }
1253 
1254 char *
1255 get_str_var_by_name (lex_ctxt * lexic, const char *name)
1256 {
1257  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1258  return (char *) var2str (&v->u);
1259 }
1260 
1261 char *
1263 {
1264  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1265  return (char *) var2str (&v->u);
1266 }
1267 
1268 static int
1269 get_var_size (const anon_nasl_var * v)
1270 {
1271  if (v == NULL)
1272  return 0;
1273  switch (v->var_type)
1274  {
1275  case VAR2_DATA:
1276  case VAR2_STRING:
1277  return v->v.v_str.s_siz;
1278 #if 0
1279  /*
1280  * This code confuses nasl_string() because it does not returns the same
1281  * length as array2str
1282  */
1283  case VAR2_ARRAY:
1284  return v->v.v_arr.max_idx; /* Do not count 'named' elements yet */
1285 #endif
1286  }
1287  return 0;
1288 }
1289 
1290 int
1291 get_var_size_by_name (lex_ctxt * lexic, const char *name)
1292 {
1293  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1294  return get_var_size (&v->u);
1295 }
1296 
1297 int
1299 {
1300  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1301  return get_var_size (&v->u);
1302 }
1303 
1304 int
1305 get_var_size_by_num (lex_ctxt * lexic, int num)
1306 {
1307  anon_nasl_var *v = get_var_ref_by_num (lexic, num);
1308  return get_var_size (v);
1309 }
1310 
1314 int
1315 get_var_type_by_num (lex_ctxt * lexic, int num)
1316 {
1317  anon_nasl_var *v = get_var_ref_by_num (lexic, num);
1318  return v == NULL ? VAR2_UNDEF : v->var_type;
1319 }
1320 
1321 int
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 }
1327 
1330 {
1331  nasl_iterator it;
1332  anon_nasl_var *v;
1333 
1334  it.a = NULL;
1335  it.v = NULL;
1336  it.i1 = 0;
1337  it.iH = 0;
1338 
1339  if (c == NULL || c == FAKE_CELL)
1340  return it;
1341 
1342  if (c->type == REF_VAR)
1343  {
1344  v = c->x.ref_val;
1345  if (v == NULL || v->var_type != VAR2_ARRAY)
1346  return it;
1347  it.a = &v->v.v_arr;
1348  }
1349  else if (c->type == REF_ARRAY || c->type == DYN_ARRAY)
1350  {
1351  it.a = c->x.ref_val;
1352  }
1353  else
1354  {
1355  nasl_perror (ctxt, "nasl_array_iterator: unhandled type %d (0x%x)\n",
1356  c->type, c->type);
1357  }
1358 
1359  return it;
1360 }
1361 
1362 tree_cell *
1364 {
1365  anon_nasl_var *av;
1366 
1367  if (it == NULL || it->a == NULL)
1368  return NULL;
1369 
1370  if (it->i1 >= 0)
1371  {
1372  while (it->i1 < it->a->max_idx)
1373  {
1374  av = it->a->num_elt[it->i1++];
1375  if (av != NULL && av->var_type != VAR2_UNDEF)
1376  return var2cell (av);
1377  }
1378  it->i1 = -1;
1379  }
1380 
1381  if (it->a->hash_elt == NULL)
1382  return NULL;
1383 
1384  if (it->v != NULL)
1385  it->v = it->v->next_var;
1386  do
1387  {
1388  while (it->v == NULL)
1389  if (it->iH >= VAR_NAME_HASH)
1390  return NULL;
1391  else
1392  it->v = it->a->hash_elt[it->iH++];
1393 
1394  while (it->v != NULL && it->v->u.var_type == VAR2_UNDEF)
1395  it->v = it->v->next_var;
1396  }
1397  while (it->v == NULL);
1398 
1399  return var2cell (&it->v->u);
1400 }
1401 
1402 int
1404 {
1405  anon_nasl_var *v2;
1406 
1407  if (i < 0)
1408  {
1409  nasl_perror (NULL,
1410  "add_var_to_list: negative index are not (yet) supported\n");
1411  return -1;
1412  }
1413 
1414  if (i >= a->max_idx)
1415  {
1416  a->num_elt = g_realloc (a->num_elt, sizeof (anon_nasl_var *) * (i + 1));
1417  bzero (a->num_elt + a->max_idx,
1418  sizeof (anon_nasl_var *) * (i + 1 - a->max_idx));
1419  a->max_idx = i + 1;
1420  }
1421 
1422  free_anon_var (a->num_elt[i]);
1423  v2 = dup_anon_var (v); /* May return NULL */
1424  a->num_elt[i] = v2;
1425  if (v2 == NULL)
1426  return 0;
1427  else
1428  return 1;
1429 }
1430 
1431 int
1433 {
1434  named_nasl_var *v2;
1435  int h = hash_str (name);
1436 
1437  if (a->hash_elt == NULL)
1438  {
1439  a->hash_elt = g_malloc0 (VAR_NAME_HASH * sizeof (named_nasl_var *));
1440  }
1441 
1442  v2 = g_malloc0 (sizeof (named_nasl_var));
1443  v2->var_name = g_strdup (name);
1444  v2->u.var_type = VAR2_UNDEF;
1445  v2->next_var = a->hash_elt[h];
1446  a->hash_elt[h] = v2;
1447 
1448  copy_anon_var (&(v2->u), v);
1449  return 0;
1450 }
1451 
1456 int
1458 {
1459  int i;
1460 
1461  for (i = a->max_idx - 1; i >= 0; i--)
1462  if (a->num_elt[i] != NULL && a->num_elt[i]->var_type != VAR2_UNDEF)
1463  {
1464  /* Fixing max_index will realloc() at next store.
1465  * I am not sure it is a good idea
1466  * Wait and see */
1467  a->max_idx = i + 1;
1468  return i + 1;
1469  }
1470  return 0;
1471 }
1472 
1477 tree_cell *
1479 {
1480  int n;
1481  tree_cell *c, *c2;
1482  nasl_array *a;
1483  anon_nasl_var v;
1484 
1485  a = g_malloc0 (sizeof (nasl_array));
1486  /* Either the elements are all "named", or they are "numbered". No mix! */
1487  if (el->x.str_val == NULL) /* numbered */
1488  {
1489  for (n = 0, c = el; c != NULL; c = c->link[1])
1490  n++;
1491  a->max_idx = n;
1492  a->num_elt = g_malloc0 (sizeof (anon_nasl_var *) * n);
1493  a->hash_elt = NULL;
1494  }
1495  else
1496  {
1497  a->num_elt = NULL;
1498  a->hash_elt = g_malloc0 (VAR_NAME_HASH * sizeof (named_nasl_var *));
1499  }
1500 
1501  for (n = 0, c = el; c != NULL; c = c->link[1])
1502  {
1503  c2 = c->link[0];
1504  if (c2 != NULL && c2 != FAKE_CELL)
1505  {
1506  memset (&v, 0, sizeof (v));
1507  switch (c2->type)
1508  {
1509  case CONST_INT:
1510  v.var_type = VAR2_INT;
1511  v.v.v_int = c2->x.i_val;
1512  break;
1513  case CONST_STR:
1514  case CONST_DATA:
1515  v.var_type = c2->type == CONST_STR ? VAR2_STRING : VAR2_DATA;
1516  if (c2->x.str_val == NULL)
1517  {
1518  v.v.v_str.s_val = NULL;
1519  v.v.v_str.s_siz = 0;
1520  }
1521  else
1522  {
1523  v.v.v_str.s_siz = c2->size;
1524  v.v.v_str.s_val = (unsigned char *) c2->x.str_val;
1525  }
1526  break;
1527  default:
1528  nasl_perror (NULL,
1529  "make_array_from_list: unhandled cell type %s at position %d\n",
1530  nasl_type_name (c2->type), n);
1531  v.var_type = VAR2_UNDEF;
1532  break;
1533  }
1534  }
1535 
1536  if (c->x.str_val == NULL)
1537  add_var_to_list (a, n++, &v);
1538  else
1539  add_var_to_array (a, c->x.str_val, &v);
1540  }
1541 
1543  c->x.ref_val = a;
1544  deref_cell (el);
1545  return c;
1546 }
int nasl_trace_enabled(void)
Checks if the nasl_trace_fp is set.
Definition: nasl_debug.c:151
anon_nasl_var * nasl_get_var_by_num(void *ctxt, nasl_array *a, int num, int create)
Definition: nasl_var.c:71
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
char * get_str_var_by_num(lex_ctxt *lexic, int num)
Definition: nasl_var.c:1248
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
tree_cell * copy_ref_array(const tree_cell *c1)
Definition: nasl_var.c:566
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
long int get_int_local_var_by_name(lex_ctxt *lexic, const char *name, int defval)
Definition: nasl_var.c:1240
named_nasl_var * v
Definition: nasl_var.h:82
tree_cell * make_array_from_elems(tree_cell *el)
Definition: nasl_var.c:1478
struct st_a_nasl_var u
Definition: nasl_var.h:68
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
short line_nb
Definition: nasl_tree.h:108
int add_var_to_array(nasl_array *a, char *name, const anon_nasl_var *v)
Definition: nasl_var.c:1432
void * ref_val
Definition: nasl_tree.h:115
tree_cell * get_variable_by_name(lex_ctxt *ctxt, const char *name)
Definition: nasl_var.c:206
void clear_anon_var(anon_nasl_var *v)
Definition: nasl_var.c:429
nasl_string_t v_str
Definition: nasl_var.h:60
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *lexic, const char *name, tree_cell *val)
Definition: nasl_var.c:908
nasl_array v_arr
Definition: nasl_var.h:62
long int get_int_var_by_num(lex_ctxt *lexic, int num, int defval)
Definition: nasl_var.c:1226
union TC::@7 x
tree_cell * decl_global_variables(lex_ctxt *lexic, tree_cell *vars)
Definition: nasl_var.c:866
tree_cell * get_array_elem(lex_ctxt *ctxt, const char *name, tree_cell *idx)
Definition: nasl_var.c:238
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1403
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
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
FILE * nasl_trace_fp
Definition: exec.c:386
int get_var_size_by_name(lex_ctxt *lexic, const char *name)
Definition: nasl_var.c:1291
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
#define NASL_DEBUG
Definition: nasl_var.c:34
#define VAR_NAME_HASH
Definition: nasl_var.h:31
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
char * array2str(const nasl_array *a)
Definition: nasl_var.c:1119
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:35
const char * name
Definition: nasl_init.c:524
tree_cell * nasl_read_var_ref(lex_ctxt *lexic, tree_cell *tc)
Definition: nasl_var.c:935
char * get_str_local_var_by_name(lex_ctxt *lexic, const char *name)
Definition: nasl_var.c:1262
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
int get_local_var_size_by_name(lex_ctxt *lexic, const char *name)
Definition: nasl_var.c:1298
tree_cell * var2cell(anon_nasl_var *v)
Definition: nasl_var.c:196
nasl_iterator nasl_array_iterator(void *ctxt, tree_cell *c)
Definition: nasl_var.c:1329
long int i_val
Definition: nasl_tree.h:114
nasl_array * a
Definition: nasl_var.h:79
tree_cell * nasl_iterate_array(nasl_iterator *it)
Definition: nasl_var.c:1363
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
const char * var2str(const anon_nasl_var *v)
Definition: nasl_var.c:1189
int get_var_size_by_num(lex_ctxt *lexic, int num)
Definition: nasl_var.c:1305
int array_max_index(nasl_array *a)
Definition: nasl_var.c:1457
const char * nasl_type_name(int t)
Definition: nasl_tree.c:420
unsigned char * s_val
Definition: nasl_var.h:35
int hash_str2(const char *s, int n)
Definition: nasl_var.c:51
tree_cell * decl_local_variables(lex_ctxt *lexic, tree_cell *vars)
Definition: nasl_var.c:853
char * get_str_var_by_name(lex_ctxt *lexic, const char *name)
Definition: nasl_var.c:1255
void free_array(nasl_array *a)
Definition: nasl_var.c:366
anon_nasl_var * add_numbered_var_to_ctxt(lex_ctxt *lexic, int num, tree_cell *val)
Definition: nasl_var.c:876
long int get_int_var_by_name(lex_ctxt *lexic, const char *name, int defval)
Definition: nasl_var.c:1233
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
int get_local_var_type_by_name(lex_ctxt *lexic, const char *name)
Definition: nasl_var.c:1322
long int v_int
Definition: nasl_var.h:61
tree_cell * nasl_affect(tree_cell *lval, tree_cell *rval)
Definition: nasl_var.c:782
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:33
tree_cell * nasl_incr_variable(lex_ctxt *lexic, tree_cell *tc, int pre, int val)
Definition: nasl_var.c:1034
int get_var_type_by_num(lex_ctxt *lexic, int num)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1315
int size
Definition: nasl_tree.h:110