2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)lcmd2.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/window/lcmd2.c,v 1.2.6.2 2001/05/17 09:45:00 obrien Exp $
38 * $DragonFly: src/usr.bin/window/lcmd2.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
42 #include <string.h> /* System string definitions. */
43 #include "mystring.h" /* Local string definitions. */
48 #include <sys/types.h>
49 #include <sys/resource.h>
55 register struct ww *w;
57 if ((w = openiwin(16, "IO Statistics")) == 0) {
58 error("Can't open statistics window: %s.", wwerror());
61 wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
62 wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
63 wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
64 wwprintf(w, "token\tuse\tbad\tsaving\ttotal\tbaud\n");
65 wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d/%d (%.1f/%.1f)\n",
66 wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc,
67 wwntokc - wwntoksave ?
68 (int) ((float) wwbaud * wwntokc /
69 (wwntokc - wwntoksave)) :
71 wwnwrc ? (int) ((float) wwbaud * (wwnwrc + wwntoksave) /
74 wwntokc - wwntoksave ?
75 (float) wwntokc / (wwntokc - wwntoksave) : 1.0,
76 wwnwrc ? (float) (wwnwrc + wwntoksave) / wwnwrc : 1.0);
77 wwprintf(w, "wwwrite\tattempt\tchar\n");
78 wwprintf(w, "%d\t%d\t%d\n",
79 wwnwwr, wwnwwra, wwnwwrc);
80 wwprintf(w, "wwupdat\tline\tmiss\tscan\tclreol\tclreos\tmiss\tline\n");
81 wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
82 wwnupdate, wwnupdline, wwnupdmiss, wwnupdscan, wwnupdclreol,
83 wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline);
84 wwprintf(w, "select\terror\tzero\n");
85 wwprintf(w, "%d\t%d\t%d\n",
86 wwnselect, wwnselecte, wwnselectz);
87 wwprintf(w, "read\terror\tzero\tchar\tack\tnack\tstat\terrorc\n");
88 wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
89 wwnread, wwnreade, wwnreadz, wwnreadc, wwnreadack, wwnreadnack,
90 wwnreadstat, wwnreadec);
91 wwprintf(w, "ptyread\terror\tzero\tcontrol\tdata\tchar\n");
92 wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\n",
93 wwnwread, wwnwreade, wwnwreadz,
94 wwnwreadp, wwnwreadd, wwnwreadc);
99 struct lcmd_arg arg_time[] = {
100 { "who", 1, ARG_STR },
107 register struct value *a;
109 register struct ww *w;
110 struct rusage rusage;
111 struct timeval timeval;
114 if ((w = openiwin(8, "Timing and Resource Usage")) == 0) {
115 error("Can't open time window: %s.", wwerror());
119 (void) gettimeofday(&timeval, (struct timezone *)0);
120 timeval.tv_sec -= starttime.tv_sec;
121 if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
123 timeval.tv_usec += 1000000;
125 (void) getrusage(a->v_type == V_STR
126 && str_match(a->v_str, "children", 1)
127 ? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
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);
154 register struct timeval *t;
158 register char *p = buf;
160 if (t->tv_sec > 60*60) {
161 (void) sprintf(p, "%ld:", t->tv_sec / (60*60));
168 if (t->tv_sec > 60) {
169 (void) sprintf(p, fill ? "%02ld:" : "%ld:", t->tv_sec / 60);
176 (void) sprintf(p, fill ? "%02ld.%02d" : "%ld.%02ld",
177 t->tv_sec, t->tv_usec / 10000);
185 register struct ww *w, *wp;
189 for (n = 0, i = 0; i < NWINDOW; i++)
193 error("No windows.");
196 if ((w = openiwin(n + 2, "Windows")) == 0) {
197 error("Can't open listing window: %s.", wwerror());
200 for (i = 0; i < NWINDOW; i++) {
201 if ((wp = window[i]) == 0)
203 wwprintf(w, "%c %c %-13s %-.*s\n",
204 wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
206 wp->ww_state == WWS_HASPROC ? "" : "(No process)",
208 wp->ww_label ? wp->ww_label : "(No label)");
218 register struct ww *w;
221 if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
222 error("Can't open variable window: %s.", wwerror());
225 if (var_walk(printvar, (long)w) >= 0)
231 register struct ww *w;
232 register struct var *r;
236 wwprintf(w, "%16s ", r->r_name);
237 switch (r->r_val.v_type) {
239 wwprintf(w, "%s\n", r->r_val.v_str);
242 wwprintf(w, "%d\n", r->r_val.v_num);
245 wwprintf(w, "ERROR\n");
251 struct lcmd_arg arg_def_shell[] = {
252 { "", 0, ARG_ANY|ARG_LIST },
260 register struct value *vp;
262 if (a->v_type == V_ERR) {
263 if ((v->v_str = str_cpy(default_shellfile)) != 0)
267 if (v->v_str = default_shellfile) {
269 for (pp = default_shell + 1; *pp; pp++) {
274 for (pp = default_shell, vp = a;
275 vp->v_type != V_ERR &&
276 pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
278 if ((*pp = vp->v_type == V_STR ?
279 str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
280 /* just leave default_shell[] the way it is */
284 if (default_shellfile = *default_shell)
285 if (*default_shell = rindex(default_shellfile, '/'))
288 *default_shell = default_shellfile;
291 struct lcmd_arg arg_alias[] = {
293 { "", 0, ARG_STR|ARG_LIST },
300 if (a->v_type == V_ERR) {
301 register struct ww *w;
304 if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
305 error("Can't open alias window: %s.", wwerror());
308 if (alias_walk(printalias, (long)w) >= 0)
312 register struct alias *ap = 0;
314 if (ap = alias_lookup(a->v_str)) {
315 if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
321 if (a[1].v_type == V_STR) {
322 register struct value *vp;
323 register char *p, *q;
327 for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
328 for (p = vp->v_str; *p; p++, n++)
330 if ((str = str_alloc(n)) == 0) {
334 for (q = str, vp = a + 1; vp->v_type != V_ERR;
336 for (p = vp->v_str; *q++ = *p++;)
339 if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
350 register struct ww *w;
351 register struct alias *a;
355 wwprintf(w, "%16s %s\n", a->a_name, a->a_buf);
359 struct lcmd_arg arg_unalias[] = {
360 { "alias", 1, ARG_STR },
367 if (a->v_type == ARG_STR)
368 v->v_num = alias_unset(a->v_str);
372 struct lcmd_arg arg_echo[] = {
373 { "window", 1, ARG_NUM },
374 { "", 0, ARG_ANY|ARG_LIST },
381 register struct value *a;
386 if ((w = vtowin(a++, selwin)) == 0)
388 while (a->v_type != V_ERR) {
389 if (a->v_type == V_NUM) {
390 (void) sprintf(buf, "%d", a->v_num);
391 (void) wwwrite(w, buf, strlen(buf));
393 (void) wwwrite(w, a->v_str, strlen(a->v_str));
394 if ((++a)->v_type != V_ERR)
395 (void) wwwrite(w, " ", 1);
397 (void) wwwrite(w, "\r\n", 2);