| 1 | /* $NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*-
|
|---|
| 4 | * Copyright (c) 1991, 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 | * Kenneth Almquist.
|
|---|
| 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 | #if 0
|
|---|
| 36 | #ifndef lint
|
|---|
| 37 | static char sccsid[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
|
|---|
| 38 | #else
|
|---|
| 39 | __RCSID("$NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $");
|
|---|
| 40 | #endif /* not lint */
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <stdarg.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <assert.h>
|
|---|
| 47 |
|
|---|
| 48 | #include "shell.h"
|
|---|
| 49 | #include "parser.h"
|
|---|
| 50 | #include "nodes.h"
|
|---|
| 51 | #include "mystring.h"
|
|---|
| 52 | #include "show.h"
|
|---|
| 53 | #include "options.h"
|
|---|
| 54 | #include "shinstance.h"
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | #ifdef DEBUG
|
|---|
| 58 | static void shtree(union node *, int, char *, FILE*);
|
|---|
| 59 | static void shcmd(union node *, FILE *);
|
|---|
| 60 | static void sharg(union node *, FILE *);
|
|---|
| 61 | static void indent(int, char *, FILE *);
|
|---|
| 62 | static void trstring(shinstance *, char *);
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | void
|
|---|
| 66 | showtree(shinstance *psh, union node *n)
|
|---|
| 67 | {
|
|---|
| 68 | trputs(psh, "showtree called\n");
|
|---|
| 69 | shtree(n, 1, NULL, stdout);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | static void
|
|---|
| 74 | shtree(union node *n, int ind, char *pfx, FILE *fp)
|
|---|
| 75 | {
|
|---|
| 76 | struct nodelist *lp;
|
|---|
| 77 | const char *s;
|
|---|
| 78 |
|
|---|
| 79 | if (n == NULL)
|
|---|
| 80 | return;
|
|---|
| 81 |
|
|---|
| 82 | indent(ind, pfx, fp);
|
|---|
| 83 | switch(n->type) {
|
|---|
| 84 | case NSEMI:
|
|---|
| 85 | s = "; ";
|
|---|
| 86 | goto binop;
|
|---|
| 87 | case NAND:
|
|---|
| 88 | s = " && ";
|
|---|
| 89 | goto binop;
|
|---|
| 90 | case NOR:
|
|---|
| 91 | s = " || ";
|
|---|
| 92 | binop:
|
|---|
| 93 | shtree(n->nbinary.ch1, ind, NULL, fp);
|
|---|
| 94 | /* if (ind < 0) */
|
|---|
| 95 | fputs(s, fp);
|
|---|
| 96 | shtree(n->nbinary.ch2, ind, NULL, fp);
|
|---|
| 97 | break;
|
|---|
| 98 | case NCMD:
|
|---|
| 99 | shcmd(n, fp);
|
|---|
| 100 | if (ind >= 0)
|
|---|
| 101 | putc('\n', fp);
|
|---|
| 102 | break;
|
|---|
| 103 | case NPIPE:
|
|---|
| 104 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
|
|---|
| 105 | shcmd(lp->n, fp);
|
|---|
| 106 | if (lp->next)
|
|---|
| 107 | fputs(" | ", fp);
|
|---|
| 108 | }
|
|---|
| 109 | if (n->npipe.backgnd)
|
|---|
| 110 | fputs(" &", fp);
|
|---|
| 111 | if (ind >= 0)
|
|---|
| 112 | putc('\n', fp);
|
|---|
| 113 | break;
|
|---|
| 114 | default:
|
|---|
| 115 | fprintf(fp, "<node type %d>", n->type);
|
|---|
| 116 | if (ind >= 0)
|
|---|
| 117 | putc('\n', fp);
|
|---|
| 118 | break;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | static void
|
|---|
| 125 | shcmd(union node *cmd, FILE *fp)
|
|---|
| 126 | {
|
|---|
| 127 | union node *np;
|
|---|
| 128 | int first;
|
|---|
| 129 | const char *s;
|
|---|
| 130 | int dftfd;
|
|---|
| 131 |
|
|---|
| 132 | first = 1;
|
|---|
| 133 | for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
|
|---|
| 134 | if (! first)
|
|---|
| 135 | putchar(' ');
|
|---|
| 136 | sharg(np, fp);
|
|---|
| 137 | first = 0;
|
|---|
| 138 | }
|
|---|
| 139 | for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
|
|---|
| 140 | if (! first)
|
|---|
| 141 | putchar(' ');
|
|---|
| 142 | switch (np->nfile.type) {
|
|---|
| 143 | case NTO: s = ">"; dftfd = 1; break;
|
|---|
| 144 | case NCLOBBER: s = ">|"; dftfd = 1; break;
|
|---|
| 145 | case NAPPEND: s = ">>"; dftfd = 1; break;
|
|---|
| 146 | case NTOFD: s = ">&"; dftfd = 1; break;
|
|---|
| 147 | case NFROM: s = "<"; dftfd = 0; break;
|
|---|
| 148 | case NFROMFD: s = "<&"; dftfd = 0; break;
|
|---|
| 149 | case NFROMTO: s = "<>"; dftfd = 0; break;
|
|---|
| 150 | default: s = "*error*"; dftfd = 0; break;
|
|---|
| 151 | }
|
|---|
| 152 | if (np->nfile.fd != dftfd)
|
|---|
| 153 | fprintf(fp, "%d", np->nfile.fd);
|
|---|
| 154 | fputs(s, fp);
|
|---|
| 155 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
|
|---|
| 156 | fprintf(fp, "%d", np->ndup.dupfd);
|
|---|
| 157 | } else {
|
|---|
| 158 | sharg(np->nfile.fname, fp);
|
|---|
| 159 | }
|
|---|
| 160 | first = 0;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | static void
|
|---|
| 167 | sharg(union node *arg, FILE *fp)
|
|---|
| 168 | {
|
|---|
| 169 | char *p;
|
|---|
| 170 | struct nodelist *bqlist;
|
|---|
| 171 | int subtype;
|
|---|
| 172 |
|
|---|
| 173 | if (arg->type != NARG) {
|
|---|
| 174 | printf("<node type %d>\n", arg->type);
|
|---|
| 175 | abort();
|
|---|
| 176 | }
|
|---|
| 177 | bqlist = arg->narg.backquote;
|
|---|
| 178 | for (p = arg->narg.text ; *p ; p++) {
|
|---|
| 179 | switch (*p) {
|
|---|
| 180 | case CTLESC:
|
|---|
| 181 | putc(*++p, fp);
|
|---|
| 182 | break;
|
|---|
| 183 | case CTLVAR:
|
|---|
| 184 | putc('$', fp);
|
|---|
| 185 | putc('{', fp);
|
|---|
| 186 | subtype = *++p;
|
|---|
| 187 | if (subtype == VSLENGTH)
|
|---|
| 188 | putc('#', fp);
|
|---|
| 189 |
|
|---|
| 190 | while (*p != '=')
|
|---|
| 191 | putc(*p++, fp);
|
|---|
| 192 |
|
|---|
| 193 | if (subtype & VSNUL)
|
|---|
| 194 | putc(':', fp);
|
|---|
| 195 |
|
|---|
| 196 | switch (subtype & VSTYPE) {
|
|---|
| 197 | case VSNORMAL:
|
|---|
| 198 | putc('}', fp);
|
|---|
| 199 | break;
|
|---|
| 200 | case VSMINUS:
|
|---|
| 201 | putc('-', fp);
|
|---|
| 202 | break;
|
|---|
| 203 | case VSPLUS:
|
|---|
| 204 | putc('+', fp);
|
|---|
| 205 | break;
|
|---|
| 206 | case VSQUESTION:
|
|---|
| 207 | putc('?', fp);
|
|---|
| 208 | break;
|
|---|
| 209 | case VSASSIGN:
|
|---|
| 210 | putc('=', fp);
|
|---|
| 211 | break;
|
|---|
| 212 | case VSTRIMLEFT:
|
|---|
| 213 | putc('#', fp);
|
|---|
| 214 | break;
|
|---|
| 215 | case VSTRIMLEFTMAX:
|
|---|
| 216 | putc('#', fp);
|
|---|
| 217 | putc('#', fp);
|
|---|
| 218 | break;
|
|---|
| 219 | case VSTRIMRIGHT:
|
|---|
| 220 | putc('%', fp);
|
|---|
| 221 | break;
|
|---|
| 222 | case VSTRIMRIGHTMAX:
|
|---|
| 223 | putc('%', fp);
|
|---|
| 224 | putc('%', fp);
|
|---|
| 225 | break;
|
|---|
| 226 | case VSLENGTH:
|
|---|
| 227 | break;
|
|---|
| 228 | default:
|
|---|
| 229 | printf("<subtype %d>", subtype);
|
|---|
| 230 | }
|
|---|
| 231 | break;
|
|---|
| 232 | case CTLENDVAR:
|
|---|
| 233 | putc('}', fp);
|
|---|
| 234 | break;
|
|---|
| 235 | case CTLBACKQ:
|
|---|
| 236 | case CTLBACKQ|CTLQUOTE:
|
|---|
| 237 | putc('$', fp);
|
|---|
| 238 | putc('(', fp);
|
|---|
| 239 | shtree(bqlist->n, -1, NULL, fp);
|
|---|
| 240 | putc(')', fp);
|
|---|
| 241 | break;
|
|---|
| 242 | default:
|
|---|
| 243 | putc(*p, fp);
|
|---|
| 244 | break;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 | static void
|
|---|
| 251 | indent(int amount, char *pfx, FILE *fp)
|
|---|
| 252 | {
|
|---|
| 253 | int i;
|
|---|
| 254 |
|
|---|
| 255 | for (i = 0 ; i < amount ; i++) {
|
|---|
| 256 | if (pfx && i == amount - 1)
|
|---|
| 257 | fputs(pfx, fp);
|
|---|
| 258 | putc('\t', fp);
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | #endif
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 | /*
|
|---|
| 266 | * Debugging stuff.
|
|---|
| 267 | */
|
|---|
| 268 |
|
|---|
| 269 | #ifdef DEBUG
|
|---|
| 270 | #ifdef TRACE_VIA_STDIO
|
|---|
| 271 | FILE *tracefile;
|
|---|
| 272 | #endif
|
|---|
| 273 |
|
|---|
| 274 | /** @def TRY_GET_PSH_OR_RETURN
|
|---|
| 275 | * Make sure @a psh is valid, trying to fetch it from TLS
|
|---|
| 276 | * if it's NULL and returning (void) if that fails. */
|
|---|
| 277 | # define TRY_GET_PSH_OR_RETURN(psh) \
|
|---|
| 278 | if (!(psh)) { \
|
|---|
| 279 | psh = shthread_get_shell(); \
|
|---|
| 280 | if (!psh) \
|
|---|
| 281 | return; \
|
|---|
| 282 | } else do { } while (0)
|
|---|
| 283 |
|
|---|
| 284 | /** @def RETURN_IF_NOT_TRACING
|
|---|
| 285 | * Return if we're not tracing. */
|
|---|
| 286 | # ifdef TRACE_VIA_STDIO
|
|---|
| 287 | # define RETURN_IF_NOT_TRACING(psh) \
|
|---|
| 288 | if (debug(psh) != 1 || !tracefile)
|
|---|
| 289 | return; \
|
|---|
| 290 | else do {} while (0)
|
|---|
| 291 | # else
|
|---|
| 292 | # define RETURN_IF_NOT_TRACING(psh) \
|
|---|
| 293 | if (debug(psh) != 1 || psh->tracefd == -1) \
|
|---|
| 294 | return; \
|
|---|
| 295 | else do {} while (0)
|
|---|
| 296 | # endif
|
|---|
| 297 |
|
|---|
| 298 | /** @def TRACE_PUTC
|
|---|
| 299 | * putc/trace_char wrapper. The @a psh / @a tracefile
|
|---|
| 300 | * is taken from the context and not as a paramenter. */
|
|---|
| 301 | # ifdef TRACE_VIA_STDIO
|
|---|
| 302 | # define TRACE_PUTC(c) fputc(c, tracefile)
|
|---|
| 303 | # else
|
|---|
| 304 | # define TRACE_PUTC(c) trace_char(psh, c)
|
|---|
| 305 | # endif
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 | # ifndef TRACE_VIA_STDIO
|
|---|
| 309 | /* Flushes the tracebuf. */
|
|---|
| 310 | static void
|
|---|
| 311 | trace_flush(shinstance *psh)
|
|---|
| 312 | {
|
|---|
| 313 | size_t pos = psh->tracepos;
|
|---|
| 314 |
|
|---|
| 315 | if (pos > sizeof(psh->tracebuf)) {
|
|---|
| 316 | char *end;
|
|---|
| 317 | assert(0);
|
|---|
| 318 | end = memchr(psh->tracebuf, '\0', sizeof(psh->tracebuf));
|
|---|
| 319 | pos = end ? end - &psh->tracebuf[0] : 0;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | if (pos) {
|
|---|
| 323 | char prefix[40];
|
|---|
| 324 | size_t len;
|
|---|
| 325 |
|
|---|
| 326 | len = sprintf(prefix, "[%d] ", sh_getpid(psh));
|
|---|
| 327 | shfile_write(&psh->fdtab, psh->tracefd, prefix, len);
|
|---|
| 328 | shfile_write(&psh->fdtab, psh->tracefd, psh->tracebuf, pos);
|
|---|
| 329 |
|
|---|
| 330 | psh->tracepos = 0;
|
|---|
| 331 | psh->tracebuf[0] = '\0';
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /* Adds a char to the trace buffer. */
|
|---|
| 336 | static void
|
|---|
| 337 | trace_char(shinstance *psh, int c)
|
|---|
| 338 | {
|
|---|
| 339 | size_t pos = psh->tracepos;
|
|---|
| 340 | if (pos >= sizeof(psh->tracebuf) - 1) {
|
|---|
| 341 | trace_flush(psh);
|
|---|
| 342 | pos = psh->tracepos;
|
|---|
| 343 | }
|
|---|
| 344 | psh->tracebuf[pos] = c;
|
|---|
| 345 | psh->tracepos = pos + 1;
|
|---|
| 346 | if (c == '\n')
|
|---|
| 347 | trace_flush(psh);
|
|---|
| 348 | else
|
|---|
| 349 | psh->tracebuf[pos + 1] = '\0';
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /* Add a string to the trace buffer. */
|
|---|
| 353 | static void
|
|---|
| 354 | trace_string(shinstance *psh, const char *str)
|
|---|
| 355 | {
|
|---|
| 356 | /* push it out line by line. */
|
|---|
| 357 | while (*str) {
|
|---|
| 358 | /* find line/string length. */
|
|---|
| 359 | size_t pos;
|
|---|
| 360 | size_t len;
|
|---|
| 361 | const char *end = str;
|
|---|
| 362 | int flush_it = 0;
|
|---|
| 363 | while (*end) {
|
|---|
| 364 | if (*end++ == '\n') {
|
|---|
| 365 | flush_it = 1;
|
|---|
| 366 | break;
|
|---|
| 367 | }
|
|---|
| 368 | }
|
|---|
| 369 | len = end - str;
|
|---|
| 370 |
|
|---|
| 371 | /* copy to the buffer */
|
|---|
| 372 | pos = psh->tracepos;
|
|---|
| 373 | if (pos + len <= sizeof(psh->tracebuf)) {
|
|---|
| 374 | memcpy(&psh->tracebuf[pos], str, len);
|
|---|
| 375 | psh->tracepos = pos + len;
|
|---|
| 376 | if (flush_it)
|
|---|
| 377 | trace_flush(psh);
|
|---|
| 378 | } else {
|
|---|
| 379 | /* it's too big for some reason... */
|
|---|
| 380 | trace_flush(psh);
|
|---|
| 381 | shfile_write(&psh->fdtab, psh->tracefd, str, len);
|
|---|
| 382 | if (!flush_it)
|
|---|
| 383 | shfile_write(&psh->fdtab, psh->tracefd, "[too long]\n", sizeof( "[too long]\n") - 1);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /* advance */
|
|---|
| 387 | str = end;
|
|---|
| 388 | }
|
|---|
| 389 | }
|
|---|
| 390 | # endif
|
|---|
| 391 |
|
|---|
| 392 | void
|
|---|
| 393 | trputc(shinstance *psh, int c)
|
|---|
| 394 | {
|
|---|
| 395 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 396 | RETURN_IF_NOT_TRACING(psh);
|
|---|
| 397 |
|
|---|
| 398 | # ifdef TRACE_VIA_STDIO
|
|---|
| 399 | putc(c, tracefile);
|
|---|
| 400 | # else
|
|---|
| 401 | trace_char(psh, c);
|
|---|
| 402 | # endif
|
|---|
| 403 | }
|
|---|
| 404 | #endif
|
|---|
| 405 |
|
|---|
| 406 | void
|
|---|
| 407 | trace(shinstance *psh, const char *fmt, ...)
|
|---|
| 408 | {
|
|---|
| 409 | #ifdef DEBUG
|
|---|
| 410 | int savederrno = errno;
|
|---|
| 411 | va_list va;
|
|---|
| 412 | # ifndef TRACE_VIA_STDIO
|
|---|
| 413 | char buf[2048];
|
|---|
| 414 | # endif
|
|---|
| 415 |
|
|---|
| 416 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 417 | RETURN_IF_NOT_TRACING(psh);
|
|---|
| 418 |
|
|---|
| 419 | # ifdef TRACE_VIA_STDIO
|
|---|
| 420 | fprintf(tracefile, "[%d] ", sh_getpid(psh));
|
|---|
| 421 | va_start(va, fmt);
|
|---|
| 422 | (void) vfprintf(tracefile, fmt, va);
|
|---|
| 423 | va_end(va);
|
|---|
| 424 | # else
|
|---|
| 425 | va_start(va, fmt);
|
|---|
| 426 | # ifdef _MSC_VER
|
|---|
| 427 | _vsnprintf(buf, sizeof(buf), fmt, va);
|
|---|
| 428 | # else
|
|---|
| 429 | vsnprintf(buf, sizeof(buf), fmt, va);
|
|---|
| 430 | # endif
|
|---|
| 431 | va_end(va);
|
|---|
| 432 | trace_string(psh, buf);
|
|---|
| 433 | # endif
|
|---|
| 434 |
|
|---|
| 435 | errno = savederrno;
|
|---|
| 436 | #endif
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | void
|
|---|
| 440 | tracev(shinstance *psh, const char *fmt, va_list va)
|
|---|
| 441 | {
|
|---|
| 442 | #ifdef DEBUG
|
|---|
| 443 | int savederrno = errno;
|
|---|
| 444 | # ifndef TRACE_VIA_STDIO
|
|---|
| 445 | char buf[2048];
|
|---|
| 446 | # endif
|
|---|
| 447 |
|
|---|
| 448 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 449 | RETURN_IF_NOT_TRACING(psh);
|
|---|
| 450 |
|
|---|
| 451 | # ifdef TRACE_VIA_STDIO
|
|---|
| 452 | fprintf(tracefile, "[%d] ", sh_getpid(psh));
|
|---|
| 453 | (void) vfprintf(tracefile, fmt, va);
|
|---|
| 454 | # else
|
|---|
| 455 | # ifdef _MSC_VER
|
|---|
| 456 | _vsnprintf(buf, sizeof(buf), fmt, va);
|
|---|
| 457 | # else
|
|---|
| 458 | vsnprintf(buf, sizeof(buf), fmt, va);
|
|---|
| 459 | # endif
|
|---|
| 460 | trace_string(psh, buf);
|
|---|
| 461 | # endif
|
|---|
| 462 |
|
|---|
| 463 | errno = savederrno;
|
|---|
| 464 | #endif
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 | #ifdef DEBUG
|
|---|
| 469 | void
|
|---|
| 470 | trputs(shinstance *psh, const char *s)
|
|---|
| 471 | {
|
|---|
| 472 | int savederrno = errno;
|
|---|
| 473 |
|
|---|
| 474 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 475 | RETURN_IF_NOT_TRACING(psh);
|
|---|
| 476 |
|
|---|
| 477 | # ifdef TRACE_VIA_STDIO
|
|---|
| 478 | fputs(s, tracefile);
|
|---|
| 479 | # else
|
|---|
| 480 | trace_string(psh, s);
|
|---|
| 481 | # endif
|
|---|
| 482 |
|
|---|
| 483 | errno = savederrno;
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 | static void
|
|---|
| 488 | trstring(shinstance *psh, char *s)
|
|---|
| 489 | {
|
|---|
| 490 | int savederrno = errno;
|
|---|
| 491 | char *p;
|
|---|
| 492 | char c;
|
|---|
| 493 |
|
|---|
| 494 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 495 | RETURN_IF_NOT_TRACING(psh);
|
|---|
| 496 |
|
|---|
| 497 | TRACE_PUTC('"');
|
|---|
| 498 | for (p = s ; *p ; p++) {
|
|---|
| 499 | switch (*p) {
|
|---|
| 500 | case '\n': c = 'n'; goto backslash;
|
|---|
| 501 | case '\t': c = 't'; goto backslash;
|
|---|
| 502 | case '\r': c = 'r'; goto backslash;
|
|---|
| 503 | case '"': c = '"'; goto backslash;
|
|---|
| 504 | case '\\': c = '\\'; goto backslash;
|
|---|
| 505 | case CTLESC: c = 'e'; goto backslash;
|
|---|
| 506 | case CTLVAR: c = 'v'; goto backslash;
|
|---|
| 507 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
|
|---|
| 508 | case CTLBACKQ: c = 'q'; goto backslash;
|
|---|
| 509 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
|
|---|
| 510 | backslash: TRACE_PUTC('\\');
|
|---|
| 511 | TRACE_PUTC(c);
|
|---|
| 512 | break;
|
|---|
| 513 | default:
|
|---|
| 514 | if (*p >= ' ' && *p <= '~')
|
|---|
| 515 | TRACE_PUTC(*p);
|
|---|
| 516 | else {
|
|---|
| 517 | TRACE_PUTC('\\');
|
|---|
| 518 | TRACE_PUTC(*p >> 6 & 03);
|
|---|
| 519 | TRACE_PUTC(*p >> 3 & 07);
|
|---|
| 520 | TRACE_PUTC(*p & 07);
|
|---|
| 521 | }
|
|---|
| 522 | break;
|
|---|
| 523 | }
|
|---|
| 524 | }
|
|---|
| 525 | TRACE_PUTC('"');
|
|---|
| 526 |
|
|---|
| 527 | errno = savederrno;
|
|---|
| 528 | }
|
|---|
| 529 | #endif
|
|---|
| 530 |
|
|---|
| 531 |
|
|---|
| 532 | void
|
|---|
| 533 | trargs(shinstance *psh, char **ap)
|
|---|
| 534 | {
|
|---|
| 535 | #ifdef DEBUG
|
|---|
| 536 | int savederrno = errno;
|
|---|
| 537 |
|
|---|
| 538 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 539 | RETURN_IF_NOT_TRACING(psh);
|
|---|
| 540 |
|
|---|
| 541 | while (*ap) {
|
|---|
| 542 | trstring(psh, *ap++);
|
|---|
| 543 | if (*ap)
|
|---|
| 544 | TRACE_PUTC(' ');
|
|---|
| 545 | else
|
|---|
| 546 | TRACE_PUTC('\n');
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | errno = savederrno;
|
|---|
| 550 | #endif
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 | #ifdef DEBUG
|
|---|
| 555 | void
|
|---|
| 556 | opentrace(shinstance *psh)
|
|---|
| 557 | {
|
|---|
| 558 | static const char s[] = "./trace";
|
|---|
| 559 | # ifdef TRACE_VIA_STDIO
|
|---|
| 560 | int fd;
|
|---|
| 561 | # endif
|
|---|
| 562 |
|
|---|
| 563 | TRY_GET_PSH_OR_RETURN(psh);
|
|---|
| 564 | if (debug(psh) != 1) {
|
|---|
| 565 | # ifdef TRACE_VIA_STDIO
|
|---|
| 566 | if (tracefile)
|
|---|
| 567 | fflush(tracefile);
|
|---|
| 568 | /* leave open because libedit might be using it */
|
|---|
| 569 | # else
|
|---|
| 570 | if (psh->tracefd != -1) {
|
|---|
| 571 | trace_flush(psh);
|
|---|
| 572 | shfile_close(&psh->fdtab, psh->tracefd);
|
|---|
| 573 | psh->tracefd = -1;
|
|---|
| 574 | }
|
|---|
| 575 | # endif
|
|---|
| 576 | return;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | # ifdef TRACE_VIA_STDIO
|
|---|
| 580 | if (tracefile) {
|
|---|
| 581 | if (!freopen(s, "a", tracefile)) {
|
|---|
| 582 | fprintf(stderr, "Can't re-open %s\n", s);
|
|---|
| 583 | debug(psh) = 0;
|
|---|
| 584 | return;
|
|---|
| 585 | }
|
|---|
| 586 | } else {
|
|---|
| 587 | fd = open(s, O_APPEND | O_RDWR | O_CREAT, 0600);
|
|---|
| 588 | if (fd != -1) {
|
|---|
| 589 | # if K_OS == K_OS_WINDOWS
|
|---|
| 590 | int fds[50];
|
|---|
| 591 | int i = 0;
|
|---|
| 592 | while (i < 50) {
|
|---|
| 593 | fds[i] = _dup(fd);
|
|---|
| 594 | if (fds[i] == -1 || fds[i] > 199)
|
|---|
| 595 | break;
|
|---|
| 596 | i++;
|
|---|
| 597 | }
|
|---|
| 598 | if (i > 0) {
|
|---|
| 599 | close(fd);
|
|---|
| 600 | fd = fds[--i];
|
|---|
| 601 | }
|
|---|
| 602 | while (i-- > 0)
|
|---|
| 603 | close(fds[i]);
|
|---|
| 604 | # else
|
|---|
| 605 | int fdTarget = 199;
|
|---|
| 606 | while (fdTarget > 10)
|
|---|
| 607 | {
|
|---|
| 608 | int fd2 = shfile_fcntl(&psh->fdtab, fd, F_DUPFD, fdTarget);
|
|---|
| 609 | if (fd2 != -1) {
|
|---|
| 610 | close(fd);
|
|---|
| 611 | fd = fd2;
|
|---|
| 612 | break;
|
|---|
| 613 | }
|
|---|
| 614 | fdTarget = (fdTarget + 1 / 2) - 1;
|
|---|
| 615 | }
|
|---|
| 616 | # endif
|
|---|
| 617 | }
|
|---|
| 618 | if (fd == -1 || (tracefile = fdopen(fd, "a")) == NULL) {
|
|---|
| 619 | fprintf(stderr, "Can't open %s\n", s);
|
|---|
| 620 | debug(psh) = 0;
|
|---|
| 621 | return;
|
|---|
| 622 | }
|
|---|
| 623 | }
|
|---|
| 624 | setvbuf(tracefile, (char *)NULL, _IOLBF, 1024);
|
|---|
| 625 | fputs("\nTracing started.\n", tracefile);
|
|---|
| 626 |
|
|---|
| 627 | # else /* !TRACE_VIA_STDIO */
|
|---|
| 628 | if (psh->tracefd != -1) {
|
|---|
| 629 | return;
|
|---|
| 630 | }
|
|---|
| 631 | psh->tracefd = shfile_open(&psh->fdtab, s, O_APPEND | O_RDWR | O_CREAT, 0600);
|
|---|
| 632 | if (psh->tracefd != -1) {
|
|---|
| 633 | /* relocate it */
|
|---|
| 634 | int fdTarget = 199;
|
|---|
| 635 | while (fdTarget > 10)
|
|---|
| 636 | {
|
|---|
| 637 | int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, fdTarget);
|
|---|
| 638 | if (fd2 != -1) {
|
|---|
| 639 | shfile_close(&psh->fdtab, psh->tracefd);
|
|---|
| 640 | psh->tracefd = fd2;
|
|---|
| 641 | break;
|
|---|
| 642 | }
|
|---|
| 643 | fdTarget = (fdTarget + 1 / 2) - 1;
|
|---|
| 644 | }
|
|---|
| 645 | }
|
|---|
| 646 | if (psh->tracefd == -1) {
|
|---|
| 647 | fprintf(stderr, "Can't open %s\n", s);
|
|---|
| 648 | debug(psh) = 0;
|
|---|
| 649 | return;
|
|---|
| 650 | }
|
|---|
| 651 | trace_string(psh, "Tracing started.\n");
|
|---|
| 652 |
|
|---|
| 653 | # endif /* !TRACE_VIA_STDIO */
|
|---|
| 654 | }
|
|---|
| 655 | #endif /* DEBUG */
|
|---|