void pfmt(Io *f, char *fmt, ...) { va_list ap; va_start(ap, fmt); pfmtnest++; for(;*fmt;fmt++) { if(*fmt!='%') pchr(f, *fmt); else switch(*++fmt){ 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': phex(f, (long)va_arg(ap, char *)); break; /*unportable*/ case 'Q': pquo(f, va_arg(ap, char *)); break; case 'q': pwrd(f, va_arg(ap, char *)); break; case 'r': perr(f); break; case 's': pstr(f, va_arg(ap, char *)); break; case 't': pcmd(f, va_arg(ap, Tree *)); break; case 'v': pval(f, va_arg(ap, Word *)); break; default: pchr(f, *fmt); break; } } va_end(ap); if(--pfmtnest==0) flush(f); }
void pquo(Io *f, char *s) { pchr(f, '\''); for(;*s;s++) if(*s=='\'') pfmt(f, "''"); else pchr(f, *s); pchr(f, '\''); }
void pstr(Io *f, char *s) { if(s==0) s="(null)"; while(*s) pchr(f, *s++); }
void panic(char *s, int n) { pfmt(err, "rc: "); pfmt(err, s, n); pchr(err, '\n'); flush(err); Abort(); }
void pdeglob(io *f, char *s) { while(*s){ if(*s==GLOB) s++; pchr(f, *s++); } }
void pdec(Io *f, long n) { if(n<0){ n=-n; if(n>=0){ pchr(f, '-'); pdec(f, n); return; } /* n is two's complement minimum integer */ n=1-n; pchr(f, '-'); pdec(f, n/10); pchr(f, n%10+'1'); return; } if(n>9) pdec(f, n/10); pchr(f, n%10+'0'); }
void pval(Io *f, Word *a) { if(a){ while(a->next && a->next->word){ pwrd(f, a->word); pchr(f, ' '); a=a->next; } pwrd(f, a->word); } }
void pfnc(io *fd, thread *t) { int i; void (*fn)(void) = t->code[t->pc].f; list *a; pfmt(fd, "pid %d cycle %p %d ", getpid(), t->code, t->pc); for (i = 0; fname[i].f; i++) if (fname[i].f == fn) { pstr(fd, fname[i].name); break; } if (!fname[i].f) pfmt(fd, "%p", fn); for (a = t->argv; a; a = a->next) pfmt(fd, " (%v)", a->words); pchr(fd, '\n'); flush(fd); }
void Xrdcmds(void) { struct thread *p = runq; word *prompt; flush(err); nerror = 0; if(flag['s'] && !truestatus()) pfmt(err, "status=%v\n", vlook("status")->val); if(runq->iflag){ prompt = vlook("prompt")->val; if(prompt) promptstr = prompt->word; else promptstr="% "; } Noerror(); if(yyparse()){ if(!p->iflag || p->eof && !Eintr()){ if(p->cmdfile) efree(p->cmdfile); closeio(p->cmdfd); Xreturn(); /* should this be omitted? */ } else{ if(Eintr()){ pchr(err, '\n'); p->eof = 0; } --p->pc; /* go back for next command */ } } else{ ntrap = 0; /* avoid double-interrupts during blocked writes */ --p->pc; /* re-execute Xrdcmds after codebuf runs */ start(codebuf, 1, runq->local); } freenodes(); }
/* * Yymain initializes each of the utility * clusters and then starts the processing * by calling yyparse. */ yymain() { #ifdef OBJ /* * initialize symbol table temp files */ startnlfile(); #endif /* * Initialize the scanner */ #ifdef PXP if (bracket == 0) { #endif if (getline() == -1) { Perror(filename, "No lines in file"); pexit(NOSTART); } #ifdef PXP } else yyline = 0; #endif #ifdef PI # ifdef OBJ magic(); # endif OBJ #endif line = 1; errpfx = 'E'; /* * Initialize the clusters * initstring(); */ inithash(); inittree(); #ifdef PI initnl(); #endif /* * Process the input */ yyparse(); #ifdef PI # ifdef OBJ /* * save outermost block of namelist */ savenl(NLNIL); magic2(); # endif OBJ # ifdef DEBUG dumpnl(NLNIL); # endif #endif #ifdef PXP prttab(); if (onefile) { extern int outcol; if (outcol) pchr('\n'); flush(); if (eflg) { writef(2, "File not rewritten because of errors\n"); pexit(ERRS); } (void) signal(SIGHUP, SIG_IGN); (void) signal(SIGINT, SIG_IGN); copyfile(); } #endif pexit(eflg ? ERRS : AOK); }
void pcmd(io *f, tree *t) { if(t==0) return; assert(f != nil); switch(t->type){ default: pfmt(f, "bad cmd %d %p %p %p", t->type, c0, c1, c2); break; case '$': pfmt(f, "$%t", c0); break; case '"': pfmt(f, "$\"%t", c0); break; case '&': pfmt(f, "%t&", c0); break; case '^': pfmt(f, "%t^%t", c0, c1); break; case '`': pfmt(f, "`%t", c0); break; case ANDAND: pfmt(f, "%t && %t", c0, c1); break; case BANG: pfmt(f, "! %t", c0); break; case BRACE: pfmt(f, "{%t}", c0); break; case COUNT: pfmt(f, "$#%t", c0); break; case FN: pfmt(f, "fn %t %t", c0, c1); break; case IF: pfmt(f, "if%t%t", c0, c1); break; case NOT: pfmt(f, "if not %t", c0); break; case OROR: pfmt(f, "%t || %t", c0, c1); break; case PCMD: case PAREN: pfmt(f, "(%t)", c0); break; case SUB: pfmt(f, "$%t(%t)", c0, c1); break; case SIMPLE: pfmt(f, "%t", c0); break; case SUBSHELL: pfmt(f, "@ %t", c0); break; case SWITCH: pfmt(f, "switch %t %t", c0, c1); break; case TWIDDLE: pfmt(f, "~ %t %t", c0, c1); break; case WHILE: pfmt(f, "while %t%t", c0, c1); break; case ARGLIST: if(c0==0) pfmt(f, "%t", c1); else if(c1==0) pfmt(f, "%t", c0); else pfmt(f, "%t %t", c0, c1); break; case ';': if(c0){ if(c1) pfmt(f, "%t%c%t", c0, nl, c1); else pfmt(f, "%t", c0); } else pfmt(f, "%t", c1); break; case WORDS: if(c0) pfmt(f, "%t ", c0); pfmt(f, "%t", c1); break; case FOR: pfmt(f, "for(%t", c0); if(c1) pfmt(f, " in %t", c1); pfmt(f, ")%t", c2); break; case WORD: if(t->quoted) pfmt(f, "%Q", t->str); else pdeglob(f, t->str); break; case DUP: if(t->rtype==DUPFD) pfmt(f, ">[%d=%d]", t->fd1, t->fd0); /* yes, fd1, then fd0; read lex.c */ else pfmt(f, ">[%d=]", t->fd0); pfmt(f, "%t", c1); break; case PIPEFD: case REDIR: switch(t->rtype){ case HERE: pchr(f, '<'); case READ: case RDWR: pchr(f, '<'); if(t->rtype==RDWR) pchr(f, '>'); if(t->fd0!=0) pfmt(f, "[%d]", t->fd0); break; case APPEND: pchr(f, '>'); case WRITE: pchr(f, '>'); if(t->fd0!=1) pfmt(f, "[%d]", t->fd0); break; } pfmt(f, "%t", c0); if(c1) pfmt(f, " %t", c1); break; case '=': pfmt(f, "%t=%t", c0, c1); if(c2) pfmt(f, " %t", c2); break; case PIPE: pfmt(f, "%t|", c0); if(t->fd1==0){ if(t->fd0!=1) pfmt(f, "[%d]", t->fd0); } else pfmt(f, "[%d=%d]", t->fd0, t->fd1); pfmt(f, "%t", c1); break; } }
void poct(Io *f, ulong n) { if(n>7) poct(f, n>>3); pchr(f, (n&7)+'0'); }