OpenVAS Scanner  7.0.1~git
nasl_func.c File Reference
#include "nasl_func.h"
#include "exec.h"
#include "nasl_debug.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for nasl_func.c:

Go to the source code of this file.

Macros

#define TRACE_BUF_SZ   255
 

Functions

static nasl_funcget_func (lex_ctxt *ctxt, const char *name)
 This function climbs up in the context list and searches for a given. More...
 
nasl_funcinsert_nasl_func (lex_ctxt *lexic, const char *fname, tree_cell *decl_node, int lint_mode)
 
tree_celldecl_nasl_func (lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
 
nasl_funcget_func_ref_by_name (lex_ctxt *ctxt, const char *name)
 
tree_cellnasl_func_call (lex_ctxt *lexic, const nasl_func *f, tree_cell *arg_list)
 
tree_cellnasl_return (lex_ctxt *ctxt, tree_cell *retv)
 
void free_func (nasl_func *f)
 

Variables

FILE * nasl_trace_fp
 

Macro Definition Documentation

◆ TRACE_BUF_SZ

#define TRACE_BUF_SZ   255

Referenced by nasl_func_call().

Function Documentation

◆ decl_nasl_func()

tree_cell* decl_nasl_func ( lex_ctxt lexic,
tree_cell decl_node,
int  lint_mode 
)

Definition at line 78 of file nasl_func.c.

References FAKE_CELL, insert_nasl_func(), nasl_perror(), TC::str_val, and TC::x.

Referenced by nasl_exec(), nasl_lint_call(), and nasl_lint_def().

79 {
80  if (decl_node == NULL || decl_node == FAKE_CELL)
81  {
82  nasl_perror (lexic, "Cannot insert NULL or FAKE cell as function\n");
83  return NULL;
84  }
85 
86  if (insert_nasl_func (lexic, decl_node->x.str_val, decl_node, lint_mode)
87  == NULL)
88  return NULL;
89  else
90  return FAKE_CELL;
91 }
#define FAKE_CELL
Definition: nasl_tree.h:119
char * str_val
Definition: nasl_tree.h:112
union TC::@2 x
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
nasl_func * insert_nasl_func(lex_ctxt *lexic, const char *fname, tree_cell *decl_node, int lint_mode)
Definition: nasl_func.c:53
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_func()

void free_func ( nasl_func f)

Definition at line 269 of file nasl_func.c.

References st_nasl_func::func_name.

Referenced by init_empty_lex_ctxt().

270 {
271  if (!f)
272  return;
273 
274  g_free (f->func_name);
275  g_free (f);
276 }
char * func_name
Definition: nasl_func.h:27
Here is the caller graph for this function:

◆ get_func()

static nasl_func* get_func ( lex_ctxt ctxt,
const char *  name 
)
static

This function climbs up in the context list and searches for a given.

function.

Definition at line 37 of file nasl_func.c.

References func_is_internal(), struct_lex_ctxt::functions, name, and struct_lex_ctxt::up_ctxt.

Referenced by get_func_ref_by_name(), and insert_nasl_func().

38 {
39  lex_ctxt *c;
40 
41  for (c = ctxt; c != NULL; c = c->up_ctxt)
42  {
43  nasl_func *v = g_hash_table_lookup (c->functions, name);
44 
45  if (v)
46  return v;
47  }
48 
49  return func_is_internal (name);
50 }
const char * name
Definition: nasl_init.c:377
nasl_func * func_is_internal(const char *)
Definition: nasl_init.c:486
GHashTable * functions
Definition: nasl_lex_ctxt.h:48
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_func_ref_by_name()

nasl_func* get_func_ref_by_name ( lex_ctxt ctxt,
const char *  name 
)

Definition at line 94 of file nasl_func.c.

References get_func(), and name.

Referenced by exec_nasl_script(), make_call_func_list(), nasl_defined_func(), nasl_exec(), nasl_lint_call(), and nasl_lint_def().

95 {
96  nasl_func *f;
97 
98  if ((f = get_func (ctxt, name)))
99  return f;
100  else
101  return NULL;
102 }
static nasl_func * get_func(lex_ctxt *ctxt, const char *name)
This function climbs up in the context list and searches for a given.
Definition: nasl_func.c:37
const char * name
Definition: nasl_init.c:377
Here is the call graph for this function:
Here is the caller graph for this function:

◆ insert_nasl_func()

nasl_func* insert_nasl_func ( lex_ctxt lexic,
const char *  fname,
tree_cell decl_node,
int  lint_mode 
)

Definition at line 53 of file nasl_func.c.

References st_nasl_func::block, FAKE_CELL, st_nasl_func::func_name, struct_lex_ctxt::functions, get_func(), TC::link, nasl_perror(), and ref_cell().

Referenced by decl_nasl_func().

55 {
56  nasl_func *pf;
57 
58  if (get_func (lexic, fname))
59  {
60  if (lint_mode == 0)
61  nasl_perror (
62  lexic, "insert_nasl_func: function '%s' is already defined\n", fname);
63  return NULL;
64  }
65  pf = g_malloc0 (sizeof (nasl_func));
66  pf->func_name = g_strdup (fname);
67 
68  if (decl_node != NULL && decl_node != FAKE_CELL)
69  {
70  pf->block = decl_node->link[1];
71  ref_cell (pf->block);
72  }
73  g_hash_table_insert (lexic->functions, pf->func_name, pf);
74  return pf;
75 }
#define FAKE_CELL
Definition: nasl_tree.h:119
void * block
Definition: nasl_func.h:28
struct TC * link[4]
Definition: nasl_tree.h:116
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:178
char * func_name
Definition: nasl_func.h:27
static nasl_func * get_func(lex_ctxt *ctxt, const char *name)
This function climbs up in the context list and searches for a given.
Definition: nasl_func.c:37
GHashTable * functions
Definition: nasl_lex_ctxt.h:48
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_func_call()

tree_cell* nasl_func_call ( lex_ctxt lexic,
const nasl_func f,
tree_cell arg_list 
)

Definition at line 107 of file nasl_func.c.

References add_named_var_to_ctxt(), add_numbered_var_to_ctxt(), st_nasl_func::block, cell2atom(), deref_cell(), dump_cell_val(), FAKE_CELL, struct_lex_ctxt::fct_ctxt, free_lex_ctxt(), func_is_internal(), st_nasl_func::func_name, init_empty_lex_ctxt(), TC::link, nasl_dump_tree(), nasl_exec(), nasl_get_filename(), nasl_get_function_name(), nasl_is_leaf(), nasl_perror(), nasl_set_filename(), nasl_set_function_name(), nasl_trace(), nasl_trace_enabled(), nasl_trace_fp, struct_lex_ctxt::oid, struct_lex_ctxt::recv_timeout, ref_cell(), struct_lex_ctxt::ret_val, struct_lex_ctxt::script_infos, TC::str_val, TRACE_BUF_SZ, struct_lex_ctxt::up_ctxt, and TC::x.

Referenced by exec_nasl_script(), and nasl_exec().

108 {
109  int nb_u = 0, nb_a = 0;
110  tree_cell *pc = NULL, *pc2 = NULL, *retc = NULL;
111  lex_ctxt *lexic2 = NULL;
112  char *trace_buf = NULL;
113  char *temp_funname = NULL, *tmp_filename = NULL;
114  int trace_buf_len = 0, tn;
115 #define TRACE_BUF_SZ 255
116 
117  /* 1. Create a new context */
118  lexic2 = init_empty_lex_ctxt ();
119  lexic2->script_infos = lexic->script_infos;
120  lexic2->oid = lexic->oid;
121  lexic2->recv_timeout = lexic->recv_timeout;
122  lexic2->fct_ctxt = 1;
123 
124  if (nasl_trace_fp != NULL)
125  {
126  trace_buf = g_malloc0 (TRACE_BUF_SZ);
127  tn = snprintf (trace_buf, TRACE_BUF_SZ, "Call %s(", f->func_name);
128  if (tn > 0)
129  trace_buf_len += tn;
130  }
131 
132  for (pc = arg_list; pc != NULL; pc = pc->link[1])
133  if (pc->x.str_val == NULL)
134  nb_u++;
135 
136  /*
137  * I should look exactly how unnamed arguments works...
138  * Or maybe I should remove this feature?
139  */
140 
141  for (nb_u = 0, pc = arg_list; pc != NULL; pc = pc->link[1])
142  {
143  pc2 = cell2atom (lexic, pc->link[0]);
144  if (pc->x.str_val == NULL)
145  {
146  /* 2. Add unnamed (numbered) variables for unnamed args */
147  if (add_numbered_var_to_ctxt (lexic2, nb_u, pc2) == NULL)
148  goto error;
149  nb_u++;
150  if (nasl_trace_fp != NULL && trace_buf_len < TRACE_BUF_SZ)
151  {
152  tn = snprintf (trace_buf + trace_buf_len,
153  TRACE_BUF_SZ - trace_buf_len, "%s%d: %s",
154  nb_a > 0 ? ", " : "", nb_u, dump_cell_val (pc2));
155  if (tn > 0)
156  trace_buf_len += tn;
157  }
158  nb_a++;
159  }
160  else
161  {
162  /* 3. and add named variables for named args */
163  if (add_named_var_to_ctxt (lexic2, pc->x.str_val, pc2) == NULL)
164  goto error;
165  if (nasl_trace_fp != NULL && trace_buf_len < TRACE_BUF_SZ)
166  {
167  tn = snprintf (trace_buf + trace_buf_len,
168  TRACE_BUF_SZ - trace_buf_len, "%s%s: %s",
169  nb_a > 0 ? ", " : "", pc->x.str_val,
170  dump_cell_val (pc2));
171  if (tn > 0)
172  trace_buf_len += tn;
173  }
174  nb_a++;
175  }
176  deref_cell (pc2);
177  }
178 
179  if (nasl_trace_fp != NULL)
180  {
181  if (trace_buf_len < TRACE_BUF_SZ)
182  nasl_trace (lexic, "NASL> %s)\n", trace_buf);
183  else
184  nasl_trace (lexic, "NASL> %s ...)\n", trace_buf);
185  g_free (trace_buf);
186  }
187 
188  /* 4. Chain new context to old (lexic) */
189  lexic2->up_ctxt = lexic;
190  /* 5. Execute */
191  tmp_filename = g_strdup (nasl_get_filename (NULL));
193  if (func_is_internal (f->func_name))
194  {
195  tree_cell *(*pf2) (lex_ctxt *) = f->block;
196  retc = pf2 (lexic2);
197  }
198  else
199  {
200  temp_funname = g_strdup (nasl_get_function_name ());
202  retc = nasl_exec (lexic2, f->block);
203  deref_cell (retc);
204  retc = FAKE_CELL;
205  nasl_set_function_name (temp_funname);
206  g_free (temp_funname);
207  }
208  nasl_set_filename (tmp_filename);
209  g_free (tmp_filename);
210 
211  if ((retc == NULL || retc == FAKE_CELL)
212  && (lexic2->ret_val != NULL && lexic2->ret_val != FAKE_CELL))
213  {
214  retc = lexic2->ret_val;
215  ref_cell (retc);
216  }
217 
218  if (nasl_trace_enabled ())
219  nasl_trace (lexic, "NASL> Return %s: %s\n", f->func_name,
220  dump_cell_val (retc));
221  if (!nasl_is_leaf (retc))
222  {
223  nasl_perror (lexic,
224  "nasl_func_call: return value from %s is not atomic!\n",
225  f->func_name);
226  nasl_dump_tree (retc);
227  }
228 
229  free_lex_ctxt (lexic2);
230  lexic2 = NULL;
231  return retc;
232 
233 error:
234  free_lex_ctxt (lexic2);
235  return NULL;
236 }
int nasl_trace_enabled(void)
Checks if the nasl_trace_fp is set.
Definition: nasl_debug.c:170
#define FAKE_CELL
Definition: nasl_tree.h:119
void * block
Definition: nasl_func.h:28
struct TC * link[4]
Definition: nasl_tree.h:116
const char * nasl_get_filename(const char *function)
Definition: nasl_debug.c:69
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:178
FILE * nasl_trace_fp
Definition: exec.c:368
char * str_val
Definition: nasl_tree.h:112
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:377
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:205
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
char * func_name
Definition: nasl_func.h:27
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:97
struct script_infos * script_infos
Definition: nasl_lex_ctxt.h:41
const char * nasl_get_function_name()
Definition: nasl_debug.c:91
void free_lex_ctxt(lex_ctxt *c)
Definition: nasl_lex_ctxt.c:55
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:184
lex_ctxt * init_empty_lex_ctxt()
Definition: nasl_lex_ctxt.c:32
int nasl_is_leaf(const tree_cell *pc)
Definition: nasl_tree.c:400
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:825
Definition: nasl_tree.h:104
union TC::@2 x
char * dump_cell_val(const tree_cell *c)
Definition: nasl_tree.c:245
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:37
#define TRACE_BUF_SZ
nasl_func * func_is_internal(const char *)
Definition: nasl_init.c:486
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
void nasl_set_function_name(const char *funname)
Definition: nasl_debug.c:82
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:36
anon_nasl_var * add_numbered_var_to_ctxt(lex_ctxt *, int, tree_cell *)
Definition: nasl_var.c:796
const char * oid
Definition: nasl_lex_ctxt.h:42
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition: exec.c:781
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_return()

tree_cell* nasl_return ( lex_ctxt ctxt,
tree_cell retv 
)

Definition at line 239 of file nasl_func.c.

References cell2atom(), copy_ref_array(), deref_cell(), FAKE_CELL, struct_lex_ctxt::fct_ctxt, REF_ARRAY, ref_cell(), struct_lex_ctxt::ret_val, TC::type, and struct_lex_ctxt::up_ctxt.

Referenced by nasl_exec().

240 {
241  tree_cell *c;
242 
243  retv = cell2atom (ctxt, retv);
244  if (retv == NULL)
245  retv = FAKE_CELL;
246 
247  if (retv != FAKE_CELL && retv->type == REF_ARRAY)
248  /* We have to "copy" it as the referenced array will be freed */
249  {
250  c = copy_ref_array (retv);
251  deref_cell (retv);
252  retv = c;
253  }
254 
255  while (ctxt != NULL)
256  {
257  ctxt->ret_val = retv;
258  ref_cell (retv);
259  if (ctxt->fct_ctxt)
260  break;
261  ctxt = ctxt->up_ctxt;
262  }
263  /* Bug? Do not return NULL, as we may test it to break the control flow */
264  deref_cell (retv);
265  return FAKE_CELL;
266 }
#define FAKE_CELL
Definition: nasl_tree.h:119
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:178
tree_cell * copy_ref_array(const tree_cell *c1)
Definition: nasl_var.c:549
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
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:37
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:36
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ nasl_trace_fp

FILE* nasl_trace_fp

Definition at line 368 of file exec.c.

Referenced by nasl_func_call().