htop/htop.c

265 lines
7.2 KiB
C
Raw Normal View History

2006-03-04 18:16:49 +00:00
/*
htop - htop.c
(C) 2004-2011 Hisham H. Muhammad
2006-03-04 18:16:49 +00:00
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h"
2011-12-26 21:35:57 +00:00
2006-03-04 18:16:49 +00:00
#include "FunctionBar.h"
#include "Hashtable.h"
2011-12-26 21:35:57 +00:00
#include "ColumnsPanel.h"
#include "CRT.h"
#include "MainPanel.h"
#include "ProcessList.h"
#include "ScreenManager.h"
#include "Settings.h"
#include "UsersTable.h"
#include "Platform.h"
2006-03-04 18:16:49 +00:00
2011-12-26 21:35:57 +00:00
#include <getopt.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
2011-12-26 21:35:57 +00:00
#include <string.h>
2014-04-22 21:19:24 +00:00
#include <time.h>
#include <unistd.h>
2011-12-26 21:35:57 +00:00
2006-03-04 18:16:49 +00:00
//#link m
static void printVersionFlag() {
fputs("htop " VERSION " - " COPYRIGHT "\n"
"Released under the GNU GPL.\n\n",
stdout);
2006-03-04 18:16:49 +00:00
exit(0);
}
static void printHelpFlag() {
fputs("htop " VERSION " - " COPYRIGHT "\n"
"Released under the GNU GPL.\n\n"
2012-08-10 21:54:41 +00:00
"-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"
2018-04-05 22:45:55 +00:00
"-t --tree Show the tree view by default\n"
2012-08-10 21:54:41 +00:00
"-u --user=USERNAME Show only processes of a given user\n"
"-p --pid=PID,[,PID,PID...] Show only the given PIDs\n"
2016-01-04 15:02:29 +00:00
"-v --version Print version info\n"
"\n"
"Long options may be passed with a single dash.\n\n"
"Press F1 inside htop for online help.\n"
"See 'man htop' for more information.\n",
stdout);
2006-03-04 18:16:49 +00:00
exit(0);
}
// ----------------------------------------
typedef struct CommandLineSettings_ {
Hashtable* pidWhiteList;
uid_t userId;
int sortKey;
int delay;
bool useColors;
2018-04-05 22:45:55 +00:00
bool treeView;
} CommandLineSettings;
static CommandLineSettings parseArguments(int argc, char** argv) {
CommandLineSettings flags = {
.pidWhiteList = NULL,
.userId = -1, // -1 is guaranteed to be an invalid uid_t (see setreuid(2))
.sortKey = 0,
.delay = -1,
.useColors = true,
2018-04-05 22:45:55 +00:00
.treeView = false,
};
static struct option long_opts[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"delay", required_argument, 0, 'd'},
{"sort-key", required_argument, 0, 's'},
{"user", required_argument, 0, 'u'},
{"no-color", no_argument, 0, 'C'},
{"no-colour",no_argument, 0, 'C'},
2018-04-05 22:45:55 +00:00
{"tree", no_argument, 0, 't'},
2012-08-10 21:54:41 +00:00
{"pid", required_argument, 0, 'p'},
{0,0,0,0}
};
int opt, opti=0;
/* Parse arguments */
while ((opt = getopt_long(argc, argv, "hvCs:td:u:p:", long_opts, &opti))) {
if (opt == EOF) break;
switch (opt) {
case 'h':
printHelpFlag();
break;
case 'v':
printVersionFlag();
break;
case 's':
2011-09-08 04:47:48 +00:00
if (strcmp(optarg, "help") == 0) {
for (int j = 1; j < Platform_numberOfFields; j++) {
const char* name = Process_fields[j].name;
if (name) printf ("%s\n", name);
}
exit(0);
}
flags.sortKey = ColumnsPanel_fieldNameToIndex(optarg);
if (flags.sortKey == -1) {
2015-10-05 14:27:44 +00:00
fprintf(stderr, "Error: invalid column \"%s\".\n", optarg);
exit(1);
2015-09-19 16:21:22 +00:00
}
break;
case 'd':
if (sscanf(optarg, "%16d", &(flags.delay)) == 1) {
if (flags.delay < 1) flags.delay = 1;
if (flags.delay > 100) flags.delay = 100;
} else {
2015-10-05 14:27:44 +00:00
fprintf(stderr, "Error: invalid delay value \"%s\".\n", optarg);
exit(1);
}
break;
case 'u':
if (!Action_setUserOnly(optarg, &(flags.userId))) {
2015-10-05 14:27:44 +00:00
fprintf(stderr, "Error: invalid user \"%s\".\n", optarg);
exit(1);
2015-09-19 16:21:22 +00:00
}
break;
case 'C':
flags.useColors = false;
2012-08-10 21:54:41 +00:00
break;
2018-04-05 22:45:55 +00:00
case 't':
flags.treeView = true;
break;
case 'p': {
2016-02-02 14:53:02 +00:00
char* argCopy = xStrdup(optarg);
char* saveptr;
char* pid = strtok_r(argCopy, ",", &saveptr);
2012-08-10 21:54:41 +00:00
if(!flags.pidWhiteList) {
flags.pidWhiteList = Hashtable_new(8, false);
2012-08-10 21:54:41 +00:00
}
while(pid) {
2012-08-10 21:54:41 +00:00
unsigned int num_pid = atoi(pid);
Hashtable_put(flags.pidWhiteList, num_pid, (void *) 1);
pid = strtok_r(NULL, ",", &saveptr);
2012-08-10 21:54:41 +00:00
}
free(argCopy);
break;
}
default:
exit(1);
2006-03-04 18:16:49 +00:00
}
}
return flags;
}
static void millisleep(unsigned long millisec) {
struct timespec req = {
.tv_sec = 0,
.tv_nsec = millisec * 1000000L
};
while(nanosleep(&req,&req)==-1) {
continue;
}
}
int main(int argc, char** argv) {
char *lc_ctype = getenv("LC_CTYPE");
if(lc_ctype != NULL)
setlocale(LC_CTYPE, lc_ctype);
else if ((lc_ctype = getenv("LC_ALL")))
setlocale(LC_CTYPE, lc_ctype);
else
setlocale(LC_CTYPE, "");
CommandLineSettings flags = parseArguments(argc, argv); // may exit()
2014-11-27 22:04:53 +00:00
#ifdef HAVE_PROC
if (access(PROCDIR, R_OK) != 0) {
fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR);
exit(1);
}
2014-11-27 22:04:53 +00:00
#endif
2006-03-04 18:16:49 +00:00
Process_setupColumnWidths();
2006-03-04 18:16:49 +00:00
UsersTable* ut = UsersTable_new();
ProcessList* pl = ProcessList_new(ut, flags.pidWhiteList, flags.userId);
Settings* settings = Settings_new(pl->cpuCount);
pl->settings = settings;
Header* header = Header_new(pl, settings, 2);
Header_populateFromSettings(header);
if (flags.delay != -1)
settings->delay = flags.delay;
if (!flags.useColors)
settings->colorScheme = COLORSCHEME_MONOCHROME;
2018-04-05 22:45:55 +00:00
if (flags.treeView)
settings->treeView = true;
2006-03-04 18:16:49 +00:00
CRT_init(settings->delay, settings->colorScheme);
2015-03-23 18:26:56 +00:00
MainPanel* panel = MainPanel_new();
ProcessList_setPanel(pl, (Panel*) panel);
2015-03-23 18:26:56 +00:00
MainPanel_updateTreeFunctions(panel, settings->treeView);
if (flags.sortKey > 0) {
settings->sortKey = flags.sortKey;
settings->treeView = false;
settings->direction = 1;
}
ProcessList_printHeader(pl, Panel_getHeader((Panel*)panel));
2006-03-04 18:16:49 +00:00
State state = {
.settings = settings,
.ut = ut,
.pl = pl,
.panel = (Panel*) panel,
.header = header,
};
MainPanel_setState(panel, &state);
ScreenManager* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, true);
2015-03-23 18:26:56 +00:00
ScreenManager_add(scr, (Panel*) panel, -1);
2006-03-04 18:16:49 +00:00
ProcessList_scan(pl);
millisleep(75);
ProcessList_scan(pl);
ScreenManager_run(scr, NULL, NULL);
2006-03-04 18:16:49 +00:00
attron(CRT_colors[RESET_COLOR]);
mvhline(LINES-1, 0, ' ', COLS);
attroff(CRT_colors[RESET_COLOR]);
refresh();
CRT_done();
if (settings->changed)
Settings_write(settings);
Header_delete(header);
ProcessList_delete(pl);
ScreenManager_delete(scr);
2006-03-04 18:16:49 +00:00
UsersTable_delete(ut);
Settings_delete(settings);
if(flags.pidWhiteList) {
Hashtable_delete(flags.pidWhiteList);
2012-08-10 21:54:41 +00:00
}
2006-03-04 18:16:49 +00:00
return 0;
}