blob: f3a6e6e205af1cd093c080ca028ef36383c609d9 [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 Krejci94aa9942018-09-07 17:12:17 +0200119#define LY_VCODE_INSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\"."
Michal Vasko58257712018-09-12 11:11:38 +0200120#define LY_VCODE_INCHILDSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\"."
121#define LY_VCODE_DUPSTMT LYVE_SYNTAX_YANG, "Duplicate keyword \"%s\"."
Michal Vasko37d705c2018-09-12 15:31:26 +0200122#define LY_VCODE_INVAL LYVE_SYNTAX_YANG, "Invalid value \"%.*s\" of \"%s\"."
Michal Vasko58257712018-09-12 11:11:38 +0200123#define LY_VCODE_MISSTMT LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s\"."
124#define LY_VCODE_INORD LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", it cannot appear after \"%s\"."
Michal Vasko37d705c2018-09-12 15:31:26 +0200125#define LY_VCODE_OOB LYVE_SYNTAX_YANG, "Value \"%.*s\" is out of \"%s\" bounds."
Michal Vasko58257712018-09-12 11:11:38 +0200126#define LY_VCODE_INDEV LYVE_SYNTAX_YANG, "Deviate \"%s\" does not support keyword \"%s\"."
Radek Krejci94aa9942018-09-07 17:12:17 +0200127
Radek Krejcic59bc972018-09-17 16:13:06 +0200128/******************************************************************************
129 * Parsers
130 *****************************************************************************/
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200131
Radek Krejcic59bc972018-09-17 16:13:06 +0200132enum yang_module_stmt {
133 Y_MOD_MODULE_HEADER,
134 Y_MOD_LINKAGE,
135 Y_MOD_META,
136 Y_MOD_REVISION,
137 Y_MOD_BODY
138};
139
140enum yang_arg {
141 Y_IDENTIF_ARG,
142 Y_PREF_IDENTIF_ARG,
143 Y_STR_ARG,
144 Y_MAYBE_STR_ARG
145};
146
147enum yang_keyword {
148 YANG_NONE = 0,
149 YANG_ACTION,
150 YANG_ANYDATA,
151 YANG_ANYXML,
152 YANG_ARGUMENT,
153 YANG_AUGMENT,
154 YANG_BASE,
155 YANG_BELONGS_TO,
156 YANG_BIT,
157 YANG_CASE,
158 YANG_CHOICE,
159 YANG_CONFIG,
160 YANG_CONTACT,
161 YANG_CONTAINER,
162 YANG_DEFAULT,
163 YANG_DESCRIPTION,
164 YANG_DEVIATE,
165 YANG_DEVIATION,
166 YANG_ENUM,
167 YANG_ERROR_APP_TAG,
168 YANG_ERROR_MESSAGE,
169 YANG_EXTENSION,
170 YANG_FEATURE,
171 YANG_FRACTION_DIGITS,
172 YANG_GROUPING,
173 YANG_IDENTITY,
174 YANG_IF_FEATURE,
175 YANG_IMPORT,
176 YANG_INCLUDE,
177 YANG_INPUT,
178 YANG_KEY,
179 YANG_LEAF,
180 YANG_LEAF_LIST,
181 YANG_LENGTH,
182 YANG_LIST,
183 YANG_MANDATORY,
184 YANG_MAX_ELEMENTS,
185 YANG_MIN_ELEMENTS,
186 YANG_MODIFIER,
187 YANG_MODULE,
188 YANG_MUST,
189 YANG_NAMESPACE,
190 YANG_NOTIFICATION,
191 YANG_ORDERED_BY,
192 YANG_ORGANIZATION,
193 YANG_OUTPUT,
194 YANG_PATH,
195 YANG_PATTERN,
196 YANG_POSITION,
197 YANG_PREFIX,
198 YANG_PRESENCE,
199 YANG_RANGE,
200 YANG_REFERENCE,
201 YANG_REFINE,
202 YANG_REQUIRE_INSTANCE,
203 YANG_REVISION,
204 YANG_REVISION_DATE,
205 YANG_RPC,
206 YANG_STATUS,
207 YANG_SUBMODULE,
208 YANG_TYPE,
209 YANG_TYPEDEF,
210 YANG_UNIQUE,
211 YANG_UNITS,
212 YANG_USES,
213 YANG_VALUE,
214 YANG_WHEN,
215 YANG_YANG_VERSION,
216 YANG_YIN_ELEMENT,
217
218 YANG_SEMICOLON,
219 YANG_LEFT_BRACE,
220 YANG_RIGHT_BRACE,
221 YANG_CUSTOM
222};
223
224/* list of the YANG statements strings */
225extern const char *const ly_stmt_list[];
226#define ly_stmt2str(STMT) ly_stmt_list[STMT]
227
228/* list of the extensions' substatements strings */
229extern const char *const lyext_substmt_list[];
230#define lyext_substmt2str(STMT) lyext_substmt_list[STMT]
231
232/* list of the deviate modifications strings */
233extern const char *const ly_devmod_list[];
234#define ly_devmod2str(TYPE) ly_devmod_list[TYPE]
235
236/******************************************************************************
Michal Vasko1324b6c2018-09-07 11:16:23 +0200237 * Generic useful functions.
Radek Krejcic59bc972018-09-17 16:13:06 +0200238 *****************************************************************************/
Michal Vasko1324b6c2018-09-07 11:16:23 +0200239
240/**
241 * @brief Wrapper for realloc() call. The only difference is that if it fails to
242 * allocate the requested memory, the original memory is freed as well.
243 *
244 * @param[in] ptr Memory to reallocate.
245 * @param[in] size New size of the memory block.
246 *
247 * @return Pointer to the new memory, NULL on error.
248 */
249void *ly_realloc(void *ptr, size_t size);
250
Radek Krejcif345c012018-09-19 11:12:59 +0200251/**
252 * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT)
253 *
254 * @param[in] ctx Context to store log message.
255 * @param[in] date Date string to check (non-necessarily terminated by \0)
256 * @param[in] date_len Length of the date string, 10 expected.
257 * @param[in] stmt Statement name for error message.
258 * @return LY_ERR value.
259 */
260LY_ERR lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt);
Michal Vasko841d1a92018-09-07 15:40:31 +0200261
Michal Vaskodac40592018-09-12 15:31:08 +0200262int lysp_get_data_line(const char *data, int fail_char);
263
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_ */