Annotation of embedaddon/libpdel/tmpl/tmpl_internal.h, revision 1.1.1.1
1.1 misho 1:
2: /*
3: * Copyright (c) 2001-2002 Packet Design, LLC.
4: * All rights reserved.
5: *
6: * Subject to the following obligations and disclaimer of warranty,
7: * use and redistribution of this software, in source or object code
8: * forms, with or without modifications are expressly permitted by
9: * Packet Design; provided, however, that:
10: *
11: * (i) Any and all reproductions of the source or object code
12: * must include the copyright notice above and the following
13: * disclaimer of warranties; and
14: * (ii) No rights are granted, in any manner or form, to use
15: * Packet Design trademarks, including the mark "PACKET DESIGN"
16: * on advertising, endorsements, or otherwise except as such
17: * appears in the above copyright notice or in the software.
18: *
19: * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
20: * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
21: * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
22: * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
23: * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
24: * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
25: * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
26: * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
27: * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
28: * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
29: * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
30: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
31: * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
32: * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
33: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35: * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
36: * THE POSSIBILITY OF SUCH DAMAGE.
37: *
38: * Author: Archie Cobbs <archie@freebsd.org>
39: */
40:
41: #include <sys/types.h>
42: #include <sys/stat.h>
43: #include <sys/mman.h>
44:
45: #include <stdio.h>
46: #include <stdlib.h>
47: #include <stdarg.h>
48: #include <string.h>
49: #include <assert.h>
50: #include <limits.h>
51: #include <fcntl.h>
52: #include <ctype.h>
53: #include <errno.h>
54: #include <paths.h>
55: #include <unistd.h>
56: #include <pthread.h>
57:
58: #include "structs/structs.h"
59: #include "structs/type/array.h"
60:
61: #include "tmpl/tmpl.h"
62: #include "io/string_fp.h"
63: #include "util/typed_mem.h"
64: #include "util/string_quote.h"
65:
66: #ifndef TMPL_DEBUG
67: #define TMPL_DEBUG 0
68: #endif
69: #if TMPL_DEBUG != 0 && defined(__GNUC__)
70: #define DBG(fmt, args...) \
71: do { fprintf(stderr, fmt , ## args); } while(0)
72: #else
73: #define DBG(fmt, args...) do { } while (0)
74: #endif
75:
76: /* How much nesting is too much */
77: #define INFINITE_LOOP 128
78:
79: /* Function types */
80: enum func_type {
81: TY_NORMAL,
82: TY_LOOP,
83: TY_ENDLOOP,
84: TY_WHILE,
85: TY_ENDWHILE,
86: TY_IF,
87: TY_ELIF,
88: TY_ELSE,
89: TY_ENDIF,
90: TY_DEFINE,
91: TY_ENDDEF,
92: TY_BREAK,
93: TY_CONTINUE,
94: TY_RETURN
95: };
96:
97: /* Return values from _tmpl_execute_elems() */
98: enum exec_rtn {
99: RTN_NORMAL,
100: RTN_BREAK,
101: RTN_CONTINUE,
102: RTN_RETURN
103: };
104:
105: /* Template element flags */
106: #define TMPL_ELEM_NL_WHITE 0x0001 /* text is nl+space */
107: #define TMPL_ELEM_MMAP_TEXT 0x0002 /* text is mmap()'d */
108:
109: /* Function call */
110: struct func_call {
111: enum func_type type; /* type of function */
112: char *funcname; /* name of function */
113: int nargs; /* number of args */
114: struct func_arg *args; /* function arguments */
115: tmpl_handler_t *handler; /* built-in: handler */
116: };
117:
118: /* Function argument */
119: struct func_arg {
120: u_char is_literal; /* literal vs. func */
121: union {
122: struct func_call call; /* function call */
123: char *literal; /* literal string */
124: } u;
125: };
126:
127: /* This represents one parsed chunk of the template */
128: struct tmpl_elem {
129:
130: /* Lexical information */
131: char *text; /* text, or NULL if function */
132: u_int len; /* length of text */
133: u_int16_t flags; /* element flags */
134:
135: /* Semantic information */
136: struct func_call call; /* function and arguments */
137: union {
138: struct {
139: int endloop;/* endloop element */
140: } u_loop; /* TY_LOOP */
141: struct {
142: int endwhile;/* endwhile element */
143: } u_while; /* TY_WHILE */
144: struct {
145: int elsie; /* else element, or -1 */
146: int endif; /* endif element */
147: } u_if; /* TY_IF */
148: struct {
149: int enddef; /* enddef element */
150: } u_define; /* TY_DEFINE */
151: } u;
152: };
153:
154: /* Run-time variables */
155: struct exec_var {
156: char *name; /* variable name */
157: char *value; /* variable value */
158: };
159:
160: /* Run-time functions */
161: struct exec_func {
162: char *name; /* function name */
163: int num_elems; /* number of elements */
164: struct tmpl_elem *elems; /* function elements */
165: void *eblock; /* unified memory block */
166: };
167:
168: /* Looping info */
169: struct loop_ctx {
170: u_int index; /* current loop index */
171: struct loop_ctx *outer; /* enclosing loop info */
172: };
173:
174: /* Parsed template file */
175: struct tmpl {
176: void *mmap_addr; /* mmap() address, or NULL */
177: size_t mmap_len; /* mmap() length */
178: int num_elems; /* number of parsed elements */
179: struct tmpl_elem *elems; /* parsed template elements */
180: void *eblock; /* unified memory block */
181: char *mtype; /* memory type */
182: char mtype_buf[TYPED_MEM_TYPELEN];
183: };
184:
185: /* Template context */
186: struct tmpl_ctx {
187: FILE *output; /* current output */
188: FILE *orig_output; /* original output */
189: int flags; /* flags */
190: u_char close_output; /* close 'output' when done */
191: int depth; /* execution nesting level */
192: void *arg; /* user function cookie */
193: tmpl_handler_t *handler; /* user function handler */
194: tmpl_errfmtr_t *errfmtr; /* user error formatter */
195: struct loop_ctx *loop; /* innermost loop, if any */
196: int num_vars; /* number of variables */
197: struct exec_var *vars; /* runtime variables */
198: int num_funcs; /* number of functions */
199: struct exec_func *funcs; /* runtime functions */
200: char *mtype; /* memory type */
201: char mtype_buf[TYPED_MEM_TYPELEN];
202: };
203:
204: /*
205: * Functions
206: */
207:
208: __BEGIN_DECLS
209:
210: /* Parsing */
211: extern int _tmpl_parse(struct tmpl *tmpl, FILE *input, int *num_errors);
212:
213: /* Variable and function handling */
214: extern int _tmpl_find_var(struct tmpl_ctx *ctx, const char *name);
215: extern int _tmpl_find_func(struct tmpl_ctx *ctx, const char *name);
216: extern int _tmpl_set_func(struct tmpl_ctx *ctx, const char *name,
217: const struct tmpl_elem *elems, int count);
218:
219: /* Memory management */
220: extern void _tmpl_compact_elems(const char *mtype,
221: struct tmpl_elem *elems, int num_elems,
222: char *mem, size_t *bsize);
223: extern void _tmpl_compact(const char *mtype, char *mem,
224: void *ptrp, size_t len, size_t *bsize);
225: extern void _tmpl_free_func(struct tmpl_ctx *ctx, struct exec_func *func);
226: extern void _tmpl_free_elems(const char *mtype, void *eblock,
227: struct tmpl_elem *elems, int num);
228: extern void _tmpl_free_call(const char *mtype, struct func_call *call);
229: extern void _tmpl_free_arg(const char *mtype, struct func_arg *arg);
230:
231: /* Template execution */
232: extern enum exec_rtn _tmpl_execute_elems(struct tmpl_ctx *ctx,
233: struct tmpl_elem *elems, int first_elem, int last_elem);
234: extern char *_tmpl_invoke(struct tmpl_ctx *ctx,
235: char **errmsgp, const struct func_call *call);
236:
237: /* Truth determination */
238: extern int _tmpl_true(const char *s);
239:
240: #if TMPL_DEBUG
241: /* Debugging */
242: extern const char *_tmpl_elemstr(const struct tmpl_elem *elem, int index);
243: #endif
244:
245: __END_DECLS
246:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>