Annotation of src/usr.bin/top/top.c, Revision 1.28
1.28 ! deraadt 1: /* $OpenBSD: top.c,v 1.27 2003/07/07 21:36:52 deraadt Exp $ */
1.1 downsj 2:
3: /*
4: * Top users/processes display for Unix
5: * Version 3
6: *
1.18 deraadt 7: * Copyright (c) 1984, 1989, William LeFebvre, Rice University
8: * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
1.1 downsj 9: *
1.18 deraadt 10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 downsj 29: */
30:
1.20 deraadt 31: const char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre";
1.1 downsj 32:
1.2 downsj 33: #include <sys/types.h>
1.23 millert 34: #include <sys/time.h>
35: #include <err.h>
36: #include <errno.h>
1.2 downsj 37: #include <stdio.h>
38: #include <ctype.h>
1.1 downsj 39: #include <signal.h>
1.2 downsj 40: #include <string.h>
1.28 ! deraadt 41: #include <poll.h>
1.2 downsj 42: #include <stdlib.h>
43: #include <unistd.h>
1.1 downsj 44:
45: /* includes specific to top */
46: #include "display.h" /* interface to display package */
47: #include "screen.h" /* interface to screen package */
48: #include "top.h"
49: #include "top.local.h"
50: #include "boolean.h"
51: #include "machine.h"
52: #include "utils.h"
53:
54: /* Size of the stdio buffer given to stdout */
1.20 deraadt 55: #define BUFFERSIZE 2048
1.1 downsj 56:
57: /* The buffer that stdio will use */
1.20 deraadt 58: char stdoutbuf[BUFFERSIZE];
1.1 downsj 59:
1.20 deraadt 60: extern int overstrike;
1.1 downsj 61:
62: /* signal handling routines */
1.20 deraadt 63: static void leave(int);
64: static void onalrm(int);
65: static void tstop(int);
66: static void winch(int);
1.1 downsj 67:
1.25 deraadt 68: volatile sig_atomic_t leaveflag, tstopflag, winchflag;
1.7 deraadt 69:
1.20 deraadt 70: static void reset_display(void);
71: int rundisplay(void);
1.1 downsj 72:
1.20 deraadt 73: static int max_topn; /* maximum displayable processes */
1.1 downsj 74:
1.20 deraadt 75: extern int (*proc_compares[])(const void *, const void *);
76: int order_index;
1.1 downsj 77:
78: /* pointers to display routines */
1.27 deraadt 79: void (*d_loadave) (int, double *) = i_loadave;
80: void (*d_procstates) (int, int *) = i_procstates;
81: void (*d_cpustates) (int *) = i_cpustates;
82: void (*d_memory) (int *) = i_memory;
83: void (*d_message) (void) = i_message;
84: void (*d_header) (char *) = i_header;
85: void (*d_process) (int, char *) = i_process;
1.20 deraadt 86:
87: int displays = 0; /* indicates unspecified */
88: char do_unames = Yes;
89: struct process_select ps;
90: char dostates = No;
91: char interactive = Maybe;
92: char warnings = 0;
93: double delay = Default_DELAY;
94: char *order_name = NULL;
95: int topn = Default_TOPN;
1.24 deraadt 96: int no_command = Yes;
1.1 downsj 97:
98: #if Default_TOPN == Infinity
1.20 deraadt 99: char topn_specified = No;
1.1 downsj 100: #endif
101:
1.20 deraadt 102: /*
103: * these defines enumerate the "strchr"s of the commands in
104: * command_chars
105: */
1.1 downsj 106: #define CMD_redraw 0
107: #define CMD_update 1
108: #define CMD_quit 2
109: #define CMD_help1 3
110: #define CMD_help2 4
1.20 deraadt 111: #define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */
112: #define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */
1.1 downsj 113: #define CMD_number1 6
114: #define CMD_number2 7
115: #define CMD_delay 8
116: #define CMD_displays 9
117: #define CMD_kill 10
118: #define CMD_renice 11
119: #define CMD_idletog 12
120: #define CMD_idletog2 13
121: #define CMD_user 14
1.12 fgsch 122: #define CMD_system 15
123: #define CMD_order 16
1.1 downsj 124:
1.27 deraadt 125: static void
1.20 deraadt 126: usage(void)
127: {
1.22 deraadt 128: extern char *__progname;
129:
1.20 deraadt 130: fprintf(stderr,
1.22 deraadt 131: "usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
132: __progname);
1.20 deraadt 133: }
134:
1.27 deraadt 135: static void
1.20 deraadt 136: parseargs(int ac, char **av)
137: {
138: char *endp;
139: int i;
140:
141: while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != -1) {
142: switch (i) {
143: case 'u': /* toggle uid/username display */
144: do_unames = !do_unames;
145: break;
146:
147: case 'U': /* display only username's processes */
1.21 millert 148: if ((ps.uid = userid(optarg)) == (uid_t)-1) {
1.20 deraadt 149: fprintf(stderr, "%s: unknown user\n", optarg);
150: exit(1);
151: }
152: break;
1.1 downsj 153:
1.20 deraadt 154: case 'S': /* show system processes */
155: ps.system = !ps.system;
156: break;
157:
158: case 'I': /* show idle processes */
159: ps.idle = !ps.idle;
160: break;
161:
162: case 'i': /* go interactive regardless */
163: interactive = Yes;
164: break;
165:
166: case 'n': /* batch, or non-interactive */
167: case 'b':
168: interactive = No;
169: break;
170:
171: case 'd': /* number of displays to show */
172: if ((i = atoiwi(optarg)) != Invalid && i != 0) {
173: displays = i;
174: break;
175: }
1.22 deraadt 176: warnx("warning: display count should be positive "
177: "-- option ignored");
1.20 deraadt 178: warnings++;
179: break;
180:
181: case 's':
182: delay = strtod(optarg, &endp);
183:
184: if (delay > 0 && delay <= 1000000 && *endp == '\0')
185: break;
186:
1.22 deraadt 187: warnx("warning: delay should be a non-negative number"
188: " -- using default");
1.20 deraadt 189: delay = Default_DELAY;
190: warnings++;
191: break;
192:
193: case 'q': /* be quick about it */
194: /* only allow this if user is really root */
195: if (getuid() == 0) {
196: /* be very un-nice! */
197: (void) nice(-20);
198: break;
199: }
1.22 deraadt 200: warnx("warning: `-q' option can only be used by root");
1.20 deraadt 201: warnings++;
202: break;
203:
204: case 'o': /* select sort order */
205: order_name = optarg;
206: break;
207:
208: default:
209: usage();
210: exit(1);
1.1 downsj 211: }
1.20 deraadt 212: }
1.1 downsj 213:
1.20 deraadt 214: /* get count of top processes to display (if any) */
215: if (optind < ac) {
216: if ((topn = atoiwi(av[optind])) == Invalid) {
1.22 deraadt 217: warnx("warning: process display count should "
218: "be non-negative -- using default");
1.20 deraadt 219: warnings++;
1.1 downsj 220: }
1.20 deraadt 221: #if Default_TOPN == Infinity
1.1 downsj 222: else
1.20 deraadt 223: topn_specified = Yes;
224: #endif
225: }
226: }
1.1 downsj 227:
1.20 deraadt 228: struct system_info system_info;
229: struct statics statics;
1.1 downsj 230:
1.20 deraadt 231: int
232: main(int argc, char *argv[])
233: {
234: char *uname_field = "USERNAME", *header_text, *env_top;
1.27 deraadt 235: char *(*get_userid)(uid_t) = username, **preset_argv, **av;
1.20 deraadt 236: int preset_argc = 0, ac, active_procs, i;
237: sigset_t mask, oldmask;
238: time_t curr_time;
239: caddr_t processes;
1.1 downsj 240:
1.20 deraadt 241: /* set the buffer for stdout */
242: #ifdef DEBUG
243: setbuffer(stdout, NULL, 0);
1.1 downsj 244: #else
1.20 deraadt 245: setbuffer(stdout, stdoutbuf, sizeof stdoutbuf);
1.1 downsj 246: #endif
247:
1.20 deraadt 248: /* initialize some selection options */
249: ps.idle = Yes;
250: ps.system = No;
1.21 millert 251: ps.uid = (uid_t)-1;
1.20 deraadt 252: ps.command = NULL;
253:
254: /* get preset options from the environment */
255: if ((env_top = getenv("TOP")) != NULL) {
256: av = preset_argv = argparse(env_top, &preset_argc);
257: ac = preset_argc;
258:
259: /*
260: * set the dummy argument to an explanatory message, in case
261: * getopt encounters a bad argument
262: */
263: preset_argv[0] = "while processing environment";
264: }
265: /* process options */
266: do {
267: /*
268: * if we're done doing the presets, then process the real
269: * arguments
270: */
271: if (preset_argc == 0) {
272: ac = argc;
273: av = argv;
274: optind = 1;
275: }
276: parseargs(ac, av);
277: i = preset_argc;
278: preset_argc = 0;
279: } while (i != 0);
280:
281: /* set constants for username/uid display correctly */
282: if (!do_unames) {
283: uname_field = " UID ";
1.26 millert 284: get_userid = format_uid;
1.20 deraadt 285: }
286: /* initialize the kernel memory interface */
287: if (machine_init(&statics) == -1)
1.1 downsj 288: exit(1);
1.20 deraadt 289:
290: /* determine sorting order index, if necessary */
291: if (order_name != NULL) {
292: if ((order_index = string_index(order_name,
293: statics.order_names)) == -1) {
294: char **pp;
295:
1.22 deraadt 296: warnx("'%s' is not a recognized sorting order",
297: order_name);
1.20 deraadt 298: fprintf(stderr, "\tTry one of these:");
299: pp = statics.order_names;
300: while (*pp != NULL)
301: fprintf(stderr, " %s", *pp++);
302: fputc('\n', stderr);
303: exit(1);
304: }
1.1 downsj 305: }
306:
1.20 deraadt 307: /* initialize termcap */
308: init_termcap(interactive);
309:
310: /* get the string to use for the process area header */
311: header_text = format_header(uname_field);
312:
313: /* initialize display interface */
314: if ((max_topn = display_init(&statics)) == -1) {
1.22 deraadt 315: warnx("can't allocate sufficient memory");
1.20 deraadt 316: exit(4);
317: }
318: /* print warning if user requested more processes than we can display */
319: if (topn > max_topn) {
1.22 deraadt 320: warnx("warning: this terminal can only display %d processes",
321: max_topn);
1.1 downsj 322: warnings++;
1.20 deraadt 323: }
324: /* adjust for topn == Infinity */
325: if (topn == Infinity) {
326: /*
327: * For smart terminals, infinity really means everything that can
328: * be displayed, or Largest.
329: * On dumb terminals, infinity means every process in the system!
330: * We only really want to do that if it was explicitly specified.
331: * This is always the case when "Default_TOPN != Infinity". But if
332: * topn wasn't explicitly specified and we are on a dumb terminal
333: * and the default is Infinity, then (and only then) we use
334: * "Nominal_TOPN" instead.
335: */
1.1 downsj 336: #if Default_TOPN == Infinity
1.20 deraadt 337: topn = smart_terminal ? Largest :
338: (topn_specified ? Largest : Nominal_TOPN);
339: #else
340: topn = Largest;
1.1 downsj 341: #endif
342: }
1.20 deraadt 343: /* set header display accordingly */
344: display_header(topn > 0);
1.1 downsj 345:
1.20 deraadt 346: /* determine interactive state */
347: if (interactive == Maybe)
348: interactive = smart_terminal;
1.1 downsj 349:
1.20 deraadt 350: /* if # of displays not specified, fill it in */
351: if (displays == 0)
352: displays = smart_terminal ? Infinity : 1;
1.1 downsj 353:
354: /*
1.20 deraadt 355: * block interrupt signals while setting up the screen and the
356: * handlers
1.1 downsj 357: */
1.20 deraadt 358: sigemptyset(&mask);
359: sigaddset(&mask, SIGINT);
360: sigaddset(&mask, SIGQUIT);
361: sigaddset(&mask, SIGTSTP);
362: sigprocmask(SIG_BLOCK, &mask, &oldmask);
363: init_screen();
364: (void) signal(SIGINT, leave);
365: (void) signal(SIGQUIT, leave);
366: (void) signal(SIGTSTP, tstop);
367: (void) signal(SIGWINCH, winch);
368: sigprocmask(SIG_SETMASK, &oldmask, NULL);
369: if (warnings) {
370: fputs("....", stderr);
371: fflush(stderr); /* why must I do this? */
372: sleep((unsigned) (3 * warnings));
373: fputc('\n', stderr);
374: }
1.7 deraadt 375: restart:
1.1 downsj 376:
1.20 deraadt 377: /*
378: * main loop -- repeat while display count is positive or while it
379: * indicates infinity (by being -1)
380: */
381: while ((displays == -1) || (displays-- > 0)) {
382: /* get the current stats */
383: get_system_info(&system_info);
384:
385: /* get the current set of processes */
386: processes = get_process_info(&system_info, &ps,
387: proc_compares[order_index]);
388:
389: /* display the load averages */
390: (*d_loadave)(system_info.last_pid, system_info.load_avg);
391:
392: /* display the current time */
393: /* this method of getting the time SHOULD be fairly portable */
394: time(&curr_time);
395: i_timeofday(&curr_time);
396:
397: /* display process state breakdown */
398: (*d_procstates)(system_info.p_total, system_info.procstates);
399:
400: /* display the cpu state percentage breakdown */
401: if (dostates) { /* but not the first time */
402: (*d_cpustates) (system_info.cpustates);
403: } else {
404: /* we'll do it next time */
405: if (smart_terminal)
406: z_cpustates();
407: else {
408: if (putchar('\n') == EOF)
409: exit(1);
410: }
411: dostates = Yes;
412: }
1.1 downsj 413:
1.20 deraadt 414: /* display memory stats */
415: (*d_memory) (system_info.memory);
1.1 downsj 416:
1.20 deraadt 417: /* handle message area */
418: (*d_message) ();
1.1 downsj 419:
1.20 deraadt 420: /* update the header area */
421: (*d_header) (header_text);
422:
423: if (topn > 0) {
424: /* determine number of processes to actually display */
425: /*
426: * this number will be the smallest of: active
427: * processes, number user requested, number current
428: * screen accommodates
429: */
430: active_procs = system_info.p_active;
431: if (active_procs > topn)
432: active_procs = topn;
433: if (active_procs > max_topn)
434: active_procs = max_topn;
435: /* now show the top "n" processes. */
436: for (i = 0; i < active_procs; i++)
437: (*d_process)(i, format_next_process(processes,
438: get_userid));
439: } else
440: i = 0;
441:
442: /* do end-screen processing */
443: u_endscreen(i);
444:
445: /* now, flush the output buffer */
446: fflush(stdout);
447:
448: /* only do the rest if we have more displays to show */
449: if (displays) {
450: /* switch out for new display on smart terminals */
451: if (smart_terminal) {
452: if (overstrike) {
453: reset_display();
454: } else {
455: d_loadave = u_loadave;
456: d_procstates = u_procstates;
457: d_cpustates = u_cpustates;
458: d_memory = u_memory;
459: d_message = u_message;
460: d_header = u_header;
461: d_process = u_process;
462: }
463: }
464: no_command = Yes;
465: if (!interactive) {
466: /* set up alarm */
467: (void) signal(SIGALRM, onalrm);
468: (void) alarm((unsigned) delay);
469:
470: /* wait for the rest of it .... */
471: pause();
472: } else {
473: while (no_command)
474: if (rundisplay())
475: goto restart;
476: }
477: }
478: }
1.1 downsj 479:
1.20 deraadt 480: quit(0);
481: /* NOTREACHED */
482: return (0);
483: }
1.7 deraadt 484:
1.20 deraadt 485: int
486: rundisplay(void)
487: {
488: static char tempbuf1[50], tempbuf2[50];
489: sigset_t mask;
490: char ch, *iptr;
491: int change, i;
1.28 ! deraadt 492: struct pollfd pfd[1];
1.21 millert 493: uid_t uid;
1.20 deraadt 494: static char command_chars[] = "\f qh?en#sdkriIuSo";
1.7 deraadt 495:
1.20 deraadt 496: /*
497: * assume valid command unless told
498: * otherwise
499: */
500: no_command = No;
1.7 deraadt 501:
1.20 deraadt 502: /*
503: * set up arguments for select with
504: * timeout
505: */
1.28 ! deraadt 506: pfd[0].fd = STDIN_FILENO;
! 507: pfd[0].events = POLLIN;
1.20 deraadt 508:
509: if (leaveflag) {
510: end_screen();
511: exit(0);
512: }
513: if (tstopflag) {
514: /* move to the lower left */
515: end_screen();
516: fflush(stdout);
517:
518: /*
519: * default the signal handler
520: * action
521: */
522: (void) signal(SIGTSTP, SIG_DFL);
523:
524: /*
525: * unblock the signal and
526: * send ourselves one
527: */
528: sigemptyset(&mask);
529: sigaddset(&mask, SIGTSTP);
530: sigprocmask(SIG_UNBLOCK, &mask, NULL);
531: (void) kill(0, SIGTSTP);
532:
533: /* reset the signal handler */
534: (void) signal(SIGTSTP, tstop);
535:
536: /* reinit screen */
537: reinit_screen();
538: reset_display();
539: tstopflag = 0;
540: return 1;
541: }
542: if (winchflag) {
543: /*
544: * reascertain the screen
545: * dimensions
546: */
547: get_screensize();
548:
549: /* tell display to resize */
550: max_topn = display_resize();
551:
552: /* reset the signal handler */
553: (void) signal(SIGWINCH, winch);
554:
555: reset_display();
556: winchflag = 0;
557: return 1;
558: }
559: /*
560: * wait for either input or the end
561: * of the delay period
562: */
1.28 ! deraadt 563: if (poll(pfd, 1, delay * 1000) > 0) {
1.20 deraadt 564: char *errmsg;
565: int newval;
566:
567: clear_message();
568:
569: /*
570: * now read it and convert to
571: * command strchr
572: */
573: (void) read(0, &ch, 1);
574: if ((iptr = strchr(command_chars, ch)) == NULL) {
1.1 downsj 575: /* illegal command */
576: new_message(MT_standout, " Command not understood");
1.9 deraadt 577: if (putchar('\r') == EOF)
1.20 deraadt 578: exit(1);
1.1 downsj 579: no_command = Yes;
1.20 deraadt 580: fflush(stdout);
581: return (0);
582: }
583:
584: change = iptr - command_chars;
585: if (overstrike && change > CMD_OSLIMIT) {
586: /* error */
587: new_message(MT_standout,
1.1 downsj 588: " Command cannot be handled by this terminal");
1.20 deraadt 589: if (putchar('\r') == EOF)
590: exit(1);
591: no_command = Yes;
592: fflush(stdout);
593: return (0);
594: }
595:
596: switch (change) {
597: case CMD_redraw: /* redraw screen */
598: reset_display();
599: break;
600:
601: case CMD_update: /* merely update display */
602: /*
603: * is the load average high?
604: */
605: if (system_info.load_avg[0] > LoadMax) {
606: /* yes, go home for visual feedback */
607: go_home();
608: fflush(stdout);
1.1 downsj 609: }
1.20 deraadt 610: break;
611:
612: case CMD_quit: /* quit */
613: quit(0);
614: break;
615:
616: case CMD_help1: /* help */
617: case CMD_help2:
618: reset_display();
619: clear();
620: show_help();
621: standout("Hit any key to continue: ");
622: fflush(stdout);
623: (void) read(0, &ch, 1);
624: break;
625:
626: case CMD_errors: /* show errors */
627: if (error_count() == 0) {
628: new_message(MT_standout,
629: " Currently no errors to report.");
630: if (putchar('\r') == EOF)
631: exit(1);
632: no_command = Yes;
633: } else {
1.1 downsj 634: reset_display();
635: clear();
1.20 deraadt 636: show_errors();
1.1 downsj 637: standout("Hit any key to continue: ");
638: fflush(stdout);
639: (void) read(0, &ch, 1);
1.20 deraadt 640: }
641: break;
642:
643: case CMD_number1: /* new number */
644: case CMD_number2:
645: new_message(MT_standout,
646: "Number of processes to show: ");
647: newval = readline(tempbuf1, 8, Yes);
648: if (newval > -1) {
649: if (newval > max_topn) {
1.1 downsj 650: new_message(MT_standout | MT_delayed,
1.20 deraadt 651: " This terminal can only "
652: "display %d processes.",
653: max_topn);
1.9 deraadt 654: if (putchar('\r') == EOF)
1.20 deraadt 655: exit(1);
656: }
657: if (newval == 0)
1.1 downsj 658: display_header(No);
1.20 deraadt 659: else if (newval > topn && topn == 0) {
1.1 downsj 660: /* redraw the header */
661: display_header(Yes);
662: d_header = i_header;
663: }
1.20 deraadt 664: topn = newval;
665: }
666: break;
667:
668: case CMD_delay: /* new seconds delay */
669: new_message(MT_standout, "Seconds to delay: ");
670: if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
671: char *endp;
672: double newdelay = strtod(tempbuf2, &endp);
673:
674: if (newdelay >= 0 && newdelay < 1000000 &&
675: *endp == '\0')
1.16 hugh 676: delay = newdelay;
1.20 deraadt 677: }
678: clear_message();
679: break;
680:
681: case CMD_displays: /* change display count */
682: new_message(MT_standout,
683: "Displays to show (currently %s): ",
684: displays == -1 ? "infinite" :
685: itoa(displays));
686: if ((i = readline(tempbuf1, 10, Yes)) > 0)
687: displays = i;
688: else if (i == 0)
689: quit(0);
690:
691: clear_message();
692: break;
693:
694: case CMD_kill: /* kill program */
695: new_message(0, "kill ");
696: if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
697: if ((errmsg = kill_procs(tempbuf2)) != NULL) {
1.4 millert 698: new_message(MT_standout, "%s", errmsg);
1.9 deraadt 699: if (putchar('\r') == EOF)
1.20 deraadt 700: exit(1);
1.1 downsj 701: no_command = Yes;
702: }
1.20 deraadt 703: } else
704: clear_message();
705: break;
706:
707: case CMD_renice: /* renice program */
708: new_message(0, "renice ");
709: if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
710: if ((errmsg = renice_procs(tempbuf2)) != NULL) {
1.4 millert 711: new_message(MT_standout, "%s", errmsg);
1.9 deraadt 712: if (putchar('\r') == EOF)
1.20 deraadt 713: exit(1);
1.1 downsj 714: no_command = Yes;
715: }
1.20 deraadt 716: } else
717: clear_message();
718: break;
1.1 downsj 719:
1.20 deraadt 720: case CMD_idletog:
721: case CMD_idletog2:
722: ps.idle = !ps.idle;
723: new_message(MT_standout | MT_delayed,
724: " %sisplaying idle processes.",
725: ps.idle ? "D" : "Not d");
726: if (putchar('\r') == EOF)
727: exit(1);
728: break;
1.1 downsj 729:
1.20 deraadt 730: case CMD_user:
731: new_message(MT_standout,
732: "Username to show: ");
733: if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
734: if (tempbuf2[0] == '+' &&
735: tempbuf2[1] == '\0') {
1.21 millert 736: ps.uid = (uid_t)-1;
737: } else if ((uid = userid(tempbuf2)) == (uid_t)-1) {
1.1 downsj 738: new_message(MT_standout,
739: " %s: unknown user", tempbuf2);
740: no_command = Yes;
1.20 deraadt 741: } else
1.21 millert 742: ps.uid = uid;
1.20 deraadt 743: if (putchar('\r') == EOF)
744: exit(1);
745: } else
746: clear_message();
747: break;
1.12 fgsch 748:
1.20 deraadt 749: case CMD_system:
750: ps.system = !ps.system;
751: new_message(MT_standout | MT_delayed,
752: " %sisplaying system processes.",
753: ps.system ? "D" : "Not d");
754: break;
755:
756: case CMD_order:
757: new_message(MT_standout,
758: "Order to sort: ");
759: if (readline(tempbuf2,
760: sizeof(tempbuf2), No) > 0) {
761: if ((i = string_index(tempbuf2,
762: statics.order_names)) == -1) {
763: new_message(MT_standout,
764: " %s: unrecognized sorting order",
765: tempbuf2);
766: no_command = Yes;
767: } else
1.1 downsj 768: order_index = i;
1.9 deraadt 769: if (putchar('\r') == EOF)
1.20 deraadt 770: exit(1);
771: } else
772: clear_message();
773: break;
1.1 downsj 774:
1.20 deraadt 775: default:
776: new_message(MT_standout, " BAD CASE IN SWITCH!");
777: if (putchar('\r') == EOF)
778: exit(1);
1.1 downsj 779: }
780: }
781:
1.20 deraadt 782: /* flush out stuff that may have been written */
783: fflush(stdout);
784: return 0;
1.1 downsj 785: }
786:
1.20 deraadt 787:
1.1 downsj 788: /*
789: * reset_display() - reset all the display routine pointers so that entire
790: * screen will get redrawn.
791: */
1.19 pvalchev 792: static void
793: reset_display(void)
1.1 downsj 794: {
1.20 deraadt 795: d_loadave = i_loadave;
796: d_procstates = i_procstates;
797: d_cpustates = i_cpustates;
798: d_memory = i_memory;
799: d_message = i_message;
800: d_header = i_header;
801: d_process = i_process;
1.1 downsj 802: }
803:
1.19 pvalchev 804: void
1.20 deraadt 805: leave(int signo)
1.1 downsj 806: {
1.20 deraadt 807: leaveflag = 1;
1.1 downsj 808: }
809:
1.19 pvalchev 810: void
1.20 deraadt 811: tstop(int signo)
1.1 downsj 812: {
1.20 deraadt 813: tstopflag = 1;
1.1 downsj 814: }
815:
1.19 pvalchev 816: void
1.20 deraadt 817: winch(int signo)
1.1 downsj 818: {
1.20 deraadt 819: winchflag = 1;
1.1 downsj 820: }
821:
1.19 pvalchev 822: void
1.20 deraadt 823: onalrm(int signo)
1.1 downsj 824: {
825: }
826:
1.19 pvalchev 827: void
1.20 deraadt 828: quit(int ret)
1.1 downsj 829: {
1.20 deraadt 830: end_screen();
831: exit(ret);
1.1 downsj 832: }