xref: /original-bsd/usr.bin/window/lcmd2.c (revision 1364b7d2)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)lcmd2.c	3.17 (Berkeley) 05/11/89";
20 #endif /* not lint */
21 
22 #include "defs.h"
23 #include "string.h"
24 #include "value.h"
25 #include "var.h"
26 #include "lcmd.h"
27 #include <sys/resource.h>
28 #include "alias.h"
29 
30 /*ARGSUSED*/
31 l_iostat(v, a)
32 struct value *v, *a;
33 {
34 	register struct ww *w;
35 
36 	if ((w = openiwin(16, "IO Statistics")) == 0) {
37 		error("Can't open statistics window: %s.", wwerror());
38 		return;
39 	}
40 	wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
41 	wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
42 		wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
43 	wwprintf(w, "token\tuse\tsaving\ttotal\tbaud\n");
44 	wwprintf(w, "%d\t%d\t%d\t%d\t%d/%d\n",
45 		wwzc0, wwzc1, wwzcsave, wwzctotal,
46 		wwzctotal - wwzcsave ?
47 			wwbaud * wwzctotal / (wwzctotal - wwzcsave) : wwbaud,
48 		wwnwrc ? wwbaud * (wwnwrc + wwzcsave) / wwnwrc : wwbaud);
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(6, "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, "time\t\tutime\t\tstime\t\tmaxrss\tixrss\tidrss\tisrss\n");
101 	wwprintf(w, "%-16s", strtime(&timeval));
102 	wwprintf(w, "%-16s", strtime(&rusage.ru_utime));
103 	wwprintf(w, "%-16s", strtime(&rusage.ru_stime));
104 	wwprintf(w, "%ld\t%ld\t%ld\t%ld\n",
105 		rusage.ru_maxrss, rusage.ru_ixrss,
106 		rusage.ru_idrss, rusage.ru_isrss);
107 	wwprintf(w, "minflt\tmajflt\tnswap\tinblk\toublk\tmsgsnd\tmsgrcv\tnsigs\tnvcsw\tnivcsw\n");
108 	wwprintf(w, "%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\n",
109 		rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
110 		rusage.ru_inblock, rusage.ru_oublock,
111 		rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
112 		rusage.ru_nvcsw, rusage.ru_nivcsw);
113 
114 	waitnl(w);
115 	closeiwin(w);
116 }
117 
118 char *
119 strtime(t)
120 register struct timeval *t;
121 {
122 	char fill = 0;
123 	static char buf[20];
124 	register char *p = buf;
125 
126 	if (t->tv_sec > 60*60) {
127 		(void) sprintf(p, "%ld:", t->tv_sec / (60*60));
128 		while (*p++)
129 			;
130 		p--;
131 		t->tv_sec %= 60*60;
132 		fill++;
133 	}
134 	if (t->tv_sec > 60) {
135 		(void) sprintf(p, fill ? "%02ld:" : "%ld:", t->tv_sec / 60);
136 		while (*p++)
137 			;
138 		p--;
139 		t->tv_sec %= 60;
140 		fill++;
141 	}
142 	(void) sprintf(p, fill ? "%02ld.%02d" : "%ld.%02ld",
143 		t->tv_sec, t->tv_usec / 10000);
144 	return buf;
145 }
146 
147 /*ARGSUSED*/
148 l_list(v, a)
149 struct value *v, *a;
150 {
151 	register struct ww *w, *wp;
152 	register i;
153 	int n;
154 
155 	for (n = 0, i = 0; i < NWINDOW; i++)
156 		if (window[i] != 0)
157 			n++;
158 	if (n == 0) {
159 		error("No windows.");
160 		return;
161 	}
162 	if ((w = openiwin(n + 2, "Windows")) == 0) {
163 		error("Can't open listing window: %s.", wwerror());
164 		return;
165 	}
166 	for (i = 0; i < NWINDOW; i++) {
167 		if ((wp = window[i]) == 0)
168 			continue;
169 		wwprintf(w, "%c %c %-13s %-.*s\n",
170 			wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
171 			i + '1',
172 			wp->ww_state == WWS_HASPROC ? "" : "(No process)",
173 			wwncol - 20,
174 			wp->ww_label ? wp->ww_label : "(No label)");
175 	}
176 	waitnl(w);
177 	closeiwin(w);
178 }
179 
180 /*ARGSUSED*/
181 l_variable(v, a)
182 struct value *v, *a;
183 {
184 	register struct ww *w;
185 	int printvar();
186 
187 	if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
188 		error("Can't open variable window: %s.", wwerror());
189 		return;
190 	}
191 	if (var_walk(printvar, (int)w) >= 0)
192 		waitnl(w);
193 	closeiwin(w);
194 }
195 
196 printvar(w, r)
197 register struct ww *w;
198 register struct var *r;
199 {
200 	if (more(w, 0) == 2)
201 		return -1;
202 	wwprintf(w, "%16s    ", r->r_name);
203 	switch (r->r_val.v_type) {
204 	case V_STR:
205 		wwprintf(w, "%s\n", r->r_val.v_str);
206 		break;
207 	case V_NUM:
208 		wwprintf(w, "%d\n", r->r_val.v_num);
209 		break;
210 	case V_ERR:
211 		wwprintf(w, "ERROR\n");
212 		break;
213 	}
214 	return 0;
215 }
216 
217 struct lcmd_arg arg_def_shell[] = {
218 	{ "",	0,		ARG_ANY|ARG_LIST },
219 	0
220 };
221 
222 l_def_shell(v, a)
223 	struct value *v, *a;
224 {
225 	register char **pp;
226 	register struct value *vp;
227 
228 	if (a->v_type == V_ERR) {
229 		if ((v->v_str = str_cpy(default_shellfile)) != 0)
230 			v->v_type = V_STR;
231 		return;
232 	}
233 	if (v->v_str = default_shellfile) {
234 		v->v_type = V_STR;
235 		for (pp = default_shell + 1; *pp; pp++) {
236 			str_free(*pp);
237 			*pp = 0;
238 		}
239 	}
240 	for (pp = default_shell, vp = a;
241 	     vp->v_type != V_ERR &&
242 	     pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
243 	     pp++, vp++)
244 		if ((*pp = vp->v_type == V_STR ?
245 		     str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
246 			/* just leave default_shell[] the way it is */
247 			p_memerror();
248 			break;
249 		}
250 	if (default_shellfile = *default_shell)
251 		if (*default_shell = rindex(default_shellfile, '/'))
252 			(*default_shell)++;
253 		else
254 			*default_shell = default_shellfile;
255 }
256 
257 struct lcmd_arg arg_alias[] = {
258 	{ "",	0,		ARG_STR },
259 	{ "",	0,		ARG_STR|ARG_LIST },
260 	0
261 };
262 
263 l_alias(v, a)
264 	struct value *v, *a;
265 {
266 	if (a->v_type == V_ERR) {
267 		register struct ww *w;
268 		int printalias();
269 
270 		if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
271 			error("Can't open alias window: %s.", wwerror());
272 			return;
273 		}
274 		if (alias_walk(printalias, (int)w) >= 0)
275 			waitnl(w);
276 		closeiwin(w);
277 	} else {
278 		register struct alias *ap = 0;
279 
280 		if (ap = alias_lookup(a->v_str)) {
281 			if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
282 				p_memerror();
283 				return;
284 			}
285 			v->v_type = V_STR;
286 		}
287 		if (a[1].v_type == V_STR) {
288 			register struct value *vp;
289 			register char *p, *q;
290 			char *str;
291 			register n;
292 
293 			for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
294 				for (p = vp->v_str; *p; p++, n++)
295 					;
296 			if ((str = str_alloc(n)) == 0) {
297 				p_memerror();
298 				return;
299 			}
300 			for (q = str, vp = a + 1; vp->v_type != V_ERR;
301 			     vp++, q[-1] = ' ')
302 				for (p = vp->v_str; *q++ = *p++;)
303 					;
304 			q[-1] = 0;
305 			if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
306 				p_memerror();
307 				str_free(str);
308 				return;
309 			}
310 			ap->a_buf = str;
311 		}
312 	}
313 }
314 
315 printalias(w, a)
316 register struct ww *w;
317 register struct alias *a;
318 {
319 	if (more(w, 0) == 2)
320 		return -1;
321 	wwprintf(w, "%16s    %s\n", a->a_name, a->a_buf);
322 	return 0;
323 }
324 
325 struct lcmd_arg arg_unalias[] = {
326 	{ "alias",	1,	ARG_STR },
327 	0
328 };
329 
330 l_unalias(v, a)
331 struct value *v, *a;
332 {
333 	if (a->v_type == ARG_STR)
334 		v->v_num = alias_unset(a->v_str);
335 	v->v_type = V_NUM;
336 }
337 
338 struct lcmd_arg arg_echo[] = {
339 	{ "window",	1,	ARG_NUM },
340 	{ "",		0,	ARG_ANY|ARG_LIST },
341 	0
342 };
343 
344 /*ARGSUSED*/
345 l_echo(v, a)
346 struct value *v;
347 register struct value *a;
348 {
349 	char buf[20];
350 	struct ww *w;
351 
352 	if ((w = vtowin(a++, selwin)) == 0)
353 		return;
354 	while (a->v_type != V_ERR) {
355 		if (a->v_type == V_NUM) {
356 			(void) sprintf(buf, "%d", a->v_num);
357 			(void) wwwrite(w, buf, strlen(buf));
358 		} else
359 			(void) wwwrite(w, a->v_str, strlen(a->v_str));
360 		if ((++a)->v_type != V_ERR)
361 			(void) wwwrite(w, " ", 1);
362 	}
363 	(void) wwwrite(w, "\r\n", 2);
364 }
365