OpenVAS Libraries  9.0.3
exec.h File Reference
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 216 of file exec.c.

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

◆ cell_cmp()

long int cell_cmp ( lex_ctxt ,
tree_cell ,
tree_cell  
)

Definition at line 240 of file exec.c.

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

References cell2atom(), cell_type(), CONST_DATA, CONST_INT, CONST_STR, deref_cell(), FAKE_CELL, and nasl_perror().

Here is the call 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 800 of file exec.c.

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

References struct_lex_ctxt::break_flag, struct_lex_ctxt::cont_flag, FAKE_CELL, struct_lex_ctxt::line_nb, TC::line_nb, nasl_dump_tree(), nasl_perror(), nasl_trace_fp, ref_cell(), and struct_lex_ctxt::ret_val.

Referenced by cell2atom(), and exec_nasl_script().

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