diff options
author | Sahil Siddiq <icegambit91@gmail.com> | 2023-01-31 21:20:57 +0530 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2023-05-22 12:07:16 +0200 |
commit | 87db37966a8969a931fd49ebc2f9709bd27290d9 (patch) | |
tree | 9b170d4d5f4648aebf68fa43029a45ca5f9efc6f | |
parent | 3fc286257d3f40733d73722a0ea6b2ad1fd1a550 (diff) |
Implement logic for '--max-iterations'
* Add '--max-iterations' option to longopts and shortopts
* Implement termination logic
* Add iterationsRemaining to 'struct Machine'
Co-authored-by: BenBE <BenBE@geshi.org>
-rw-r--r-- | CommandLine.c | 20 | ||||
-rw-r--r-- | Machine.h | 2 | ||||
-rw-r--r-- | ScreenManager.c | 6 |
3 files changed, 26 insertions, 2 deletions
diff --git a/CommandLine.c b/CommandLine.c index 8095fa8f..0f168fb0 100644 --- a/CommandLine.c +++ b/CommandLine.c @@ -57,7 +57,8 @@ static void printHelpFlag(const char* name) { #ifdef HAVE_GETMOUSE printf("-M --no-mouse Disable the mouse\n"); #endif - printf("-p --pid=PID[,PID,PID...] Show only the given PIDs\n" + printf("-n --max-iterations=NUMBER Exit htop after NUMBER iterations/frame updates\n" + "-p --pid=PID[,PID,PID...] Show only the given PIDs\n" " --readonly Disable all system and process changing features\n" "-s --sort-key=COLUMN Sort by COLUMN in list view (try --sort-key=help for a list)\n" "-t --tree Show the tree view (can be combined with -s)\n" @@ -78,6 +79,7 @@ typedef struct CommandLineSettings_ { uid_t userId; int sortKey; int delay; + int iterationsRemaining; bool useColors; #ifdef HAVE_GETMOUSE bool enableMouse; @@ -97,6 +99,7 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin .userId = (uid_t)-1, // -1 is guaranteed to be an invalid uid_t (see setreuid(2)) .sortKey = 0, .delay = -1, + .iterationsRemaining = -1, .useColors = true, #ifdef HAVE_GETMOUSE .enableMouse = true, @@ -113,6 +116,7 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"delay", required_argument, 0, 'd'}, + {"max-iterations", required_argument, 0, 'n'}, {"sort-key", required_argument, 0, 's'}, {"user", optional_argument, 0, 'u'}, {"no-color", no_argument, 0, 'C'}, @@ -130,7 +134,7 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin int opt, opti = 0; /* Parse arguments */ - while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:F:H::", long_opts, &opti))) { + while ((opt = getopt_long(argc, argv, "hVMCs:td:n:u::Up:F:H::", long_opts, &opti))) { if (opt == EOF) break; switch (opt) { @@ -176,6 +180,17 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin return STATUS_ERROR_EXIT; } break; + case 'n': + if (sscanf(optarg, "%16d", &flags->iterationsRemaining) == 1) { + if (flags->iterationsRemaining <= 0) { + fprintf(stderr, "Error: maximum iteration count must be positive.\n"); + return STATUS_ERROR_EXIT; + } + } else { + fprintf(stderr, "Error: invalid maximum iteration count \"%s\".\n", optarg); + return STATUS_ERROR_EXIT; + } + break; case 'u': { const char* username = optarg; @@ -360,6 +375,7 @@ int CommandLine_run(int argc, char** argv) { ScreenSettings_setSortKey(settings->ss, flags.sortKey); } + host->iterationsRemaining = flags.iterationsRemaining; CRT_init(settings, flags.allowUnicode); MainPanel* panel = MainPanel_new(); @@ -46,6 +46,8 @@ typedef struct Machine_ { uint64_t realtimeMs; /* current time in milliseconds */ uint64_t monotonicMs; /* same, but from monotonic clock */ + int64_t iterationsRemaining; + #ifdef HAVE_LIBHWLOC hwloc_topology_t topology; bool topologyOk; diff --git a/ScreenManager.c b/ScreenManager.c index 18e09343..a089eda1 100644 --- a/ScreenManager.c +++ b/ScreenManager.c @@ -246,6 +246,12 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey, con if (redraw || force_redraw) { ScreenManager_drawPanels(this, focus, force_redraw); force_redraw = false; + if (this->host->iterationsRemaining != -1) { + if (!--this->host->iterationsRemaining) { + quit = true; + continue; + } + } } int prevCh = ch; |