blob: 4f0157b261cb4ea6d26db4d0dac12e8c16cbf86a [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
Michal Vasko58257712018-09-12 11:11:38 +0200116#define LY_VCODE_INCHAR LYVE_SYNTAX, "Invalid character 0x%x."
Michal Vasko37d705c2018-09-12 15:31:26 +0200117#define LY_VCODE_INSTREXP LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected %s."
Michal Vasko58257712018-09-12 11:11:38 +0200118#define LY_VCODE_EOF LYVE_SYNTAX, "Unexpected end-of-file."
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200119#define LY_VCODE_NTERM LYVE_SYNTAX, "%s not terminated."
120#define LY_VCODE_NSUPP LYVE_SYNTAX, "%s not supported."
Radek Krejci94aa9942018-09-07 17:12:17 +0200121#define LY_VCODE_INSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\"."
Michal Vasko58257712018-09-12 11:11:38 +0200122#define LY_VCODE_INCHILDSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\"."
123#define LY_VCODE_DUPSTMT LYVE_SYNTAX_YANG, "Duplicate keyword \"%s\"."
Michal Vasko37d705c2018-09-12 15:31:26 +0200124#define LY_VCODE_INVAL LYVE_SYNTAX_YANG, "Invalid value \"%.*s\" of \"%s\"."
Michal Vasko58257712018-09-12 11:11:38 +0200125#define LY_VCODE_MISSTMT LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s\"."
126#define LY_VCODE_INORD LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", it cannot appear after \"%s\"."
Michal Vasko37d705c2018-09-12 15:31:26 +0200127#define LY_VCODE_OOB LYVE_SYNTAX_YANG, "Value \"%.*s\" is out of \"%s\" bounds."
Michal Vasko58257712018-09-12 11:11:38 +0200128#define LY_VCODE_INDEV LYVE_SYNTAX_YANG, "Deviate \"%s\" does not support keyword \"%s\"."
Radek Krejci94aa9942018-09-07 17:12:17 +0200129
Radek Krejcic59bc972018-09-17 16:13:06 +0200130/******************************************************************************
131 * Parsers
132 *****************************************************************************/
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200133
Radek Krejcic59bc972018-09-17 16:13:06 +0200134enum yang_module_stmt {
135 Y_MOD_MODULE_HEADER,
136 Y_MOD_LINKAGE,
137 Y_MOD_META,
138 Y_MOD_REVISION,
139 Y_MOD_BODY
140};
141
142enum yang_arg {
143 Y_IDENTIF_ARG,
144 Y_PREF_IDENTIF_ARG,
145 Y_STR_ARG,
146 Y_MAYBE_STR_ARG
147};
148
149enum yang_keyword {
150 YANG_NONE = 0,
151 YANG_ACTION,
152 YANG_ANYDATA,
153 YANG_ANYXML,
154 YANG_ARGUMENT,
155 YANG_AUGMENT,
156 YANG_BASE,
157 YANG_BELONGS_TO,
158 YANG_BIT,
159 YANG_CASE,
160 YANG_CHOICE,
161 YANG_CONFIG,
162 YANG_CONTACT,
163 YANG_CONTAINER,
164 YANG_DEFAULT,
165 YANG_DESCRIPTION,
166 YANG_DEVIATE,
167 YANG_DEVIATION,
168 YANG_ENUM,
169 YANG_ERROR_APP_TAG,
170 YANG_ERROR_MESSAGE,
171 YANG_EXTENSION,
172 YANG_FEATURE,
173 YANG_FRACTION_DIGITS,
174 YANG_GROUPING,
175 YANG_IDENTITY,
176 YANG_IF_FEATURE,
177 YANG_IMPORT,
178 YANG_INCLUDE,
179 YANG_INPUT,
180 YANG_KEY,
181 YANG_LEAF,
182 YANG_LEAF_LIST,
183 YANG_LENGTH,
184 YANG_LIST,
185 YANG_MANDATORY,
186 YANG_MAX_ELEMENTS,
187 YANG_MIN_ELEMENTS,
188 YANG_MODIFIER,
189 YANG_MODULE,
190 YANG_MUST,
191 YANG_NAMESPACE,
192 YANG_NOTIFICATION,
193 YANG_ORDERED_BY,
194 YANG_ORGANIZATION,
195 YANG_OUTPUT,
196 YANG_PATH,
197 YANG_PATTERN,
198 YANG_POSITION,
199 YANG_PREFIX,
200 YANG_PRESENCE,
201 YANG_RANGE,
202 YANG_REFERENCE,
203 YANG_REFINE,
204 YANG_REQUIRE_INSTANCE,
205 YANG_REVISION,
206 YANG_REVISION_DATE,
207 YANG_RPC,
208 YANG_STATUS,
209 YANG_SUBMODULE,
210 YANG_TYPE,
211 YANG_TYPEDEF,
212 YANG_UNIQUE,
213 YANG_UNITS,
214 YANG_USES,
215 YANG_VALUE,
216 YANG_WHEN,
217 YANG_YANG_VERSION,
218 YANG_YIN_ELEMENT,
219
220 YANG_SEMICOLON,
221 YANG_LEFT_BRACE,
222 YANG_RIGHT_BRACE,
223 YANG_CUSTOM
224};
225
226/* list of the YANG statements strings */
227extern const char *const ly_stmt_list[];
228#define ly_stmt2str(STMT) ly_stmt_list[STMT]
229
230/* list of the extensions' substatements strings */
231extern const char *const lyext_substmt_list[];
232#define lyext_substmt2str(STMT) lyext_substmt_list[STMT]
233
234/* list of the deviate modifications strings */
235extern const char *const ly_devmod_list[];
236#define ly_devmod2str(TYPE) ly_devmod_list[TYPE]
237
238/******************************************************************************
Michal Vasko1324b6c2018-09-07 11:16:23 +0200239 * Generic useful functions.
Radek Krejcic59bc972018-09-17 16:13:06 +0200240 *****************************************************************************/
Michal Vasko1324b6c2018-09-07 11:16:23 +0200241
242/**
243 * @brief Wrapper for realloc() call. The only difference is that if it fails to
244 * allocate the requested memory, the original memory is freed as well.
245 *
246 * @param[in] ptr Memory to reallocate.
247 * @param[in] size New size of the memory block.
248 *
249 * @return Pointer to the new memory, NULL on error.
250 */
251void *ly_realloc(void *ptr, size_t size);
252
Radek Krejcif345c012018-09-19 11:12:59 +0200253/**
254 * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT)
255 *
256 * @param[in] ctx Context to store log message.
257 * @param[in] date Date string to check (non-necessarily terminated by \0)
258 * @param[in] date_len Length of the date string, 10 expected.
259 * @param[in] stmt Statement name for error message.
260 * @return LY_ERR value.
261 */
262LY_ERR lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt);
Michal Vasko841d1a92018-09-07 15:40:31 +0200263
Michal Vasko1324b6c2018-09-07 11:16:23 +0200264/*
265 * Macros to work with lysp structures arrays.
266 *
267 * There is a byte allocated after the last item with value 0.
268 */
269#define LYSP_ARRAY_NEW_RET(CTX, ARRAY, NEW_ITEM, RETVAL) int _count; \
270 for (_count = 0; *(ARRAY) && *((uint8_t *)(*(ARRAY) + _count)); ++_count); \
271 if (!_count) *(ARRAY) = malloc(sizeof **(ARRAY) + 1); \
272 else *(ARRAY) = ly_realloc(*(ARRAY), (_count + 1) * sizeof **(ARRAY) + 1); \
273 LY_CHECK_ERR_RET(!*(ARRAY), LOGMEM(CTX), RETVAL); \
274 *((uint8_t *)(*(ARRAY) + _count + 1)) = 0; \
Michal Vasko50b70142018-09-07 13:26:29 +0200275 (NEW_ITEM) = (*(ARRAY)) + _count; \
276 memset(NEW_ITEM, 0, sizeof *(NEW_ITEM));
Michal Vasko1324b6c2018-09-07 11:16:23 +0200277
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200278#endif /* LY_COMMON_H_ */