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