char *copyright =
"Copyright (c) 1984 through 2007, William LeFebvre";
/*
* Top users/processes display for Unix
*
* This program may be freely redistributed,
* but this entire comment MUST remain intact.
*
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
* Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
* Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
* Copyright (c) 1996, William LeFebvre, Group sys Consulting
* Copyright (c) 2007-2019, Mark Wong
*/
/*
* See the file "HISTORY" for information on version-to-version changes.
*/
/*
* This file contains "main" and other high-level routines.
*/
/*
* The following preprocessor variables, when defined, are used to
* distinguish between different Unix implementations:
*
* FD_SET - macros FD_SET and FD_ZERO are used when defined
*/
#include "os.h"
#include <signal.h>
#include <setjmp.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
/* determine which type of signal functions to use */
#ifdef HAVE_SIGACTION
#undef HAVE_SIGHOLD
#else
#if !defined(HAVE_SIGHOLD) || !defined(HAVE_SIGRELSE)
#define BSD_SIGNALS
#endif
#endif
/* includes specific to top */
#include "pg_top.h"
#include "remote.h"
#include "commands.h"
#include "display.h" /* interface to display package */
#include "screen.h" /* interface to screen package */
#include "boolean.h"
#include "utils.h"
#include "version.h"
#ifdef ENABLE_COLOR
#include "color.h"
#endif
#include "port.h"
/* Size of the stdio buffer given to stdout */
#define Buffersize 2048
/* The buffer that stdio will use */
char stdoutbuf[Buffersize];
/* build signal masks */
#ifndef sigmask
#define sigmask(s) (1 << ((s) - 1))
#endif
/* for getopt: */
extern int optind;
extern char *optarg;
/* imported from screen.c */
extern int overstrike;
/* values which need to be accessed by signal handlers */
int max_topn; /* maximum displayable processes */
/* miscellaneous things */
char *myname = "pg_top";
jmp_buf jmp_int;
/* internal variables */
static const char *progname = "pg_top";
void process_commands(struct pg_top_context *);
static void usage(const char *progname);
/* List of all the options available */
static struct option long_options[] = {
{"batch", no_argument, NULL, 'b'},
{"show-command", no_argument, NULL, 'c'},
{"color-mode", no_argument, NULL, 'C'},
{"interactive", no_argument, NULL, 'i'},
{"hide-idle", no_argument, NULL, 'I'},
{"non-interactive", no_argument, NULL, 'n'},
{"order-field", required_argument, NULL, 'o'},
{"remote-mode", no_argument, NULL, 'r'},
{"set-delay", required_argument, NULL, 's'},
{"show-tags", no_argument, NULL, 'T'},
{"version", no_argument, NULL, 'V'},
{"set-display", required_argument, NULL, 'x'},
{"show-username", required_argument, NULL, 'z'},
{"help", no_argument, NULL, '?'},
{"dbname", required_argument, NULL, 'd'},
{"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{"username", required_argument, NULL, 'U'},
{"password", no_argument, NULL, 'W'},
{NULL, 0, NULL, 0}
};
/* pointers to display routines */
void (*d_loadave) (int, double *) = i_loadave;
void (*d_minibar) (
int (*) (char *, int)) = i_minibar;
void (*d_uptime) (time_t *, time_t *) = i_uptime;
void (*d_procstates) (int, int *) = i_procstates;
void (*d_cpustates) (int64_t *) = i_cpustates;
void (*d_memory) (long *) = i_memory;
void (*d_swap) (long *) = i_swap;
void (*d_db) (struct db_info *) = i_db;
void (*d_io) (struct io_info *) = i_io;
void (*d_message) () = i_message;
void (*d_process) (int, char *) = i_process;
/*
* usage - print help message with details about commands
*/
static void
usage(const char *progname)
{
printf("%s monitors a PostgreSQL database cluster.\n\n", progname);
printf("Usage:\n");
printf(" %s [OPTION]... [COUNT]\n", progname);
printf("\nGeneral options:\n");
printf(" -b, --batch use batch mode\n