1 /* 2 * 3 * debugger 4 * 5 */ 6 7 #include "defs.h" 8 #include "fns.h" 9 10 int infile = STDIN; 11 int maxpos = MAXPOS; 12 13 Biobuf stdout; 14 15 extern int printcol; 16 17 void 18 printc(int c) 19 { 20 dprint("%c", c); 21 } 22 23 /* move to next f1-sized tab stop */ 24 int 25 tconv(void *oa, Fconv *f) 26 { 27 int n; 28 char buf[128]; 29 30 USED(oa); 31 n = f->f1 - (printcol%f->f1); 32 if(n != 0) { 33 memset(buf, ' ', n); 34 buf[n] = '\0'; 35 f->f1 = 0; 36 strconv(buf, f); 37 } 38 return 0; 39 } 40 41 /* hexadecimal with initial # */ 42 int 43 myxconv(void *oa, Fconv *f) 44 { 45 46 /* if unsigned, emit #1234 */ 47 if((f->f3&32) || *(long*)oa>=0){ 48 if(f->out < f->eout) 49 *f->out++ = '#'; 50 } 51 else{ /* emit -#1234 */ 52 *(long*)oa = -*(long*)oa; 53 if(f->out < f->eout-1) { 54 *f->out++ = '-'; 55 *f->out++ = '#'; 56 } 57 } 58 numbconv(oa, f); 59 return sizeof(long); 60 } 61 62 charpos(void) 63 { 64 return printcol; 65 } 66 67 void 68 flushbuf(void) 69 { 70 if (printcol != 0) 71 printc(EOR); 72 } 73 74 void 75 prints(char *s) 76 { 77 dprint("%s",s); 78 } 79 80 void 81 newline(void) 82 { 83 printc(EOR); 84 } 85 86 #define MAXIFD 5 87 struct { 88 int fd; 89 int r9; 90 } istack[MAXIFD]; 91 int ifiledepth; 92 93 void 94 iclose(int stack, int err) 95 { 96 if (err) { 97 if (infile) { 98 close(infile); 99 infile=STDIN; 100 } 101 while (--ifiledepth >= 0) 102 if (istack[ifiledepth].fd) 103 close(istack[ifiledepth].fd); 104 ifiledepth = 0; 105 } else if (stack == 0) { 106 if (infile) { 107 close(infile); 108 infile=STDIN; 109 } 110 } else if (stack > 0) { 111 if (ifiledepth >= MAXIFD) 112 error("$<< nested too deeply"); 113 istack[ifiledepth].fd = infile; 114 ifiledepth++; 115 infile = STDIN; 116 } else { 117 if (infile) { 118 close(infile); 119 infile=STDIN; 120 } 121 if (ifiledepth > 0) { 122 infile = istack[--ifiledepth].fd; 123 } 124 } 125 } 126 127 void 128 oclose(void) 129 { 130 flushbuf(); 131 Bterm(&stdout); 132 Binit(&stdout, 1, OWRITE); 133 } 134 135 void 136 redirout(char *file) 137 { 138 int fd; 139 140 if (file == 0){ 141 oclose(); 142 return; 143 } 144 flushbuf(); 145 if ((fd = open(file, 1)) >= 0) 146 seek(fd, 0L, 2); 147 else if ((fd = create(file, 1, 0666)) < 0) 148 error("cannot create"); 149 Bterm(&stdout); 150 Binit(&stdout, fd, OWRITE); 151 } 152 153 void 154 endline(void) 155 { 156 157 if (maxpos <= charpos()) 158 newline(); 159 } 160 161 void 162 flush(void) 163 { 164 Bflush(&stdout); 165 } 166 167 int 168 dprint(char *fmt, ...) 169 { 170 char buf[4096], *out; 171 int n; 172 173 if(mkfault) 174 return -1; 175 out = doprint(buf, buf+sizeof buf, fmt, (&fmt+1)); 176 n = Bwrite(&stdout, buf, (long)(out-buf)); 177 return n; 178 } 179 180 void 181 outputinit(void) 182 { 183 Binit(&stdout, 1, OWRITE); 184 fmtinstall('t', tconv); 185 fmtinstall('x', myxconv); 186 } 187