From c75c5ef9c6631127e5c0f3ace4b59e4acadd04e5 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Thu, 19 Nov 2020 12:32:07 +1100 Subject: [PATCH 1/3] Minor cleanups to platform-specific init and done Move platform-specific code out of the htop.c main function and into the platform sub-directories - primarily this is the Linux procfs path check and sensors setup/teardown; not needed on any other platforms. No functional changes here. --- darwin/Platform.c | 15 ++++++++-- darwin/Platform.h | 11 ++++--- dragonflybsd/Platform.c | 17 ++++++++--- dragonflybsd/Platform.h | 8 ++++-- freebsd/Platform.c | 17 ++++++++--- freebsd/Platform.h | 6 +++- htop.c | 63 +++++++++++++---------------------------- linux/Platform.c | 21 ++++++++++++-- linux/Platform.h | 8 ++++-- openbsd/Platform.c | 17 ++++++++--- openbsd/Platform.h | 8 ++++-- solaris/Platform.c | 17 ++++++++--- solaris/Platform.h | 8 ++++-- unsupported/Platform.c | 19 +++++++++---- unsupported/Platform.h | 8 ++++-- 15 files changed, 157 insertions(+), 86 deletions(-) diff --git a/darwin/Platform.c b/darwin/Platform.c index bf9645b3..0deca555 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -131,11 +131,20 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -void Platform_setBindings(Htop_Action* keys) { - (void) keys; +int Platform_numberOfFields = 100; + +void Platform_init(void) { + /* no platform-specific setup needed */ } -int Platform_numberOfFields = 100; +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + +void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ + (void) keys; +} int Platform_getUptime() { struct timeval bootTime, currTime; diff --git a/darwin/Platform.h b/darwin/Platform.h index 40b7d730..e1f83553 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -19,20 +19,23 @@ in the source distribution for its full text. #include "ProcessLocksScreen.h" #include "SignalsPanel.h" +extern ProcessFieldData Process_fields[]; extern ProcessField Platform_defaultFields[]; +extern int Platform_numberOfFields; + extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -extern ProcessFieldData Process_fields[]; - extern const MeterClass* const Platform_meterTypes[]; -void Platform_setBindings(Htop_Action* keys); +void Platform_init(void); -extern int Platform_numberOfFields; +void Platform_done(void); + +void Platform_setBindings(Htop_Action* keys); int Platform_getUptime(void); diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c index c6966c11..00fe93ea 100644 --- a/dragonflybsd/Platform.c +++ b/dragonflybsd/Platform.c @@ -74,10 +74,6 @@ const SignalItem Platform_signals[] = { const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); -void Platform_setBindings(Htop_Action* keys) { - (void) keys; -} - const MeterClass* const Platform_meterTypes[] = { &CPUMeter_class, &ClockMeter_class, @@ -107,6 +103,19 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; +void Platform_init(void) { + /* no platform-specific setup needed */ +} + +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + +void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ + (void) keys; +} + int Platform_getUptime() { struct timeval bootTime, currTime; int mib[2] = { CTL_KERN, KERN_BOOTTIME }; diff --git a/dragonflybsd/Platform.h b/dragonflybsd/Platform.h index 267b8f37..a05eb641 100644 --- a/dragonflybsd/Platform.h +++ b/dragonflybsd/Platform.h @@ -27,10 +27,14 @@ extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -void Platform_setBindings(Htop_Action* keys); - extern const MeterClass* const Platform_meterTypes[]; +void Platform_init(void); + +void Platform_done(void); + +void Platform_setBindings(Htop_Action* keys); + int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); diff --git a/freebsd/Platform.c b/freebsd/Platform.c index 8efa4e68..90fda93e 100644 --- a/freebsd/Platform.c +++ b/freebsd/Platform.c @@ -81,10 +81,6 @@ const SignalItem Platform_signals[] = { const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); -void Platform_setBindings(Htop_Action* keys) { - (void) keys; -} - const MeterClass* const Platform_meterTypes[] = { &CPUMeter_class, &ClockMeter_class, @@ -118,6 +114,19 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; +void Platform_init(void) { + /* no platform-specific setup needed */ +} + +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + +void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ + (void) keys; +} + int Platform_getUptime() { struct timeval bootTime, currTime; int mib[2] = { CTL_KERN, KERN_BOOTTIME }; diff --git a/freebsd/Platform.h b/freebsd/Platform.h index 5e3b29fb..64222b3d 100644 --- a/freebsd/Platform.h +++ b/freebsd/Platform.h @@ -26,9 +26,13 @@ extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; +extern const MeterClass* const Platform_meterTypes[]; + void Platform_setBindings(Htop_Action* keys); -extern const MeterClass* const Platform_meterTypes[]; +void Platform_init(void); + +void Platform_done(void); int Platform_getUptime(void); diff --git a/htop.c b/htop.c index 49b22b4b..23c2a988 100644 --- a/htop.c +++ b/htop.c @@ -35,18 +35,12 @@ in the source distribution for its full text. #include "UsersTable.h" #include "XUtils.h" - -#ifdef HAVE_LIBSENSORS -#include -#endif - - static void printVersionFlag(void) { - fputs("htop " VERSION "\n", stdout); + fputs(PACKAGE " " VERSION "\n", stdout); } static void printHelpFlag(void) { - fputs("htop " VERSION "\n" + fputs(PACKAGE " " VERSION "\n" COPYRIGHT "\n" "Released under the GNU GPLv2.\n\n" "-C --no-color Use a monochrome color scheme\n" @@ -63,8 +57,8 @@ static void printHelpFlag(void) { "-V --version Print version info\n" "\n" "Long options may be passed with a single dash.\n\n" - "Press F1 inside htop for online help.\n" - "See 'man htop' for more information.\n", + "Press F1 inside " PACKAGE " for online help.\n" + "See 'man " PACKAGE "' for more information.\n", stdout); } @@ -270,22 +264,16 @@ static void setCommFilter(State* state, char** commFilter) { int main(int argc, char** argv) { char *lc_ctype = getenv("LC_CTYPE"); - if (lc_ctype != NULL) { + if (lc_ctype != NULL) setlocale(LC_CTYPE, lc_ctype); - } else if ((lc_ctype = getenv("LC_ALL"))) { + else if ((lc_ctype = getenv("LC_ALL"))) setlocale(LC_CTYPE, lc_ctype); - } else { + else setlocale(LC_CTYPE, ""); - } - CommandLineSettings flags = parseArguments(argc, argv); // may exit() + CommandLineSettings flags = parseArguments(argc, argv); -#ifdef HTOP_LINUX - if (access(PROCDIR, R_OK) != 0) { - fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR); - exit(1); - } -#endif + Platform_init(); Process_setupColumnWidths(); @@ -299,31 +287,21 @@ int main(int argc, char** argv) { Header_populateFromSettings(header); - if (flags.delay != -1) { + if (flags.delay != -1) settings->delay = flags.delay; - } - if (!flags.useColors) { + if (!flags.useColors) settings->colorScheme = COLORSCHEME_MONOCHROME; - } - if (!flags.enableMouse) { + if (!flags.enableMouse) settings->enableMouse = false; - } - if (flags.treeView) { + if (flags.treeView) settings->treeView = true; - } - if (flags.highlightChanges) { + if (flags.highlightChanges) settings->highlightChanges = true; - } - if (flags.highlightDelaySecs != -1) { + if (flags.highlightDelaySecs != -1) settings->highlightDelaySecs = flags.highlightDelaySecs; - } CRT_init(settings->delay, settings->colorScheme, flags.allowUnicode); -#ifdef HAVE_LIBSENSORS - sensors_init(NULL); -#endif - MainPanel* panel = MainPanel_new(); ProcessList_setPanel(pl, (Panel*) panel); @@ -346,9 +324,8 @@ int main(int argc, char** argv) { }; MainPanel_setState(panel, &state); - if (flags.commFilter) { + if (flags.commFilter) setCommFilter(&state, &(flags.commFilter)); - } ScreenManager* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, &state, true); ScreenManager_add(scr, (Panel*) panel, -1); @@ -364,9 +341,7 @@ int main(int argc, char** argv) { attroff(CRT_colors[RESET_COLOR]); refresh(); -#ifdef HAVE_LIBSENSORS - sensors_cleanup(); -#endif + Platform_done(); CRT_done(); if (settings->changed) @@ -380,8 +355,8 @@ int main(int argc, char** argv) { UsersTable_delete(ut); Settings_delete(settings); - if(flags.pidMatchList) { + if (flags.pidMatchList) Hashtable_delete(flags.pidMatchList); - } + return 0; } diff --git a/linux/Platform.c b/linux/Platform.c index 7c58d3a8..b8b92381 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -56,11 +56,12 @@ in the source distribution for its full text. #include "zfs/ZfsArcStats.h" #include "zfs/ZfsCompressedArcMeter.h" +#ifdef HAVE_LIBSENSORS +#include +#endif ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, (int)M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; -//static ProcessField defaultIoFields[] = { PID, IO_PRIORITY, USER, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, COMM, 0 }; - int Platform_numberOfFields = LAST_PROCESSFIELD; const SignalItem Platform_signals[] = { @@ -107,6 +108,22 @@ static time_t Platform_Battery_cacheTime; static double Platform_Battery_cacheLevel = NAN; static ACPresence Platform_Battery_cacheIsOnAC; +void Platform_init(void) { + if (access(PROCDIR, R_OK) != 0) { + fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR); + exit(1); + } +#ifdef HAVE_LIBSENSORS + sensors_init(NULL); +#endif +} + +void Platform_done(void) { +#ifdef HAVE_LIBSENSORS + sensors_cleanup(); +#endif +} + static Htop_Reaction Platform_actionSetIOPriority(State* st) { Panel* panel = st->panel; diff --git a/linux/Platform.h b/linux/Platform.h index dbd6bb46..280b997c 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -26,10 +26,14 @@ extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -void Platform_setBindings(Htop_Action* keys); - extern const MeterClass* const Platform_meterTypes[]; +void Platform_init(void); + +void Platform_done(void); + +void Platform_setBindings(Htop_Action* keys); + int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); diff --git a/openbsd/Platform.c b/openbsd/Platform.c index bf045396..084cb536 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -89,10 +89,6 @@ const SignalItem Platform_signals[] = { const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); -void Platform_setBindings(Htop_Action* keys) { - (void) keys; -} - const MeterClass* const Platform_meterTypes[] = { &CPUMeter_class, &ClockMeter_class, @@ -122,6 +118,19 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; +void Platform_init(void) { + /* no platform-specific setup needed */ +} + +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + +void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ + (void) keys; +} + // preserved from FreeBSD port int Platform_getUptime() { struct timeval bootTime, currTime; diff --git a/openbsd/Platform.h b/openbsd/Platform.h index a624bb7b..dd5c6529 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -28,10 +28,14 @@ extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -void Platform_setBindings(Htop_Action* keys); - extern const MeterClass* const Platform_meterTypes[]; +void Platform_init(void); + +void Platform_done(void); + +void Platform_setBindings(Htop_Action* keys); + int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); diff --git a/solaris/Platform.c b/solaris/Platform.c index 7eff2d5f..d31c0a8d 100644 --- a/solaris/Platform.c +++ b/solaris/Platform.c @@ -119,14 +119,23 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -void Platform_setBindings(Htop_Action* keys) { - (void) keys; -} - int Platform_numberOfFields = LAST_PROCESSFIELD; extern char Process_pidFormat[20]; +void Platform_init(void) { + /* no platform-specific setup needed */ +} + +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + +void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ + (void) keys; +} + int Platform_getUptime() { int boot_time = 0; int curr_time = time(NULL); diff --git a/solaris/Platform.h b/solaris/Platform.h index 051a64da..0c2e44a2 100644 --- a/solaris/Platform.h +++ b/solaris/Platform.h @@ -45,12 +45,16 @@ extern ProcessField Platform_defaultFields[]; extern const MeterClass* const Platform_meterTypes[]; -void Platform_setBindings(Htop_Action* keys); - extern int Platform_numberOfFields; extern char Process_pidFormat[20]; +void Platform_init(void); + +void Platform_done(void); + +void Platform_setBindings(Htop_Action* keys); + int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); diff --git a/unsupported/Platform.c b/unsupported/Platform.c index 6646398d..793a73e3 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -88,18 +88,25 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -void Platform_setBindings(Htop_Action* keys) { - (void) keys; -} - int Platform_numberOfFields = 100; -extern char Process_pidFormat[20]; - ProcessPidColumn Process_pidColumns[] = { { .id = 0, .label = NULL }, }; +void Platform_init(void) { + /* no platform-specific setup needed */ +} + +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + +void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ + (void) keys; +} + int Platform_getUptime() { return 0; } diff --git a/unsupported/Platform.h b/unsupported/Platform.h index c75a283a..d3f5d72a 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -25,14 +25,18 @@ extern ProcessFieldData Process_fields[]; extern const MeterClass* const Platform_meterTypes[]; -void Platform_setBindings(Htop_Action* keys); - extern int Platform_numberOfFields; extern char Process_pidFormat[20]; extern ProcessPidColumn Process_pidColumns[]; +void Platform_init(void); + +void Platform_done(void); + +void Platform_setBindings(Htop_Action* keys); + int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); From 5d50f43d5fb9b791d0807bce2d701d7b43df67c1 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Thu, 19 Nov 2020 19:00:00 +1100 Subject: [PATCH 2/3] Add whitespace to improve Linux Platform_init readability --- linux/Platform.c | 1 + 1 file changed, 1 insertion(+) diff --git a/linux/Platform.c b/linux/Platform.c index b8b92381..d77ec508 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -113,6 +113,7 @@ void Platform_init(void) { fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR); exit(1); } + #ifdef HAVE_LIBSENSORS sensors_init(NULL); #endif From 82a69ee87af4aec4b5520d55c4949f287f4e6708 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Mon, 23 Nov 2020 17:32:57 +1100 Subject: [PATCH 3/3] Consistent ordering of function declarations for FreeBSD --- freebsd/Platform.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freebsd/Platform.h b/freebsd/Platform.h index 64222b3d..b493ed82 100644 --- a/freebsd/Platform.h +++ b/freebsd/Platform.h @@ -28,12 +28,12 @@ extern const unsigned int Platform_numberOfSignals; extern const MeterClass* const Platform_meterTypes[]; -void Platform_setBindings(Htop_Action* keys); - void Platform_init(void); void Platform_done(void); +void Platform_setBindings(Htop_Action* keys); + int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen);