]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/lcmd2.c
This commit was generated by cvs2svn to compensate for changes in r68349,
[FreeBSD/FreeBSD.git] / usr.bin / window / lcmd2.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)lcmd2.c     8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "defs.h"
43 #include <string.h> /* System string definitions. */
44 #include "mystring.h" /* Local string definitions. */
45 #include "value.h"
46 #include "var.h"
47 #include "lcmd.h"
48 #include "alias.h"
49 #include <sys/types.h>
50 #include <sys/resource.h>
51
52 /*ARGSUSED*/
53 l_iostat(v, a)
54 struct value *v, *a;
55 {
56         register struct ww *w;
57
58         if ((w = openiwin(16, "IO Statistics")) == 0) {
59                 error("Can't open statistics window: %s.", wwerror());
60                 return;
61         }
62         wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
63         wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
64                 wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
65         wwprintf(w, "token\tuse\tbad\tsaving\ttotal\tbaud\n");
66         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d/%d (%.1f/%.1f)\n",
67                 wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc,
68                 wwntokc - wwntoksave ?
69                         (int) ((float) wwbaud * wwntokc /
70                                         (wwntokc - wwntoksave)) :
71                         wwbaud,
72                 wwnwrc ? (int) ((float) wwbaud * (wwnwrc + wwntoksave) /
73                                         wwnwrc) :
74                         wwbaud,
75                 wwntokc - wwntoksave ?
76                         (float) wwntokc / (wwntokc - wwntoksave) : 1.0,
77                 wwnwrc ? (float) (wwnwrc + wwntoksave) / wwnwrc : 1.0);
78         wwprintf(w, "wwwrite\tattempt\tchar\n");
79         wwprintf(w, "%d\t%d\t%d\n",
80                 wwnwwr, wwnwwra, wwnwwrc);
81         wwprintf(w, "wwupdat\tline\tmiss\tscan\tclreol\tclreos\tmiss\tline\n");
82         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
83                 wwnupdate, wwnupdline, wwnupdmiss, wwnupdscan, wwnupdclreol,
84                 wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline);
85         wwprintf(w, "select\terror\tzero\n");
86         wwprintf(w, "%d\t%d\t%d\n",
87                 wwnselect, wwnselecte, wwnselectz);
88         wwprintf(w, "read\terror\tzero\tchar\tack\tnack\tstat\terrorc\n");
89         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
90                 wwnread, wwnreade, wwnreadz, wwnreadc, wwnreadack, wwnreadnack,
91                 wwnreadstat, wwnreadec);
92         wwprintf(w, "ptyread\terror\tzero\tcontrol\tdata\tchar\n");
93         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\n",
94                 wwnwread, wwnwreade, wwnwreadz,
95                 wwnwreadp, wwnwreadd, wwnwreadc);
96         waitnl(w);
97         closeiwin(w);
98 }
99
100 struct lcmd_arg arg_time[] = {
101         { "who",        1,      ARG_STR },
102         0
103 };
104
105 /*ARGSUSED*/
106 l_time(v, a)
107 struct value *v;
108 register struct value *a;
109 {
110         register struct ww *w;
111         struct rusage rusage;
112         struct timeval timeval;
113         char *strtime();
114
115         if ((w = openiwin(8, "Timing and Resource Usage")) == 0) {
116                 error("Can't open time window: %s.", wwerror());
117                 return;
118         }
119
120         (void) gettimeofday(&timeval, (struct timezone *)0);
121         timeval.tv_sec -= starttime.tv_sec;
122         if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
123                 timeval.tv_sec--;
124                 timeval.tv_usec += 1000000;
125         }
126         (void) getrusage(a->v_type == V_STR
127                         && str_match(a->v_str, "children", 1)
128                 ? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
129
130         wwprintf(w, "%-15s %-15s %-15s\n",
131                 "time", "utime", "stime");
132         wwprintf(w, "%-15s ", strtime(&timeval));
133         wwprintf(w, "%-15s ", strtime(&rusage.ru_utime));
134         wwprintf(w, "%-15s\n", strtime(&rusage.ru_stime));
135         wwprintf(w, "%-15s %-15s %-15s %-15s\n",
136                 "maxrss", "ixrss", "idrss", "isrss");
137         wwprintf(w, "%-15ld %-15ld %-15ld %-15ld\n",
138                 rusage.ru_maxrss, rusage.ru_ixrss,
139                 rusage.ru_idrss, rusage.ru_isrss);
140         wwprintf(w, "%-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
141                 "minflt", "majflt", "nswap", "inblk", "oublk",
142                 "msgsnd", "msgrcv", "nsigs", "nvcsw", "nivcsw");
143         wwprintf(w, "%-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld\n",
144                 rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
145                 rusage.ru_inblock, rusage.ru_oublock,
146                 rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
147                 rusage.ru_nvcsw, rusage.ru_nivcsw);
148
149         waitnl(w);
150         closeiwin(w);
151 }
152
153 char *
154 strtime(t)
155 register struct timeval *t;
156 {
157         char fill = 0;
158         static char buf[20];
159         register char *p = buf;
160
161         if (t->tv_sec > 60*60) {
162                 (void) sprintf(p, "%ld:", t->tv_sec / (60*60));
163                 while (*p++)
164                         ;
165                 p--;
166                 t->tv_sec %= 60*60;
167                 fill++;
168         }
169         if (t->tv_sec > 60) {
170                 (void) sprintf(p, fill ? "%02ld:" : "%ld:", t->tv_sec / 60);
171                 while (*p++)
172                         ;
173                 p--;
174                 t->tv_sec %= 60;
175                 fill++;
176         }
177         (void) sprintf(p, fill ? "%02ld.%02d" : "%ld.%02ld",
178                 t->tv_sec, t->tv_usec / 10000);
179         return buf;
180 }
181
182 /*ARGSUSED*/
183 l_list(v, a)
184 struct value *v, *a;
185 {
186         register struct ww *w, *wp;
187         register i;
188         int n;
189
190         for (n = 0, i = 0; i < NWINDOW; i++)
191                 if (window[i] != 0)
192                         n++;
193         if (n == 0) {
194                 error("No windows.");
195                 return;
196         }
197         if ((w = openiwin(n + 2, "Windows")) == 0) {
198                 error("Can't open listing window: %s.", wwerror());
199                 return;
200         }
201         for (i = 0; i < NWINDOW; i++) {
202                 if ((wp = window[i]) == 0)
203                         continue;
204                 wwprintf(w, "%c %c %-13s %-.*s\n",
205                         wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
206                         i + '1',
207                         wp->ww_state == WWS_HASPROC ? "" : "(No process)",
208                         wwncol - 20,
209                         wp->ww_label ? wp->ww_label : "(No label)");
210         }
211         waitnl(w);
212         closeiwin(w);
213 }
214
215 /*ARGSUSED*/
216 l_variable(v, a)
217 struct value *v, *a;
218 {
219         register struct ww *w;
220         int printvar();
221
222         if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
223                 error("Can't open variable window: %s.", wwerror());
224                 return;
225         }
226         if (var_walk(printvar, (long)w) >= 0)
227                 waitnl(w);
228         closeiwin(w);
229 }
230
231 printvar(w, r)
232 register struct ww *w;
233 register struct var *r;
234 {
235         if (more(w, 0) == 2)
236                 return -1;
237         wwprintf(w, "%16s    ", r->r_name);
238         switch (r->r_val.v_type) {
239         case V_STR:
240                 wwprintf(w, "%s\n", r->r_val.v_str);
241                 break;
242         case V_NUM:
243                 wwprintf(w, "%d\n", r->r_val.v_num);
244                 break;
245         case V_ERR:
246                 wwprintf(w, "ERROR\n");
247                 break;
248         }
249         return 0;
250 }
251
252 struct lcmd_arg arg_def_shell[] = {
253         { "",   0,              ARG_ANY|ARG_LIST },
254         0
255 };
256
257 l_def_shell(v, a)
258         struct value *v, *a;
259 {
260         register char **pp;
261         register struct value *vp;
262
263         if (a->v_type == V_ERR) {
264                 if ((v->v_str = str_cpy(default_shellfile)) != 0)
265                         v->v_type = V_STR;
266                 return;
267         }
268         if (v->v_str = default_shellfile) {
269                 v->v_type = V_STR;
270                 for (pp = default_shell + 1; *pp; pp++) {
271                         str_free(*pp);
272                         *pp = 0;
273                 }
274         }
275         for (pp = default_shell, vp = a;
276              vp->v_type != V_ERR &&
277              pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
278              pp++, vp++)
279                 if ((*pp = vp->v_type == V_STR ?
280                      str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
281                         /* just leave default_shell[] the way it is */
282                         p_memerror();
283                         break;
284                 }
285         if (default_shellfile = *default_shell)
286                 if (*default_shell = rindex(default_shellfile, '/'))
287                         (*default_shell)++;
288                 else
289                         *default_shell = default_shellfile;
290 }
291
292 struct lcmd_arg arg_alias[] = {
293         { "",   0,              ARG_STR },
294         { "",   0,              ARG_STR|ARG_LIST },
295         0
296 };
297
298 l_alias(v, a)
299         struct value *v, *a;
300 {
301         if (a->v_type == V_ERR) {
302                 register struct ww *w;
303                 int printalias();
304
305                 if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
306                         error("Can't open alias window: %s.", wwerror());
307                         return;
308                 }
309                 if (alias_walk(printalias, (long)w) >= 0)
310                         waitnl(w);
311                 closeiwin(w);
312         } else {
313                 register struct alias *ap = 0;
314
315                 if (ap = alias_lookup(a->v_str)) {
316                         if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
317                                 p_memerror();
318                                 return;
319                         }
320                         v->v_type = V_STR;
321                 }
322                 if (a[1].v_type == V_STR) {
323                         register struct value *vp;
324                         register char *p, *q;
325                         char *str;
326                         register n;
327
328                         for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
329                                 for (p = vp->v_str; *p; p++, n++)
330                                         ;
331                         if ((str = str_alloc(n)) == 0) {
332                                 p_memerror();
333                                 return;
334                         }
335                         for (q = str, vp = a + 1; vp->v_type != V_ERR;
336                              vp++, q[-1] = ' ')
337                                 for (p = vp->v_str; *q++ = *p++;)
338                                         ;
339                         q[-1] = 0;
340                         if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
341                                 p_memerror();
342                                 str_free(str);
343                                 return;
344                         }
345                         ap->a_buf = str;
346                 }
347         }
348 }
349
350 printalias(w, a)
351 register struct ww *w;
352 register struct alias *a;
353 {
354         if (more(w, 0) == 2)
355                 return -1;
356         wwprintf(w, "%16s    %s\n", a->a_name, a->a_buf);
357         return 0;
358 }
359
360 struct lcmd_arg arg_unalias[] = {
361         { "alias",      1,      ARG_STR },
362         0
363 };
364
365 l_unalias(v, a)
366 struct value *v, *a;
367 {
368         if (a->v_type == ARG_STR)
369                 v->v_num = alias_unset(a->v_str);
370         v->v_type = V_NUM;
371 }
372
373 struct lcmd_arg arg_echo[] = {
374         { "window",     1,      ARG_NUM },
375         { "",           0,      ARG_ANY|ARG_LIST },
376         0
377 };
378
379 /*ARGSUSED*/
380 l_echo(v, a)
381 struct value *v;
382 register struct value *a;
383 {
384         char buf[20];
385         struct ww *w;
386
387         if ((w = vtowin(a++, selwin)) == 0)
388                 return;
389         while (a->v_type != V_ERR) {
390                 if (a->v_type == V_NUM) {
391                         (void) sprintf(buf, "%d", a->v_num);
392                         (void) wwwrite(w, buf, strlen(buf));
393                 } else
394                         (void) wwwrite(w, a->v_str, strlen(a->v_str));
395                 if ((++a)->v_type != V_ERR)
396                         (void) wwwrite(w, " ", 1);
397         }
398         (void) wwwrite(w, "\r\n", 2);
399 }