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

Annotation of src/usr.bin/window/lcmd2.c, Revision 1.1

1.1     ! deraadt     1: /*     $NetBSD: lcmd2.c,v 1.7 1995/09/29 00:44:04 cgd Exp $    */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 1983, 1993
        !             5:  *     The Regents of the University of California.  All rights reserved.
        !             6:  *
        !             7:  * This code is derived from software contributed to Berkeley by
        !             8:  * Edward Wang at The University of California, Berkeley.
        !             9:  *
        !            10:  * Redistribution and use in source and binary forms, with or without
        !            11:  * modification, are permitted provided that the following conditions
        !            12:  * are met:
        !            13:  * 1. Redistributions of source code must retain the above copyright
        !            14:  *    notice, this list of conditions and the following disclaimer.
        !            15:  * 2. Redistributions in binary form must reproduce the above copyright
        !            16:  *    notice, this list of conditions and the following disclaimer in the
        !            17:  *    documentation and/or other materials provided with the distribution.
        !            18:  * 3. All advertising materials mentioning features or use of this software
        !            19:  *    must display the following acknowledgement:
        !            20:  *     This product includes software developed by the University of
        !            21:  *     California, Berkeley and its contributors.
        !            22:  * 4. Neither the name of the University nor the names of its contributors
        !            23:  *    may be used to endorse or promote products derived from this software
        !            24:  *    without specific prior written permission.
        !            25:  *
        !            26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            36:  * SUCH DAMAGE.
        !            37:  */
        !            38:
        !            39: #ifndef lint
        !            40: #if 0
        !            41: static char sccsid[] = "@(#)lcmd2.c    8.1 (Berkeley) 6/6/93";
        !            42: #else
        !            43: static char rcsid[] = "$NetBSD: lcmd2.c,v 1.7 1995/09/29 00:44:04 cgd Exp $";
        !            44: #endif
        !            45: #endif /* not lint */
        !            46:
        !            47: #include "defs.h"
        !            48: #include "string.h"
        !            49: #include "value.h"
        !            50: #include "var.h"
        !            51: #include "lcmd.h"
        !            52: #include "alias.h"
        !            53: #include <sys/types.h>
        !            54: #include <sys/resource.h>
        !            55: #include <string.h>
        !            56:
        !            57: /*ARGSUSED*/
        !            58: l_iostat(v, a)
        !            59: struct value *v, *a;
        !            60: {
        !            61:        register struct ww *w;
        !            62:
        !            63:        if ((w = openiwin(16, "IO Statistics")) == 0) {
        !            64:                error("Can't open statistics window: %s.", wwerror());
        !            65:                return;
        !            66:        }
        !            67:        wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
        !            68:        wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
        !            69:                wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
        !            70:        wwprintf(w, "token\tuse\tbad\tsaving\ttotal\tbaud\n");
        !            71:        wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d/%d (%.1f/%.1f)\n",
        !            72:                wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc,
        !            73:                wwntokc - wwntoksave ?
        !            74:                        (int) ((float) wwbaud * wwntokc /
        !            75:                                        (wwntokc - wwntoksave)) :
        !            76:                        wwbaud,
        !            77:                wwnwrc ? (int) ((float) wwbaud * (wwnwrc + wwntoksave) /
        !            78:                                        wwnwrc) :
        !            79:                        wwbaud,
        !            80:                wwntokc - wwntoksave ?
        !            81:                        (float) wwntokc / (wwntokc - wwntoksave) : 1.0,
        !            82:                wwnwrc ? (float) (wwnwrc + wwntoksave) / wwnwrc : 1.0);
        !            83:        wwprintf(w, "wwwrite\tattempt\tchar\n");
        !            84:        wwprintf(w, "%d\t%d\t%d\n",
        !            85:                wwnwwr, wwnwwra, wwnwwrc);
        !            86:        wwprintf(w, "wwupdat\tline\tmiss\tscan\tclreol\tclreos\tmiss\tline\n");
        !            87:        wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
        !            88:                wwnupdate, wwnupdline, wwnupdmiss, wwnupdscan, wwnupdclreol,
        !            89:                wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline);
        !            90:        wwprintf(w, "select\terror\tzero\n");
        !            91:        wwprintf(w, "%d\t%d\t%d\n",
        !            92:                wwnselect, wwnselecte, wwnselectz);
        !            93:        wwprintf(w, "read\terror\tzero\tchar\tack\tnack\tstat\terrorc\n");
        !            94:        wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
        !            95:                wwnread, wwnreade, wwnreadz, wwnreadc, wwnreadack, wwnreadnack,
        !            96:                wwnreadstat, wwnreadec);
        !            97:        wwprintf(w, "ptyread\terror\tzero\tcontrol\tdata\tchar\n");
        !            98:        wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\n",
        !            99:                wwnwread, wwnwreade, wwnwreadz,
        !           100:                wwnwreadp, wwnwreadd, wwnwreadc);
        !           101:        waitnl(w);
        !           102:        closeiwin(w);
        !           103: }
        !           104:
        !           105: struct lcmd_arg arg_time[] = {
        !           106:        { "who",        1,      ARG_STR },
        !           107:        0
        !           108: };
        !           109:
        !           110: /*ARGSUSED*/
        !           111: l_time(v, a)
        !           112: struct value *v;
        !           113: register struct value *a;
        !           114: {
        !           115:        register struct ww *w;
        !           116:        struct rusage rusage;
        !           117:        struct timeval timeval;
        !           118:        char *strtime();
        !           119:
        !           120:        if ((w = openiwin(8, "Timing and Resource Usage")) == 0) {
        !           121:                error("Can't open time window: %s.", wwerror());
        !           122:                return;
        !           123:        }
        !           124:
        !           125:        (void) gettimeofday(&timeval, (struct timezone *)0);
        !           126:         timersub(&timeval, &starttime, &timeval);
        !           127:        (void) getrusage(a->v_type == V_STR
        !           128:                        && str_match(a->v_str, "children", 1)
        !           129:                ? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
        !           130:
        !           131:        wwprintf(w, "%-15s %-15s %-15s\n",
        !           132:                "time", "utime", "stime");
        !           133:        wwprintf(w, "%-15s ", strtime(&timeval));
        !           134:        wwprintf(w, "%-15s ", strtime(&rusage.ru_utime));
        !           135:        wwprintf(w, "%-15s\n", strtime(&rusage.ru_stime));
        !           136:        wwprintf(w, "%-15s %-15s %-15s %-15s\n",
        !           137:                "maxrss", "ixrss", "idrss", "isrss");
        !           138:        wwprintf(w, "%-15ld %-15ld %-15ld %-15ld\n",
        !           139:                rusage.ru_maxrss, rusage.ru_ixrss,
        !           140:                rusage.ru_idrss, rusage.ru_isrss);
        !           141:        wwprintf(w, "%-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
        !           142:                "minflt", "majflt", "nswap", "inblk", "oublk",
        !           143:                "msgsnd", "msgrcv", "nsigs", "nvcsw", "nivcsw");
        !           144:        wwprintf(w, "%-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld\n",
        !           145:                rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
        !           146:                rusage.ru_inblock, rusage.ru_oublock,
        !           147:                rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
        !           148:                rusage.ru_nvcsw, rusage.ru_nivcsw);
        !           149:
        !           150:        waitnl(w);
        !           151:        closeiwin(w);
        !           152: }
        !           153:
        !           154: char *
        !           155: strtime(t)
        !           156: register struct timeval *t;
        !           157: {
        !           158:        char fill = 0;
        !           159:        static char buf[20];
        !           160:        register char *p = buf;
        !           161:
        !           162:        if (t->tv_sec > 60*60) {
        !           163:                (void) sprintf(p, "%ld:", t->tv_sec / (60*60));
        !           164:                while (*p++)
        !           165:                        ;
        !           166:                p--;
        !           167:                t->tv_sec %= 60*60;
        !           168:                fill++;
        !           169:        }
        !           170:        if (t->tv_sec > 60) {
        !           171:                (void) sprintf(p, fill ? "%02ld:" : "%ld:", t->tv_sec / 60);
        !           172:                while (*p++)
        !           173:                        ;
        !           174:                p--;
        !           175:                t->tv_sec %= 60;
        !           176:                fill++;
        !           177:        }
        !           178:        (void) sprintf(p, fill ? "%02ld.%02d" : "%ld.%02ld",
        !           179:                t->tv_sec, t->tv_usec / 10000);
        !           180:        return buf;
        !           181: }
        !           182:
        !           183: /*ARGSUSED*/
        !           184: l_list(v, a)
        !           185: struct value *v, *a;
        !           186: {
        !           187:        register struct ww *w, *wp;
        !           188:        register i;
        !           189:        int n;
        !           190:
        !           191:        for (n = 0, i = 0; i < NWINDOW; i++)
        !           192:                if (window[i] != 0)
        !           193:                        n++;
        !           194:        if (n == 0) {
        !           195:                error("No windows.");
        !           196:                return;
        !           197:        }
        !           198:        if ((w = openiwin(n + 2, "Windows")) == 0) {
        !           199:                error("Can't open listing window: %s.", wwerror());
        !           200:                return;
        !           201:        }
        !           202:        for (i = 0; i < NWINDOW; i++) {
        !           203:                if ((wp = window[i]) == 0)
        !           204:                        continue;
        !           205:                wwprintf(w, "%c %c %-13s %-.*s\n",
        !           206:                        wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
        !           207:                        i + '1',
        !           208:                        wp->ww_state == WWS_HASPROC ? "" : "(No process)",
        !           209:                        wwncol - 20,
        !           210:                        wp->ww_label ? wp->ww_label : "(No label)");
        !           211:        }
        !           212:        waitnl(w);
        !           213:        closeiwin(w);
        !           214: }
        !           215:
        !           216: /*ARGSUSED*/
        !           217: l_variable(v, a)
        !           218: struct value *v, *a;
        !           219: {
        !           220:        register struct ww *w;
        !           221:        int printvar();
        !           222:
        !           223:        if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
        !           224:                error("Can't open variable window: %s.", wwerror());
        !           225:                return;
        !           226:        }
        !           227:        if (var_walk(printvar, (long)w) >= 0)
        !           228:                waitnl(w);
        !           229:        closeiwin(w);
        !           230: }
        !           231:
        !           232: printvar(w, r)
        !           233: register struct ww *w;
        !           234: register struct var *r;
        !           235: {
        !           236:        if (more(w, 0) == 2)
        !           237:                return -1;
        !           238:        wwprintf(w, "%16s    ", r->r_name);
        !           239:        switch (r->r_val.v_type) {
        !           240:        case V_STR:
        !           241:                wwprintf(w, "%s\n", r->r_val.v_str);
        !           242:                break;
        !           243:        case V_NUM:
        !           244:                wwprintf(w, "%d\n", r->r_val.v_num);
        !           245:                break;
        !           246:        case V_ERR:
        !           247:                wwprintf(w, "ERROR\n");
        !           248:                break;
        !           249:        }
        !           250:        return 0;
        !           251: }
        !           252:
        !           253: struct lcmd_arg arg_def_shell[] = {
        !           254:        { "",   0,              ARG_ANY|ARG_LIST },
        !           255:        0
        !           256: };
        !           257:
        !           258: l_def_shell(v, a)
        !           259:        struct value *v, *a;
        !           260: {
        !           261:        register char **pp;
        !           262:        register struct value *vp;
        !           263:
        !           264:        if (a->v_type == V_ERR) {
        !           265:                if ((v->v_str = str_cpy(default_shellfile)) != 0)
        !           266:                        v->v_type = V_STR;
        !           267:                return;
        !           268:        }
        !           269:        if (v->v_str = default_shellfile) {
        !           270:                v->v_type = V_STR;
        !           271:                for (pp = default_shell + 1; *pp; pp++) {
        !           272:                        str_free(*pp);
        !           273:                        *pp = 0;
        !           274:                }
        !           275:        }
        !           276:        for (pp = default_shell, vp = a;
        !           277:             vp->v_type != V_ERR &&
        !           278:             pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
        !           279:             pp++, vp++)
        !           280:                if ((*pp = vp->v_type == V_STR ?
        !           281:                     str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
        !           282:                        /* just leave default_shell[] the way it is */
        !           283:                        p_memerror();
        !           284:                        break;
        !           285:                }
        !           286:        if (default_shellfile = *default_shell)
        !           287:                if (*default_shell = rindex(default_shellfile, '/'))
        !           288:                        (*default_shell)++;
        !           289:                else
        !           290:                        *default_shell = default_shellfile;
        !           291: }
        !           292:
        !           293: struct lcmd_arg arg_alias[] = {
        !           294:        { "",   0,              ARG_STR },
        !           295:        { "",   0,              ARG_STR|ARG_LIST },
        !           296:        0
        !           297: };
        !           298:
        !           299: l_alias(v, a)
        !           300:        struct value *v, *a;
        !           301: {
        !           302:        if (a->v_type == V_ERR) {
        !           303:                register struct ww *w;
        !           304:                int printalias();
        !           305:
        !           306:                if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
        !           307:                        error("Can't open alias window: %s.", wwerror());
        !           308:                        return;
        !           309:                }
        !           310:                if (alias_walk(printalias, (long)w) >= 0)
        !           311:                        waitnl(w);
        !           312:                closeiwin(w);
        !           313:        } else {
        !           314:                register struct alias *ap = 0;
        !           315:
        !           316:                if (ap = alias_lookup(a->v_str)) {
        !           317:                        if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
        !           318:                                p_memerror();
        !           319:                                return;
        !           320:                        }
        !           321:                        v->v_type = V_STR;
        !           322:                }
        !           323:                if (a[1].v_type == V_STR) {
        !           324:                        register struct value *vp;
        !           325:                        register char *p, *q;
        !           326:                        char *str;
        !           327:                        register n;
        !           328:
        !           329:                        for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
        !           330:                                for (p = vp->v_str; *p; p++, n++)
        !           331:                                        ;
        !           332:                        if ((str = str_alloc(n)) == 0) {
        !           333:                                p_memerror();
        !           334:                                return;
        !           335:                        }
        !           336:                        for (q = str, vp = a + 1; vp->v_type != V_ERR;
        !           337:                             vp++, q[-1] = ' ')
        !           338:                                for (p = vp->v_str; *q++ = *p++;)
        !           339:                                        ;
        !           340:                        q[-1] = 0;
        !           341:                        if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
        !           342:                                p_memerror();
        !           343:                                str_free(str);
        !           344:                                return;
        !           345:                        }
        !           346:                        ap->a_buf = str;
        !           347:                }
        !           348:        }
        !           349: }
        !           350:
        !           351: printalias(w, a)
        !           352: register struct ww *w;
        !           353: register struct alias *a;
        !           354: {
        !           355:        if (more(w, 0) == 2)
        !           356:                return -1;
        !           357:        wwprintf(w, "%16s    %s\n", a->a_name, a->a_buf);
        !           358:        return 0;
        !           359: }
        !           360:
        !           361: struct lcmd_arg arg_unalias[] = {
        !           362:        { "alias",      1,      ARG_STR },
        !           363:        0
        !           364: };
        !           365:
        !           366: l_unalias(v, a)
        !           367: struct value *v, *a;
        !           368: {
        !           369:        if (a->v_type == ARG_STR)
        !           370:                v->v_num = alias_unset(a->v_str);
        !           371:        v->v_type = V_NUM;
        !           372: }
        !           373:
        !           374: struct lcmd_arg arg_echo[] = {
        !           375:        { "window",     1,      ARG_NUM },
        !           376:        { "",           0,      ARG_ANY|ARG_LIST },
        !           377:        0
        !           378: };
        !           379:
        !           380: /*ARGSUSED*/
        !           381: l_echo(v, a)
        !           382: struct value *v;
        !           383: register struct value *a;
        !           384: {
        !           385:        char buf[20];
        !           386:        struct ww *w;
        !           387:
        !           388:        if ((w = vtowin(a++, selwin)) == 0)
        !           389:                return;
        !           390:        while (a->v_type != V_ERR) {
        !           391:                if (a->v_type == V_NUM) {
        !           392:                        (void) sprintf(buf, "%d", a->v_num);
        !           393:                        (void) wwwrite(w, buf, strlen(buf));
        !           394:                } else
        !           395:                        (void) wwwrite(w, a->v_str, strlen(a->v_str));
        !           396:                if ((++a)->v_type != V_ERR)
        !           397:                        (void) wwwrite(w, " ", 1);
        !           398:        }
        !           399:        (void) wwwrite(w, "\r\n", 2);
        !           400: }