Commit | Line | Data |
---|---|---|
0a4ab290 | 1 | #ifndef lint |
1b736021 | 2 | static char sccsid[] = "@(#)lcmd2.c 3.8 %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 |
14 | l_iostat(v, a) |
15 | struct 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 | ||
46 | struct lcmd_arg arg_time[] = { | |
7ecf4dca | 47 | { "who", 1, ARG_STR }, |
0a4ab290 EW |
48 | 0 |
49 | }; | |
50 | ||
e404c17a | 51 | /*ARGSUSED*/ |
0a4ab290 EW |
52 | l_time(v, a) |
53 | struct value *v; | |
54 | register 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 | ||
94 | char * | |
95 | strtime(t) | |
96 | register 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*/ | |
124 | l_list(v, a) | |
125 | struct 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*/ | |
157 | l_variable(v, a) | |
158 | struct 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 | ||
172 | printvar(w, r) | |
173 | register struct ww *w; | |
174 | register 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 | |
193 | struct lcmd_arg arg_shell[] = { | |
855d0f8f | 194 | { "", 0, ARG_ANY|ARG_LIST }, |
7ecf4dca EW |
195 | 0 |
196 | }; | |
197 | ||
198 | l_shell(v, a) | |
855d0f8f | 199 | struct value *v, *a; |
7ecf4dca | 200 | { |
855d0f8f EW |
201 | register char **pp, *p; |
202 | register struct value *vp; | |
7ecf4dca EW |
203 | |
204 | if (a->v_type == V_ERR) { | |
205 | if ((v->v_str = str_cpy(shellfile)) != 0) | |
206 | v->v_type = V_STR; | |
207 | return; | |
208 | } | |
209 | if (v->v_str = shellfile) { | |
210 | v->v_type = V_STR; | |
855d0f8f | 211 | for (pp = shell + 1; *pp; pp++) { |
7ecf4dca | 212 | str_free(*pp); |
855d0f8f EW |
213 | *pp = 0; |
214 | } | |
7ecf4dca | 215 | } |
855d0f8f EW |
216 | for (pp = shell, vp = a; |
217 | vp->v_type != V_ERR && pp < &shell[sizeof shell/sizeof *shell-1]; | |
218 | pp++, vp++) | |
219 | if ((*pp = vp->v_type == V_STR ? | |
220 | str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) { | |
7ecf4dca EW |
221 | /* just leave shell[] the way it is */ |
222 | p_memerror(); | |
855d0f8f | 223 | break; |
7ecf4dca EW |
224 | } |
225 | if (shellfile = *shell) | |
226 | if (*shell = rindex(shellfile, '/')) | |
227 | (*shell)++; | |
228 | else | |
229 | *shell = shellfile; | |
230 | } | |
bb4a0c0b EW |
231 | |
232 | struct lcmd_arg arg_alias[] = { | |
855d0f8f EW |
233 | { "", 0, ARG_STR }, |
234 | { "", 0, ARG_STR|ARG_LIST }, | |
bb4a0c0b EW |
235 | 0 |
236 | }; | |
237 | ||
238 | l_alias(v, a) | |
855d0f8f | 239 | struct value *v, *a; |
bb4a0c0b EW |
240 | { |
241 | if (a->v_type == V_ERR) { | |
242 | register struct ww *w; | |
243 | int printalias(); | |
244 | ||
245 | if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) { | |
246 | error("Can't open alias window: %s.", wwerror()); | |
247 | return; | |
248 | } | |
249 | if (alias_walk(printalias, (int)w) >= 0) | |
250 | waitnl(w); | |
251 | closeiwin(w); | |
252 | } else { | |
253 | register struct alias *ap = 0; | |
254 | ||
255 | if (ap = alias_lookup(a->v_str)) { | |
256 | if ((v->v_str = str_cpy(ap->a_buf)) == 0) { | |
257 | p_memerror(); | |
258 | return; | |
259 | } | |
260 | v->v_type = V_STR; | |
261 | } | |
855d0f8f EW |
262 | if (a[1].v_type == V_STR) { |
263 | register struct value *vp; | |
264 | register char *p, *q; | |
265 | char *str; | |
266 | register n; | |
267 | ||
268 | for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++) | |
269 | for (p = vp->v_str; *p; p++, n++) | |
270 | ; | |
271 | if ((str = str_alloc(n)) == 0) { | |
272 | p_memerror(); | |
273 | return; | |
274 | } | |
275 | for (q = str, vp = a + 1; vp->v_type != V_ERR; | |
276 | vp++, q[-1] = ' ') | |
277 | for (p = vp->v_str; *q++ = *p++;) | |
278 | ; | |
279 | q[-1] = 0; | |
280 | if ((ap = alias_set(a[0].v_str, 0)) == 0) { | |
281 | p_memerror(); | |
282 | return; | |
283 | } | |
284 | ap->a_buf = str; | |
bb4a0c0b EW |
285 | } |
286 | } | |
287 | } | |
288 | ||
289 | printalias(w, a) | |
290 | register struct ww *w; | |
291 | register struct alias *a; | |
292 | { | |
293 | if (more(w, 0) == 2) | |
294 | return -1; | |
295 | wwprintf(w, "%16s %s\n", a->a_name, a->a_buf); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | struct lcmd_arg arg_unalias[] = { | |
300 | { "name", 1, ARG_STR }, | |
301 | 0 | |
302 | }; | |
303 | ||
304 | l_unalias(v, a) | |
305 | struct value *v, *a; | |
306 | { | |
307 | if (a->v_type == ARG_STR) | |
308 | v->v_num = alias_unset(a->v_str); | |
309 | v->v_type = V_NUM; | |
310 | } | |
1b736021 EW |
311 | |
312 | struct lcmd_arg arg_echo[] = { | |
313 | { "window", 1, ARG_NUM }, | |
314 | { "", 0, ARG_ANY|ARG_LIST }, | |
315 | 0 | |
316 | }; | |
317 | ||
318 | /*ARGSUSED*/ | |
319 | l_echo(v, a) | |
320 | struct value *v; | |
321 | register struct value *a; | |
322 | { | |
323 | char buf[20]; | |
324 | struct ww *w; | |
325 | ||
326 | if ((w = vtowin(a++)) == 0) | |
327 | return; | |
328 | while (a->v_type != V_ERR) { | |
329 | if (a->v_type == V_NUM) { | |
330 | (void) sprintf(buf, "%d", a->v_num); | |
331 | (void) wwwrite(w, buf, strlen(buf)); | |
332 | } else | |
333 | (void) wwwrite(w, a->v_str, strlen(a->v_str)); | |
334 | if ((++a)->v_type != V_ERR) | |
335 | (void) wwwrite(w, " ", 1); | |
336 | } | |
337 | (void) wwwrite(w, "\r\n", 2); | |
338 | } |