date and time created 83/10/02 21:41:00 by sam
[unix-history] / usr / src / usr.bin / systat / cmds.c
CommitLineData
612f3023
SL
1#ifndef lint
2static char sccsid[] = "@(#)cmds.c 1.1 (Lucasfilm) %G%";
3#endif
4
5/*
6 * Command support.
7 */
8
9#include "systat.h"
10
11command(cmd)
12 char *cmd;
13{
14 register char *cp;
15 register struct cmdtab *p;
16 char *arg;
17
18 for (cp = cmd; *cp && !isspace(*cp); cp++)
19 ;
20 if (*cp)
21 *cp++ = '\0';
22 if (strcmp(cmd, "quit") == 0)
23 die();
24 if (strcmp(cmd, "status") == 0 || strcmp(cmd, "help") == 0) {
25 status();
26 return;
27 }
28 if (strcmp(cmd, "load") == 0) {
29 load();
30 return;
31 }
32 p = getcmd(cmd);
33 if (p != (struct cmdtab *)-1) {
34 if (curcmd == p)
35 return;
36 alarm(0);
37 (*curcmd->c_close)(wnd);
38 wnd = (*p->c_open)();
39 curcmd = p;
40 if (p->c_flags == 0) {
41 (*p->c_init)();
42 p->c_flags = 1;
43 }
44 labels();
45 display();
46 status();
47 return;
48 }
49 if (strcmp(cmd, "stop") == 0) {
50 alarm(0);
51 mvaddstr(22, 0, "Refresh disabled.");
52 clrtoeol();
53 return;
54 }
55 /* commands with arguments */
56 for (; *cp && isspace(*cp); cp++)
57 ;
58 if (strcmp(cmd, "start") == 0) {
59 int x;
60
61 if (*cp == '\0')
62 x = naptime;
63 else
64 x = atoi(cp);
65 if (x <= 0) {
66 mvprintw(22, 0, "%d: bad interval.", x);
67 clrtoeol();
68 return;
69 }
70 alarm(0);
71 naptime = x;
72 display();
73 status();
74 return;
75 }
76 if (*cmd) {
77 mvprintw(22, 0, "%s: Unknown command.", cmd);
78 clrtoeol();
79 }
80}
81
82struct cmdtab *
83getcmd(name)
84 register char *name;
85{
86 register char *p, *q;
87 register struct cmdtab *c, *found;
88 register int nmatches, longest;
89
90 longest = 0;
91 nmatches = 0;
92 found = 0;
93 for (c = cmdtab; p = c->c_name; c++) {
94 for (q = name; *q == *p++; q++)
95 if (*q == 0) /* exact match? */
96 return (c);
97 if (!*q) { /* the name was a prefix */
98 if (q - name > longest) {
99 longest = q - name;
100 nmatches = 1;
101 found = c;
102 } else if (q - name == longest)
103 nmatches++;
104 }
105 }
106 if (nmatches > 1)
107 return ((struct cmdtab *)-1);
108 return (found);
109}
110
111status()
112{
113
114 mvprintw(22, 0, "Showing %s, refresh every %d seconds.",
115 curcmd->c_name, naptime);
116 clrtoeol();
117}
118
119suspend()
120{
121 int oldmask;
122
123 alarm(0);
124 move(22, 0);
125 refresh();
126 echo();
127 nocrmode();
128 signal(SIGTSTP, SIG_DFL);
129 oldmask = sigsetmask(0);
130 kill(getpid(), SIGTSTP);
131 sigsetmask(oldmask);
132 signal(SIGTSTP, suspend);
133 crmode();
134 noecho();
135 move(22, col);
136 wrefresh(curscr);
137 alarm(naptime);
138}