static int var_cmp(const void * a, const void * b) { anon_nasl_var ** pv1 = (anon_nasl_var **)a, ** pv2 = (anon_nasl_var**)b; tree_cell *t1, *t2; t1 = var2cell((anon_nasl_var*)*pv1); t2 = var2cell((anon_nasl_var*)*pv2); return cell_cmp(mylexic, t1, t2); }
tree_cell* get_variable_by_name(lex_ctxt* ctxt, const char* name) { named_nasl_var * v; if (name == NULL) return NULL; v = get_var_ref_by_name(ctxt, name, 1); return var2cell(&v->u); }
tree_cell* nasl_iterate_array(nasl_iterator* it) { anon_nasl_var *av; if (it == NULL || it->a == NULL) return NULL; if (it->i1 >= 0) { while (it->i1 < it->a->max_idx) { av = it->a->num_elt[it->i1 ++]; if (av != NULL && av->var_type != VAR2_UNDEF) return var2cell(av); } it->i1 = -1; } if (it->a->hash_elt == NULL) return NULL; if (it->v != NULL) it->v = it->v->next_var; do { while (it->v == NULL) if (it->iH >= VAR_NAME_HASH) return NULL; else it->v = it->a->hash_elt[it->iH ++]; while (it->v != NULL && it->v->u.var_type == VAR2_UNDEF) it->v = it->v->next_var; } while (it->v == NULL); return var2cell(&it->v->u); }
tree_cell* get_array_elem(lex_ctxt* ctxt, const char* name, tree_cell* idx) { named_nasl_var *v = get_var_ref_by_name(ctxt, name, 1); named_nasl_var *nv; anon_nasl_var *u, *av; tree_cell *tc, idx0; u = &v->u; if (idx == NULL) { #if NASL_DEBUG > 0 nasl_perror(ctxt, "get_array_elem: NULL index\n"); #endif /* Treat it as zero */ idx = &idx0; idx->x.i_val = 0; idx->type = CONST_INT; } switch (u->var_type) { case VAR2_UNDEF: /* We define the array here */ u->var_type = VAR2_ARRAY; case VAR2_ARRAY: switch(idx->type) { case CONST_INT: av = nasl_get_var_by_num(&u->v.v_arr, idx->x.i_val, 1); return var2cell(av); case CONST_STR: case CONST_DATA: nv = get_var_by_name(&u->v.v_arr, idx->x.str_val); return var2cell(nv != NULL ? &nv->u : NULL); default: nasl_perror(ctxt, "get_array_elem: unhandled index type 0x%x\n", idx->type); return NULL; } /*NOTREACHED*/ break; case VAR2_INT: nasl_perror(ctxt, "get_array_elem: variable %s is an integer\n", name); return NULL; case VAR2_STRING: case VAR2_DATA: if (idx->type == CONST_INT) { int l = u->v.v_str.s_siz; if (idx->x.i_val >= l) { nasl_perror(ctxt, "get_array_elem: requesting character after end of string %s (%d >= %d)\n", name, idx->x.i_val, l); tc = alloc_expr_cell(idx->line_nb, CONST_DATA /*CONST_STR*/, NULL, NULL); tc->x.str_val = estrdup(""); tc->size = 0; return tc; } else { if ( idx->x.i_val < 0) { nasl_perror(ctxt, "Negative index !\n"); return NULL; } tc = alloc_expr_cell(idx->line_nb, CONST_DATA /*CONST_STR*/, NULL, NULL); tc->x.str_val = emalloc(2); tc->x.str_val[0] = u->v.v_str.s_val[idx->x.i_val]; tc->x.str_val[1] = '\0'; tc->size = 1; return tc; } } else { nasl_perror(ctxt, "get_array_elem: Cannot use a non integer index (type 0x%x) in string\n", idx->type); return NULL; } /*NOTREACHED*/ break; default: nasl_perror(ctxt, "Severe bug: unknown variable type 0x%x %s\n", u->var_type, get_line_nb(idx)); return NULL; } /*NOTREACHED*/ return NULL; }