1 /* $NetBSD: lcmd2.c,v 1.14 2006/12/18 20:04:55 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)lcmd2.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: lcmd2.c,v 1.14 2006/12/18 20:04:55 christos Exp $");
44 #include <sys/types.h>
46 #include <sys/resource.h>
49 #include "window_string.h"
54 int printalias(void *, struct var
*);
55 int printvar(void *, struct var
*);
56 char *strtime(struct timeval
*t
);
59 l_iostat(struct value
*v __unused
, struct value
*a __unused
)
63 if ((w
= openiwin(16, "IO Statistics")) == 0) {
64 error("Can't open statistics window: %s.", wwerror());
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
)) :
77 wwnwrc
? (int) ((float) wwbaud
* (wwnwrc
+ wwntoksave
) /
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
);
105 struct lcmd_arg arg_time
[] = {
106 { "who", 1, ARG_STR
},
111 l_time(struct value
*v __unused
, struct value
*a
)
114 struct rusage rusage
;
115 struct timeval timeval
;
117 if ((w
= openiwin(8, "Timing and Resource Usage")) == 0) {
118 error("Can't open time window: %s.", wwerror());
122 (void) gettimeofday(&timeval
, (struct timezone
*)0);
123 timersub(&timeval
, &starttime
, &timeval
);
124 (void) getrusage(a
->v_type
== V_STR
125 && str_match(a
->v_str
, "children", 1)
126 ? RUSAGE_CHILDREN
: RUSAGE_SELF
, &rusage
);
128 wwprintf(w
, "%-15s %-15s %-15s\n",
129 "time", "utime", "stime");
130 wwprintf(w
, "%-15s ", strtime(&timeval
));
131 wwprintf(w
, "%-15s ", strtime(&rusage
.ru_utime
));
132 wwprintf(w
, "%-15s\n", strtime(&rusage
.ru_stime
));
133 wwprintf(w
, "%-15s %-15s %-15s %-15s\n",
134 "maxrss", "ixrss", "idrss", "isrss");
135 wwprintf(w
, "%-15ld %-15ld %-15ld %-15ld\n",
136 rusage
.ru_maxrss
, rusage
.ru_ixrss
,
137 rusage
.ru_idrss
, rusage
.ru_isrss
);
138 wwprintf(w
, "%-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
139 "minflt", "majflt", "nswap", "inblk", "oublk",
140 "msgsnd", "msgrcv", "nsigs", "nvcsw", "nivcsw");
141 wwprintf(w
, "%-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld\n",
142 rusage
.ru_minflt
, rusage
.ru_majflt
, rusage
.ru_nswap
,
143 rusage
.ru_inblock
, rusage
.ru_oublock
,
144 rusage
.ru_msgsnd
, rusage
.ru_msgrcv
, rusage
.ru_nsignals
,
145 rusage
.ru_nvcsw
, rusage
.ru_nivcsw
);
152 strtime(struct timeval
*t
)
158 if (t
->tv_sec
> 60*60) {
159 (void) sprintf(p
, "%ld:", (long int)t
->tv_sec
/ (60*60));
166 if (t
->tv_sec
> 60) {
167 (void) sprintf(p
, fill
? "%02lld:" : "%lld:",
168 (long long)t
->tv_sec
/ 60);
175 (void) sprintf(p
, fill
? "%02lld.%02ld" : "%lld.%02ld",
176 (long long)t
->tv_sec
, (long)t
->tv_usec
/ 10000);
181 l_list(struct value
*v __unused
, struct value
*a __unused
)
187 for (n
= 0, i
= 0; i
< NWINDOW
; i
++)
191 error("No windows.");
194 if ((w
= openiwin(n
+ 2, "Windows")) == 0) {
195 error("Can't open listing window: %s.", wwerror());
198 for (i
= 0; i
< NWINDOW
; i
++) {
199 if ((wp
= window
[i
]) == 0)
201 wwprintf(w
, "%c %c %-13s %-.*s\n",
202 wp
== selwin
? '+' : (wp
== lastselwin
? '-' : ' '),
204 wp
->ww_state
== WWS_HASPROC
? "" : "(No process)",
206 wp
->ww_label
? wp
->ww_label
: "(No label)");
213 l_variable(struct value
*v __unused
, struct value
*a __unused
)
217 if ((w
= openiwin(wwnrow
- 3, "Variables")) == 0) {
218 error("Can't open variable window: %s.", wwerror());
221 if (var_walk(printvar
, (void *)w
) >= 0)
227 printvar(void *vw
, struct var
*r
)
232 wwprintf(w
, "%16s ", r
->r_name
);
233 switch (r
->r_val
.v_type
) {
235 wwprintf(w
, "%s\n", r
->r_val
.v_str
);
238 wwprintf(w
, "%d\n", r
->r_val
.v_num
);
241 wwprintf(w
, "ERROR\n");
247 struct lcmd_arg arg_def_shell
[] = {
248 { "", 0, ARG_ANY
|ARG_LIST
},
253 l_def_shell(struct value
*v
, struct value
*a
)
258 if (a
->v_type
== V_ERR
) {
259 if ((v
->v_str
= str_cpy(default_shellfile
)) != 0)
263 if ((v
->v_str
= default_shellfile
)) {
265 for (pp
= default_shell
+ 1; *pp
; pp
++) {
270 for (pp
= default_shell
, vp
= a
;
271 vp
->v_type
!= V_ERR
&&
272 pp
< &default_shell
[sizeof default_shell
/sizeof *default_shell
-1];
274 if ((*pp
= vp
->v_type
== V_STR
?
275 str_cpy(vp
->v_str
) : str_itoa(vp
->v_num
)) == 0) {
276 /* just leave default_shell[] the way it is */
280 if ((default_shellfile
= *default_shell
)) {
281 if ((*default_shell
= strrchr(default_shellfile
, '/')))
284 *default_shell
= default_shellfile
;
288 struct lcmd_arg arg_alias
[] = {
290 { "", 0, ARG_STR
|ARG_LIST
},
295 l_alias(struct value
*v
, struct value
*a
)
297 if (a
->v_type
== V_ERR
) {
300 if ((w
= openiwin(wwnrow
- 3, "Aliases")) == 0) {
301 error("Can't open alias window: %s.", wwerror());
304 if (alias_walk(printalias
, (void *)w
) >= 0)
308 struct alias
*ap
= 0;
310 if ((ap
= alias_lookup(a
->v_str
))) {
311 if ((v
->v_str
= str_cpy(ap
->a_buf
)) == 0) {
317 if (a
[1].v_type
== V_STR
) {
323 for (n
= 0, vp
= a
+ 1; vp
->v_type
!= V_ERR
; vp
++, n
++)
324 for (p
= vp
->v_str
; *p
; p
++, n
++)
326 if ((str
= str_alloc(n
)) == 0) {
330 for (q
= str
, vp
= a
+ 1; vp
->v_type
!= V_ERR
;
332 for (p
= vp
->v_str
; (*q
++ = *p
++);)
335 if ((ap
= alias_set(a
[0].v_str
, (char *)0)) == 0) {
346 printalias(void *vw
, struct var
*a
)
351 wwprintf(w
, "%16s %s\n", a
->a_name
, a
->a_buf
);
355 struct lcmd_arg arg_unalias
[] = {
356 { "alias", 1, ARG_STR
},
361 l_unalias(struct value
*v
, struct value
*a
)
363 if (a
->v_type
== ARG_STR
)
364 v
->v_num
= alias_unset(a
->v_str
);
368 struct lcmd_arg arg_echo
[] = {
369 { "window", 1, ARG_NUM
},
370 { "", 0, ARG_ANY
|ARG_LIST
},
375 l_echo(struct value
*v __unused
, struct value
*a
)
380 if ((w
= vtowin(a
++, selwin
)) == 0)
382 while (a
->v_type
!= V_ERR
) {
383 if (a
->v_type
== V_NUM
) {
384 (void) sprintf(buf
, "%d", a
->v_num
);
385 (void) wwwrite(w
, buf
, strlen(buf
));
387 (void) wwwrite(w
, a
->v_str
, strlen(a
->v_str
));
388 if ((++a
)->v_type
!= V_ERR
)
389 (void) wwwrite(w
, " ", 1);
391 (void) wwwrite(w
, "\r\n", 2);