1562e9c7a7fc70dd09289dcec0e731ea46b02393
[debian/amanda] / recover-src / uparse.y
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998, 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  * parser for amrecover interactive language
30  */
31 %{
32 #include "amanda.h"
33 #include "amrecover.h"
34
35 #define DATE_ALLOC_SIZE sizeof("YYYY-MM-DD-HH-MM-SS")   /* includes null */
36
37 void            yyerror(char *s);
38 extern int      yylex(void);
39 extern char *   yytext;
40 %}
41
42 /* DECLARATIONS */
43 %union {
44         int     intval;
45         double  floatval;
46         char *  strval;
47         int     subtok;
48 }
49
50         /* literal keyword tokens */
51
52 %token LISTHOST LISTDISK LISTPROPERTY
53 %token SETHOST SETDISK SETDATE SETTAPE SETMODE SETDEVICE SETPROPERTY
54 %token CD CDX QUIT DHIST LS ADD ADDX EXTRACT DASH_H
55 %token LIST DELETE DELETEX PWD CLEAR HELP LCD LPWD MODE SMB TAR
56 %token APPEND PRIORITY
57 %token NL
58
59         /* typed tokens */
60
61 %token <strval> STRING
62
63
64 /* GRAMMAR */
65 %%
66
67 ucommand:
68         set_command
69   |     setdate_command
70   |     display_command
71   |     quit_command
72   |     add_command
73   |     addx_command
74   |     delete_command
75   |     deletex_command
76   |     local_command
77   |     help_command
78   |     extract_command
79   |     invalid_command
80   ;
81
82 set_command:
83         LISTHOST NL { list_host(); }
84   |     LISTHOST invalid_string { yyerror("Invalid argument"); }
85   |     LISTDISK STRING NL { list_disk($2); amfree($2); }
86   |     LISTDISK NL { list_disk(NULL); }
87   |     LISTDISK STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
88   |     LISTPROPERTY NL { list_property(); }
89   |     LISTPROPERTY invalid_string { yyerror("Invalid argument"); }
90   |     SETHOST STRING NL { set_host($2); amfree($2); }
91   |     SETHOST STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
92   |     SETHOST NL { yyerror("Argument required"); }
93   |     SETDISK STRING STRING NL { set_disk($2, $3); amfree($2); amfree($3); }
94   |     SETDISK STRING NL { set_disk($2, NULL); amfree($2); }
95   |     SETDISK STRING STRING invalid_string { yyerror("Invalid argument"); amfree($2); amfree($3); }
96   |     SETDISK { yyerror("Argument required"); }
97   |     SETTAPE STRING NL { set_tape($2); amfree($2); }
98   |     SETTAPE NL { set_tape("default"); }
99   |     SETTAPE STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
100   |     SETDEVICE STRING NL { set_device(NULL, $2); amfree($2); }
101   |     SETDEVICE DASH_H STRING STRING NL { set_device($3, $4); amfree($3); amfree($4);  }
102   |     SETDEVICE NL { set_device(NULL, NULL); }
103   |     SETDEVICE STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
104   |     SETDEVICE DASH_H STRING NL { yyerror("Invalid argument"); amfree($3); }
105   |     SETDEVICE DASH_H STRING STRING invalid_string { yyerror("Invalid argument"); amfree($3); amfree($4); }
106   |     SETPROPERTY STRING property_value { set_property_name($2, 0); amfree($2); }
107   |     SETPROPERTY APPEND STRING property_value { set_property_name($3, 1); amfree($3); }
108   |     SETPROPERTY PRIORITY STRING property_value { set_property_name($3, 0); amfree($3); }
109   |     SETPROPERTY APPEND PRIORITY STRING property_value { set_property_name($4, 1); amfree($4); }
110   |     SETPROPERTY NL { yyerror("Invalid argument"); }
111   |     SETPROPERTY APPEND NL { yyerror("Invalid argument"); }
112   |     SETPROPERTY PRIORITY NL { yyerror("Invalid argument"); }
113   |     SETPROPERTY APPEND PRIORITY NL { yyerror("Invalid argument"); }
114   |     CD STRING NL { cd_glob($2, 1); amfree($2); }
115   |     CD STRING invalid_string { yyerror("Invalid argument"); }
116   |     CD NL { yyerror("Argument required"); }
117   |     CDX STRING NL { cd_regex($2, 1); amfree($2); }
118   |     CDX STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
119   |     CDX NL { yyerror("Argument required"); }
120   |     SETMODE SMB NL { set_mode(SAMBA_SMBCLIENT); }
121   |     SETMODE TAR NL { set_mode(SAMBA_TAR); }
122   |     SETMODE SMB invalid_string { yyerror("Invalid argument"); }
123   |     SETMODE TAR invalid_string { yyerror("Invalid argument"); }
124   |     SETMODE invalid_string { yyerror("Invalid argument"); }
125   |     SETMODE NL { yyerror("Argument required"); }
126   ;
127
128 setdate_command:
129         SETDATE STRING NL {
130                         time_t now;
131                         struct tm *t;
132                         int y=2000, m=0, d=1, h=0, mi=0, s=0;
133                         int ret;
134                         char *mydate = $2;
135
136                         now = time((time_t *)NULL);
137                         t = localtime(&now);
138                         if (t) {
139                             y = 1900+t->tm_year;
140                             m = t->tm_mon+1;
141                             d = t->tm_mday;
142                         }
143                         if (sscanf(mydate, "---%d", &d) == 1 ||
144                             sscanf(mydate, "--%d-%d", &m, &d) == 2 ||
145                             sscanf(mydate, "%d-%d-%d-%d-%d-%d", &y, &m, &d, &h, &mi, &s) >= 3) {
146                             if (y < 70) {
147                                 y += 2000;
148                             } else if (y < 100) {
149                                 y += 1900;
150                             }
151                             if(y < 1000 || y > 9999) {
152                                 printf("invalid year");
153                             } else if(m < 1 || m > 12) {
154                                 printf("invalid month");
155                             } else if(d < 1 || d > 31) {
156                                 printf("invalid day");
157                             } else if(h < 0 || h > 24) {
158                                 printf("invalid hour");
159                             } else if(mi < 0 || mi > 59) {
160                                 printf("invalid minute");
161                             } else if(s < 0 || s > 59) {
162                                 printf("invalid second");
163                             } else {
164                                 char result[DATE_ALLOC_SIZE];
165                                 if (h == 0 && mi == 0 && s == 0)
166                                     g_snprintf(result, DATE_ALLOC_SIZE, "%04d-%02d-%02d", y, m, d);
167                                 else
168                                     g_snprintf(result, DATE_ALLOC_SIZE, "%04d-%02d-%02d-%02d-%02d-%02d", y, m, d, h, mi, s);
169                                 set_date(result);
170                             }
171                         } else {
172                             printf("Invalid date: %s\n", mydate);
173                         }
174                      }
175   |     SETDATE NL { yyerror("Argument required"); }
176   |     SETDATE STRING invalid_string { yyerror("Invalid argument"); }
177   ;
178
179 display_command:
180         DHIST NL { list_disk_history(); }
181   |     DHIST invalid_string { yyerror("Invalid argument"); }
182   |     LS NL { list_directory(); }
183   |     LS invalid_string { yyerror("Invalid argument"); }
184   |     LIST STRING NL { display_extract_list($2); amfree($2); }
185   |     LIST NL { display_extract_list(NULL); }
186   |     LIST STRING invalid_string { yyerror("Invalid argument"); }
187   |     PWD NL { show_directory(); }
188   |     PWD invalid_string { yyerror("Invalid argument"); }
189   |     CLEAR NL { clear_extract_list(); }
190   |     CLEAR invalid_string { yyerror("Invalid argument"); }
191   |     MODE NL { show_mode (); }
192   |     MODE invalid_string { yyerror("Invalid argument"); }
193   ;
194
195 quit_command:
196         QUIT NL { quit(); }
197   |     QUIT invalid_string { yyerror("Invalid argument"); }
198   ;
199
200 add_command:
201         ADD add_path NL
202   ;
203
204 add_path:
205         add_path STRING { add_glob($2); amfree($2); }
206   |     STRING { add_glob($1); amfree($1); }
207   ;
208
209 addx_command:
210         ADDX addx_path NL
211   ;
212
213 addx_path:
214         addx_path STRING { add_regex($2); amfree($2); }
215   |     STRING { add_regex($1); amfree($1); }
216   ;
217
218 delete_command:
219         DELETE delete_path NL
220   ;
221
222 delete_path:
223         delete_path STRING { delete_glob($2); amfree($2); }
224   |     STRING { delete_glob($1); amfree($1); }
225   ;
226
227 deletex_command:
228         DELETEX deletex_path NL
229   ;
230
231 deletex_path:
232         deletex_path STRING { delete_regex($2); amfree($2); }
233   |     STRING { delete_regex($1); amfree($1); }
234   ;
235
236 local_command:
237         LPWD NL { char * buf= g_get_current_dir(); puts(buf); free(buf); }
238   |     LPWD invalid_string { yyerror("Invalid argument"); }
239   |     LCD STRING NL {
240                 local_cd($2);
241                 amfree($2);
242         }
243   |     LCD STRING invalid_string { yyerror("Invalid argument"); }
244   |     LCD NL { yyerror("Argument required"); }
245   ;
246
247 help_command:
248         HELP NL { help_list(); }
249   |     HELP invalid_string { yyerror("Invalid argument"); }
250   ;
251
252 extract_command:
253         EXTRACT NL { extract_files(); }
254   |     EXTRACT invalid_string { yyerror("Invalid argument"); }
255   ;
256
257 invalid_command:
258         STRING bogus_string {
259             char * errstr = vstralloc("Invalid command: ", $1, NULL);
260             yyerror(errstr);
261             amfree(errstr);
262             YYERROR;
263         } /* Quiets compiler warnings about unused label */
264   ;
265
266 property_value:
267         STRING property_value { add_property_value($1); amfree( $1); }
268   |     NL { ; }
269   ;
270
271 invalid_string:
272         STRING bogus_string { amfree($1); }
273   ;
274
275 bogus_string:
276         STRING bogus_string { amfree($1); }
277   |     NL { ; }
278
279 /* ADDITIONAL C CODE */
280 %%
281
282 void
283 yyerror(
284     char *      s)
285 {
286         g_printf("%s\n", s);
287 }