[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.9

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