Add a 'b' option to cgtop, equivalent to the same option in top

[zj: use static]
This commit is contained in:
David Strauss 2012-07-25 16:33:07 -07:00 committed by Zbigniew Jędrzejewski-Szmek
parent a152771af1
commit e66bb58bed

View File

@ -56,6 +56,7 @@ typedef struct Group {
static unsigned arg_depth = 3; static unsigned arg_depth = 3;
static unsigned arg_iterations = 0; static unsigned arg_iterations = 0;
static bool arg_batch = false;
static usec_t arg_delay = 1*USEC_PER_SEC; static usec_t arg_delay = 1*USEC_PER_SEC;
static enum { static enum {
@ -507,6 +508,7 @@ static void help(void) {
" -i Order by IO load\n" " -i Order by IO load\n"
" -d --delay=DELAY Specify delay\n" " -d --delay=DELAY Specify delay\n"
" -n --iterations=N Run for N iterations before exiting\n" " -n --iterations=N Run for N iterations before exiting\n"
" -b --batch Run in batch mode, accepting no input\n"
" --depth=DEPTH Maximum traversal depth (default: 2)\n", " --depth=DEPTH Maximum traversal depth (default: 2)\n",
program_invocation_short_name); program_invocation_short_name);
} }
@ -521,6 +523,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "delay", required_argument, NULL, 'd' }, { "delay", required_argument, NULL, 'd' },
{ "iterations", required_argument, NULL, 'n' }, { "iterations", required_argument, NULL, 'n' },
{ "batch", no_argument, NULL, 'b' },
{ "depth", required_argument, NULL, ARG_DEPTH }, { "depth", required_argument, NULL, ARG_DEPTH },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
@ -531,7 +534,7 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 1); assert(argc >= 1);
assert(argv); assert(argv);
while ((c = getopt_long(argc, argv, "hptcmin:d:", options, NULL)) >= 0) { while ((c = getopt_long(argc, argv, "hptcmin:bd:", options, NULL)) >= 0) {
switch (c) { switch (c) {
@ -566,6 +569,10 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case 'b':
arg_batch = true;
break;
case 'p': case 'p':
arg_order = ORDER_PATH; arg_order = ORDER_PATH;
break; break;
@ -655,17 +662,25 @@ int main(int argc, char *argv[]) {
if (arg_iterations && iteration >= arg_iterations) if (arg_iterations && iteration >= arg_iterations)
break; break;
r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL); if (arg_batch) {
if (r == -ETIMEDOUT) usleep(last_refresh + arg_delay - t);
continue; } else {
if (r < 0) { r = read_one_char(stdin, &key,
log_error("Couldn't read key: %s", strerror(-r)); last_refresh + arg_delay - t, NULL);
goto finish; if (r == -ETIMEDOUT)
continue;
if (r < 0) {
log_error("Couldn't read key: %s", strerror(-r));
goto finish;
}
} }
fputs("\r \r", stdout); fputs("\r \r", stdout);
fflush(stdout); fflush(stdout);
if (arg_batch)
continue;
switch (key) { switch (key) {
case ' ': case ' ':