1 /**	$MirOS: src/usr.bin/window/lcmd2.c,v 1.2 2005/03/13 18:34:06 tg Exp $ */
2 /*	$OpenBSD: lcmd2.c,v 1.9 2003/08/01 22:01:37 david Exp $	*/
3 /*	$NetBSD: lcmd2.c,v 1.7 1995/09/29 00:44:04 cgd Exp $	*/
4 
5 /*
6  * Copyright (c) 1983, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Edward Wang at The University of California, Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. 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 #include "defs.h"
38 #include "string.h"
39 #include "value.h"
40 #include "var.h"
41 #include "lcmd.h"
42 #include "alias.h"
43 #include <sys/types.h>
44 #include <sys/resource.h>
45 #include <stdio.h>
46 #include <string.h>
47 
48 __SCCSID("@(#)lcmd2.c	8.1 (Berkeley) 6/6/93");
49 __RCSID("$MirOS: src/usr.bin/window/lcmd2.c,v 1.2 2005/03/13 18:34:06 tg Exp $");
50 
51 /*ARGSUSED*/
52 l_iostat(v, a)
53 struct value *v, *a;
54 {
55 	struct ww *w;
56 
57 	if ((w = openiwin(16, "IO Statistics")) == 0) {
58 		error("Can't open statistics window: %s.", wwerror());
59 		return;
60 	}
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)) :
70 			wwbaud,
71 		wwnwrc ? (int) ((float) wwbaud * (wwnwrc + wwntoksave) /
72 					wwnwrc) :
73 			wwbaud,
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);
95 	waitnl(w);
96 	closeiwin(w);
97 }
98 
99 struct lcmd_arg arg_time[] = {
100 	{ "who",	1,	ARG_STR },
101 	0
102 };
103 
104 /*ARGSUSED*/
105 l_time(v, a)
106 struct value *v;
107 struct value *a;
108 {
109 	struct ww *w;
110 	struct rusage rusage;
111 	struct timeval timeval;
112 	char *strtime();
113 
114 	if ((w = openiwin(8, "Timing and Resource Usage")) == 0) {
115 		error("Can't open time window: %s.", wwerror());
116 		return;
117 	}
118 
119 	(void) gettimeofday(&timeval, (struct timezone *)0);
120         timersub(&timeval, &starttime, &timeval);
121 	(void) getrusage(a->v_type == V_STR
122 			&& str_match(a->v_str, "children", 1)
123 		? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
124 
125 	wwprintf(w, "%-15s %-15s %-15s\n",
126 		"time", "utime", "stime");
127 	wwprintf(w, "%-15s ", strtime(&timeval));
128 	wwprintf(w, "%-15s ", strtime(&rusage.ru_utime));
129 	wwprintf(w, "%-15s\n", strtime(&rusage.ru_stime));
130 	wwprintf(w, "%-15s %-15s %-15s %-15s\n",
131 		"maxrss", "ixrss", "idrss", "isrss");
132 	wwprintf(w, "%-15ld %-15ld %-15ld %-15ld\n",
133 		rusage.ru_maxrss, rusage.ru_ixrss,
134 		rusage.ru_idrss, rusage.ru_isrss);
135 	wwprintf(w, "%-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
136 		"minflt", "majflt", "nswap", "inblk", "oublk",
137 		"msgsnd", "msgrcv", "nsigs", "nvcsw", "nivcsw");
138 	wwprintf(w, "%-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld\n",
139 		rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
140 		rusage.ru_inblock, rusage.ru_oublock,
141 		rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
142 		rusage.ru_nvcsw, rusage.ru_nivcsw);
143 
144 	waitnl(w);
145 	closeiwin(w);
146 }
147 
148 char *
strtime(t)149 strtime(t)
150 struct timeval *t;
151 {
152 	char fill = 0;
153 	static char buf[20];
154 	char *p = buf;
155 
156 	if (t->tv_sec > 60*60) {
157 		(void) snprintf(p, buf + sizeof buf - p,
158 			"%lld:", (int64_t)t->tv_sec / (60*60));
159 		while (*p++)
160 			;
161 		p--;
162 		t->tv_sec %= 60*60;
163 		fill++;
164 	}
165 	if (t->tv_sec > 60) {
166 		(void) snprintf(p, buf + sizeof buf - p,
167 			fill ? "%02lld:" : "%lld:", (int64_t)t->tv_sec / 60);
168 		while (*p++)
169 			;
170 		p--;
171 		t->tv_sec %= 60;
172 		fill++;
173 	}
174 	(void) snprintf(p, buf + sizeof buf - p,
175 		fill ? "%02lld.%02ld" : "%lld.%02ld",
176 		(int64_t)t->tv_sec, (long)t->tv_usec / 10000L);
177 	return buf;
178 }
179 
180 /*ARGSUSED*/
181 l_list(v, a)
182 struct value *v, *a;
183 {
184 	struct ww *w, *wp;
185 	int i;
186 	int n;
187 
188 	for (n = 0, i = 0; i < NWINDOW; i++)
189 		if (window[i] != 0)
190 			n++;
191 	if (n == 0) {
192 		error("No windows.");
193 		return;
194 	}
195 	if ((w = openiwin(n + 2, "Windows")) == 0) {
196 		error("Can't open listing window: %s.", wwerror());
197 		return;
198 	}
199 	for (i = 0; i < NWINDOW; i++) {
200 		if ((wp = window[i]) == 0)
201 			continue;
202 		wwprintf(w, "%c %c %-13s %-.*s\n",
203 			wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
204 			i + '1',
205 			wp->ww_state == WWS_HASPROC ? "" : "(No process)",
206 			wwncol - 20,
207 			wp->ww_label ? wp->ww_label : "(No label)");
208 	}
209 	waitnl(w);
210 	closeiwin(w);
211 }
212 
213 /*ARGSUSED*/
214 l_variable(v, a)
215 struct value *v, *a;
216 {
217 	struct ww *w;
218 	int printvar();
219 
220 	if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
221 		error("Can't open variable window: %s.", wwerror());
222 		return;
223 	}
224 	if (var_walk(printvar, (long)w) >= 0)
225 		waitnl(w);
226 	closeiwin(w);
227 }
228 
229 printvar(w, r)
230 struct ww *w;
231 struct var *r;
232 {
233 	if (more(w, 0) == 2)
234 		return -1;
235 	wwprintf(w, "%16s    ", r->r_name);
236 	switch (r->r_val.v_type) {
237 	case V_STR:
238 		wwprintf(w, "%s\n", r->r_val.v_str);
239 		break;
240 	case V_NUM:
241 		wwprintf(w, "%d\n", r->r_val.v_num);
242 		break;
243 	case V_ERR:
244 		wwprintf(w, "ERROR\n");
245 		break;
246 	}
247 	return 0;
248 }
249 
250 struct lcmd_arg arg_def_shell[] = {
251 	{ "",	0,		ARG_ANY|ARG_LIST },
252 	0
253 };
254 
255 l_def_shell(v, a)
256 	struct value *v, *a;
257 {
258 	char **pp;
259 	struct value *vp;
260 
261 	if (a->v_type == V_ERR) {
262 		if ((v->v_str = str_cpy(default_shellfile)) != 0)
263 			v->v_type = V_STR;
264 		return;
265 	}
266 	if (v->v_str = default_shellfile) {
267 		v->v_type = V_STR;
268 		for (pp = default_shell + 1; *pp; pp++) {
269 			str_free(*pp);
270 			*pp = 0;
271 		}
272 	}
273 	for (pp = default_shell, vp = a;
274 	     vp->v_type != V_ERR &&
275 	     pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
276 	     pp++, vp++)
277 		if ((*pp = vp->v_type == V_STR ?
278 		     str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
279 			/* just leave default_shell[] the way it is */
280 			p_memerror();
281 			break;
282 		}
283 	if (default_shellfile = *default_shell)
284 		if (*default_shell = strrchr(default_shellfile, '/'))
285 			(*default_shell)++;
286 		else
287 			*default_shell = default_shellfile;
288 }
289 
290 struct lcmd_arg arg_alias[] = {
291 	{ "",	0,		ARG_STR },
292 	{ "",	0,		ARG_STR|ARG_LIST },
293 	0
294 };
295 
296 l_alias(v, a)
297 	struct value *v, *a;
298 {
299 	if (a->v_type == V_ERR) {
300 		struct ww *w;
301 		int printalias();
302 
303 		if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
304 			error("Can't open alias window: %s.", wwerror());
305 			return;
306 		}
307 		if (alias_walk(printalias, (long)w) >= 0)
308 			waitnl(w);
309 		closeiwin(w);
310 	} else {
311 		struct alias *ap = 0;
312 
313 		if (ap = alias_lookup(a->v_str)) {
314 			if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
315 				p_memerror();
316 				return;
317 			}
318 			v->v_type = V_STR;
319 		}
320 		if (a[1].v_type == V_STR) {
321 			struct value *vp;
322 			char *p, *q;
323 			char *str;
324 			int n;
325 
326 			for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
327 				for (p = vp->v_str; *p; p++, n++)
328 					;
329 			if ((str = str_alloc(n)) == 0) {
330 				p_memerror();
331 				return;
332 			}
333 			for (q = str, vp = a + 1; vp->v_type != V_ERR;
334 			     vp++, q[-1] = ' ')
335 				for (p = vp->v_str; *q++ = *p++;)
336 					;
337 			q[-1] = 0;
338 			if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
339 				p_memerror();
340 				str_free(str);
341 				return;
342 			}
343 			ap->a_buf = str;
344 		}
345 	}
346 }
347 
348 printalias(w, a)
349 struct ww *w;
350 struct alias *a;
351 {
352 	if (more(w, 0) == 2)
353 		return -1;
354 	wwprintf(w, "%16s    %s\n", a->a_name, a->a_buf);
355 	return 0;
356 }
357 
358 struct lcmd_arg arg_unalias[] = {
359 	{ "alias",	1,	ARG_STR },
360 	0
361 };
362 
363 l_unalias(v, a)
364 struct value *v, *a;
365 {
366 	if (a->v_type == ARG_STR)
367 		v->v_num = alias_unset(a->v_str);
368 	v->v_type = V_NUM;
369 }
370 
371 struct lcmd_arg arg_echo[] = {
372 	{ "window",	1,	ARG_NUM },
373 	{ "",		0,	ARG_ANY|ARG_LIST },
374 	0
375 };
376 
377 /*ARGSUSED*/
378 l_echo(v, a)
379 struct value *v;
380 struct value *a;
381 {
382 	char buf[20];
383 	struct ww *w;
384 
385 	if ((w = vtowin(a++, selwin)) == 0)
386 		return;
387 	while (a->v_type != V_ERR) {
388 		if (a->v_type == V_NUM) {
389 			(void) snprintf(buf, sizeof(buf), "%d", a->v_num);
390 			(void) wwwrite(w, buf, strlen(buf));
391 		} else
392 			(void) wwwrite(w, a->v_str, strlen(a->v_str));
393 		if ((++a)->v_type != V_ERR)
394 			(void) wwwrite(w, " ", 1);
395 	}
396 	(void) wwwrite(w, "\r\n", 2);
397 }
398