1 /*	$Id: axp_extern.h 20800 2012-01-19 05:13:45Z m-oki $	*/
2 
3 /*
4  * Copyright (c) 2012, Internet Initiative Japan, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * TODO:
32  * - encoding
33  * - escape
34  */
35 #ifndef __AXP_PATTERN_H__
36 #define __AXP_PATTERN_H__
37 
38 #include <sys/types.h>
39 
40 struct arms_xml_parse;
41 
42 typedef struct arms_xml_parse AXP;
43 typedef int (*AXP_CALLBACK)(AXP *,
44 			    int,
45 			    int,
46 			    int,
47 			    const char *, size_t,
48 			    void *);
49 
50 typedef int (*AXP_BUILDCALLBACK)(AXP *,
51 				 char *, size_t,
52 				 void *);
53 
54 /*
55  * schema sample (maybe automatically generated)
56  *
57  * struct axp_schema rs_sol_msg[] = {
58  *	{ ARMS_MESSAGE, "arms-message", NULL, AXP_TYPE_CHILD, NULL, rs_sol_msg1,},
59  *	NULL,
60  * };
61  *
62  * struct axp_schema rs_sol_msg1[] = {
63  *	{ ARMS_RESPONSE, "arms-response", {"type", "rs-solicitation", NULL},
64  *	  AXP_TYPE_CHILD, NULL, rs_sol_msg2, },
65  *	NULL,
66  * };
67  *
68  * struct axp_schema rs_sol_msg2[] = {
69  *	{ RESULT_CODE, "result-code", NULL, AXP_TYPE_INT, result_cb, NULL },
70  *	{ "result-description", NULL, AXP_TYPE_STRING, result_str_cb, NULL },
71  *	{ "rs-solicitation-response", NULL, AXP_TYPE_CHILD, NULL, rs_sol_msg3 },
72  *	NULL,
73  * };
74  *
75  * struct axp_schema rs_sol_msg3[] = {
76  *	{ LL_TIMEOUT, "ll-timeout" },
77  *	{ RS_RETRY_MAX, "rs-retry-max" },
78  *	{ RS_RETRY_INTERVAL, "rs-retry-interval", },
79  *	{ LIFETIME, "lifetime", },
80  *	{ RS_INFO, "rs-info", ..., rs_sol_msg4 },
81  *	NULL,
82  * };
83  *
84  * struct axp_schema rs_sol_msg4[] = {
85  *	{ URL, "url", NULL, AXP_TYPE_TEXT, },
86  *	{ MD_CONFIG, "md-config", NULL, AXP_TYPE_TEXT, config_cb, NULL },
87  };
88  */
89 struct axp_schema {
90 	int as_tagtype;
91 	char *as_tag;
92 	int as_type;
93 	char **as_attr;
94 	AXP_CALLBACK as_cb;
95 	struct axp_schema *as_child;
96 };
97 
98 enum {
99 	AXP_TYPE_INT,
100 	AXP_TYPE_TEXT,
101 	AXP_TYPE_CHILD
102 };
103 
104 
105 enum {
106 	AXP_PARSE_START,
107 	AXP_PARSE_CONTENT,
108 	AXP_PARSE_END,
109 	AXP_PARSE_TAG,
110 	AXP_PARSE_VALUE,
111 	AXP_PARSE_ERROR
112 };
113 
114 /* content buffer size */
115 #define AXP_BUFSIZE 65536
116 
117 AXP *axp_create(struct axp_schema *, const char *, void *, AXP_BUILDCALLBACK);
118 int axp_parse(AXP *, const char *, size_t);
119 int axp_endparse(AXP *);
120 int axp_destroy(AXP *);
121 
122 /*
123  * e.g.  axp_refer(obj, RESULT_CODE, &result);
124  */
125 int axp_refer(AXP *, int, void *);
126 
127 const char * axp_find_attr(AXP *, int, char *);
128 
129 /*
130  * XML text build functions.
131  * 1. obj = axp_create();
132  * 2. axp_setbufsiz(obj, size);
133  * 3. while(<>) {
134  *	axp_set(obj, tag, &val); // callback if buffer is filled.
135  *    };
136  * 4. axp_build(obj); // callback
137  */
138 int axp_setbufsiz(AXP *, size_t);
139 int axp_set(AXP *, int, void *);
140 void axp_reset(AXP *, int);
141 int axp_set_attr(AXP *, int, const char *, const char *);
142 int axp_build(AXP *);
143 
144 int axp_get_tagstate(AXP *);
145 int axp_set_userdata(AXP *, void *);
146 void *axp_get_userdata(AXP *);
147 
148 #endif  /* __AXP_PATTERN_H__ */
149