Blob
1 /*2 *3 * debugger4 *5 */7 #include "defs.h"8 #include "fns.h"10 int printcol = 0;11 int infile = STDIN;12 int maxpos = MAXPOS;14 Biobuf bstdout;16 void17 printc(int c)18 {19 dprint("%c", c);20 }22 /* was move to next f1-sized tab stop; now just print a tab */23 int24 tconv(Fmt *f)25 {26 return fmtstrcpy(f, "\t");27 }29 void30 flushbuf(void)31 {32 if (printcol != 0)33 printc(EOR);34 }36 void37 prints(char *s)38 {39 dprint("%s",s);40 }42 void43 newline(void)44 {45 printc(EOR);46 }48 #define MAXIFD 549 struct {50 int fd;51 int r9;52 } istack[MAXIFD];53 int ifiledepth;55 void56 iclose(int stack, int err)57 {58 if (err) {59 if (infile) {60 close(infile);61 infile=STDIN;62 }63 while (--ifiledepth >= 0)64 if (istack[ifiledepth].fd)65 close(istack[ifiledepth].fd);66 ifiledepth = 0;67 } else if (stack == 0) {68 if (infile) {69 close(infile);70 infile=STDIN;71 }72 } else if (stack > 0) {73 if (ifiledepth >= MAXIFD)74 error("$<< nested too deeply");75 istack[ifiledepth].fd = infile;76 ifiledepth++;77 infile = STDIN;78 } else {79 if (infile) {80 close(infile);81 infile=STDIN;82 }83 if (ifiledepth > 0) {84 infile = istack[--ifiledepth].fd;85 }86 }87 }89 void90 oclose(void)91 {92 flushbuf();93 Bterm(&bstdout);94 Binit(&bstdout, 1, OWRITE);95 }97 void98 redirout(char *file)99 {100 int fd;102 if (file == 0){103 oclose();104 return;105 }106 flushbuf();107 if ((fd = open(file, 1)) >= 0)108 seek(fd, 0L, 2);109 else if ((fd = create(file, 1, 0666)) < 0)110 error("cannot create");111 Bterm(&bstdout);112 Binit(&bstdout, fd, OWRITE);113 }115 void116 endline(void)117 {119 if (maxpos <= printcol)120 newline();121 }123 void124 flush(void)125 {126 Bflush(&bstdout);127 }129 int130 dprint(char *fmt, ...)131 {132 int n, w;133 char *p;134 char buf[4096];135 Rune r;136 va_list arg;138 if(mkfault)139 return -1;140 va_start(arg, fmt);141 n = vseprint(buf, buf+sizeof buf, fmt, arg) - buf;142 va_end(arg);143 Bwrite(&bstdout, buf, n);144 for(p=buf; *p; p+=w){145 w = chartorune(&r, p);146 if(r == '\n')147 printcol = 0;148 else149 printcol++;150 }151 return n;152 }154 void155 outputinit(void)156 {157 Binit(&bstdout, 1, OWRITE);158 fmtinstall('t', tconv);159 }