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