1 /* $Id: xpath.c,v 1.3 2003-03-01 22:45:38 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 #include <zebra_xpath.h>
31 static char *get_xp_part (char **strs, NMEM mem, int *literal)
41 if (strchr("()", *cp))
43 else if (strchr("><=", *cp))
45 while (strchr("><=", *cp))
48 else if (*cp == '"' || *cp == '\'')
53 while (*cp && *cp != sep)
55 res = nmem_malloc(mem, cp - str + 1);
57 memcpy (res, str, (cp-str));
65 while (*cp && !strchr("><=()]\" ", *cp))
70 res = nmem_malloc(mem, cp - str + 1);
72 memcpy (res, str, (cp-str));
79 static struct xpath_predicate *get_xpath_boolean(char **pr, NMEM mem,
80 char **look, int *literal);
82 static struct xpath_predicate *get_xpath_relation(char **pr, NMEM mem,
83 char **look, int *literal)
85 struct xpath_predicate *res = 0;
86 if (!*literal && !strcmp(*look, "("))
88 *look = get_xp_part(pr, mem, literal);
89 res = get_xpath_boolean(pr, mem, look, literal);
90 if (!strcmp(*look, ")"))
91 *look = get_xp_part(pr, mem, literal);
97 res=nmem_malloc(mem, sizeof(struct xpath_predicate));
98 res->which = XPATH_PREDICATE_RELATION;
99 res->u.relation.name = *look;
101 *look = get_xp_part(pr, mem, literal);
102 if (*look && !*literal && strchr("><=", **look))
104 res->u.relation.op = *look;
106 *look = get_xp_part(pr, mem, literal);
108 return 0; /* error */
109 res->u.relation.value = *look;
110 *look = get_xp_part(pr, mem, literal);
114 res->u.relation.op = "";
115 res->u.relation.value = "";
121 static struct xpath_predicate *get_xpath_boolean(char **pr, NMEM mem,
122 char **look, int *literal)
124 struct xpath_predicate *left = 0;
126 left = get_xpath_relation(pr, mem, look, literal);
130 while (*look && !*literal &&
131 (!strcmp(*look, "and") || !strcmp(*look, "or") ||
132 !strcmp(*look, "not")))
134 struct xpath_predicate *res, *right;
136 res = nmem_malloc(mem, sizeof(struct xpath_predicate));
137 res->which = XPATH_PREDICATE_BOOLEAN;
138 res->u.boolean.op = *look;
139 res->u.boolean.left = left;
141 *look = get_xp_part(pr, mem, literal); /* skip the boolean name */
142 right = get_xpath_relation(pr, mem, look, literal);
144 res->u.boolean.right = right;
151 static struct xpath_predicate *get_xpath_predicate(char *predicate, NMEM mem)
154 char **pr = &predicate;
155 char *look = get_xp_part(pr, mem, &literal);
159 return get_xpath_boolean(pr, mem, &look, &literal);
162 int zebra_parse_xpath_str(const char *xpath_string,
163 struct xpath_location_step *xpath, int max, NMEM mem)
170 if (!xpath_string || *xpath_string != '/')
174 while (*cp && no < max)
177 while (*cp && !strchr("/[",*cp))
182 xpath[no].predicate = 0;
183 xpath[no].part = nmem_malloc (mem, i+1);
185 memcpy (xpath[no].part, cp - i, i);
186 xpath[no].part[i] = 0;
195 xpath[no].predicate = get_xpath_predicate(a, mem);
196 while(*cp && *cp != ']') {
201 } /* end of ] predicate */
208 /* for debugging .. */
210 dump_xp_steps(xpath, no);
216 void dump_xp_predicate (struct xpath_predicate *p)
219 if (p->which == XPATH_PREDICATE_RELATION &&
220 p->u.relation.name[0]) {
221 fprintf (stderr, "%s,%s,%s",
224 p->u.relation.value);
226 fprintf (stderr, "(");
227 dump_xp_predicate(p->u.boolean.left);
228 fprintf (stderr, ") %s (", p->u.boolean.op);
229 dump_xp_predicate(p->u.boolean.right);
230 fprintf (stderr, ")");
235 void dump_xp_steps (struct xpath_location_step *xpath, int no)
238 for (i=0; i<no; i++) {
239 fprintf (stderr, "Step %d: %s ",i,xpath[i].part);
240 dump_xp_predicate(xpath[i].predicate);
241 fprintf (stderr, "\n");