add no_lg.{c,o} to check directly for -lg in f77_abort().
[unix-history] / usr / src / usr.bin / systat / cmds.c
CommitLineData
07ed1e09
KM
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
612f3023 7#ifndef lint
07ed1e09
KM
8static char sccsid[] = "@(#)cmds.c 5.1 (Berkeley) %G%";
9#endif not lint
612f3023
SL
10
11/*
12 * Command support.
13 */
14
15#include "systat.h"
855ff304 16#include <ctype.h>
612f3023
SL
17
18command(cmd)
19 char *cmd;
20{
21 register char *cp;
22 register struct cmdtab *p;
833d578b 23 int interval, omask;
612f3023
SL
24 char *arg;
25
833d578b 26 omask = sigblock(sigmask(SIGALRM));
612f3023
SL
27 for (cp = cmd; *cp && !isspace(*cp); cp++)
28 ;
29 if (*cp)
30 *cp++ = '\0';
acaaf0c4
SL
31 if (*cmd == '\0')
32 return;
855ff304
KM
33 for (; *cp && isspace(*cp); cp++)
34 ;
4fefc2e5 35 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
612f3023 36 die();
612f3023
SL
37 if (strcmp(cmd, "load") == 0) {
38 load();
833d578b 39 goto done;
612f3023 40 }
612f3023
SL
41 if (strcmp(cmd, "stop") == 0) {
42 alarm(0);
855ff304 43 mvaddstr(CMDLINE, 0, "Refresh disabled.");
612f3023 44 clrtoeol();
833d578b 45 goto done;
612f3023 46 }
9c0c7319
SL
47 if (strcmp(cmd, "help") == 0) {
48 int col, len;
612f3023 49
9c0c7319
SL
50 move(CMDLINE, col = 0);
51 for (p = cmdtab; p->c_name; p++) {
52 len = strlen(p->c_name);
53 if (col + len > COLS)
54 break;
55 addstr(p->c_name); col += len;
56 if (col + 1 < COLS)
57 addch(' ');
58 }
59 clrtoeol();
833d578b 60 goto done;
9c0c7319
SL
61 }
62 interval = atoi(cmd);
63 if (interval <= 0 &&
64 (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
65 interval = *cp ? atoi(cp) : naptime;
66 if (interval <= 0) {
67 error("%d: bad interval.", interval);
833d578b 68 goto done;
612f3023 69 }
9c0c7319
SL
70 }
71 if (interval > 0) {
612f3023 72 alarm(0);
9c0c7319 73 naptime = interval;
612f3023
SL
74 display();
75 status();
833d578b 76 goto done;
612f3023 77 }
acaaf0c4
SL
78 p = lookup(cmd);
79 if (p == (struct cmdtab *)-1) {
80 error("%s: Ambiguous command.", cmd);
833d578b 81 goto done;
612f3023 82 }
acaaf0c4
SL
83 if (p) {
84 if (curcmd == p)
833d578b 85 goto done;
acaaf0c4
SL
86 alarm(0);
87 (*curcmd->c_close)(wnd);
88 wnd = (*p->c_open)();
5d60dc8f
SL
89 if (wnd == 0) {
90 error("Couldn't open new display");
91 wnd = (*curcmd->c_open)();
92 if (wnd == 0) {
93 error("Couldn't change back to previous cmd");
94 exit(1);
95 }
96 p = curcmd;
97 }
acaaf0c4 98 curcmd = p;
9c0c7319 99 if ((p->c_flags & CF_INIT) == 0) {
acaaf0c4 100 (*p->c_init)();
9c0c7319 101 p->c_flags |= CF_INIT;
acaaf0c4
SL
102 }
103 labels();
104 display();
105 status();
833d578b 106 goto done;
acaaf0c4 107 }
833d578b
SL
108 if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
109 error("%s: Unknown command.", cmd);
110done:
111 sigsetmask(omask);
612f3023
SL
112}
113
114struct cmdtab *
4fefc2e5 115lookup(name)
612f3023
SL
116 register char *name;
117{
118 register char *p, *q;
119 register struct cmdtab *c, *found;
120 register int nmatches, longest;
121
122 longest = 0;
123 nmatches = 0;
124 found = 0;
125 for (c = cmdtab; p = c->c_name; c++) {
126 for (q = name; *q == *p++; q++)
127 if (*q == 0) /* exact match? */
128 return (c);
129 if (!*q) { /* the name was a prefix */
130 if (q - name > longest) {
131 longest = q - name;
132 nmatches = 1;
133 found = c;
134 } else if (q - name == longest)
135 nmatches++;
136 }
137 }
138 if (nmatches > 1)
139 return ((struct cmdtab *)-1);
140 return (found);
141}
142
143status()
144{
145
855ff304 146 error("Showing %s, refresh every %d seconds.",
612f3023 147 curcmd->c_name, naptime);
612f3023
SL
148}
149
150suspend()
151{
152 int oldmask;
153
154 alarm(0);
855ff304 155 move(CMDLINE, 0);
612f3023
SL
156 refresh();
157 echo();
158 nocrmode();
159 signal(SIGTSTP, SIG_DFL);
160 oldmask = sigsetmask(0);
161 kill(getpid(), SIGTSTP);
162 sigsetmask(oldmask);
163 signal(SIGTSTP, suspend);
164 crmode();
165 noecho();
855ff304 166 move(CMDLINE, col);
612f3023
SL
167 wrefresh(curscr);
168 alarm(naptime);
169}
833d578b
SL
170
171prefix(s1, s2)
172 register char *s1, *s2;
173{
174
175 while (*s1 == *s2) {
176 if (*s1 == '\0')
177 return (1);
178 s1++, s2++;
179 }
180 return (*s1 == '\0');
181}