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