]> andersk Git - moira.git/blob - backup/rest_db.dc
Initial revision
[moira.git] / backup / rest_db.dc
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 <ctype.h>
19 #include <mit-copyright.h>
20 EXEC SQL INCLUDE sqlca;
21 #include "dump_db.h"
22
23
24 /*ARGSUSED*/
25 main(argc, argv)
26     int argc;
27     char **argv;
28 {
29     char buf[BUFSIZ];
30     char *prefix;
31     EXEC SQL BEGIN DECLARE SECTION;
32     char *db;
33     EXEC SQL END DECLARE SECTION;
34
35     if (argc != 2) {
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 SMS database?")) {
42         printf("I didn't think so\n");
43         exit(1);
44     }
45     sprintf(buf, "Have you initialized an empty database named %s?", db);
46     if (!yes_or_no(buf)) {
47         printf("You should have\n");
48         exit(1);
49     }
50     
51     printf("Opening database: ");
52     (void) fflush(stdout);
53     EXEC SQL CONNECT :db;
54     if (sqlca.sqlcode != 0) {
55         com_err(argv[0], 0, "Ingres database open failed");
56         exit(1);
57     }
58     EXEC SQL SET AUTOCOMMIT ON;
59
60     printf(" done\n");
61
62     printf("Prefix of backup to restore: ");
63     (void) fflush(stdout);
64     if (gets(buf) == NULL) {
65         return 1;
66     }
67     prefix = buf;
68         
69     if (!yes_or_no("Are you SURE?")) {
70         printf("I didn't think so\n");
71         exit(1);
72     }
73     do_restores(prefix);
74     printf("Restore complete\n");
75     EXEC SQL COMMIT;
76     EXEC SQL DISCONNECT;
77     exit(0);
78     /*NOTREACHED*/
79 }
80
81 yes_or_no(prompt)
82     char *prompt;
83 {
84     char buf[BUFSIZ];
85     int ret;
86     
87     int tt = open("/dev/tty", O_RDWR, 0);
88     FILE *o, *i;
89     
90     register char *cp;
91     
92     if (tt < 0) return 0;
93     
94     (void) fflush(stdout);
95     (void) fflush(stderr);
96     o = fdopen(dup(tt), "w");    
97     i = fdopen(dup(tt), "r");
98     (void) close(tt);    
99
100     for (;;) {
101         fprintf(o, "%s (yes or no): ", prompt);
102         (void) fflush(o);
103         if (fgets(buf, BUFSIZ, i) == NULL) goto err;
104         for (cp = buf; *cp; cp++) {
105             if (isupper(*cp)) *cp=tolower(*cp);
106         }
107         if (strcmp(buf, "yes\n") == 0) {
108             ret = 1; goto out;
109         }
110         if (strcmp(buf, "no\n") == 0) {
111             ret = 0; goto out;
112         }
113     }
114
115 err:
116     ret = 0;
117
118 out:
119     (void) fclose(o);
120     (void) fclose(i);
121     return ret;
122 }
123
124 int parse_int(f)
125     register FILE *f;
126 {
127     register int c;
128     register int val = 0;
129     register int sign = 1;
130     while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n') {
131         if (c == '-') sign = -1;
132         else if (isdigit(c)) {
133             val *= 10;
134             val += (c - '0');
135         } else (void) fprintf(stderr,"non-digit in numeric field\n");
136     }
137     (void) ungetc(c, f);
138     return(val * sign);
139 }
140
141 void parse_str(f, buf, len)
142     register FILE *f;
143     register char *buf;
144     register int len;           /* incl trailing NULL */
145 {
146     register int c;
147
148     while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n' && len > 0) {
149         if (c == '\\') {
150             c = getc(f);
151             if (isdigit(c)) {
152                 /* Expect three-digit octal number.. */
153                 register int c1, c2;
154                 c1 = getc(f);
155                 c2 = getc(f);
156                 if (!isdigit(c1) || !isdigit(c2)) 
157                     punt("Broken \\###");
158                 /* Convert to ASCII code: */
159                 *buf++ =  (((c-'0')<<6) + ((c1-'0')<<3) + c2-'0');
160             } else if (c == '\\' || c == SEP_CHAR) {
161                 *buf++ = c;
162                 --len;
163             } else punt ("Broken '\\'");
164         } else {
165             *buf++ = c;
166             --len;
167         }
168     }
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         *buf++ = 0;
178         (void) ungetc(c, f);
179     }
180 }
181     
182 void parse_sep(f)
183     FILE *f;
184 {
185     if (getc(f) != SEP_CHAR) punt("Expected Separator");
186 }
187 void parse_nl(f)
188     FILE *f;
189 {
190     if (getc(f) != '\n') punt("Expected newline");
191 }
192
193
194 FILE *open_file(prefix, suffix)
195     char *prefix, *suffix;
196 {
197     char name[BUFSIZ];
198     int fd;
199     FILE *f;
200     
201     EXEC SQL COMMIT WORK;
202
203     (void) strcpy(name, prefix);
204     (void) strcat(name, suffix);
205
206     fd = open(name, O_RDONLY, 0);
207     if (fd < 0) {
208         punt(name);
209     }
210     f = fdopen(fd, "r");
211     if (f == NULL) {
212         fprintf(stderr, "fdopen of ");
213         punt(name);
214     }
215     fprintf(stderr, "Working on %s\n", name);
216     return(f);
217 }
This page took 0.457378 seconds and 5 git commands to generate.