From 58c4c3e54604f2dc4a699068066548d1805fdde3 Mon Sep 17 00:00:00 2001 From: krw Date: Thu, 12 Aug 2021 12:31:16 +0000 Subject: [PATCH] Further reduce cross-file confusion by adding USER_help() and calling it from Xhelp(). Move declaration of struct cmd to nestle next to its only instantiation (cmd_table) and use nitems() when scanning cmd_table. No functional change. --- sbin/fdisk/cmd.c | 18 ++---------------- sbin/fdisk/cmd.h | 10 +--------- sbin/fdisk/misc.h | 6 +++++- sbin/fdisk/part.c | 6 +----- sbin/fdisk/user.c | 32 +++++++++++++++++++++++++++++--- sbin/fdisk/user.h | 3 ++- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index ba6c5f742b4..af2fd613b31 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.137 2021/08/07 13:37:50 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.138 2021/08/12 12:31:16 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -482,21 +482,7 @@ Xexit(char *args, struct mbr *mbr) int Xhelp(char *args, struct mbr *mbr) { - char help[80]; - char *mbrstr; - int i; - - for (i = 0; cmd_table[i].cmd_name != NULL; i++) { - strlcpy(help, cmd_table[i].cmd_help, sizeof(help)); - if (letoh64(gh.gh_sig) == GPTSIGNATURE) { - if (cmd_table[i].cmd_gpt == 0) - continue; - mbrstr = strstr(help, "MBR"); - if (mbrstr) - memcpy(mbrstr, "GPT", 3); - } - printf("\t%s\t\t%s\n", cmd_table[i].cmd_name, help); - } + USER_help(); return CMD_CONT; } diff --git a/sbin/fdisk/cmd.h b/sbin/fdisk/cmd.h index ffa270133fa..0997ba4ac6d 100644 --- a/sbin/fdisk/cmd.h +++ b/sbin/fdisk/cmd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.h,v 1.23 2021/07/12 22:18:54 krw Exp $ */ +/* $OpenBSD: cmd.h,v 1.24 2021/08/12 12:31:16 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -25,14 +25,6 @@ #define CMD_CLEAN 0x0003 #define CMD_DIRTY 0x0004 -struct cmd { - char *cmd_name; - int cmd_gpt; - int (*cmd_fcn)(char *, struct mbr *); - char *cmd_help; -}; -extern const struct cmd cmd_table[]; - int Xreinit(char *, struct mbr *); int Xdisk(char *, struct mbr *); int Xmanual(char *, struct mbr *); diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index 9acfba50b6c..1b95e853716 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.39 2021/07/12 22:18:54 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.40 2021/08/12 12:31:16 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -27,6 +27,10 @@ struct unit_type { extern struct unit_type unit_types[]; #define SECTORS 1 +#ifndef nitems +#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) +#endif + /* Prototypes */ int unit_lookup(const char *); int string_from_line(char *, const size_t); diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c index 3deaf7b030c..e2c09945296 100644 --- a/sbin/fdisk/part.c +++ b/sbin/fdisk/part.c @@ -1,4 +1,4 @@ -/* $OpenBSD: part.c,v 1.102 2021/07/22 13:30:40 krw Exp $ */ +/* $OpenBSD: part.c,v 1.103 2021/08/12 12:31:16 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -152,10 +152,6 @@ static const struct protected_guid { { "2e54b353-1271-4842-806f-e436d6af6985" }, /* HiFive BBL */ }; -#ifndef nitems -#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) -#endif - int PRT_protected_guid(const struct uuid *uuid) { diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index 05a644eaac0..3ea302e486d 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.70 2021/08/10 13:48:34 krw Exp $ */ +/* $OpenBSD: user.c,v 1.71 2021/08/12 12:31:16 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -31,6 +31,13 @@ #include "gpt.h" #include "disk.h" +struct cmd { + char *cmd_name; + int cmd_gpt; + int (*cmd_fcn)(char *, struct mbr *); + char *cmd_help; +}; + const struct cmd cmd_table[] = { {"help", 1, Xhelp, "Command help list"}, {"manual", 1, Xmanual, "Show entire OpenBSD man page for fdisk"}, @@ -47,7 +54,6 @@ const struct cmd cmd_table[] = { {"exit", 1, Xexit, "Exit edit of current MBR, without saving changes"}, {"quit", 1, Xquit, "Quit edit of current MBR, saving current changes"}, {"abort", 1, Xabort, "Abort program without saving current changes"}, - {NULL, 0, NULL, NULL} }; @@ -86,7 +92,7 @@ again: if (cmd[0] == '\0') continue; - for (i = 0; cmd_table[i].cmd_name != NULL; i++) + for (i = 0; i < nitems(cmd_table); i++) if (strstr(cmd_table[i].cmd_name, cmd) == cmd_table[i].cmd_name) break; @@ -176,6 +182,26 @@ USER_print_disk(const int verbosity) } while (lba_self); } +void +USER_help(void) +{ + char help[80]; + char *mbrstr; + int i; + + for (i = 0; i < nitems(cmd_table); i++) { + strlcpy(help, cmd_table[i].cmd_help, sizeof(help)); + if (letoh64(gh.gh_sig) == GPTSIGNATURE) { + if (cmd_table[i].cmd_gpt == 0) + continue; + mbrstr = strstr(help, "MBR"); + if (mbrstr) + memcpy(mbrstr, "GPT", 3); + } + printf("\t%s\t\t%s\n", cmd_table[i].cmd_name, help); + } +} + void ask_cmd(char **cmd, char **arg) { diff --git a/sbin/fdisk/user.h b/sbin/fdisk/user.h index 522be670550..f560bd70633 100644 --- a/sbin/fdisk/user.h +++ b/sbin/fdisk/user.h @@ -1,4 +1,4 @@ -/* $OpenBSD: user.h,v 1.19 2021/07/13 15:03:34 krw Exp $ */ +/* $OpenBSD: user.h,v 1.20 2021/08/12 12:31:16 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -22,5 +22,6 @@ /* Prototypes */ void USER_edit(const uint64_t, const uint64_t); void USER_print_disk(const int); +void USER_help(void); #endif /* _USER_H */ -- 2.20.1