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