Add -p flag, contributed by Rob Hoelz

This commit is contained in:
Hisham Muhammad 2012-08-10 21:54:41 +00:00
parent 6c71b7ed2d
commit e6c6d7fbf7
5 changed files with 50 additions and 13 deletions

View File

@ -250,7 +250,11 @@ void Panel_draw(Panel* this, bool focus) {
int scrollH = this->scrollH; int scrollH = this->scrollH;
int y = this->y; int x = this->x; int y = this->y; int x = this->x;
int first = this->scrollV; 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) { if (this->selected < first) {
first = this->selected; first = this->selected;
this->scrollV = first; this->scrollV = first;

View File

@ -114,6 +114,7 @@ typedef struct ProcessList_ {
uid_t userId; uid_t userId;
bool filtering; bool filtering;
const char* incFilter; const char* incFilter;
Hashtable* pidWhiteList;
int cpuCount; int cpuCount;
int totalTasks; int totalTasks;
@ -180,12 +181,13 @@ const char *ProcessList_treeStrUtf8[TREE_STR_COUNT] = {
"\xe2\x94\x80", // TREE_STR_SHUT ─ "\xe2\x94\x80", // TREE_STR_SHUT ─
}; };
ProcessList* ProcessList_new(UsersTable* usersTable) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
ProcessList* this; ProcessList* this;
this = calloc(sizeof(ProcessList), 1); this = calloc(sizeof(ProcessList), 1);
this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare); this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare);
this->processTable = Hashtable_new(140, false); this->processTable = Hashtable_new(140, false);
this->usersTable = usersTable; this->usersTable = usersTable;
this->pidWhiteList = pidWhiteList;
/* tree-view auxiliary buffers */ /* tree-view auxiliary buffers */
this->processes2 = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare); 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) { static bool ProcessList_readCmdlineFile(Process* process, const char* dirname, const char* name) {
if (Process_isKernelThread(process)) if (Process_isKernelThread(process))
return true; return true;
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name); snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
@ -617,6 +620,7 @@ static bool ProcessList_readCmdlineFile(Process* process, const char* dirname, c
fclose(file); fclose(file);
free(process->comm); free(process->comm);
process->comm = strdup(command); process->comm = strdup(command);
return true; return true;
} }
@ -928,7 +932,8 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool
if ( (!p->show) if ( (!p->show)
|| (userOnly && (p->st_uid != userId)) || (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; hidden = true;
if (!hidden) { if (!hidden) {

View File

@ -97,6 +97,7 @@ typedef struct ProcessList_ {
uid_t userId; uid_t userId;
bool filtering; bool filtering;
const char* incFilter; const char* incFilter;
Hashtable* pidWhiteList;
int cpuCount; int cpuCount;
int totalTasks; int totalTasks;
@ -144,7 +145,7 @@ extern const char *ProcessList_treeStrAscii[TREE_STR_COUNT];
extern const char *ProcessList_treeStrUtf8[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); void ProcessList_delete(ProcessList* this);

View File

@ -29,12 +29,15 @@ Start htop in monochrome mode
\fB\-h \-\-help \fB\-h \-\-help
Display a help message and exit Display a help message and exit
.TP .TP
\fB\-u \-\-user=USERNAME\fR \fB\-p \-\-pid=PID,PID...\fR
Show only the processes of a given user Show only the given PIDs
.TP .TP
\fB\-s \-\-sort\-key COLUMN\fR \fB\-s \-\-sort\-key COLUMN\fR
Sort by this column (use \-\-sort\-key help for a column list) Sort by this column (use \-\-sort\-key help for a column list)
.TP .TP
\fB\-u \-\-user=USERNAME\fR
Show only the processes of a given user
.TP
\fB\-v \-\-version \fB\-v \-\-version
Output version information and exit Output version information and exit
.PP .PP

38
htop.c
View File

@ -51,11 +51,12 @@ static void printVersionFlag() {
static void printHelpFlag() { static void printHelpFlag() {
fputs("htop " VERSION " - " COPYRIGHT "\n" fputs("htop " VERSION " - " COPYRIGHT "\n"
"Released under the GNU GPL.\n\n" "Released under the GNU GPL.\n\n"
"-C --no-color Use a monochrome color scheme\n" "-C --no-color Use a monochrome color scheme\n"
"-d --delay=DELAY Set the delay between updates, in tenths of seconds\n" "-d --delay=DELAY Set the delay between updates, in tenths of seconds\n"
"-h --help Print this help screen\n" "-h --help Print this help screen\n"
"-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\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" "-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" "-v --version Print version info\n"
"\n" "\n"
"Long options may be passed with a single dash.\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; uid_t userId = 0;
int usecolors = 1; int usecolors = 1;
TreeType treeType = TREE_TYPE_AUTO; TreeType treeType = TREE_TYPE_AUTO;
char *argCopy;
char *pid;
Hashtable *pidWhiteList = NULL;
int opt, opti=0; int opt, opti=0;
static struct option long_opts[] = static struct option long_opts[] =
@ -280,6 +284,7 @@ int main(int argc, char** argv) {
{"user", required_argument, 0, 'u'}, {"user", required_argument, 0, 'u'},
{"no-color", no_argument, 0, 'C'}, {"no-color", no_argument, 0, 'C'},
{"no-colour",no_argument, 0, 'C'}, {"no-colour",no_argument, 0, 'C'},
{"pid", required_argument, 0, 'p'},
{0,0,0,0} {0,0,0,0}
}; };
int sortKey = 0; int sortKey = 0;
@ -293,7 +298,7 @@ int main(int argc, char** argv) {
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
/* Parse arguments */ /* 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; if (opt == EOF) break;
switch (opt) { switch (opt) {
case 'h': case 'h':
@ -332,6 +337,22 @@ int main(int argc, char** argv) {
break; break;
case 'C': case 'C':
usecolors=0; 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; break;
default: default:
exit(1); exit(1);
@ -356,7 +377,7 @@ int main(int argc, char** argv) {
ProcessList* pl = NULL; ProcessList* pl = NULL;
UsersTable* ut = UsersTable_new(); UsersTable* ut = UsersTable_new();
pl = ProcessList_new(ut); pl = ProcessList_new(ut, pidWhiteList);
Process_getMaxPid(); Process_getMaxPid();
Header* header = Header_new(pl); Header* header = Header_new(pl);
@ -936,5 +957,8 @@ int main(int argc, char** argv) {
((Object*)killPanel)->delete((Object*)killPanel); ((Object*)killPanel)->delete((Object*)killPanel);
UsersTable_delete(ut); UsersTable_delete(ut);
Settings_delete(settings); Settings_delete(settings);
if(pidWhiteList) {
Hashtable_delete(pidWhiteList);
}
return 0; return 0;
} }