9698e18352ec51eea857de4d185a420ee8125320
[tpot/pegasus/.git] / src / Unsupported / slp_client / src / cmd-utils / slp_client / url.y
1 /*****************************************************************************
2  *  Description: encode/decode attribute urls
3  *
4  *  Originated: March 6, 2000
5  *      Original Author: Mike Day md@soft-hackle.net
6  *                                mdd@us.ibm.com
7  *
8  *  $Header: /cvs/MSB/pegasus/src/Unsupported/slp_client/src/cmd-utils/slp_client/Attic/url.y,v 1.2 2003/06/05 20:39:29 mday Exp $                                                                  
9  *                                                                          
10  *  Copyright (c) 2001 - 2003  IBM                                          
11  *  Copyright (c) 2000 - 2003 Michael Day                                    
12  *                                                                           
13  *  Permission is hereby granted, free of charge, to any person obtaining a  
14  *  copy of this software and associated documentation files (the "Software"),
15  *  to deal in the Software without restriction, including without limitation 
16  *  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
17  *  and/or sell copies of the Software, and to permit persons to whom the     
18  *  Software is furnished to do so, subject to the following conditions:       
19  * 
20  *  The above copyright notice and this permission notice shall be included in 
21  *  all copies or substantial portions of the Software.
22  * 
23  * 
24  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  *  DEALINGS IN THE SOFTWARE.
31  *
32  *****************************************************************************/
33
34
35
36 %{
37
38 #include "slp_client.h"
39
40
41
42 /* prototypes and globals go here */
43 void urlerror(int8 *, ...);
44 int32 urlwrap(void);
45 int32 urllex(void);   
46 int32 urlparse(void);
47 void url_close_lexer(uint32 handle);
48 uint32 url_init_lexer(int8 *s);
49
50 lslpAtomizedURL urlHead = 
51 {
52         &urlHead, &urlHead, TRUE, NULL, 0
53 };
54
55 static lslpAtomList srvcHead = {&srvcHead, &srvcHead, TRUE, NULL, 0};
56 static lslpAtomList siteHead = {&siteHead, &siteHead, TRUE, NULL, 0};
57 static lslpAtomList pathHead = {&pathHead, &pathHead, TRUE, NULL, 0};
58 static lslpAtomList attrHead = {&attrHead, &attrHead, TRUE, NULL, 0};
59
60 %}
61
62 /* definitions for ytab.h */
63 %name-prefix="url"  
64
65 %union {
66         int32 _i;
67         int8 *_s;
68         lslpAtomList *_atl;
69         lslpAtomizedURL *_aturl;
70 }
71
72 %token<_i> _RESERVED
73 %token<_s> _HEXDIG _STAG _RESNAME _NAME _ELEMENT _IPADDR  _IPX
74 %token<_s> _AT _ZONE
75
76 /* typecast the non-terminals */
77
78 /* %type <_i> */
79 %type <_s> ip_site ipx_site at_site hostport host service_id
80 %type <_aturl> url
81 %type <_atl> service_list service site path_list path_el attr_list attr_el
82 %type <_atl> url_part sap
83
84 %%
85 url: service_list sap {
86                         if (NULL != ($$ = (lslpAtomizedURL *)calloc(1, sizeof(lslpAtomizedURL))))
87                         {
88                                 int32 urlLen = 1;
89                                 lslpAtomList *temp = srvcHead.next;
90                                 while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
91                                 {
92                                         urlLen += strlen(temp->str);
93                                         temp = temp->next;
94                                 }
95                                 temp = siteHead.next;
96                                 while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
97                                 {
98                                         urlLen += strlen(temp->str);
99                                         temp = temp->next;
100                                 }
101                                 temp = pathHead.next;
102                                 while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
103                                 {
104                                         urlLen += strlen(temp->str) + 1;
105                                         temp = temp->next;
106                                 }
107                                 temp = attrHead.next;
108                                 while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
109                                 {
110                                         urlLen += strlen(temp->str) + 1;
111                                         temp = temp->next;
112                                 }
113                                 if (NULL != ($$->url = (int8 *)calloc(urlLen, sizeof(int8))))
114                                 {
115                                         temp = srvcHead.next;
116                                         if (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
117                                         {
118                                                 strcpy($$->url, temp->str);
119                                                 temp = temp->next;
120                                         }
121                                         while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
122                                         {
123                                                 strcat($$->url, temp->str);
124                                                 temp = temp->next;
125                                         }
126                                         temp = siteHead.next;
127                                         while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
128                                         {
129                                                 strcat($$->url, temp->str);
130                                                 temp = temp->next;
131                                         }
132                                         temp = pathHead.next;
133                                         while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
134                                         {
135                                                 strcat($$->url, "/");
136                                                 strcat($$->url, temp->str);
137                                                 temp = temp->next;
138                                         }
139                                         temp = attrHead.next;
140                                         while (! _LSLP_IS_HEAD(temp) && temp->str != NULL)
141                                         {
142                                                 strcat($$->url, ";");
143                                                 strcat($$->url, temp->str);
144                                                 temp = temp->next;
145                                         }
146                                         $$->urlHash = lslpCheckSum($$->url, (int16)strlen($$->url));
147                                 }
148
149                                 /* make certain the listheads are initialized */
150                                 $$->srvcs.next = $$->srvcs.prev = &($$->srvcs);
151                                 $$->srvcs.isHead = TRUE;
152                                 $$->site.next = $$->site.prev = &($$->site);
153                                 $$->site.isHead = TRUE;
154                                 $$->path.next = $$->path.prev = &($$->path);
155                                 $$->path.isHead = TRUE;
156                                 $$->attrs.next = $$->attrs.prev = &($$->attrs);
157                                 $$->attrs.isHead = TRUE;
158                                 if (! _LSLP_IS_EMPTY(&srvcHead ))
159                                 {
160                                         _LSLP_LINK_HEAD(&($$->srvcs), &srvcHead);
161                                 }
162                                 if (! _LSLP_IS_EMPTY(&siteHead))
163                                 {
164                                         _LSLP_LINK_HEAD(&($$->site), &siteHead);
165                                 }
166                                 if (! _LSLP_IS_EMPTY(&pathHead))
167                                 {
168                                         _LSLP_LINK_HEAD(&($$->path), &pathHead);
169                                 }
170                                 if (! _LSLP_IS_EMPTY(&attrHead))
171                                 {
172                                         _LSLP_LINK_HEAD(&($$->attrs), &attrHead);
173                                 }
174                                 _LSLP_INSERT_BEFORE($$, &urlHead);
175                                 lslpInitAtomLists();
176                         }
177                 }
178         ;
179 service_list: service   {
180                         $$ = &srvcHead;
181                         if ($1 != NULL)
182                         {
183                                 _LSLP_INSERT_BEFORE($1, $$);
184                         }
185                 }
186
187         | service_list service {
188                         $$ = &srvcHead;
189                         if ($2 != NULL)
190                         {
191                                 _LSLP_INSERT_BEFORE($2, $$);
192                         }
193                 }
194         ;
195
196 service:  _RESNAME ':'  {
197                         if (NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
198                         {
199                                 $$->next = $$->prev = $$;
200                                 if (NULL != ($$->str = (int8 *)calloc(2 + strlen($1), sizeof(int8))))
201                                 {
202                                         strcpy($$->str, $1);
203                                         strcat($$->str, ":");   
204                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
205                                 }
206                                 else
207                                 {
208                                         free($$);
209                                         $$ = NULL;
210                                 }
211                         }
212
213                 }
214         |         _RESNAME '.' _RESNAME ':'     {
215                         if (NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
216                         {
217                                 $$->next = $$->prev = $$;
218                                 if (NULL != ($$->str = (int8 *)calloc(3 + strlen($1) + strlen($3), sizeof(int8))))
219                                 {
220                                         strcpy($$->str, $1);
221                                         strcat($$->str, ".");
222                                         strcat($$->str, $3);
223                                         strcat($$->str, ":");   
224                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
225                                 }
226                                 else
227                                 {
228                                         free($$);
229                                         $$ = NULL;
230                                 }
231                         }
232
233                 }
234         ;
235
236 sap:  site {
237                         $$ = &siteHead;
238                         if ($1 != NULL)
239                         {
240                                 _LSLP_INSERT_BEFORE($1, $$);
241                         }
242                 }
243         |       site url_part   {
244                         $$ = &siteHead;
245                         if ($1 != NULL)
246                         {
247                                         _LSLP_INSERT_BEFORE($1, $$);
248                         }
249                 }
250         ;
251
252 site:  ip_site {
253                         if ($1 != NULL)
254                         {
255                                 if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
256                                 {
257                                         $$->next = $$->prev = $$;
258                                         $$->str = $1;
259                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
260                                 }
261                         }
262                         else
263                                 $$ = NULL;
264                 }
265         |       ipx_site {
266                         if ($1 != NULL)
267                         {
268                                 if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
269                                 {
270                                         $$->next = $$->prev = $$;
271                                         $$->str = $1;
272                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
273                                 }
274                         }
275                         else
276                                 $$ = NULL;
277                 }
278         |       at_site {
279                         if ($1 != NULL)
280                         {
281                                 if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
282                                 {
283                                         $$->next = $$->prev = $$;
284                                         $$->str = $1;
285                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
286                                 }
287                         }
288                         else
289                                 $$ = NULL;
290                 }
291
292 |       service_id {
293   
294                         if ($1 != NULL)
295                         {
296                                 if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
297                                 {
298                                         $$->next = $$->prev = $$;
299                                         $$->str = $1;
300                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
301                                 }
302                         }
303                         else
304                                 $$ = NULL;
305   
306 }
307
308         ;
309
310 ip_site: '/''/' {
311                         $$ = strdup("//");
312                 }
313         |   '/''/' hostport {
314                         if(NULL != $3 && (NULL !=($$ = (int8 *)calloc(3 + strlen($3), sizeof(int8)))))
315                         {
316                                 strcpy($$, "//");
317                                 strcat($$, $3);
318                         }
319
320                 }
321         |   '/''/' _RESNAME '@' hostport {
322                         if(NULL != $5 && (NULL !=($$ = (int8 *)calloc(4 + strlen($3) + strlen($5), sizeof(int8)))))
323                         {
324                                 strcpy($$, "//");
325                                 strcat($$, $3);
326                                 strcat($$, "@");
327                                 strcat($$, $5);
328                         }
329                 }
330         ;
331
332 service_id: _RESNAME {
333           $$ = strdup($1);
334         }
335         ;
336
337 ipx_site: _IPX {
338                         $$ = $1;
339                 }
340         ;
341
342 at_site: _AT _ZONE ':' _ZONE ':' _ZONE {
343                         if(NULL != ($$ = (int8 *)calloc(strlen($1) + strlen($2) + strlen($4) + strlen($6) + 3, sizeof(int8))))
344                         {
345                                 strcpy($$, $1);
346                                 strcat($$, $2);
347                                 strcat($$, ":");
348                                 strcat($$, $4);
349                                 strcat($$, ":");
350                                 strcat($$, $6);
351                         }
352                 }
353         ;
354
355 hostport: host {
356                         $$ = $1;        
357                 }
358         |       host ':' _HEXDIG {
359                         if ($1 != NULL)
360                         {
361                                 if(NULL != ($$ = (int8 *)calloc(strlen($1) + strlen($3) + 2, sizeof(int8))))
362                                 {
363                                         strcpy($$, $1);
364                                         strcat($$, ":");
365                                         strcat($$, $3);
366                                 }
367                         }
368                         else
369                                 $$ = NULL;      
370                 }
371         ;
372
373 host: _RESNAME {
374                         $$ = $1;
375                 }
376         |  _IPADDR {
377                         $$ = $1;
378                 }
379         ;
380
381 url_part: path_list {
382                         ;       
383                 }
384         |       attr_list {
385                         ;               
386                 }
387         |       path_list attr_list {
388                         ;
389                 }
390         ;
391
392 path_list:      path_el {
393                         $$ = &pathHead;
394                         if ($1 != NULL)
395                         {
396                                 _LSLP_INSERT_BEFORE($1, $$);
397                         }
398                 }
399         | path_list path_el {
400                         $$ = &pathHead;
401                         if ($2 != NULL)
402                         {
403                                 _LSLP_INSERT_BEFORE($2, $$);
404                         }
405                 }
406
407         ;
408
409 path_el: '/' {
410                          /* dangling path slash - do nothing */
411                          $$ = NULL;
412                          }
413         |    '/' _ELEMENT  {
414                         if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
415                         {
416                                 $$->prev = $$->next = $$;
417                                 if(NULL != ($$->str = (int8 *)calloc(1 + strlen($2), sizeof(int8))))
418                                 {
419                                         strcpy($$->str, $2);
420                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
421
422                                 }
423                                 else
424                                 {
425                                         free($$);
426                                         $$ = NULL;
427                                 }
428                         }
429
430                 }
431         |        '/' _RESNAME  {
432                         if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
433                         {
434                                 $$->prev = $$->next = $$;
435                                 if(NULL != ($$->str = (int8 *)calloc(1 + strlen($2), sizeof(int8))))
436                                 {
437                                         strcpy($$->str, $2);
438                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
439                                 }
440                                 else
441                                 {
442                                         free($$);
443                                         $$ = NULL;
444                                 }
445                         }
446
447                 }
448         |       '/' _HEXDIG {
449                         if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
450                         {
451                                 $$->prev = $$->next = $$;
452                                 if(NULL != ($$->str = (int8 *)calloc(1 + strlen($2), sizeof(int8))))
453                                 {
454                                         strcpy($$->str, $2);
455                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
456                                 }
457                                 else
458                                 {
459                                         free($$);
460                                         $$ = NULL;
461                                 }
462                         }
463                 }
464         ;
465
466 attr_list: attr_el {
467                         $$ = &attrHead;
468                         if ($1 != NULL)
469                         {
470                                 _LSLP_INSERT_BEFORE($1, $$);
471                         }
472                 }
473         | attr_list attr_el {
474                         $$ = &attrHead;
475                         if ($2 != NULL)
476                         {
477                                 _LSLP_INSERT_BEFORE($2, $$);
478                         }
479                 }
480         ;
481
482 attr_el: ';' _ELEMENT  {
483                         if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
484                         {
485                                 $$->prev = $$->next = $$;
486                                 if(NULL != ($$->str = (int8 *)calloc(1 + strlen($2), sizeof(int8))))
487                                 {
488                                         strcpy($$->str, $2);
489                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
490                                 }
491                                 else
492                                 {
493                                         free($$);
494                                         $$ = NULL;
495                                 }
496                         }
497                 }
498
499         |  ';' _ELEMENT '=' _ELEMENT {
500                         if(NULL != ($$ = (lslpAtomList *)calloc(1, sizeof(lslpAtomList))))
501                         {
502                                 $$->prev = $$->next = $$;
503                                 if(NULL != ($$->str = (int8 *)calloc(2 + strlen($2) + strlen($4), sizeof(int8))))
504                                 {
505                                         strcpy($$->str, $2);
506                                         strcat($$->str, "=");
507                                         strcat($$->str, $4);
508                                         $$->hash = lslpCheckSum($$->str, (int16)strlen($$->str));
509                                 }
510                                 else
511                                 {
512                                         free($$);
513                                         $$ = NULL;
514                                 }
515                         }
516                 }
517         ;
518
519 %% 
520
521
522 lslpAtomList *lslpAllocAtom(void)
523 {
524         lslpAtomList *temp = (lslpAtomList *)calloc(1, sizeof(lslpAtomList));
525         if (temp != NULL)
526         {
527                 temp->next = temp->prev = temp;
528         }
529         return(temp);
530 }       
531
532 lslpAtomList *lslpAllocAtomList(void)
533 {
534         lslpAtomList *temp =lslpAllocAtom(); 
535         if (temp != NULL)
536                 temp->isHead = TRUE;
537         return(temp);
538 }       
539
540 void lslpFreeAtom(lslpAtomList *a )
541 {
542         assert(a != NULL);
543         if (a->str != NULL)
544                 free(a->str);
545         free(a);
546         return;
547 }       
548
549 void lslpFreeAtomList(lslpAtomList *l, int32 flag)
550 {
551         lslpAtomList *temp;
552         assert(l != NULL);
553         assert(_LSLP_IS_HEAD(l));
554         while (! _LSLP_IS_EMPTY(l))
555         {
556                 temp = l->next;
557                 temp->prev->next = temp->next;
558                 temp->next->prev = temp->prev; 
559                 lslpFreeAtom(temp);
560         }
561         if (flag)
562                 lslpFreeAtom(l);
563         return;
564 }       
565
566 lslpAtomizedURL *lslpAllocAtomizedURL(void)
567 {
568         lslpAtomizedURL *temp = (lslpAtomizedURL *)calloc(1, sizeof(lslpAtomizedURL));
569         if (temp != NULL)
570         {
571                 temp->prev = temp->next = temp;
572         }
573         return(temp);
574 }       
575
576 lslpAtomizedURL *lslpAllocAtomizedURLList(void)
577 {
578         lslpAtomizedURL *temp = lslpAllocAtomizedURL();
579         if (temp != NULL)
580         {
581                 temp->isHead = TRUE;
582         }
583         return(temp);
584 }       
585
586 void lslpFreeAtomizedURL(lslpAtomizedURL *u)
587 {
588         assert(u != NULL);
589         if (u->url != NULL)
590                 free(u->url);
591         if (! _LSLP_IS_EMPTY(&(u->srvcs)))
592                 lslpFreeAtomList(&(u->srvcs), 0);
593         if (! _LSLP_IS_EMPTY(&(u->site)))
594                 lslpFreeAtomList(&(u->site), 0);
595         if (! _LSLP_IS_EMPTY(&(u->path)))
596                 lslpFreeAtomList(&(u->path), 0);
597         if (!  _LSLP_IS_EMPTY(&(u->attrs)))
598                 lslpFreeAtomList(&(u->attrs), 0);
599         free(u);
600         return;
601 }       
602
603 void lslpFreeAtomizedURLList(lslpAtomizedURL *l, int32 flag)
604 {
605         lslpAtomizedURL *temp;
606         assert(l != NULL);
607         assert(_LSLP_IS_HEAD(l));
608         while (! (_LSLP_IS_HEAD(l->next)))
609         {
610                 temp = l->next;
611                 temp->prev->next = temp->next;
612                 temp->next->prev = temp->prev;
613                 lslpFreeAtomizedURL(temp);
614         }
615         if (flag)
616                 free(l);
617         return;
618 }       
619
620 void lslpInitAtomLists(void)
621 {
622         srvcHead.next = srvcHead.prev = &srvcHead;
623         siteHead.next = siteHead.prev = &siteHead;
624         pathHead.next = pathHead.prev = &pathHead;
625         attrHead.next = attrHead.prev = &attrHead;
626         return;
627 }       
628
629 void lslpInitURLList(void)
630 {
631         urlHead.next = urlHead.prev = &urlHead;
632         return;
633 }       
634
635
636
637 void lslpCleanUpURLLists(void)
638 {
639   
640   lslpFreeAtomList(&srvcHead, 0);
641   lslpFreeAtomList(&siteHead, 0);
642   lslpFreeAtomList(&pathHead, 0);
643   lslpFreeAtomList(&attrHead, 0);
644   lslpFreeAtomizedURLList(&urlHead, 0);
645 }
646
647 lslpAtomizedURL *_lslpDecodeURLs(int8 *u[], int32 count)
648 {
649   int32 i;
650   
651   uint32 lexer = 0;
652   lslpAtomizedURL *temp = NULL;
653   assert(u != NULL && u[count - 1] != NULL);
654   lslpInitURLList();
655   lslpInitAtomLists();
656   
657   for (i = 0; i < count; i++) {
658     if (NULL == u[i])
659       break;
660     if((0 != (lexer = url_init_lexer(u[i])))) {
661       if(urlparse())
662         lslpCleanUpURLLists();
663       url_close_lexer(lexer);
664     }
665   }
666   if (! _LSLP_IS_EMPTY(&urlHead)) {
667     if(NULL != (temp = lslpAllocAtomizedURLList())) {
668       _LSLP_LINK_HEAD(temp, &urlHead);
669     }
670   }
671   
672   return(temp); 
673 }        
674