Changeset 2297
- Timestamp:
- Mar 1, 2009, 3:04:38 AM (16 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/shfile.c
r2296 r2297 41 41 # include <fcntl.h> 42 42 # include <dirent.h> 43 #endif44 45 #if defined(DEBUG) && defined(TRACE_VIA_STDIO)46 extern FILE *tracefile;47 43 #endif 48 44 … … 611 607 #endif 612 608 613 #if defined(DEBUG) && defined(TRACE_VIA_STDIO) 614 if (tracefile) 615 #endif 616 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno)); 609 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno)); 617 610 return fd; 618 611 } … … 999 992 #endif 1000 993 1001 #ifdef DEBUG 1002 # ifdef TRACE_VIA_STDIO 1003 if (tracefile) 1004 # endif 1005 switch (cmd) 1006 { 1007 case F_GETFL: 1008 TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); 1009 break; 1010 case F_SETFL: 1011 TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); 1012 break; 1013 case F_DUPFD: 1014 TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); 1015 break; 1016 default: 1017 TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); 1018 break; 1019 } 1020 #endif 994 switch (cmd) 995 { 996 case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); break; 997 case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break; 998 case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break; 999 default: TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break; 1000 } 1021 1001 return rc; 1022 1002 } -
trunk/src/kash/show.c
r2296 r2297 263 263 264 264 265 #ifdef DEBUG 265 266 /* 266 267 * Debugging stuff. 267 268 */ 268 269 #ifdef DEBUG270 #ifdef TRACE_VIA_STDIO271 FILE *tracefile;272 #endif273 269 274 270 /** @def TRY_GET_PSH_OR_RETURN … … 284 280 /** @def RETURN_IF_NOT_TRACING 285 281 * 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 282 # define RETURN_IF_NOT_TRACING(psh) \ 283 if (debug(psh) != 1 || psh->tracefd == -1) \ 284 return; \ 285 else do {} while (0) 286 309 287 /* Flushes the tracebuf. */ 310 288 static void … … 321 299 322 300 if (pos) { 301 int s = errno; 323 302 char prefix[40]; 324 303 size_t len; … … 330 309 psh->tracepos = 0; 331 310 psh->tracebuf[0] = '\0'; 311 312 errno = s; 332 313 } 333 314 } … … 378 359 } else { 379 360 /* it's too big for some reason... */ 361 int s = errno; 380 362 trace_flush(psh); 381 363 shfile_write(&psh->fdtab, psh->tracefd, str, len); 382 364 if (!flush_it) 383 365 shfile_write(&psh->fdtab, psh->tracefd, "[too long]\n", sizeof( "[too long]\n") - 1); 366 errno = s; 384 367 } 385 368 … … 388 371 } 389 372 } 390 # endif391 373 392 374 void … … 396 378 RETURN_IF_NOT_TRACING(psh); 397 379 398 # ifdef TRACE_VIA_STDIO399 putc(c, tracefile);400 # else401 380 trace_char(psh, c); 402 # endif 403 } 404 #endif 381 } 405 382 406 383 void 407 384 trace(shinstance *psh, const char *fmt, ...) 408 385 { 409 #ifdef DEBUG410 int savederrno = errno;411 386 va_list va; 412 # ifndef TRACE_VIA_STDIO413 387 char buf[2048]; 414 # endif415 388 416 389 TRY_GET_PSH_OR_RETURN(psh); 417 390 RETURN_IF_NOT_TRACING(psh); 418 391 419 # ifdef TRACE_VIA_STDIO420 fprintf(tracefile, "[%d] ", sh_getpid(psh));421 va_start(va, fmt);422 (void) vfprintf(tracefile, fmt, va);423 va_end(va);424 # else425 392 va_start(va, fmt); 426 393 # ifdef _MSC_VER … … 431 398 va_end(va); 432 399 trace_string(psh, buf); 433 # endif434 435 errno = savederrno;436 #endif437 400 } 438 401 … … 440 403 tracev(shinstance *psh, const char *fmt, va_list va) 441 404 { 442 #ifdef DEBUG443 int savederrno = errno;444 # ifndef TRACE_VIA_STDIO445 405 char buf[2048]; 446 # endif447 406 448 407 TRY_GET_PSH_OR_RETURN(psh); 449 408 RETURN_IF_NOT_TRACING(psh); 450 409 451 # ifdef TRACE_VIA_STDIO452 fprintf(tracefile, "[%d] ", sh_getpid(psh));453 (void) vfprintf(tracefile, fmt, va);454 # else455 410 # ifdef _MSC_VER 456 411 _vsnprintf(buf, sizeof(buf), fmt, va); … … 459 414 # endif 460 415 trace_string(psh, buf); 461 # endif 462 463 errno = savederrno; 464 #endif 465 } 466 467 468 #ifdef DEBUG 416 } 417 469 418 void 470 419 trputs(shinstance *psh, const char *s) 471 420 { 472 int savederrno = errno;473 474 421 TRY_GET_PSH_OR_RETURN(psh); 475 422 RETURN_IF_NOT_TRACING(psh); 476 423 477 # ifdef TRACE_VIA_STDIO478 fputs(s, tracefile);479 # else480 424 trace_string(psh, s); 481 # endif 482 483 errno = savederrno; 425 trace_char(psh, '\n'); 484 426 } 485 427 … … 488 430 trstring(shinstance *psh, char *s) 489 431 { 490 int savederrno = errno;491 432 char *p; 492 433 char c; … … 495 436 RETURN_IF_NOT_TRACING(psh); 496 437 497 TRACE_PUTC('"');438 trace_char(psh, '"'); 498 439 for (p = s ; *p ; p++) { 499 440 switch (*p) { … … 508 449 case CTLBACKQ: c = 'q'; goto backslash; 509 450 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; 510 backslash: TRACE_PUTC('\\');511 TRACE_PUTC(c);451 backslash: trace_char(psh, '\\'); 452 trace_char(psh, c); 512 453 break; 513 454 default: 514 455 if (*p >= ' ' && *p <= '~') 515 TRACE_PUTC(*p);456 trace_char(psh, *p); 516 457 else { 517 TRACE_PUTC('\\');518 TRACE_PUTC(*p >> 6 & 03);519 TRACE_PUTC(*p >> 3 & 07);520 TRACE_PUTC(*p & 07);458 trace_char(psh, '\\'); 459 trace_char(psh, *p >> 6 & 03); 460 trace_char(psh, *p >> 3 & 07); 461 trace_char(psh, *p & 07); 521 462 } 522 463 break; 523 464 } 524 465 } 525 TRACE_PUTC('"'); 526 527 errno = savederrno; 528 } 529 #endif 530 466 trace_char(psh, '"'); 467 } 531 468 532 469 void 533 470 trargs(shinstance *psh, char **ap) 534 471 { 535 #ifdef DEBUG536 int savederrno = errno;537 538 472 TRY_GET_PSH_OR_RETURN(psh); 539 473 RETURN_IF_NOT_TRACING(psh); … … 542 476 trstring(psh, *ap++); 543 477 if (*ap) 544 TRACE_PUTC(' ');478 trace_char(psh, ' '); 545 479 else 546 TRACE_PUTC('\n'); 547 } 548 549 errno = savederrno; 550 #endif 551 } 552 553 554 #ifdef DEBUG 480 trace_char(psh, '\n'); 481 } 482 } 483 555 484 void 556 485 opentrace(shinstance *psh) 557 486 { 558 487 static const char s[] = "./trace"; 559 # ifdef TRACE_VIA_STDIO560 int fd;561 # endif562 488 563 489 TRY_GET_PSH_OR_RETURN(psh); 564 490 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 491 /* disabled */ 570 492 if (psh->tracefd != -1) { 571 493 trace_flush(psh); … … 573 495 psh->tracefd = -1; 574 496 } 575 # endif576 497 return; 577 498 } 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 } 499 /* else: (re-)enabled */ 500 501 if (psh->tracefd != -1) 502 return; 503 631 504 psh->tracefd = shfile_open(&psh->fdtab, s, O_APPEND | O_RDWR | O_CREAT, 0600); 632 505 if (psh->tracefd != -1) { 633 506 /* relocate it */ 634 int fdTarget= 199;635 while ( fdTarget> 10)507 int want_fd = 199; 508 while (want_fd > 10) 636 509 { 637 int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, fdTarget);510 int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, want_fd); 638 511 if (fd2 != -1) { 639 512 shfile_close(&psh->fdtab, psh->tracefd); … … 641 514 break; 642 515 } 643 fdTarget = (fdTarget + 1/ 2) - 1;516 want_fd = ((want_fd + 1) / 2) - 1; 644 517 } 645 518 } … … 650 523 } 651 524 trace_string(psh, "Tracing started.\n"); 652 653 # endif /* !TRACE_VIA_STDIO */ 654 } 525 } 526 655 527 #endif /* DEBUG */ 528 -
trunk/src/kash/show.h
r1233 r2297 39 39 union node; 40 40 void showtree(struct shinstance *, union node *); 41 #ifdef DEBUG 41 42 void trace(struct shinstance *, const char *, ...); 42 43 void tracev(struct shinstance *, const char *, va_list); 43 44 void trargs(struct shinstance *, char **); 44 #ifdef DEBUG45 45 void trputc(struct shinstance *, int); 46 46 void trputs(struct shinstance *, const char *);
Note:
See TracChangeset
for help on using the changeset viewer.