output.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. *
  3. * debugger
  4. *
  5. */
  6. #include "defs.h"
  7. #include "fns.h"
  8. int printcol = 0;
  9. int infile = STDIN;
  10. int maxpos = MAXPOS;
  11. Biobuf stdout;
  12. void
  13. printc(int c)
  14. {
  15. dprint("%c", c);
  16. }
  17. /* was move to next f1-sized tab stop; now just print a tab */
  18. int
  19. tconv(Fmt *f)
  20. {
  21. return fmtstrcpy(f, "\t");
  22. }
  23. void
  24. flushbuf(void)
  25. {
  26. if (printcol != 0)
  27. printc(EOR);
  28. }
  29. void
  30. prints(char *s)
  31. {
  32. dprint("%s",s);
  33. }
  34. void
  35. newline(void)
  36. {
  37. printc(EOR);
  38. }
  39. #define MAXIFD 5
  40. struct {
  41. int fd;
  42. int r9;
  43. } istack[MAXIFD];
  44. int ifiledepth;
  45. void
  46. iclose(int stack, int err)
  47. {
  48. if (err) {
  49. if (infile) {
  50. close(infile);
  51. infile=STDIN;
  52. }
  53. while (--ifiledepth >= 0)
  54. if (istack[ifiledepth].fd)
  55. close(istack[ifiledepth].fd);
  56. ifiledepth = 0;
  57. } else if (stack == 0) {
  58. if (infile) {
  59. close(infile);
  60. infile=STDIN;
  61. }
  62. } else if (stack > 0) {
  63. if (ifiledepth >= MAXIFD)
  64. error("$<< nested too deeply");
  65. istack[ifiledepth].fd = infile;
  66. ifiledepth++;
  67. infile = STDIN;
  68. } else {
  69. if (infile) {
  70. close(infile);
  71. infile=STDIN;
  72. }
  73. if (ifiledepth > 0) {
  74. infile = istack[--ifiledepth].fd;
  75. }
  76. }
  77. }
  78. void
  79. oclose(void)
  80. {
  81. flushbuf();
  82. Bterm(&stdout);
  83. Binit(&stdout, 1, OWRITE);
  84. }
  85. void
  86. redirout(char *file)
  87. {
  88. int fd;
  89. if (file == 0){
  90. oclose();
  91. return;
  92. }
  93. flushbuf();
  94. if ((fd = open(file, 1)) >= 0)
  95. seek(fd, 0L, 2);
  96. else if ((fd = create(file, 1, 0666)) < 0)
  97. error("cannot create");
  98. Bterm(&stdout);
  99. Binit(&stdout, fd, OWRITE);
  100. }
  101. void
  102. endline(void)
  103. {
  104. if (maxpos <= printcol)
  105. newline();
  106. }
  107. void
  108. flush(void)
  109. {
  110. Bflush(&stdout);
  111. }
  112. int
  113. dprint(char *fmt, ...)
  114. {
  115. int n, w;
  116. char *p;
  117. char buf[4096];
  118. Rune r;
  119. va_list arg;
  120. if(mkfault)
  121. return -1;
  122. va_start(arg, fmt);
  123. n = vseprint(buf, buf+sizeof buf, fmt, arg) - buf;
  124. va_end(arg);
  125. //Bprint(&stdout, "[%s]", fmt);
  126. Bwrite(&stdout, buf, n);
  127. for(p=buf; *p; p+=w){
  128. w = chartorune(&r, p);
  129. if(r == '\n')
  130. printcol = 0;
  131. else
  132. printcol++;
  133. }
  134. return n;
  135. }
  136. void
  137. outputinit(void)
  138. {
  139. Binit(&stdout, 1, OWRITE);
  140. fmtinstall('t', tconv);
  141. }