]> andersk Git - moira.git/blame - backup/rest_db.pc
eliminate use of the `register' keyword: let the compiler decide
[moira.git] / backup / rest_db.pc
CommitLineData
d77bf7b9 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>.
5eaef520 9 *
d77bf7b9 10 */
11
12#ifndef lint
13static char *rcsid_rest_db_qc = "$Header$";
14#endif lint
15
16#include <sys/file.h>
17#include <stdio.h>
3d058c8a 18#include <fcntl.h>
d77bf7b9 19#include <ctype.h>
20#include <mit-copyright.h>
3388d7f8 21EXEC SQL INCLUDE sqlca;
d77bf7b9 22#include "dump_db.h"
23
24
5eaef520 25int main(int argc, char **argv)
d77bf7b9 26{
5eaef520 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);
3388d7f8 37 }
5eaef520 38 db = argv[1];
3388d7f8 39
5eaef520 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);
d77bf7b9 44 }
5eaef520 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);
3388d7f8 50 }
d77bf7b9 51
5eaef520 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);
d77bf7b9 59 }
5eaef520 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);
d77bf7b9 72 }
5eaef520 73 do_restores(prefix);
74 printf("Restore complete\n");
75 EXEC SQL COMMIT;
76 exit(0);
77 /*NOTREACHED*/
d77bf7b9 78}
79
5eaef520 80int yes_or_no(char *prompt)
d77bf7b9 81{
5eaef520 82 char buf[BUFSIZ];
83 int ret;
84
85 int tt = open("/dev/tty", O_RDWR, 0);
86 FILE *o, *i;
87
44d12d58 88 char *cp;
5eaef520 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);
d77bf7b9 109 }
5eaef520 110 if (!strcmp(buf, "yes\n"))
111 {
112 ret = 1;
113 goto out;
d77bf7b9 114 }
5eaef520 115 if (!strcmp(buf, "no\n"))
116 {
117 ret = 0;
118 goto out;
d77bf7b9 119 }
120 }
121
122err:
5eaef520 123 ret = 0;
d77bf7b9 124
125out:
5eaef520 126 fclose(o);
127 fclose(i);
128 return ret;
d77bf7b9 129}
130
44d12d58 131int parse_int(FILE *f)
d77bf7b9 132{
44d12d58 133 int c;
134 int val = 0;
135 int sign = 1;
5eaef520 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");
d77bf7b9 146 }
5eaef520 147 ungetc(c, f);
148 return val * sign;
d77bf7b9 149}
150
44d12d58 151void parse_str(FILE *f, char *buf, int maxlen)
d77bf7b9 152{
44d12d58 153 int c, len = 0;
5eaef520 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.. */
44d12d58 163 int c1, c2;
5eaef520 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++;
d77bf7b9 184 }
185 }
5eaef520 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);
d77bf7b9 196 }
5eaef520 197 else
198 ungetc(c, f);
d77bf7b9 199}
5eaef520 200
201void parse_sep(FILE *f)
d77bf7b9 202{
5eaef520 203 if (getc(f) != SEP_CHAR)
204 punt("Expected Separator");
d77bf7b9 205}
5eaef520 206void parse_nl(FILE *f)
d77bf7b9 207{
5eaef520 208 if (getc(f) != '\n')
209 punt("Expected newline");
d77bf7b9 210}
211
212
5eaef520 213FILE *open_file(char *prefix, char *suffix)
d77bf7b9 214{
5eaef520 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);
d77bf7b9 232 }
5eaef520 233 fprintf(stderr, "Working on %s\n", name);
234 return f;
d77bf7b9 235}
This page took 0.090694 seconds and 5 git commands to generate.