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