htop.c 7.24 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - htop.c
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
#include "config.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9

Hisham Muhammad's avatar
Hisham Muhammad committed
10
#include "FunctionBar.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "Hashtable.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include "ColumnsPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
17
18
#include "CRT.h"
#include "MainPanel.h"
#include "ProcessList.h"
#include "ScreenManager.h"
#include "Settings.h"
#include "UsersTable.h"
19
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
20

Hisham Muhammad's avatar
Hisham Muhammad committed
21
#include <getopt.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
25
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
26
#include <time.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
27
#include <unistd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
28

Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
//#link m

31
static void printVersionFlag() {
32
33
34
   fputs("htop " VERSION " - " COPYRIGHT "\n"
         "Released under the GNU GPL.\n\n",
         stdout);
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
   exit(0);
}
37
 
38
static void printHelpFlag() {
39
40
   fputs("htop " VERSION " - " COPYRIGHT "\n"
         "Released under the GNU GPL.\n\n"
41
42
43
44
         "-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"
45
         "-t --tree                   Show the tree view by default\n"
46
47
         "-u --user=USERNAME          Show only processes of a given user\n"
         "-p --pid=PID,[,PID,PID...]  Show only the given PIDs\n"
Christian Hesse's avatar
Christian Hesse committed
48
         "-v --version                Print version info\n"
49
50
51
52
53
         "\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);
Hisham Muhammad's avatar
Hisham Muhammad committed
54
55
56
   exit(0);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
57
58
// ----------------------------------------

Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
61
62
63
64
typedef struct CommandLineSettings_ {
   Hashtable* pidWhiteList;
   uid_t userId;
   int sortKey;
   int delay;
   bool useColors;
65
   bool treeView;
Hisham Muhammad's avatar
Hisham Muhammad committed
66
67
68
69
70
71
72
73
74
75
} 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,
76
      .treeView = false,
Hisham Muhammad's avatar
Hisham Muhammad committed
77
   };
78
79
80
81
82
83
84
85
86
87

   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'},
88
      {"tree",     no_argument,         0, 't'},
89
      {"pid",      required_argument,   0, 'p'},
Hisham Muhammad's avatar
Hisham Muhammad committed
90
      {"io",       no_argument,         0, 'i'},
91
92
      {0,0,0,0}
   };
93

Hisham Muhammad's avatar
Hisham Muhammad committed
94
   int opt, opti=0;
95
   /* Parse arguments */
96
   while ((opt = getopt_long(argc, argv, "hvCst::d:u:p:i", long_opts, &opti))) {
97
98
99
100
101
102
103
104
105
      if (opt == EOF) break;
      switch (opt) {
         case 'h':
            printHelpFlag();
            break;
         case 'v':
            printVersionFlag();
            break;
         case 's':
Hisham Muhammad's avatar
Hisham Muhammad committed
106
            if (strcmp(optarg, "help") == 0) {
107
               for (int j = 1; j < Platform_numberOfFields; j++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
108
109
110
                  const char* name = Process_fields[j].name;
                  if (name) printf ("%s\n", name);
               }
111
112
               exit(0);
            }
Hisham Muhammad's avatar
Hisham Muhammad committed
113
114
            flags.sortKey = ColumnsPanel_fieldNameToIndex(optarg);
            if (flags.sortKey == -1) {
Hisham Muhammad's avatar
Hisham Muhammad committed
115
               fprintf(stderr, "Error: invalid column \"%s\".\n", optarg);
116
            }
117
118
            break;
         case 'd':
Hisham Muhammad's avatar
Hisham Muhammad committed
119
120
121
            if (sscanf(optarg, "%16d", &(flags.delay)) == 1) {
               if (flags.delay < 1) flags.delay = 1;
               if (flags.delay > 100) flags.delay = 100;
122
            } else {
Hisham Muhammad's avatar
Hisham Muhammad committed
123
               fprintf(stderr, "Error: invalid delay value \"%s\".\n", optarg);
124
            }
125
126
            break;
         case 'u':
Hisham Muhammad's avatar
Hisham Muhammad committed
127
            if (!Action_setUserOnly(optarg, &(flags.userId))) {
Hisham Muhammad's avatar
Hisham Muhammad committed
128
               fprintf(stderr, "Error: invalid user \"%s\".\n", optarg);
129
            }
130
131
            break;
         case 'C':
Hisham Muhammad's avatar
Hisham Muhammad committed
132
            flags.useColors = false;
133
            break;
134
135
136
         case 't':
            flags.treeView = true;
            break;
Hisham Muhammad's avatar
Hisham Muhammad committed
137
         case 'p': {
Hisham's avatar
Hisham committed
138
            char* argCopy = xStrdup(optarg);
139
            char* saveptr;
Hisham Muhammad's avatar
Hisham Muhammad committed
140
            char* pid = strtok_r(argCopy, ",", &saveptr);
141

142
            if(!flags.pidWhiteList) {
Hisham Muhammad's avatar
Hisham Muhammad committed
143
               flags.pidWhiteList = Hashtable_new(8, false);
144
145
            }

146
            while(pid) {
147
                unsigned int num_pid = atoi(pid);
Hisham Muhammad's avatar
Hisham Muhammad committed
148
                Hashtable_put(flags.pidWhiteList, num_pid, (void *) 1);
149
                pid = strtok_r(NULL, ",", &saveptr);
150
151
152
            }
            free(argCopy);

153
            break;
154
         }
155
156
         default:
            exit(1);
Hisham Muhammad's avatar
Hisham Muhammad committed
157
158
      }
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
159
160
   return flags;
}
161

Hisham Muhammad's avatar
Hisham Muhammad committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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()
183

184
#ifdef HAVE_PROC
185
   if (access(PROCDIR, R_OK) != 0) {
186
187
      fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR);
      exit(1);
188
   }
189
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
190
   
191
   Process_setupColumnWidths();
Hisham Muhammad's avatar
Hisham Muhammad committed
192
   
Hisham Muhammad's avatar
Hisham Muhammad committed
193
194
195
196
197
198
   UsersTable* ut = UsersTable_new();
   ProcessList* pl = ProcessList_new(ut, flags.pidWhiteList, flags.userId);
   
   Settings* settings = Settings_new(pl->cpuCount);
   pl->settings = settings;

199
200
201
202
   Header* header = Header_new(pl, settings, 2);

   Header_populateFromSettings(header);

Hisham Muhammad's avatar
Hisham Muhammad committed
203
204
205
   if (flags.delay != -1)
      settings->delay = flags.delay;
   if (!flags.useColors) 
206
      settings->colorScheme = COLORSCHEME_MONOCHROME;
207
   if (flags.treeView)
208
      settings->screens[0]->treeView = true;
209

Hisham Muhammad's avatar
Hisham Muhammad committed
210
   CRT_init(settings->delay, settings->colorScheme);
211
   
212
   MainPanel* panel = MainPanel_new();
Hisham Muhammad's avatar
Hisham Muhammad committed
213
214
   ProcessList_setPanel(pl, (Panel*) panel);

215
   MainPanel_updateTreeFunctions(panel, settings->screens[0]->treeView);
Hisham Muhammad's avatar
Hisham Muhammad committed
216
217
      
   if (flags.sortKey > 0) {
218
219
220
      settings->screens[0]->sortKey = flags.sortKey;
      settings->screens[0]->treeView = false;
      settings->screens[0]->direction = 1;
221
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
222
   ProcessList_printHeader(pl, Panel_getHeader((Panel*)panel));
Hisham Muhammad's avatar
Hisham Muhammad committed
223

Hisham Muhammad's avatar
Hisham Muhammad committed
224
225
226
227
228
229
230
231
232
   State state = {
      .settings = settings,
      .ut = ut,
      .pl = pl,
      .panel = (Panel*) panel,
      .header = header,
   };
   MainPanel_setState(panel, &state);
   
233
   ScreenManager* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, true);
234
   ScreenManager_add(scr, (Panel*) panel, -1);
Hisham Muhammad's avatar
Hisham Muhammad committed
235

Hisham Muhammad's avatar
Hisham Muhammad committed
236
   ProcessList_scan(pl);
237
   millisleep(75);
238
   ProcessList_scan(pl);
Hisham Muhammad's avatar
Hisham Muhammad committed
239
240

   ScreenManager_run(scr, NULL, NULL);   
Hisham Muhammad's avatar
Hisham Muhammad committed
241
242
243
244
245
246
247
248
249
250
251
   
   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);
Hisham Muhammad's avatar
Hisham Muhammad committed
252
253

   ScreenManager_delete(scr);
Hisham Muhammad's avatar
Hisham Muhammad committed
254
   
Hisham Muhammad's avatar
Hisham Muhammad committed
255
256
   UsersTable_delete(ut);
   Settings_delete(settings);
Hisham Muhammad's avatar
Hisham Muhammad committed
257
   
Hisham Muhammad's avatar
Hisham Muhammad committed
258
259
   if(flags.pidWhiteList) {
      Hashtable_delete(flags.pidWhiteList);
260
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
261
262
   return 0;
}