2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #ifndef __UCI_INTERNAL_H
16 #define __UCI_INTERNAL_H
18 struct uci_parse_context
26 struct uci_package *package;
27 struct uci_section *section;
35 static void uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, char *section, char *option, char *value);
36 static void uci_free_history(struct uci_history *h);
39 * functions for debug and error handling, for internal use only
43 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
49 * throw an uci exception and store the error number
52 #define UCI_THROW(ctx, err) do { \
53 DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
54 longjmp(ctx->trap, err); \
58 * store the return address for handling exceptions
59 * needs to be called in every externally visible library function
61 * NB: this does not handle recursion at all. Calling externally visible
62 * functions from other uci functions is only allowed at the end of the
63 * calling function, or by wrapping the function call in UCI_TRAP_SAVE
64 * and UCI_TRAP_RESTORE.
66 #define UCI_HANDLE_ERR(ctx) do { \
67 DPRINTF("ENTER: %s\n", __func__); \
71 return UCI_ERR_INVAL; \
73 __val = setjmp(ctx->trap); \
74 ctx->internal = false; \
76 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
83 * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
84 * are intercepted and redirected to the label specified in 'handler'
85 * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
86 * exception handler is restored
88 #define UCI_TRAP_SAVE(ctx, handler) do { \
91 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
92 __val = setjmp(ctx->trap); \
95 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
98 #define UCI_TRAP_RESTORE(ctx) \
99 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
103 * UCI_INTERNAL: Do an internal call of a public API function
105 * Sets Exception handling to passthrough mode.
106 * Allows API functions to change behavior compared to public use
108 #define UCI_INTERNAL(func, ctx, ...) do { \
109 ctx->internal = true; \
110 func(ctx, __VA_ARGS__); \
114 * check the specified condition.
115 * throw an invalid argument exception if it's false
117 #define UCI_ASSERT(ctx, expr) do { \
119 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
120 UCI_THROW(ctx, UCI_ERR_INVAL); \