From e6c6d7fbf7c463c7e8c877b60059c47f1841e9c0 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 10 Aug 2012 21:54:41 +0000 Subject: [PATCH] Add -p flag, contributed by Rob Hoelz --- Panel.c | 6 +++++- ProcessList.c | 9 +++++++-- ProcessList.h | 3 ++- htop.1.in | 7 +++++-- htop.c | 38 +++++++++++++++++++++++++++++++------- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Panel.c b/Panel.c index e8de2b86..d0f69bfd 100644 --- a/Panel.c +++ b/Panel.c @@ -250,7 +250,11 @@ void Panel_draw(Panel* this, bool focus) { int scrollH = this->scrollH; int y = this->y; int x = this->x; int first = this->scrollV; - int last = MIN(itemCount, this->scrollV + MIN(itemCount, this->h)); + if (itemCount > this->h && first > itemCount - this->h) { + first = itemCount - this->h; + this->scrollV = first; + } + int last = MIN(itemCount, first + MIN(itemCount, this->h)); if (this->selected < first) { first = this->selected; this->scrollV = first; diff --git a/ProcessList.c b/ProcessList.c index bbb5cffc..14a8b29c 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -114,6 +114,7 @@ typedef struct ProcessList_ { uid_t userId; bool filtering; const char* incFilter; + Hashtable* pidWhiteList; int cpuCount; int totalTasks; @@ -180,12 +181,13 @@ const char *ProcessList_treeStrUtf8[TREE_STR_COUNT] = { "\xe2\x94\x80", // TREE_STR_SHUT ─ }; -ProcessList* ProcessList_new(UsersTable* usersTable) { +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) { ProcessList* this; this = calloc(sizeof(ProcessList), 1); this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare); this->processTable = Hashtable_new(140, false); this->usersTable = usersTable; + this->pidWhiteList = pidWhiteList; /* tree-view auxiliary buffers */ this->processes2 = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare); @@ -599,6 +601,7 @@ static void ProcessList_readVServerData(Process* process, const char* dirname, c static bool ProcessList_readCmdlineFile(Process* process, const char* dirname, const char* name) { if (Process_isKernelThread(process)) return true; + char filename[MAX_NAME+1]; snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name); FILE* file = fopen(filename, "r"); @@ -617,6 +620,7 @@ static bool ProcessList_readCmdlineFile(Process* process, const char* dirname, c fclose(file); free(process->comm); process->comm = strdup(command); + return true; } @@ -928,7 +932,8 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool if ( (!p->show) || (userOnly && (p->st_uid != userId)) - || (filtering && !(String_contains_i(p->comm, incFilter))) ) + || (filtering && !(String_contains_i(p->comm, incFilter))) + || (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) ) hidden = true; if (!hidden) { diff --git a/ProcessList.h b/ProcessList.h index d7a5ef94..dc628bf1 100644 --- a/ProcessList.h +++ b/ProcessList.h @@ -97,6 +97,7 @@ typedef struct ProcessList_ { uid_t userId; bool filtering; const char* incFilter; + Hashtable* pidWhiteList; int cpuCount; int totalTasks; @@ -144,7 +145,7 @@ extern const char *ProcessList_treeStrAscii[TREE_STR_COUNT]; extern const char *ProcessList_treeStrUtf8[TREE_STR_COUNT]; -ProcessList* ProcessList_new(UsersTable* usersTable); +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList); void ProcessList_delete(ProcessList* this); diff --git a/htop.1.in b/htop.1.in index fa7682ba..bd8b9091 100644 --- a/htop.1.in +++ b/htop.1.in @@ -29,12 +29,15 @@ Start htop in monochrome mode \fB\-h \-\-help Display a help message and exit .TP -\fB\-u \-\-user=USERNAME\fR -Show only the processes of a given user +\fB\-p \-\-pid=PID,PID...\fR +Show only the given PIDs .TP \fB\-s \-\-sort\-key COLUMN\fR Sort by this column (use \-\-sort\-key help for a column list) .TP +\fB\-u \-\-user=USERNAME\fR +Show only the processes of a given user +.TP \fB\-v \-\-version Output version information and exit .PP diff --git a/htop.c b/htop.c index ba291691..c8f42e11 100644 --- a/htop.c +++ b/htop.c @@ -51,11 +51,12 @@ static void printVersionFlag() { static void printHelpFlag() { fputs("htop " VERSION " - " COPYRIGHT "\n" "Released under the GNU GPL.\n\n" - "-C --no-color Use a monochrome color scheme\n" - "-d --delay=DELAY Set the delay between updates, in tenths of seconds\n" - "-h --help Print this help screen\n" - "-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\n" - "-u --user=USERNAME Show only processes of a given user\n" + "-C --no-color Use a monochrome color scheme\n" + "-d --delay=DELAY Set the delay between updates, in tenths of seconds\n" + "-h --help Print this help screen\n" + "-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\n" + "-u --user=USERNAME Show only processes of a given user\n" + "-p --pid=PID,[,PID,PID...] Show only the given PIDs\n" "-v --version Print version info\n" "\n" "Long options may be passed with a single dash.\n\n" @@ -269,6 +270,9 @@ int main(int argc, char** argv) { uid_t userId = 0; int usecolors = 1; TreeType treeType = TREE_TYPE_AUTO; + char *argCopy; + char *pid; + Hashtable *pidWhiteList = NULL; int opt, opti=0; static struct option long_opts[] = @@ -280,6 +284,7 @@ int main(int argc, char** argv) { {"user", required_argument, 0, 'u'}, {"no-color", no_argument, 0, 'C'}, {"no-colour",no_argument, 0, 'C'}, + {"pid", required_argument, 0, 'p'}, {0,0,0,0} }; int sortKey = 0; @@ -293,7 +298,7 @@ int main(int argc, char** argv) { setlocale(LC_CTYPE, ""); /* Parse arguments */ - while ((opt = getopt_long(argc, argv, "hvCs:d:u:", long_opts, &opti))) { + while ((opt = getopt_long(argc, argv, "hvCs:d:u:p:", long_opts, &opti))) { if (opt == EOF) break; switch (opt) { case 'h': @@ -332,6 +337,22 @@ int main(int argc, char** argv) { break; case 'C': usecolors=0; + break; + case 'p': + argCopy = strdup(optarg); + pid = strtok(argCopy, ","); + + if( !pidWhiteList ) { + pidWhiteList = Hashtable_new(8, false); + } + + while( pid ) { + unsigned int num_pid = atoi(pid); + Hashtable_put(pidWhiteList, num_pid, (void *) 1); + pid = strtok(NULL, ","); + } + free(argCopy); + break; default: exit(1); @@ -356,7 +377,7 @@ int main(int argc, char** argv) { ProcessList* pl = NULL; UsersTable* ut = UsersTable_new(); - pl = ProcessList_new(ut); + pl = ProcessList_new(ut, pidWhiteList); Process_getMaxPid(); Header* header = Header_new(pl); @@ -936,5 +957,8 @@ int main(int argc, char** argv) { ((Object*)killPanel)->delete((Object*)killPanel); UsersTable_delete(ut); Settings_delete(settings); + if(pidWhiteList) { + Hashtable_delete(pidWhiteList); + } return 0; }