]> andersk Git - moira.git/blob - backup/rest_db.qc
Used /bin/sh format instead of /bin/csh format, by accident.
[moira.git] / backup / rest_db.qc
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 #include "dump_db.h"
21
22
23 /*ARGSUSED*/
24 main(argc, argv)
25     int argc;
26     char **argv;
27 {
28     char buf[BUFSIZ];
29     char *prefix;
30     
31     if (!yes_or_no("Do you *REALLY* want to wipe the SMS database?")) {
32         printf("I didn't think so\n");
33         exit(1);
34     }
35     if (!yes_or_no("Have you initialized an empty database named smstemp?")) {
36         printf("You should have\n");
37         exit(1);
38     }
39     
40     printf("Opening database: ");
41     (void) fflush(stdout);
42 ##      ingres smstemp
43
44     printf(" done\n");
45
46     printf("Prefix of backup to restore: ");
47     (void) fflush(stdout);
48     if (gets(buf) == NULL) {
49         return 1;
50     }
51     prefix = buf;
52         
53     if (!yes_or_no("Are you SURE?")) {
54         printf("I didn't think so\n");
55         exit(1);
56     }
57     do_restores(prefix);
58     printf("Restore complete\n");
59 ##      exit
60     exit(0);
61     /*NOTREACHED*/
62 }
63
64 yes_or_no(prompt)
65     char *prompt;
66 {
67     char buf[BUFSIZ];
68     int ret;
69     
70     int tt = open("/dev/tty", O_RDWR, 0);
71     FILE *o, *i;
72     
73     register char *cp;
74     
75     if (tt < 0) return 0;
76     
77     (void) fflush(stdout);
78     (void) fflush(stderr);
79     o = fdopen(dup(tt), "w");    
80     i = fdopen(dup(tt), "r");
81     (void) close(tt);    
82
83     for (;;) {
84         fprintf(o, "%s (yes or no): ", prompt);
85         (void) fflush(o);
86         if (fgets(buf, BUFSIZ, i) == NULL) goto err;
87         for (cp = buf; *cp; cp++) {
88             if (isupper(*cp)) *cp=tolower(*cp);
89         }
90         if (strcmp(buf, "yes\n") == 0) {
91             ret = 1; goto out;
92         }
93         if (strcmp(buf, "no\n") == 0) {
94             ret = 0; goto out;
95         }
96     }
97
98 err:
99     ret = 0;
100
101 out:
102     (void) fclose(o);
103     (void) fclose(i);
104     return ret;
105 }
106
107 int parse_int(f)
108     register FILE *f;
109 {
110     register int c;
111     register int val = 0;
112     register int sign = 1;
113     while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n') {
114         if (c == '-') sign = -1;
115         else if (isdigit(c)) {
116             val *= 10;
117             val += (c - '0');
118         } else (void) fprintf(stderr,"non-digit in numeric field\n");
119     }
120     (void) ungetc(c, f);
121     return(val * sign);
122 }
123
124 void parse_str(f, buf, len)
125     register FILE *f;
126     register char *buf;
127     register int len;           /* incl trailing NULL */
128 {
129     register int c;
130
131     while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n' && len > 0) {
132         if (c == '\\') {
133             c = getc(f);
134             if (isdigit(c)) {
135                 /* Expect three-digit octal number.. */
136                 register int c1, c2;
137                 c1 = getc(f);
138                 c2 = getc(f);
139                 if (!isdigit(c1) || !isdigit(c2)) 
140                     punt("Broken \\###");
141                 /* Convert to ASCII code: */
142                 *buf++ =  (((c-'0')<<6) + ((c1-'0')<<3) + c2-'0');
143             } else if (c == '\\' || c == SEP_CHAR) {
144                 *buf++ = c;
145                 --len;
146             } else punt ("Broken '\\'");
147         } else {
148             *buf++ = c;
149             --len;
150         }
151     }
152     if (c == EOF)
153         return;
154     
155     if (c != EOF && c != SEP_CHAR && c != '\n') {
156         fprintf(stderr, "Field too wide, truncated\n");
157         while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n');
158         (void) ungetc(c, f);
159     } else {
160         *buf++ = 0;
161         (void) ungetc(c, f);
162     }
163 }
164     
165 void parse_sep(f)
166     FILE *f;
167 {
168     if (getc(f) != SEP_CHAR) punt("Expected Separator");
169 }
170 void parse_nl(f)
171     FILE *f;
172 {
173     if (getc(f) != '\n') punt("Expected newline");
174 }
175
176
177 FILE *open_file(prefix, suffix)
178     char *prefix, *suffix;
179 {
180     char name[BUFSIZ];
181     int fd;
182     FILE *f;
183     
184     (void) strcpy(name, prefix);
185     (void) strcat(name, suffix);
186
187     fd = open(name, O_RDONLY, 0);
188     if (fd < 0) {
189         punt(name);
190     }
191     f = fdopen(fd, "r");
192     if (f == NULL) {
193         fprintf(stderr, "fdopen of ");
194         punt(name);
195     }
196     fprintf(stderr, "Working on %s\n", name);
197     return(f);
198 }
This page took 0.051439 seconds and 5 git commands to generate.