3 * This program restores the Moira database from an mrbackup.
5 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
20 EXEC SQL INCLUDE sqlca;
24 int yes_or_no(char *prompt);
26 int main(int argc, char **argv)
30 EXEC SQL BEGIN DECLARE SECTION;
32 EXEC SQL END DECLARE SECTION;
36 fprintf(stderr, "Usage: %s database\n", argv[0]);
41 if (!yes_or_no("Do you *REALLY* want to wipe the moira database?"))
43 printf("I didn't think so\n");
46 sprintf(buf, "Have you initialized an empty database named %s?", db);
49 printf("You should have\n");
53 printf("Opening database: ");
55 EXEC SQL CONNECT :db IDENTIFIED BY :db;
56 if (sqlca.sqlcode != 0)
58 com_err(argv[0], 0, "Ingres database open failed");
63 printf("Prefix of backup to restore: ");
69 if (!yes_or_no("Are you SURE?"))
71 printf("I didn't think so\n");
75 printf("Restore complete\n");
81 int yes_or_no(char *prompt)
86 int tt = open("/dev/tty", O_RDWR, 0);
96 o = fdopen(dup(tt), "w");
97 i = fdopen(dup(tt), "r");
102 fprintf(o, "%s (yes or no): ", prompt);
104 if (!fgets(buf, BUFSIZ, i))
106 for (cp = buf; *cp; cp++)
111 if (!strcmp(buf, "yes\n"))
116 if (!strcmp(buf, "no\n"))
132 int parse_int(FILE *f)
137 while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n')
146 fprintf(stderr, "non-digit in numeric field\n");
152 void parse_str(FILE *f, char *buf, int maxlen)
156 while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n' && len < maxlen)
163 /* Expect three-digit octal number.. */
167 if (!isdigit(c1) || !isdigit(c2))
168 punt("Broken \\###");
169 /* Convert to ASCII code: */
170 *buf++ = ((c - '0') << 6) + ((c1 - '0') << 3) + c2 - '0';
173 else if (c == '\\' || c == SEP_CHAR)
179 punt ("Broken '\\'");
191 if (c != EOF && c != SEP_CHAR && c != '\n')
193 fprintf(stderr, "Field too wide, truncated\n");
194 while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n')
202 void parse_sep(FILE *f)
204 if (getc(f) != SEP_CHAR)
205 punt("Expected Separator");
208 void parse_nl(FILE *f)
211 punt("Expected newline");
214 FILE *open_file(char *prefix, char *suffix)
220 EXEC SQL COMMIT WORK;
222 strcpy(name, prefix);
223 strcat(name, suffix);
225 fd = open(name, O_RDONLY, 0);
231 fprintf(stderr, "fdopen of ");
234 fprintf(stderr, "Working on %s\n", name);