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