Imported Upstream version 2.5.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: uscan.l,v 1.27 2006/07/05 11:15:56 martinea Exp $
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 DATE_ALLOC_SIZE         sizeof("YYYY-MM-DD-HH-MM-SS")   /* includes null */
49
50 #define YY_DECL int yylex(void)
51 extern int      yylex(void);
52
53 extern void     yyerror(char *s);
54 extern int      yyparse(void);
55 static int      ll_parse_date(int type, char *text);
56 int             process_line(char *line);
57 %}
58
59 %x quotedpath
60
61 %{
62 static char *string_buf = NULL;
63 %}
64
65 %%
66
67 %{
68     /* literal keyword tokens */
69 %}
70
71 listhost        { return LISTHOST; }
72 listdisk        { return LISTDISK; }
73 sethost         { return SETHOST; }
74 setdisk         { return SETDISK; }
75 setdate         { return SETDATE; }
76 setmode         { return SETMODE; }
77 settape         { return SETTAPE; }
78 cd              { return CD; }
79 cdx             { return CDX; }
80 quit            { return QUIT; }
81 exit            { return QUIT; }
82 history         { return DHIST; }
83 ls              { return LS; }
84 add             { return ADD; }
85 addx            { return ADDX; }
86 list            { return LIST; }
87 delete          { return DELETE; }
88 deletex         { return DELETEX; }
89 pwd             { return PWD; }
90 clear           { return CLEAR; }
91 help            { return HELP; }
92 \?              { return HELP; }
93 lcd             { return LCD; }
94 lpwd            { return LPWD; }
95 extract         { return EXTRACT; }
96 smb             { return SMB; }
97 tar             { return TAR; }
98 mode            { return MODE; }
99
100 %{
101     /* dates */
102 %}
103
104 ---[0-9]+               { return ll_parse_date(1, yytext); }
105 --[0-9]+-[0-9]+         { return ll_parse_date(2, yytext); }
106 [0-9]+-[0-9]+-[0-9]+    { return ll_parse_date(3, yytext); }
107 [0-9]+-[0-9]+-[0-9]+-[0-9]+-[0-9]+-[0-9]+       { return ll_parse_date(4, yytext); }
108 [0-9]+-[0-9]+-[0-9]+-[0-9]+-[0-9]+      { return ll_parse_date(5, yytext); }
109
110 %{
111     /* quoted file names */
112 %}
113
114 \"                      {
115     if(string_buf != NULL) {
116         printf("ERROR:string_buf != NULL: %s\n",string_buf);
117     }
118     BEGIN(quotedpath);
119     strappend(string_buf, yytext);
120 }
121
122 <quotedpath>[^\\\"]+    {
123     strappend(string_buf, yytext);
124 }
125
126 <quotedpath>\\. {
127     /* escaped character (including quote) */
128     strappend(string_buf, yytext);
129 }
130
131 <quotedpath>\"  { /* saw closing quote - all done */
132     strappend(string_buf, yytext);
133     yylval.strval = string_buf;
134     string_buf = NULL;
135     BEGIN(INITIAL);
136     return PATH;
137 }
138
139 %{
140     /* file names */
141 %}
142
143 [^[:space:][:cntrl:]"]+         {
144     yylval.strval = stralloc(yytext);
145     return PATH;
146 }
147
148 %{
149     /* whitespace */
150 %}
151
152 [[:space:]]+    ;     /* whitespace */
153
154 %{
155     /* anything else */
156     /* everything should have been handled by now, so this rule is disabled */
157 %}
158
159 %{
160 #if 0
161 .       { yyerror("invalid character"); }
162 #endif
163 %}
164
165 %%
166
167 int
168 process_line(
169     char *      line)
170 {
171     YY_BUFFER_STATE b;
172     int result;
173
174     b = yy_scan_string(line);           /* tell lex to scan lineread */
175     result = yyparse();                 /* parse lineread and act */
176     yy_delete_buffer(b);
177     return result;
178 }
179
180 static int
181 ll_parse_date(
182     int         type,
183     char *      text)
184 {
185     time_t now;
186     struct tm *t;
187     int y=2000, m=0, d=1, h=0, mi=0, s=0;
188     int ret;
189
190     now = time((time_t *)NULL);
191     t = localtime(&now);
192     if (t) {
193         y = 1900+t->tm_year;
194         m = t->tm_mon+1;
195         d = t->tm_mday;
196     }
197     switch(type) {
198     case 1:
199         if (sscanf(text, "---%d", &d) != 1) {
200             yyerror("invalid date");
201         }
202         break;
203     case 2:
204         if (sscanf(text, "--%d-%d", &m, &d) != 2) {
205             yyerror("invalid date");
206         }
207         break;
208     case 3:
209         if (sscanf(text, "%d-%d-%d", &y, &m, &d) != 3) {
210             yyerror("invalid date");        
211         }
212         break;
213     case 4:
214         if (sscanf(text, "%d-%d-%d-%d-%d-%d", &y, &m, &d, &h, &mi, &s) != 6) {
215             yyerror("invalid date");
216         }
217         break;
218     case 5:
219         if (sscanf(text, "%d-%d-%d-%d-%d", &y, &m, &d, &h, &mi) != 5) {
220             yyerror("invalid date");
221         }
222         break;
223     }
224
225     ret = PATH;                         /* cause a parse error */
226     if(y < 70) {
227         y += 2000;
228     } else if(y < 100) {
229         y += 1900;
230     }
231     if(y < 1000 || y > 9999) {
232         yyerror("invalid year");
233     } else if(m < 1 || m > 12) {
234         yyerror("invalid month");
235     } else if(d < 1 || d > 31) {
236         yyerror("invalid day");
237     } else if(h < 0 || h > 24) {
238         yyerror("invalid hour");
239     } else if(mi < 0 || mi > 59) {
240         yyerror("invalid minute");
241     } else if(s < 0 || s > 59) {
242         yyerror("invalid second");
243     } else if(type < 4) {
244         yylval.strval = alloc(DATE_ALLOC_SIZE);
245         snprintf(yylval.strval, DATE_ALLOC_SIZE, "%04d-%02d-%02d", y, m, d);
246         ret = DATE;
247     } else {
248         yylval.strval = alloc(DATE_ALLOC_SIZE);
249         snprintf(yylval.strval, DATE_ALLOC_SIZE, "%04d-%02d-%02d-%02d-%02d-%02d", y, m, d, h, mi, s);
250         ret = DATE;
251     }
252     return ret;
253 }
254
255 int
256 yywrap(void)
257 {
258   return 1;
259 }
260