#include <sys/resource.h>
#include <sys/signal.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "commands.h"
#include "top.h"
#include "machine.h"
static int err_compar(const void *p1, const void *p2);
struct errs
{
int errnum;
char *arg;
};
static char *err_string(void);
static int str_adderr(char *str, int len, int err);
static int str_addarg(char *str, int len, char *arg, bool first);
const struct command all_commands[] =
{
{' ', "update the display", false, CMD_update},
{'/', "filter on command name (+ selects all commands)", false, CMD_grep},
{'a', "toggle the display of process titles", false, CMD_showargs},
{'C', "toggle the display of raw or weighted CPU percentage", false, CMD_wcputog},
{'d', "change number of displays to show", false, CMD_displays},
{'e', "list errors generated by last \"kill\" or \"renice\" command", false, CMD_errors},
{'H', "toggle the display of threads", false, CMD_thrtog},
{'h', "show this help text", true, CMD_help},
{'?', NULL, true, CMD_help},
{'i', "toggle the display of idle processes", false, CMD_idletog},
{'I', NULL, false, CMD_idletog},
{'J', "display processes for only one jail (+ selects all jails)", false, CMD_jail},
{'j', "toggle the display of jail ID", false, CMD_jidtog},
{'k', "kill processes; send a signal to a list of processes", false, CMD_kill},
{'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog},
{'n', "change number of processes to display", false, CMD_number},
{'#', NULL, false, CMD_number},
{'o', "specify the sort order", false, CMD_order},
{'P', "toggle the display of per-CPU statistics", false, CMD_pcputog},
{'p', "display one process (+ selects all processes)", false, CMD_pid},
{'q', "quit" , true, CMD_quit},
{'r', "renice a process", false, CMD_renice},
{'S', "toggle the display of system processes", false, CMD_viewsys},
{'s', "change number of seconds to delay between updates", false, CMD_delay},
{'T', "toggle the display of thread IDs", false, CMD_toggletid},
{'t', "toggle the display of this process", false, CMD_selftog},
{'u', "display processes for only one user (+ selects all users)", false, CMD_user},
{'w', "toggle the display of swap use for each process", false, CMD_swaptog},
{'z', "toggle the display of the system idle process", false, CMD_kidletog},
{0, NULL, true, CMD_NONE}
};
void
show_help(void)
{
const struct command *curcmd, *nextcmd;
char keys[8] = "";
_Static_assert(sizeof(keys) >= sizeof("a or b"), "keys right size");
printf("Top version FreeBSD, %s\n", copyright);
curcmd = all_commands;
while (curcmd->c != 0) {
if (overstrike && !curcmd->available_to_dumb) {
++curcmd;
continue;
}
if (curcmd->desc == NULL) {
++curcmd;
continue;
}
nextcmd = curcmd + 1;
if (nextcmd->desc == NULL && nextcmd->c != '\0') {
sprintf(keys, "%c or %c", curcmd->c, nextcmd->c);
} else if (curcmd->c == ' '){
sprintf(keys, "space");
} else {
sprintf(keys, "%c", curcmd->c);
}
printf("%s\t- %s\n", keys, curcmd->desc);
++curcmd;
}
if (overstrike)
{
fputs("\
Other commands are also available, but this terminal is not\n\
sophisticated enough to handle those commands gracefully.\n", stdout);
}
}
static char *
next_field(char *str)
{
if ((str = strchr(str, ' ')) == NULL)
{
return(NULL);
}
*str = '\0';
while (*++str == ' ') ;
return(*str == '\0' ? NULL : str);
}
static int
scanint(char *str, int *intp)
{
int val = 0;
char ch;
if (*str == '\0')
{
return(-1);
}
while ((ch = *str++) != '\0')
{
if (isdigit(ch))
{
val = val * 10 + (ch - '0');
}
else if (isspace(ch))
{
break;
}
else
{
return(-1);
}
}
*intp = val;
return(0);
}
#define ERRMAX 20
static struct errs errs[ERRMAX];
static int errcnt;
static char err_toomany[] = " too many errors occurred";
static char err_listem[] =
" Many errors occurred. Press `e' to display the list of errors.";
#define ERR_RESET errcnt = 0
#define ERROR(p, e) if (errcnt >= ERRMAX) \
{ \
return(err_toomany); \
} \
else \
{ \
errs[errcnt].arg = (p); \
errs[errcnt++].errnum = (e); \
}
#define STRMAX 80
char *
err_string(void)
{
struct errs *errp;
int cnt = 0;
bool first = true;
int currerr = -1;
int stringlen;
static char string[STRMAX];
if (errcnt == 0)
{
return(NULL);
}
qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
string[0] = ' ';
string[1] = '\0';
stringlen = STRMAX - 2;
while (cnt < errcnt)
{
errp = &(errs[cnt++]);
if (errp->errnum != currerr)
{
if (currerr >= 0)
{
if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
{
return(err_listem);
}
strcat(string, "; ");
}
currerr = errp->errnum;
first = true;
}
if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
{
return(err_listem);
}
first = false;
}
stringlen = str_adderr(string, stringlen, currerr);
return(stringlen == 0 ? err_listem : string);
}
static int
str_adderr(char *str, int len, int err)
{
const char *msg;
int msglen;
msg = err == 0 ? "Not a number" : strerror(err);
msglen = strlen(msg) + 2;
if (len <= msglen)
{
return(0);
}
strcat(str, ": ");
strcat(str, msg);
return(len - msglen);
}
static int
str_addarg(char str[], int len, char arg[], bool first)
{
int arglen;
arglen = strlen(arg);
if (!first)
{
arglen += 2;
}
if (len <= arglen)
{
return(0);
}
if (!first)
{
strcat(str, ", ");
}
strcat(str, arg);
return(len - arglen);
}
static int
err_compar(const void *p1, const void *p2)
{
int result;
const struct errs * const g1 = (const struct errs * const)p1;
const struct errs * const g2 = (const struct errs * const)p2;
if ((result = g1->errnum - g2->errnum) == 0)
{
return(strcmp(g1->arg, g2->arg));
}
return(result);
}
int
error_count(void)
{
return(errcnt);
}
void
show_errors(void)
{
int cnt = 0;
struct errs *errp = errs;
printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
while (cnt++ < errcnt)
{
printf("%5s: %s\n", errp->arg,
errp->errnum == 0 ? "Not a number" : strerror(errp->errnum));
errp++;
}
}
static const char no_proc_specified[] = " no processes specified";
static const char invalid_signal_number[] = " invalid_signal_number";
static const char bad_signal_name[] = " bad signal name";
static const char bad_pri_value[] = " bad priority value";
static int
signame_to_signum(const char * sig)
{
int n;
if (strncasecmp(sig, "SIG", 3) == 0)
sig += 3;
for (n = 1; n < sys_nsig; n++) {
if (!strcasecmp(sys_signame[n], sig))
return (n);
}
return (-1);
}
const char *
kill_procs(char *str)
{
char *nptr;
int signum = SIGTERM;
int procnum;
ERR_RESET;
while (isspace(*str)) str++;
if (str[0] == '-')
{
if ((nptr = next_field(str)) == NULL)
{
return(no_proc_specified);
}
if (isdigit(str[1]))
{
scanint(str + 1, &signum);
if (signum <= 0 || signum >= NSIG)
{
return(invalid_signal_number);
}
}
else
{
signum = signame_to_signum(str + 1);
if (signum == -1 )
{
return(bad_signal_name);
}
}
str = nptr;
}
do
{
if (scanint(str, &procnum) == -1)
{
ERROR(str, 0);
}
else
{
if (kill(procnum, signum) == -1)
{
ERROR(str, errno);
}
}
} while ((str = next_field(str)) != NULL);
return(err_string());
}
const char *
renice_procs(char *str)
{
char negate;
int prio;
int procnum;
ERR_RESET;
if ((negate = (*str == '-')) != 0)
{
str++;
}
procnum = scanint(str, &prio);
if (negate)
{
prio = -prio;
}
if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
{
return(bad_pri_value);
}
if ((str = next_field(str)) == NULL)
{
return(no_proc_specified);
}
do
{
if (scanint(str, &procnum) == -1)
{
ERROR(str, 0);
}
if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
{
ERROR(str, errno);
}
} while ((str = next_field(str)) != NULL);
return(err_string());
}