blob: a15fb72897a8572b1dfd3459266eec1b5a689011 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file common.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief common internal definitions for libyang
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_COMMON_H_
16#define LY_COMMON_H_
17
18#include <stdint.h>
19#include <stdlib.h>
20
Radek Krejci2c22f122018-09-05 15:08:03 +020021#include "config.h"
Radek Krejciad573502018-09-07 15:26:55 +020022#include "log.h"
Radek Krejcic59bc972018-09-17 16:13:06 +020023#include "tree_schema.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020024
25#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
26# define THREAD_LOCAL _Thread_local
27#elif defined __GNUC__ || \
28 defined __SUNPRO_C || \
29 defined __xlC__
30# define THREAD_LOCAL __thread
31#else
32# error "Cannot define THREAD_LOCAL"
33#endif
34
Radek Krejcic59bc972018-09-17 16:13:06 +020035#define GETMACRO1(_1, NAME, ...) NAME
Radek Krejci5aeea3a2018-09-05 13:29:36 +020036#define GETMACRO2(_1, _2, NAME, ...) NAME
37#define GETMACRO3(_1, _2, _3, NAME, ...) NAME
38#define GETMACRO4(_1, _2, _3, _4, NAME, ...) NAME
39
40/*
Radek Krejcic59bc972018-09-17 16:13:06 +020041 * If the compiler supports attribute to mark objects as hidden, mark all
42 * objects as hidden and export only objects explicitly marked to be part of
43 * the public API.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020044 */
Radek Krejcic59bc972018-09-17 16:13:06 +020045#define API __attribute__((visibility("default")))
46
47/******************************************************************************
48 * Logger
49 *****************************************************************************/
Radek Krejci5aeea3a2018-09-05 13:29:36 +020050
51/* internal logging options */
52enum int_log_opts {
53 ILO_LOG = 0, /* log normally */
54 ILO_STORE, /* only store any messages, they will be processed higher on stack */
55 ILO_IGNORE, /* completely ignore messages */
56 ILO_ERR2WRN, /* change errors to warnings */
57};
58
Radek Krejci94aa9942018-09-07 17:12:17 +020059enum LY_VLOG_ELEM {
60 LY_VLOG_NONE = 0,
61 LY_VLOG_LINE,/* line number */
62 LY_VLOG_LYS, /* struct lysc_node* */
63 LY_VLOG_LYD, /* struct lyd_node* */
64 LY_VLOG_STR, /* const char* */
65 LY_VLOG_PREV /* use exact same previous path */
66};
67
Radek Krejci5aeea3a2018-09-05 13:29:36 +020068extern THREAD_LOCAL enum int_log_opts log_opt;
69extern volatile uint8_t ly_log_level;
70extern volatile uint8_t ly_log_opts;
71
Radek Krejciad573502018-09-07 15:26:55 +020072void ly_err_free(void *ptr);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020073void ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...);
Radek Krejci94aa9942018-09-07 17:12:17 +020074void ly_vlog(const struct ly_ctx *ctx, enum LY_VLOG_ELEM elem_type, const void *elem, LY_VECODE code, const char *format, ...);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020075
76#define LOGERR(ctx, errno, str, args...) ly_log(ctx, LY_LLERR, errno, str, ##args)
77#define LOGWRN(ctx, str, args...) ly_log(ctx, LY_LLWRN, 0, str, ##args)
78#define LOGVRB(str, args...) ly_log(NULL, LY_LLVRB, 0, str, ##args)
79
Radek Krejci4ab61562018-09-05 15:00:37 +020080#ifdef NDEBUG
81# define LOGDBG(dbg_group, str, args...)
82#else
83 void ly_log_dbg(int group, const char *format, ...);
84# define LOGDBG(dbg_group, str, args...) ly_log_dbg(dbg_group, str, ##args);
85#endif
86
Radek Krejci5aeea3a2018-09-05 13:29:36 +020087#define LOGMEM(CTX) LOGERR(CTX, LY_EMEM, "Memory allocation failed (%s()).", __func__)
88#define LOGINT(CTX) LOGERR(CTX, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__)
89#define LOGARG(CTX, ARG) LOGERR(CTX, LY_EINVAL, "Invalid argument %s (%s()).", #ARG, __func__)
Radek Krejcic07921a2018-09-17 11:40:15 +020090#define LOGVAL(CTX, ELEM_TYPE, ELEM, CODE, FORMAT...) ly_vlog(CTX, ELEM_TYPE, ELEM, CODE, ##FORMAT)
Radek Krejci94aa9942018-09-07 17:12:17 +020091
92#define LOGMEM_RET(CTX) LOGMEM(CTX); return LY_EMEM
93#define LOGINT_RET(CTX) LOGINT(CTX); return LY_EINT
94#define LOGARG_RET(CTX) LOGARG(CTX); return LY_EINVAL
Radek Krejci5aeea3a2018-09-05 13:29:36 +020095
96/*
97 * Common code to check return value and perform appropriate action.
98 */
99#define LY_CHECK_GOTO(COND, GOTO) if (COND) {goto GOTO;}
100#define LY_CHECK_ERR_GOTO(COND, ERR, GOTO) if (COND) {ERR; goto GOTO;}
Radek Krejcic59bc972018-09-17 16:13:06 +0200101#define LY_CHECK_RET1(RETVAL) if (RETVAL != LY_SUCCESS) {return RETVAL;}
102#define LY_CHECK_RET2(COND, RETVAL) if (COND) {return RETVAL;}
103#define LY_CHECK_RET(...) GETMACRO2(__VA_ARGS__, LY_CHECK_RET2, LY_CHECK_RET1)(__VA_ARGS__)
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200104#define LY_CHECK_ERR_RET(COND, ERR, RETVAL) if (COND) {ERR; return RETVAL;}
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200105
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200106#define LY_CHECK_ARG_GOTO1(CTX, ARG, GOTO) if (!ARG) {LOGARG(CTX, ARG);goto GOTO;}
107#define LY_CHECK_ARG_GOTO2(CTX, ARG1, ARG2, GOTO) LY_CHECK_ARG_GOTO1(CTX, ARG1, GOTO);LY_CHECK_ARG_GOTO1(CTX, ARG2, GOTO)
108#define LY_CHECK_ARG_GOTO3(CTX, ARG1, ARG2, ARG3, GOTO) LY_CHECK_ARG_GOTO2(CTX, ARG1, ARG2, GOTO);LY_CHECK_ARG_GOTO1(CTX, ARG3, GOTO)
109#define LY_CHECK_ARG_GOTO(CTX, ...) GETMACRO4(__VA_ARGS__, LY_CHECK_ARG_GOTO3, LY_CHECK_ARG_GOTO2, LY_CHECK_ARG_GOTO1)(CTX, __VA_ARGS__)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200110
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200111#define LY_CHECK_ARG_RET1(CTX, ARG, RETVAL) if (!ARG) {LOGARG(CTX, ARG);return RETVAL;}
112#define LY_CHECK_ARG_RET2(CTX, ARG1, ARG2, RETVAL) LY_CHECK_ARG_RET1(CTX, ARG1, RETVAL);LY_CHECK_ARG_RET1(CTX, ARG2, RETVAL)
113#define LY_CHECK_ARG_RET3(CTX, ARG1, ARG2, ARG3, RETVAL) LY_CHECK_ARG_RET2(CTX, ARG1, ARG2, RETVAL);LY_CHECK_ARG_RET1(CTX, ARG3, RETVAL)
114#define LY_CHECK_ARG_RET(CTX, ...) GETMACRO4(__VA_ARGS__, LY_CHECK_ARG_RET3, LY_CHECK_ARG_RET2, LY_CHECK_ARG_RET1)(CTX, __VA_ARGS__)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115
Radek Krejcid972c252018-09-25 13:23:39 +0200116/* count sequence size for LY_VCODE_INCHILDSTMT validation error code */
117size_t LY_VCODE_INSTREXP_len(const char *str);
118/* default maximum characters to print in LY_VCODE_INCHILDSTMT */
119#define LY_VCODE_INSTREXP_MAXLEN 20
120
Michal Vasko58257712018-09-12 11:11:38 +0200121#define LY_VCODE_INCHAR LYVE_SYNTAX, "Invalid character 0x%x."
Michal Vasko37d705c2018-09-12 15:31:26 +0200122#define LY_VCODE_INSTREXP LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected %s."
Michal Vasko58257712018-09-12 11:11:38 +0200123#define LY_VCODE_EOF LYVE_SYNTAX, "Unexpected end-of-file."
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200124#define LY_VCODE_NTERM LYVE_SYNTAX, "%s not terminated."
125#define LY_VCODE_NSUPP LYVE_SYNTAX, "%s not supported."
Radek Krejci94aa9942018-09-07 17:12:17 +0200126#define LY_VCODE_INSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\"."
Michal Vasko58257712018-09-12 11:11:38 +0200127#define LY_VCODE_INCHILDSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\"."
128#define LY_VCODE_DUPSTMT LYVE_SYNTAX_YANG, "Duplicate keyword \"%s\"."
Michal Vasko37d705c2018-09-12 15:31:26 +0200129#define LY_VCODE_INVAL LYVE_SYNTAX_YANG, "Invalid value \"%.*s\" of \"%s\"."
Michal Vasko58257712018-09-12 11:11:38 +0200130#define LY_VCODE_MISSTMT LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s\"."
131#define LY_VCODE_INORD LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", it cannot appear after \"%s\"."
Michal Vasko37d705c2018-09-12 15:31:26 +0200132#define LY_VCODE_OOB LYVE_SYNTAX_YANG, "Value \"%.*s\" is out of \"%s\" bounds."
Michal Vasko58257712018-09-12 11:11:38 +0200133#define LY_VCODE_INDEV LYVE_SYNTAX_YANG, "Deviate \"%s\" does not support keyword \"%s\"."
Radek Krejci94aa9942018-09-07 17:12:17 +0200134
Radek Krejcic59bc972018-09-17 16:13:06 +0200135/******************************************************************************
136 * Parsers
137 *****************************************************************************/
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200138
Radek Krejcic59bc972018-09-17 16:13:06 +0200139enum yang_module_stmt {
140 Y_MOD_MODULE_HEADER,
141 Y_MOD_LINKAGE,
142 Y_MOD_META,
143 Y_MOD_REVISION,
144 Y_MOD_BODY
145};
146
147enum yang_arg {
148 Y_IDENTIF_ARG,
149 Y_PREF_IDENTIF_ARG,
150 Y_STR_ARG,
151 Y_MAYBE_STR_ARG
152};
153
154enum yang_keyword {
155 YANG_NONE = 0,
156 YANG_ACTION,
157 YANG_ANYDATA,
158 YANG_ANYXML,
159 YANG_ARGUMENT,
160 YANG_AUGMENT,
161 YANG_BASE,
162 YANG_BELONGS_TO,
163 YANG_BIT,
164 YANG_CASE,
165 YANG_CHOICE,
166 YANG_CONFIG,
167 YANG_CONTACT,
168 YANG_CONTAINER,
169 YANG_DEFAULT,
170 YANG_DESCRIPTION,
171 YANG_DEVIATE,
172 YANG_DEVIATION,
173 YANG_ENUM,
174 YANG_ERROR_APP_TAG,
175 YANG_ERROR_MESSAGE,
176 YANG_EXTENSION,
177 YANG_FEATURE,
178 YANG_FRACTION_DIGITS,
179 YANG_GROUPING,
180 YANG_IDENTITY,
181 YANG_IF_FEATURE,
182 YANG_IMPORT,
183 YANG_INCLUDE,
184 YANG_INPUT,
185 YANG_KEY,
186 YANG_LEAF,
187 YANG_LEAF_LIST,
188 YANG_LENGTH,
189 YANG_LIST,
190 YANG_MANDATORY,
191 YANG_MAX_ELEMENTS,
192 YANG_MIN_ELEMENTS,
193 YANG_MODIFIER,
194 YANG_MODULE,
195 YANG_MUST,
196 YANG_NAMESPACE,
197 YANG_NOTIFICATION,
198 YANG_ORDERED_BY,
199 YANG_ORGANIZATION,
200 YANG_OUTPUT,
201 YANG_PATH,
202 YANG_PATTERN,
203 YANG_POSITION,
204 YANG_PREFIX,
205 YANG_PRESENCE,
206 YANG_RANGE,
207 YANG_REFERENCE,
208 YANG_REFINE,
209 YANG_REQUIRE_INSTANCE,
210 YANG_REVISION,
211 YANG_REVISION_DATE,
212 YANG_RPC,
213 YANG_STATUS,
214 YANG_SUBMODULE,
215 YANG_TYPE,
216 YANG_TYPEDEF,
217 YANG_UNIQUE,
218 YANG_UNITS,
219 YANG_USES,
220 YANG_VALUE,
221 YANG_WHEN,
222 YANG_YANG_VERSION,
223 YANG_YIN_ELEMENT,
224
225 YANG_SEMICOLON,
226 YANG_LEFT_BRACE,
227 YANG_RIGHT_BRACE,
228 YANG_CUSTOM
229};
230
231/* list of the YANG statements strings */
232extern const char *const ly_stmt_list[];
233#define ly_stmt2str(STMT) ly_stmt_list[STMT]
234
235/* list of the extensions' substatements strings */
236extern const char *const lyext_substmt_list[];
237#define lyext_substmt2str(STMT) lyext_substmt_list[STMT]
238
239/* list of the deviate modifications strings */
240extern const char *const ly_devmod_list[];
241#define ly_devmod2str(TYPE) ly_devmod_list[TYPE]
242
243/******************************************************************************
Michal Vasko1324b6c2018-09-07 11:16:23 +0200244 * Generic useful functions.
Radek Krejcic59bc972018-09-17 16:13:06 +0200245 *****************************************************************************/
Michal Vasko1324b6c2018-09-07 11:16:23 +0200246
247/**
248 * @brief Wrapper for realloc() call. The only difference is that if it fails to
249 * allocate the requested memory, the original memory is freed as well.
250 *
251 * @param[in] ptr Memory to reallocate.
252 * @param[in] size New size of the memory block.
253 *
254 * @return Pointer to the new memory, NULL on error.
255 */
256void *ly_realloc(void *ptr, size_t size);
257
Radek Krejcif345c012018-09-19 11:12:59 +0200258/**
259 * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT)
260 *
261 * @param[in] ctx Context to store log message.
262 * @param[in] date Date string to check (non-necessarily terminated by \0)
263 * @param[in] date_len Length of the date string, 10 expected.
264 * @param[in] stmt Statement name for error message.
265 * @return LY_ERR value.
266 */
267LY_ERR lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt);
Michal Vasko841d1a92018-09-07 15:40:31 +0200268
Michal Vasko1324b6c2018-09-07 11:16:23 +0200269/*
270 * Macros to work with lysp structures arrays.
271 *
272 * There is a byte allocated after the last item with value 0.
273 */
274#define LYSP_ARRAY_NEW_RET(CTX, ARRAY, NEW_ITEM, RETVAL) int _count; \
275 for (_count = 0; *(ARRAY) && *((uint8_t *)(*(ARRAY) + _count)); ++_count); \
276 if (!_count) *(ARRAY) = malloc(sizeof **(ARRAY) + 1); \
277 else *(ARRAY) = ly_realloc(*(ARRAY), (_count + 1) * sizeof **(ARRAY) + 1); \
278 LY_CHECK_ERR_RET(!*(ARRAY), LOGMEM(CTX), RETVAL); \
279 *((uint8_t *)(*(ARRAY) + _count + 1)) = 0; \
Michal Vasko50b70142018-09-07 13:26:29 +0200280 (NEW_ITEM) = (*(ARRAY)) + _count; \
281 memset(NEW_ITEM, 0, sizeof *(NEW_ITEM));
Michal Vasko1324b6c2018-09-07 11:16:23 +0200282
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200283#endif /* LY_COMMON_H_ */