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