Imported Upstream version 3.3.1
[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 SETTRANSLATE
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   |     SETTRANSLATE NL { set_translate(NULL); }
91   |     SETTRANSLATE STRING invalid_string NL { yyerror("Invalid argument"); }
92   |     SETTRANSLATE STRING NL { set_translate($2); amfree($2); }
93   |     SETHOST STRING NL { set_host($2); amfree($2); }
94   |     SETHOST STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
95   |     SETHOST NL { yyerror("Argument required"); }
96   |     SETDISK STRING STRING NL { set_disk($2, $3); amfree($2); amfree($3); }
97   |     SETDISK STRING NL { set_disk($2, NULL); amfree($2); }
98   |     SETDISK STRING STRING invalid_string { yyerror("Invalid argument"); amfree($2); amfree($3); }
99   |     SETDISK { yyerror("Argument required"); }
100   |     SETTAPE STRING NL { set_tape($2); amfree($2); }
101   |     SETTAPE NL { set_tape("default"); }
102   |     SETTAPE STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
103   |     SETDEVICE STRING NL { set_device(NULL, $2); amfree($2); }
104   |     SETDEVICE DASH_H STRING STRING NL { set_device($3, $4); amfree($3); amfree($4);  }
105   |     SETDEVICE NL { set_device(NULL, NULL); }
106   |     SETDEVICE STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
107   |     SETDEVICE DASH_H STRING NL { yyerror("Invalid argument"); amfree($3); }
108   |     SETDEVICE DASH_H STRING STRING invalid_string { yyerror("Invalid argument"); amfree($3); amfree($4); }
109   |     SETPROPERTY STRING property_value { set_property_name($2, 0); amfree($2); }
110   |     SETPROPERTY APPEND STRING property_value { set_property_name($3, 1); amfree($3); }
111   |     SETPROPERTY PRIORITY STRING property_value { set_property_name($3, 0); amfree($3); }
112   |     SETPROPERTY APPEND PRIORITY STRING property_value { set_property_name($4, 1); amfree($4); }
113   |     SETPROPERTY NL { yyerror("Invalid argument"); }
114   |     SETPROPERTY APPEND NL { yyerror("Invalid argument"); }
115   |     SETPROPERTY PRIORITY NL { yyerror("Invalid argument"); }
116   |     SETPROPERTY APPEND PRIORITY NL { yyerror("Invalid argument"); }
117   |     CD STRING NL { cd_glob($2, 1); amfree($2); }
118   |     CD STRING invalid_string { yyerror("Invalid argument"); }
119   |     CD NL { yyerror("Argument required"); }
120   |     CDX STRING NL { cd_regex($2, 1); amfree($2); }
121   |     CDX STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
122   |     CDX NL { yyerror("Argument required"); }
123   |     SETMODE SMB NL { set_mode(SAMBA_SMBCLIENT); }
124   |     SETMODE TAR NL { set_mode(SAMBA_TAR); }
125   |     SETMODE SMB invalid_string { yyerror("Invalid argument"); }
126   |     SETMODE TAR invalid_string { yyerror("Invalid argument"); }
127   |     SETMODE invalid_string { yyerror("Invalid argument"); }
128   |     SETMODE NL { yyerror("Argument required"); }
129   ;
130
131 setdate_command:
132         SETDATE STRING NL {
133                         time_t now;
134                         struct tm *t;
135                         int y=2000, m=0, d=1, h=0, mi=0, s=0;
136                         int ret;
137                         char *mydate = $2;
138
139                         now = time((time_t *)NULL);
140                         t = localtime(&now);
141                         if (t) {
142                             y = 1900+t->tm_year;
143                             m = t->tm_mon+1;
144                             d = t->tm_mday;
145                         }
146                         if (sscanf(mydate, "---%d", &d) == 1 ||
147                             sscanf(mydate, "--%d-%d", &m, &d) == 2 ||
148                             sscanf(mydate, "%d-%d-%d-%d-%d-%d", &y, &m, &d, &h, &mi, &s) >= 3) {
149                             if (y < 70) {
150                                 y += 2000;
151                             } else if (y < 100) {
152                                 y += 1900;
153                             }
154                             if(y < 1000 || y > 9999) {
155                                 printf("invalid year");
156                             } else if(m < 1 || m > 12) {
157                                 printf("invalid month");
158                             } else if(d < 1 || d > 31) {
159                                 printf("invalid day");
160                             } else if(h < 0 || h > 24) {
161                                 printf("invalid hour");
162                             } else if(mi < 0 || mi > 59) {
163                                 printf("invalid minute");
164                             } else if(s < 0 || s > 59) {
165                                 printf("invalid second");
166                             } else {
167                                 char result[DATE_ALLOC_SIZE];
168                                 if (h == 0 && mi == 0 && s == 0)
169                                     g_snprintf(result, DATE_ALLOC_SIZE, "%04d-%02d-%02d", y, m, d);
170                                 else
171                                     g_snprintf(result, DATE_ALLOC_SIZE, "%04d-%02d-%02d-%02d-%02d-%02d", y, m, d, h, mi, s);
172                                 set_date(result);
173                             }
174                         } else {
175                             printf("Invalid date: %s\n", mydate);
176                         }
177                      }
178   |     SETDATE NL { yyerror("Argument required"); }
179   |     SETDATE STRING invalid_string { yyerror("Invalid argument"); }
180   ;
181
182 display_command:
183         DHIST NL { list_disk_history(); }
184   |     DHIST invalid_string { yyerror("Invalid argument"); }
185   |     LS NL { list_directory(); }
186   |     LS invalid_string { yyerror("Invalid argument"); }
187   |     LIST STRING NL { display_extract_list($2); amfree($2); }
188   |     LIST NL { display_extract_list(NULL); }
189   |     LIST STRING invalid_string { yyerror("Invalid argument"); }
190   |     PWD NL { show_directory(); }
191   |     PWD invalid_string { yyerror("Invalid argument"); }
192   |     CLEAR NL { clear_extract_list(); }
193   |     CLEAR invalid_string { yyerror("Invalid argument"); }
194   |     MODE NL { show_mode (); }
195   |     MODE invalid_string { yyerror("Invalid argument"); }
196   ;
197
198 quit_command:
199         QUIT NL { quit(); }
200   |     QUIT invalid_string { yyerror("Invalid argument"); }
201   ;
202
203 add_command:
204         ADD add_path NL
205   ;
206
207 add_path:
208         add_path STRING { add_glob($2); amfree($2); }
209   |     STRING { add_glob($1); amfree($1); }
210   ;
211
212 addx_command:
213         ADDX addx_path NL
214   ;
215
216 addx_path:
217         addx_path STRING { add_regex($2); amfree($2); }
218   |     STRING { add_regex($1); amfree($1); }
219   ;
220
221 delete_command:
222         DELETE delete_path NL
223   ;
224
225 delete_path:
226         delete_path STRING { delete_glob($2); amfree($2); }
227   |     STRING { delete_glob($1); amfree($1); }
228   ;
229
230 deletex_command:
231         DELETEX deletex_path NL
232   ;
233
234 deletex_path:
235         deletex_path STRING { delete_regex($2); amfree($2); }
236   |     STRING { delete_regex($1); amfree($1); }
237   ;
238
239 local_command:
240         LPWD NL { char * buf= g_get_current_dir(); puts(buf); free(buf); }
241   |     LPWD invalid_string { yyerror("Invalid argument"); }
242   |     LCD STRING NL {
243                 local_cd($2);
244                 amfree($2);
245         }
246   |     LCD STRING invalid_string { yyerror("Invalid argument"); }
247   |     LCD NL { yyerror("Argument required"); }
248   ;
249
250 help_command:
251         HELP NL { help_list(); }
252   |     HELP invalid_string { yyerror("Invalid argument"); }
253   ;
254
255 extract_command:
256         EXTRACT NL { extract_files(); }
257   |     EXTRACT invalid_string { yyerror("Invalid argument"); }
258   ;
259
260 invalid_command:
261         STRING bogus_string {
262             char * errstr = vstralloc("Invalid command: ", $1, NULL);
263             yyerror(errstr);
264             amfree(errstr);
265             YYERROR;
266         } /* Quiets compiler warnings about unused label */
267   ;
268
269 property_value:
270         STRING property_value { add_property_value($1); amfree( $1); }
271   |     NL { ; }
272   ;
273
274 invalid_string:
275         STRING bogus_string { amfree($1); }
276   ;
277
278 bogus_string:
279         STRING bogus_string { amfree($1); }
280   |     NL { ; }
281
282 /* ADDITIONAL C CODE */
283 %%
284
285 void
286 yyerror(
287     char *      s)
288 {
289         g_printf("%s\n", s);
290 }