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