plot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. flushimage(display, 1);
  184. for(;;){
  185. m=emouse();
  186. if(m.buttons&4 && emenuhit(3, &m, &menu)==0) exits(0);
  187. }
  188. }
  189. int isalpha(int c)
  190. {
  191. return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
  192. }
  193. int isupper(int c)
  194. {
  195. return 'A'<=c && c<='Z';
  196. }
  197. int isdigit(int c)
  198. {
  199. return '0'<=c && c<='9';
  200. }
  201. int ispunct(int c)
  202. {
  203. return strchr("!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~", c)!=0;
  204. }
  205. int isspace(int c)
  206. {
  207. return strchr(" \t\n\v\f\r", c)!=0;
  208. }
  209. int nextc(void){
  210. int c;
  211. Rune r;
  212. for(;;){
  213. if(fsp->peekc!=Beof){
  214. c=fsp->peekc;
  215. fsp->peekc=Beof;
  216. return c;
  217. }
  218. if(fsp->fd)
  219. c=Bgetrune(fsp->fd);
  220. else if(*fsp->corebuf){
  221. fsp->corebuf+=chartorune(&r, fsp->corebuf);
  222. c=r;
  223. }else
  224. c=Beof;
  225. if(c!=Beof || fsp==fstack) break;
  226. if(fsp->fd) Bterm(fsp->fd);
  227. --fsp;
  228. }
  229. if(c=='\n') fsp->lineno++;
  230. return c;
  231. }
  232. /*
  233. * Read a string into argstr -- ignores leading spaces
  234. * and an optional leading quote-mark
  235. */
  236. void
  237. strarg(void){
  238. int c;
  239. Rune r;
  240. int quote=0;
  241. char *s=argstr;
  242. do
  243. c=nextc();
  244. while(c==' ' || c=='\t');
  245. if(c=='\'' || c=='"'){
  246. quote=c;
  247. c=nextc();
  248. }
  249. r = 0;
  250. while(c!='\n' && c!=Beof){
  251. r=c;
  252. s+=runetochar(s, &r);
  253. c=nextc();
  254. }
  255. if(quote && s!=argstr && r==quote) --s;
  256. *s='\0';
  257. }
  258. /*
  259. * Read a floating point number into argstr
  260. */
  261. numstring(void){
  262. int ndp=0;
  263. int ndig=0;
  264. char *s=argstr;
  265. int c=nextc();
  266. if(c=='+' || c=='-'){
  267. *s++=c;
  268. c=nextc();
  269. }
  270. while(isdigit(c) || c=='.'){
  271. if(s!=&argstr[NARGSTR]) *s++=c;
  272. if(c=='.') ndp++;
  273. else ndig++;
  274. c=nextc();
  275. }
  276. if(ndp>1 || ndig==0){
  277. fsp->peekc=c;
  278. return 0;
  279. }
  280. if(c=='e' || c=='E'){
  281. if(s!=&argstr[NARGSTR]) *s++=c;
  282. c=nextc();
  283. if(c=='+' || c=='-'){
  284. if(s!=&argstr[NARGSTR]) *s++=c;
  285. c=nextc();
  286. }
  287. if(!isdigit(c)){
  288. fsp->peekc=c;
  289. return 0;
  290. }
  291. while(isdigit(c)){
  292. if(s!=&argstr[NARGSTR]) *s++=c;
  293. c=nextc();
  294. }
  295. }
  296. fsp->peekc=c;
  297. *s='\0';
  298. return 1;
  299. }
  300. /*
  301. * Read n numeric arguments, storing them in
  302. * x[0], ..., x[n-1]
  303. */
  304. void
  305. numargs(int n){
  306. int i, c;
  307. for(i=0;i!=n;i++){
  308. do{
  309. c=nextc();
  310. }while(strchr(" \t\n", c) || c!='.' && c!='+' && c!='-' && ispunct(c));
  311. fsp->peekc=c;
  312. if(!numstring()){
  313. fprint(2, "line %d: number expected\n", fsp->lineno);
  314. exits("input error");
  315. }
  316. x[i]=atof(argstr)*fsp->scale;
  317. }
  318. }
  319. /*
  320. * Read a list of lists of control vertices, storing points in x[.],
  321. * pointers in pts[.] and counts in cnt[.]
  322. */
  323. void
  324. polyarg(void){
  325. int nleft, l, r, c;
  326. double **ptsp=pts, *xp=x;
  327. int *cntp=cnt;
  328. do{
  329. c=nextc();
  330. }while(c==' ' || c=='\t');
  331. if(c=='{'){
  332. l='{';
  333. r='}';
  334. }
  335. else{
  336. l=r='\n';
  337. fsp->peekc=c;
  338. }
  339. nleft=1;
  340. *cntp=0;
  341. *ptsp=xp;
  342. for(;;){
  343. c=nextc();
  344. if(c==r){
  345. if(*cntp){
  346. if(*cntp&1){
  347. fprint(2, "line %d: phase error\n",
  348. fsp->lineno);
  349. exits("bad input");
  350. }
  351. *cntp/=2;
  352. if(ptsp==&pts[NPTS]){
  353. fprint(2, "line %d: out of polygons\n",
  354. fsp->lineno);
  355. exits("exceeded limit");
  356. }
  357. *++ptsp=xp;
  358. *++cntp=0;
  359. }
  360. if(--nleft==0) return;
  361. }
  362. else switch(c){
  363. case Beof: return;
  364. case ' ': break;
  365. case '\t': break;
  366. case '\n': break;
  367. case '.': case '+': case '-':
  368. case '0': case '1': case '2': case '3': case '4':
  369. case '5': case '6': case '7': case '8': case '9':
  370. fsp->peekc=c;
  371. if(!numstring()){
  372. fprint(2, "line %d: expected number\n", fsp->lineno);
  373. exits("bad input");
  374. }
  375. if(xp==&x[NX]){
  376. fprint(2, "line %d: out of space\n", fsp->lineno);
  377. exits("exceeded limit");
  378. }
  379. *xp++=atof(argstr);
  380. ++*cntp;
  381. break;
  382. default:
  383. if(c==l) nleft++;
  384. else if(!ispunct(c)){
  385. fsp->peekc=c;
  386. return;
  387. }
  388. }
  389. }
  390. }
  391. process(Biobuf *fd){
  392. char *s;
  393. int c;
  394. fsp=fstack;
  395. fsp->fd=fd;
  396. fsp->corebuf=0;
  397. fsp->peekc=Beof;
  398. fsp->lineno=1;
  399. fsp->scale=1.;
  400. for(;;){
  401. do
  402. c=nextc();
  403. while(c==' ' || c=='\t');
  404. if(c==':'){
  405. do
  406. c=nextc();
  407. while(c!='\n' && c!=Beof);
  408. if(c==Beof) break;
  409. continue;
  410. }
  411. while(c=='.'){
  412. c=nextc();
  413. if(isdigit(c)){
  414. if(fsp->fd) Bungetc(fsp->fd);
  415. else --fsp->corebuf;
  416. c='.';
  417. break;
  418. }
  419. }
  420. if(c==Beof) break;
  421. if(c=='\n') continue;
  422. if(isalpha(c)){
  423. s=argstr;
  424. do{
  425. if(isupper(c)) c=tolower(c);
  426. if(s!=&argstr[NARGSTR]) *s++=c;
  427. c=nextc();
  428. }while(isalpha(c));
  429. fsp->peekc=c;
  430. *s='\0';
  431. for(pplots=plots;pplots->cc;pplots++)
  432. if(strncmp(argstr, pplots->cc, pplots->numc)==0)
  433. break;
  434. if(pplots->cc==0){
  435. fprint(2, "line %d, %s unknown\n", fsp->lineno,
  436. argstr);
  437. exits("bad command");
  438. }
  439. }
  440. else{
  441. fsp->peekc=c;
  442. }
  443. if(!pplots){
  444. fprint(2, "line %d, no command!\n", fsp->lineno);
  445. exits("no command");
  446. }
  447. switch(pplots-plots){
  448. case ARC: numargs(7); rarc(x[0],x[1],x[2],x[3],x[4],x[5],x[6]); break;
  449. case BOX: numargs(4); box(x[0], x[1], x[2], x[3]); break;
  450. case CALL: strarg(); call(argstr); pplots=0; break;
  451. case CFILL: strarg(); cfill(argstr); pplots=0; break;
  452. case CIRC: numargs(3); circ(x[0], x[1], x[2]); break;
  453. case CLOSEPL: strarg(); closepl(); pplots=0; break;
  454. case COLOR: strarg(); color(argstr); pplots=0; break;
  455. case CSPLINE: polyarg(); splin(4, cnt, pts); break;
  456. case DEFINE: strarg(); define(argstr); pplots=0; break;
  457. case DISK: numargs(3); plotdisc(x[0], x[1], x[2]); break;
  458. case DSPLINE: polyarg(); splin(3, cnt, pts); break;
  459. case ERASE: strarg(); erase(); pplots=0; break;
  460. case FILL: polyarg(); fill(cnt, pts); break;
  461. case FRAME: numargs(4); frame(x[0], x[1], x[2], x[3]); break;
  462. case FSPLINE: polyarg(); splin(1, cnt, pts); break;
  463. case GRADE: numargs(1); grade(x[0]); break;
  464. case IDLE: strarg(); idle(); pplots=0; break;
  465. case INCLUDE: strarg(); include(argstr); pplots=0; break;
  466. case LINE: numargs(4); plotline(x[0], x[1], x[2], x[3]); break;
  467. case LSPLINE: polyarg(); splin(2, cnt, pts); break;
  468. case MOVE: numargs(2); move(x[0], x[1]); break;
  469. case OPENPL: strarg(); openpl(argstr); pplots=0; break;
  470. case PARABOLA: numargs(6); parabola(x[0],x[1],x[2],x[3],x[4],x[5]); break;
  471. case PAUSE: strarg(); ppause(); pplots=0; break;
  472. case PEN: strarg(); pen(argstr); pplots=0; break;
  473. case POINT: numargs(2); dpoint(x[0], x[1]); break;
  474. case POLY: polyarg(); plotpoly(cnt, pts); break;
  475. case RANGE: numargs(4); range(x[0], x[1], x[2], x[3]); break;
  476. case RESTORE: strarg(); restore(); pplots=0; break;
  477. case RMOVE: numargs(2); rmove(x[0], x[1]); break;
  478. case RVEC: numargs(2); rvec(x[0], x[1]); break;
  479. case SAVE: strarg(); save(); pplots=0; break;
  480. case SBOX: numargs(4); sbox(x[0], x[1], x[2], x[3]); break;
  481. case SPLINE: polyarg(); splin(0, cnt, pts); break;
  482. case TEXT: strarg(); text(argstr); pplots=0; break;
  483. case VEC: numargs(2); vec(x[0], x[1]); break;
  484. default:
  485. fprint(2, "plot: missing case %ld\n", pplots-plots);
  486. exits("internal error");
  487. }
  488. }
  489. return 1;
  490. }
  491. char *names = 0;
  492. char *enames = 0;
  493. char *bstash = 0;
  494. char *estash = 0;
  495. unsigned size = 1024;
  496. char *nstash = 0;
  497. void define(char *a){
  498. char *ap;
  499. short i, j;
  500. int curly = 0;
  501. ap = a;
  502. while(isalpha(*ap))ap++;
  503. if(ap == a){
  504. fprint(2,"no name with define\n");
  505. exits("define");
  506. }
  507. i = ap - a;
  508. if(names+i+1 > enames){
  509. names = malloc((unsigned)512);
  510. enames = names + 512;
  511. }
  512. fptr->name = names;
  513. strncpy(names, a,i);
  514. names += i;
  515. *names++ = '\0';
  516. if(!bstash){
  517. bstash = nstash = malloc(size);
  518. estash = bstash + size;
  519. }
  520. fptr->stash = nstash;
  521. while(*ap != '{')
  522. if(*ap == '\n'){
  523. if((ap=Brdline(fsp->fd, '\n'))==0){
  524. fprint(2,"unexpected end of file\n");
  525. exits("eof");
  526. }
  527. }
  528. else ap++;
  529. while((j=Bgetc(fsp->fd))!= Beof){
  530. if(j == '{')curly++;
  531. else if(j == '}'){
  532. if(curly == 0)break;
  533. else curly--;
  534. }
  535. *nstash++ = j;
  536. if(nstash == estash){
  537. free(bstash);
  538. size += 1024;
  539. bstash = realloc(bstash,size);
  540. estash = bstash+size;
  541. }
  542. }
  543. *nstash++ = '\0';
  544. if(fptr++ >= &flibr[MAXL]){
  545. fprint(2,"Too many objects\n");
  546. exits("too many objects");
  547. }
  548. }
  549. void call(char *a){
  550. char *ap;
  551. struct fcall *f;
  552. char sav;
  553. double SC;
  554. ap = a;
  555. while(isalpha(*ap))ap++;
  556. sav = *ap;
  557. *ap = '\0';
  558. for(f=flibr;f<fptr;f++){
  559. if (!(strcmp(a, f->name)))
  560. break;
  561. }
  562. if(f == fptr){
  563. fprint(2, "object %s not defined\n",a);
  564. exits("undefined");
  565. }
  566. *ap = sav;
  567. while (isspace(*ap) || *ap == ',')
  568. ap++;
  569. if (*ap != '\0')
  570. SC = atof(ap);
  571. else SC = 1.;
  572. if(++fsp==&fstack[NFSTACK]){
  573. fprint(2, "input stack overflow\n");
  574. exits("blew stack");
  575. }
  576. fsp->peekc=Beof;
  577. fsp->lineno=1;
  578. fsp->corebuf=f->stash;
  579. fsp->fd=0;
  580. fsp->scale=fsp[-1].scale*SC;
  581. }
  582. void include(char *a){
  583. Biobuf *fd;
  584. fd=Bopen(a, OREAD);
  585. if(fd==0){
  586. perror(a);
  587. exits("can't include");
  588. }
  589. if(++fsp==&fstack[NFSTACK]){
  590. fprint(2, "input stack overflow\n");
  591. exits("blew stack");
  592. }
  593. fsp->peekc=Beof;
  594. fsp->lineno=1;
  595. fsp->corebuf=0;
  596. fsp->fd=fd;
  597. }
  598. /*
  599. * Doesn't work. Why?
  600. */
  601. int server(void){
  602. int fd, p[2];
  603. char buf[32];
  604. pipe(p);
  605. fd = create("/srv/plot", 1, 0666);
  606. sprint(buf, "%d", p[1]);
  607. write(fd, buf, strlen(buf));
  608. close(fd);
  609. close(p[1]);
  610. return p[0];
  611. }