plot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "plot.h"
  5. #include <draw.h>
  6. #include <event.h>
  7. void define(char*);
  8. void call(char*);
  9. void include(char*);
  10. int process(Biobuf*);
  11. int server(void);
  12. enum{
  13. ARC,
  14. BOX,
  15. CALL,
  16. CFILL,
  17. CIRC,
  18. CLOSEPL,
  19. COLOR,
  20. CSPLINE,
  21. DEFINE,
  22. DISK,
  23. DSPLINE,
  24. ERASE,
  25. FILL,
  26. FRAME,
  27. FSPLINE,
  28. GRADE,
  29. IDLE,
  30. INCLUDE,
  31. LINE,
  32. LSPLINE,
  33. MOVE,
  34. OPENPL,
  35. PARABOLA,
  36. PEN,
  37. PAUSE,
  38. POINT,
  39. POLY,
  40. RANGE,
  41. RESTORE,
  42. RMOVE,
  43. RVEC,
  44. SAVE,
  45. SBOX,
  46. SPLINE,
  47. TEXT,
  48. VEC,
  49. LAST
  50. };
  51. struct pcall {
  52. char *cc;
  53. int numc;
  54. } plots[] = {
  55. [ARC] "a", 1,
  56. [BOX] "bo", 2,
  57. [CALL] "ca", 2,
  58. [CFILL] "cf", 2,
  59. [CIRC] "ci", 2,
  60. [CLOSEPL] "cl", 2,
  61. [COLOR] "co", 2,
  62. [CSPLINE] "cs", 2,
  63. [DEFINE] "de", 2,
  64. [DISK] "di", 2,
  65. [DSPLINE] "ds", 2,
  66. [ERASE] "e", 1,
  67. [FILL] "fi", 2,
  68. [FRAME] "fr", 2,
  69. [FSPLINE] "fs", 2,
  70. [GRADE] "g", 1,
  71. [IDLE] "id", 2,
  72. [INCLUDE] "in", 2,
  73. [LINE] "li", 2,
  74. [LSPLINE] "ls", 2,
  75. [MOVE] "m", 1,
  76. [OPENPL] "o", 1,
  77. [PARABOLA] "par", 3,
  78. [PEN] "pe", 2,
  79. [PAUSE] "pau", 3,
  80. [POINT] "poi", 3,
  81. [POLY] "pol", 3,
  82. [RANGE] "ra", 2,
  83. [RESTORE] "re", 2,
  84. [RMOVE] "rm", 2,
  85. [RVEC] "rv", 2,
  86. [SAVE] "sa", 2,
  87. [SBOX] "sb", 2,
  88. [SPLINE] "sp", 2,
  89. [TEXT] "t", 1,
  90. [VEC] "v", 1,
  91. [LAST] 0, 0,
  92. };
  93. struct pcall *pplots; /* last command read */
  94. #define MAXL 16
  95. struct fcall {
  96. char *name;
  97. char *stash;
  98. } flibr[MAXL]; /* define strings */
  99. struct fcall *fptr = flibr;
  100. #define NFSTACK 50
  101. struct fstack{
  102. int peekc;
  103. int lineno;
  104. char *corebuf;
  105. Biobuf *fd;
  106. double scale;
  107. }fstack[NFSTACK]; /* stack of open input files & defines */
  108. struct fstack *fsp=fstack;
  109. #define NARGSTR 8192
  110. char argstr[NARGSTR+1]; /* string arguments */
  111. #define NX 8192
  112. double x[NX]; /* numeric arguments */
  113. #define NPTS 256
  114. int cnt[NPTS]; /* control-polygon vertex counts */
  115. double *pts[NPTS]; /* control-polygon vertex pointers */
  116. void eresized(int new){
  117. if(new && getwindow(display, Refnone) < 0){
  118. fprint(2, "Can't reattach to window: %r\n");
  119. exits("resize");
  120. }
  121. }
  122. char *items[]={
  123. "exit",
  124. 0
  125. };
  126. Menu menu={items};
  127. void
  128. main(int arc, char *arv[]){
  129. char *ap;
  130. Biobuf *bp;
  131. int fd;
  132. int i;
  133. int dflag;
  134. char *oflag;
  135. Mouse m;
  136. bp = 0;
  137. fd = dup(0, -1); /* because openpl will close 0! */
  138. dflag=0;
  139. oflag="";
  140. for(i=1;i!=arc;i++) if(arv[i][0]=='-') switch(arv[i][1]){
  141. case 'd': dflag=1; break;
  142. case 'o': oflag=arv[i]+2; break;
  143. case 's': fd=server(); break;
  144. }
  145. openpl(oflag);
  146. if(dflag) doublebuffer();
  147. for (; arc > 1; arc--, arv++) {
  148. if (arv[1][0] == '-') {
  149. ap = arv[1];
  150. ap++;
  151. switch (*ap) {
  152. default:
  153. fprint(2, "%s not allowed as argument\n", ap);
  154. exits("usage");
  155. case 'T': break;
  156. case 'D': break;
  157. case 'd': break;
  158. case 'o': break;
  159. case 'W': break;
  160. case 's': break;
  161. case 'e': erase(); break;
  162. case 'C': closepl(); break;
  163. case 'w': ppause(); break;
  164. case 'c': color(ap+1); break;
  165. case 'f': cfill(ap+1); break;
  166. case 'p': pen(ap+1); break;
  167. case 'g': grade(atof(ap+1)); break;
  168. }
  169. }
  170. else if ((bp = Bopen(arv[1], OREAD)) == 0) {
  171. perror(arv[1]);
  172. fprint(2, "Cannot find file %s\n", arv[1]);
  173. }
  174. else if(process(bp)) Bterm(fsp->fd);
  175. else break;
  176. }
  177. if (bp == 0){
  178. bp = malloc(sizeof *bp);
  179. Binit(bp, fd, OREAD);
  180. process(bp);
  181. }
  182. closepl();
  183. for(;;){
  184. m=emouse();
  185. if(m.buttons&4 && emenuhit(3, &m, &menu)==0) exits(0);
  186. }
  187. }
  188. int isalpha(int c)
  189. {
  190. return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
  191. }
  192. int isupper(int c)
  193. {
  194. return 'A'<=c && c<='Z';
  195. }
  196. int isdigit(int c)
  197. {
  198. return '0'<=c && c<='9';
  199. }
  200. int ispunct(int c)
  201. {
  202. return strchr("!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~", c)!=0;
  203. }
  204. int isspace(int c)
  205. {
  206. return strchr(" \t\n\v\f\r", c)!=0;
  207. }
  208. int nextc(void){
  209. int c;
  210. Rune r;
  211. for(;;){
  212. if(fsp->peekc!=Beof){
  213. c=fsp->peekc;
  214. fsp->peekc=Beof;
  215. return c;
  216. }
  217. if(fsp->fd)
  218. c=Bgetrune(fsp->fd);
  219. else if(*fsp->corebuf){
  220. fsp->corebuf+=chartorune(&r, fsp->corebuf);
  221. c=r;
  222. }else
  223. c=Beof;
  224. if(c!=Beof || fsp==fstack) break;
  225. if(fsp->fd) Bterm(fsp->fd);
  226. --fsp;
  227. }
  228. if(c=='\n') fsp->lineno++;
  229. return c;
  230. }
  231. /*
  232. * Read a string into argstr -- ignores leading spaces
  233. * and an optional leading quote-mark
  234. */
  235. void
  236. strarg(void){
  237. int c;
  238. Rune r;
  239. int quote=0;
  240. char *s=argstr;
  241. do
  242. c=nextc();
  243. while(c==' ' || c=='\t');
  244. if(c=='\'' || c=='"'){
  245. quote=c;
  246. c=nextc();
  247. }
  248. r = 0;
  249. while(c!='\n' && c!=Beof){
  250. r=c;
  251. s+=runetochar(s, &r);
  252. c=nextc();
  253. }
  254. if(quote && s!=argstr && r==quote) --s;
  255. *s='\0';
  256. }
  257. /*
  258. * Read a floating point number into argstr
  259. */
  260. numstring(void){
  261. int ndp=0;
  262. int ndig=0;
  263. char *s=argstr;
  264. int c=nextc();
  265. if(c=='+' || c=='-'){
  266. *s++=c;
  267. c=nextc();
  268. }
  269. while(isdigit(c) || c=='.'){
  270. if(s!=&argstr[NARGSTR]) *s++=c;
  271. if(c=='.') ndp++;
  272. else ndig++;
  273. c=nextc();
  274. }
  275. if(ndp>1 || ndig==0){
  276. fsp->peekc=c;
  277. return 0;
  278. }
  279. if(c=='e' || c=='E'){
  280. if(s!=&argstr[NARGSTR]) *s++=c;
  281. c=nextc();
  282. if(c=='+' || c=='-'){
  283. if(s!=&argstr[NARGSTR]) *s++=c;
  284. c=nextc();
  285. }
  286. if(!isdigit(c)){
  287. fsp->peekc=c;
  288. return 0;
  289. }
  290. while(isdigit(c)){
  291. if(s!=&argstr[NARGSTR]) *s++=c;
  292. c=nextc();
  293. }
  294. }
  295. fsp->peekc=c;
  296. *s='\0';
  297. return 1;
  298. }
  299. /*
  300. * Read n numeric arguments, storing them in
  301. * x[0], ..., x[n-1]
  302. */
  303. void
  304. numargs(int n){
  305. int i, c;
  306. for(i=0;i!=n;i++){
  307. do{
  308. c=nextc();
  309. }while(strchr(" \t\n", c) || c!='.' && c!='+' && c!='-' && ispunct(c));
  310. fsp->peekc=c;
  311. if(!numstring()){
  312. fprint(2, "line %d: number expected\n", fsp->lineno);
  313. exits("input error");
  314. }
  315. x[i]=atof(argstr)*fsp->scale;
  316. }
  317. }
  318. /*
  319. * Read a list of lists of control vertices, storing points in x[.],
  320. * pointers in pts[.] and counts in cnt[.]
  321. */
  322. void
  323. polyarg(void){
  324. int nleft, l, r, c;
  325. double **ptsp=pts, *xp=x;
  326. int *cntp=cnt;
  327. do{
  328. c=nextc();
  329. }while(c==' ' || c=='\t');
  330. if(c=='{'){
  331. l='{';
  332. r='}';
  333. }
  334. else{
  335. l=r='\n';
  336. fsp->peekc=c;
  337. }
  338. nleft=1;
  339. *cntp=0;
  340. *ptsp=xp;
  341. for(;;){
  342. c=nextc();
  343. if(c==r){
  344. if(*cntp){
  345. if(*cntp&1){
  346. fprint(2, "line %d: phase error\n",
  347. fsp->lineno);
  348. exits("bad input");
  349. }
  350. *cntp/=2;
  351. if(ptsp==&pts[NPTS]){
  352. fprint(2, "line %d: out of polygons\n",
  353. fsp->lineno);
  354. exits("exceeded limit");
  355. }
  356. *++ptsp=xp;
  357. *++cntp=0;
  358. }
  359. if(--nleft==0) return;
  360. }
  361. else switch(c){
  362. case Beof: return;
  363. case ' ': break;
  364. case '\t': break;
  365. case '\n': break;
  366. case '.': case '+': case '-':
  367. case '0': case '1': case '2': case '3': case '4':
  368. case '5': case '6': case '7': case '8': case '9':
  369. fsp->peekc=c;
  370. if(!numstring()){
  371. fprint(2, "line %d: expected number\n", fsp->lineno);
  372. exits("bad input");
  373. }
  374. if(xp==&x[NX]){
  375. fprint(2, "line %d: out of space\n", fsp->lineno);
  376. exits("exceeded limit");
  377. }
  378. *xp++=atof(argstr);
  379. ++*cntp;
  380. break;
  381. default:
  382. if(c==l) nleft++;
  383. else if(!ispunct(c)){
  384. fsp->peekc=c;
  385. return;
  386. }
  387. }
  388. }
  389. }
  390. process(Biobuf *fd){
  391. char *s;
  392. int c;
  393. fsp=fstack;
  394. fsp->fd=fd;
  395. fsp->corebuf=0;
  396. fsp->peekc=Beof;
  397. fsp->lineno=1;
  398. fsp->scale=1.;
  399. for(;;){
  400. do
  401. c=nextc();
  402. while(c==' ' || c=='\t');
  403. if(c==':'){
  404. do
  405. c=nextc();
  406. while(c!='\n' && c!=Beof);
  407. if(c==Beof) break;
  408. continue;
  409. }
  410. while(c=='.'){
  411. c=nextc();
  412. if(isdigit(c)){
  413. if(fsp->fd) Bungetc(fsp->fd);
  414. else --fsp->corebuf;
  415. c='.';
  416. break;
  417. }
  418. }
  419. if(c==Beof) break;
  420. if(c=='\n') continue;
  421. if(isalpha(c)){
  422. s=argstr;
  423. do{
  424. if(isupper(c)) c=tolower(c);
  425. if(s!=&argstr[NARGSTR]) *s++=c;
  426. c=nextc();
  427. }while(isalpha(c));
  428. fsp->peekc=c;
  429. *s='\0';
  430. for(pplots=plots;pplots->cc;pplots++)
  431. if(strncmp(argstr, pplots->cc, pplots->numc)==0)
  432. break;
  433. if(pplots->cc==0){
  434. fprint(2, "line %d, %s unknown\n", fsp->lineno,
  435. argstr);
  436. exits("bad command");
  437. }
  438. }
  439. else{
  440. fsp->peekc=c;
  441. }
  442. if(!pplots){
  443. fprint(2, "line %d, no command!\n", fsp->lineno);
  444. exits("no command");
  445. }
  446. switch(pplots-plots){
  447. case ARC: numargs(7); rarc(x[0],x[1],x[2],x[3],x[4],x[5],x[6]); break;
  448. case BOX: numargs(4); box(x[0], x[1], x[2], x[3]); break;
  449. case CALL: strarg(); call(argstr); pplots=0; break;
  450. case CFILL: strarg(); cfill(argstr); pplots=0; break;
  451. case CIRC: numargs(3); circ(x[0], x[1], x[2]); break;
  452. case CLOSEPL: strarg(); closepl(); pplots=0; break;
  453. case COLOR: strarg(); color(argstr); pplots=0; break;
  454. case CSPLINE: polyarg(); splin(4, cnt, pts); break;
  455. case DEFINE: strarg(); define(argstr); pplots=0; break;
  456. case DISK: numargs(3); plotdisc(x[0], x[1], x[2]); break;
  457. case DSPLINE: polyarg(); splin(3, cnt, pts); break;
  458. case ERASE: strarg(); erase(); pplots=0; break;
  459. case FILL: polyarg(); fill(cnt, pts); break;
  460. case FRAME: numargs(4); frame(x[0], x[1], x[2], x[3]); break;
  461. case FSPLINE: polyarg(); splin(1, cnt, pts); break;
  462. case GRADE: numargs(1); grade(x[0]); break;
  463. case IDLE: strarg(); idle(); pplots=0; break;
  464. case INCLUDE: strarg(); include(argstr); pplots=0; break;
  465. case LINE: numargs(4); plotline(x[0], x[1], x[2], x[3]); break;
  466. case LSPLINE: polyarg(); splin(2, cnt, pts); break;
  467. case MOVE: numargs(2); move(x[0], x[1]); break;
  468. case OPENPL: strarg(); openpl(argstr); pplots=0; break;
  469. case PARABOLA: numargs(6); parabola(x[0],x[1],x[2],x[3],x[4],x[5]); break;
  470. case PAUSE: strarg(); ppause(); pplots=0; break;
  471. case PEN: strarg(); pen(argstr); pplots=0; break;
  472. case POINT: numargs(2); dpoint(x[0], x[1]); break;
  473. case POLY: polyarg(); plotpoly(cnt, pts); break;
  474. case RANGE: numargs(4); range(x[0], x[1], x[2], x[3]); break;
  475. case RESTORE: strarg(); restore(); pplots=0; break;
  476. case RMOVE: numargs(2); rmove(x[0], x[1]); break;
  477. case RVEC: numargs(2); rvec(x[0], x[1]); break;
  478. case SAVE: strarg(); save(); pplots=0; break;
  479. case SBOX: numargs(4); sbox(x[0], x[1], x[2], x[3]); break;
  480. case SPLINE: polyarg(); splin(0, cnt, pts); break;
  481. case TEXT: strarg(); text(argstr); pplots=0; break;
  482. case VEC: numargs(2); vec(x[0], x[1]); break;
  483. default:
  484. fprint(2, "plot: missing case %ld\n", pplots-plots);
  485. exits("internal error");
  486. }
  487. }
  488. return 1;
  489. }
  490. char *names = 0;
  491. char *enames = 0;
  492. char *bstash = 0;
  493. char *estash = 0;
  494. unsigned size = 1024;
  495. char *nstash = 0;
  496. void define(char *a){
  497. char *ap;
  498. short i, j;
  499. int curly = 0;
  500. ap = a;
  501. while(isalpha(*ap))ap++;
  502. if(ap == a){
  503. fprint(2,"no name with define\n");
  504. exits("define");
  505. }
  506. i = ap - a;
  507. if(names+i+1 > enames){
  508. names = malloc((unsigned)512);
  509. enames = names + 512;
  510. }
  511. fptr->name = names;
  512. strncpy(names, a,i);
  513. names += i;
  514. *names++ = '\0';
  515. if(!bstash){
  516. bstash = nstash = malloc(size);
  517. estash = bstash + size;
  518. }
  519. fptr->stash = nstash;
  520. while(*ap != '{')
  521. if(*ap == '\n'){
  522. if((ap=Brdline(fsp->fd, '\n'))==0){
  523. fprint(2,"unexpected end of file\n");
  524. exits("eof");
  525. }
  526. }
  527. else ap++;
  528. while((j=Bgetc(fsp->fd))!= Beof){
  529. if(j == '{')curly++;
  530. else if(j == '}'){
  531. if(curly == 0)break;
  532. else curly--;
  533. }
  534. *nstash++ = j;
  535. if(nstash == estash){
  536. free(bstash);
  537. size += 1024;
  538. bstash = realloc(bstash,size);
  539. estash = bstash+size;
  540. }
  541. }
  542. *nstash++ = '\0';
  543. if(fptr++ >= &flibr[MAXL]){
  544. fprint(2,"Too many objects\n");
  545. exits("too many objects");
  546. }
  547. }
  548. void call(char *a){
  549. char *ap;
  550. struct fcall *f;
  551. char sav;
  552. double SC;
  553. ap = a;
  554. while(isalpha(*ap))ap++;
  555. sav = *ap;
  556. *ap = '\0';
  557. for(f=flibr;f<fptr;f++){
  558. if (!(strcmp(a, f->name)))
  559. break;
  560. }
  561. if(f == fptr){
  562. fprint(2, "object %s not defined\n",a);
  563. exits("undefined");
  564. }
  565. *ap = sav;
  566. while (isspace(*ap) || *ap == ',')
  567. ap++;
  568. if (*ap != '\0')
  569. SC = atof(ap);
  570. else SC = 1.;
  571. if(++fsp==&fstack[NFSTACK]){
  572. fprint(2, "input stack overflow\n");
  573. exits("blew stack");
  574. }
  575. fsp->peekc=Beof;
  576. fsp->lineno=1;
  577. fsp->corebuf=f->stash;
  578. fsp->fd=0;
  579. fsp->scale=fsp[-1].scale*SC;
  580. }
  581. void include(char *a){
  582. Biobuf *fd;
  583. fd=Bopen(a, OREAD);
  584. if(fd==0){
  585. perror(a);
  586. exits("can't include");
  587. }
  588. if(++fsp==&fstack[NFSTACK]){
  589. fprint(2, "input stack overflow\n");
  590. exits("blew stack");
  591. }
  592. fsp->peekc=Beof;
  593. fsp->lineno=1;
  594. fsp->corebuf=0;
  595. fsp->fd=fd;
  596. }
  597. /*
  598. * Doesn't work. Why?
  599. */
  600. int server(void){
  601. int fd, p[2];
  602. char buf[32];
  603. pipe(p);
  604. fd = create("/srv/plot", 1, 0666);
  605. sprint(buf, "%d", p[1]);
  606. write(fd, buf, strlen(buf));
  607. close(fd);
  608. close(p[1]);
  609. return p[0];
  610. }