]> andersk Git - moira.git/blob - backup/rest_db.pc
1d26fb501c9ba336175f3239a811a4ad4f8e6449
[moira.git] / backup / rest_db.pc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
7  *  For copying and distribution information, please see the file
8  *  <mit-copyright.h>.
9  *
10  */
11
12 #ifndef lint
13 static char *rcsid_rest_db_qc = "$Header$";
14 #endif lint
15
16 #include <sys/file.h>
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include <ctype.h>
20 #include <mit-copyright.h>
21 EXEC SQL INCLUDE sqlca;
22 #include "dump_db.h"
23
24
25 int main(int argc, char **argv)
26 {
27   char buf[BUFSIZ];
28   char *prefix;
29   EXEC SQL BEGIN DECLARE SECTION;
30   char *db;
31   EXEC SQL END DECLARE SECTION;
32
33   if (argc != 2)
34     {
35       fprintf(stderr, "Usage: %s database\n", argv[0]);
36       exit(1);
37     }
38   db = argv[1];
39
40   if (!yes_or_no("Do you *REALLY* want to wipe the moira database?"))
41     {
42       printf("I didn't think so\n");
43       exit(1);
44     }
45   sprintf(buf, "Have you initialized an empty database named %s?", db);
46   if (!yes_or_no(buf))
47     {
48       printf("You should have\n");
49       exit(1);
50     }
51
52   printf("Opening database: ");
53   fflush(stdout);
54   EXEC SQL CONNECT :db IDENTIFIED BY :db;
55   if (sqlca.sqlcode != 0)
56     {
57       com_err(argv[0], 0, "Ingres database open failed");
58       exit(1);
59     }
60   printf(" done\n");
61
62   printf("Prefix of backup to restore: ");
63   fflush(stdout);
64   if (!gets(buf))
65     return 1;
66   prefix = buf;
67
68   if (!yes_or_no("Are you SURE?"))
69     {
70       printf("I didn't think so\n");
71       exit(1);
72     }
73   do_restores(prefix);
74   printf("Restore complete\n");
75   EXEC SQL COMMIT;
76   exit(0);
77   /*NOTREACHED*/
78 }
79
80 int yes_or_no(char *prompt)
81 {
82   char buf[BUFSIZ];
83   int ret;
84
85   int tt = open("/dev/tty", O_RDWR, 0);
86   FILE *o, *i;
87
88   register char *cp;
89
90   if (tt < 0)
91     return 0;
92
93   fflush(stdout);
94   fflush(stderr);
95   o = fdopen(dup(tt), "w");
96   i = fdopen(dup(tt), "r");
97   close(tt);
98
99   for (;;)
100     {
101       fprintf(o, "%s (yes or no): ", prompt);
102       fflush(o);
103       if (!fgets(buf, BUFSIZ, i))
104         goto err;
105       for (cp = buf; *cp; cp++)
106         {
107           if (isupper(*cp))
108             *cp = tolower(*cp);
109         }
110       if (!strcmp(buf, "yes\n"))
111         {
112           ret = 1;
113           goto out;
114         }
115       if (!strcmp(buf, "no\n"))
116         {
117           ret = 0;
118           goto out;
119         }
120     }
121
122 err:
123   ret = 0;
124
125 out:
126   fclose(o);
127   fclose(i);
128   return ret;
129 }
130
131 int parse_int(register FILE *f)
132 {
133   register int c;
134   register int val = 0;
135   register int sign = 1;
136   while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n')
137     {
138       if (c == '-')
139         sign = -1;
140       else if (isdigit(c))
141         {
142           val *= 10;
143           val += (c - '0');
144         } else
145           fprintf(stderr, "non-digit in numeric field\n");
146     }
147   ungetc(c, f);
148   return val * sign;
149 }
150
151 void parse_str(register FILE *f, register char *buf, register int maxlen)
152 {
153   register int c, len = 0;
154
155   while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n' && len < maxlen)
156     {
157       if (c == '\\')
158         {
159           c = getc(f);
160           if (isdigit(c))
161             {
162               /* Expect three-digit octal number.. */
163               register int c1, c2;
164               c1 = getc(f);
165               c2 = getc(f);
166               if (!isdigit(c1) || !isdigit(c2))
167                 punt("Broken \\###");
168               /* Convert to ASCII code: */
169               *buf++ =  ((c - '0') << 6) + ((c1 - '0') << 3) + c2 - '0';
170               len++;
171             }
172           else if (c == '\\' || c == SEP_CHAR)
173             {
174               *buf++ = c;
175               len++;
176             }
177           else
178             punt ("Broken '\\'");
179         }
180       else
181         {
182           *buf++ = c;
183           len++;
184         }
185     }
186   *buf = '\0';
187   if (c == EOF)
188     return;
189
190   if (c != EOF && c != SEP_CHAR && c != '\n')
191     {
192       fprintf(stderr, "Field too wide, truncated\n");
193       while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n')
194         ;
195       ungetc(c, f);
196     }
197   else
198     ungetc(c, f);
199 }
200
201 void parse_sep(FILE *f)
202 {
203   if (getc(f) != SEP_CHAR)
204     punt("Expected Separator");
205 }
206 void parse_nl(FILE *f)
207 {
208   if (getc(f) != '\n')
209     punt("Expected newline");
210 }
211
212
213 FILE *open_file(char *prefix, char *suffix)
214 {
215   char name[BUFSIZ];
216   int fd;
217   FILE *f;
218
219   EXEC SQL COMMIT WORK;
220
221   strcpy(name, prefix);
222   strcat(name, suffix);
223
224   fd = open(name, O_RDONLY, 0);
225   if (fd < 0)
226     punt(name);
227   f = fdopen(fd, "r");
228   if (!f)
229     {
230       fprintf(stderr, "fdopen of ");
231       punt(name);
232     }
233   fprintf(stderr, "Working on %s\n", name);
234   return f;
235 }
This page took 0.047272 seconds and 3 git commands to generate.