spelling
[unix-history] / usr / src / usr.bin / window / lcmd2.c
CommitLineData
0a4ab290 1#ifndef lint
7f3640e9 2static char sccsid[] = "@(#)lcmd2.c 3.6 %G%";
0a4ab290
EW
3#endif
4
5#include "defs.h"
6#include "string.h"
7#include "value.h"
e404c17a 8#include "var.h"
0a4ab290
EW
9#include "lcmd.h"
10#include <sys/resource.h>
bb4a0c0b 11#include "alias.h"
0a4ab290 12
e404c17a 13/*ARGSUSED*/
0a4ab290
EW
14l_iostat(v, a)
15struct value *v, *a;
16{
17 register struct ww *w;
18
19 if ((w = openiwin(14, "IO Statistics")) == 0) {
20 error("Can't open statistics window: %s.", wwerror());
21 return;
22 }
23 wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
24 wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
25 wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
7f3640e9 26 wwprintf(w, "wwwrite\tattempt\tchar\n");
0a4ab290
EW
27 wwprintf(w, "%d\t%d\t%d\n",
28 wwnwwr, wwnwwra, wwnwwrc);
29 wwprintf(w, "wwupdat\tline\tmiss\tmajor\tmiss\n");
30 wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
31 wwnupdate, wwnupdline, wwnupdmiss, wwnmajline, wwnmajmiss);
32 wwprintf(w, "select\terror\tzero\n");
33 wwprintf(w, "%d\t%d\t%d\n",
34 wwnselect, wwnselecte, wwnselectz);
35 wwprintf(w, "read\terror\tzero\tchar\n");
36 wwprintf(w, "%d\t%d\t%d\t%d\n",
37 wwnread, wwnreade, wwnreadz, wwnreadc);
38 wwprintf(w, "ptyread\terror\tzero\tcontrol\tdata\tchar\n");
39 wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\n",
40 wwnwread, wwnwreade, wwnwreadz,
41 wwnwreadp, wwnwreadd, wwnwreadc);
42 waitnl(w);
43 closeiwin(w);
44}
45
46struct lcmd_arg arg_time[] = {
7ecf4dca 47 { "who", 1, ARG_STR },
0a4ab290
EW
48 0
49};
50
e404c17a 51/*ARGSUSED*/
0a4ab290
EW
52l_time(v, a)
53struct value *v;
54register struct value *a;
55{
56 register struct ww *w;
57 struct rusage rusage;
58 struct timeval timeval;
0a4ab290
EW
59 char *strtime();
60
61 if ((w = openiwin(6, "Timing and Resource Usage")) == 0) {
62 error("Can't open time window: %s.", wwerror());
63 return;
64 }
65
66 (void) gettimeofday(&timeval, (struct timezone *)0);
67 timeval.tv_sec -= starttime.tv_sec;
68 if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
69 timeval.tv_sec--;
70 timeval.tv_usec += 1000000;
71 }
72 (void) getrusage(a->v_type == V_STR
73 && str_match(a->v_str, "children", 1)
74 ? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
75
76 wwprintf(w, "time\t\tutime\t\tstime\t\tmaxrss\tixrss\tidrss\tisrss\n");
77 wwprintf(w, "%-16s", strtime(&timeval));
78 wwprintf(w, "%-16s", strtime(&rusage.ru_utime));
79 wwprintf(w, "%-16s", strtime(&rusage.ru_stime));
80 wwprintf(w, "%D\t%D\t%D\t%D\n",
81 rusage.ru_maxrss, rusage.ru_ixrss,
82 rusage.ru_idrss, rusage.ru_isrss);
83 wwprintf(w, "minflt\tmajflt\tnswap\tinblk\toublk\tmsgsnd\tmsgrcv\tnsigs\tnvcsw\tnivcsw\n");
84 wwprintf(w, "%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\n",
85 rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
86 rusage.ru_inblock, rusage.ru_oublock,
87 rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
88 rusage.ru_nvcsw, rusage.ru_nivcsw);
89
90 waitnl(w);
91 closeiwin(w);
92}
93
94char *
95strtime(t)
96register struct timeval *t;
97{
98 char fill = 0;
99 static char buf[20];
100 register char *p = buf;
101
102 if (t->tv_sec > 60*60) {
103 (void) sprintf(p, "%D:", t->tv_sec / (60*60));
104 while (*p++)
105 ;
106 p--;
107 t->tv_sec %= 60*60;
108 fill++;
109 }
110 if (t->tv_sec > 60) {
111 (void) sprintf(p, fill ? "%02D:" : "%D:", t->tv_sec / 60);
112 while (*p++)
113 ;
114 p--;
115 t->tv_sec %= 60;
116 fill++;
117 }
118 (void) sprintf(p, fill ? "%02D.%02d" : "%D.%02D",
119 t->tv_sec, t->tv_usec / 10000);
120 return buf;
121}
e404c17a
EW
122
123/*ARGSUSED*/
124l_list(v, a)
125struct value *v, *a;
126{
127 register struct ww *w, *wp;
128 register i;
129 int n;
130
131 for (n = 0, i = 0; i < NWINDOW; i++)
132 if (window[i] != 0)
133 n++;
134 if (n == 0) {
135 error("No windows.");
136 return;
137 }
138 if ((w = openiwin(n + 2, "Windows")) == 0) {
139 error("Can't open listing window: %s.", wwerror());
140 return;
141 }
142 for (i = 0; i < NWINDOW; i++) {
143 if ((wp = window[i]) == 0)
144 continue;
145 wwprintf(w, "%c %c %-13s %-.*s\n",
146 wp == selwin ? '*' : ' ',
147 i + '1',
148 wp->ww_state == WWS_HASPROC ? "" : "(No process)",
149 wwncol - 20,
150 wp->ww_label ? wp->ww_label : "(No label)");
151 }
152 waitnl(w);
153 closeiwin(w);
154}
155
156/*ARGSUSED*/
157l_variable(v, a)
158struct value *v, *a;
159{
160 register struct ww *w;
161 int printvar();
162
163 if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
164 error("Can't open variable window: %s.", wwerror());
165 return;
166 }
167 if (var_walk(printvar, (int)w) >= 0)
168 waitnl(w);
169 closeiwin(w);
170}
171
172printvar(w, r)
173register struct ww *w;
174register struct var *r;
175{
176 if (more(w, 0) == 2)
177 return -1;
bb4a0c0b 178 wwprintf(w, "%16s ", r->r_name);
e404c17a
EW
179 switch (r->r_val.v_type) {
180 case V_STR:
181 wwprintf(w, "%s\n", r->r_val.v_str);
182 break;
183 case V_NUM:
184 wwprintf(w, "%d\n", r->r_val.v_num);
185 break;
186 case V_ERR:
187 wwprintf(w, "ERROR\n");
188 break;
189 }
190 return 0;
191}
7ecf4dca
EW
192
193struct lcmd_arg arg_shell[] = {
194 { "shell", 1, ARG_STR },
195 0
196};
197
198l_shell(v, a)
199struct value *v, *a;
200{
201 register char **pp;
202
203 if (a->v_type == V_ERR) {
204 if ((v->v_str = str_cpy(shellfile)) != 0)
205 v->v_type = V_STR;
206 return;
207 }
208 if (v->v_str = shellfile) {
209 v->v_type = V_STR;
210 for (pp = shell + 1; *pp; pp++)
211 str_free(*pp);
212 }
213 if (mkargv(a->v_str, shell, sizeof shell / sizeof *shell) < 0)
214 *shell = 0;
215 for (pp = shell; *pp; pp++)
216 if ((*pp = str_cpy(*pp)) == 0) {
217 /* just leave shell[] the way it is */
218 p_memerror();
219 }
220 if (shellfile = *shell)
221 if (*shell = rindex(shellfile, '/'))
222 (*shell)++;
223 else
224 *shell = shellfile;
225}
bb4a0c0b
EW
226
227struct lcmd_arg arg_alias[] = {
228 { "name", 1, ARG_STR },
229 { "string", 1, ARG_STR },
230 0
231};
232
233l_alias(v, a)
234struct value *v, *a;
235{
236 if (a->v_type == V_ERR) {
237 register struct ww *w;
238 int printalias();
239
240 if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
241 error("Can't open alias window: %s.", wwerror());
242 return;
243 }
244 if (alias_walk(printalias, (int)w) >= 0)
245 waitnl(w);
246 closeiwin(w);
247 } else {
248 register struct alias *ap = 0;
249
250 if (ap = alias_lookup(a->v_str)) {
251 if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
252 p_memerror();
253 return;
254 }
255 v->v_type = V_STR;
256 }
257 if (a[1].v_type == V_STR &&
258 alias_set(a[0].v_str, a[1].v_str) == 0) {
259 p_memerror();
260 return;
261 }
262 }
263}
264
265printalias(w, a)
266register struct ww *w;
267register struct alias *a;
268{
269 if (more(w, 0) == 2)
270 return -1;
271 wwprintf(w, "%16s %s\n", a->a_name, a->a_buf);
272 return 0;
273}
274
275struct lcmd_arg arg_unalias[] = {
276 { "name", 1, ARG_STR },
277 0
278};
279
280l_unalias(v, a)
281struct value *v, *a;
282{
283 if (a->v_type == ARG_STR)
284 v->v_num = alias_unset(a->v_str);
285 v->v_type = V_NUM;
286}