6 void pfmt(io *f, char *fmt, ...){
12 if(*fmt!='%') pchr(f, *fmt);
14 case '\0': va_end(ap); return;
15 case 'c': pchr(f, va_arg(ap, int)); break;
16 case 'd': pdec(f, va_arg(ap, int)); break;
17 case 'o': poct(f, va_arg(ap, unsigned)); break;
18 case 'p': phex(f, (long)va_arg(ap, char *)); break; /*unportable*/
19 case 'Q': pquo(f, va_arg(ap, char *)); break;
20 case 'q': pwrd(f, va_arg(ap, char *)); break;
21 case 'r': errstr(err, sizeof err); pstr(f, err); break;
22 case 's': pstr(f, va_arg(ap, char *)); break;
23 case 't': pcmd(f, va_arg(ap, struct tree *)); break;
24 case 'v': pval(f, va_arg(ap, struct word *)); break;
25 default: pchr(f, *fmt); break;
28 if(--pfmtnest==0) flush(f);
30 void pchr(io *b, int c)
32 if(b->bufp==b->ebuf) fullbuf(b, c);
37 if(b->bufp==b->ebuf) return emptybuf(b);
38 return *b->bufp++ & 0xFF;
41 void pquo(io *f, char *s)
45 if(*s=='\'') pfmt(f, "''");
49 void pwrd(io *f, char *s)
52 for(t=s;*t;t++) if(!wordchr(*t)) break;
53 if(t==s || *t) pquo(f, s);
56 void phex(io *f, long p)
59 for(n=28;n>=0;n-=4) pchr(f, "0123456789ABCDEF"[(p>>n)&0xF]);
61 void pstr(io *f, char *s)
64 while(*s) pchr(f, *s++);
66 void pdec(io *f, long n)
75 /* n is two's complement minimum integer */
82 if(n>9) pdec(f, n/10);
85 void poct(io *f, ulong n)
87 if(n>7) poct(f, n>>3);
90 void pval(io *f, word *a)
93 while(a->next && a->next->word){
101 int fullbuf(io *f, int c)
112 f->strp=realloc(f->strp, n+101);
113 if(f->strp==0) panic("Can't realloc %d bytes in flush!", n+101);
116 for(s=f->bufp;s<=f->ebuf;s++) *s='\0';
120 if(n && Write(f->fd, f->buf, n) < 0){
121 Write(3, "Write error\n", 12);
132 f->bufp=f->ebuf=f->buf;
137 io *f=new(struct io);
140 f->bufp=f->strp=emalloc(101);
142 for(s=f->bufp;s<=f->ebuf;s++) *s='\0';
146 * Open a corebuffer to read. EOF occurs after reading len
147 * characters from buf.
149 io *opencore(char *s, int len)
151 io *f=new(struct io);
152 char *buf=emalloc(len);
153 f->fd= -1 /*open("/dev/null", 0)*/;
162 if(io->fd==-1) io->bufp=io->strp;
164 io->bufp=io->ebuf=io->buf;
171 if(io->fd>=0) close(io->fd);
172 if(io->strp) efree(io->strp);
178 if(f->fd==-1 || (n=Read(f->fd, f->buf, NBUF))<=0) return EOF;
181 return *f->bufp++&0xff;