BUG#: 8628
[tpot/pegasus.git.bak/pegasus.git] / src / slp / slp_client / src / cmd-utils / slp_client / url.l
1 /*
2 //%LICENSE////////////////////////////////////////////////////////////////
3 //
4 // Licensed to The Open Group (TOG) under one or more contributor license
5 // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
6 // this work for additional information regarding copyright ownership.
7 // Each contributor licenses this file to you under the OpenPegasus Open
8 // Source License; you may not use this file except in compliance with the
9 // License.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 //////////////////////////////////////////////////////////////////////////
30 */
31 /* NOCHKSRC */
32 /*****************************************************************************
33  *  Description: encode/decode attribute urls
34  *
35  *  Originated: February 25, 2000
36  *      Original Author: Mike Day md@soft-hackle.net
37  *                                mdd@us.ibm.com
38  *
39  *  Copyright (c) 2001 - 2003  IBM
40  *  Copyright (c) 2000 - 2003 Michael Day
41  *
42  *  Permission is hereby granted, free of charge, to any person obtaining a
43  *  copy of this software and associated documentation files (the "Software"),
44  *  to deal in the Software without restriction, including without limitation
45  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
46  *  and/or sell copies of the Software, and to permit persons to whom the
47  *  Software is furnished to do so, subject to the following conditions:
48  *
49  *  The above copyright notice and this permission notice shall be included in
50  *  all copies or substantial portions of the Software.
51  *
52  *
53  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
56  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
58  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
59  *  DEALINGS IN THE SOFTWARE.
60  *
61  *****************************************************************************/
62
63
64 %{
65 #undef YYLMAX
66 #define YYLMAX 2048
67 #define YY_NEVER_INTERACTIVE 1
68 #undef YY_INPUT
69 #define YY_INPUT(b, r, m) abort()
70 #define exit(i) abort()
71 #include "slp_client.h"
72 #include "y_url.h"
73 #include <stdarg.h>
74
75 #define urlless yyless
76 void urlerror(const char *, ...);
77
78 static int16 heapIndex;
79 static char *heap;
80 static char *buf;
81 /* special counter to exit the appletalk state */
82 char atalk_state = 0;
83
84 static char *_lslp_strdup(const char *s);
85 void url_close_lexer(size_t handle);
86 size_t url_init_lexer(const char *s);
87
88 %}
89
90  /* {}  */
91 digit                   [0-9]
92 upalpha                 [A-Z]
93 lowalpha                [a-z]
94 alpha                   [a-zA-Z]
95 alphanum                [a-zA-Z0-9]
96 hex                     [a-fA-F0-9]
97 safe                    [-$_.~]
98 escaped                 ("%"{hex}{2} | "\\"{hex}{2})
99 mark                    [-_.!~*'()]
100 unreserved              ({alphanum}|{mark})
101 reserved                [;/?:@&=+$,]
102 uric                    ({reserved} | {unreserved} | {escaped})
103
104  /* IPv6 definitions */
105 hex4                    ({hex}{1,4})
106 hexseq                  {hex4}|{hex4}(":"{hex4})*
107 hexpart                 {hexseq}|{hexseq}"::"({hexseq})*|"::"({hexseq})*
108 ip6prefix               {hexpart}"/"{1}{digit}{2}
109 ip4addr                 ({digit}{1,3})"."({digit}{1,3})"."({digit}{1,3})"."({digit}{1,3})
110 ip6addr                 {hexpart}|{hexpart}":"{ip4addr}
111
112 %x IP_SITE
113 %x AT_SITE
114 %x ATTRIBUTE
115 %p       9000
116 %n    300
117 %e    700
118 %option noyywrap prefix="url"
119 %%
120
121  /* the ip user @ host syntax is a special state because some reserved */
122  /* characters are valid - but user the string itself is optional so we can't */
123  /* build it into a full-time rule */
124
125 "//"                    {
126                           BEGIN IP_SITE;
127                           urlless(0);
128                         }
129  /* return the slash as a token */
130 <IP_SITE>"/"            {urllval._i = *urltext; return(urllval._i);}
131
132  /* this next rule needs to kick in even if it matches a zero-length string */
133  /* i.e., it needs to be guaranteed even if there is no user @ host production */
134 <IP_SITE>[-_.!~*'()a-zA-Z0-9;+&=\%]*|"["{ip6addr}"]" {
135                         BEGIN INITIAL;
136                         if (urlleng > 0)
137                         {
138                             if(NULL != (urllval._s = _lslp_strdup(urltext)))
139                                 return(_RESNAME);
140                             else
141                                 return(0L);
142                         }
143                 }
144
145  /* appletalk syntax includes three reserved characters - give the lexer a chance */
146  /* to match an appletalk production before it starts returning reserved */
147  /* chars as tokens. */
148
149 "/"[aA]{1}[tT]{1}"/"    {
150                         BEGIN AT_SITE;
151                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
152                                 return(_AT);
153                         else
154                                 return(0L);
155                         }
156
157 <AT_SITE>(([-a-zA-Z0-9$_.~])|("%"([a-fA-F0-9]{2,2}))){1,31} {
158                         atalk_state++;
159                         if (atalk_state == 3)
160                                 {BEGIN INITIAL;}
161                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
162                                 return(_ZONE);
163                         else
164                                 return(0L);
165                         }
166
167 <AT_SITE>":"            {urllval._i = *urltext; return(urllval._i);}
168 <AT_SITE>"/"            {BEGIN INITIAL; urlless(0);}
169
170  /* ipx syntax includes two reserved characters - give the lexer a chance */
171  /* to match an ipx production before it starts returning reserved chars */
172  /* as tokens */
173
174 "/"[iI]{1}[pP]{1}[xX]{1}"/"({hex}{8})":"({hex}{12})":"({hex}{4}) {
175                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
176                                 return(_IPX);
177                         else
178                                 return(0L);
179                         }
180
181  /* reserved characters  - always a token in normal state unless part of */
182  /* one of the productions above */
183 ";"                     {BEGIN ATTRIBUTE; urllval._i = *urltext; return(urllval._i);}
184 "/"                     {urllval._i = *urltext; return(urllval._i);}
185 "?"                     {urllval._i = *urltext; return(urllval._i);}
186 ":"                     {urllval._i = *urltext; return(urllval._i);}
187 "@"                     {urllval._i = *urltext; return(urllval._i);}
188 "&"                     {urllval._i = *urltext; return(urllval._i);}
189 "="                     {urllval._i = *urltext; return(urllval._i);}
190 "+"                     {urllval._i = *urltext; return(urllval._i);}
191 "$"                     {urllval._i = *urltext; return(urllval._i);}
192 ","                     {urllval._i = *urltext; return(urllval._i);}
193
194  /* a string consisting of only hex digits */
195 {hex}+                  {
196                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
197                                 return(_HEXDIG);
198                         else
199                                 return(0L);
200                         }
201
202  /* an ipv4 address */
203 ({hex}{1,3})"."({hex}{1,3})"."({hex}{1,3})"."({hex}{1,3})|"["{ip6addr}"]" {
204                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
205                                 return(_IPADDR);
206                         else
207                                 return(0L);
208                         }
209
210  /* resource names start with alpha and include alphanum and '+' or '-' */
211  /* but '+' is reserved and must be escaped */
212 [-a-zA-Z0-9.]*  {
213                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
214                                 return(_RESNAME);
215                         else
216                                 return(0L);
217                         }
218
219
220  /* anything else that is not reserved */
221 <ATTRIBUTE>[!-~]+ {
222                         BEGIN INITIAL;
223                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
224                                 return(_ELEMENT);
225                         else
226                                 return(0L);
227                         }
228
229  /* anything else that is not reserved */
230 [^;/?:@&=+$,]+          {
231                         if(NULL != (urllval._s  = _lslp_strdup(urltext)))
232                                 return(_ELEMENT);
233                         else
234                                 return(0L);
235                         }
236
237  /* anything else is an error */
238
239 %%
240
241 static char *_lslp_strdup(const char *s)
242 {
243   char *p = &heap[heapIndex];
244
245   while ((heap[heapIndex++] = *s++))
246       ;
247   return p;
248 }
249
250 void url_close_lexer(size_t handle)
251 {
252   PEGASUS_ASSERT(handle);
253   PEGASUS_ASSERT(buf);
254   PEGASUS_ASSERT(heap);
255   url_delete_buffer((YY_BUFFER_STATE)handle);
256   free(buf);
257   free(heap);
258   buf = heap = 0;
259 }
260
261 size_t url_init_lexer(const char *s)
262 {
263   size_t len = strlen(s) + 2;
264
265   if (!(heap = (char*)malloc(len)))
266   {
267       return 0;
268   }
269   if (!(buf = (char*)malloc(len)))
270   {
271       free (heap);
272       return 0;
273   }
274   heapIndex = 0;
275   strcpy(buf, s);
276   buf[len - 1] = 0;
277
278   return ((size_t) url_scan_buffer(buf, len));
279 }
280
281 void urlerror(const char *s, ...)
282 {
283  return;
284 }       
285