1 /* $Id: xpath.c,v 1.1 2003-02-04 12:06:48 pop Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 char *get_xp_part (char **strs, NMEM mem) {
39 char *sep = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\" ";
41 while (*cp == ' ') {cp++; str++;}
42 if (!strchr("><=] ", *cp)) sep = "><=] ";
44 while (*cp && !(strchr(sep,*cp) && !quoted) && (*cp != ']')) {
45 if (*cp =='"') quoted = 1 - quoted;
48 /* removing leading and trailing " */
50 if (*str == '"') str++;
51 if (*(cp-1) == '"') cp--;
53 res = nmem_malloc(mem, cp - str + 1);
54 memcpy (res, str, (cp-str));
55 *(res + (cp-str)) = '\0';
63 struct xpath_predicate *get_xpath_predicate(char *predicates, NMEM mem) {
69 struct xpath_predicate *r1;
70 struct xpath_predicate *r2;
71 struct xpath_predicate *res = 0;
73 char *pr = predicates;
75 if ((p1 = get_xp_part(&pr, mem))) {
76 if ((p2 = get_xp_part(&pr, mem))) {
77 if (!strcmp (p2, "and") || !strcmp (p2, "or") || !strcmp (p2, "not")) {
78 r1=nmem_malloc(mem, sizeof(struct xpath_predicate));
79 r1->which = XPATH_PREDICATE_RELATION;
80 r1->u.relation.name = p1;
81 r1->u.relation.op = "";
82 r1->u.relation.value = "";
84 r2 = get_xpath_predicate (pr, mem);
86 res = nmem_malloc(mem, sizeof(struct xpath_predicate));
87 res->which = XPATH_PREDICATE_BOOLEAN;
88 res->u.boolean.op = p2;
89 res->u.boolean.left = r1;
90 res->u.boolean.right = r2;
95 if (strchr("><=] ", *p2)) {
96 r1 = nmem_malloc(mem, sizeof(struct xpath_predicate));
98 r1->which = XPATH_PREDICATE_RELATION;
99 r1->u.relation.name = p1;
100 r1->u.relation.op = p2;
102 if ((p3 = get_xp_part(&pr, mem))) {
103 r1->u.relation.value = p3;
109 if ((p4 = get_xp_part(&pr, mem))) {
110 if (!strcmp (p4, "and") || !strcmp (p4, "or") || !strcmp (p4, "not")) {
112 r2 = get_xpath_predicate (pr, mem);
114 res = nmem_malloc(mem, sizeof(struct xpath_predicate));
115 res->which = XPATH_PREDICATE_BOOLEAN;
116 res->u.boolean.op = p4;
117 res->u.boolean.left = r1;
118 res->u.boolean.right = r2;
128 r1 = nmem_malloc(mem, sizeof(struct xpath_predicate));
130 r1->which = XPATH_PREDICATE_RELATION;
131 r1->u.relation.name = p1;
132 r1->u.relation.op = "";
133 r1->u.relation.value = "";
141 int parse_xpath_str(const char *xpath_string,
142 struct xpath_location_step *xpath, NMEM mem)
149 if (!xpath_string || *xpath_string != '/')
156 while (*cp && !strchr("/[",*cp))
161 xpath[no].predicate = 0;
162 xpath[no].part = nmem_malloc (mem, i+1);
163 memcpy (xpath[no].part, cp - i, i);
164 xpath[no].part[i] = 0;
173 xpath[no].predicate = get_xpath_predicate(a, mem);
174 while(*cp && *cp != ']') {
179 } /* end of ] predicate */
188 void dump_xp_predicate (struct xpath_predicate *p) {
190 if (p->which == XPATH_PREDICATE_RELATION &&
191 p->u.relation.name[0]) {
192 fprintf (stderr, "%s,%s,%s",
195 p->u.relation.value);
197 fprintf (stderr, "(");
198 dump_xp_predicate(p->u.boolean.left);
199 fprintf (stderr, ") %s (", p->u.boolean.op);
200 dump_xp_predicate(p->u.boolean.right);
201 fprintf (stderr, ")");
206 void dump_xp_steps (struct xpath_location_step *xpath, int no) {
208 for (i=0; i<no; i++) {
209 fprintf (stderr, "Step %d: %s ",i,xpath[i].part);
210 dump_xp_predicate(xpath[i].predicate);
211 fprintf (stderr, "\n");