xref: /original-bsd/usr.bin/window/lcmd2.c (revision b7261a4b)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)lcmd2.c	3.23 (Berkeley) 06/17/90";
13 #endif /* not lint */
14 
15 #include "defs.h"
16 #include "string.h"
17 #include "value.h"
18 #include "var.h"
19 #include "lcmd.h"
20 #include <sys/resource.h>
21 #include "alias.h"
22 
23 /*ARGSUSED*/
24 l_iostat(v, a)
25 struct value *v, *a;
26 {
27 	register struct ww *w;
28 
29 	if ((w = openiwin(16, "IO Statistics")) == 0) {
30 		error("Can't open statistics window: %s.", wwerror());
31 		return;
32 	}
33 	wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
34 	wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
35 		wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
36 	wwprintf(w, "token\tuse\tbad\tsaving\ttotal\tbaud\n");
37 	wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d/%d (%.1f/%.1f)\n",
38 		wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc,
39 		wwntokc - wwntoksave ?
40 			(int) ((float) wwbaud * wwntokc /
41 					(wwntokc - wwntoksave)) :
42 			wwbaud,
43 		wwnwrc ? (int) ((float) wwbaud * (wwnwrc + wwntoksave) /
44 					wwnwrc) :
45 			wwbaud,
46 		wwntokc - wwntoksave ?
47 			(float) wwntokc / (wwntokc - wwntoksave) : 1.0,
48 		wwnwrc ? (float) (wwnwrc + wwntoksave) / wwnwrc : 1.0);
49 	wwprintf(w, "wwwrite\tattempt\tchar\n");
50 	wwprintf(w, "%d\t%d\t%d\n",
51 		wwnwwr, wwnwwra, wwnwwrc);
52 	wwprintf(w, "wwupdat\tline\tmiss\tscan\tclreol\tclreos\tmiss\tline\n");
53 	wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
54 		wwnupdate, wwnupdline, wwnupdmiss, wwnupdscan, wwnupdclreol,
55 		wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline);
56 	wwprintf(w, "select\terror\tzero\n");
57 	wwprintf(w, "%d\t%d\t%d\n",
58 		wwnselect, wwnselecte, wwnselectz);
59 	wwprintf(w, "read\terror\tzero\tchar\n");
60 	wwprintf(w, "%d\t%d\t%d\t%d\n",
61 		wwnread, wwnreade, wwnreadz, wwnreadc);
62 	wwprintf(w, "ptyread\terror\tzero\tcontrol\tdata\tchar\n");
63 	wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\n",
64 		wwnwread, wwnwreade, wwnwreadz,
65 		wwnwreadp, wwnwreadd, wwnwreadc);
66 	waitnl(w);
67 	closeiwin(w);
68 }
69 
70 struct lcmd_arg arg_time[] = {
71 	{ "who",	1,	ARG_STR },
72 	0
73 };
74 
75 /*ARGSUSED*/
76 l_time(v, a)
77 struct value *v;
78 register struct value *a;
79 {
80 	register struct ww *w;
81 	struct rusage rusage;
82 	struct timeval timeval;
83 	char *strtime();
84 
85 	if ((w = openiwin(8, "Timing and Resource Usage")) == 0) {
86 		error("Can't open time window: %s.", wwerror());
87 		return;
88 	}
89 
90 	(void) gettimeofday(&timeval, (struct timezone *)0);
91 	timeval.tv_sec -= starttime.tv_sec;
92 	if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
93 		timeval.tv_sec--;
94 		timeval.tv_usec += 1000000;
95 	}
96 	(void) getrusage(a->v_type == V_STR
97 			&& str_match(a->v_str, "children", 1)
98 		? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
99 
100 	wwprintf(w, "%-15s %-15s %-15s\n",
101 		"time", "utime", "stime");
102 	wwprintf(w, "%-15s ", strtime(&timeval));
103 	wwprintf(w, "%-15s ", strtime(&rusage.ru_utime));
104 	wwprintf(w, "%-15s\n", strtime(&rusage.ru_stime));
105 	wwprintf(w, "%-15s %-15s %-15s %-15s\n",
106 		"maxrss", "ixrss", "idrss", "isrss");
107 	wwprintf(w, "%-15ld %-15ld %-15ld %-15ld\n",
108 		rusage.ru_maxrss, rusage.ru_ixrss,
109 		rusage.ru_idrss, rusage.ru_isrss);
110 	wwprintf(w, "%-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
111 		"minflt", "majflt", "nswap", "inblk", "oublk",
112 		"msgsnd", "msgrcv", "nsigs", "nvcsw", "nivcsw");
113 	wwprintf(w, "%-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld\n",
114 		rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
115 		rusage.ru_inblock, rusage.ru_oublock,
116 		rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
117 		rusage.ru_nvcsw, rusage.ru_nivcsw);
118 
119 	waitnl(w);
120 	closeiwin(w);
121 }
122 
123 char *
124 strtime(t)
125 register struct timeval *t;
126 {
127 	char fill = 0;
128 	static char buf[20];
129 	register char *p = buf;
130 
131 	if (t->tv_sec > 60*60) {
132 		(void) sprintf(p, "%ld:", t->tv_sec / (60*60));
133 		while (*p++)
134 			;
135 		p--;
136 		t->tv_sec %= 60*60;
137 		fill++;
138 	}
139 	if (t->tv_sec > 60) {
140 		(void) sprintf(p, fill ? "%02ld:" : "%ld:", t->tv_sec / 60);
141 		while (*p++)
142 			;
143 		p--;
144 		t->tv_sec %= 60;
145 		fill++;
146 	}
147 	(void) sprintf(p, fill ? "%02ld.%02d" : "%ld.%02ld",
148 		t->tv_sec, t->tv_usec / 10000);
149 	return buf;
150 }
151 
152 /*ARGSUSED*/
153 l_list(v, a)
154 struct value *v, *a;
155 {
156 	register struct ww *w, *wp;
157 	register i;
158 	int n;
159 
160 	for (n = 0, i = 0; i < NWINDOW; i++)
161 		if (window[i] != 0)
162 			n++;
163 	if (n == 0) {
164 		error("No windows.");
165 		return;
166 	}
167 	if ((w = openiwin(n + 2, "Windows")) == 0) {
168 		error("Can't open listing window: %s.", wwerror());
169 		return;
170 	}
171 	for (i = 0; i < NWINDOW; i++) {
172 		if ((wp = window[i]) == 0)
173 			continue;
174 		wwprintf(w, "%c %c %-13s %-.*s\n",
175 			wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
176 			i + '1',
177 			wp->ww_state == WWS_HASPROC ? "" : "(No process)",
178 			wwncol - 20,
179 			wp->ww_label ? wp->ww_label : "(No label)");
180 	}
181 	waitnl(w);
182 	closeiwin(w);
183 }
184 
185 /*ARGSUSED*/
186 l_variable(v, a)
187 struct value *v, *a;
188 {
189 	register struct ww *w;
190 	int printvar();
191 
192 	if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
193 		error("Can't open variable window: %s.", wwerror());
194 		return;
195 	}
196 	if (var_walk(printvar, (int)w) >= 0)
197 		waitnl(w);
198 	closeiwin(w);
199 }
200 
201 printvar(w, r)
202 register struct ww *w;
203 register struct var *r;
204 {
205 	if (more(w, 0) == 2)
206 		return -1;
207 	wwprintf(w, "%16s    ", r->r_name);
208 	switch (r->r_val.v_type) {
209 	case V_STR:
210 		wwprintf(w, "%s\n", r->r_val.v_str);
211 		break;
212 	case V_NUM:
213 		wwprintf(w, "%d\n", r->r_val.v_num);
214 		break;
215 	case V_ERR:
216 		wwprintf(w, "ERROR\n");
217 		break;
218 	}
219 	return 0;
220 }
221 
222 struct lcmd_arg arg_def_shell[] = {
223 	{ "",	0,		ARG_ANY|ARG_LIST },
224 	0
225 };
226 
227 l_def_shell(v, a)
228 	struct value *v, *a;
229 {
230 	register char **pp;
231 	register struct value *vp;
232 
233 	if (a->v_type == V_ERR) {
234 		if ((v->v_str = str_cpy(default_shellfile)) != 0)
235 			v->v_type = V_STR;
236 		return;
237 	}
238 	if (v->v_str = default_shellfile) {
239 		v->v_type = V_STR;
240 		for (pp = default_shell + 1; *pp; pp++) {
241 			str_free(*pp);
242 			*pp = 0;
243 		}
244 	}
245 	for (pp = default_shell, vp = a;
246 	     vp->v_type != V_ERR &&
247 	     pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
248 	     pp++, vp++)
249 		if ((*pp = vp->v_type == V_STR ?
250 		     str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
251 			/* just leave default_shell[] the way it is */
252 			p_memerror();
253 			break;
254 		}
255 	if (default_shellfile = *default_shell)
256 		if (*default_shell = rindex(default_shellfile, '/'))
257 			(*default_shell)++;
258 		else
259 			*default_shell = default_shellfile;
260 }
261 
262 struct lcmd_arg arg_alias[] = {
263 	{ "",	0,		ARG_STR },
264 	{ "",	0,		ARG_STR|ARG_LIST },
265 	0
266 };
267 
268 l_alias(v, a)
269 	struct value *v, *a;
270 {
271 	if (a->v_type == V_ERR) {
272 		register struct ww *w;
273 		int printalias();
274 
275 		if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
276 			error("Can't open alias window: %s.", wwerror());
277 			return;
278 		}
279 		if (alias_walk(printalias, (int)w) >= 0)
280 			waitnl(w);
281 		closeiwin(w);
282 	} else {
283 		register struct alias *ap = 0;
284 
285 		if (ap = alias_lookup(a->v_str)) {
286 			if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
287 				p_memerror();
288 				return;
289 			}
290 			v->v_type = V_STR;
291 		}
292 		if (a[1].v_type == V_STR) {
293 			register struct value *vp;
294 			register char *p, *q;
295 			char *str;
296 			register n;
297 
298 			for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
299 				for (p = vp->v_str; *p; p++, n++)
300 					;
301 			if ((str = str_alloc(n)) == 0) {
302 				p_memerror();
303 				return;
304 			}
305 			for (q = str, vp = a + 1; vp->v_type != V_ERR;
306 			     vp++, q[-1] = ' ')
307 				for (p = vp->v_str; *q++ = *p++;)
308 					;
309 			q[-1] = 0;
310 			if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
311 				p_memerror();
312 				str_free(str);
313 				return;
314 			}
315 			ap->a_buf = str;
316 		}
317 	}
318 }
319 
320 printalias(w, a)
321 register struct ww *w;
322 register struct alias *a;
323 {
324 	if (more(w, 0) == 2)
325 		return -1;
326 	wwprintf(w, "%16s    %s\n", a->a_name, a->a_buf);
327 	return 0;
328 }
329 
330 struct lcmd_arg arg_unalias[] = {
331 	{ "alias",	1,	ARG_STR },
332 	0
333 };
334 
335 l_unalias(v, a)
336 struct value *v, *a;
337 {
338 	if (a->v_type == ARG_STR)
339 		v->v_num = alias_unset(a->v_str);
340 	v->v_type = V_NUM;
341 }
342 
343 struct lcmd_arg arg_echo[] = {
344 	{ "window",	1,	ARG_NUM },
345 	{ "",		0,	ARG_ANY|ARG_LIST },
346 	0
347 };
348 
349 /*ARGSUSED*/
350 l_echo(v, a)
351 struct value *v;
352 register struct value *a;
353 {
354 	char buf[20];
355 	struct ww *w;
356 
357 	if ((w = vtowin(a++, selwin)) == 0)
358 		return;
359 	while (a->v_type != V_ERR) {
360 		if (a->v_type == V_NUM) {
361 			(void) sprintf(buf, "%d", a->v_num);
362 			(void) wwwrite(w, buf, strlen(buf));
363 		} else
364 			(void) wwwrite(w, a->v_str, strlen(a->v_str));
365 		if ((++a)->v_type != V_ERR)
366 			(void) wwwrite(w, " ", 1);
367 	}
368 	(void) wwwrite(w, "\r\n", 2);
369 }
370