OpenVAS Scanner  7.0.1~git
exec.h File Reference
#include "nasl_lex_ctxt.h"
Include dependency graph for exec.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

tree_cellnasl_exec (lex_ctxt *, tree_cell *)
 Execute a parse tree. More...
 
long int cell_cmp (lex_ctxt *, tree_cell *, tree_cell *)
 
tree_cellcell2atom (lex_ctxt *, tree_cell *)
 

Function Documentation

◆ cell2atom()

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

Definition at line 205 of file exec.c.

206 {
207  tree_cell *c2 = NULL, *ret = NULL;
208  if (c1 == NULL || c1 == FAKE_CELL)
209  return c1;
210 
211  switch (c1->type)
212  {
213  case CONST_INT:
214  case CONST_STR:
215  case CONST_DATA:
216  case REF_ARRAY:
217  case DYN_ARRAY:
218  ref_cell (c1);
219  return c1;
220  default:
221  c2 = nasl_exec (lexic, c1);
222  ret = cell2atom (lexic, c2);
223  deref_cell (c2);
224  return ret;
225  }
226 }
#define FAKE_CELL
Definition: nasl_tree.h:119
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:178
short type
Definition: nasl_tree.h:106
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:205
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
Definition: nasl_tree.h:104
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition: exec.c:781

◆ cell_cmp()

long int cell_cmp ( lex_ctxt ,
tree_cell ,
tree_cell  
)

Definition at line 229 of file exec.c.

References cell2atom(), cell2int(), cell2str(), cell_type(), CONST_DATA, CONST_INT, CONST_STR, deref_cell(), DYN_ARRAY, FAKE_CELL, nasl_perror(), nasl_type_name(), REF_ARRAY, and TC::size.

Referenced by nasl_exec(), and var_cmp().

230 {
231  int flag, typ, typ1, typ2;
232  long int x1, x2;
233  char *s1, *s2;
234  int len_s1, len_s2, len_min;
235 
236  if (c1 == NULL || c1 == FAKE_CELL)
237  nasl_perror (lexic, "cell_cmp: c1 == NULL !\n");
238  if (c2 == NULL || c2 == FAKE_CELL)
239  nasl_perror (lexic, "cell_cmp: c2 == NULL !\n");
240 
241  /* We first convert the cell to atomic types. */
242  c1 = cell2atom (lexic, c1);
243  c2 = cell2atom (lexic, c2);
244 
245  /*
246  * Comparing anything to something else which is entirely different
247  * may lead to unpredictable results.
248  * Here are the rules:
249  * 1. No problem with same types, although we do not compare arrays yet
250  * 2. No problem with CONST_DATA / CONST_STR
251  * 3. When an integer is compared to a string, the integer is converted
252  * 4. When NULL is compared to an integer, it is converted to 0
253  * 5. When NULL is compared to a string, it is converted to ""
254  * 6. NULL is "smaller" than anything else (i.e. an array)
255  * Anything else is an error
256  */
257  typ1 = cell_type (c1);
258  typ2 = cell_type (c2);
259 
260  if (typ1 == 0 && typ2 == 0) /* Two NULL */
261  {
262  deref_cell (c1);
263  deref_cell (c2);
264  return 0;
265  }
266 
267  if (typ1 == typ2) /* Same type, no problem */
268  typ = typ1;
269  else if ((typ1 == CONST_DATA || typ1 == CONST_STR)
270  && (typ2 == CONST_DATA || typ2 == CONST_STR))
271  typ = CONST_DATA; /* Same type in fact (string) */
272  /* We convert an integer into a string before compare */
273  else if ((typ1 == CONST_INT && (typ2 == CONST_DATA || typ2 == CONST_STR))
274  || (typ2 == CONST_INT && (typ1 == CONST_DATA || typ1 == CONST_STR)))
275  typ = CONST_DATA;
276  else if (typ1 == 0) /* 1st argument is null */
277  if (typ2 == CONST_INT || typ2 == CONST_DATA || typ2 == CONST_STR)
278  typ = typ2; /* We convert it to 0 or "" */
279  else
280  {
281  deref_cell (c1);
282  deref_cell (c2);
283  return -1; /* NULL is smaller than anything else */
284  }
285  else if (typ2 == 0) /* 2nd argument is null */
286  if (typ1 == CONST_INT || typ1 == CONST_DATA || typ1 == CONST_STR)
287  typ = typ1; /* We convert it to 0 or "" */
288  else
289  {
290  deref_cell (c1);
291  deref_cell (c2);
292  return 1; /* Anything else is greater than NULL */
293  }
294  else
295  {
296  gchar *n1, *n2;
297 
298  n1 = cell2str (lexic, c1);
299  n2 = cell2str (lexic, c2);
300  nasl_perror (lexic,
301  "cell_cmp: comparing '%s' of type %s and '%s' of "
302  "type %s does not make sense\n",
303  n1, nasl_type_name (typ1), n2, nasl_type_name (typ2));
304  g_free (n1);
305  g_free (n2);
306  deref_cell (c1);
307  deref_cell (c2);
308  return 0;
309  }
310 
311  switch (typ)
312  {
313  case CONST_INT:
314  x1 = cell2int (lexic, c1);
315  x2 = cell2int (lexic, c2);
316  deref_cell (c1);
317  deref_cell (c2);
318  return x1 - x2;
319 
320  case CONST_STR:
321  case CONST_DATA:
322  s1 = cell2str (lexic, c1);
323  if (typ1 == CONST_STR || typ1 == CONST_DATA)
324  len_s1 = c1->size;
325  else if (s1 == NULL)
326  len_s1 = 0;
327  else
328  len_s1 = strlen (s1);
329 
330  s2 = cell2str (lexic, c2);
331  if (typ2 == CONST_STR || typ2 == CONST_DATA)
332  len_s2 = c2->size;
333  else if (s2 == NULL)
334  len_s2 = 0;
335  else
336  len_s2 = strlen (s2);
337 
338  len_min = len_s1 < len_s2 ? len_s1 : len_s2;
339  flag = 0;
340 
341  if (len_min > 0)
342  flag = memcmp (s1, s2, len_min);
343  if (flag == 0)
344  flag = len_s1 - len_s2;
345 
346  g_free (s1);
347  g_free (s2);
348  deref_cell (c1);
349  deref_cell (c2);
350  return flag;
351 
352  case REF_ARRAY:
353  case DYN_ARRAY:
354  g_message ("cell_cmp: cannot compare arrays yet");
355  deref_cell (c1);
356  deref_cell (c2);
357  return 0;
358 
359  default:
360  g_message ("cell_cmp: don't known how to compare %s and %s",
361  nasl_type_name (typ1), nasl_type_name (typ2));
362  deref_cell (c1);
363  deref_cell (c2);
364  return 0;
365  }
366 }
#define FAKE_CELL
Definition: nasl_tree.h:119
static long int cell2int(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:135
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:205
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
static char * cell2str(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:161
const char * nasl_type_name(int t)
Definition: nasl_tree.c:357
int cell_type(const tree_cell *c)
Definition: nasl_tree.c:418
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_exec()

tree_cell* nasl_exec ( lex_ctxt ,
tree_cell  
)

Execute a parse tree.

Todo:
There is a lot of duplicated code in following cases, could be refactored.

Definition at line 781 of file exec.c.

References nasl_iterator::a, alloc_expr_cell(), alloc_typed_cell(), bool2cell(), struct_lex_ctxt::break_flag, cell2atom(), cell2bool(), cell2int(), cell2intW(), cell2str(), cell_cmp(), COMP_EQ, COMP_GE, COMP_GT, COMP_LE, COMP_LT, COMP_MATCH, COMP_NE, COMP_NOMATCH, COMP_RE_MATCH, COMP_RE_NOMATCH, CONST_DATA, CONST_INT, CONST_STR, struct_lex_ctxt::cont_flag, decl_global_variables(), decl_local_variables(), decl_nasl_func(), deref_cell(), DYN_ARRAY, expo(), EXPR_AND, EXPR_BIT_AND, EXPR_BIT_NOT, EXPR_BIT_OR, EXPR_BIT_XOR, EXPR_DECR, EXPR_DIV, EXPR_EXPO, EXPR_INCR, EXPR_L_SHIFT, EXPR_MINUS, EXPR_MODULO, EXPR_MULT, EXPR_NOT, EXPR_OR, EXPR_PLUS, EXPR_R_SHIFT, EXPR_R_USHIFT, EXPR_U_MINUS, FAKE_CELL, free_array(), get_array_elem(), get_func_ref_by_name(), get_variable_by_name(), int2cell(), struct_lex_ctxt::line_nb, TC::line_nb, TC::link, nasl_affect(), nasl_array_iterator(), nasl_exec(), nasl_func_call(), nasl_incr_variable(), nasl_iterate_array(), nasl_perror(), nasl_read_var_ref(), nasl_return(), nasl_short_dump(), nasl_trace_fp, NODE_AFF, NODE_ARG, NODE_ARRAY_EL, NODE_BREAK, NODE_CONTINUE, NODE_DECL, NODE_DIV_EQ, NODE_EMPTY, NODE_FOR, NODE_FOREACH, NODE_FUN_CALL, NODE_FUN_DEF, NODE_GLOBAL, NODE_IF_ELSE, NODE_INSTR_L, NODE_L_SHIFT_EQ, NODE_LOCAL, NODE_MINUS_EQ, NODE_MODULO_EQ, NODE_MULT_EQ, NODE_PLUS_EQ, NODE_R_SHIFT_EQ, NODE_R_USHIFT_EQ, NODE_REPEAT_UNTIL, NODE_REPEATED, NODE_RETURN, NODE_VAR, NODE_WHILE, REF_ARRAY, ref_cell(), TC::ref_val, REF_VAR, struct_lex_ctxt::ret_val, TC::size, TC::str_val, TC::type, val, and TC::x.

Referenced by cell2atom(), cell2bool(), cell2int3(), cell2str(), exec_nasl_script(), nasl_exec(), and nasl_func_call().

782 {
783  tree_cell *ret = NULL, *ret2 = NULL, *tc1 = NULL, *tc2 = NULL, *tc3 = NULL,
784  *idx = NULL, *args;
785  int flag, z;
786  char *s1 = NULL, *s2 = NULL, *s3 = NULL, *p = NULL;
787  char *p1, *p2;
788  int len1, len2;
789  nasl_func *pf = NULL;
790  long int x, y, n;
791  int i, lint_mode = 0;
792 
793  if (st)
794  if (st->line_nb != 0)
795  lexic->line_nb = st->line_nb;
796  /* return */
797  if (lexic->ret_val != NULL)
798  {
799  ref_cell (lexic->ret_val);
800  return lexic->ret_val;
801  }
802 
803  /* break or continue */
804  if (lexic->break_flag || lexic->cont_flag)
805  return FAKE_CELL;
806 
807  if (st == FAKE_CELL)
808  return FAKE_CELL;
809 
810  if (st == NULL)
811  {
812  return NULL;
813  }
814 
815  if (nasl_trace_fp != NULL)
817 
818  switch (st->type)
819  {
820  case NODE_IF_ELSE:
821  ret = nasl_exec (lexic, st->link[0]);
822 #ifdef STOP_AT_FIRST_ERROR
823  if (ret == NULL)
824  return NULL;
825 #endif
826  if (cell2bool (lexic, ret))
827  ret2 = nasl_exec (lexic, st->link[1]);
828  else if (st->link[2] != NULL) /* else branch */
829  ret2 = nasl_exec (lexic, st->link[2]);
830  else /* No else */
831  ret2 = FAKE_CELL;
832  deref_cell (ret);
833  return ret2;
834 
835  case NODE_INSTR_L: /* Block. [0] = first instr, [1] = tail */
836  ret = nasl_exec (lexic, st->link[0]);
837  if (st->link[1] == NULL || lexic->break_flag || lexic->cont_flag)
838  return ret;
839  deref_cell (ret);
840  ret = nasl_exec (lexic, st->link[1]);
841  return ret;
842 
843  case NODE_FOR:
844  /* [0] = start expr, [1] = cond, [2] = end_expr, [3] = block */
845  ret2 = nasl_exec (lexic, st->link[0]);
846 #ifdef STOP_AT_FIRST_ERROR
847  if (ret2 == NULL)
848  return NULL;
849 #endif
850  deref_cell (ret2);
851  for (;;)
852  {
853  /* Break the loop if 'return' */
854  if (lexic->ret_val != NULL)
855  {
856  ref_cell (lexic->ret_val);
857  return lexic->ret_val;
858  }
859 
860  /* condition */
861  if ((ret = nasl_exec (lexic, st->link[1])) == NULL)
862  return NULL; /* We can return here, as NULL is false */
863  flag = cell2bool (lexic, ret);
864  deref_cell (ret);
865  if (!flag)
866  break;
867  /* block */
868  ret = nasl_exec (lexic, st->link[3]);
869 #ifdef STOP_AT_FIRST_ERROR
870  if (ret == NULL)
871  return NULL;
872 #endif
873  deref_cell (ret);
874 
875  /* break */
876  if (lexic->break_flag)
877  {
878  lexic->break_flag = 0;
879  return FAKE_CELL;
880  }
881 
882  lexic->cont_flag = 0; /* No need to test if set */
883 
884  /* end expression */
885  ret = nasl_exec (lexic, st->link[2]);
886 #ifdef STOP_AT_FIRST_ERROR
887  if (ret == NULL)
888  return NULL;
889 #endif
890  deref_cell (ret);
891  }
892  return FAKE_CELL;
893 
894  case NODE_WHILE:
895  /* [0] = cond, [1] = block */
896  for (;;)
897  {
898  /* return? */
899  if (lexic->ret_val != NULL)
900  {
901  ref_cell (lexic->ret_val);
902  return lexic->ret_val;
903  }
904  /* Condition */
905  if ((ret = nasl_exec (lexic, st->link[0])) == NULL)
906  return NULL; /* NULL is false */
907  flag = cell2bool (lexic, ret);
908  deref_cell (ret);
909  if (!flag)
910  break;
911  /* Block */
912  ret = nasl_exec (lexic, st->link[1]);
913 #ifdef STOP_AT_FIRST_ERROR
914  if (ret == NULL)
915  return NULL;
916 #endif
917  deref_cell (ret);
918 
919  /* break */
920  if (lexic->break_flag)
921  {
922  lexic->break_flag = 0;
923  return FAKE_CELL;
924  }
925  lexic->cont_flag = 0;
926  }
927  return FAKE_CELL;
928 
929  case NODE_REPEAT_UNTIL:
930  /* [0] = block, [1] = cond */
931  for (;;)
932  {
933  /* return? */
934  if (lexic->ret_val != NULL)
935  {
936  ref_cell (lexic->ret_val);
937  return lexic->ret_val;
938  }
939  /* Block */
940  ret = nasl_exec (lexic, st->link[0]);
941 #ifdef STOP_AT_FIRST_ERROR
942  if (ret == NULL)
943  return NULL;
944 #endif
945  deref_cell (ret);
946 
947  /* break */
948  if (lexic->break_flag)
949  {
950  lexic->break_flag = 0;
951  return FAKE_CELL;
952  }
953  lexic->cont_flag = 0;
954 
955  /* Condition */
956  ret = nasl_exec (lexic, st->link[1]);
957 #ifdef STOP_AT_FIRST_ERROR
958  if (ret == NULL)
959  return NULL;
960 #endif
961  flag = cell2bool (lexic, ret);
962  deref_cell (ret);
963  if (flag)
964  break;
965  }
966  return FAKE_CELL;
967 
968  case NODE_FOREACH:
969  /* str_val = index name, [0] = array, [1] = block */
970  {
971  nasl_iterator ai;
972  tree_cell *v, *a, *val;
973 
974  v = get_variable_by_name (lexic, st->x.str_val);
975  if (v == NULL)
976  return NULL; /* We cannot go on if we have no variable to iterate */
977  a = nasl_exec (lexic, st->link[0]);
978  ai = nasl_array_iterator (lexic, a);
979  while ((val = nasl_iterate_array (&ai)) != NULL)
980  {
981  tc1 = nasl_affect (v, val);
982  ret = nasl_exec (lexic, st->link[1]);
983  deref_cell (val);
984  deref_cell (tc1);
985 #ifdef STOP_AT_FIRST_ERROR
986  if (ret == NULL)
987  break;
988 #endif
989  deref_cell (ret);
990 
991  /* return */
992  if (lexic->ret_val != NULL)
993  break;
994  /* break */
995  if (lexic->break_flag)
996  {
997  lexic->break_flag = 0;
998  break;
999  }
1000  lexic->cont_flag = 0;
1001  }
1002  free_array (ai.a);
1003  g_free (ai.a);
1004  deref_cell (a);
1005  deref_cell (v);
1006  }
1007  return FAKE_CELL;
1008 
1009  case NODE_FUN_DEF:
1010  /* x.str_val = function name, [0] = argdecl, [1] = block */
1011  /* 3rd arg is only for lint. Hier is always 0 */
1012  ret = decl_nasl_func (lexic, st, lint_mode);
1013  return ret;
1014 
1015  case NODE_FUN_CALL:
1016  pf = get_func_ref_by_name (lexic, st->x.str_val);
1017  if (pf == NULL)
1018  {
1019  nasl_perror (lexic, "Undefined function '%s'\n", st->x.str_val);
1020  return NULL;
1021  }
1022  args = st->link[0];
1023  ret = nasl_func_call (lexic, pf, args);
1024  return ret;
1025 
1026  case NODE_REPEATED:
1027  n = cell2intW (lexic, st->link[1]);
1028  if (n <= 0)
1029  return NULL;
1030 
1031 #ifdef STOP_AT_FIRST_ERROR
1032  for (tc1 = NULL, i = 1; i <= n; i++)
1033  {
1034  deref_cell (tc1);
1035  if ((tc1 = nasl_exec (lexic, st->link[0])) == NULL)
1036  return NULL;
1037  }
1038  return tc1;
1039 #else
1040  for (i = 1; i <= n; i++)
1041  {
1042  tc1 = nasl_exec (lexic, st->link[0]);
1043  deref_cell (tc1);
1044  }
1045  return FAKE_CELL;
1046 #endif
1047 
1048  /*
1049  * I wonder...
1050  * Will nasl_exec be really called with NODE_EXEC or NODE_ARG?
1051  */
1052  case NODE_DECL: /* Used in function declarations */
1053  /* [0] = next arg in list */
1054  /* TBD? */
1055  return st; /* ? */
1056 
1057  case NODE_ARG: /* Used function calls */
1058  /* val = name can be NULL, [0] = val, [1] = next arg */
1059  ret = nasl_exec (lexic, st->link[0]); /* Is this wise? */
1060  return ret;
1061 
1062  case NODE_RETURN:
1063  /* [0] = ret val */
1064  ret = nasl_return (lexic, st->link[0]);
1065  return ret;
1066 
1067  case NODE_BREAK:
1068  lexic->break_flag = 1;
1069  return FAKE_CELL;
1070 
1071  case NODE_CONTINUE:
1072  lexic->cont_flag = 1;
1073  return FAKE_CELL;
1074 
1075  case NODE_ARRAY_EL: /* val = array name, [0] = index */
1076  idx = cell2atom (lexic, st->link[0]);
1077  ret = get_array_elem (lexic, st->x.str_val, idx);
1078  deref_cell (idx);
1079  return ret;
1080 
1083  case NODE_AFF:
1084  /* [0] = lvalue, [1] = rvalue */
1085  tc1 = nasl_exec (lexic, st->link[0]);
1086  tc2 = nasl_exec (lexic, st->link[1]);
1087  ret = nasl_affect (tc1, tc2);
1088  deref_cell (tc1); /* Must free VAR_REF */
1089  deref_cell (ret);
1090  return tc2; /* So that "a = b = e;" works */
1091 
1092  case NODE_PLUS_EQ:
1093  tc1 = nasl_exec (lexic, st->link[0]);
1094  tc2 = nasl_exec (lexic, st->link[1]);
1095  tc3 = alloc_expr_cell (0, EXPR_PLUS, tc1, tc2);
1096  ret2 = nasl_exec (lexic, tc3);
1097  ret = nasl_affect (tc1, ret2);
1098  deref_cell (tc3); /* Frees tc1 and tc2 */
1099  deref_cell (ret);
1100  return ret2; /* So that "a = b += e;" works */
1101 
1102  case NODE_MINUS_EQ:
1103  tc1 = nasl_exec (lexic, st->link[0]);
1104  tc2 = nasl_exec (lexic, st->link[1]);
1105  tc3 = alloc_expr_cell (0, EXPR_MINUS, tc1, tc2);
1106  ret2 = nasl_exec (lexic, tc3);
1107  ret = nasl_affect (tc1, ret2);
1108  deref_cell (tc3); /* Frees tc1 and tc2 */
1109  deref_cell (ret);
1110  return ret2; /* So that "a = b -= e;" works */
1111 
1112  case NODE_MULT_EQ:
1113  tc1 = nasl_exec (lexic, st->link[0]);
1114  tc2 = nasl_exec (lexic, st->link[1]);
1115  tc3 = alloc_expr_cell (0, EXPR_MULT, tc1, tc2);
1116  ret2 = nasl_exec (lexic, tc3);
1117  ret = nasl_affect (tc1, ret2);
1118  deref_cell (tc3); /* Frees tc1 and tc2 */
1119  deref_cell (ret);
1120  return ret2;
1121 
1122  case NODE_DIV_EQ:
1123  tc1 = nasl_exec (lexic, st->link[0]);
1124  tc2 = nasl_exec (lexic, st->link[1]);
1125  tc3 = alloc_expr_cell (0, EXPR_DIV, tc1, tc2);
1126  ret2 = nasl_exec (lexic, tc3);
1127  ret = nasl_affect (tc1, ret2);
1128  deref_cell (tc3); /* Frees tc1 and tc2 */
1129  deref_cell (ret);
1130  return ret2;
1131 
1132  case NODE_MODULO_EQ:
1133  tc1 = nasl_exec (lexic, st->link[0]);
1134  tc2 = nasl_exec (lexic, st->link[1]);
1135  tc3 = alloc_expr_cell (0, EXPR_MODULO, tc1, tc2);
1136  ret2 = nasl_exec (lexic, tc3);
1137  ret = nasl_affect (tc1, ret2);
1138  deref_cell (tc3); /* Frees tc1 and tc2 */
1139  deref_cell (ret);
1140  return ret2;
1141 
1142  case NODE_L_SHIFT_EQ:
1143  tc1 = nasl_exec (lexic, st->link[0]);
1144  tc2 = nasl_exec (lexic, st->link[1]);
1145  tc3 = alloc_expr_cell (0, EXPR_L_SHIFT, tc1, tc2);
1146  ret2 = nasl_exec (lexic, tc3);
1147  ret = nasl_affect (tc1, ret2);
1148  deref_cell (tc3); /* Frees tc1 and tc2 */
1149  deref_cell (ret);
1150  return ret2;
1151 
1152  case NODE_R_SHIFT_EQ:
1153  tc1 = nasl_exec (lexic, st->link[0]);
1154  tc2 = nasl_exec (lexic, st->link[1]);
1155  tc3 = alloc_expr_cell (0, EXPR_R_SHIFT, tc1, tc2);
1156  ret2 = nasl_exec (lexic, tc3);
1157  ret = nasl_affect (tc1, ret2);
1158  deref_cell (tc3); /* Frees tc1 and tc2 */
1159  deref_cell (ret);
1160  return ret2;
1161 
1162  case NODE_R_USHIFT_EQ:
1163  tc1 = nasl_exec (lexic, st->link[0]);
1164  tc2 = nasl_exec (lexic, st->link[1]);
1165  tc3 = alloc_expr_cell (0, EXPR_R_USHIFT, tc1, tc2);
1166  ret2 = nasl_exec (lexic, tc3);
1167  ret = nasl_affect (tc1, ret2);
1168  deref_cell (tc3); /* Frees tc1 and tc2 */
1169  deref_cell (ret);
1170  return ret2;
1171 
1172  case NODE_VAR:
1173  /* val = variable name */
1174  ret = get_variable_by_name (lexic, st->x.str_val);
1175  return ret;
1176 
1177  case NODE_LOCAL: /* [0] = argdecl */
1178  ret = decl_local_variables (lexic, st->link[0]);
1179  return ret;
1180 
1181  case NODE_GLOBAL: /* [0] = argdecl */
1182  ret = decl_global_variables (lexic, st->link[0]);
1183  return ret;
1184 
1185  case EXPR_AND:
1186  x = cell2bool (lexic, st->link[0]);
1187  if (!x)
1188  return bool2cell (0);
1189 
1190  y = cell2bool (lexic, st->link[1]);
1191  return bool2cell (y);
1192 
1193  case EXPR_OR:
1194  x = cell2bool (lexic, st->link[0]);
1195  if (x)
1196  return bool2cell (x);
1197  y = cell2bool (lexic, st->link[1]);
1198  return bool2cell (y);
1199 
1200  case EXPR_NOT:
1201  x = cell2bool (lexic, st->link[0]);
1202  return bool2cell (!x);
1203 
1204  case EXPR_INCR:
1205  case EXPR_DECR:
1206  x = (st->type == EXPR_INCR) ? 1 : -1;
1207  if (st->link[0] == NULL)
1208  {
1209  y = 1; /* pre */
1210  tc1 = st->link[1];
1211  }
1212  else
1213  {
1214  y = 0; /* post */
1215  tc1 = st->link[0];
1216  }
1217  tc2 = nasl_exec (lexic, tc1);
1218  if (tc2 == NULL)
1219  return NULL;
1220  ret = nasl_incr_variable (lexic, tc2, y, x);
1221  deref_cell (tc2);
1222  return ret;
1223 
1224  case EXPR_PLUS:
1225  s1 = s2 = NULL;
1226  tc1 = cell2atom (lexic, st->link[0]);
1227 #ifdef STOP_AT_FIRST_ERROR
1228  if (tc1 == NULL || tc1 == FAKE_CELL)
1229  return NULL;
1230 #endif
1231  tc2 = cell2atom (lexic, st->link[1]);
1232  if (tc2 == NULL || tc2 == FAKE_CELL)
1233  {
1234 #ifdef STOP_AT_FIRST_ERROR
1235  deref_cell (tc1);
1236  return NULL;
1237 #else
1238  return tc1;
1239 #endif
1240  }
1241 
1242  if (tc1 == NULL || tc1 == FAKE_CELL)
1243  return tc2;
1244 
1245  /*
1246  * Anything added to a string is converted to a string
1247  * Otherwise anything added to an intger is converted into an integer
1248  */
1249  if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1250  flag = CONST_DATA;
1251  else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1252  flag = CONST_STR;
1253  else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1254  flag = CONST_INT;
1255  else
1256  flag = NODE_EMPTY;
1257  switch (flag)
1258  {
1259  long sz;
1260  case CONST_INT:
1261  x = tc1->x.i_val;
1262  y = cell2int (lexic, tc2);
1263  ret = int2cell (x + y);
1264  break;
1265 
1266  case CONST_STR:
1267  case CONST_DATA:
1268  s1 = s2 = NULL;
1269  if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1270  len1 = tc1->size;
1271  else
1272  {
1273  s1 = cell2str (lexic, tc1);
1274  len1 = (s1 == NULL ? 0 : strlen (s1));
1275  }
1276 
1277  if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1278  len2 = tc2->size;
1279  else
1280  {
1281  s2 = cell2str (lexic, tc2);
1282  len2 = (s2 == NULL ? 0 : strlen (s2));
1283  }
1284 
1285  sz = len1 + len2;
1286  s3 = g_malloc0 (sz + 1);
1287  if (len1 > 0)
1288  memcpy (s3, s1 != NULL ? s1 : tc1->x.str_val, len1);
1289  if (len2 > 0)
1290  memcpy (s3 + len1, s2 != NULL ? s2 : tc2->x.str_val, len2);
1291  g_free (s1);
1292  g_free (s2);
1293  ret = alloc_typed_cell (flag);
1294  ret->x.str_val = s3;
1295  ret->size = sz;
1296  break;
1297 
1298  default:
1299  ret = NULL;
1300  break;
1301  }
1302  deref_cell (tc1);
1303  deref_cell (tc2);
1304  return ret;
1305 
1306  case EXPR_MINUS: /* Infamous duplicated code */
1307  s1 = s2 = NULL;
1308  tc1 = cell2atom (lexic, st->link[0]);
1309 #ifdef STOP_AT_FIRST_ERROR
1310  if (tc1 == NULL || tc1 == FAKE_CELL)
1311  return NULL;
1312 #endif
1313  tc2 = cell2atom (lexic, st->link[1]);
1314  if (tc2 == NULL || tc2 == FAKE_CELL)
1315  {
1316 #ifdef STOP_AT_FIRST_ERROR
1317  deref_cell (tc1);
1318  return NULL;
1319 #else
1320  return tc1;
1321 #endif
1322  }
1323 
1324  if (tc1 == NULL || tc1 == FAKE_CELL)
1325  {
1326  if (tc2->type == CONST_INT)
1327  {
1328  y = cell2int (lexic, tc2);
1329  ret = int2cell (-y);
1330  }
1331  else
1332  ret = NULL;
1333  deref_cell (tc2);
1334  return ret;
1335  }
1336 
1337  /*
1338  * Anything subtracted from a string is converted to a string
1339  * Otherwise anything subtracted from integer is converted into an
1340  * integer
1341  */
1342  if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1343  flag = CONST_DATA;
1344  else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1345  flag = CONST_STR;
1346  else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1347  flag = CONST_INT;
1348  else
1349  flag = NODE_EMPTY;
1350  switch (flag)
1351  {
1352  case CONST_INT:
1353  x = cell2int (lexic, tc1);
1354  y = cell2int (lexic, tc2);
1355  ret = int2cell (x - y);
1356  break;
1357 
1358  case CONST_STR:
1359  case CONST_DATA:
1360  if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1361  {
1362  p1 = tc1->x.str_val;
1363  len1 = tc1->size;
1364  }
1365  else
1366  {
1367  p1 = s1 = cell2str (lexic, tc1);
1368  len1 = (s1 == NULL ? 0 : strlen (s1));
1369  }
1370 
1371  if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1372  {
1373  p2 = tc2->x.str_val;
1374  len2 = tc2->size;
1375  }
1376  else
1377  {
1378  p2 = s2 = cell2str (lexic, tc2);
1379  len2 = (s2 == NULL ? 0 : strlen (s2));
1380  }
1381 
1382  if (len2 == 0 || len1 < len2
1383  || (p = memmem (p1, len1, p2, len2)) == NULL)
1384  {
1385  s3 = g_malloc0 (len1 + 1);
1386  memcpy (s3, p1, len1);
1387  ret = alloc_typed_cell (flag);
1388  ret->x.str_val = s3;
1389  ret->size = len1;
1390  }
1391  else
1392  {
1393  long sz = len1 - len2;
1394  if (sz <= 0)
1395  {
1396  sz = 0;
1397  s3 = g_strdup ("");
1398  }
1399  else
1400  {
1401  s3 = g_malloc0 (sz + 1);
1402  if (p - p1 > 0)
1403  memcpy (s3, p1, p - p1);
1404  if (sz > p - p1)
1405  memcpy (s3 + (p - p1), p + len2, sz - (p - p1));
1406  }
1407  ret = alloc_typed_cell (flag);
1408  ret->x.str_val = s3;
1409  ret->size = sz;
1410  }
1411 
1412  g_free (s1);
1413  g_free (s2);
1414  break;
1415 
1416  default:
1417  ret = NULL;
1418  break;
1419  }
1420  deref_cell (tc1);
1421  deref_cell (tc2);
1422  return ret;
1423 
1424  case EXPR_MULT:
1425  x = cell2intW (lexic, st->link[0]);
1426  y = cell2intW (lexic, st->link[1]);
1427  return int2cell (x * y);
1428 
1429  case EXPR_DIV:
1430  x = cell2intW (lexic, st->link[0]);
1431  y = cell2intW (lexic, st->link[1]);
1432  if (y != 0)
1433  return int2cell (x / y);
1434  else
1435  return int2cell (0);
1436 
1437  case EXPR_EXPO:
1438  x = cell2intW (lexic, st->link[0]);
1439  y = cell2intW (lexic, st->link[1]);
1440  return int2cell (expo (x, y));
1441 
1442  case EXPR_MODULO:
1443  x = cell2intW (lexic, st->link[0]);
1444  y = cell2intW (lexic, st->link[1]);
1445  if (y != 0)
1446  return int2cell (x % y);
1447  else
1448  return int2cell (0);
1449 
1450  case EXPR_BIT_AND:
1451  x = cell2intW (lexic, st->link[0]);
1452  y = cell2intW (lexic, st->link[1]);
1453  return int2cell (x & y);
1454 
1455  case EXPR_BIT_OR:
1456  x = cell2intW (lexic, st->link[0]);
1457  y = cell2intW (lexic, st->link[1]);
1458  return int2cell (x | y);
1459 
1460  case EXPR_BIT_XOR:
1461  x = cell2intW (lexic, st->link[0]);
1462  y = cell2intW (lexic, st->link[1]);
1463  return int2cell (x ^ y);
1464 
1465  case EXPR_BIT_NOT:
1466  x = cell2intW (lexic, st->link[0]);
1467  return int2cell (~x);
1468 
1469  case EXPR_U_MINUS:
1470  x = cell2intW (lexic, st->link[0]);
1471  return int2cell (-x);
1472 
1473  /* TBD: Handle shift for strings and arrays */
1474  case EXPR_L_SHIFT:
1475  x = cell2intW (lexic, st->link[0]);
1476  y = cell2intW (lexic, st->link[1]);
1477  return int2cell (x << y);
1478 
1479  case EXPR_R_SHIFT: /* arithmetic right shift */
1480  x = cell2intW (lexic, st->link[0]);
1481  y = cell2intW (lexic, st->link[1]);
1482  z = x >> y;
1483 #ifndef __GNUC__
1484  if (x < 0 && z >= 0) /* Fix it */
1485  z |= (~0) << (sizeof (x) * 8 - y);
1486 #endif
1487  return int2cell (z);
1488 
1489  case EXPR_R_USHIFT:
1490  x = cell2intW (lexic, st->link[0]);
1491  y = cell2intW (lexic, st->link[1]);
1492  z = (unsigned) x >> (unsigned) y;
1493 #ifndef __GNUC__
1494  if (x < 0 && z <= 0) /* Fix it! */
1495  z &= ~((~0) << (sizeof (x) * 8 - y));
1496 #endif
1497  return int2cell (z);
1498 
1499  case COMP_MATCH:
1500  case COMP_NOMATCH:
1501  tc1 = cell2atom (lexic, st->link[0]);
1502  tc2 = cell2atom (lexic, st->link[1]);
1503  s1 = s2 = NULL;
1504 
1505  if (tc1 == NULL || tc1 == FAKE_CELL)
1506  {
1507  p1 = "";
1508  len1 = 0;
1509  }
1510  else if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1511  {
1512  p1 = tc1->x.str_val;
1513  len1 = tc1->size;
1514  }
1515  else
1516  {
1517  p1 = s1 = cell2str (lexic, tc1);
1518  len1 = strlen (s1);
1519  }
1520 
1521  if (tc2 == NULL || tc2 == FAKE_CELL)
1522  {
1523  p2 = "";
1524  len2 = 0;
1525  }
1526  else if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1527  {
1528  p2 = tc2->x.str_val;
1529  len2 = tc2->size;
1530  }
1531  else
1532  {
1533  p2 = s2 = cell2str (lexic, tc2);
1534  len2 = strlen (s2);
1535  }
1536 
1537  if (len1 <= len2)
1538  flag = (memmem (p2, len2, p1, len1) != NULL);
1539  else
1540  flag = 0;
1541 
1542  g_free (s1);
1543  g_free (s2);
1544  deref_cell (tc1);
1545  deref_cell (tc2);
1546  if (st->type == COMP_MATCH)
1547  return bool2cell (flag);
1548  else
1549  return bool2cell (!flag);
1550 
1551  case COMP_RE_MATCH:
1552  case COMP_RE_NOMATCH:
1553  if (st->x.ref_val == NULL)
1554  {
1555  nasl_perror (lexic, "nasl_exec: bad regex at or near line %d\n",
1556  st->line_nb);
1557  return NULL;
1558  }
1559  s1 = cell2str (lexic, st->link[0]);
1560  if (s1 == NULL)
1561  return 0;
1562  flag = regexec (st->x.ref_val, s1, 0, NULL, 0);
1563  g_free (s1);
1564  if (st->type == COMP_RE_MATCH)
1565  return bool2cell (flag != REG_NOMATCH);
1566  else
1567  return bool2cell (flag == REG_NOMATCH);
1568 
1569  case COMP_LT:
1570  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) < 0);
1571 
1572  case COMP_LE:
1573  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) <= 0);
1574 
1575  case COMP_EQ:
1576  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) == 0);
1577 
1578  case COMP_NE:
1579  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) != 0);
1580 
1581  case COMP_GT:
1582  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) > 0);
1583 
1584  case COMP_GE:
1585  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) >= 0);
1586 
1587  case REF_ARRAY:
1588  case DYN_ARRAY:
1589  case CONST_INT:
1590  case CONST_STR:
1591  case CONST_DATA:
1592  ref_cell (st); /* nasl_exec returns a cell that should be deref-ed */
1593  return st;
1594 
1595  case REF_VAR:
1596  ret = nasl_read_var_ref (lexic, st);
1597  return ret;
1598 
1599  default:
1600  nasl_perror (lexic, "nasl_exec: unhandled node type %d\n", st->type);
1601  abort ();
1602  return NULL;
1603  }
1604 }
tree_cell * nasl_return(lex_ctxt *ctxt, tree_cell *retv)
Definition: nasl_func.c:239
#define FAKE_CELL
Definition: nasl_tree.h:119
FILE * nasl_trace_fp
Definition: exec.c:368
static long int expo(long int x, long int y)
Definition: exec.c:756
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:178
static long int cell2int(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:135
static tree_cell * bool2cell(long int x)
Definition: exec.c:155
static int cell2bool(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:55
const char * val
Definition: nasl_init.c:378
char * str_val
Definition: nasl_tree.h:112
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:205
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
tree_cell * nasl_incr_variable(lex_ctxt *, tree_cell *, int, int)
Definition: nasl_var.c:933
static void nasl_short_dump(FILE *fp, const tree_cell *c)
Definition: exec.c:662
tree_cell * decl_local_variables(lex_ctxt *, tree_cell *)
Definition: nasl_var.c:773
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:191
Definition: nasl_tree.h:104
union TC::@2 x
long int cell_cmp(lex_ctxt *lexic, tree_cell *c1, tree_cell *c2)
Definition: exec.c:229
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
static char * cell2str(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:161
tree_cell * nasl_func_call(lex_ctxt *lexic, const nasl_func *f, tree_cell *arg_list)
Definition: nasl_func.c:107
tree_cell * get_array_elem(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:223
nasl_iterator nasl_array_iterator(void *ctxt, tree_cell *c)
Definition: nasl_var.c:1178
nasl_array * a
Definition: nasl_var.h:78
tree_cell * nasl_iterate_array(nasl_iterator *it)
Definition: nasl_var.c:1214
static tree_cell * int2cell(long int x)
Definition: exec.c:147
tree_cell * nasl_read_var_ref(lex_ctxt *, tree_cell *)
Definition: nasl_var.c:847
void free_array(nasl_array *a)
Definition: nasl_var.c:354
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition: nasl_tree.c:74
tree_cell * decl_global_variables(lex_ctxt *, tree_cell *)
Definition: nasl_var.c:786
tree_cell * nasl_affect(tree_cell *lval, tree_cell *rval)
Definition: nasl_var.c:712
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
Definition: nasl_func.c:78
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
Definition: nasl_func.c:94
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition: exec.c:781
static long int cell2intW(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:141
int size
Definition: nasl_tree.h:109
Here is the call graph for this function:
Here is the caller graph for this function: