-/* $OpenBSD: cmd.c,v 1.139 2021/08/15 13:45:42 krw Exp $ */
+/* $OpenBSD: cmd.c,v 1.140 2021/08/24 12:34:04 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
dogpt = 0;
- if (strncasecmp(args, "gpt", 3) == 0)
+ if (strcasecmp(args, "gpt") == 0)
dogpt = 1;
- else if (strncasecmp(args, "mbr", 3) == 0)
+ else if (strcasecmp(args, "mbr") == 0)
dogpt = 0;
else if (strlen(args) > 0) {
printf("Unrecognized modifier '%s'\n", args);
struct gpt_partition gg;
to = args;
- from = strsep(&to, " \t");
+ from = strsep(&to, WHITESPACE);
pt = parsepn(to);
if (pt == -1)
int i, pn;
flag = args;
- part = strsep(&flag, " \t");
+ part = strsep(&flag, WHITESPACE);
pn = parsepn(part);
if (pn == -1)
do {
printf("%s [%d - %d]: [%d] ", str, low, high, dflt);
- string_from_line(lbuf, sizeof(lbuf));
+ string_from_line(lbuf, sizeof(lbuf), TRIMMED);
if (lbuf[0] == '\0') {
num = dflt;
do {
printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt);
printf("(? for help) ");
- string_from_line(lbuf, sizeof(lbuf));
+ string_from_line(lbuf, sizeof(lbuf), TRIMMED);
- if (lbuf[0] == '?') {
+ if (strcmp(lbuf, "?") == 0) {
PRT_printall();
continue;
}
buf[0] = '\0';
printf("%s: [%s] ", prompt, oval ? oval : "");
- string_from_line(buf, sizeof(buf));
+ string_from_line(buf, sizeof(buf), UNTRIMMED);
if (buf[0] == '\0' && oval)
strlcpy(buf, oval, sizeof(buf));
-/* $OpenBSD: misc.c,v 1.80 2021/08/15 13:45:42 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.81 2021/08/24 12:34:04 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
}
void
-string_from_line(char *buf, const size_t buflen)
+string_from_line(char *buf, const size_t buflen, const int trim)
{
static char *line;
static size_t sz;
ssize_t len;
+ unsigned int i;
len = getline(&line, &sz, stdin);
if (len == -1)
errx(1, "eof");
- line[strcspn(line, "\n")] = '\0';
-
- strlcpy(buf, line, buflen);
+ switch (trim) {
+ case UNTRIMMED:
+ line[strcspn(line, "\n")] = '\0';
+ strlcpy(buf, line, buflen);
+ break;
+ case TRIMMED:
+ for (i = strlen(line); i > 0; i--) {
+ if (isspace((unsigned char)line[i - 1]) == 0)
+ break;
+ line[i - 1] = '\0';
+ }
+ strlcpy(buf, line + strspn(line, WHITESPACE), buflen);
+ break;
+ }
}
int
do {
printf("%s [%llu - %llu]: [%llu] ", prompt, minval, maxval,
oval);
- string_from_line(buf, sizeof(buf));
+ string_from_line(buf, sizeof(buf), TRIMMED);
if (buf[0] == '\0') {
rslt = snprintf(buf, sizeof(buf), "%llu", oval);
-/* $OpenBSD: misc.h,v 1.41 2021/08/15 13:45:42 krw Exp $ */
+/* $OpenBSD: misc.h,v 1.42 2021/08/24 12:34:04 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
+#define WHITESPACE " \f\n\r\t\v"
+#define UNTRIMMED 0
+#define TRIMMED 1
+
/* Prototypes */
int unit_lookup(const char *);
-void string_from_line(char *, const size_t);
+void string_from_line(char *, const size_t, const int);
int ask_yn(const char *);
uint64_t getuint64(const char *, uint64_t, const uint64_t, const uint64_t);
char *utf16le_to_string(const uint16_t *);
-/* $OpenBSD: user.c,v 1.73 2021/08/15 13:45:42 krw Exp $ */
+/* $OpenBSD: user.c,v 1.74 2021/08/24 12:34:04 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
break;
/* Quick hack to put in '?' == 'help' */
- if (!strcmp(cmd, "?"))
+ if (strcmp(cmd, "?") == 0)
i = 0;
if (i >= nitems(cmd_table) || (letoh64(gh.gh_sig) ==
continue;
}
- st = cmd_table[i].cmd_fcn(args, &mbr);
+ st = cmd_table[i].cmd_fcn(args ? args : "", &mbr);
/* Update status */
if (st == CMD_EXIT)
ask_cmd(char **cmd, char **arg)
{
static char lbuf[100];
- size_t cmdstart, cmdend, argstart;
- string_from_line(lbuf, sizeof(lbuf));
+ string_from_line(lbuf, sizeof(lbuf), TRIMMED);
- cmdstart = strspn(lbuf, " \t");
- cmdend = cmdstart + strcspn(&lbuf[cmdstart], " \t");
- argstart = cmdend + strspn(&lbuf[cmdend], " \t");
+ *arg = lbuf;
+ *cmd = strsep(arg, WHITESPACE);
- /* *cmd and *arg may be set to point at final NUL! */
- *cmd = &lbuf[cmdstart];
- lbuf[cmdend] = '\0';
- *arg = &lbuf[argstart];
+ if (*arg != NULL)
+ *arg += strspn(*arg, WHITESPACE);
}