[BACK]Return to cmds.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/cmds.c, Revision 1.5

1.5     ! millert     1: /*     $OpenBSD: cmds.c,v 1.4 1996/06/26 05:40:04 deraadt Exp $        */
1.2       deraadt     2: /*     $NetBSD: cmds.c,v 1.4 1996/05/10 23:16:32 thorpej Exp $ */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1980, 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)cmds.c     8.2 (Berkeley) 4/29/95";
                     40: #endif
1.5     ! millert    41: static char rcsid[] = "$OpenBSD: cmds.c,v 1.4 1996/06/26 05:40:04 deraadt Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <stdlib.h>
                     45: #include <unistd.h>
                     46: #include <signal.h>
                     47: #include <ctype.h>
                     48: #include <string.h>
                     49: #include "systat.h"
                     50: #include "extern.h"
                     51:
                     52: void
                     53: command(cmd)
                     54:         char *cmd;
                     55: {
                     56:         register struct cmdtab *p;
                     57:         register char *cp;
                     58:        int interval, omask;
                     59:
                     60:        omask = sigblock(sigmask(SIGALRM));
                     61:         for (cp = cmd; *cp && !isspace(*cp); cp++)
                     62:                 ;
                     63:         if (*cp)
                     64:                 *cp++ = '\0';
                     65:        if (*cmd == '\0')
                     66:                return;
                     67:        for (; *cp && isspace(*cp); cp++)
                     68:                ;
                     69:         if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
                     70:                 die(0);
                     71:        if (strcmp(cmd, "load") == 0) {
                     72:                load();
                     73:                goto done;
                     74:        }
                     75:         if (strcmp(cmd, "stop") == 0) {
                     76:                 alarm(0);
                     77:                 mvaddstr(CMDLINE, 0, "Refresh disabled.");
                     78:                 clrtoeol();
                     79:                goto done;
                     80:         }
                     81:        if (strcmp(cmd, "help") == 0) {
                     82:                int col, len;
                     83:
                     84:                move(CMDLINE, col = 0);
                     85:                for (p = cmdtab; p->c_name; p++) {
                     86:                        len = strlen(p->c_name);
                     87:                        if (col + len > COLS)
                     88:                                break;
                     89:                        addstr(p->c_name); col += len;
                     90:                        if (col + 1 < COLS)
                     91:                                addch(' ');
                     92:                }
                     93:                clrtoeol();
                     94:                goto done;
                     95:        }
                     96:        interval = atoi(cmd);
                     97:         if (interval <= 0 &&
                     98:            (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
                     99:                interval = *cp ? atoi(cp) : naptime;
                    100:                 if (interval <= 0) {
                    101:                        error("%d: bad interval.", interval);
                    102:                        goto done;
                    103:                 }
                    104:        }
                    105:        if (interval > 0) {
                    106:                 alarm(0);
                    107:                 naptime = interval;
                    108:                 display(0);
                    109:                 status();
                    110:                goto done;
                    111:         }
                    112:        p = lookup(cmd);
                    113:        if (p == (struct cmdtab *)-1) {
1.3       deraadt   114:                error("%s: Ambiguous command.", cmd);
1.1       deraadt   115:                goto done;
                    116:        }
                    117:         if (p) {
                    118:                 if (curcmd == p)
                    119:                        goto done;
                    120:                 alarm(0);
                    121:                (*curcmd->c_close)(wnd);
                    122:                wnd = (*p->c_open)();
                    123:                if (wnd == 0) {
                    124:                        error("Couldn't open new display");
                    125:                        wnd = (*curcmd->c_open)();
                    126:                        if (wnd == 0) {
                    127:                                error("Couldn't change back to previous cmd");
                    128:                                exit(1);
                    129:                        }
                    130:                        p = curcmd;
                    131:                }
                    132:                if ((p->c_flags & CF_INIT) == 0) {
                    133:                        if ((*p->c_init)())
                    134:                                p->c_flags |= CF_INIT;
                    135:                        else
                    136:                                goto done;
                    137:                }
                    138:                 curcmd = p;
                    139:                labels();
                    140:                 display(0);
                    141:                 status();
                    142:                goto done;
                    143:         }
1.3       deraadt   144:        if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
                    145:                error("%s: Unknown command.", cmd);
1.1       deraadt   146: done:
                    147:        sigsetmask(omask);
                    148: }
                    149:
                    150: struct cmdtab *
                    151: lookup(name)
                    152:        register char *name;
                    153: {
                    154:        register char *p, *q;
                    155:        register struct cmdtab *c, *found;
                    156:        register int nmatches, longest;
                    157:
                    158:        longest = 0;
                    159:        nmatches = 0;
                    160:        found = (struct cmdtab *) 0;
1.5     ! millert   161:        for (c = cmdtab; (p = c->c_name); c++) {
1.1       deraadt   162:                for (q = name; *q == *p++; q++)
                    163:                        if (*q == 0)            /* exact match? */
                    164:                                return (c);
                    165:                if (!*q) {                      /* the name was a prefix */
                    166:                        if (q - name > longest) {
                    167:                                longest = q - name;
                    168:                                nmatches = 1;
                    169:                                found = c;
                    170:                        } else if (q - name == longest)
                    171:                                nmatches++;
                    172:                }
                    173:        }
1.3       deraadt   174:        if (nmatches > 1)
1.1       deraadt   175:                return ((struct cmdtab *)-1);
                    176:        return (found);
                    177: }
                    178:
                    179: void
                    180: status()
                    181: {
                    182:
                    183:         error("Showing %s, refresh every %d seconds.",
                    184:           curcmd->c_name, naptime);
                    185: }
                    186:
                    187: int
                    188: prefix(s1, s2)
                    189:         register char *s1, *s2;
                    190: {
                    191:
                    192:         while (*s1 == *s2) {
                    193:                 if (*s1 == '\0')
                    194:                         return (1);
                    195:                 s1++, s2++;
                    196:         }
                    197:         return (*s1 == '\0');
                    198: }