Imported Upstream version 3.3.3
[debian/amanda] / recover-src / uscan.l
1 /*
2  * amanda, the advanced maryland automatic network disk archiver
3  * Copyright (c) 1991-2000 University of Maryland at College Park
4  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id$
29  *
30  * lexer for amrecover interactive language
31  */
32 %{
33 #include "amanda.h"
34 #include "uparse.h"
35
36 /*
37  * We redefine this here to prevent compiler warning about ignoring fwrite
38  * return value...
39  */
40 #undef ECHO
41 #define ECHO do {                                               \
42         if (fwrite(yytext, (size_t)yyleng, 1, yyout) <= 0) {    \
43             yyerror("ECHO failure");                            \
44         }                                                       \
45 } while (0)
46
47 #define YY_NO_UNPUT
48
49 #define YY_DECL int yylex(void)
50 extern int      yylex(void);
51
52 extern void     yyerror(char *s);
53 extern int      yyparse(void);
54 int             process_line(char *line);
55 %}
56
57 %x quotedstring
58 %x needdevice
59 %x needstring
60 %x needmode
61 %x astring
62 %x propertyappend
63 %x propertypriority
64
65 LINEFEED        [[:space:]]*("\n"|"\r"|"\r\n")
66 %{
67 static char *string_buf = NULL;
68 %}
69
70 %%
71
72 %{
73     /* literal keyword tokens */
74 %}
75
76 listhost        { BEGIN(needstring); return LISTHOST; }
77 listdisk        { BEGIN(needstring); return LISTDISK; }
78 listproperty    { BEGIN(needstring); return LISTPROPERTY; }
79 sethost         { BEGIN(needstring); return SETHOST; }
80 setdisk         { BEGIN(needstring); return SETDISK; }
81 setdate         { BEGIN(needstring); return SETDATE; }
82 setmode         { BEGIN(needmode); return SETMODE; }
83 settape         { BEGIN(needstring); return SETTAPE; }
84 setdevice       { BEGIN(needdevice); return SETDEVICE; }
85 setproperty     { BEGIN(propertyappend); return SETPROPERTY; }
86 settranslate    { BEGIN(needstring); return SETTRANSLATE; }
87 <needdevice>-h  { BEGIN(needstring); return DASH_H; }
88 cd              { BEGIN(needstring); return CD; }
89 cdx             { BEGIN(needstring); return CDX; }
90 quit            { BEGIN(needstring); return QUIT; }
91 exit            { BEGIN(needstring); return QUIT; }
92 history         { BEGIN(needstring); return DHIST; }
93 ls              { BEGIN(needstring); return LS; }
94 add             { BEGIN(needstring); return ADD; }
95 addx            { BEGIN(needstring); return ADDX; }
96 list            { BEGIN(needstring); return LIST; }
97 delete          { BEGIN(needstring); return DELETE; }
98 deletex         { BEGIN(needstring); return DELETEX; }
99 pwd             { BEGIN(needstring); return PWD; }
100 clear           { BEGIN(needstring); return CLEAR; }
101 <INITIAL>help   { BEGIN(INITIAL); return HELP; }
102 \?              { BEGIN(needstring); return HELP; }
103 lcd             { BEGIN(needstring); return LCD; }
104 lpwd            { BEGIN(needstring); return LPWD; }
105 extract         { BEGIN(needstring); return EXTRACT; }
106 mode            { BEGIN(needstring); return MODE; }
107 <needmode>tar   { BEGIN(needstring); return TAR; }
108 <needmode>smb   { BEGIN(needstring); return SMB; }
109 <INITIAL,needdevice,needmode,needstring,astring,propertyappend,propertypriority>{LINEFEED}      { BEGIN(INITIAL); return NL; }
110
111 %{
112     /* quoted file names */
113 %}
114
115 <propertyappend>append {
116     BEGIN(propertypriority);
117     return APPEND;
118 }
119
120 <propertyappend,propertypriority>priority {
121     BEGIN(needstring);
122     return PRIORITY;
123 }
124
125 <needdevice,needmode,needstring,astring,propertyappend,propertypriority>\"                      {
126     if(string_buf != NULL) {
127         g_printf("ERROR:string_buf != NULL: %s\n",string_buf);
128     }
129     BEGIN(quotedstring);
130     strappend(string_buf, yytext);
131 }
132
133 <quotedstring>[^\\\"\n\r]+      {
134     strappend(string_buf, yytext);
135 }
136
137 <quotedstring>\\.       {
138     /* escaped character (including quote) */
139     strappend(string_buf, yytext);
140 }
141
142 <quotedstring>\"        { /* saw closing quote - all done */
143     strappend(string_buf, yytext);
144     yylval.strval = string_buf;
145     string_buf = NULL;
146     BEGIN(needstring);
147     return STRING;
148 }
149
150 <quotedstring>{LINEFEED} {
151     fprintf(stderr,"Unterminated quoted string\n");
152     string_buf = NULL;
153     BEGIN(INITIAL);
154     return NL;
155 }
156
157 %{
158     /* file names */
159 %}
160
161 <INITIAL,needdevice,needmode,needstring,astring,propertyappend,propertypriority>[^[:space:][:cntrl:]"]+         {
162     yylval.strval = stralloc(yytext);
163     BEGIN(needstring);
164     return STRING;
165 }
166
167 %{
168     /* whitespace */
169 %}
170
171 <INITIAL,needdevice,needmode,needstring,astring,propertyappend,propertypriority>[[:space:]]+ ; /* whitespace */
172
173 %{
174     /* anything else */
175     /* everything should have been handled by now, so this rule is disabled */
176 %}
177
178 %{
179 #if 0
180 .       { yyerror("invalid character"); }
181 #endif
182 %}
183
184 %%
185
186 int
187 process_line(
188     char *      line)
189 {
190     YY_BUFFER_STATE b;
191     int result;
192
193     char *line1 = stralloc2(line, "\n");
194     b = yy_scan_string(line1);          /* tell lex to scan lineread */
195     result = yyparse();                 /* parse lineread and act */
196     yy_delete_buffer(b);
197     amfree(line1);
198     return result;
199 }
200
201
202 int
203 yywrap(void)
204 {
205   return 1;
206 }
207