output.c 2.4 KB

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