From 253ff23f9e3a26c9575758fd1849b457c5d4d635 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Mon, 22 Mar 2021 14:49:07 +1100 Subject: [PATCH] Use a platform-specific routine for long option usage Related to https://github.com/htop-dev/htop/pull/564 --- darwin/Platform.h | 7 ++++--- dragonflybsd/Platform.h | 7 ++++--- freebsd/Platform.h | 7 ++++--- htop.c | 9 ++++----- linux/Platform.c | 14 ++++++++++++++ linux/Platform.h | 22 +++++++++------------- openbsd/Platform.h | 7 ++++--- solaris/Platform.h | 7 ++++--- unsupported/Platform.h | 7 ++++--- 9 files changed, 51 insertions(+), 36 deletions(-) diff --git a/darwin/Platform.h b/darwin/Platform.h index af95db9c..7bce9b35 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -23,9 +23,6 @@ in the source distribution for its full text. #include "generic/uname.h" -#define PLATFORM_LONG_OPTIONS -#define PLATFORM_LONG_OPTIONS_USAGE - extern const ProcessField Platform_defaultFields[]; extern double Platform_timebaseToNS; @@ -80,6 +77,10 @@ static inline void Platform_getRelease(char** string) { *string = Generic_uname(); } +#define PLATFORM_LONG_OPTIONS + +static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { } + static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) { return false; } diff --git a/dragonflybsd/Platform.h b/dragonflybsd/Platform.h index 9ddcbc70..993768be 100644 --- a/dragonflybsd/Platform.h +++ b/dragonflybsd/Platform.h @@ -21,9 +21,6 @@ in the source distribution for its full text. #include "generic/uname.h" -#define PLATFORM_LONG_OPTIONS -#define PLATFORM_LONG_OPTIONS_USAGE - extern const ProcessField Platform_defaultFields[]; extern const SignalItem Platform_signals[]; @@ -70,6 +67,10 @@ static inline void Platform_getRelease(char** string) { *string = Generic_uname(); } +#define PLATFORM_LONG_OPTIONS + +static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { } + static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) { return false; } diff --git a/freebsd/Platform.h b/freebsd/Platform.h index 678579e3..884b7634 100644 --- a/freebsd/Platform.h +++ b/freebsd/Platform.h @@ -22,9 +22,6 @@ in the source distribution for its full text. #include "generic/uname.h" -#define PLATFORM_LONG_OPTIONS -#define PLATFORM_LONG_OPTIONS_USAGE - extern const ProcessField Platform_defaultFields[]; extern const SignalItem Platform_signals[]; @@ -75,6 +72,10 @@ static inline void Platform_getRelease(char** string) { *string = Generic_uname(); } +#define PLATFORM_LONG_OPTIONS + +static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { } + static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) { return false; } diff --git a/htop.c b/htop.c index 15eb2a4b..656aa086 100644 --- a/htop.c +++ b/htop.c @@ -48,7 +48,6 @@ static void printHelpFlag(const char* name) { "-d --delay=DELAY Set the delay between updates, in tenths of seconds\n" "-F --filter=FILTER Show only the commands matching the given filter\n" "-h --help Print this help screen\n" - PLATFORM_LONG_OPTIONS_USAGE "-H --highlight-changes[=DELAY] Highlight new and old processes\n" "-M --no-mouse Disable the mouse\n" "-p --pid=PID[,PID,PID...] Show only the given PIDs\n" @@ -56,12 +55,12 @@ static void printHelpFlag(const char* name) { "-t --tree Show the tree view (can be combined with -s)\n" "-u --user[=USERNAME] Show only processes for a given user (or $USER)\n" "-U --no-unicode Do not use unicode but plain ASCII\n" - "-V --version Print version info\n" - "\n" + "-V --version Print version info\n", name); + Platform_longOptionsUsage(name); + printf("\n" "Long options may be passed with a single dash.\n\n" "Press F1 inside %s for online help.\n" - "See 'man %s' for more information.\n", - name, name, name); + "See 'man %s' for more information.\n", name, name); } // ---------------------------------------- diff --git a/linux/Platform.c b/linux/Platform.c index a6885254..13c0ddb2 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -846,6 +846,20 @@ void Platform_getBattery(double* percent, ACPresence* isOnAC) { Platform_Battery_cacheTime = now; } +void Platform_longOptionsUsage(const char* name) +{ +#ifdef HAVE_LIBCAP + printf( +" --drop-capabilities[=none|basic|strict] Drop Linux capabilities when running as root\n" +" none - do not drop any capabilities\n" +" basic (default) - drop all capabilities not needed by %s\n" +" strict - drop all capabilities except those needed for\n" +" core functionality\n", name); +#else + (void) name; +#endif +} + bool Platform_getLongOption(int opt, int argc, char** argv) { #ifndef HAVE_LIBCAP (void) argc; diff --git a/linux/Platform.h b/linux/Platform.h index 30544997..18d303d1 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -27,19 +27,6 @@ in the source distribution for its full text. #define PATH_MAX 4096 #endif -#ifdef HAVE_LIBCAP - #define PLATFORM_LONG_OPTIONS \ - {"drop-capabilities", optional_argument, 0, 128}, - #define PLATFORM_LONG_OPTIONS_USAGE \ - " --drop-capabilities[=none|basic|strict] Drop Linux capabilities when running as root\n" \ - " none - do not drop any capabilities\n" \ - " basic (default) - drop all capabilities not needed by htop\n" \ - " strict - drop all capabilities except those needed for core functionality\n" -#else - #define PLATFORM_LONG_OPTIONS - #define PLATFORM_LONG_OPTIONS_USAGE -#endif - extern const ProcessField Platform_defaultFields[]; @@ -95,6 +82,15 @@ static inline void Platform_getRelease(char** string) { *string = Generic_uname(); } +#ifdef HAVE_LIBCAP + #define PLATFORM_LONG_OPTIONS \ + {"drop-capabilities", optional_argument, 0, 128}, +#else + #define PLATFORM_LONG_OPTIONS +#endif + +void Platform_longOptionsUsage(const char* name); + bool Platform_getLongOption(int opt, int argc, char** argv); #endif diff --git a/openbsd/Platform.h b/openbsd/Platform.h index c4add09f..f54b0509 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -23,9 +23,6 @@ in the source distribution for its full text. #include "generic/uname.h" -#define PLATFORM_LONG_OPTIONS -#define PLATFORM_LONG_OPTIONS_USAGE - extern const ProcessField Platform_defaultFields[]; /* see /usr/include/sys/signal.h */ @@ -73,6 +70,10 @@ static inline void Platform_getRelease(char** string) { *string = Generic_uname(); } +#define PLATFORM_LONG_OPTIONS + +static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { } + static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) { return false; } diff --git a/solaris/Platform.h b/solaris/Platform.h index 4485cbbf..9f479d1b 100644 --- a/solaris/Platform.h +++ b/solaris/Platform.h @@ -26,9 +26,6 @@ in the source distribution for its full text. #include "generic/uname.h" -#define PLATFORM_LONG_OPTIONS -#define PLATFORM_LONG_OPTIONS_USAGE - #define kill(pid, signal) kill(pid / 1024, signal) typedef struct var kvar_t; @@ -92,6 +89,10 @@ static inline void Platform_getRelease(char** string) { *string = Generic_uname(); } +#define PLATFORM_LONG_OPTIONS + +static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { } + static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) { return false; } diff --git a/unsupported/Platform.h b/unsupported/Platform.h index 8df948ef..2331e278 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -17,9 +17,6 @@ in the source distribution for its full text. #include "UnsupportedProcess.h" -#define PLATFORM_LONG_OPTIONS -#define PLATFORM_LONG_OPTIONS_USAGE - extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; @@ -64,6 +61,10 @@ void Platform_getHostname(char* buffer, size_t size); void Platform_getRelease(char** string); +#define PLATFORM_LONG_OPTIONS + +static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { } + static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) { return false; }