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