34 #define G_LOG_DOMAIN "lib nasl" 73 gchar *keywords[] = {
"ACT_UNKNOWN",
"description",
"NULL",
"SCRIPT_NAME",
74 "COMMAND_LINE",
"_FCT_ANON_ARGS", NULL};
76 for (i = 0; keywords[i] != NULL; i++)
77 *defined_var = g_slist_prepend (*defined_var, keywords[i]);
94 gchar *lala = g_strdup (((
func_info *) lelem)->func_name);
95 return g_strcmp0 (lala, data);
114 && !g_str_has_suffix (
nasl_name,
".inc"))
122 if ((finfo_aux = g_slist_find_custom (*def_func_tree, fdata->
caller_func,
143 return g_strcmp0 (lelem, data);
162 if (!g_strcmp0 (value,
"NO"))
163 *unusedfiles = g_slist_prepend (*unusedfiles, key);
176 if (filename != NULL)
178 nasl_perror (lexic,
"The included file '%s' is never used.",
191 GHashTable **include_files, GHashTable **func_fnames_tab,
192 gchar *err_fname, GSList **called_funcs, GSList **def_func_tree)
196 char *incname = NULL;
197 gchar *tmp_filename = NULL;
199 static gchar *current_fun_def = NULL;
206 g_hash_table_insert (*func_fnames_tab, g_strdup (st->
x.
str_val),
207 g_strdup (err_fname));
218 *def_func_tree = g_slist_prepend (*def_func_tree, finfo);
232 if (!g_slist_find_custom (*called_funcs, st->
x.
str_val,
240 current_fun_def = g_strdup (st->
x.
str_val);
242 g_hash_table_replace (*include_files, incname, g_strdup (
"NO"));
244 err_fname = g_strdup (incname);
248 for (i = 0; i < 4; i++)
251 include_files, func_fnames_tab, err_fname,
252 called_funcs, def_func_tree))
260 g_free (tmp_filename);
271 GHashTable **func_fnames_tab, gchar *err_fname,
272 GSList **called_funcs, GSList **def_func_tree)
277 char *incname = NULL;
278 static int defined_flag = 0;
285 if (!g_slist_find_custom (*called_funcs, st->
x.
str_val,
296 if (st->
x.
str_val != NULL && defined_flag == 1)
307 incname = g_hash_table_lookup (*func_fnames_tab, st->
x.
str_val);
311 GSList *called_f_aux;
312 called_f_aux = g_slist_find_custom (*def_func_tree, st->
x.
str_val,
314 if (called_f_aux != NULL)
321 if (*include_files && st->
x.
str_val)
323 if (g_hash_table_lookup (*include_files,
327 g_hash_table_replace (*include_files, incname, g_strdup (
"YES"));
330 if (g_strcmp0 (st->
x.
str_val,
"defined_func") == 0)
335 for (i = 0; i < 4; i++)
338 func_fnames_tab, err_fname, called_funcs,
353 GHashTable **func_fnames_tab, gchar *err_fname,
354 GSList **defined_var, GSList **called_funcs)
358 static int defined_fn_mode = 0;
359 static int defined_var_mode = 0;
360 static int def_glob_var = 0;
361 static GSList *local_var_list = NULL;
368 if (!g_slist_find_custom (*called_funcs, st->
x.
str_val,
375 if ((defined_fn_mode == 1 || def_glob_var) && st->
type !=
NODE_DECL)
384 && defined_var_mode == 0)
385 defined_var_mode = 1;
387 && defined_fn_mode == 0)
397 && (defined_var_mode == 1 || defined_fn_mode == 1))
401 if (!g_slist_find_custom (local_var_list, st->
x.
str_val,
403 *defined_var = g_slist_prepend (*defined_var, st->
x.
str_val);
404 defined_var_mode = 0;
412 if (defined_fn_mode == 1)
413 local_var_list = g_slist_prepend (local_var_list, st->
x.
str_val);
414 if (def_glob_var == 1)
415 *defined_var = g_slist_prepend (*defined_var, st->
x.
str_val);
421 *defined_var = g_slist_prepend (*defined_var, st->
x.
str_val);
426 if (!g_slist_find_custom (*defined_var, st->
x.
str_val,
428 && !g_slist_find_custom (local_var_list, st->
x.
str_val,
432 nasl_perror (lexic,
"The variable %s was not declared",
438 for (i = 0; i < 4; i++)
441 func_fnames_tab, err_fname, defined_var,
451 g_slist_free (local_var_list);
452 local_var_list = NULL;
474 g_slist_prepend (*called_funcs, g_strdup (st->
x.
str_val));
479 for (i = 0; i < 4; i++)
502 GHashTable *include_files = NULL;
503 GHashTable *func_fnames_tab = NULL;
504 GSList *unusedfiles = NULL;
505 GSList *called_funcs = NULL;
506 GSList *def_func_tree = NULL;
507 gchar *err_fname = NULL;
511 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
513 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
517 lexic_aux->
oid = lexic->
oid;
523 if ((ret =
nasl_lint_def (lexic_aux, st, lint_mode, &include_files,
524 &func_fnames_tab, err_fname, &called_funcs,
529 if ((ret =
nasl_lint_call (lexic_aux, st, &include_files, &func_fnames_tab,
530 err_fname, &called_funcs, &def_func_tree))
537 if (unusedfiles != NULL)
539 if ((g_slist_length (unusedfiles)) > 0)
548 nasl_lint_def (lexic, st, lint_mode, &include_files, &func_fnames_tab,
549 err_fname, &called_funcs, &def_func_tree))
554 GSList *defined_var = NULL;
557 err_fname, &defined_var, &called_funcs);
558 g_slist_free (defined_var);
562 g_slist_free (called_funcs);
564 g_slist_free_full (def_func_tree, (GDestroyNotify)
free_list_func);
565 def_func_tree = NULL;
566 g_hash_table_destroy (include_files);
567 include_files = NULL;
568 g_hash_table_destroy (func_fnames_tab);
569 func_fnames_tab = NULL;
571 g_slist_free (unusedfiles);
const char * nasl_get_filename(const char *function)
void free_list_func(func_info *data)
Free a func_info structure.
struct st_func_info func_info
Define struct to store information about a called function.
void nasl_set_filename(const char *filename)
gint list_cmp1(gconstpointer lelem, gconstpointer data)
This function is called by g_slist_find_custom.
struct script_infos * script_infos
void free_lex_ctxt(lex_ctxt *c)
Define struct to store information about a called function.
tree_cell * nasl_lint_call(lex_ctxt *lexic, tree_cell *st, GHashTable **include_files, GHashTable **func_fnames_tab, gchar *err_fname, GSList **called_funcs, GSList **def_func_tree)
Check if a called function was defined.
lex_ctxt * init_empty_lex_ctxt()
void add_nasl_library(GSList **list)
Add "built-in" variables to a list.
void nasl_perror(lex_ctxt *lexic, char *msg,...)
tree_cell * nasl_lint_defvar(lex_ctxt *lexic, tree_cell *st, GHashTable **include_files, GHashTable **func_fnames_tab, gchar *err_fname, GSList **defined_var, GSList **called_funcs)
Consider all cases in which a variable is set, and add it to a list. If a variable is read...
void check_called_files(gpointer key, gpointer value, GSList **unusedfiles)
This function is called by g_hash_table_foreach to check if an include file was used or not...
tree_cell * nasl_lint_def(lex_ctxt *lexic, tree_cell *st, int lint_mode, GHashTable **include_files, GHashTable **func_fnames_tab, gchar *err_fname, GSList **called_funcs, GSList **def_func_tree)
Loads all defined functions. Also, It constructs a tree of called functions to help recognize a not d...
tree_cell * nasl_lint(lex_ctxt *lexic, tree_cell *st)
Search for errors in a nasl script.
gint list_cmp(gconstpointer lelem, gconstpointer data)
This function is called by g_slist_find_custom.
gint reverse_search(GSList **def_func_tree, GSList *finfo)
Check if an undefined called function is needed or not. This is the case in which the function is cal...
void print_uncall_files(gpointer filename, gpointer lexic)
It shows a msg for unused included files.
tree_cell * make_call_func_list(lex_ctxt *lexic, tree_cell *st, GSList **called_funcs)
Make a list of all called functions.
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
void add_predef_varname(GSList **defined_var)
Add keywords to the varnames list.