blob: a214f4dafc6eb81ab0c59c080288b0a6b6628958 [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_keyword {
140 YANG_NONE = 0,
141 YANG_ACTION,
142 YANG_ANYDATA,
143 YANG_ANYXML,
144 YANG_ARGUMENT,
145 YANG_AUGMENT,
146 YANG_BASE,
147 YANG_BELONGS_TO,
148 YANG_BIT,
149 YANG_CASE,
150 YANG_CHOICE,
151 YANG_CONFIG,
152 YANG_CONTACT,
153 YANG_CONTAINER,
154 YANG_DEFAULT,
155 YANG_DESCRIPTION,
156 YANG_DEVIATE,
157 YANG_DEVIATION,
158 YANG_ENUM,
159 YANG_ERROR_APP_TAG,
160 YANG_ERROR_MESSAGE,
161 YANG_EXTENSION,
162 YANG_FEATURE,
163 YANG_FRACTION_DIGITS,
164 YANG_GROUPING,
165 YANG_IDENTITY,
166 YANG_IF_FEATURE,
167 YANG_IMPORT,
168 YANG_INCLUDE,
169 YANG_INPUT,
170 YANG_KEY,
171 YANG_LEAF,
172 YANG_LEAF_LIST,
173 YANG_LENGTH,
174 YANG_LIST,
175 YANG_MANDATORY,
176 YANG_MAX_ELEMENTS,
177 YANG_MIN_ELEMENTS,
178 YANG_MODIFIER,
179 YANG_MODULE,
180 YANG_MUST,
181 YANG_NAMESPACE,
182 YANG_NOTIFICATION,
183 YANG_ORDERED_BY,
184 YANG_ORGANIZATION,
185 YANG_OUTPUT,
186 YANG_PATH,
187 YANG_PATTERN,
188 YANG_POSITION,
189 YANG_PREFIX,
190 YANG_PRESENCE,
191 YANG_RANGE,
192 YANG_REFERENCE,
193 YANG_REFINE,
194 YANG_REQUIRE_INSTANCE,
195 YANG_REVISION,
196 YANG_REVISION_DATE,
197 YANG_RPC,
198 YANG_STATUS,
199 YANG_SUBMODULE,
200 YANG_TYPE,
201 YANG_TYPEDEF,
202 YANG_UNIQUE,
203 YANG_UNITS,
204 YANG_USES,
205 YANG_VALUE,
206 YANG_WHEN,
207 YANG_YANG_VERSION,
208 YANG_YIN_ELEMENT,
209
210 YANG_SEMICOLON,
211 YANG_LEFT_BRACE,
212 YANG_RIGHT_BRACE,
213 YANG_CUSTOM
214};
215
216/* list of the YANG statements strings */
217extern const char *const ly_stmt_list[];
218#define ly_stmt2str(STMT) ly_stmt_list[STMT]
219
220/* list of the extensions' substatements strings */
221extern const char *const lyext_substmt_list[];
222#define lyext_substmt2str(STMT) lyext_substmt_list[STMT]
223
224/* list of the deviate modifications strings */
225extern const char *const ly_devmod_list[];
226#define ly_devmod2str(TYPE) ly_devmod_list[TYPE]
227
228/******************************************************************************
Michal Vasko1324b6c2018-09-07 11:16:23 +0200229 * Generic useful functions.
Radek Krejcic59bc972018-09-17 16:13:06 +0200230 *****************************************************************************/
Michal Vasko1324b6c2018-09-07 11:16:23 +0200231
232/**
233 * @brief Wrapper for realloc() call. The only difference is that if it fails to
234 * allocate the requested memory, the original memory is freed as well.
235 *
236 * @param[in] ptr Memory to reallocate.
237 * @param[in] size New size of the memory block.
238 *
239 * @return Pointer to the new memory, NULL on error.
240 */
241void *ly_realloc(void *ptr, size_t size);
242
Radek Krejcif345c012018-09-19 11:12:59 +0200243/**
Radek Krejcib416be62018-10-01 14:51:45 +0200244 * @brief Get UTF8 code point of the next character in the input string.
245 *
246 * @param[in,out] input Input string to process, updated according to the processed/read data.
247 * @param[out] utf8_char UTF8 code point of the next character.
248 * @param[out] bytes_read Number of bytes used to encode the read utf8_char.
249 * @return LY_ERR value
250 */
251LY_ERR ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read);
252
253/**
Radek Krejci86d106e2018-10-18 09:53:19 +0200254 * @brief mmap(2) wrapper to map input files into memory to unify parsing.
Radek Krejcif345c012018-09-19 11:12:59 +0200255 *
Radek Krejci86d106e2018-10-18 09:53:19 +0200256 * The address space is allocate only for reading.
257 *
258 * @param[in] ctx libyang context for logging
259 * @param[in] fd Open file descriptor of a file to map.
260 * @param[out] length Allocated size.
261 * @param[out] addr Address where the file is mapped.
Radek Krejcif345c012018-09-19 11:12:59 +0200262 * @return LY_ERR value.
263 */
Radek Krejci86d106e2018-10-18 09:53:19 +0200264LY_ERR ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr);
Michal Vasko841d1a92018-09-07 15:40:31 +0200265
Radek Krejci86d106e2018-10-18 09:53:19 +0200266/**
267 * @brief munmap(2) wrapper to free the memory mapped by ly_mmap()
Michal Vasko1324b6c2018-09-07 11:16:23 +0200268 *
Radek Krejci86d106e2018-10-18 09:53:19 +0200269 * @param[in] addr Address where the input file is mapped.
270 * @param[in] length Allocated size of the address space.
271 * @return LY_ERR value.
272 */
273LY_ERR ly_munmap(void *addr, size_t length);
274
275/**
276 * @brief (Re-)Allocation of a ([sized array](@ref sizedarrays)).
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200277 *
Radek Krejci86d106e2018-10-18 09:53:19 +0200278 * @param[in] CTX libyang context for logging.
279 * @param[in,out] ARRAY Pointer to the array to allocate/resize.
280 * @param[out] NEW_ITEM Returning pointer to the newly allocated record in the ARRAY.
281 * @param[in] RETVAL Return value for the case of error (memory allocation failure).
Michal Vasko1324b6c2018-09-07 11:16:23 +0200282 */
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200283#define LYSP_ARRAY_NEW_RET(CTX, ARRAY, NEW_ITEM, RETVAL) \
284 if (!(ARRAY)) { \
285 ARRAY = malloc(sizeof(uint32_t) + sizeof *(ARRAY)); \
286 *((uint32_t*)(ARRAY)) = 1; \
287 } else { \
288 ++(*((uint32_t*)(ARRAY))); \
289 ARRAY = ly_realloc(ARRAY, sizeof(uint32_t) + (*((uint32_t*)(ARRAY)) * sizeof *(ARRAY))); \
290 LY_CHECK_ERR_RET(!(ARRAY), LOGMEM(CTX), RETVAL); \
291 } \
292 (NEW_ITEM) = (void*)((uint32_t*)((ARRAY) + *((uint32_t*)(ARRAY)) - 1) + 1); \
Michal Vasko50b70142018-09-07 13:26:29 +0200293 memset(NEW_ITEM, 0, sizeof *(NEW_ITEM));
Michal Vasko1324b6c2018-09-07 11:16:23 +0200294
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200295#endif /* LY_COMMON_H_ */