1 #ifndef lint
2 static char *sccsid ="ideal.c (CWI) 1.1 85/03/01";
3 #endif
4 #include "ideal.h"
5 #include "y.tab.h"
6
7 boolean dbg = FALSE;
8 int when_bug = 0;
9 char *filename;
10 int lineno = 0;
11 char libstr[50];
12 boolean radflag = FALSE;
13
14 BOXPTR boxlist = NULL;
15
main(argc,argv)16 main(argc, argv)
17 int argc;
18 char *argv[];
19 {
20 bug_off;
21 while (argc > 1 && argv[1][0] == '-') {
22 switch (argv[1][1]) {
23 case 'd':
24 when_bug = argv[1][2]?atoi(&argv[1][2]):~0;
25 break;
26 case 'l':
27 idinclude (&argv[1][2], LIBFIL);
28 while (yyparse());
29 break;
30 case 'r':
31 radflag = TRUE;
32 break;
33 case 'p':
34 case '4':
35 case 'n':
36 break;
37 default:
38 fprintf(stderr, "ideal: unknown flag %c\n", argv[1][1]);
39 break;
40 }
41 argc--;
42 argv++;
43 }
44 if (argc < 2) {
45 filename = "standard input";
46 lineno = 0;
47 filepush (stdin);
48 while (yyparse ());
49 } else
50 while (argc-- > 1) {
51 filename = *argv;
52 lineno = 0;
53 idinclude (*++argv, CHATTY);
54 while (yyparse ());
55 }
56 exit(0);
57 }
58
interpret()59 interpret()
60 {
61 PUTNODE dummyroot;
62 if (when_bug & 01) bug_on;
63 else bug_off;
64 dummyroot.name = lookup("main");
65 dummyroot.parm = boxgen (lookup("main"), (STMTPTR) NULL);
66 /* if boxlist includes main, execute it */
67 if (boxlist && findbox(lookup("main"),TRUE)->stmtlist) {
68 NOADPTR noadtree;
69 bug_off;
70 /* make room for all variables */
71 noadtree = buildnoadtree (&dummyroot);
72 /* solve all equations */
73 eqneval (noadtree);
74 nl_eval ();
75 depvarkill ();
76 /* make a list of segments in the picture */
77 noadtree->linelist = build (noadtree, noadtree->linelist);
78 /* draw the thing */
79 act (noadtree->linelist);
80 /* free the thing, but save definitions */
81 noadfree (noadtree);
82 fflush (stdout);
83 forget (lookup ("main"));
84 }
85 }
86
idinclude(filnam,mode)87 idinclude (filnam, mode)
88 char *filnam;
89 int mode;
90 {
91 FILE *nufile;
92 dprintf "opening file %s\n", filnam);
93 if (mode == CHATTY)
94 nufile = fopen (filnam, "r");
95 else if (mode == LIBFIL) {
96 strcpy (libstr, LIBDIR);
97 strcat (&libstr[0],filnam);
98 filnam = libstr;
99 nufile = fopen (filnam, "r");
100 }
101 filename = filnam;
102 if (!nufile) {
103 fprintf (stderr, "ideal: can't open file %s\n", filnam);
104 exit (1);
105 } else {
106 filepush (nufile);
107 }
108 }
109
act(the_picture)110 act (the_picture)
111 LINEPTR the_picture;
112 {
113 LINEPTR lineseg;
114 float maxx, maxy, minx, miny;
115 maxx = -10000.0;
116 maxy = -10000.0;
117 minx = 10000.0;
118 miny = 10000.0;
119 for (lineseg = the_picture; lineseg; lineseg = lineseg->next) {
120 switch (lineseg->kind) {
121 case LINE:
122 maxx = max(maxx, max(lineseg->x0, lineseg->x1));
123 maxy = max(maxy, max(lineseg->y0, lineseg->y1));
124 minx = min(minx, min(lineseg->x0, lineseg->x1));
125 miny = min(miny, min(lineseg->y0, lineseg->y1));
126 break;
127 case CIRCLE:
128 maxx = max(maxx, ((CIRCPTR) lineseg)->x0 + fabs(((CIRCPTR) lineseg)->r));
129 minx = min(minx, ((CIRCPTR) lineseg)->x0 - fabs(((CIRCPTR) lineseg)->r));
130 maxy = max(maxy, ((CIRCPTR) lineseg)->y0 + fabs(((CIRCPTR) lineseg)->r));
131 miny = min(miny, ((CIRCPTR) lineseg)->y0 - fabs(((CIRCPTR) lineseg)->r));
132 break;
133 case ARC:
134 maxx = max(maxx, ((ARCPTR) lineseg)->x0 + fabs(((ARCPTR) lineseg)->radius));
135 minx = min(minx, ((ARCPTR) lineseg)->x0 - fabs(((ARCPTR) lineseg)->radius));
136 maxy = max(maxy, ((ARCPTR) lineseg)->y0 + fabs(((ARCPTR) lineseg)->radius));
137 miny = min(miny, ((ARCPTR) lineseg)->y0 - fabs(((ARCPTR) lineseg)->radius));
138 break;
139 case STRING:
140 maxx = max(maxx, ((TEXTPTR) lineseg)->x0);
141 minx = min(minx, ((TEXTPTR) lineseg)->x0);
142 maxy = max(maxy, ((TEXTPTR) lineseg)->y0);
143 miny = min(miny, ((TEXTPTR) lineseg)->y0);
144 break;
145 case SPLINE:
146 break;
147 default:
148 fprintf (stderr, "ideal: act: can't happen\n");
149 break;
150 }
151 }
152 if (the_picture) {
153 boundscall (maxx, maxy, minx, miny);
154 }
155 for (lineseg = the_picture; lineseg; lineseg = lineseg->next) {
156 switch (lineseg->kind) {
157 case LINE:
158 linecall (lineseg);
159 break;
160 case CIRCLE:
161 circcall ((CIRCPTR) lineseg);
162 break;
163 case ARC:
164 arccall ((ARCPTR) lineseg);
165 break;
166 case STRING:
167 textcall ((TEXTPTR) lineseg);
168 break;
169 case SPLINE:
170 splcall (((SPLPTR) lineseg)->knotlist);
171 break;
172 default:
173 fprintf (stderr, "ideal: act: can't happen\n");
174 break;
175 }
176 }
177 }
178