1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)main.c 5.10 (Berkeley) 06/26/91"; 15 #endif not lint 16 17 #include "systat.h" 18 #include <varargs.h> 19 20 static struct nlist nlst[] = { 21 #define X_FIRST 0 22 #define X_HZ 0 23 { "_hz" }, 24 #define X_PHZ 1 25 { "_phz" }, 26 { "" } 27 }; 28 29 int naptime = 5; 30 31 void die(), display(), suspend(); 32 33 sig_t sigtstpdfl; 34 int dellave; 35 36 static WINDOW *wload; /* one line window for load average */ 37 38 int verbose = 1; /* to report kvm read errs */ 39 40 main(argc, argv) 41 int argc; 42 char **argv; 43 { 44 45 argc--, argv++; 46 while (argc > 0) { 47 if (argv[0][0] == '-') { 48 struct cmdtab *p; 49 50 p = lookup(&argv[0][1]); 51 if (p == (struct cmdtab *)-1) { 52 fprintf(stderr, "%s: unknown request\n", 53 &argv[0][1]); 54 exit(1); 55 } 56 curcmd = p; 57 } else { 58 naptime = atoi(argv[0]); 59 if (naptime <= 0) 60 naptime = 5; 61 } 62 argc--, argv++; 63 } 64 kvm_nlist(nlst); 65 if (nlst[X_FIRST].n_type == 0) { 66 fprintf(stderr, "Couldn't namelist.\n"); 67 exit(1); 68 } 69 signal(SIGINT, die); 70 signal(SIGQUIT, die); 71 signal(SIGTERM, die); 72 73 /* 74 * Initialize display. Load average appears in a one line 75 * window of its own. Current command's display appears in 76 * an overlapping sub-window of stdscr configured by the display 77 * routines to minimize update work by curses. 78 */ 79 initscr(); 80 CMDLINE = LINES - 1; 81 wnd = (*curcmd->c_open)(); 82 if (wnd == NULL) { 83 fprintf(stderr, "Couldn't initialize display.\n"); 84 die(); 85 } 86 wload = newwin(1, 0, 3, 20); 87 if (wload == NULL) { 88 fprintf(stderr, "Couldn't set up load average window.\n"); 89 die(); 90 } 91 gethostname(hostname, sizeof (hostname)); 92 NREAD(X_HZ, &hz, LONG); 93 NREAD(X_PHZ, &phz, LONG); 94 (*curcmd->c_init)(); 95 curcmd->c_flags |= CF_INIT; 96 labels(); 97 98 dellave = 0.0; 99 100 signal(SIGALRM, display); 101 sigtstpdfl = signal(SIGTSTP, suspend); 102 display(); 103 noecho(); 104 crmode(); 105 keyboard(); 106 /*NOTREACHED*/ 107 } 108 109 labels() 110 { 111 if (curcmd->c_flags & CF_LOADAV) { 112 mvaddstr(2, 20, 113 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 114 mvaddstr(3, 5, "Load Average"); 115 } 116 (*curcmd->c_label)(); 117 #ifdef notdef 118 mvprintw(21, 25, "CPU usage on %s", hostname); 119 #endif 120 refresh(); 121 } 122 123 void 124 display() 125 { 126 register int i, j; 127 128 /* Get the load average over the last minute. */ 129 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 130 (*curcmd->c_fetch)(); 131 if (curcmd->c_flags & CF_LOADAV) { 132 j = 5.0*avenrun[0] + 0.5; 133 dellave -= avenrun[0]; 134 if (dellave >= 0.0) 135 c = '<'; 136 else { 137 c = '>'; 138 dellave = -dellave; 139 } 140 if (dellave < 0.1) 141 c = '|'; 142 dellave = avenrun[0]; 143 wmove(wload, 0, 0); wclrtoeol(wload); 144 for (i = (j > 50) ? 50 : j; i > 0; i--) 145 waddch(wload, c); 146 if (j > 50) 147 wprintw(wload, " %4.1f", avenrun[0]); 148 } 149 (*curcmd->c_refresh)(); 150 if (curcmd->c_flags & CF_LOADAV) 151 wrefresh(wload); 152 wrefresh(wnd); 153 move(CMDLINE, col); 154 refresh(); 155 alarm(naptime); 156 } 157 158 load() 159 { 160 161 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 162 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 163 avenrun[0], avenrun[1], avenrun[2]); 164 clrtoeol(); 165 } 166 167 void 168 die() 169 { 170 move(CMDLINE, 0); 171 clrtoeol(); 172 refresh(); 173 endwin(); 174 exit(0); 175 } 176 177 error(va_alist) 178 va_dcl 179 { 180 va_list ap; 181 char *fmt, msg[200]; 182 183 va_start(ap); 184 fmt = va_arg(ap, char *); 185 (void) vsnprintf(msg, sizeof msg, fmt, ap); 186 va_end(ap); 187 mvaddstr(CMDLINE, 0, msg); 188 clrtoeol(); 189 refresh(); 190 } 191