97 struct pcall *pplots; /* last command read */
103 } flibr[MAXL]; /* define strings */
105 struct fcall *fptr = flibr;
114 }fstack[NFSTACK]; /* stack of open input files & defines */
115 struct fstack *fsp=fstack;
118 char argstr[NARGSTR+1]; /* string arguments */
121 double x[NX]; /* numeric arguments */
124 int cnt[NPTS]; /* control-polygon vertex counts */
125 double *pts[NPTS]; /* control-polygon vertex pointers */
127 void eresized(int new){
128 if(new && getwindow(display, Refnone) < 0){
129 fprint(2, "Can't reattach to window: %r\n");
139 main(int arc, char *arv[]){
148 fd = dup(0, -1); /* because openpl will close 0! */
151 for(i=1;i!=arc;i++) if(arv[i][0]=='-') switch(arv[i][1]){
152 case 'd': dflag=1; break;
153 case 'o': oflag=arv[i]+2; break;
154 case 's': fd=server(); break;
157 if(dflag) doublebuffer();
158 for (; arc > 1; arc--, arv++) {
159 if (arv[1][0] == '-') {
164 fprint(2, "%s not allowed as argument\n", ap);
172 case 'e': erase(); break;
173 case 'C': closepl(); break;
174 case 'w': ppause(); break;
175 case 'c': color(ap+1); break;
176 case 'f': cfill(ap+1); break;
177 case 'p': pen(ap+1); break;
178 case 'g': grade(atof(ap+1)); break;
181 else if ((bp = Bopen(arv[1], OREAD)) == 0) {
183 fprint(2, "Cannot find file %s\n", arv[1]);
185 else if(process(bp)) Bterm(fsp->fd);
189 bp = malloc(sizeof *bp);
190 Binit(bp, fd, OREAD);
194 flushimage(display, 1);
197 if(m.buttons&4 && emenuhit(3, &m, &menu)==0) exits(0);
202 return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
206 return 'A'<=c && c<='Z';
210 return '0'<=c && c<='9';
214 return strchr("!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~", c)!=0;
218 return strchr(" \t\n\v\f\r", c)!=0;
224 if(fsp->peekc!=Beof){
231 else if(*fsp->corebuf){
232 fsp->corebuf+=chartorune(&r, fsp->corebuf);
236 if(c!=Beof || fsp==fstack) break;
237 if(fsp->fd) Bterm(fsp->fd);
240 if(c=='\n') fsp->lineno++;
244 * Read a string into argstr -- ignores leading spaces
245 * and an optional leading quote-mark
255 while(c==' ' || c=='\t');
256 if(c=='\'' || c=='"'){
261 while(c!='\n' && c!=Beof){
263 s+=runetochar(s, &r);
266 if(quote && s!=argstr && r==quote) --s;
270 * Read a floating point number into argstr
277 if(c=='+' || c=='-'){
281 while(isdigit(c) || c=='.'){
282 if(s!=&argstr[NARGSTR]) *s++=c;
287 if(ndp>1 || ndig==0){
291 if(c=='e' || c=='E'){
292 if(s!=&argstr[NARGSTR]) *s++=c;
294 if(c=='+' || c=='-'){
295 if(s!=&argstr[NARGSTR]) *s++=c;
303 if(s!=&argstr[NARGSTR]) *s++=c;
312 * Read n numeric arguments, storing them in
321 }while(strchr(" \t\n", c) || c!='.' && c!='+' && c!='-' && ispunct(c));
324 fprint(2, "line %d: number expected\n", fsp->lineno);
325 exits("input error");
327 x[i]=atof(argstr)*fsp->scale;
331 * Read a list of lists of control vertices, storing points in x[.],
332 * pointers in pts[.] and counts in cnt[.]
337 double **ptsp=pts, *xp=x;
341 }while(c==' ' || c=='\t');
358 fprint(2, "line %d: phase error\n",
363 if(ptsp==&pts[NPTS]){
364 fprint(2, "line %d: out of polygons\n",
366 exits("exceeded limit");
371 if(--nleft==0) return;
378 case '.': case '+': case '-':
379 case '0': case '1': case '2': case '3': case '4':
380 case '5': case '6': case '7': case '8': case '9':
383 fprint(2, "line %d: expected number\n", fsp->lineno);
387 fprint(2, "line %d: out of space\n", fsp->lineno);
388 exits("exceeded limit");
395 else if(!ispunct(c)){
415 while(c==' ' || c=='\t');
419 while(c!='\n' && c!=Beof);
426 if(fsp->fd) Bungetc(fsp->fd);
433 if(c=='\n') continue;
437 if(isupper(c)) c=tolower(c);
438 if(s!=&argstr[NARGSTR]) *s++=c;
443 for(pplots=plots;pplots->cc;pplots++)
444 if(strncmp(argstr, pplots->cc, pplots->numc)==0)
447 fprint(2, "line %d, %s unknown\n", fsp->lineno,
449 exits("bad command");
456 fprint(2, "line %d, no command!\n", fsp->lineno);
459 switch(pplots-plots){
460 case ARC: numargs(7); rarc(x[0],x[1],x[2],x[3],x[4],x[5],x[6]); break;
461 case BOX: numargs(4); box(x[0], x[1], x[2], x[3]); break;
462 case CALL: strarg(); call(argstr); pplots=0; break;
463 case CFILL: strarg(); cfill(argstr); pplots=0; break;
464 case CIRC: numargs(3); circ(x[0], x[1], x[2]); break;
465 case CLOSEPL: strarg(); closepl(); pplots=0; break;
466 case COLOR: strarg(); color(argstr); pplots=0; break;
467 case CSPLINE: polyarg(); splin(4, cnt, pts); break;
468 case DEFINE: strarg(); define(argstr); pplots=0; break;
469 case DISK: numargs(3); plotdisc(x[0], x[1], x[2]); break;
470 case DSPLINE: polyarg(); splin(3, cnt, pts); break;
471 case ERASE: strarg(); erase(); pplots=0; break;
472 case FILL: polyarg(); fill(cnt, pts); break;
473 case FRAME: numargs(4); frame(x[0], x[1], x[2], x[3]); break;
474 case FSPLINE: polyarg(); splin(1, cnt, pts); break;
475 case GRADE: numargs(1); grade(x[0]); break;
476 case IDLE: strarg(); idle(); pplots=0; break;
477 case INCLUDE: strarg(); include(argstr); pplots=0; break;
478 case LINE: numargs(4); plotline(x[0], x[1], x[2], x[3]); break;
479 case LSPLINE: polyarg(); splin(2, cnt, pts); break;
480 case MOVE: numargs(2); move(x[0], x[1]); break;
481 case OPENPL: strarg(); openpl(argstr); pplots=0; break;
482 case PARABOLA: numargs(6); parabola(x[0],x[1],x[2],x[3],x[4],x[5]); break;
483 case PAUSE: strarg(); ppause(); pplots=0; break;
484 case PEN: strarg(); pen(argstr); pplots=0; break;
485 case POINT: numargs(2); dpoint(x[0], x[1]); break;
486 case POLY: polyarg(); plotpoly(cnt, pts); break;
487 case RANGE: numargs(4); range(x[0], x[1], x[2], x[3]); break;
488 case RESTORE: strarg(); restore(); pplots=0; break;
489 case RMOVE: numargs(2); rmove(x[0], x[1]); break;
490 case RVEC: numargs(2); rvec(x[0], x[1]); break;
491 case SAVE: strarg(); save(); pplots=0; break;
492 case SBOX: numargs(4); sbox(x[0], x[1], x[2], x[3]); break;
493 case SPLINE: polyarg(); splin(0, cnt, pts); break;
494 case TEXT: strarg(); text(argstr); pplots=0; break;
495 case VEC: numargs(2); vec(x[0], x[1]); break;
497 fprint(2, "plot: missing case %ld\n", pplots-plots);
498 exits("internal error");
507 unsigned size = 1024;
509 void define(char *a){
514 while(isalpha(*ap))ap++;
516 fprint(2,"no name with define\n");
520 if(names+i+1 > enames){
521 names = malloc((unsigned)512);
522 enames = names + 512;
529 bstash = nstash = malloc(size);
530 estash = bstash + size;
532 fptr->stash = nstash;
535 if((ap=Brdline(fsp->fd, '\n'))==0){
536 fprint(2,"unexpected end of file\n");
541 while((j=Bgetc(fsp->fd))!= Beof){
548 if(nstash == estash){
551 bstash = realloc(bstash,size);
552 estash = bstash+size;
556 if(fptr++ >= &flibr[MAXL]){
557 fprint(2,"Too many objects\n");
558 exits("too many objects");
567 while(isalpha(*ap))ap++;
570 for(f=flibr;f<fptr;f++){
571 if (!(strcmp(a, f->name)))
575 fprint(2, "object %s not defined\n",a);
579 while (isspace(*ap) || *ap == ',')
584 if(++fsp==&fstack[NFSTACK]){
585 fprint(2, "input stack overflow\n");
590 fsp->corebuf=f->stash;
592 fsp->scale=fsp[-1].scale*SC;
594 void include(char *a){
599 exits("can't include");
601 if(++fsp==&fstack[NFSTACK]){
602 fprint(2, "input stack overflow\n");
617 fd = create("/srv/plot", 1, 0666);
618 sprint(buf, "%d", p[1]);
619 write(fd, buf, strlen(buf));