-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
319 lines (293 loc) · 7.16 KB
/
command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/****************************************************************************
* Functions for handling user interaction.
****************************************************************************/
#include "main.h"
static void KillCommand();
static void ReniceCommand();
static void IdleCommand();
static void NumberCommand();
static void SecondsCommand();
static void OrderCommand();
static void UserCommand();
static void InvalidCommand();
static int GetNumber(unsigned long *value);
/****************************************************************************
* The user interaction loop.
****************************************************************************/
void CommandLoop() {
char ch;
signal(SIGTERM, Terminate);
signal(SIGQUIT, Terminate);
signal(SIGKILL, Terminate);
signal(SIGINT, Terminate);
AlarmHandler();
system("stty raw -echo");
for(;;) {
if(read(0, &ch, 1) == 1) {
signal(SIGALRM, SIG_IGN);
switch(ch) {
case 'q':
Terminate();
break;
case 'k':
KillCommand();
break;
case 'r':
ReniceCommand();
break;
case 'i':
case 'I':
IdleCommand();
break;
case 'h':
case '?':
DisplayHelp();
break;
case 'n':
case '#':
NumberCommand();
break;
case 's':
SecondsCommand();
break;
case 'u':
UserCommand();
break;
case 'o':
OrderCommand();
break;
default:
InvalidCommand();
break;
}
fflush(stdout);
AlarmHandler();
}
}
}
/****************************************************************************
* Read a number from the user.
****************************************************************************/
int GetNumber(unsigned long *value) {
int len;
char ch;
fflush(stdout);
*value = 0;
for(len = 0; len < 8;) {
if(read(0, &ch, 1) == 1) {
if(ch >= '0' && ch <= '9') {
printf("%c", ch);
*value = *value * 10 + ch - '0';
++len;
} else if(ch == '\n' || ch == '\r') {
break;
} else if(ch == '\b') {
if(len) {
printf("\033[1D \033[1D");
--len;
*value /= 10;
}
}
fflush(stdout);
}
}
return len;
}
/****************************************************************************
* Read a string from the user.
****************************************************************************/
int GetString(char *str, int max) {
int len;
char ch;
fflush(stdout);
for(len = 0; len < max; ) {
str[len] = 0;
if(read(0, &ch, 1) == 1) {
if(isgraph((int)ch)) {
printf("%c", ch);
str[len++] = ch;
} else if(ch == '\n' || ch == '\r') {
break;
} else if(ch == '\b') {
if(len) {
printf("\033[1D \033[1D");
--len;
}
}
fflush(stdout);
}
}
return len;
}
/****************************************************************************
* Kill a process.
****************************************************************************/
void KillCommand() {
unsigned long pid;
printf("kill: ");
if(GetNumber(&pid)) {
if(kill(pid, SIGTERM)) {
printf("\033[%d;%dH\033[2K", 5, 0);
switch(errno) {
case EINVAL:
printf("invalid signal");
break;
case EPERM:
printf("permission denied");
break;
case ESRCH:
printf("no such PID");
break;
default:
printf("process could not be killed");
break;
}
fflush(stdout);
sleep(2);
}
}
}
/****************************************************************************
* Renice a process.
****************************************************************************/
void ReniceCommand() {
unsigned long priority;
unsigned long pid;
printf("\033[2Krenice priority (0 - 40): ");
if(GetNumber(&priority)) {
printf("\033[%d;%dH\033[2Krenice PID: ", 5, 0);
if(GetNumber(&pid)) {
errno = 0;
setpriority(PRIO_PROCESS, pid, priority - 20);
if(errno) {
printf("\033[%d;%dH\033[2K", 5, 0);
switch(errno) {
case ESRCH:
printf("no such PID");
break;
case EINVAL:
printf("invalid");
break;
case EPERM:
printf("permission denied");
break;
case EACCES:
printf("must be super-user");
break;
default:
printf("process could not be reniced");
break;
}
fflush(stdout);
sleep(2);
}
}
}
}
/****************************************************************************
* Toggle the display of idle processes.
****************************************************************************/
void IdleCommand() {
if(displayIdle) {
displayIdle = 0;
printf("Not displaying idle processes");
} else {
displayIdle = 1;
printf("Displaying idle processes");
}
fflush(stdout);
sleep(2);
}
/****************************************************************************
* Set the number of processes to display.
****************************************************************************/
void NumberCommand() {
unsigned long number;
printf("Number of processes to show: ");
if(GetNumber(&number)) {
displayLines = number;
if(displayLines < 0) {
displayLines = 0;
}
}
}
/****************************************************************************
* Set the number of seconds delay between updates.
****************************************************************************/
void SecondsCommand() {
unsigned long seconds;
printf("Seconds to delay: ");
if(GetNumber(&seconds)) {
delay = seconds;
if(delay <= 0) {
delay = 2;
}
}
}
/****************************************************************************
* Select a user whose processes to display.
****************************************************************************/
void UserCommand() {
uid_t temp;
char str[9];
printf("user (+ for all): ");
if(GetString(str, 8)) {
if(!strcmp(str, "+")) {
displayUser = -1;
} else {
temp = GetUserID(str);
if(temp < 0) {
printf("\033[%d;%dH\033[2K", 5, 0);
printf("invalid user name");
fflush(stdout);
sleep(2);
} else {
displayUser = GetUserID(str);
}
}
}
}
/****************************************************************************
* Set the sort order.
****************************************************************************/
void OrderCommand() {
char str[9];
printf("Sort order (size, res, cpu, time, state, command): ");
if(GetString(str, 8)) {
printf("\033[%d;%dH\033[2K", 5, 0);
if(!strcmp(str, "size")) {
printf("ordering by size");
displayOrder = ORDER_SIZE;
} else if(!strcmp(str, "res")) {
printf("ordering by res");
displayOrder = ORDER_RES;
} else if(!strcmp(str, "cpu")) {
printf("ordering by cpu");
displayOrder = ORDER_CPU;
} else if(!strcmp(str, "time")) {
printf("ordering by time");
displayOrder = ORDER_TIME;
} else if(!strcmp(str, "state")) {
printf("ordering by state");
displayOrder = ORDER_STATE;
} else if(!strcmp(str, "command")) {
printf("ordering by command");
displayOrder = ORDER_COMMAND;
} else {
printf("invalid order");
fflush(stdout);
sleep(2);
return;
}
fflush(stdout);
sleep(2);
}
}
/****************************************************************************
* Show that an invalid command was entered.
****************************************************************************/
void InvalidCommand() {
printf("Invalid command");
fflush(stdout);
sleep(2);
}