| 1 | //
 | 
|---|
| 2 | // ipv2_cwk.cc
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
| 7 | // Maintainer: LPS
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // This file is part of the SC Toolkit.
 | 
|---|
| 10 | //
 | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
| 14 | // any later version.
 | 
|---|
| 15 | //
 | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 | // GNU Library General Public License for more details.
 | 
|---|
| 20 | //
 | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
| 26 | //
 | 
|---|
| 27 | 
 | 
|---|
| 28 | /* These routines manipulate the current working keyword.  This
 | 
|---|
| 29 |  * is an ordered list of keyword_tree's.  When a relative
 | 
|---|
| 30 |  * keyword is searched for we start looking under the first keyword
 | 
|---|
| 31 |  * tree in the current working keyword list and if it is not found
 | 
|---|
| 32 |  * continue looking under successive members of the list. */
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #include <stdlib.h>
 | 
|---|
| 35 | #include <string.h>
 | 
|---|
| 36 | #include <util/keyval/ipv2.h>
 | 
|---|
| 37 | 
 | 
|---|
| 38 | using namespace std;
 | 
|---|
| 39 | using namespace sc;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | /* This sets up the current working keyword path to the declaration
 | 
|---|
| 42 |  * list. */
 | 
|---|
| 43 | void
 | 
|---|
| 44 | IPV2::cwk_root()
 | 
|---|
| 45 | {
 | 
|---|
| 46 |   free_keyword_tree_list(ip_cwk);
 | 
|---|
| 47 |   ip_cwk = splice_keyword_tree_list(ip_tree,NULL);
 | 
|---|
| 48 |   }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | /* This sets up the current working keyword path to NULL
 | 
|---|
| 51 |  * list. */
 | 
|---|
| 52 | void
 | 
|---|
| 53 | IPV2::cwk_clear()
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   free_keyword_tree_list(ip_cwk);
 | 
|---|
| 56 |   ip_cwk = NULL;
 | 
|---|
| 57 |   }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | /* This adds a keyword tree to the keyword path. */
 | 
|---|
| 60 | void
 | 
|---|
| 61 | IPV2::ip_cwk_add_kt(ip_keyword_tree_t *kt)
 | 
|---|
| 62 | {
 | 
|---|
| 63 |   ip_cwk = splice_keyword_tree_list(kt,ip_cwk);
 | 
|---|
| 64 |   }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | /* This adds a keyword to the keyword path. */
 | 
|---|
| 67 | /* NOTE: the last path to be searched must be added first. */
 | 
|---|
| 68 | void
 | 
|---|
| 69 | IPV2::cwk_add(const char* keyword)
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   ip_keyword_tree_t *kt;
 | 
|---|
| 72 |   ip_keyword_tree_list_t *I,*old_cwk;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   old_cwk = ip_cwk;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   /* Initialize the new cwk list. */
 | 
|---|
| 77 |   ip_cwk = NULL;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   /* See if the keyword we were given is NULL.
 | 
|---|
| 80 |    * If so, just copy the cwk. */
 | 
|---|
| 81 |   if (!keyword) {
 | 
|---|
| 82 |     ip_cwk = old_cwk;
 | 
|---|
| 83 |     }
 | 
|---|
| 84 |   /* See if we have been given an absolute path. */
 | 
|---|
| 85 |   else if (keyword[0] == ':') {
 | 
|---|
| 86 |     /* Copy the old keyword tree list to the new ip_cwk global. */
 | 
|---|
| 87 |     for (I=old_cwk; I!=NULL; I=I->p) {
 | 
|---|
| 88 |       ip_cwk = splice_keyword_tree_list(I->kt,ip_cwk);
 | 
|---|
| 89 |       }
 | 
|---|
| 90 |     /* Add the keyword into the keyword list. */
 | 
|---|
| 91 |     kt = ip_descend_tree(ip_tree,&(keyword[1]));
 | 
|---|
| 92 |     if (kt) ip_cwk = splice_keyword_tree_list(kt->down,ip_cwk);
 | 
|---|
| 93 |     free_keyword_tree_list(old_cwk);
 | 
|---|
| 94 |     }
 | 
|---|
| 95 |   else {
 | 
|---|
| 96 |     /* For an relative path append the keyword to each of the keyword
 | 
|---|
| 97 |      * paths in the current working keyword list. */
 | 
|---|
| 98 |     ip_keyword_tree_t *kt;
 | 
|---|
| 99 |     for (I=old_cwk; I!=NULL; I=I->p) {
 | 
|---|
| 100 |       kt = ip_descend_tree(I->kt,keyword);
 | 
|---|
| 101 |       if (kt) {
 | 
|---|
| 102 |         kt = ip_descend_tree(I->kt,keyword);
 | 
|---|
| 103 |         ip_cwk = splice_keyword_tree_list(kt->down,ip_cwk);
 | 
|---|
| 104 |         }
 | 
|---|
| 105 |       }
 | 
|---|
| 106 |     free_keyword_tree_list(old_cwk);
 | 
|---|
| 107 |     }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   if (ip_keyword) {
 | 
|---|
| 110 |     *ip_out << "IP_KEYWORDS from IPV2::cwk_add (" << keyword << "): {"
 | 
|---|
| 111 |             << endl;
 | 
|---|
| 112 |     for (I=ip_cwk; I!=NULL; I=I->p) {
 | 
|---|
| 113 |         *ip_out << "  ";
 | 
|---|
| 114 |         print_keyword(*ip_out,I->kt);
 | 
|---|
| 115 |         *ip_out << endl;
 | 
|---|
| 116 |       }
 | 
|---|
| 117 |     *ip_out << "  }" << endl;
 | 
|---|
| 118 |     }
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | /* This pushes the old cwk list without modifying the current cwk list. */
 | 
|---|
| 123 | void
 | 
|---|
| 124 | IPV2::cwk_push()
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   ip_keyword_tree_list_t *I;
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   /* Allocate a stack slot to hold the old cwk. */
 | 
|---|
| 129 |   if (!cwkstack) {
 | 
|---|
| 130 |     cwkstack = (ip_cwk_stack_t *) malloc(sizeof(ip_cwk_stack_t));
 | 
|---|
| 131 |     cwkstack->p = NULL;
 | 
|---|
| 132 |     }
 | 
|---|
| 133 |   else {
 | 
|---|
| 134 |     ip_cwk_stack_t *tmp = cwkstack;
 | 
|---|
| 135 |     cwkstack = (ip_cwk_stack_t *) malloc(sizeof(ip_cwk_stack_t));
 | 
|---|
| 136 |     cwkstack->p = tmp;
 | 
|---|
| 137 |     }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |   /* Push the previous cwk list onto the stack. */
 | 
|---|
| 140 |   cwkstack->ktl = ip_cwk;
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   /* Copy the old keyword tree list to the ip_cwk global. */
 | 
|---|
| 143 |   ip_cwk = NULL;
 | 
|---|
| 144 |   for (I=cwkstack->ktl; I!=NULL; I=I->p) {
 | 
|---|
| 145 |     ip_cwk = splice_keyword_tree_list(I->kt,ip_cwk);
 | 
|---|
| 146 |     }
 | 
|---|
| 147 |   }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /* This moves up the keyword tree for each member of the cwk list.
 | 
|---|
| 150 |  * If a cwk is already at the top of the tree, then that cwk list entry
 | 
|---|
| 151 |  * will be deleted. */
 | 
|---|
| 152 | void
 | 
|---|
| 153 | IPV2::cwk_pop()
 | 
|---|
| 154 | {
 | 
|---|
| 155 |   ip_cwk_stack_t *tmp;
 | 
|---|
| 156 |   if (!cwkstack) {
 | 
|---|
| 157 |     error("IPV2::cwk_pop: tried to pop above the top");
 | 
|---|
| 158 |     }
 | 
|---|
| 159 |   free_keyword_tree_list(ip_cwk);
 | 
|---|
| 160 |   ip_cwk = cwkstack->ktl;
 | 
|---|
| 161 |   tmp = cwkstack;
 | 
|---|
| 162 |   cwkstack = tmp->p;
 | 
|---|
| 163 |   free(tmp);
 | 
|---|
| 164 |   }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | /* Descend the keyword tree using the cwk and obtain a new keyword tree. */
 | 
|---|
| 167 | ip_keyword_tree_t *
 | 
|---|
| 168 | IPV2::ip_cwk_descend_tree(const char* keyword)
 | 
|---|
| 169 | {
 | 
|---|
| 170 |   ip_keyword_tree_list_t *I;
 | 
|---|
| 171 |   ip_keyword_tree_t *kt=NULL;
 | 
|---|
| 172 | 
 | 
|---|
| 173 |   /* If the keyword is NULL, then the first value in the cwk list is returned.*/
 | 
|---|
| 174 |   if (keyword[0] == '\0') {
 | 
|---|
| 175 |     if (ip_cwk) kt = ip_cwk->kt;
 | 
|---|
| 176 |     else kt = NULL;
 | 
|---|
| 177 |     }
 | 
|---|
| 178 |   /* Is the keyword an absolute path? */
 | 
|---|
| 179 |   else if (keyword[0] != ':') {
 | 
|---|
| 180 |     /* See if we can descend to this keyword in any of the cwk's */
 | 
|---|
| 181 |     for (I=ip_cwk; I!=NULL; I=I->p) {
 | 
|---|
| 182 |       if ((kt = ip_descend_tree(I->kt,keyword)) != NULL) break;
 | 
|---|
| 183 |       }
 | 
|---|
| 184 |     }
 | 
|---|
| 185 |   else {
 | 
|---|
| 186 |     kt = ip_descend_tree(ip_tree,&(keyword[1]));
 | 
|---|
| 187 |     }
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   return kt;
 | 
|---|
| 190 |   }
 | 
|---|
| 191 | 
 | 
|---|
| 192 | ////////////////////////////////////////////////////////////////////////
 | 
|---|
| 193 | // IPV2StrTok provides strtok functionality, but allows multiple strings
 | 
|---|
| 194 | // to be processed at a time.
 | 
|---|
| 195 | 
 | 
|---|
| 196 | class IPV2StrTok {
 | 
|---|
| 197 |   private:
 | 
|---|
| 198 |     char* str;
 | 
|---|
| 199 |     const char* delim;
 | 
|---|
| 200 |     int ndelim;
 | 
|---|
| 201 |   public:
 | 
|---|
| 202 |     IPV2StrTok(char* s, const char*d): str(s), delim(d), ndelim(strlen(d)) {}
 | 
|---|
| 203 |     char* tok();
 | 
|---|
| 204 |     int is_white(char c);
 | 
|---|
| 205 | };
 | 
|---|
| 206 | 
 | 
|---|
| 207 | int
 | 
|---|
| 208 | IPV2StrTok::is_white(char c)
 | 
|---|
| 209 | {
 | 
|---|
| 210 |   for (int i=0; i<ndelim; i++) {
 | 
|---|
| 211 |       if (c == delim[i]) {
 | 
|---|
| 212 |           return 1;
 | 
|---|
| 213 |         }
 | 
|---|
| 214 |     }
 | 
|---|
| 215 |   return 0;
 | 
|---|
| 216 | }
 | 
|---|
| 217 | 
 | 
|---|
| 218 | char*
 | 
|---|
| 219 | IPV2StrTok::tok()
 | 
|---|
| 220 | {
 | 
|---|
| 221 |   // move str past the white space
 | 
|---|
| 222 |   while (*str && is_white(*str)) str++;
 | 
|---|
| 223 |   char *ret = str;
 | 
|---|
| 224 | 
 | 
|---|
| 225 |   // put 0 at the end of the string and advance str
 | 
|---|
| 226 |   while (*str && !is_white(*str)) str++;
 | 
|---|
| 227 |   if (*str) {
 | 
|---|
| 228 |       *str = '\0';
 | 
|---|
| 229 |       str++;
 | 
|---|
| 230 |     }
 | 
|---|
| 231 | 
 | 
|---|
| 232 |   if (*ret) return ret;
 | 
|---|
| 233 |   else return NULL;
 | 
|---|
| 234 | }
 | 
|---|
| 235 | 
 | 
|---|
| 236 | ////////////////////////////////////////////////////////////////////////
 | 
|---|
| 237 | 
 | 
|---|
| 238 | /* Descend the given keyword tree using the info in the passed string.
 | 
|---|
| 239 |  * The new keyword tree or NULL, if it is not found, will be returned. */
 | 
|---|
| 240 | ip_keyword_tree_t *
 | 
|---|
| 241 | IPV2::ip_descend_tree(ip_keyword_tree_t* kt,const char* keyword)
 | 
|---|
| 242 | {
 | 
|---|
| 243 |   ip_keyword_tree_t *I,*r;
 | 
|---|
| 244 |   char ch[KEYWORD_LENGTH];
 | 
|---|
| 245 |   char *token;
 | 
|---|
| 246 |   int found;
 | 
|---|
| 247 | 
 | 
|---|
| 248 |   if (!keyword) return kt;
 | 
|---|
| 249 | 
 | 
|---|
| 250 |   if (strlen(keyword)+1 > KEYWORD_LENGTH) {
 | 
|---|
| 251 |       error("ip_descend_tree: maximum KEYWORD_LENGTH has been exceeded");
 | 
|---|
| 252 |       }
 | 
|---|
| 253 | 
 | 
|---|
| 254 |   if (keyword[0] == ':') {
 | 
|---|
| 255 |       kt = ip_tree;
 | 
|---|
| 256 |       strcpy(ch,&keyword[1]);
 | 
|---|
| 257 |     }
 | 
|---|
| 258 |   else {
 | 
|---|
| 259 |       strcpy(ch,keyword);
 | 
|---|
| 260 |     }
 | 
|---|
| 261 | 
 | 
|---|
| 262 |   r = kt;
 | 
|---|
| 263 |   //IPV2StrTok tok(ch, ": \t");
 | 
|---|
| 264 |   IPV2StrTok tok(ch, ":");
 | 
|---|
| 265 |   token = tok.tok();
 | 
|---|
| 266 |   while ((r != NULL) && (token != NULL)) {
 | 
|---|
| 267 |     /* Transverse the circular list. */
 | 
|---|
| 268 |     found = 0;
 | 
|---|
| 269 |     I = r;
 | 
|---|
| 270 |     do {
 | 
|---|
| 271 |       if (!strcmp(token,"..")) {
 | 
|---|
| 272 |         r = I->up;
 | 
|---|
| 273 |         token = tok.tok();
 | 
|---|
| 274 |         if (token == NULL) return I;
 | 
|---|
| 275 |         found = 1;
 | 
|---|
| 276 |         break;
 | 
|---|
| 277 |         }
 | 
|---|
| 278 |       else if (! I->keyword) {
 | 
|---|
| 279 |           return NULL;
 | 
|---|
| 280 |         }
 | 
|---|
| 281 |       else if (!strcmp(token,I->keyword)) {
 | 
|---|
| 282 |         I->seen = 1;
 | 
|---|
| 283 |         if (I->variable) I = ip_descend_tree(I,I->variable);
 | 
|---|
| 284 |         token = tok.tok();
 | 
|---|
| 285 |         if (token == NULL) return I;
 | 
|---|
| 286 |         r = I->down;
 | 
|---|
| 287 |         if (!r) {
 | 
|---|
| 288 |             return NULL;
 | 
|---|
| 289 |           }
 | 
|---|
| 290 |         found = 1;
 | 
|---|
| 291 |         break;
 | 
|---|
| 292 |         }
 | 
|---|
| 293 |       } while ((I = I->across) != r);
 | 
|---|
| 294 |     if (!found) {
 | 
|---|
| 295 |         return NULL;
 | 
|---|
| 296 |       }
 | 
|---|
| 297 |     }
 | 
|---|
| 298 | 
 | 
|---|
| 299 |   if (r && ip_keyword) {
 | 
|---|
| 300 |     *ip_out << "IP_KEYWORD from ip_descend_tree: ";
 | 
|---|
| 301 |     print_keyword(*ip_out,r);
 | 
|---|
| 302 |     *ip_out << endl;
 | 
|---|
| 303 |     }
 | 
|---|
| 304 | 
 | 
|---|
| 305 |   return r;
 | 
|---|
| 306 |   }
 | 
|---|
| 307 | 
 | 
|---|
| 308 | /* Return the value of the given keyword. */
 | 
|---|
| 309 | char *
 | 
|---|
| 310 | IPV2::ip_key_value(const char* keyword)
 | 
|---|
| 311 | {
 | 
|---|
| 312 |   ip_keyword_tree_t *kt;
 | 
|---|
| 313 | 
 | 
|---|
| 314 |   kt = ip_cwk_descend_tree(keyword);
 | 
|---|
| 315 | 
 | 
|---|
| 316 |   if (kt && ip_keyword) {
 | 
|---|
| 317 |     *ip_out << "IP_KEYWORD from ip_key_value: ";
 | 
|---|
| 318 |     print_keyword(*ip_out,kt);
 | 
|---|
| 319 |     *ip_out << endl;
 | 
|---|
| 320 |     }
 | 
|---|
| 321 | 
 | 
|---|
| 322 |   if (kt) return kt->value;
 | 
|---|
| 323 |   else return NULL;
 | 
|---|
| 324 |   }
 | 
|---|
| 325 | 
 | 
|---|
| 326 | /* Free memory for a keyword tree list. */
 | 
|---|
| 327 | void
 | 
|---|
| 328 | IPV2::free_keyword_tree_list(ip_keyword_tree_list_t *ktl)
 | 
|---|
| 329 | {
 | 
|---|
| 330 |   if (!ktl) return;
 | 
|---|
| 331 |   free_keyword_tree_list(ktl->p);
 | 
|---|
| 332 |   free(ktl);
 | 
|---|
| 333 |   }
 | 
|---|
| 334 | 
 | 
|---|
| 335 | /* Splice a new keyword tree into a keyword tree list. */
 | 
|---|
| 336 | ip_keyword_tree_list_t*
 | 
|---|
| 337 | IPV2::splice_keyword_tree_list(ip_keyword_tree_t*kt,ip_keyword_tree_list_t*p)
 | 
|---|
| 338 | {
 | 
|---|
| 339 |   ip_keyword_tree_list_t *r;
 | 
|---|
| 340 | 
 | 
|---|
| 341 |   if (kt==NULL) return p;
 | 
|---|
| 342 |   r = (ip_keyword_tree_list_t *) malloc(sizeof(ip_keyword_tree_list_t));
 | 
|---|
| 343 |   r->kt = kt;
 | 
|---|
| 344 |   r->p = p;
 | 
|---|
| 345 |   return r;
 | 
|---|
| 346 |   }
 | 
|---|