libamxo  4.3.4
Object Definition Language (ODL) parsing
amxo_parser_utils.c File Reference
#include "amxo_parser_priv.h"
#include "amxo_parser_hooks_priv.h"
#include "amxo_parser.tab.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static char * amxo_parser_get_resolver_name (const char *data)
 
bool amxo_parser_no_resolve (amxo_parser_t *parser)
 
void amxo_parser_msg (amxo_parser_t *parser, const char *format,...)
 
int amxo_parser_printf (amxo_parser_t *parser, const char *format,...)
 
int amxo_parser_resolve_internal (amxo_parser_t *pctx, const char *fn_name, amxo_fn_type_t type, const char *data)
 
int amxo_parser_call_entry_point (amxo_parser_t *pctx, const char *lib_name, const char *fn_name)
 
amxo_action_t amxo_parser_get_action_id (amxo_parser_t *pctx, const char *action_name)
 
void amxo_parser_print (amxo_parser_t *pctx, const char *text)
 
void amxo_parser_resolve_value (amxo_parser_t *pctx, amxc_string_t *value)
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 55 of file amxo_parser_utils.c.

Function Documentation

◆ amxo_parser_call_entry_point()

int amxo_parser_call_entry_point ( amxo_parser_t pctx,
const char *  lib_name,
const char *  fn_name 
)

Definition at line 172 of file amxo_parser_utils.c.

174  {
175  int retval = -1;
176  amxc_string_t data;
177  amxc_string_init(&data, 0);
178  amxc_string_setf(&data, "%s", lib_name);
179 
180  when_true_status(amxo_parser_no_resolve(pctx), exit, retval = 0);
181 
182  pctx->resolved_fn = NULL;
183  retval = amxo_parser_resolve(pctx, "import", fn_name, amxo_function_ep, amxc_string_get(&data, 0));
184  if(retval == 1) {
185  amxo_parser_msg(pctx,
186  "No entry point \"%s\" found using \"%s\"",
187  fn_name,
188  "import");
189  pctx->status = amxd_status_function_not_found;
190  }
191 
192  if(pctx->resolved_fn != NULL) {
194  retval = amxo_parser_add_entry_point(pctx, fn);
195  amxc_string_delete(&pctx->resolved_fn_name);
196  }
197 
198 exit:
199  amxc_string_clean(&data);
200  return retval;
201 }
PRIVATE int amxo_parser_resolve(amxo_parser_t *parser, const char *resolver_name, const char *func_name, amxo_fn_type_t type, const char *data)
bool amxo_parser_no_resolve(amxo_parser_t *parser)
void amxo_parser_msg(amxo_parser_t *parser, const char *format,...)
@ amxo_function_ep
Definition: amxo_types.h:86
int(* amxo_entry_point_t)(int reason, amxd_dm_t *dm, amxo_parser_t *parser)
Definition: amxo_types.h:218
int amxo_parser_add_entry_point(amxo_parser_t *parser, amxo_entry_point_t fn)
Adds an entry point function.
amxo_fn_ptr_t resolved_fn
Definition: amxo_types.h:266
amxc_string_t * resolved_fn_name
Definition: amxo_types.h:267
amxd_status_t status
Definition: amxo_types.h:258

◆ amxo_parser_get_action_id()

amxo_action_t amxo_parser_get_action_id ( amxo_parser_t pctx,
const char *  action_name 
)

Definition at line 203 of file amxo_parser_utils.c.

204  {
205  static const char* names[] = {
206  "any",
207  "read",
208  "write",
209  "validate",
210  "list",
211  "describe",
212  "add-inst",
213  "del-inst",
214  "destroy",
215  "translate",
216  "apply"
217  };
219  amxc_var_t* ca = GET_ARG(&pctx->config, "_current_action");
220 
221  for(int i = 0; i <= action_max; i++) {
222  if(strcmp(action_name, names[i]) == 0) {
223  action_id = (amxo_action_t) i;
224  break;
225  }
226  }
227 
228  if(action_id < 0) {
229  pctx->status = amxd_status_invalid_action;
230  amxo_parser_msg(pctx,
231  "Invalid action name \"%s\"",
232  action_name);
233  }
234 
235  if(ca == NULL) {
236  amxc_var_add_key(uint32_t, &pctx->config, "_current_action", action_id);
237  } else {
238  amxc_var_set(uint32_t, ca, action_id);
239  }
240 
241  return action_id;
242 }
@ amxo_action_invalid
@ action_max
enum _amxo_action amxo_action_t
amxc_var_t config
Definition: amxo_types.h:250

◆ amxo_parser_get_resolver_name()

static char* amxo_parser_get_resolver_name ( const char *  data)
static

Definition at line 62 of file amxo_parser_utils.c.

62  {
63  amxc_string_t full_data;
64  size_t length = strlen(data);
65  amxc_llist_t parts;
66  amxc_llist_it_t* it = NULL;
67  char* name = NULL;
68 
69  amxc_llist_init(&parts);
70  amxc_string_init(&full_data, length + 1);
71  amxc_string_set_at(&full_data, 0, data, length, amxc_string_overwrite);
72  amxc_string_split_to_llist(&full_data, &parts, ':');
73  it = amxc_llist_take_first(&parts);
74  amxc_llist_clean(&parts, amxc_string_list_it_free);
75  amxc_string_clean(&full_data);
76 
77  when_null(it, exit);
78 
79  amxc_string_trim(amxc_string_from_llist_it(it), NULL);
80  name = amxc_string_take_buffer(amxc_string_from_llist_it(it));
81  amxc_llist_it_clean(it, amxc_string_list_it_free);
82 
83 exit:
84  return name;
85 }

◆ amxo_parser_msg()

void amxo_parser_msg ( amxo_parser_t parser,
const char *  format,
  ... 
)

Definition at line 98 of file amxo_parser_utils.c.

98  {
99  va_list args;
100 
101  amxc_string_reset(&parser->msg);
102  va_start(args, format);
103  amxc_string_vsetf(&parser->msg, format, args);
104  va_end(args);
105  va_start(args, format);
106  vsyslog(LOG_PID | LOG_USER, format, args);
107  va_end(args);
108 }
amxc_string_t msg
Definition: amxo_types.h:259

◆ amxo_parser_no_resolve()

bool amxo_parser_no_resolve ( amxo_parser_t parser)

Definition at line 87 of file amxo_parser_utils.c.

87  {
88  amxc_var_t* var_resolve = GET_ARG(&parser->config, "odl-resolve");
89  bool resolve = true;
90 
91  if(var_resolve != NULL) {
92  resolve = amxc_var_dyncast(bool, var_resolve);
93  }
94 
95  return !resolve;
96 }

◆ amxo_parser_print()

void amxo_parser_print ( amxo_parser_t pctx,
const char *  text 
)

Definition at line 244 of file amxo_parser_utils.c.

244  {
245  amxc_string_t print_txt;
246 
247  amxc_string_init(&print_txt, 0);
248 
249  if(amxc_string_set_resolved(&print_txt, text, &pctx->config) > 0) {
250  text = amxc_string_get(&print_txt, 0);
251  }
252 
253  printf("%s (%s@%d)\n", text, pctx->file, pctx->line);
254 
255  amxc_string_clean(&print_txt);
256 }
const char * file
Definition: amxo_types.h:279
uint32_t line
Definition: amxo_types.h:280

◆ amxo_parser_printf()

int amxo_parser_printf ( amxo_parser_t parser,
const char *  format,
  ... 
)

Definition at line 110 of file amxo_parser_utils.c.

110  {
111  bool silent = GET_BOOL(&parser->config, "silent");
112  va_list args;
113  va_start(args, format);
114  if(!silent) {
115  vfprintf(stderr, format, args);
116  }
117  va_end(args);
118  va_start(args, format);
119  vsyslog(LOG_PID | LOG_USER, format, args);
120  va_end(args);
121  return 0;
122 }

◆ amxo_parser_resolve_internal()

int amxo_parser_resolve_internal ( amxo_parser_t pctx,
const char *  fn_name,
amxo_fn_type_t  type,
const char *  data 
)

Definition at line 124 of file amxo_parser_utils.c.

127  {
128  int retval = -1;
129  char* name = NULL;
130  const char* res_data = NULL;
131 
132  when_true_status(amxo_parser_no_resolve(pctx), exit, retval = 0);
133 
134  if((data == NULL) || (data[0] == '\0')) {
135  amxo_parser_msg(pctx, "Resolver name must be provide (is empty)");
136  pctx->status = amxd_status_invalid_name;
137  goto exit;
138  }
139 
140  name = amxo_parser_get_resolver_name(data);
141  if((name == NULL) || (name[0] == '\0')) {
142  amxo_parser_msg(pctx, "Resolver name must be provide (is empty)");
143  pctx->status = amxd_status_invalid_name;
144  goto exit;
145  }
146  res_data = data + strlen(name);
147  while(isspace(res_data[0]) || res_data[0] == ':') {
148  res_data++;
149  }
150 
151  pctx->resolved_fn = NULL;
152  retval = amxo_parser_resolve(pctx, name, fn_name, type, res_data);
153  if(retval == -1) {
154  pctx->status = amxd_status_invalid_name;
155  amxo_parser_msg(pctx, "No function resolver found with name \"%s\"", name);
156  } else if(retval == 1) {
157  pctx->status = amxd_status_function_not_found;
158  amxo_parser_msg(pctx,
159  "No function implemention found for \"%s\" using \"%s\"",
160  fn_name,
161  name);
162  } else if(retval == 0) {
163  amxc_string_new(&pctx->resolved_fn_name, 0);
164  amxc_string_set(pctx->resolved_fn_name, fn_name);
165  }
166 
167 exit:
168  free(name);
169  return retval;
170 }
static char * amxo_parser_get_resolver_name(const char *data)

◆ amxo_parser_resolve_value()

void amxo_parser_resolve_value ( amxo_parser_t pctx,
amxc_string_t *  value 
)

Definition at line 258 of file amxo_parser_utils.c.

258  {
259  amxc_string_trim(value, NULL);
260  amxc_string_resolve(value, &pctx->config);
261 }