if(fsp->fd) Bungetc(fsp->fd); else --fsp->corebuf; c='.'; break; } } if(c==Beof) break; if(c=='\n') continue; if(isalpha(c)){ s=argstr; do{ if(isupper(c)) c=tolower(c); if(s!=&argstr[NARGSTR]) *s++=c; c=nextc(); }while(isalpha(c)); fsp->peekc=c; *s='\0'; for(pplots=plots;pplots->cc;pplots++) if(strncmp(argstr, pplots->cc, pplots->numc)==0) break; if(pplots->cc==0) sysfatal("%s:%d: %s unknown", fsp->name, fsp->lineno, argstr); } else{ fsp->peekc=c; } if(!pplots) sysfatal("%s:%d: no command\n", fsp->name, fsp->lineno); switch(pplots-plots){ case ARC: numargs(7); rarc(x[0],x[1],x[2],x[3],x[4],x[5],x[6]); break; case BOX: numargs(4); box(x[0], x[1], x[2], x[3]); break; case CALL: strarg(); call(argstr); pplots=0; break; case CFILL: strarg(); cfill(argstr); pplots=0; break; case CIRC: numargs(3); circ(x[0], x[1], x[2]); break; case CLOSEPL: strarg(); closepl(); pplots=0; break; case COLOR: strarg(); color(argstr); pplots=0; break; case CSPLINE: polyarg(); splin(4, cnt, pts); break; case DEFINE: strarg(); define(argstr); pplots=0; break; case DISK: numargs(3); plotdisc(x[0], x[1], x[2]); break; case DSPLINE: polyarg(); splin(3, cnt, pts); break; case ERASE: strarg(); erase(); pplots=0; break; case FILL: polyarg(); fill(cnt, pts); break; case FRAME: numargs(4); frame(x[0], x[1], x[2], x[3]); break; case FSPLINE: polyarg(); splin(1, cnt, pts); break; case GRADE: numargs(1); grade(x[0]); break; case IDLE: strarg(); idle(); pplots=0; break; case INCLUDE: strarg(); include(argstr); pplots=0; break; case LINE: numargs(4); plotline(x[0], x[1], x[2], x[3]); break; case LSPLINE: polyarg(); splin(2, cnt, pts); break; case MOVE: numargs(2); move(x[0], x[1]); break; case OPENPL: strarg(); openpl(argstr); pplots=0; break; case PARABOLA: numargs(6); parabola(x[0],x[1],x[2],x[3],x[4],x[5]); break; case PAUSE: strarg(); ppause(); pplots=0; break; case PEN: strarg(); pen(argstr); pplots=0; break; case POINT: numargs(2); dpoint(x[0], x[1]); break; case POLY: polyarg(); plotpoly(cnt, pts); break; case RANGE: numargs(4); range(x[0], x[1], x[2], x[3]); break; case RESTORE: strarg(); restore(); pplots=0; break; case RMOVE: numargs(2); rmove(x[0], x[1]); break; case RVEC: numargs(2); rvec(x[0], x[1]); break; case SAVE: strarg(); save(); pplots=0; break; case SBOX: numargs(4); sbox(x[0], x[1], x[2], x[3]); break; case SPLINE: polyarg(); splin(0, cnt, pts); break; case TEXT: strarg(); text(argstr); pplots=0; break; case VEC: numargs(2); vec(x[0], x[1]); break; default: sysfatal("%s:%d: plot: missing case %zd\n", fsp->name, fsp->lineno, pplots-plots); } } return 1;}char *names = 0;char *enames = 0;char *bstash = 0;char *estash = 0;unsigned size = 1024;char *nstash = 0;void define(char *a){ char *ap; short i, j; int curly = 0; ap = a; while(isalpha(*ap))ap++; if(ap == a) sysfatal("plot: no name with define\n"); i = ap - a; if(names+i+1 > enames){ names = malloc((unsigned)512); enames = names + 512; } fptr->name = names; strncpy(names, a,i); names += i; *names++ = '\0'; if(!bstash){ bstash = nstash = malloc(size); estash = bstash + size; } fptr->stash = nstash; while(*ap != '{') if(*ap == '\n'){ if((ap=Brdline(fsp->fd, '\n'))==0) sysfatal("plot: unexpected eof"); } else ap++; while((j=Bgetc(fsp->fd))!= Beof){ if(j == '{')curly++; else if(j == '}'){ if(curly == 0)break; else curly--; } *nstash++ = j; if(nstash == estash){ free(bstash); size += 1024; bstash = realloc(bstash,size); if(bstash == nil) sysfatal("plot: realloc: %r"); estash = bstash+size; } } *nstash++ = '\0'; if(fptr++ >= &flibr[MAXL]) sysfatal("too many objects");}void call(char *a){ char *ap; struct fcall *f; char sav; double SC; ap = a; while(isalpha(*ap))ap++; sav = *ap; *ap = '\0'; for(f=flibr;f<fptr;f++){ if (!(strcmp(a, f->name))) break; } if(f == fptr) sysfatal("plot: object %s not defined",a); *ap = sav; while (isspace(*ap) || *ap == ',') ap++; if (*ap != '\0') SC = atof(ap); else SC = 1.; if(++fsp==&fstack[NFSTACK]) sysfatal("plot: input stack overflow"); snprint(fsp->name, sizeof fsp->name, "call %s", f->name); fsp->peekc=Beof; fsp->lineno=1; fsp->corebuf=f->stash; fsp->fd=0; fsp->scale=fsp[-1].scale*SC;}void include(char *a){ Biobuf *fd; fd=Bopen(a, OREAD); if(fd==0) sysfatal("plot: cant include %s: %r", a); if(++fsp==&fstack[NFSTACK]) sysfatal("plot: input stack overflow"); snprint(fsp->name, sizeof fsp->name, "%s", a); fsp->peekc=Beof; fsp->lineno=1; fsp->corebuf=0; fsp->fd=fd;}/* * Doesn't work. Why? */int server(void){ int fd, p[2]; char buf[32]; pipe(p); if((fd = create("/srv/plot", OWRITE, 0666)) < 0) sysfatal("open: %r"); sprint(buf, "%d", p[1]); write(fd, buf, strlen(buf)); close(fd); close(p[1]); return p[0];}Lines 501–683 of 683. Previous lines
if(fsp->fd) Bungetc(fsp->fd); else --fsp->corebuf; c='.'; break; } } if(c==Beof) break; if(c=='\n') continue; if(isalpha(c)){ s=argstr; do{ if(isupper(c)) c=tolower(c); if(s!=&argstr[NARGSTR]) *s++=c; c=nextc(); }while(isalpha(c)); fsp->peekc=c; *s='\0'; for(pplots=plots;pplots->cc;pplots++) if(strncmp(argstr, pplots->cc, pplots->numc)==0) break; if(pplots->cc==0) sysfatal("%s:%d: %s unknown", fsp->name, fsp->lineno, argstr); } else{ fsp->peekc=c; } if(!pplots) sysfatal("%s:%d: no command\n", fsp->name, fsp->lineno); switch(pplots-plots){ case ARC: numargs(7); rarc(x[0],x[1],x[2],x[3],x[4],x[5],x[6]); break; case BOX: numargs(4); box(x[0], x[1], x[2], x[3]); break; case CALL: strarg(); call(argstr); pplots=0; break; case CFILL: strarg(); cfill(argstr); pplots=0; break; case CIRC: numargs(3); circ(x[0], x[1], x[2]); break; case CLOSEPL: strarg(); closepl(); pplots=0; break; case COLOR: strarg(); color(argstr); pplots=0; break; case CSPLINE: polyarg(); splin(4, cnt, pts); break; case DEFINE: strarg(); define(argstr); pplots=0; break; case DISK: numargs(3); plotdisc(x[0], x[1], x[2]); break; case DSPLINE: polyarg(); splin(3, cnt, pts); break; case ERASE: strarg(); erase(); pplots=0; break; case FILL: polyarg(); fill(cnt, pts); break; case FRAME: numargs(4); frame(x[0], x[1], x[2], x[3]); break; case FSPLINE: polyarg(); splin(1, cnt, pts); break; case GRADE: numargs(1); grade(x[0]); break; case IDLE: strarg(); idle(); pplots=0; break; case INCLUDE: strarg(); include(argstr); pplots=0; break; case LINE: numargs(4); plotline(x[0], x[1], x[2], x[3]); break; case LSPLINE: polyarg(); splin(2, cnt, pts); break; case MOVE: numargs(2); move(x[0], x[1]); break; case OPENPL: strarg(); openpl(argstr); pplots=0; break; case PARABOLA: numargs(6); parabola(x[0],x[1],x[2],x[3],x[4],x[5]); break; case PAUSE: strarg(); ppause(); pplots=0; break; case PEN: strarg(); pen(argstr); pplots=0; break; case POINT: numargs(2); dpoint(x[0], x[1]); break; case POLY: polyarg(); plotpoly(cnt, pts); break; case RANGE: numargs(4); range(x[0], x[1], x[2], x[3]); break; case RESTORE: strarg(); restore(); pplots=0; break; case RMOVE: numargs(2); rmove(x[0], x[1]); break; case RVEC: numargs(2); rvec(x[0], x[1]); break; case SAVE: strarg(); save(); pplots=0; break; case SBOX: numargs(4); sbox(x[0], x[1], x[2], x[3]); break; case SPLINE: polyarg(); splin(0, cnt, pts); break; case TEXT: strarg(); text(argstr); pplots=0; break; case VEC: numargs(2); vec(x[0], x[1]); break; default: sysfatal("%s:%d: plot: missing case %zd\n", fsp->name, fsp->lineno, pplots-plots); } } return 1; } char *names = 0; char *enames = 0; char *bstash = 0; char *estash = 0; unsigned size = 1024; char *nstash = 0; void define(char *a){ char *ap; short i, j; int curly = 0; ap = a; while(isalpha(*ap))ap++; if(ap == a) sysfatal("plot: no name with define\n"); i = ap - a; if(names+i+1 > enames){ names = malloc((unsigned)512); enames = names + 512; } fptr->name = names; strncpy(names, a,i); names += i; *names++ = '\0'; if(!bstash){ bstash = nstash = malloc(size); estash = bstash + size; } fptr->stash = nstash; while(*ap != '{') if(*ap == '\n'){ if((ap=Brdline(fsp->fd, '\n'))==0) sysfatal("plot: unexpected eof"); } else ap++; while((j=Bgetc(fsp->fd))!= Beof){ if(j == '{')curly++; else if(j == '}'){ if(curly == 0)break; else curly--; } *nstash++ = j; if(nstash == estash){ free(bstash); size += 1024; bstash = realloc(bstash,size); if(bstash == nil) sysfatal("plot: realloc: %r"); estash = bstash+size; } } *nstash++ = '\0'; if(fptr++ >= &flibr[MAXL]) sysfatal("too many objects"); } void call(char *a){ char *ap; struct fcall *f; char sav; double SC; ap = a; while(isalpha(*ap))ap++; sav = *ap; *ap = '\0'; for(f=flibr;f<fptr;f++){ if (!(strcmp(a, f->name))) break; } if(f == fptr) sysfatal("plot: object %s not defined",a); *ap = sav; while (isspace(*ap) || *ap == ',') ap++; if (*ap != '\0') SC = atof(ap); else SC = 1.; if(++fsp==&fstack[NFSTACK]) sysfatal("plot: input stack overflow"); snprint(fsp->name, sizeof fsp->name, "call %s", f->name); fsp->peekc=Beof; fsp->lineno=1; fsp->corebuf=f->stash; fsp->fd=0; fsp->scale=fsp[-1].scale*SC; } void include(char *a){ Biobuf *fd; fd=Bopen(a, OREAD); if(fd==0) sysfatal("plot: cant include %s: %r", a); if(++fsp==&fstack[NFSTACK]) sysfatal("plot: input stack overflow"); snprint(fsp->name, sizeof fsp->name, "%s", a); fsp->peekc=Beof; fsp->lineno=1; fsp->corebuf=0; fsp->fd=fd; } /* * Doesn't work. Why? */ int server(void){ int fd, p[2]; char buf[32]; pipe(p); if((fd = create("/srv/plot", OWRITE, 0666)) < 0) sysfatal("open: %r"); sprint(buf, "%d", p[1]); write(fd, buf, strlen(buf)); close(fd); close(p[1]); return p[0]; }