1 /*
2  *
3   ***** BEGIN LICENSE BLOCK *****
4 
5   Copyright (C) 2009-2019 Olof Hagsand
6   Copyright (C) 2020 Olof Hagsand and Rubicon Communications, LLC(Netgate)
7 
8   This file is part of CLIXON.
9 
10   Licensed under the Apache License, Version 2.0 (the "License");
11   you may not use this file except in compliance with the License.
12   You may obtain a copy of the License at
13 
14     http://www.apache.org/licenses/LICENSE-2.0
15 
16   Unless required by applicable law or agreed to in writing, software
17   distributed under the License is distributed on an "AS IS" BASIS,
18   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   See the License for the specific language governing permissions and
20   limitations under the License.
21 
22   Alternatively, the contents of this file may be used under the terms of
23   the GNU General Public License Version 3 or later (the "GPL"),
24   in which case the provisions of the GPL are applicable instead
25   of those above. If you wish to allow use of your version of this file only
26   under the terms of the GPL, and not to allow others to
27   use your version of this file under the terms of Apache License version 2,
28   indicate your decision by deleting the provisions above and replace them with
29   the  notice and other provisions required by the GPL. If you do not delete
30   the provisions above, a recipient may use your version of this file under
31   the terms of any one of the Apache License version 2 or the GPL.
32 
33   ***** END LICENSE BLOCK *****
34  *
35  * "api-path" is "URI-encoded path expression" definition in RFC8040 3.53.
36  * BNF:
37  *  <api-path>       := <root> ("/" (<api-identifier> | <list-instance>))*
38  *  <root>           := <string>
39  *  <api-identifier> := [<module-name> ":"] <identifier>
40  *  <module-name>    := <identifier>
41  *  <list-instance>  := <api-identifier> "=" key-value *("," key-value)
42  *  <key-value>      := <string>
43  *  <string>         := <an unquoted string>
44  *  <identifier>     := (<ALPHA> | "_") (<ALPHA> | <DIGIT> | "_" | "-" | ".")
45  * @note 1. <root> is the RESTCONF root resource (Sec 3.3) omitted in all calls below, it is
46  *          assumed to be stripped from api-path before calling these functions.
47  * @note 2. characters in a key value string are constrained, and some characters need to be
48  *          percent-encoded,
49  * XXX For some reason, I cant use "return *string" in these rules, so I resort to symbols
50  * (eg slash)
51  */
52 
53 %{
54 
55 #include "clixon_config.h"
56 
57 #include <stdio.h>
58 #include <string.h>
59 #include <stdint.h>
60 #include <errno.h>
61 
62 #include "clixon_api_path_parse.tab.h" /* generated */
63 
64 #include <cligen/cligen.h>
65 
66 #include "clixon_queue.h"
67 #include "clixon_hash.h"
68 #include "clixon_handle.h"
69 #include "clixon_yang.h"
70 #include "clixon_log.h"
71 #include "clixon_string.h"
72 #include "clixon_xml.h"
73 #include "clixon_path.h"
74 #include "clixon_api_path_parse.h"
75 
76 /* Redefine main lex function so that you can send arguments to it: _yy is added to arg list */
77 #define YY_DECL int clixon_api_path_parselex(void *_ay)
78 
79 /* Dont use input function (use user-buffer) */
80 #define YY_NO_INPUT
81 
82 /* typecast macro */
83 #define _AY ((clixon_api_path_yacc *)_ay)
84 
85 #define     MAXBUF  4*4*64*1024
86 
87 #undef clixon_api_path_parsewrap
88 int
clixon_api_path_parsewrap(void)89 clixon_api_path_parsewrap(void)
90 {
91     return 1;
92 }
93 
94 %}
95 
96 namestart  [A-Z_a-z]
97 namechar   [A-Z_a-z\-\.0-9]
98 identifier {namestart}{namechar}*
99 
100 %x INIT
101 %s KEYV
102 
103 %%
104 
105 <INIT,KEYV>[ \t]
106 <INIT,KEYV>\n      { _AY->ay_linenum++; }
107 <INIT,KEYV>\r
108 <INIT,KEYV><<EOF>> { return X_EOF; }
109 
110 <INIT>\/           { return SLASH;}
111 <INIT>\=           { BEGIN(KEYV); return EQUAL; }
112 <INIT>\:           { return COLON; }
113 <INIT>{identifier} { clixon_api_path_parselval.string = strdup(yytext);
114                      return IDENTIFIER; }
115 <INIT>.            { clixon_api_path_parseerror(_AY, "LEXICAL ERROR\n"); return -1; }
116 
117 <KEYV>\,           { return COMMA; }
118 <KEYV>\/           { BEGIN(INIT); return SLASH; }
119 <KEYV>[^:/?#\[\]@,]+ { clixon_api_path_parselval.string = strdup(yytext);
120                      return STRING;}
121 <KEYV>.            { clixon_api_path_parseerror(_AY, "LEXICAL ERROR\n"); return -1; }
122 
123 %%
124 
125 /*! Initialize scanner.
126  */
127 int
128 api_path_scan_init(clixon_api_path_yacc *ay)
129 {
130   BEGIN(INIT);
131   ay->ay_lexbuf = yy_scan_string(ay->ay_parse_string);
132 #if 1 /* XXX: just to use unput to avoid warning  */
133   if (0)
134     yyunput(0, "");
135 #endif
136 
137   return 0;
138 }
139 
140 /*
141  * free buffers
142  * Even within Flex version 2.5 (this is assumed), freeing buffers is different.
143  */
144 int
api_path_scan_exit(clixon_api_path_yacc * ay)145 api_path_scan_exit(clixon_api_path_yacc *ay)
146 {
147     yy_delete_buffer(ay->ay_lexbuf);
148     clixon_api_path_parselex_destroy();  /* modern */
149     return 0;
150 }
151 
152