]> andersk Git - moira.git/blob - backup/rest_db.pc
initial import of gdss from the Athena source tree
[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 /*ARGSUSED*/
26 main(argc, argv)
27     int argc;
28     char **argv;
29 {
30     char buf[BUFSIZ];
31     char *prefix;
32     EXEC SQL BEGIN DECLARE SECTION;
33     char *db;
34     EXEC SQL END DECLARE SECTION;
35
36     if (argc != 2) {
37         fprintf(stderr, "Usage: %s database\n", argv[0]);
38         exit(1);
39     }
40     db = argv[1];
41
42     if (!yes_or_no("Do you *REALLY* want to wipe the moira database?")) {
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         printf("You should have\n");
49         exit(1);
50     }
51     
52     printf("Opening database: ");
53     (void) fflush(stdout);
54     EXEC SQL CONNECT :db IDENTIFIED BY :db;
55     if (sqlca.sqlcode != 0) {
56         com_err(argv[0], 0, "Ingres database open failed");
57         exit(1);
58     }
59     printf(" done\n");
60
61     printf("Prefix of backup to restore: ");
62     (void) fflush(stdout);
63     if (gets(buf) == NULL) {
64         return 1;
65     }
66     prefix = buf;
67         
68     if (!yes_or_no("Are you SURE?")) {
69         printf("I didn't think so\n");
70         exit(1);
71     }
72     do_restores(prefix);
73     printf("Restore complete\n");
74     EXEC SQL COMMIT;
75     exit(0);
76     /*NOTREACHED*/
77 }
78
79 yes_or_no(prompt)
80     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) return 0;
91     
92     (void) fflush(stdout);
93     (void) fflush(stderr);
94     o = fdopen(dup(tt), "w");    
95     i = fdopen(dup(tt), "r");
96     (void) close(tt);    
97
98     for (;;) {
99         fprintf(o, "%s (yes or no): ", prompt);
100         (void) fflush(o);
101         if (fgets(buf, BUFSIZ, i) == NULL) goto err;
102         for (cp = buf; *cp; cp++) {
103             if (isupper(*cp)) *cp=tolower(*cp);
104         }
105         if (strcmp(buf, "yes\n") == 0) {
106             ret = 1; goto out;
107         }
108         if (strcmp(buf, "no\n") == 0) {
109             ret = 0; goto out;
110         }
111     }
112
113 err:
114     ret = 0;
115
116 out:
117     (void) fclose(o);
118     (void) fclose(i);
119     return ret;
120 }
121
122 int parse_int(f)
123     register FILE *f;
124 {
125     register int c;
126     register int val = 0;
127     register int sign = 1;
128     while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n') {
129         if (c == '-') sign = -1;
130         else if (isdigit(c)) {
131             val *= 10;
132             val += (c - '0');
133         } else (void) fprintf(stderr,"non-digit in numeric field\n");
134     }
135     (void) ungetc(c, f);
136     return(val * sign);
137 }
138
139 void parse_str(f, buf, maxlen)
140     register FILE *f;
141     register char *buf;
142     register int maxlen;
143 {
144     register int c, len=0;
145
146     while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n' && len < maxlen) {
147         if (c == '\\') {
148             c = getc(f);
149             if (isdigit(c)) {
150                 /* Expect three-digit octal number.. */
151                 register int c1, c2;
152                 c1 = getc(f);
153                 c2 = getc(f);
154                 if (!isdigit(c1) || !isdigit(c2)) 
155                     punt("Broken \\###");
156                 /* Convert to ASCII code: */
157                 *buf++ =  (((c-'0')<<6) + ((c1-'0')<<3) + c2-'0');
158                 len++;
159             } else if (c == '\\' || c == SEP_CHAR) {
160                 *buf++ = c;
161                 len++;
162             } else punt ("Broken '\\'");
163         } else {
164             *buf++ = c;
165             len++;
166         }
167     }
168     *buf='\0';
169     if (c == EOF)
170         return;
171     
172     if (c != EOF && c != SEP_CHAR && c != '\n') {
173         fprintf(stderr, "Field too wide, truncated\n");
174         while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n');
175         (void) ungetc(c, f);
176     } else {
177         (void) ungetc(c, f);
178     }
179 }
180     
181 void parse_sep(f)
182     FILE *f;
183 {
184     if (getc(f) != SEP_CHAR) punt("Expected Separator");
185 }
186 void parse_nl(f)
187     FILE *f;
188 {
189     if (getc(f) != '\n') punt("Expected newline");
190 }
191
192
193 FILE *open_file(prefix, suffix)
194     char *prefix, *suffix;
195 {
196     char name[BUFSIZ];
197     int fd;
198     FILE *f;
199     
200     EXEC SQL COMMIT WORK;
201
202     (void) strcpy(name, prefix);
203     (void) strcat(name, suffix);
204
205     fd = open(name, O_RDONLY, 0);
206     if (fd < 0) {
207         punt(name);
208     }
209     f = fdopen(fd, "r");
210     if (f == NULL) {
211         fprintf(stderr, "fdopen of ");
212         punt(name);
213     }
214     fprintf(stderr, "Working on %s\n", name);
215     return(f);
216 }
This page took 0.050493 seconds and 5 git commands to generate.