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