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