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