]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/window/lcmd2.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)lcmd2.c     8.1 (Berkeley) 6/6/93";
40 #endif /* not lint */
41 #endif
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include "defs.h"
47 #include <string.h> /* System string definitions. */
48 #include "mystring.h" /* Local string definitions. */
49 #include "value.h"
50 #include "var.h"
51 #include "lcmd.h"
52 #include "alias.h"
53 #include <sys/types.h>
54 #include <sys/resource.h>
55
56 /*ARGSUSED*/
57 l_iostat(v, a)
58 struct value *v, *a;
59 {
60         register struct ww *w;
61
62         if ((w = openiwin(16, "IO Statistics")) == 0) {
63                 error("Can't open statistics window: %s.", wwerror());
64                 return;
65         }
66         wwprintf(w, "ttflush\twrite\terror\tzero\tchar\n");
67         wwprintf(w, "%d\t%d\t%d\t%d\t%d\n",
68                 wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc);
69         wwprintf(w, "token\tuse\tbad\tsaving\ttotal\tbaud\n");
70         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d/%d (%.1f/%.1f)\n",
71                 wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc,
72                 wwntokc - wwntoksave ?
73                         (int) ((float) wwbaud * wwntokc /
74                                         (wwntokc - wwntoksave)) :
75                         wwbaud,
76                 wwnwrc ? (int) ((float) wwbaud * (wwnwrc + wwntoksave) /
77                                         wwnwrc) :
78                         wwbaud,
79                 wwntokc - wwntoksave ?
80                         (float) wwntokc / (wwntokc - wwntoksave) : 1.0,
81                 wwnwrc ? (float) (wwnwrc + wwntoksave) / wwnwrc : 1.0);
82         wwprintf(w, "wwwrite\tattempt\tchar\n");
83         wwprintf(w, "%d\t%d\t%d\n",
84                 wwnwwr, wwnwwra, wwnwwrc);
85         wwprintf(w, "wwupdat\tline\tmiss\tscan\tclreol\tclreos\tmiss\tline\n");
86         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
87                 wwnupdate, wwnupdline, wwnupdmiss, wwnupdscan, wwnupdclreol,
88                 wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline);
89         wwprintf(w, "select\terror\tzero\n");
90         wwprintf(w, "%d\t%d\t%d\n",
91                 wwnselect, wwnselecte, wwnselectz);
92         wwprintf(w, "read\terror\tzero\tchar\tack\tnack\tstat\terrorc\n");
93         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
94                 wwnread, wwnreade, wwnreadz, wwnreadc, wwnreadack, wwnreadnack,
95                 wwnreadstat, wwnreadec);
96         wwprintf(w, "ptyread\terror\tzero\tcontrol\tdata\tchar\n");
97         wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\n",
98                 wwnwread, wwnwreade, wwnwreadz,
99                 wwnwreadp, wwnwreadd, wwnwreadc);
100         waitnl(w);
101         closeiwin(w);
102 }
103
104 struct lcmd_arg arg_time[] = {
105         { "who",        1,      ARG_STR },
106         0
107 };
108
109 /*ARGSUSED*/
110 l_time(v, a)
111 struct value *v;
112 register struct value *a;
113 {
114         register struct ww *w;
115         struct rusage rusage;
116         struct timeval timeval;
117         char *strtime();
118
119         if ((w = openiwin(8, "Timing and Resource Usage")) == 0) {
120                 error("Can't open time window: %s.", wwerror());
121                 return;
122         }
123
124         (void) gettimeofday(&timeval, (struct timezone *)0);
125         timeval.tv_sec -= starttime.tv_sec;
126         if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
127                 timeval.tv_sec--;
128                 timeval.tv_usec += 1000000;
129         }
130         (void) getrusage(a->v_type == V_STR
131                         && str_match(a->v_str, "children", 1)
132                 ? RUSAGE_CHILDREN : RUSAGE_SELF, &rusage);
133
134         wwprintf(w, "%-15s %-15s %-15s\n",
135                 "time", "utime", "stime");
136         wwprintf(w, "%-15s ", strtime(&timeval));
137         wwprintf(w, "%-15s ", strtime(&rusage.ru_utime));
138         wwprintf(w, "%-15s\n", strtime(&rusage.ru_stime));
139         wwprintf(w, "%-15s %-15s %-15s %-15s\n",
140                 "maxrss", "ixrss", "idrss", "isrss");
141         wwprintf(w, "%-15ld %-15ld %-15ld %-15ld\n",
142                 rusage.ru_maxrss, rusage.ru_ixrss,
143                 rusage.ru_idrss, rusage.ru_isrss);
144         wwprintf(w, "%-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
145                 "minflt", "majflt", "nswap", "inblk", "oublk",
146                 "msgsnd", "msgrcv", "nsigs", "nvcsw", "nivcsw");
147         wwprintf(w, "%-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld %-7ld\n",
148                 rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
149                 rusage.ru_inblock, rusage.ru_oublock,
150                 rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
151                 rusage.ru_nvcsw, rusage.ru_nivcsw);
152
153         waitnl(w);
154         closeiwin(w);
155 }
156
157 char *
158 strtime(t)
159 register struct timeval *t;
160 {
161         char fill = 0;
162         static char buf[20];
163         register char *p = buf;
164
165         if (t->tv_sec > 60*60) {
166                 (void) sprintf(p, "%ld:", t->tv_sec / (60*60));
167                 while (*p++)
168                         ;
169                 p--;
170                 t->tv_sec %= 60*60;
171                 fill++;
172         }
173         if (t->tv_sec > 60) {
174                 (void) sprintf(p, fill ? "%02ld:" : "%ld:", t->tv_sec / 60);
175                 while (*p++)
176                         ;
177                 p--;
178                 t->tv_sec %= 60;
179                 fill++;
180         }
181         (void) sprintf(p, fill ? "%02ld.%02ld" : "%ld.%02ld",
182                 t->tv_sec, t->tv_usec / 10000);
183         return buf;
184 }
185
186 /*ARGSUSED*/
187 l_list(v, a)
188 struct value *v, *a;
189 {
190         register struct ww *w, *wp;
191         register i;
192         int n;
193
194         for (n = 0, i = 0; i < NWINDOW; i++)
195                 if (window[i] != 0)
196                         n++;
197         if (n == 0) {
198                 error("No windows.");
199                 return;
200         }
201         if ((w = openiwin(n + 2, "Windows")) == 0) {
202                 error("Can't open listing window: %s.", wwerror());
203                 return;
204         }
205         for (i = 0; i < NWINDOW; i++) {
206                 if ((wp = window[i]) == 0)
207                         continue;
208                 wwprintf(w, "%c %c %-13s %-.*s\n",
209                         wp == selwin ? '+' : (wp == lastselwin ? '-' : ' '),
210                         i + '1',
211                         wp->ww_state == WWS_HASPROC ? "" : "(No process)",
212                         wwncol - 20,
213                         wp->ww_label ? wp->ww_label : "(No label)");
214         }
215         waitnl(w);
216         closeiwin(w);
217 }
218
219 /*ARGSUSED*/
220 l_variable(v, a)
221 struct value *v, *a;
222 {
223         register struct ww *w;
224         int printvar();
225
226         if ((w = openiwin(wwnrow - 3, "Variables")) == 0) {
227                 error("Can't open variable window: %s.", wwerror());
228                 return;
229         }
230         if (var_walk(printvar, (long)w) >= 0)
231                 waitnl(w);
232         closeiwin(w);
233 }
234
235 printvar(w, r)
236 register struct ww *w;
237 register struct var *r;
238 {
239         if (more(w, 0) == 2)
240                 return -1;
241         wwprintf(w, "%16s    ", r->r_name);
242         switch (r->r_val.v_type) {
243         case V_STR:
244                 wwprintf(w, "%s\n", r->r_val.v_str);
245                 break;
246         case V_NUM:
247                 wwprintf(w, "%d\n", r->r_val.v_num);
248                 break;
249         case V_ERR:
250                 wwprintf(w, "ERROR\n");
251                 break;
252         }
253         return 0;
254 }
255
256 struct lcmd_arg arg_def_shell[] = {
257         { "",   0,              ARG_ANY|ARG_LIST },
258         0
259 };
260
261 l_def_shell(v, a)
262         struct value *v, *a;
263 {
264         register char **pp;
265         register struct value *vp;
266
267         if (a->v_type == V_ERR) {
268                 if ((v->v_str = str_cpy(default_shellfile)) != 0)
269                         v->v_type = V_STR;
270                 return;
271         }
272         if (v->v_str = default_shellfile) {
273                 v->v_type = V_STR;
274                 for (pp = default_shell + 1; *pp; pp++) {
275                         str_free(*pp);
276                         *pp = 0;
277                 }
278         }
279         for (pp = default_shell, vp = a;
280              vp->v_type != V_ERR &&
281              pp < &default_shell[sizeof default_shell/sizeof *default_shell-1];
282              pp++, vp++)
283                 if ((*pp = vp->v_type == V_STR ?
284                      str_cpy(vp->v_str) : str_itoa(vp->v_num)) == 0) {
285                         /* just leave default_shell[] the way it is */
286                         p_memerror();
287                         break;
288                 }
289         if (default_shellfile = *default_shell)
290                 if (*default_shell = rindex(default_shellfile, '/'))
291                         (*default_shell)++;
292                 else
293                         *default_shell = default_shellfile;
294 }
295
296 struct lcmd_arg arg_alias[] = {
297         { "",   0,              ARG_STR },
298         { "",   0,              ARG_STR|ARG_LIST },
299         0
300 };
301
302 l_alias(v, a)
303         struct value *v, *a;
304 {
305         if (a->v_type == V_ERR) {
306                 register struct ww *w;
307                 int printalias();
308
309                 if ((w = openiwin(wwnrow - 3, "Aliases")) == 0) {
310                         error("Can't open alias window: %s.", wwerror());
311                         return;
312                 }
313                 if (alias_walk(printalias, (long)w) >= 0)
314                         waitnl(w);
315                 closeiwin(w);
316         } else {
317                 register struct alias *ap = 0;
318
319                 if (ap = alias_lookup(a->v_str)) {
320                         if ((v->v_str = str_cpy(ap->a_buf)) == 0) {
321                                 p_memerror();
322                                 return;
323                         }
324                         v->v_type = V_STR;
325                 }
326                 if (a[1].v_type == V_STR) {
327                         register struct value *vp;
328                         register char *p, *q;
329                         char *str;
330                         register n;
331
332                         for (n = 0, vp = a + 1; vp->v_type != V_ERR; vp++, n++)
333                                 for (p = vp->v_str; *p; p++, n++)
334                                         ;
335                         if ((str = str_alloc(n)) == 0) {
336                                 p_memerror();
337                                 return;
338                         }
339                         for (q = str, vp = a + 1; vp->v_type != V_ERR;
340                              vp++, q[-1] = ' ')
341                                 for (p = vp->v_str; *q++ = *p++;)
342                                         ;
343                         q[-1] = 0;
344                         if ((ap = alias_set(a[0].v_str, (char *)0)) == 0) {
345                                 p_memerror();
346                                 str_free(str);
347                                 return;
348                         }
349                         ap->a_buf = str;
350                 }
351         }
352 }
353
354 printalias(w, a)
355 register struct ww *w;
356 register struct alias *a;
357 {
358         if (more(w, 0) == 2)
359                 return -1;
360         wwprintf(w, "%16s    %s\n", a->a_name, a->a_buf);
361         return 0;
362 }
363
364 struct lcmd_arg arg_unalias[] = {
365         { "alias",      1,      ARG_STR },
366         0
367 };
368
369 l_unalias(v, a)
370 struct value *v, *a;
371 {
372         if (a->v_type == ARG_STR)
373                 v->v_num = alias_unset(a->v_str);
374         v->v_type = V_NUM;
375 }
376
377 struct lcmd_arg arg_echo[] = {
378         { "window",     1,      ARG_NUM },
379         { "",           0,      ARG_ANY|ARG_LIST },
380         0
381 };
382
383 /*ARGSUSED*/
384 l_echo(v, a)
385 struct value *v;
386 register struct value *a;
387 {
388         char buf[20];
389         struct ww *w;
390
391         if ((w = vtowin(a++, selwin)) == 0)
392                 return;
393         while (a->v_type != V_ERR) {
394                 if (a->v_type == V_NUM) {
395                         (void) sprintf(buf, "%d", a->v_num);
396                         (void) wwwrite(w, buf, strlen(buf));
397                 } else
398                         (void) wwwrite(w, a->v_str, strlen(a->v_str));
399                 if ((++a)->v_type != V_ERR)
400                         (void) wwwrite(w, " ", 1);
401         }
402         (void) wwwrite(w, "\r\n", 2);
403 }