interact.c revision 1.1 1 1.1 christos /* $NetBSD: interact.c,v 1.1 1997/03/08 23:46:12 christos 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.1 christos #ifndef lint
33 1.1 christos static char rcsid[] = "$NetBSD: interact.c,v 1.1 1997/03/08 23:46:12 christos Exp $";
34 1.1 christos #endif /* lint */
35 1.1 christos
36 1.1 christos #include <stdio.h>
37 1.1 christos #include <string.h>
38 1.1 christos #include <stdlib.h>
39 1.1 christos #include <sys/types.h>
40 1.1 christos #include <sys/param.h>
41 1.1 christos #define DKTYPENAMES
42 1.1 christos #include <sys/disklabel.h>
43 1.1 christos
44 1.1 christos #include "extern.h"
45 1.1 christos
46 1.1 christos static void cmd_help __P((struct disklabel *, char *, int));
47 1.1 christos static void cmd_print __P((struct disklabel *, char *, int));
48 1.1 christos static void cmd_part __P((struct disklabel *, char *, int));
49 1.1 christos static void cmd_label __P((struct disklabel *, char *, int));
50 1.1 christos static void cmd_round __P((struct disklabel *, char *, int));
51 1.1 christos static void cmd_name __P((struct disklabel *, char *, int));
52 1.1 christos static int runcmd __P((char *, struct disklabel *, int));
53 1.1 christos static int getinput __P((const char *, const char *, const char *, char *));
54 1.1 christos static void defnum __P((char *, struct disklabel *, int));
55 1.1 christos static int getnum __P((char *, struct disklabel *));
56 1.1 christos static void deffstypename __P((char *, int));
57 1.1 christos static int getfstypename __P((const char *));
58 1.1 christos
59 1.1 christos static int rounding = 0; /* sector rounding */
60 1.1 christos
61 1.1 christos static struct cmds {
62 1.1 christos const char *name;
63 1.1 christos void (*func) __P((struct disklabel *, char *, int));
64 1.1 christos const char *help;
65 1.1 christos } cmds[] = {
66 1.1 christos { "a", cmd_part, "define partition a" },
67 1.1 christos { "b", cmd_part, "define partition b" },
68 1.1 christos { "c", cmd_part, "define partition c" },
69 1.1 christos { "d", cmd_part, "define partition d" },
70 1.1 christos { "e", cmd_part, "define partition e" },
71 1.1 christos { "f", cmd_part, "define partition f" },
72 1.1 christos { "g", cmd_part, "define partition g" },
73 1.1 christos { "h", cmd_part, "define partition h" },
74 1.1 christos { "l", cmd_label, "write the current partition table" },
75 1.1 christos { "n", cmd_name, "name the label" },
76 1.1 christos { "p", cmd_print, "print current partition table" },
77 1.1 christos { "q", NULL, "quit" },
78 1.1 christos { "r", cmd_round, "rounding (c)ylinders (s)ectors" },
79 1.1 christos { "?", cmd_help, "print this menu" },
80 1.1 christos { NULL, NULL, NULL }
81 1.1 christos };
82 1.1 christos
83 1.1 christos
84 1.1 christos
85 1.1 christos static void
86 1.1 christos cmd_help(lp, s, fd)
87 1.1 christos struct disklabel *lp;
88 1.1 christos char *s;
89 1.1 christos int fd;
90 1.1 christos {
91 1.1 christos struct cmds *cmd;
92 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
93 1.1 christos printf("%s\t%s\n", cmd->name, cmd->help);
94 1.1 christos }
95 1.1 christos
96 1.1 christos
97 1.1 christos static void
98 1.1 christos cmd_print(lp, s, fd)
99 1.1 christos struct disklabel *lp;
100 1.1 christos char *s;
101 1.1 christos int fd;
102 1.1 christos {
103 1.1 christos display(stdout, lp);
104 1.1 christos }
105 1.1 christos
106 1.1 christos
107 1.1 christos static void
108 1.1 christos cmd_name(lp, s, fd)
109 1.1 christos struct disklabel *lp;
110 1.1 christos char *s;
111 1.1 christos int fd;
112 1.1 christos {
113 1.1 christos char line[BUFSIZ];
114 1.1 christos int i = getinput(":", "Label name", lp->d_packname, line);
115 1.1 christos
116 1.1 christos if (i <= 0)
117 1.1 christos return;
118 1.1 christos (void) strncpy(lp->d_packname, line, sizeof(lp->d_packname));
119 1.1 christos }
120 1.1 christos
121 1.1 christos
122 1.1 christos static void
123 1.1 christos cmd_round(lp, s, fd)
124 1.1 christos struct disklabel *lp;
125 1.1 christos char *s;
126 1.1 christos int fd;
127 1.1 christos {
128 1.1 christos int i;
129 1.1 christos char line[BUFSIZ];
130 1.1 christos
131 1.1 christos i = getinput(":", "Rounding", rounding ? "cylinders" : "sectors", line);
132 1.1 christos
133 1.1 christos if (i <= 0)
134 1.1 christos return;
135 1.1 christos
136 1.1 christos switch (line[0]) {
137 1.1 christos case 'c':
138 1.1 christos rounding = 1;
139 1.1 christos return;
140 1.1 christos case 's':
141 1.1 christos rounding = 0;
142 1.1 christos return;
143 1.1 christos default:
144 1.1 christos printf("Rounding can be (c)ylinders or (s)ectors\n");
145 1.1 christos return;
146 1.1 christos }
147 1.1 christos }
148 1.1 christos
149 1.1 christos
150 1.1 christos static void
151 1.1 christos cmd_part(lp, s, fd)
152 1.1 christos struct disklabel *lp;
153 1.1 christos char *s;
154 1.1 christos int fd;
155 1.1 christos {
156 1.1 christos int i;
157 1.1 christos char line[BUFSIZ];
158 1.1 christos char def[BUFSIZ];
159 1.1 christos int part = *s - 'a';
160 1.1 christos struct partition *p = &lp->d_partitions[part];
161 1.1 christos
162 1.1 christos if (part <= lp->d_npartitions)
163 1.1 christos lp->d_npartitions = part + 1;
164 1.1 christos
165 1.1 christos for (;;) {
166 1.1 christos deffstypename(def, p->p_fstype);
167 1.1 christos i = getinput(":", "Filesystem type", def, line);
168 1.1 christos if (i <= 0)
169 1.1 christos break;
170 1.1 christos if ((i = getfstypename(line)) == -1) {
171 1.1 christos printf("Invalid file system typename `%s'\n", line);
172 1.1 christos continue;
173 1.1 christos }
174 1.1 christos p->p_fstype = i;
175 1.1 christos break;
176 1.1 christos }
177 1.1 christos for (;;) {
178 1.1 christos defnum(def, lp, p->p_offset);
179 1.1 christos i = getinput(":", "Start offset", def, line);
180 1.1 christos if (i <= 0)
181 1.1 christos break;
182 1.1 christos if ((i = getnum(line, lp)) == -1) {
183 1.1 christos printf("Bad offset `%s'\n", line);
184 1.1 christos continue;
185 1.1 christos }
186 1.1 christos p->p_offset = i;
187 1.1 christos break;
188 1.1 christos }
189 1.1 christos for (;;) {
190 1.1 christos defnum(def, lp, p->p_size);
191 1.1 christos i = getinput(":", "Partition size", def, line);
192 1.1 christos if (i <= 0)
193 1.1 christos break;
194 1.1 christos if ((i = getnum(line, lp)) == -1) {
195 1.1 christos printf("Bad size `%s'\n", line);
196 1.1 christos continue;
197 1.1 christos }
198 1.1 christos p->p_size = i;
199 1.1 christos break;
200 1.1 christos }
201 1.1 christos }
202 1.1 christos
203 1.1 christos
204 1.1 christos static void
205 1.1 christos cmd_label(lp, s, fd)
206 1.1 christos struct disklabel *lp;
207 1.1 christos char *s;
208 1.1 christos int fd;
209 1.1 christos {
210 1.1 christos char line[BUFSIZ];
211 1.1 christos int i;
212 1.1 christos
213 1.1 christos i = getinput("?", "Label disk", "n", line);
214 1.1 christos
215 1.1 christos if (i <= 0 || *line != 'y')
216 1.1 christos return;
217 1.1 christos
218 1.1 christos if (checklabel(lp) != 0) {
219 1.1 christos printf("Label not written\n");
220 1.1 christos return;
221 1.1 christos }
222 1.1 christos
223 1.1 christos if ((i = writelabel(fd, bootarea, lp)) != 0) {
224 1.1 christos printf("Label not written %d\n", strerror(i));
225 1.1 christos return;
226 1.1 christos }
227 1.1 christos printf("Label written\n");
228 1.1 christos }
229 1.1 christos
230 1.1 christos
231 1.1 christos static int
232 1.1 christos runcmd(line, lp, fd)
233 1.1 christos char *line;
234 1.1 christos struct disklabel *lp;
235 1.1 christos int fd;
236 1.1 christos {
237 1.1 christos struct cmds *cmd;
238 1.1 christos
239 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
240 1.1 christos if (strncmp(line, cmd->name, strlen(cmd->name)) == 0) {
241 1.1 christos if (cmd->func == NULL)
242 1.1 christos return -1;
243 1.1 christos (*cmd->func)(lp, line, fd);
244 1.1 christos return;
245 1.1 christos }
246 1.1 christos printf("Unknown command %s\n", line);
247 1.1 christos }
248 1.1 christos
249 1.1 christos
250 1.1 christos static int
251 1.1 christos getinput(sep, prompt, def, line)
252 1.1 christos const char *sep;
253 1.1 christos const char *prompt;
254 1.1 christos const char *def;
255 1.1 christos char *line;
256 1.1 christos {
257 1.1 christos for (;;) {
258 1.1 christos printf("%s", prompt);
259 1.1 christos if (def)
260 1.1 christos printf(" [%s]", def);
261 1.1 christos printf("%s ", sep);
262 1.1 christos
263 1.1 christos if (fgets(line, BUFSIZ, stdin) == NULL)
264 1.1 christos return -1;
265 1.1 christos if (line[0] == '\n' || line[0] == '\0') {
266 1.1 christos if (def)
267 1.1 christos return 0;
268 1.1 christos }
269 1.1 christos else {
270 1.1 christos char *p;
271 1.1 christos
272 1.1 christos if ((p = strrchr(line, '\n')) != NULL)
273 1.1 christos *p = '\0';
274 1.1 christos return 1;
275 1.1 christos }
276 1.1 christos }
277 1.1 christos }
278 1.1 christos
279 1.1 christos
280 1.1 christos static void
281 1.1 christos defnum(buf, lp, size)
282 1.1 christos char *buf;
283 1.1 christos struct disklabel *lp;
284 1.1 christos int size;
285 1.1 christos {
286 1.1 christos (void) snprintf(buf, BUFSIZ, "%gc, %ds, %gM",
287 1.1 christos size / (float) lp->d_secpercyl,
288 1.1 christos size, size * (lp->d_secsize / (float) (1024 * 1024)));
289 1.1 christos }
290 1.1 christos
291 1.1 christos
292 1.1 christos static int
293 1.1 christos getnum(buf, lp)
294 1.1 christos char *buf;
295 1.1 christos struct disklabel *lp;
296 1.1 christos {
297 1.1 christos char *ep;
298 1.1 christos double d = strtod(buf, &ep);
299 1.1 christos int rv;
300 1.1 christos
301 1.1 christos if (buf == ep)
302 1.1 christos return -1;
303 1.1 christos
304 1.1 christos #define ROUND(a) ((a / lp->d_secpercyl) + \
305 1.1 christos ((a % lp->d_secpercyl) ? 1 : 0)) * lp->d_secpercyl
306 1.1 christos
307 1.1 christos switch (*ep) {
308 1.1 christos case '\0':
309 1.1 christos case 's':
310 1.1 christos rv = (int) d;
311 1.1 christos break;
312 1.1 christos
313 1.1 christos case 'c':
314 1.1 christos rv = (int) (d * lp->d_secpercyl);
315 1.1 christos break;
316 1.1 christos
317 1.1 christos case 'M':
318 1.1 christos rv = (int) (d * 1024 * 1024 / lp->d_secsize);
319 1.1 christos break;
320 1.1 christos
321 1.1 christos default:
322 1.1 christos printf("Unit error %c\n", *ep);
323 1.1 christos return -1;
324 1.1 christos }
325 1.1 christos
326 1.1 christos if (rounding)
327 1.1 christos return ROUND(rv);
328 1.1 christos else
329 1.1 christos return rv;
330 1.1 christos }
331 1.1 christos
332 1.1 christos
333 1.1 christos static void
334 1.1 christos deffstypename(buf, i)
335 1.1 christos char *buf;
336 1.1 christos int i;
337 1.1 christos {
338 1.1 christos if (i < 0 || i >= DKMAXTYPES)
339 1.1 christos i = 0;
340 1.1 christos (void) strcpy(buf, fstypenames[i]);
341 1.1 christos }
342 1.1 christos
343 1.1 christos
344 1.1 christos static int
345 1.1 christos getfstypename(buf)
346 1.1 christos const char *buf;
347 1.1 christos {
348 1.1 christos int i;
349 1.1 christos
350 1.1 christos for (i = 0; i < DKMAXTYPES; i++)
351 1.1 christos if (strcmp(buf, fstypenames[i]) == 0)
352 1.1 christos return i;
353 1.1 christos return -1;
354 1.1 christos }
355 1.1 christos
356 1.1 christos
357 1.1 christos void
358 1.1 christos interact(lp, fd)
359 1.1 christos struct disklabel *lp;
360 1.1 christos int fd;
361 1.1 christos {
362 1.1 christos char line[BUFSIZ];
363 1.1 christos
364 1.1 christos for (;;) {
365 1.1 christos if (getinput(">", "partition", NULL, line) == -1)
366 1.1 christos return;
367 1.1 christos if (runcmd(line, lp, fd) == -1)
368 1.1 christos return;
369 1.1 christos }
370 1.1 christos }
371