]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
d77bf7b9 | 2 | * |
7ac48069 | 3 | * This program restores the Moira database from an mrbackup. |
5eaef520 | 4 | * |
7ac48069 | 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>. | |
d77bf7b9 | 8 | */ |
9 | ||
7ac48069 | 10 | #include <mit-copyright.h> |
11 | #include <moira.h> | |
12 | #include "dump_db.h" | |
d77bf7b9 | 13 | |
d77bf7b9 | 14 | #include <ctype.h> |
7ac48069 | 15 | #include <fcntl.h> |
16 | #include <stdio.h> | |
17 | #include <string.h> | |
18 | #include <unistd.h> | |
19 | ||
3388d7f8 | 20 | EXEC SQL INCLUDE sqlca; |
d77bf7b9 | 21 | |
7ac48069 | 22 | RCSID("$Header$"); |
23 | ||
24 | int yes_or_no(char *prompt); | |
d77bf7b9 | 25 | |
5eaef520 | 26 | int main(int argc, char **argv) |
d77bf7b9 | 27 | { |
5eaef520 | 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); | |
3388d7f8 | 38 | } |
5eaef520 | 39 | db = argv[1]; |
3388d7f8 | 40 | |
5eaef520 | 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); | |
d77bf7b9 | 45 | } |
5eaef520 | 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); | |
3388d7f8 | 51 | } |
d77bf7b9 | 52 | |
5eaef520 | 53 | printf("Opening database: "); |
54 | fflush(stdout); | |
55 | EXEC SQL CONNECT :db IDENTIFIED BY :db; | |
56 | if (sqlca.sqlcode != 0) | |
57 | { | |
e1d0c974 | 58 | com_err(argv[0], 0, "Database open failed"); |
5eaef520 | 59 | exit(1); |
d77bf7b9 | 60 | } |
5eaef520 | 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); | |
d77bf7b9 | 73 | } |
5eaef520 | 74 | do_restores(prefix); |
75 | printf("Restore complete\n"); | |
76 | EXEC SQL COMMIT; | |
77 | exit(0); | |
78 | /*NOTREACHED*/ | |
d77bf7b9 | 79 | } |
80 | ||
5eaef520 | 81 | int yes_or_no(char *prompt) |
d77bf7b9 | 82 | { |
5eaef520 | 83 | char buf[BUFSIZ]; |
84 | int ret; | |
85 | ||
86 | int tt = open("/dev/tty", O_RDWR, 0); | |
87 | FILE *o, *i; | |
88 | ||
44d12d58 | 89 | char *cp; |
5eaef520 | 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); | |
d77bf7b9 | 110 | } |
5eaef520 | 111 | if (!strcmp(buf, "yes\n")) |
112 | { | |
113 | ret = 1; | |
114 | goto out; | |
d77bf7b9 | 115 | } |
5eaef520 | 116 | if (!strcmp(buf, "no\n")) |
117 | { | |
118 | ret = 0; | |
119 | goto out; | |
d77bf7b9 | 120 | } |
121 | } | |
122 | ||
123 | err: | |
5eaef520 | 124 | ret = 0; |
d77bf7b9 | 125 | |
126 | out: | |
5eaef520 | 127 | fclose(o); |
128 | fclose(i); | |
129 | return ret; | |
d77bf7b9 | 130 | } |
131 | ||
44d12d58 | 132 | int parse_int(FILE *f) |
d77bf7b9 | 133 | { |
44d12d58 | 134 | int c; |
135 | int val = 0; | |
136 | int sign = 1; | |
5eaef520 | 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"); | |
d77bf7b9 | 147 | } |
5eaef520 | 148 | ungetc(c, f); |
149 | return val * sign; | |
d77bf7b9 | 150 | } |
151 | ||
44d12d58 | 152 | void parse_str(FILE *f, char *buf, int maxlen) |
d77bf7b9 | 153 | { |
44d12d58 | 154 | int c, len = 0; |
5eaef520 | 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.. */ | |
44d12d58 | 164 | int c1, c2; |
5eaef520 | 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++; | |
d77bf7b9 | 185 | } |
186 | } | |
5eaef520 | 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); | |
d77bf7b9 | 197 | } |
5eaef520 | 198 | else |
199 | ungetc(c, f); | |
d77bf7b9 | 200 | } |
5eaef520 | 201 | |
202 | void parse_sep(FILE *f) | |
d77bf7b9 | 203 | { |
5eaef520 | 204 | if (getc(f) != SEP_CHAR) |
205 | punt("Expected Separator"); | |
d77bf7b9 | 206 | } |
7ac48069 | 207 | |
5eaef520 | 208 | void parse_nl(FILE *f) |
d77bf7b9 | 209 | { |
5eaef520 | 210 | if (getc(f) != '\n') |
211 | punt("Expected newline"); | |
d77bf7b9 | 212 | } |
213 | ||
5eaef520 | 214 | FILE *open_file(char *prefix, char *suffix) |
d77bf7b9 | 215 | { |
5eaef520 | 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); | |
d77bf7b9 | 233 | } |
5eaef520 | 234 | fprintf(stderr, "Working on %s\n", name); |
235 | return f; | |
d77bf7b9 | 236 | } |