History

bleh, still have to .keep some dirs.

initial commit

echoline committed 11 years ago

Commit 3833b13No parent (first commit)14470 files · +3080158 −-4496Browse files

Showing the changes to sys/src/cmd/plot/plot.c (1 of 14470 files). Show all

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t