1 /*
2  *
3   ***** BEGIN LICENSE BLOCK *****
4 
5   Copyright (C) 2009-2019 Olof Hagsand
6 
7   This file is part of CLIXON.
8 
9   Licensed under the Apache License, Version 2.0 (the "License");
10   you may not use this file except in compliance with the License.
11   You may obtain a copy of the License at
12 
13     http://www.apache.org/licenses/LICENSE-2.0
14 
15   Unless required by applicable law or agreed to in writing, software
16   distributed under the License is distributed on an "AS IS" BASIS,
17   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   See the License for the specific language governing permissions and
19   limitations under the License.
20 
21   Alternatively, the contents of this file may be used under the terms of
22   the GNU General Public License Version 3 or later (the "GPL"),
23   in which case the provisions of the GPL are applicable instead
24   of those above. If you wish to allow use of your version of this file only
25   under the terms of the GPL, and not to allow others to
26   use your version of this file under the terms of Apache License version 2,
27   indicate your decision by deleting the provisions above and replace them with
28   the  notice and other provisions required by the GPL. If you do not delete
29   the provisions above, a recipient may use your version of this file under
30   the terms of any one of the Apache License version 2 or the GPL.
31 
32   ***** END LICENSE BLOCK *****
33 
34  * Clixon XML XPATH 1.0 according to https://www.w3.org/TR/xpath-10
35  */
36 #ifndef _CLIXON_XPATH_H
37 #define _CLIXON_XPATH_H
38 
39 /*
40  * Types
41  */
42 enum xp_op{
43     XO_AND,
44     XO_OR,
45     XO_DIV,
46     XO_MOD,
47     XO_ADD,
48     XO_MULT,
49     XO_SUB,
50     XO_EQ,
51     XO_NE,
52     XO_GE,
53     XO_LE,
54     XO_LT,
55     XO_GT,
56     XO_UNION,
57 };
58 
59 /* Axis specifiers according to https://www.w3.org/TR/xpath-10/#NT-AxisName
60  * @see axis_type_int2str
61  */
62 enum axis_type{
63     A_NAN = 0, /* Not set */
64     A_ANCESTOR,
65     A_ANCESTOR_OR_SELF,
66     A_ATTRIBUTE,
67     A_CHILD,
68     A_DESCENDANT,
69     A_DESCENDANT_OR_SELF,
70     A_FOLLOWING,
71     A_FOLLOWING_SIBLING,
72     A_NAMESPACE,
73     A_PARENT,
74     A_PRECEDING,
75     A_PRECEDING_SIBLING,
76     A_SELF,
77     A_ROOT /* XXX Not in https://www.w3.org/TR/xpath-10 */
78 };
79 
80 /* used as non-terminal type in yacc rules
81  * @see xpath_tree_int2str
82  */
83 enum xp_type{
84     XP_EXP,
85     XP_AND,
86     XP_RELEX,
87     XP_ADD,
88     XP_UNION,
89     XP_PATHEXPR,
90     XP_FILTEREXPR,
91     XP_LOCPATH,
92     XP_ABSPATH,
93     XP_RELLOCPATH,
94     XP_STEP,
95     XP_NODE, /* s0 is namespace prefix, s1 is name */
96     XP_NODE_FN,
97     XP_PRED,
98     XP_PRI0,
99     XP_PRIME_NR,
100     XP_PRIME_STR,
101     XP_PRIME_FN,
102 };
103 
104 /*! XPATH Parsing generates a tree of nodes that is later traversed
105  * That is, a tree-structured XPATH.
106  * Note that the structure follows XPATH 1.0 closely. The drawback wit this is that the tree gets
107  * very deep very quickly, even for simple XPATHs.
108  */
109 struct xpath_tree{
110     enum xp_type       xs_type;
111     int                xs_int;    /* step-> axis_type */
112     double             xs_double; /* set if XP_PRIME_NR */
113     char              *xs_strnr;  /* original string xs_double: numeric value */
114     char              *xs_s0;     /* set if XP_PRIME_STR, XP_PRIME_FN, XP_NODE[_FN] prefix*/
115     char              *xs_s1;     /* set if XP_NODE NAME */
116     struct xpath_tree *xs_c0;     /* child 0 */
117     struct xpath_tree *xs_c1;     /* child 1 */
118     int                xs_match;  /* meta: match this node */
119 };
120 typedef struct xpath_tree xpath_tree;
121 
122 /*
123  * Prototypes
124  */
125 char* axis_type_int2str(int axis_type);
126 char* xpath_tree_int2str(int nodetype);
127 int   xpath_tree_print_cb(cbuf *cb, xpath_tree *xs);
128 int   xpath_tree_print(FILE *f, xpath_tree *xs);
129 int   xpath_tree2cbuf(xpath_tree *xs, cbuf *xpathcb);
130 int   xpath_tree_eq(xpath_tree *xt1, xpath_tree *xt2, xpath_tree ***vec, size_t *len);
131 xpath_tree *xpath_tree_traverse(xpath_tree *xt, ...);
132 int   xpath_tree_free(xpath_tree *xs);
133 int   xpath_parse(const char *xpath, xpath_tree **xptree);
134 int   xpath_vec_ctx(cxobj *xcur, cvec *nsc, const char *xpath, int localonly, xp_ctx  **xrp);
135 
136 #if defined(__GNUC__) && __GNUC__ >= 3
137 int    xpath_vec_bool(cxobj *xcur, cvec *nsc, const char *xpformat, ...) __attribute__ ((format (printf, 3, 4)));
138 int    xpath_vec_flag(cxobj *xcur, cvec *nsc, const char *xpformat, uint16_t flags,
139 		   cxobj ***vec, int *veclen, ...) __attribute__ ((format (printf, 3, 7)));
140 
141 #else
142 int    xpath_vec_bool(cxobj *xcur, cvec *nsc, const char *xpformat, ...);
143 int    xpath_vec_flag(cxobj *xcur, cvec *nsc, const char *xpformat, uint16_t flags,
144 		      cxobj ***vec, int *veclen, ...);
145 #endif
146 
147 /* Functions with explicit namespace context (nsc) set. If you do not need
148  * explicit namespace contexts (most do not) consider using the API functions
149  * below without nsc set.
150  * If you do not know what a namespace context is, see README.md#xml-and-xpath
151  */
152 #if defined(__GNUC__) && __GNUC__ >= 3
153 cxobj *xpath_first(cxobj *xcur, cvec *nsc, const char *xpformat,  ...) __attribute__ ((format (printf, 3, 4)));
154 cxobj *xpath_first_localonly(cxobj *xcur, const char *xpformat,  ...) __attribute__ ((format (printf, 2, 3)));
155 int    xpath_vec(cxobj *xcur, cvec *nsc, const char *xpformat, cxobj ***vec, size_t *veclen, ...) __attribute__ ((format (printf, 3, 6)));
156 #else
157 cxobj *xpath_first(cxobj *xcur, cvec *nsc, const char *xpformat, ...);
158 cxobj *xpath_first_localonly(cxobj *xcur, const char *xpformat, ...);
159 int    xpath_vec(cxobj *xcur, cvec *nsc, const char *xpformat, cxobj  ***vec, size_t *veclen, ...);
160 #endif
161 
162 int xpath2canonical(const char *xpath0, cvec *nsc0, yang_stmt *yspec, char **xpath1, cvec **nsc1);
163 
164 #endif /* _CLIXON_XPATH_H */
165