Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 2c22f12 | 2018-09-05 15:08:03 +0200 | [diff] [blame] | 21 | #include "config.h" |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 22 | #include "log.h" |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 23 | #include "tree_schema.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 24 | |
| 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 Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 35 | #define GETMACRO1(_1, NAME, ...) NAME |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 36 | #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 Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 41 | * 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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 44 | */ |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 45 | #define API __attribute__((visibility("default"))) |
| 46 | |
| 47 | /****************************************************************************** |
| 48 | * Logger |
| 49 | *****************************************************************************/ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 50 | |
| 51 | /* internal logging options */ |
| 52 | enum 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 Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 59 | enum 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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 68 | extern THREAD_LOCAL enum int_log_opts log_opt; |
| 69 | extern volatile uint8_t ly_log_level; |
| 70 | extern volatile uint8_t ly_log_opts; |
| 71 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 72 | void ly_err_free(void *ptr); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 73 | void ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 74 | void ly_vlog(const struct ly_ctx *ctx, enum LY_VLOG_ELEM elem_type, const void *elem, LY_VECODE code, const char *format, ...); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 75 | |
| 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 Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 80 | #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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 87 | #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 Krejci | c07921a | 2018-09-17 11:40:15 +0200 | [diff] [blame] | 90 | #define LOGVAL(CTX, ELEM_TYPE, ELEM, CODE, FORMAT...) ly_vlog(CTX, ELEM_TYPE, ELEM, CODE, ##FORMAT) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 91 | |
| 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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 95 | |
| 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 Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 101 | #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 Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 104 | #define LY_CHECK_ERR_RET(COND, ERR, RETVAL) if (COND) {ERR; return RETVAL;} |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 105 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 106 | #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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 110 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 111 | #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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 115 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 116 | /* count sequence size for LY_VCODE_INCHILDSTMT validation error code */ |
| 117 | size_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 Vasko | 5825771 | 2018-09-12 11:11:38 +0200 | [diff] [blame] | 121 | #define LY_VCODE_INCHAR LYVE_SYNTAX, "Invalid character 0x%x." |
Michal Vasko | 37d705c | 2018-09-12 15:31:26 +0200 | [diff] [blame] | 122 | #define LY_VCODE_INSTREXP LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected %s." |
Michal Vasko | 5825771 | 2018-09-12 11:11:38 +0200 | [diff] [blame] | 123 | #define LY_VCODE_EOF LYVE_SYNTAX, "Unexpected end-of-file." |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 124 | #define LY_VCODE_NTERM LYVE_SYNTAX, "%s not terminated." |
| 125 | #define LY_VCODE_NSUPP LYVE_SYNTAX, "%s not supported." |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 126 | #define LY_VCODE_INSTMT LYVE_SYNTAX_YANG, "Invalid keyword \"%s\"." |
Michal Vasko | 5825771 | 2018-09-12 11:11:38 +0200 | [diff] [blame] | 127 | #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 Vasko | 37d705c | 2018-09-12 15:31:26 +0200 | [diff] [blame] | 129 | #define LY_VCODE_INVAL LYVE_SYNTAX_YANG, "Invalid value \"%.*s\" of \"%s\"." |
Michal Vasko | 5825771 | 2018-09-12 11:11:38 +0200 | [diff] [blame] | 130 | #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 Vasko | 37d705c | 2018-09-12 15:31:26 +0200 | [diff] [blame] | 132 | #define LY_VCODE_OOB LYVE_SYNTAX_YANG, "Value \"%.*s\" is out of \"%s\" bounds." |
Michal Vasko | 5825771 | 2018-09-12 11:11:38 +0200 | [diff] [blame] | 133 | #define LY_VCODE_INDEV LYVE_SYNTAX_YANG, "Deviate \"%s\" does not support keyword \"%s\"." |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 134 | |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 135 | /****************************************************************************** |
| 136 | * Parsers |
| 137 | *****************************************************************************/ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 138 | |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 139 | enum 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 */ |
| 217 | extern const char *const ly_stmt_list[]; |
| 218 | #define ly_stmt2str(STMT) ly_stmt_list[STMT] |
| 219 | |
| 220 | /* list of the extensions' substatements strings */ |
| 221 | extern const char *const lyext_substmt_list[]; |
| 222 | #define lyext_substmt2str(STMT) lyext_substmt_list[STMT] |
| 223 | |
| 224 | /* list of the deviate modifications strings */ |
| 225 | extern const char *const ly_devmod_list[]; |
| 226 | #define ly_devmod2str(TYPE) ly_devmod_list[TYPE] |
| 227 | |
| 228 | /****************************************************************************** |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 229 | * Generic useful functions. |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 230 | *****************************************************************************/ |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 231 | |
| 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 | */ |
| 241 | void *ly_realloc(void *ptr, size_t size); |
| 242 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 243 | /** |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 244 | * @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 | */ |
| 251 | LY_ERR ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read); |
| 252 | |
| 253 | /** |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 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 | */ |
| 262 | LY_ERR lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 263 | |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 264 | /* |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame^] | 265 | * Macros to work with 0-terminated arrays. |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 266 | * |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame^] | 267 | * There is a NULL pointer allocated after the last item to terminate the array. |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 268 | */ |
| 269 | #define LYSP_ARRAY_NEW_RET(CTX, ARRAY, NEW_ITEM, RETVAL) int _count; \ |
Michal Vasko | 4f3980b | 2018-10-15 10:50:48 +0200 | [diff] [blame] | 270 | for (_count = 0; *(ARRAY) && *((void **)(*(ARRAY) + _count)); ++_count); \ |
| 271 | if (!_count) *(ARRAY) = malloc(sizeof **(ARRAY) + sizeof(void *)); \ |
| 272 | else *(ARRAY) = ly_realloc(*(ARRAY), (_count + 1) * sizeof **(ARRAY) + sizeof(void *)); \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame^] | 273 | LY_CHECK_ERR_RET(!*(ARRAY), LOGMEM(CTX), RETVAL); \ |
Michal Vasko | 4f3980b | 2018-10-15 10:50:48 +0200 | [diff] [blame] | 274 | *((void **)(*(ARRAY) + _count + 1)) = NULL; \ |
Michal Vasko | 50b7014 | 2018-09-07 13:26:29 +0200 | [diff] [blame] | 275 | (NEW_ITEM) = (*(ARRAY)) + _count; \ |
| 276 | memset(NEW_ITEM, 0, sizeof *(NEW_ITEM)); |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 277 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 278 | #endif /* LY_COMMON_H_ */ |