libamxo  4.3.4
Object Definition Language (ODL) parsing
amxo_parser_utils.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #ifndef _GNU_SOURCE
55 #define _GNU_SOURCE
56 #endif
57 
58 #include "amxo_parser_priv.h"
59 #include "amxo_parser_hooks_priv.h"
60 #include "amxo_parser.tab.h"
61 
62 static char* amxo_parser_get_resolver_name(const char* data) {
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 }
86 
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 }
97 
98 void amxo_parser_msg(amxo_parser_t* parser, const char* format, ...) {
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 }
109 
110 int amxo_parser_printf(amxo_parser_t* parser, const char* format, ...) {
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 }
123 
125  const char* fn_name,
126  amxo_fn_type_t type,
127  const char* data) {
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 }
171 
173  const char* lib_name,
174  const char* fn_name) {
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 }
202 
204  const char* action_name) {
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 }
243 
244 void amxo_parser_print(amxo_parser_t* pctx, const char* text) {
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 }
257 
258 void amxo_parser_resolve_value(amxo_parser_t* pctx, amxc_string_t* value) {
259  amxc_string_trim(value, NULL);
260  amxc_string_resolve(value, &pctx->config);
261 }
@ amxo_action_invalid
@ action_max
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)
enum _amxo_action amxo_action_t
bool amxo_parser_no_resolve(amxo_parser_t *parser)
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)
void amxo_parser_msg(amxo_parser_t *parser, const char *format,...)
int amxo_parser_printf(amxo_parser_t *parser, const char *format,...)
static char * amxo_parser_get_resolver_name(const char *data)
enum _amxo_fn_type amxo_fn_type_t
@ 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.
The ODL parser structure.
Definition: amxo_types.h:245
amxo_fn_ptr_t resolved_fn
Definition: amxo_types.h:266
amxc_string_t msg
Definition: amxo_types.h:259
amxc_var_t config
Definition: amxo_types.h:250
amxc_string_t * resolved_fn_name
Definition: amxo_types.h:267
const char * file
Definition: amxo_types.h:279
amxd_status_t status
Definition: amxo_types.h:258
uint32_t line
Definition: amxo_types.h:280