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