@@ -54,6 +54,7 @@ static void printHelpFlag(const char* name) {
5454 "-C --no-color Use a monochrome color scheme\n"
5555 "-d --delay=DELAY Set the delay between updates, in tenths of seconds\n"
5656 "-F --filter=FILTER Show only the commands matching the given filter\n"
57+ "-S --state=STATESCHARS Show only the states matching the given states\n"
5758 "-h --help Print this help screen\n"
5859 "-H --highlight-changes[=DELAY] Highlight new and old processes\n" , name );
5960#ifdef HAVE_GETMOUSE
@@ -78,6 +79,7 @@ static void printHelpFlag(const char* name) {
7879typedef struct CommandLineSettings_ {
7980 Hashtable * pidMatchList ;
8081 char * commFilter ;
82+ char * stateFilter ;
8183 uid_t userId ;
8284 int sortKey ;
8385 int delay ;
@@ -98,6 +100,7 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
98100 * flags = (CommandLineSettings ) {
99101 .pidMatchList = NULL ,
100102 .commFilter = NULL ,
103+ .stateFilter = NULL ,
101104 .userId = (uid_t )- 1 , // -1 is guaranteed to be an invalid uid_t (see setreuid(2))
102105 .sortKey = 0 ,
103106 .delay = -1 ,
@@ -128,6 +131,7 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
128131 {"tree" , no_argument , 0 , 't' },
129132 {"pid" , required_argument , 0 , 'p' },
130133 {"filter" , required_argument , 0 , 'F' },
134+ {"state" , required_argument , 0 , 'S' },
131135 {"highlight-changes" , optional_argument , 0 , 'H' },
132136 {"readonly" , no_argument , 0 , 128 },
133137 PLATFORM_LONG_OPTIONS
@@ -136,7 +140,7 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
136140
137141 int opt , opti = 0 ;
138142 /* Parse arguments */
139- while ((opt = getopt_long (argc , argv , "hVMCs:td:n:u::Up:F:H::" , long_opts , & opti ))) {
143+ while ((opt = getopt_long (argc , argv , "hVMCs:td:n:u::Up:F:H::S: " , long_opts , & opti ))) {
140144 if (opt == EOF )
141145 break ;
142146
@@ -255,6 +259,41 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
255259 }
256260 free_and_xStrdup (& flags -> commFilter , optarg );
257261 break ;
262+
263+ case 'S' :
264+ //ensures that optarg is not NULL
265+ assert (optarg );
266+
267+ // check if the argument is empty
268+ if (optarg [0 ] == '\0' ) {
269+ fprintf (stderr , "Error: state filter cannot be empty.\n" );
270+ return STATUS_ERROR_EXIT ;
271+ }
272+
273+ // for each character in optarg:
274+ // - check if it corresponds to a valid ProcessState enum value
275+ // - if any character is invalid, print an error and exit
276+ for (char * c = optarg ; * c != '\0' ; c ++ ) {
277+ int valid = 0 ;
278+
279+ // test all ProcessState enum values
280+ for (ProcessState s = UNKNOWN ; s <= SLEEPING ; s ++ ) {
281+ if (* c == processStateChar (s )) {
282+ valid = 1 ; // character is valid
283+ break ;
284+ }
285+ }
286+
287+ // if character is invalid, report error and exit
288+ if (!valid ) {
289+ fprintf (stderr , "Error: invalid state filter value \"%s\".\n" , optarg );
290+ return STATUS_ERROR_EXIT ;
291+ }
292+ }
293+
294+ free_and_xStrdup (& flags -> stateFilter , optarg );
295+ break ;
296+
258297 case 'H' : {
259298 const char * delay = optarg ;
260299 if (!delay && optind < argc && argv [optind ] != NULL &&
@@ -382,6 +421,9 @@ int CommandLine_run(int argc, char** argv) {
382421 .hideSelection = false,
383422 .hideMeters = false,
384423 };
424+ if (flags .stateFilter ) {
425+ free_and_xStrdup (& settings -> stateFilter , flags .stateFilter );
426+ }
385427
386428 MainPanel_setState (panel , & state );
387429 if (flags .commFilter )
0 commit comments