summaryrefslogtreecommitdiffstats
path: root/CommandLine.c
diff options
context:
space:
mode:
authorSahil Siddiq <icegambit91@gmail.com>2023-01-31 21:20:57 +0530
committerBenBE <BenBE@geshi.org>2023-05-22 12:07:16 +0200
commit87db37966a8969a931fd49ebc2f9709bd27290d9 (patch)
tree9b170d4d5f4648aebf68fa43029a45ca5f9efc6f /CommandLine.c
parent3fc286257d3f40733d73722a0ea6b2ad1fd1a550 (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>
Diffstat (limited to 'CommandLine.c')
-rw-r--r--CommandLine.c20
1 files changed, 18 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();

© 2014-2024 Faster IT GmbH | imprint | privacy policy