- simplify getinput.

- add adjust command.
This commit is contained in:
christos 2013-01-15 23:52:48 +00:00
parent d035981822
commit b7825cc95f
1 changed files with 81 additions and 57 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $ */
/* $NetBSD: interact.c,v 1.36 2013/01/15 23:52:48 christos Exp $ */
/*
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
@ -30,7 +30,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $");
__RCSID("$NetBSD: interact.c,v 1.36 2013/01/15 23:52:48 christos Exp $");
#endif /* lint */
#include <sys/param.h>
@ -40,7 +40,9 @@ __RCSID("$NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $");
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#if HAVE_NBTOOL_CONFIG_H
#define getmaxpartitions() MAXPARTITIONS
@ -53,6 +55,7 @@ __RCSID("$NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $");
#include "extern.h"
static void cmd_help(struct disklabel *, char *, int);
static void cmd_adjust(struct disklabel *, char *, int);
static void cmd_chain(struct disklabel *, char *, int);
static void cmd_print(struct disklabel *, char *, int);
static void cmd_printall(struct disklabel *, char *, int);
@ -63,7 +66,7 @@ static void cmd_round(struct disklabel *, char *, int);
static void cmd_name(struct disklabel *, char *, int);
static void cmd_listfstypes(struct disklabel *, char *, int);
static int runcmd(struct disklabel *, char *, int);
static int getinput(const char *, const char *, const char *, char *);
static int getinput(char *, const char *, ...) __printflike(2, 3);
static int alphacmp(const void *, const void *);
static void defnum(struct disklabel *, char *, uint32_t);
static void dumpnames(const char *, const char * const *, size_t);
@ -78,6 +81,7 @@ static struct cmds {
const char *help;
} cmds[] = {
{ "?", cmd_help, "print this menu" },
{ "A", cmd_adjust, "adjust the label size to the max disk size" },
{ "C", cmd_chain, "make partitions contiguous" },
{ "E", cmd_printall, "print disk label and current partition table"},
{ "I", cmd_info, "change label information" },
@ -91,7 +95,6 @@ static struct cmds {
};
static void
cmd_help(struct disklabel *lp, char *s, int fd)
{
@ -104,14 +107,40 @@ cmd_help(struct disklabel *lp, char *s, int fd)
}
static void
cmd_adjust(struct disklabel *lp, char *s, int fd)
{
struct disklabel dl;
if (ioctl(fd, DIOCGDEFLABEL, &dl) == -1) {
warn("Cannot get default label");
return;
}
if (dl.d_secperunit != lp->d_secperunit) {
char line[BUFSIZ];
int i = getinput(line, "Adjust disklabel sector from %" PRIu32
" to %" PRIu32 " [n]? ", lp->d_secperunit, dl.d_secperunit);
if (i <= 0)
return;
if (line[0] != 'Y' && line[0] != 'y')
return;
lp->d_secperunit = dl.d_secperunit;
return;
}
printf("Already at %" PRIu32 " sectors\n", dl.d_secperunit);
return;
}
static void
cmd_chain(struct disklabel *lp, char *s, int fd)
{
int i;
char line[BUFSIZ];
i = getinput(":", "Automatically adjust partitions",
chaining ? "yes" : "no", line);
i = getinput(line, "Automatically adjust partitions [%s]? ",
chaining ? "yes" : "no");
if (i <= 0)
return;
@ -150,7 +179,6 @@ static void
cmd_info(struct disklabel *lp, char *s, int fd)
{
char line[BUFSIZ];
char def[BUFSIZ];
int v, i;
u_int32_t u;
@ -162,8 +190,7 @@ cmd_info(struct disklabel *lp, char *s, int fd)
i = lp->d_type;
if (i < 0 || i >= DKMAXTYPES)
i = 0;
snprintf(def, sizeof(def), "%s", dktypenames[i]);
i = getinput(":", "Disk type [?]", def, line);
i = getinput(line, "Disk type [%s]: ", dktypenames[i]);
if (i == -1)
return;
else if (i == 0)
@ -190,9 +217,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
}
/* d_typename */
snprintf(def, sizeof(def), "%.*s",
i = getinput(line, "Disk name [%.*s]: ",
(int) sizeof(lp->d_typename), lp->d_typename);
i = getinput(":", "Disk name", def, line);
if (i == -1)
return;
else if (i == 1)
@ -203,8 +229,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_npartitions */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu16, lp->d_npartitions);
i = getinput(":", "Number of partitions", def, line);
i = getinput(line, "Number of partitions [%" PRIu16 "]: ",
lp->d_npartitions);
if (i == -1)
return;
else if (i == 0)
@ -219,8 +245,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_secsize */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_secsize);
i = getinput(":", "Sector size (bytes)", def, line);
i = getinput(line, "Sector size (bytes) [%" PRIu32 "]: ",
lp->d_secsize);
if (i == -1)
return;
else if (i == 0)
@ -235,8 +261,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_nsectors */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_nsectors);
i = getinput(":", "Number of sectors per track", def, line);
i = getinput(line, "Number of sectors per track [%" PRIu32
"]: ", lp->d_nsectors);
if (i == -1)
return;
else if (i == 0)
@ -251,8 +277,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_ntracks */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_ntracks);
i = getinput(":", "Number of tracks per cylinder", def, line);
i = getinput(line, "Number of tracks per cylinder [%" PRIu32
"]: ", lp->d_ntracks);
if (i == -1)
return;
else if (i == 0)
@ -267,8 +293,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_secpercyl */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_secpercyl);
i = getinput(":", "Number of sectors/cylinder", def, line);
i = getinput(line, "Number of sectors/cylinder [%" PRIu32 "]: ",
lp->d_secpercyl);
if (i == -1)
return;
else if (i == 0)
@ -284,8 +310,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_ncylinders */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_ncylinders);
i = getinput(":", "Total number of cylinders", def, line);
i = getinput(line, "Total number of cylinders [%" PRIu32 "]: ",
lp->d_ncylinders);
if (i == -1)
return;
else if (i == 0)
@ -300,8 +326,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_secperunit */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_secperunit);
i = getinput(":", "Total number of sectors", def, line);
i = getinput(line, "Total number of sectors [%" PRIu32 "]: ",
lp->d_secperunit);
if (i == -1)
return;
else if (i == 0)
@ -318,8 +344,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_interleave */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu16, lp->d_interleave);
i = getinput(":", "Hardware sectors interleave", def, line);
i = getinput(line, "Hardware sectors interleave [%" PRIu16
"]: ", lp->d_interleave);
if (i == -1)
return;
else if (i == 0)
@ -334,8 +360,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_trackskew */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu16, lp->d_trackskew);
i = getinput(":", "Sector 0 skew, per track", def, line);
i = getinput(line, "Sector 0 skew, per track [%" PRIu16 "]: ",
lp->d_trackskew);
if (i == -1)
return;
else if (i == 0)
@ -350,8 +376,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_cylskew */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu16, lp->d_cylskew);
i = getinput(":", "Sector 0 skew, per cylinder", def, line);
i = getinput(line, "Sector 0 skew, per cylinder [%" PRIu16
"]: ", lp->d_cylskew);
if (i == -1)
return;
else if (i == 0)
@ -366,8 +392,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_headswitch */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_headswitch);
i = getinput(":", "Head switch time (usec)", def, line);
i = getinput(line, "Head switch time (usec) [%" PRIu32 "]: ",
lp->d_headswitch);
if (i == -1)
return;
else if (i == 0)
@ -382,8 +408,8 @@ cmd_info(struct disklabel *lp, char *s, int fd)
/* d_trkseek */
for (;;) {
snprintf(def, sizeof(def), "%" PRIu32, lp->d_trkseek);
i = getinput(":", "Track seek time (usec)", def, line);
i = getinput(line, "Track seek time (usec) [%" PRIu32 "]:",
lp->d_trkseek);
if (i == -1)
return;
else if (i == 0)
@ -402,12 +428,10 @@ static void
cmd_name(struct disklabel *lp, char *s, int fd)
{
char line[BUFSIZ];
char def[BUFSIZ];
int i;
snprintf(def, sizeof(def), "%.*s",
i = getinput(line, "Label name [%.*s]: ",
(int) sizeof(lp->d_packname), lp->d_packname);
i = getinput(":", "Label name", def, line);
if (i <= 0)
return;
(void) strncpy(lp->d_packname, line, sizeof(lp->d_packname));
@ -420,7 +444,8 @@ cmd_round(struct disklabel *lp, char *s, int fd)
int i;
char line[BUFSIZ];
i = getinput(":", "Rounding", rounding ? "cylinders" : "sectors", line);
i = getinput(line, "Rounding [%s]: ", rounding ? "cylinders" :
"sectors");
if (i <= 0)
return;
@ -461,8 +486,7 @@ cmd_part(struct disklabel *lp, char *s, int fd)
i = p->p_fstype;
if (i < 0 || i >= FSMAXTYPES)
i = 0;
snprintf(def, sizeof(def), "%s", fstypenames[i]);
i = getinput(":", "Filesystem type [?]", def, line);
i = getinput(line, "Filesystem type [%s]: ", fstypenames[i]);
if (i == -1)
return;
else if (i == 0)
@ -484,9 +508,8 @@ cmd_part(struct disklabel *lp, char *s, int fd)
}
for (;;) {
defnum(lp, def, p->p_offset);
i = getinput(":",
"Start offset ('x' to start after partition 'x')",
def, line);
i = getinput(line, "Start offset ('x' to start after partition"
" 'x') [%s]: ", def);
if (i == -1)
return;
else if (i == 0)
@ -518,8 +541,8 @@ cmd_part(struct disklabel *lp, char *s, int fd)
}
for (;;) {
defnum(lp, def, p->p_size);
i = getinput(":", "Partition size ('$' for all remaining)",
def, line);
i = getinput(line, "Partition size ('$' for all remaining) "
"[%s]: ", def);
if (i == -1)
return;
else if (i == 0)
@ -561,7 +584,7 @@ cmd_label(struct disklabel *lp, char *s, int fd)
char line[BUFSIZ];
int i;
i = getinput("?", "Label disk", "n", line);
i = getinput(line, "Label disk [n]?");
if (i <= 0 || (*line != 'y' && *line != 'Y') )
return;
@ -611,21 +634,19 @@ runcmd(struct disklabel *lp, char *line, int fd)
static int
getinput(const char *sep, const char *prompt, const char *def, char *line)
getinput(char *line, const char *prompt, ...)
{
for (;;) {
printf("%s", prompt);
if (def)
printf(" [%s]", def);
printf("%s ", sep);
va_list ap;
va_start(ap, prompt);
vprintf(prompt, ap);
va_end(ap);
if (fgets(line, BUFSIZ, stdin) == NULL)
return -1;
if (line[0] == '\n' || line[0] == '\0') {
if (def)
return 0;
}
if (line[0] == '\n' || line[0] == '\0')
return 0;
else {
char *p;
@ -780,8 +801,11 @@ interact(struct disklabel *lp, int fd)
puts("Enter '?' for help");
for (;;) {
if (getinput(">", "partition", NULL, line) == -1)
int i = getinput(line, "partition>");
if (i == -1)
return;
if (i == 0)
continue;
if (runcmd(lp, line, fd) == -1)
return;
}