Files
plan9port/src/cmd/rc/io.c

265 lines
3.7 KiB
C
Raw Normal View History

#include <limits.h>
#include "rc.h"
#include "exec.h"
#include "io.h"
#include "fns.h"
2007-03-26 12:02:41 +00:00
int pfmtnest = 0;
void
pfmt(io *f, char *fmt, ...)
{
va_list ap;
char err[ERRMAX];
va_start(ap, fmt);
pfmtnest++;
for(;*fmt;fmt++)
2007-03-26 12:02:41 +00:00
if(*fmt!='%')
pchr(f, *fmt);
else switch(*++fmt){
2007-03-26 12:02:41 +00:00
case '\0':
va_end(ap);
return;
case 'c':
pchr(f, va_arg(ap, int));
break;
case 'd':
pdec(f, va_arg(ap, int));
break;
case 'o':
poct(f, va_arg(ap, unsigned));
break;
case 'p':
pptr(f, va_arg(ap, void*));
break;
case 'Q':
pquo(f, va_arg(ap, char *));
break;
case 'q':
pwrd(f, va_arg(ap, char *));
break;
case 'r':
2007-03-26 15:02:15 +00:00
rerrstr(err, sizeof err); pstr(f, err);
2007-03-26 12:02:41 +00:00
break;
case 's':
pstr(f, va_arg(ap, char *));
break;
case 't':
rc: add recursive descent parser The old yacc-based parser is available with the -Y flag, which will probably be removed at some point. The new -D flag dumps a parse tree of the input, without executing it. This allows comparing the output of rc -D and rc -DY on different scripts to see that the two parsers behave the same. The rc paper ends by saying: It is remarkable that in the four most recent editions of the UNIX system programmer’s manual the Bourne shell grammar described in the manual page does not admit the command who|wc. This is surely an oversight, but it suggests something darker: nobody really knows what the Bourne shell’s grammar is. Even examination of the source code is little help. The parser is implemented by recursive descent, but the routines corresponding to the syntactic categories all have a flag argument that subtly changes their operation depending on the context. Rc’s parser is implemented using yacc, so I can say precisely what the grammar is. The new recursive descent parser here has no such flags. It is a straightforward translation of the yacc. The new parser will make it easier to handle free carats in more generality as well as potentially allow the use of unquoted = as a word character. Going through this exercise has highlighted a few dark corners here as well. For example, I was surprised to find that x >f | y >f x | y are different commands (the latter redirects y's output). It is similarly surprising that a=b x | y sets a during the execution of y. It is also a bit counter-intuitive x | y | z x | if(c) y | z are not both 3-phase pipelines. These are certainly not things we should change, but they are not entirely obvious from the man page description, undercutting the quoted claim a bit. On the other hand, who | wc is clearly accepted by the grammar in the manual page, and the new parser still handles that test case.
2020-05-04 18:34:19 -04:00
pcmd(f, va_arg(ap, tree *));
break;
case 'u':
pcmdu(f, va_arg(ap, tree *));
2007-03-26 12:02:41 +00:00
break;
case 'v':
pval(f, va_arg(ap, struct word *));
break;
default:
pchr(f, *fmt);
break;
}
va_end(ap);
2007-03-26 12:02:41 +00:00
if(--pfmtnest==0)
flush(f);
}
2007-03-26 12:02:41 +00:00
void
pchr(io *b, int c)
{
2007-03-26 12:02:41 +00:00
if(b->bufp==b->ebuf)
fullbuf(b, c);
else *b->bufp++=c;
}
2007-03-26 12:02:41 +00:00
int
rchr(io *b)
{
2007-03-26 12:02:41 +00:00
if(b->bufp==b->ebuf)
return emptybuf(b);
return *b->bufp++ & 0xFF;
}
2007-03-26 12:02:41 +00:00
void
pquo(io *f, char *s)
{
pchr(f, '\'');
for(;*s;s++)
2007-03-26 12:02:41 +00:00
if(*s=='\'')
pfmt(f, "''");
else pchr(f, *s);
pchr(f, '\'');
}
2007-03-26 12:02:41 +00:00
void
pwrd(io *f, char *s)
{
char *t;
2007-03-26 12:02:41 +00:00
for(t = s;*t;t++) if(!wordchr(*t)) break;
if(t==s || *t)
pquo(f, s);
else pstr(f, s);
}
2007-03-26 12:02:41 +00:00
void
pptr(io *f, void *v)
{
int n;
2007-03-26 12:02:41 +00:00
uintptr p;
p = (uintptr)v;
if(sizeof(uintptr) == sizeof(uvlong) && p>>32)
for(n = 60;n>=32;n-=4) pchr(f, "0123456789ABCDEF"[(p>>n)&0xF]);
for(n = 28;n>=0;n-=4) pchr(f, "0123456789ABCDEF"[(p>>n)&0xF]);
}
2007-03-26 12:02:41 +00:00
void
pstr(io *f, char *s)
{
2007-03-26 12:02:41 +00:00
if(s==0)
s="(null)";
while(*s) pchr(f, *s++);
}
2007-03-26 12:02:41 +00:00
void
pdec(io *f, int n)
{
if(n<0){
if(n!=INT_MIN){
pchr(f, '-');
pdec(f, -n);
return;
}
/* n is two's complement minimum integer */
n = -(INT_MIN+1);
pchr(f, '-');
pdec(f, n/10);
pchr(f, n%10+'1');
return;
}
2007-03-26 12:02:41 +00:00
if(n>9)
pdec(f, n/10);
pchr(f, n%10+'0');
}
2007-03-26 12:02:41 +00:00
void
poct(io *f, unsigned n)
{
2007-03-26 12:02:41 +00:00
if(n>7)
poct(f, n>>3);
pchr(f, (n&7)+'0');
}
2007-03-26 12:02:41 +00:00
void
pval(io *f, word *a)
{
if(a){
while(a->next && a->next->word){
pwrd(f, a->word);
pchr(f, ' ');
2007-03-26 12:02:41 +00:00
a = a->next;
}
pwrd(f, a->word);
}
}
2007-03-26 12:02:41 +00:00
int
fullbuf(io *f, int c)
{
flush(f);
return *f->bufp++=c;
}
2007-03-26 12:02:41 +00:00
void
flush(io *f)
{
int n;
char *s;
if(f->strp){
2007-03-26 12:02:41 +00:00
n = f->ebuf-f->strp;
f->strp = realloc(f->strp, n+101);
if(f->strp==0)
panic("Can't realloc %d bytes in flush!", n+101);
f->bufp = f->strp+n;
f->ebuf = f->bufp+100;
for(s = f->bufp;s<=f->ebuf;s++) *s='\0';
}
else{
2007-03-26 12:02:41 +00:00
n = f->bufp-f->buf;
if(n && Write(f->fd, f->buf, n) < 0){
Write(3, "Write error\n", 12);
2007-03-26 12:02:41 +00:00
if(ntrap)
dotrap();
}
2007-03-26 12:02:41 +00:00
f->bufp = f->buf;
f->ebuf = f->buf+NBUF;
}
}
2007-03-26 12:02:41 +00:00
io*
openfd(int fd)
{
io *f = new(struct io);
f->fd = fd;
f->bufp = f->ebuf = f->buf;
f->strp = 0;
return f;
}
2007-03-26 12:02:41 +00:00
io*
openstr(void)
{
io *f = new(struct io);
char *s;
f->fd=-1;
2007-03-26 12:02:41 +00:00
f->bufp = f->strp = emalloc(101);
f->ebuf = f->bufp+100;
for(s = f->bufp;s<=f->ebuf;s++) *s='\0';
return f;
}
/*
* Open a corebuffer to read. EOF occurs after reading len
* characters from buf.
*/
2007-03-26 12:02:41 +00:00
io*
opencore(char *s, int len)
{
2007-03-26 12:02:41 +00:00
io *f = new(struct io);
char *buf = emalloc(len);
f->fd= -1 /*open("/dev/null", 0)*/;
2007-03-26 12:02:41 +00:00
f->bufp = f->strp = buf;
f->ebuf = buf+len;
Memcpy(buf, s, len);
return f;
}
2007-03-26 12:02:41 +00:00
void
2007-03-26 17:04:41 +00:00
iorewind(io *io)
{
2007-03-26 12:02:41 +00:00
if(io->fd==-1)
io->bufp = io->strp;
else{
2007-03-26 12:02:41 +00:00
io->bufp = io->ebuf = io->buf;
Seek(io->fd, 0L, 0);
}
}
2007-03-26 12:02:41 +00:00
void
closeio(io *io)
{
2007-03-26 12:02:41 +00:00
if(io->fd>=0)
close(io->fd);
if(io->strp)
efree(io->strp);
efree((char *)io);
}
2007-03-26 12:02:41 +00:00
int
emptybuf(io *f)
{
int n;
2007-03-26 12:02:41 +00:00
if(f->fd==-1 || (n = Read(f->fd, f->buf, NBUF))<=0) return EOF;
f->bufp = f->buf;
f->ebuf = f->buf+n;
return *f->bufp++&0xff;
}