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 7 #ifndef lint 8 static char sccsid[] = "@(#)cmds.c 5.5 (Berkeley) 03/26/87"; 9 #endif not lint 10 11 /* 12 * Command support. 13 */ 14 15 #include "systat.h" 16 #include <ctype.h> 17 18 command(cmd) 19 char *cmd; 20 { 21 register char *cp; 22 register struct cmdtab *p; 23 int interval, omask; 24 extern (*sigtstpdfl)(); 25 26 omask = sigblock(sigmask(SIGALRM)); 27 for (cp = cmd; *cp && !isspace(*cp); cp++) 28 ; 29 if (*cp) 30 *cp++ = '\0'; 31 if (*cmd == '\0') 32 return; 33 for (; *cp && isspace(*cp); cp++) 34 ; 35 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0) 36 die(); 37 if (strcmp(cmd, "load") == 0) { 38 load(); 39 goto done; 40 } 41 if (strcmp(cmd, "stop") == 0) { 42 alarm(0); 43 mvaddstr(CMDLINE, 0, "Refresh disabled."); 44 clrtoeol(); 45 goto done; 46 } 47 if (strcmp(cmd, "help") == 0) { 48 int col, len; 49 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(); 60 goto done; 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); 68 goto done; 69 } 70 } 71 if (interval > 0) { 72 alarm(0); 73 naptime = interval; 74 display(); 75 status(); 76 goto done; 77 } 78 p = lookup(cmd); 79 if (p == (struct cmdtab *)-1) { 80 error("%s: Ambiguous command.", cmd); 81 goto done; 82 } 83 if (p) { 84 if (curcmd == p) 85 goto done; 86 alarm(0); 87 (*curcmd->c_close)(wnd); 88 wnd = (*p->c_open)(); 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 } 98 if ((p->c_flags & CF_INIT) == 0) { 99 if ((*p->c_init)()) 100 p->c_flags |= CF_INIT; 101 else 102 goto done; 103 } 104 curcmd = p; 105 labels(); 106 display(); 107 status(); 108 goto done; 109 } 110 if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp)) 111 error("%s: Unknown command.", cmd); 112 done: 113 sigsetmask(omask); 114 } 115 116 struct cmdtab * 117 lookup(name) 118 register char *name; 119 { 120 register char *p, *q; 121 register struct cmdtab *c, *found; 122 register int nmatches, longest; 123 124 longest = 0; 125 nmatches = 0; 126 found = (struct cmdtab *) 0; 127 for (c = cmdtab; p = c->c_name; c++) { 128 for (q = name; *q == *p++; q++) 129 if (*q == 0) /* exact match? */ 130 return (c); 131 if (!*q) { /* the name was a prefix */ 132 if (q - name > longest) { 133 longest = q - name; 134 nmatches = 1; 135 found = c; 136 } else if (q - name == longest) 137 nmatches++; 138 } 139 } 140 if (nmatches > 1) 141 return ((struct cmdtab *)-1); 142 return (found); 143 } 144 145 status() 146 { 147 148 error("Showing %s, refresh every %d seconds.", 149 curcmd->c_name, naptime); 150 } 151 152 suspend() 153 { 154 int oldmask; 155 156 alarm(0); 157 move(CMDLINE, 0); 158 refresh(); 159 echo(); 160 nocrmode(); 161 signal(SIGTSTP, sigtstpdfl); 162 oldmask = sigsetmask(0); 163 kill(getpid(), SIGTSTP); 164 sigsetmask(oldmask); 165 signal(SIGTSTP, suspend); 166 crmode(); 167 noecho(); 168 move(CMDLINE, col); 169 alarm(naptime); 170 } 171 172 prefix(s1, s2) 173 register char *s1, *s2; 174 { 175 176 while (*s1 == *s2) { 177 if (*s1 == '\0') 178 return (1); 179 s1++, s2++; 180 } 181 return (*s1 == '\0'); 182 } 183