1ff6c33c9SBruce Evans /*- 2ff6c33c9SBruce Evans * Copyright (c) 1980, 1992, 1993 3ff6c33c9SBruce Evans * The Regents of the University of California. All rights reserved. 4ff6c33c9SBruce Evans * 5ff6c33c9SBruce Evans * Redistribution and use in source and binary forms, with or without 6ff6c33c9SBruce Evans * modification, are permitted provided that the following conditions 7ff6c33c9SBruce Evans * are met: 8ff6c33c9SBruce Evans * 1. Redistributions of source code must retain the above copyright 9ff6c33c9SBruce Evans * notice, this list of conditions and the following disclaimer. 10ff6c33c9SBruce Evans * 2. Redistributions in binary form must reproduce the above copyright 11ff6c33c9SBruce Evans * notice, this list of conditions and the following disclaimer in the 12ff6c33c9SBruce Evans * documentation and/or other materials provided with the distribution. 13ff6c33c9SBruce Evans * 4. Neither the name of the University nor the names of its contributors 14ff6c33c9SBruce Evans * may be used to endorse or promote products derived from this software 15ff6c33c9SBruce Evans * without specific prior written permission. 16ff6c33c9SBruce Evans * 17ff6c33c9SBruce Evans * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18ff6c33c9SBruce Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19ff6c33c9SBruce Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ff6c33c9SBruce Evans * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21ff6c33c9SBruce Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22ff6c33c9SBruce Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23ff6c33c9SBruce Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24ff6c33c9SBruce Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25ff6c33c9SBruce Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26ff6c33c9SBruce Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27ff6c33c9SBruce Evans * SUCH DAMAGE. 28ff6c33c9SBruce Evans */ 29ff6c33c9SBruce Evans 309ff712b0SMark Murray #include <sys/cdefs.h> 31ff6c33c9SBruce Evans 329ff712b0SMark Murray __FBSDID("$FreeBSD$"); 339ff712b0SMark Murray 349ff712b0SMark Murray #ifdef lint 359ff712b0SMark Murray static const char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/29/95"; 369ff712b0SMark Murray #endif 379ff712b0SMark Murray 388b3daf89SAlexander V. Chernikov #include <sys/param.h> 398b3daf89SAlexander V. Chernikov 40ff6c33c9SBruce Evans #include <ctype.h> 419ff712b0SMark Murray #include <signal.h> 429ff712b0SMark Murray #include <stdlib.h> 43ff6c33c9SBruce Evans #include <string.h> 449ff712b0SMark Murray #include <unistd.h> 459ff712b0SMark Murray 46ff6c33c9SBruce Evans #include "systat.h" 47ff6c33c9SBruce Evans #include "extern.h" 48ff6c33c9SBruce Evans 49ff6c33c9SBruce Evans void 5093b9f504SXin LI command(const char *cmd) 51ff6c33c9SBruce Evans { 529ff712b0SMark Murray struct cmdtab *p; 539ff712b0SMark Murray char *cp, *tmpstr, *tmpstr1; 548b3daf89SAlexander V. Chernikov double t; 55ff6c33c9SBruce Evans 569ff712b0SMark Murray tmpstr = tmpstr1 = strdup(cmd); 579ff712b0SMark Murray for (cp = tmpstr1; *cp && !isspace(*cp); cp++) 58ff6c33c9SBruce Evans ; 59ff6c33c9SBruce Evans if (*cp) 60ff6c33c9SBruce Evans *cp++ = '\0'; 619ff712b0SMark Murray if (*tmpstr1 == '\0') 62ff6c33c9SBruce Evans return; 63ff6c33c9SBruce Evans for (; *cp && isspace(*cp); cp++) 64ff6c33c9SBruce Evans ; 659ff712b0SMark Murray if (strcmp(tmpstr1, "quit") == 0 || strcmp(tmpstr1, "q") == 0) 66ff6c33c9SBruce Evans die(0); 679ff712b0SMark Murray if (strcmp(tmpstr1, "load") == 0) { 68ff6c33c9SBruce Evans load(); 69ff6c33c9SBruce Evans goto done; 70ff6c33c9SBruce Evans } 719ff712b0SMark Murray if (strcmp(tmpstr1, "stop") == 0) { 728b3daf89SAlexander V. Chernikov delay = 0; 73ff6c33c9SBruce Evans mvaddstr(CMDLINE, 0, "Refresh disabled."); 74ff6c33c9SBruce Evans clrtoeol(); 75ff6c33c9SBruce Evans goto done; 76ff6c33c9SBruce Evans } 779ff712b0SMark Murray if (strcmp(tmpstr1, "help") == 0) { 789ff712b0SMark Murray int _col, _len; 79ff6c33c9SBruce Evans 809ff712b0SMark Murray move(CMDLINE, _col = 0); 81ff6c33c9SBruce Evans for (p = cmdtab; p->c_name; p++) { 829ff712b0SMark Murray _len = strlen(p->c_name); 839ff712b0SMark Murray if (_col + _len > COLS) 84ff6c33c9SBruce Evans break; 859ff712b0SMark Murray addstr(p->c_name); _col += _len; 869ff712b0SMark Murray if (_col + 1 < COLS) 87ff6c33c9SBruce Evans addch(' '); 88ff6c33c9SBruce Evans } 89ff6c33c9SBruce Evans clrtoeol(); 90ff6c33c9SBruce Evans goto done; 91ff6c33c9SBruce Evans } 928b3daf89SAlexander V. Chernikov t = strtod(tmpstr1, NULL) * 1000000.0; 938b3daf89SAlexander V. Chernikov if (t > 0 && t < (double)UINT_MAX) 948b3daf89SAlexander V. Chernikov delay = (unsigned int)t; 958b3daf89SAlexander V. Chernikov if ((t <= 0 || t > (double)UINT_MAX) && 968b3daf89SAlexander V. Chernikov (strcmp(tmpstr1, "start") == 0 || 978b3daf89SAlexander V. Chernikov strcmp(tmpstr1, "interval") == 0)) { 988b3daf89SAlexander V. Chernikov if (*cp != '\0') { 998b3daf89SAlexander V. Chernikov t = strtod(cp, NULL) * 1000000.0; 1008b3daf89SAlexander V. Chernikov if (t <= 0 || t >= (double)UINT_MAX) { 1018b3daf89SAlexander V. Chernikov error("%d: bad interval.", (int)t); 102ff6c33c9SBruce Evans goto done; 103ff6c33c9SBruce Evans } 104ff6c33c9SBruce Evans } 1058b3daf89SAlexander V. Chernikov } 1068b3daf89SAlexander V. Chernikov if (t > 0) { 1078b3daf89SAlexander V. Chernikov delay = (unsigned int)t; 1088b3daf89SAlexander V. Chernikov display(); 109ff6c33c9SBruce Evans status(); 110ff6c33c9SBruce Evans goto done; 111ff6c33c9SBruce Evans } 1129ff712b0SMark Murray p = lookup(tmpstr1); 113ff6c33c9SBruce Evans if (p == (struct cmdtab *)-1) { 1149ff712b0SMark Murray error("%s: Ambiguous command.", tmpstr1); 115ff6c33c9SBruce Evans goto done; 116ff6c33c9SBruce Evans } 117ff6c33c9SBruce Evans if (p) { 118ff6c33c9SBruce Evans if (curcmd == p) 119ff6c33c9SBruce Evans goto done; 120ff6c33c9SBruce Evans (*curcmd->c_close)(wnd); 121319e2a24SPoul-Henning Kamp curcmd->c_flags &= ~CF_INIT; 122ff6c33c9SBruce Evans wnd = (*p->c_open)(); 123*61c2ed54SMarcelo Araujo if (wnd == NULL) { 124ff6c33c9SBruce Evans error("Couldn't open new display"); 125ff6c33c9SBruce Evans wnd = (*curcmd->c_open)(); 126*61c2ed54SMarcelo Araujo if (wnd == NULL) { 127ff6c33c9SBruce Evans error("Couldn't change back to previous cmd"); 128ff6c33c9SBruce Evans exit(1); 129ff6c33c9SBruce Evans } 130ff6c33c9SBruce Evans p = curcmd; 131ff6c33c9SBruce Evans } 132ff6c33c9SBruce Evans if ((p->c_flags & CF_INIT) == 0) { 133ff6c33c9SBruce Evans if ((*p->c_init)()) 134ff6c33c9SBruce Evans p->c_flags |= CF_INIT; 135ff6c33c9SBruce Evans else 136ff6c33c9SBruce Evans goto done; 137ff6c33c9SBruce Evans } 138ff6c33c9SBruce Evans curcmd = p; 139ff6c33c9SBruce Evans labels(); 1408b3daf89SAlexander V. Chernikov display(); 141ff6c33c9SBruce Evans status(); 142ff6c33c9SBruce Evans goto done; 143ff6c33c9SBruce Evans } 144*61c2ed54SMarcelo Araujo if (curcmd->c_cmd == NULL || !(*curcmd->c_cmd)(tmpstr1, cp)) 1459ff712b0SMark Murray error("%s: Unknown command.", tmpstr1); 146ff6c33c9SBruce Evans done: 1479ff712b0SMark Murray free(tmpstr); 148ff6c33c9SBruce Evans } 149ff6c33c9SBruce Evans 150ff6c33c9SBruce Evans struct cmdtab * 15193b9f504SXin LI lookup(const char *name) 152ff6c33c9SBruce Evans { 1539ff712b0SMark Murray const char *p, *q; 1549ff712b0SMark Murray struct cmdtab *ct, *found; 1559ff712b0SMark Murray int nmatches, longest; 156ff6c33c9SBruce Evans 157ff6c33c9SBruce Evans longest = 0; 158ff6c33c9SBruce Evans nmatches = 0; 159ff6c33c9SBruce Evans found = (struct cmdtab *) 0; 1609ff712b0SMark Murray for (ct = cmdtab; (p = ct->c_name); ct++) { 161ff6c33c9SBruce Evans for (q = name; *q == *p++; q++) 162ff6c33c9SBruce Evans if (*q == 0) /* exact match? */ 1639ff712b0SMark Murray return (ct); 164ff6c33c9SBruce Evans if (!*q) { /* the name was a prefix */ 165ff6c33c9SBruce Evans if (q - name > longest) { 166ff6c33c9SBruce Evans longest = q - name; 167ff6c33c9SBruce Evans nmatches = 1; 1689ff712b0SMark Murray found = ct; 169ff6c33c9SBruce Evans } else if (q - name == longest) 170ff6c33c9SBruce Evans nmatches++; 171ff6c33c9SBruce Evans } 172ff6c33c9SBruce Evans } 173237f5336SBruce Evans if (nmatches > 1) 174ff6c33c9SBruce Evans return ((struct cmdtab *)-1); 175ff6c33c9SBruce Evans return (found); 176ff6c33c9SBruce Evans } 177ff6c33c9SBruce Evans 178ff6c33c9SBruce Evans void 17993b9f504SXin LI status(void) 180ff6c33c9SBruce Evans { 181ff6c33c9SBruce Evans 182ff6c33c9SBruce Evans error("Showing %s, refresh every %d seconds.", 1838b3daf89SAlexander V. Chernikov curcmd->c_name, delay / 1000000); 184ff6c33c9SBruce Evans } 185ff6c33c9SBruce Evans 186ff6c33c9SBruce Evans int 18793b9f504SXin LI prefix(const char *s1, const char *s2) 188ff6c33c9SBruce Evans { 189ff6c33c9SBruce Evans 190ff6c33c9SBruce Evans while (*s1 == *s2) { 191ff6c33c9SBruce Evans if (*s1 == '\0') 192ff6c33c9SBruce Evans return (1); 193ff6c33c9SBruce Evans s1++, s2++; 194ff6c33c9SBruce Evans } 195ff6c33c9SBruce Evans return (*s1 == '\0'); 196ff6c33c9SBruce Evans } 197