interact.c revision 1.8.2.1 1 1.8.2.1 he /* $NetBSD: interact.c,v 1.8.2.1 1999/09/10 22:37:10 he Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1997 Christos Zoulas. All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos * 3. All advertising materials mentioning features or use of this software
15 1.1 christos * must display the following acknowledgement:
16 1.1 christos * This product includes software developed by Christos Zoulas.
17 1.1 christos * 4. The name of the author may not be used to endorse or promote products
18 1.1 christos * derived from this software without specific prior written permission.
19 1.1 christos *
20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.5 christos #include <sys/cdefs.h>
33 1.1 christos #ifndef lint
34 1.8.2.1 he __RCSID("$NetBSD: interact.c,v 1.8.2.1 1999/09/10 22:37:10 he Exp $");
35 1.1 christos #endif /* lint */
36 1.1 christos
37 1.1 christos #include <stdio.h>
38 1.1 christos #include <string.h>
39 1.1 christos #include <stdlib.h>
40 1.2 christos #include <util.h>
41 1.1 christos #include <sys/types.h>
42 1.1 christos #include <sys/param.h>
43 1.8 christos #define FSTYPENAMES
44 1.1 christos #include <sys/disklabel.h>
45 1.1 christos
46 1.1 christos #include "extern.h"
47 1.1 christos
48 1.1 christos static void cmd_help __P((struct disklabel *, char *, int));
49 1.1 christos static void cmd_print __P((struct disklabel *, char *, int));
50 1.1 christos static void cmd_part __P((struct disklabel *, char *, int));
51 1.1 christos static void cmd_label __P((struct disklabel *, char *, int));
52 1.1 christos static void cmd_round __P((struct disklabel *, char *, int));
53 1.1 christos static void cmd_name __P((struct disklabel *, char *, int));
54 1.1 christos static int runcmd __P((char *, struct disklabel *, int));
55 1.1 christos static int getinput __P((const char *, const char *, const char *, char *));
56 1.1 christos static void defnum __P((char *, struct disklabel *, int));
57 1.1 christos static int getnum __P((char *, struct disklabel *));
58 1.1 christos static void deffstypename __P((char *, int));
59 1.1 christos static int getfstypename __P((const char *));
60 1.1 christos
61 1.1 christos static int rounding = 0; /* sector rounding */
62 1.1 christos
63 1.1 christos static struct cmds {
64 1.1 christos const char *name;
65 1.1 christos void (*func) __P((struct disklabel *, char *, int));
66 1.1 christos const char *help;
67 1.1 christos } cmds[] = {
68 1.1 christos { "?", cmd_help, "print this menu" },
69 1.2 christos { "N", cmd_name, "name the label" },
70 1.2 christos { "P", cmd_print, "print current partition table" },
71 1.2 christos { "Q", NULL, "quit" },
72 1.2 christos { "R", cmd_round, "rounding (c)ylinders (s)ectors" },
73 1.2 christos { "W", cmd_label, "write the current partition table" },
74 1.1 christos { NULL, NULL, NULL }
75 1.1 christos };
76 1.1 christos
77 1.1 christos
78 1.1 christos
79 1.1 christos static void
80 1.1 christos cmd_help(lp, s, fd)
81 1.1 christos struct disklabel *lp;
82 1.1 christos char *s;
83 1.1 christos int fd;
84 1.1 christos {
85 1.1 christos struct cmds *cmd;
86 1.2 christos
87 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
88 1.1 christos printf("%s\t%s\n", cmd->name, cmd->help);
89 1.2 christos printf("[a-%c]\tdefine named partition\n",
90 1.2 christos 'a' + getmaxpartitions() - 1);
91 1.1 christos }
92 1.1 christos
93 1.1 christos
94 1.1 christos static void
95 1.1 christos cmd_print(lp, s, fd)
96 1.1 christos struct disklabel *lp;
97 1.1 christos char *s;
98 1.1 christos int fd;
99 1.1 christos {
100 1.1 christos display(stdout, lp);
101 1.1 christos }
102 1.1 christos
103 1.1 christos
104 1.1 christos static void
105 1.1 christos cmd_name(lp, s, fd)
106 1.1 christos struct disklabel *lp;
107 1.1 christos char *s;
108 1.1 christos int fd;
109 1.1 christos {
110 1.1 christos char line[BUFSIZ];
111 1.1 christos int i = getinput(":", "Label name", lp->d_packname, line);
112 1.1 christos
113 1.1 christos if (i <= 0)
114 1.1 christos return;
115 1.1 christos (void) strncpy(lp->d_packname, line, sizeof(lp->d_packname));
116 1.1 christos }
117 1.1 christos
118 1.1 christos
119 1.1 christos static void
120 1.1 christos cmd_round(lp, s, fd)
121 1.1 christos struct disklabel *lp;
122 1.1 christos char *s;
123 1.1 christos int fd;
124 1.1 christos {
125 1.1 christos int i;
126 1.1 christos char line[BUFSIZ];
127 1.1 christos
128 1.1 christos i = getinput(":", "Rounding", rounding ? "cylinders" : "sectors", line);
129 1.1 christos
130 1.1 christos if (i <= 0)
131 1.1 christos return;
132 1.1 christos
133 1.1 christos switch (line[0]) {
134 1.1 christos case 'c':
135 1.1 christos rounding = 1;
136 1.1 christos return;
137 1.1 christos case 's':
138 1.1 christos rounding = 0;
139 1.1 christos return;
140 1.1 christos default:
141 1.1 christos printf("Rounding can be (c)ylinders or (s)ectors\n");
142 1.1 christos return;
143 1.1 christos }
144 1.1 christos }
145 1.1 christos
146 1.1 christos
147 1.1 christos static void
148 1.1 christos cmd_part(lp, s, fd)
149 1.1 christos struct disklabel *lp;
150 1.1 christos char *s;
151 1.1 christos int fd;
152 1.1 christos {
153 1.1 christos int i;
154 1.1 christos char line[BUFSIZ];
155 1.1 christos char def[BUFSIZ];
156 1.1 christos int part = *s - 'a';
157 1.1 christos struct partition *p = &lp->d_partitions[part];
158 1.1 christos
159 1.4 christos if (part >= lp->d_npartitions)
160 1.1 christos lp->d_npartitions = part + 1;
161 1.1 christos
162 1.1 christos for (;;) {
163 1.1 christos deffstypename(def, p->p_fstype);
164 1.1 christos i = getinput(":", "Filesystem type", def, line);
165 1.1 christos if (i <= 0)
166 1.1 christos break;
167 1.1 christos if ((i = getfstypename(line)) == -1) {
168 1.1 christos printf("Invalid file system typename `%s'\n", line);
169 1.1 christos continue;
170 1.1 christos }
171 1.1 christos p->p_fstype = i;
172 1.1 christos break;
173 1.1 christos }
174 1.1 christos for (;;) {
175 1.1 christos defnum(def, lp, p->p_offset);
176 1.1 christos i = getinput(":", "Start offset", def, line);
177 1.1 christos if (i <= 0)
178 1.1 christos break;
179 1.1 christos if ((i = getnum(line, lp)) == -1) {
180 1.1 christos printf("Bad offset `%s'\n", line);
181 1.1 christos continue;
182 1.1 christos }
183 1.1 christos p->p_offset = i;
184 1.1 christos break;
185 1.1 christos }
186 1.1 christos for (;;) {
187 1.1 christos defnum(def, lp, p->p_size);
188 1.1 christos i = getinput(":", "Partition size", def, line);
189 1.1 christos if (i <= 0)
190 1.1 christos break;
191 1.1 christos if ((i = getnum(line, lp)) == -1) {
192 1.1 christos printf("Bad size `%s'\n", line);
193 1.1 christos continue;
194 1.1 christos }
195 1.1 christos p->p_size = i;
196 1.1 christos break;
197 1.1 christos }
198 1.1 christos }
199 1.1 christos
200 1.1 christos
201 1.1 christos static void
202 1.1 christos cmd_label(lp, s, fd)
203 1.1 christos struct disklabel *lp;
204 1.1 christos char *s;
205 1.1 christos int fd;
206 1.1 christos {
207 1.1 christos char line[BUFSIZ];
208 1.1 christos int i;
209 1.1 christos
210 1.1 christos i = getinput("?", "Label disk", "n", line);
211 1.1 christos
212 1.8.2.1 he if (i <= 0 || (*line != 'y' && *line != 'Y') )
213 1.1 christos return;
214 1.1 christos
215 1.1 christos if (checklabel(lp) != 0) {
216 1.1 christos printf("Label not written\n");
217 1.1 christos return;
218 1.1 christos }
219 1.1 christos
220 1.6 enami if (writelabel(fd, bootarea, lp) != 0) {
221 1.6 enami printf("Label not written\n");
222 1.1 christos return;
223 1.1 christos }
224 1.1 christos printf("Label written\n");
225 1.1 christos }
226 1.1 christos
227 1.1 christos
228 1.1 christos static int
229 1.1 christos runcmd(line, lp, fd)
230 1.1 christos char *line;
231 1.1 christos struct disklabel *lp;
232 1.1 christos int fd;
233 1.1 christos {
234 1.1 christos struct cmds *cmd;
235 1.1 christos
236 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
237 1.1 christos if (strncmp(line, cmd->name, strlen(cmd->name)) == 0) {
238 1.1 christos if (cmd->func == NULL)
239 1.1 christos return -1;
240 1.1 christos (*cmd->func)(lp, line, fd);
241 1.5 christos return 0;
242 1.1 christos }
243 1.2 christos
244 1.2 christos if (line[1] == '\0' &&
245 1.2 christos line[0] >= 'a' && line[0] < 'a' + getmaxpartitions()) {
246 1.2 christos cmd_part(lp, line, fd);
247 1.5 christos return 0;
248 1.2 christos }
249 1.2 christos
250 1.1 christos printf("Unknown command %s\n", line);
251 1.5 christos return 1;
252 1.1 christos }
253 1.1 christos
254 1.1 christos
255 1.1 christos static int
256 1.1 christos getinput(sep, prompt, def, line)
257 1.1 christos const char *sep;
258 1.1 christos const char *prompt;
259 1.1 christos const char *def;
260 1.1 christos char *line;
261 1.1 christos {
262 1.1 christos for (;;) {
263 1.1 christos printf("%s", prompt);
264 1.1 christos if (def)
265 1.1 christos printf(" [%s]", def);
266 1.1 christos printf("%s ", sep);
267 1.1 christos
268 1.1 christos if (fgets(line, BUFSIZ, stdin) == NULL)
269 1.1 christos return -1;
270 1.1 christos if (line[0] == '\n' || line[0] == '\0') {
271 1.1 christos if (def)
272 1.1 christos return 0;
273 1.1 christos }
274 1.1 christos else {
275 1.1 christos char *p;
276 1.1 christos
277 1.1 christos if ((p = strrchr(line, '\n')) != NULL)
278 1.1 christos *p = '\0';
279 1.1 christos return 1;
280 1.1 christos }
281 1.1 christos }
282 1.1 christos }
283 1.1 christos
284 1.1 christos
285 1.1 christos static void
286 1.1 christos defnum(buf, lp, size)
287 1.1 christos char *buf;
288 1.1 christos struct disklabel *lp;
289 1.1 christos int size;
290 1.1 christos {
291 1.1 christos (void) snprintf(buf, BUFSIZ, "%gc, %ds, %gM",
292 1.1 christos size / (float) lp->d_secpercyl,
293 1.1 christos size, size * (lp->d_secsize / (float) (1024 * 1024)));
294 1.1 christos }
295 1.1 christos
296 1.1 christos
297 1.1 christos static int
298 1.1 christos getnum(buf, lp)
299 1.1 christos char *buf;
300 1.1 christos struct disklabel *lp;
301 1.1 christos {
302 1.1 christos char *ep;
303 1.1 christos double d = strtod(buf, &ep);
304 1.1 christos int rv;
305 1.1 christos
306 1.1 christos if (buf == ep)
307 1.1 christos return -1;
308 1.1 christos
309 1.1 christos #define ROUND(a) ((a / lp->d_secpercyl) + \
310 1.1 christos ((a % lp->d_secpercyl) ? 1 : 0)) * lp->d_secpercyl
311 1.1 christos
312 1.1 christos switch (*ep) {
313 1.1 christos case '\0':
314 1.1 christos case 's':
315 1.1 christos rv = (int) d;
316 1.1 christos break;
317 1.1 christos
318 1.1 christos case 'c':
319 1.1 christos rv = (int) (d * lp->d_secpercyl);
320 1.1 christos break;
321 1.1 christos
322 1.1 christos case 'M':
323 1.1 christos rv = (int) (d * 1024 * 1024 / lp->d_secsize);
324 1.1 christos break;
325 1.1 christos
326 1.1 christos default:
327 1.1 christos printf("Unit error %c\n", *ep);
328 1.1 christos return -1;
329 1.1 christos }
330 1.1 christos
331 1.1 christos if (rounding)
332 1.1 christos return ROUND(rv);
333 1.1 christos else
334 1.1 christos return rv;
335 1.1 christos }
336 1.1 christos
337 1.1 christos
338 1.1 christos static void
339 1.1 christos deffstypename(buf, i)
340 1.1 christos char *buf;
341 1.1 christos int i;
342 1.1 christos {
343 1.7 bouyer if (i < 0 || i >= FSMAXTYPES)
344 1.1 christos i = 0;
345 1.1 christos (void) strcpy(buf, fstypenames[i]);
346 1.1 christos }
347 1.1 christos
348 1.1 christos
349 1.1 christos static int
350 1.1 christos getfstypename(buf)
351 1.1 christos const char *buf;
352 1.1 christos {
353 1.1 christos int i;
354 1.1 christos
355 1.7 bouyer for (i = 0; i < FSMAXTYPES; i++)
356 1.1 christos if (strcmp(buf, fstypenames[i]) == 0)
357 1.1 christos return i;
358 1.1 christos return -1;
359 1.1 christos }
360 1.1 christos
361 1.1 christos
362 1.1 christos void
363 1.1 christos interact(lp, fd)
364 1.1 christos struct disklabel *lp;
365 1.1 christos int fd;
366 1.1 christos {
367 1.1 christos char line[BUFSIZ];
368 1.1 christos
369 1.1 christos for (;;) {
370 1.1 christos if (getinput(">", "partition", NULL, line) == -1)
371 1.1 christos return;
372 1.1 christos if (runcmd(line, lp, fd) == -1)
373 1.1 christos return;
374 1.1 christos }
375 1.1 christos }
376