tload: new usage & fix coding style

Coding style fixed and more readable help output.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-06-05 13:19:35 +02:00
parent 05765771c2
commit a08e0a6ef2

257
tload.c
View File

@ -11,143 +11,172 @@
*/ */
#include "proc/version.h" #include "proc/version.h"
#include "proc/sysinfo.h" #include "proc/sysinfo.h"
#include <stdio.h> #include <err.h>
#include <stdlib.h> #include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <setjmp.h> #include <setjmp.h>
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
static char *screen; static char *screen;
static int nrows = 25; static int nrows = 25;
static int ncols = 80; static int ncols = 80;
static int scr_size; static int scr_size;
static int fd=1; static int fd = 1;
static int dly=5; static int dly = 5;
static jmp_buf jb; static jmp_buf jb;
static void alrm(int signo) static void alrm(int signo __attribute__ ((__unused__)))
{ {
(void)signo; signal(SIGALRM, alrm);
signal(SIGALRM, alrm); alarm(dly);
alarm(dly);
} }
static void setsize(int i) static void setsize(int i)
{ {
struct winsize win; struct winsize win;
signal(SIGWINCH, setsize); signal(SIGWINCH, setsize);
if (ioctl(fd, TIOCGWINSZ, &win) != -1) { if (ioctl(fd, TIOCGWINSZ, &win) != -1) {
if (win.ws_col > 0) if (win.ws_col > 0)
ncols = win.ws_col; ncols = win.ws_col;
if (win.ws_row > 0) if (win.ws_row > 0)
nrows = win.ws_row; nrows = win.ws_row;
} }
scr_size = nrows * ncols; scr_size = nrows * ncols;
if (screen == NULL) if (screen == NULL)
screen = (char *) malloc(scr_size); screen = (char *)malloc(scr_size);
else else
screen = (char *) realloc(screen, scr_size); screen = (char *)realloc(screen, scr_size);
if (screen == NULL) { if (screen == NULL)
perror(""); err(EXIT_FAILURE, "cannot allocate %zu bytes", scr_size);
exit(1);
} memset(screen, ' ', scr_size - 1);
memset(screen, ' ', scr_size-1); *(screen + scr_size - 2) = '\0';
*(screen + scr_size - 2) = '\0'; if (i)
if (i) longjmp(jb, 0);
longjmp(jb, 0); }
static void __attribute__ ((__noreturn__))
usage(FILE * out)
{
fprintf(out,
"\nUsage: %s [options] [tty]\n"
"\nOptions:\n", program_invocation_short_name);
fprintf(out,
" -d, --delay <secs> update delay in seconds\n"
" -s, --scale <num> vertical scale\n"
" -h, --help display this help text\n"
" -V, --version display version information and exit\n");
fprintf(out, "\nFor more information see tload(1).\n");
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int lines, row, col=0; int lines, row, col = 0;
int i, opt; int i, opt;
double av[3]; double av[3];
static double max_scale, scale_fact; static double max_scale, scale_fact;
char *scale_arg = NULL; char *scale_arg = NULL;
while ((opt = getopt(argc, argv, "s:d:V")) != -1) static const struct option longopts[] = {
switch (opt) { {"scale", required_argument, NULL, 's'},
case 's': scale_arg = optarg; break; {"delay", required_argument, NULL, 'd'},
case 'd': dly = atoi(optarg); break; {"help", no_argument, NULL, 'h'},
case 'V': display_version(); exit(0); break; {"version", no_argument, NULL, 'V'},
default: {NULL, 0, NULL, 0}
printf("usage: tload [-V] [-d delay] [-s scale] [tty]\n"); };
exit(1);
while ((opt = getopt_long(argc, argv, "s:d:Vh", longopts, NULL)) != -1)
switch (opt) {
case 's':
scale_arg = optarg;
break;
case 'd':
dly = atoi(optarg);
break;
case 'V':
display_version();
exit(0);
break;
case 'h':
usage(stdout);
default:
usage(stderr);
}
if (argc > optind)
if ((fd = open(argv[optind], 1)) == -1)
err(EXIT_FAILURE, "can not open tty");
setsize(0);
if (scale_arg)
max_scale = atof(scale_arg);
else
max_scale = nrows;
scale_fact = max_scale;
setjmp(jb);
col = 0;
alrm(0);
while (1) {
if (scale_fact < max_scale)
scale_fact *= 2.0; /* help it drift back up. */
loadavg(&av[0], &av[1], &av[2]);
repeat:
lines = av[0] * scale_fact;
row = nrows - 1;
while (--lines >= 0) {
*(screen + row * ncols + col) = '*';
if (--row < 0) {
scale_fact /= 2.0;
goto repeat;
}
}
while (row >= 0)
*(screen + row-- * ncols + col) = ' ';
for (i = 1;; ++i) {
char *p;
row = nrows - (i * scale_fact);
if (row < 0)
break;
if (*(p = screen + row * ncols + col) == ' ')
*p = '-';
else
*p = '=';
}
if (++col == ncols) {
--col;
memmove(screen, screen + 1, scr_size - 1);
for (row = nrows - 2; row >= 0; --row)
*(screen + row * ncols + col) = ' ';
}
i = sprintf(screen, " %.2f, %.2f, %.2f", av[0], av[1], av[2]);
if (i > 0)
screen[i] = ' ';
write(fd, "\033[H", 3);
write(fd, screen, scr_size - 1);
pause();
} }
if (argc > optind) {
if ((fd = open(argv[optind], 1)) == -1) {
perror("tty");
exit(1);
}
}
setsize(0);
if (scale_arg)
max_scale = atof(scale_arg);
else
max_scale = nrows;
scale_fact = max_scale;
setjmp(jb);
col = 0;
alrm(0);
while (1) {
if (scale_fact < max_scale)
scale_fact *= 2.0; /* help it drift back up. */
loadavg(&av[0], &av[1], &av[2]);
repeat:
lines = av[0] * scale_fact;
row = nrows-1;
while (--lines >= 0) {
*(screen + row * ncols + col) = '*';
if (--row < 0) {
scale_fact /= 2.0;
goto repeat;
}
}
while (row >= 0)
*(screen + row-- * ncols + col) = ' ';
for (i = 1; ; ++i) {
char *p;
row = nrows - (i * scale_fact);
if (row < 0)
break;
if (*(p = screen + row * ncols + col) == ' ')
*p = '-';
else
*p = '=';
}
if (++col == ncols) {
--col;
memmove(screen, screen + 1, scr_size-1);
for(row = nrows-2; row >= 0; --row)
*(screen + row * ncols + col) = ' ';
}
i = sprintf(screen, " %.2f, %.2f, %.2f",
av[0], av[1], av[2]);
if (i>0)
screen[i] = ' ';
write(fd, "\033[H", 3);
write(fd, screen, scr_size - 1);
pause();
}
} }