]> andersk Git - moira.git/blame - backup/rest_db.qc
Initial revision
[moira.git] / backup / rest_db.qc
CommitLineData
dec49b94 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 *
8 * $Log$
9 * Revision 1.1 1987-07-13 03:50:02 wesommer
10 * Initial revision
11 *
12 */
13
14#ifndef lint
15static char *rcsid_rest_db_qc = "$Header$";
16#endif lint
17
18#include <sys/file.h>
19#include <stdio.h>
20#include <ctype.h>
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
154 if (c != EOF && c != ':' && c != '\n')
155 fprintf(stderr, "Field too wide, truncated\n");
156 else {
157 *buf++ = 0;
158 (void) ungetc(c, f);
159 }
160}
161
162void parse_sep(f)
163 FILE *f;
164{
165 if (getc(f) != ':') punt("Expected colon");
166}
167void parse_nl(f)
168 FILE *f;
169{
170 if (getc(f) != '\n') punt("Expected newline");
171}
172
173
174FILE *open_file(prefix, suffix)
175 char *prefix, *suffix;
176{
177 char name[BUFSIZ];
178 int fd;
179 FILE *f;
180
181 (void) strcpy(name, prefix);
182 (void) strcat(name, suffix);
183
184 fd = open(name, O_RDONLY, 0);
185 if (fd < 0) {
186 punt(name);
187 }
188 f = fdopen(fd, "r");
189 if (f == NULL) {
190 fprintf(stderr, "fdopen of ");
191 punt(name);
192 }
193 fprintf(stderr, "Working on %s\n", name);
194 return(f);
195}
196
197/*
198 * Local Variables:
199 * mode: c
200 * c-indent-level: 4
201 * c-continued-statement-offset: 4
202 * c-brace-offset: -4
203 * c-argdecl-indent: 4
204 * c-label-offset: -4
205 * End:
206 */
This page took 0.07028 seconds and 5 git commands to generate.