1 /*
2 Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 - Neither the name of The Numerical ALgorithms Group Ltd. nor the
18 names of its contributors may be used to endorse or promote products
19 derived from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define _SPOON2D_C
35 #include "fricas_c_macros.h"
36
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <assert.h>
41
42 #include "viewAlone.h"
43
44
45 #include "util.H1"
46 #include "all_alone.H1"
47 #include "strutil.h"
48
49 /* #define huhDEBUG */
50
51 /* #define spoonDEBUG */
52
53 void
spoonView2D(void)54 spoonView2D(void)
55 {
56
57 int i,code,pipe0[2],pipe1[2],there;
58 char * env_fricas;
59 char * run_view;
60
61 fricas_sprintf_to_buf1(errorStr, "%s", "creating pipes");
62 check(pipe(pipe0));
63 check(pipe(pipe1));
64 switch(fork()) {
65
66 case -1:
67 fprintf(stderr,
68 "Cannot create a new process - probably have too many things running already.\n");
69 exit(-1);
70
71 case 0:
72 /************
73 * Child *
74 ************/
75 /* printf("(spoon2D child) mapping of pipes to standard I/O for view2D\n");
76 */
77 fricas_sprintf_to_buf1(errorStr, "%s",
78 "(viewAlone) mapping of pipes to standard I/O for view2D");
79 check(dup2(pipe0[0],0));
80 check(dup2(pipe1[1],1));
81 close(pipe0[0]);
82 close(pipe0[1]);
83 close(pipe1[0]);
84 close(pipe1[1]);
85 printf("(spoon2D child) start the TwoDimensionalViewport process\n");
86 fricas_sprintf_to_buf1(errorStr, "%s",
87 "(viewAlone) execution of the TwoDimensionalViewport process");
88 env_fricas = getenv("FRICAS");
89 {
90 size_t env_fricas_len = strlen(env_fricas);
91 if (env_fricas_len > 20000) {
92 fprintf(stderr, "FRICAS env var too long\n");
93 exit(-1);
94 }
95 run_view = malloc(env_fricas_len + strlen("/lib/view2D") + 1);
96 if(!run_view) {
97 fprintf(stderr, "(spoon2D child) out of memory\n");
98 exit(-1);
99 }
100 strcpy(run_view, env_fricas);
101 strcat(run_view, "/lib/view2D");
102 }
103 check(execl(run_view, run_view, NULL));
104 fprintf(stderr,"Could not execute view2D! Check that view2D is on your path variable.\n");
105 exit(-1);
106
107 default:
108 /*************
109 * Parent *
110 *************/
111
112 viewP.viewType = view2DType;
113
114 /* set up pipes to child process */
115 close(pipe0[0]);
116 close(pipe1[1]);
117 viewP.viewIn = pipe1[0];
118 viewP.viewOut = pipe0[1];
119 /* printf("(spoon2D parent) pipes created\n"); */
120 if (viewP.viewIn <0) {
121 fprintf(stderr," Could not connect from Viewport manager to viewport process. Try again.\n");
122 return;
123 } else {
124 code = read(viewP.viewIn,&ack,intSize);
125 if (code < 0) {
126 fprintf(stderr," Could not connect from Viewport manager to viewport process. Try again.\n");
127 return;
128 }
129 }
130 /* printf("(spoon2D parent) making View2D data\n"); */
131 makeView2DFromFileData(&doView2D);
132
133 /* tell child it is to be a stand alone program */
134 i = yes;
135 fprintf(stderr," Transmitting data to viewport...\n");
136 write(viewP.viewOut,&i,intSize);
137
138 write(viewP.viewOut,&doView2D,sizeof(view2DStruct));
139 i = strlen(doView2D.title)+1;
140 write(viewP.viewOut,&i,intSize); /* tell the length of the title to child */
141 write(viewP.viewOut,doView2D.title,i); /* tell the title to the child, child */
142 for (i=0; i<maxGraphs; i++) {
143 there = graphArray[i].key;
144 write(viewP.viewOut,&there,intSize);
145 sendGraphToView2D(i,there,&viewP);
146 }; /* for i in graphs */
147
148 fprintf(stderr," Done.\n");
149
150 /*** get acknowledge from viewport */
151
152 code = read(viewP.viewIn,&(viewP.viewWindow),sizeof(Window));
153 sleep(1); /* wait a second...*/
154 exit(0);
155
156 } /* switch */
157
158 } /* forkView2D() */
159
160
161
162 /*void sendGraphToView2D(i,there,viewP) */
163 void
sendGraphToView2D(int i,int there,viewManager * viewP)164 sendGraphToView2D(int i,int there,viewManager *viewP)
165 {
166 graphStruct *gPtr;
167 pointListStruct *llPtr;
168 pointStruct *p;
169 int j,k;
170 /* printf("(spoon2D sendGraphToView2D) i=%d there=%d\n",i,there); */
171 if (there) {
172
173 gPtr = &(graphArray[i]);
174 /* printf("(spoon2D sendGraphToView2D) graph %d is there\n",i); */
175 write(viewP->viewOut,&(gPtr->xmin),floatSize);
176 write(viewP->viewOut,&(gPtr->xmax),floatSize);
177 write(viewP->viewOut,&(gPtr->ymin),floatSize);
178 write(viewP->viewOut,&(gPtr->ymax),floatSize);
179 write(viewP->viewOut,&(gPtr->xNorm),floatSize);
180 write(viewP->viewOut,&(gPtr->yNorm),floatSize);
181 write(viewP->viewOut,&(gPtr->spadUnitX),floatSize);
182 write(viewP->viewOut,&(gPtr->spadUnitY),floatSize);
183 write(viewP->viewOut,&(gPtr->unitX),floatSize);
184 write(viewP->viewOut,&(gPtr->unitY),floatSize);
185 write(viewP->viewOut,&(gPtr->originX),floatSize);
186 write(viewP->viewOut,&(gPtr->originY),floatSize);
187 write(viewP->viewOut,&(gPtr->numberOfLists),intSize);
188
189 llPtr = gPtr->listOfListsOfPoints;
190 for (j=0; j<(gPtr->numberOfLists); j++) {
191 write(viewP->viewOut,&(llPtr->numberOfPoints),intSize);
192 p = llPtr->listOfPoints;
193 for (k=0; k<(llPtr->numberOfPoints); k++) {
194 write(viewP->viewOut,&(p->x),floatSize);
195 write(viewP->viewOut,&(p->y),floatSize);
196 write(viewP->viewOut,&(p->hue),floatSize);
197 write(viewP->viewOut,&(p->shade),floatSize);
198 p++;
199 } /* for k in list of points */
200 write(viewP->viewOut,&(llPtr->pointColor),intSize);
201 write(viewP->viewOut,&(llPtr->lineColor),intSize);
202 write(viewP->viewOut,&(llPtr->pointSize),intSize);
203 llPtr++;
204 } /* for j in list of lists of points */
205
206 /* a state is defined for a graph if it is there */
207 write(viewP->viewOut,&(graphStateArray[i].scaleX),floatSize);
208 write(viewP->viewOut,&(graphStateArray[i].scaleY),floatSize);
209 write(viewP->viewOut,&(graphStateArray[i].deltaX),floatSize);
210 write(viewP->viewOut,&(graphStateArray[i].deltaY),floatSize);
211 write(viewP->viewOut,&(graphStateArray[i].pointsOn),intSize);
212 write(viewP->viewOut,&(graphStateArray[i].connectOn),intSize);
213 write(viewP->viewOut,&(graphStateArray[i].splineOn),intSize);
214 write(viewP->viewOut,&(graphStateArray[i].axesOn),intSize);
215 write(viewP->viewOut,&(graphStateArray[i].axesColor),intSize);
216 write(viewP->viewOut,&(graphStateArray[i].unitsOn),intSize);
217 write(viewP->viewOut,&(graphStateArray[i].unitsColor),intSize);
218 write(viewP->viewOut,&(graphStateArray[i].showing),intSize);
219
220 } /* if graph is there */
221
222 }
223
224
225 void
makeView2DFromFileData(view2DStruct * doView2D)226 makeView2DFromFileData(view2DStruct *doView2D)
227 {
228
229 int i,j,k;
230 char title[256];
231 size_t title_size;
232 FILE *graphFile;
233 char graphFilename[256];
234 pointListStruct *aList;
235 pointStruct *aPoint;
236 /* printf("(spoon2D makeView2DFromFileData)\n"); */
237 fgets(title,256,viewFile);
238 /* printf("(spoon2D) title=%s\n",title); */
239 title_size = strlen(title);
240 assert(title_size > 0);
241 if (!(doView2D->title = (char *)malloc(title_size))) {
242 fprintf(stderr,
243 "Ran out of memory (malloc) trying to get the title.\n");
244 exit(-1);
245 }
246 memcpy(doView2D->title, title, title_size - 1);
247 /* put in a null terminator over the newline that the fgets reads */
248 doView2D->title[title_size-1] = '\0';
249 fscanf(viewFile,"%d %d %d %d\n",
250 &(doView2D->vX),
251 &(doView2D->vY),
252 &(doView2D->vW),
253 &(doView2D->vH));
254 /* printf("(spoon2D) X=%d Y=%d W=%d H=%d \n",
255 doView2D->vX,doView2D->vY,doView2D->vW,doView2D->vH); */
256 for (i=0; i<maxGraphs; i++) {
257 fscanf(viewFile,"%d\n",
258 &(graphArray[i].key));
259 /* printf("(spoon2D) i=%d key=%d\n",
260 i,graphArray[i].key); */
261 fscanf(viewFile,"%g %g\n",
262 &(graphStateArray[i].scaleX),
263 &(graphStateArray[i].scaleY));
264 /* printf("(spoon2D) scaleX=%g scaleY=%g\n",
265 graphStateArray[i].scaleX,graphStateArray[i].scaleY); */
266 fscanf(viewFile,"%g %g\n",
267 &(graphStateArray[i].deltaX),
268 &(graphStateArray[i].deltaY));
269 /* printf("(spoon2D) deltaX=%g deltaY=%g\n",
270 graphStateArray[i].deltaX,graphStateArray[i].deltaY); */
271 fscanf(viewFile,"%g %g\n",
272 &(graphStateArray[i].centerX),
273 &(graphStateArray[i].centerY));
274 /* printf("(spoon2D) centerX=%g centerY=%g\n",
275 graphStateArray[i].centerX,graphStateArray[i].centerY); */
276 fscanf(viewFile,"%d %d %d %d %d %d %d\n",
277 &(graphStateArray[i].pointsOn),
278 &(graphStateArray[i].connectOn),
279 &(graphStateArray[i].splineOn),
280 &(graphStateArray[i].axesOn),
281 &(graphStateArray[i].axesColor),
282 &(graphStateArray[i].unitsOn),
283 &(graphStateArray[i].unitsColor));
284 /*
285 printf("(spoon2D) pointsOn=%d connectOn=%d splineOn=%d axesOn=%d axesColor=%d unitsOn=%d unitsColor=%d\n",
286 graphStateArray[i].pointsOn,graphStateArray[i].connectOn,
287 graphStateArray[i].splineOn,graphStateArray[i].axesOn,
288 graphStateArray[i].axesColor,graphStateArray[i].unitsOn,
289 graphStateArray[i].unitsColor);
290 */
291 fscanf(viewFile,"%d %d\n",
292 &(graphStateArray[i].showing),
293 &(graphStateArray[i].selected));
294 /*
295 printf("(spoon2D) showing=%d selected=%d\n",
296 graphStateArray[i].showing,graphStateArray[i].selected);
297 */
298 }
299 fclose(viewFile);
300 for (i=0; i<maxGraphs; i++) {
301 if (graphArray[i].key) {
302 /** OPEN FILE FOR GRAPHS **/
303 fricas_sprintf_to_buf3(graphFilename, "%s%s%d", pathname, "/graph", i);
304 if ((graphFile = fopen(graphFilename,"r")) == NULL) {
305 fprintf(stderr," Error: Cannot find the file %s\n",graphFilename);
306 perror("fopen");
307 return;
308 } else {
309 /* printf("(spoon2d) \n\nGRAPH%i\n",i); */
310 fscanf(graphFile,"%g %g %g %g\n",
311 &(graphArray[i].xmin),
312 &(graphArray[i].ymin),
313 &(graphArray[i].xmax),
314 &(graphArray[i].ymax));
315 /* printf("(spoon2d) xmin=%g ymin=%g xmax=%g ymax=%g\n",
316 graphArray[i].xmin,graphArray[i].ymin,
317 graphArray[i].xmax,graphArray[i].ymax); */
318 fscanf(graphFile,"%g %g\n",
319 &(graphArray[i].xNorm),
320 &(graphArray[i].yNorm));
321 /* printf("(spoon2d) xNorm=%g yNorm=%g\n",
322 graphArray[i].xNorm,graphArray[i].yNorm); */
323 fscanf(graphFile,"%g %g\n",
324 &(graphArray[i].originX),
325 &(graphArray[i].originY));
326 /* printf("(spoon2d) originX=%g originY=%g\n",
327 graphArray[i].originX,graphArray[i].originY); */
328 fscanf(graphFile,"%g %g\n",
329 &(graphArray[i].spadUnitX),
330 &(graphArray[i].spadUnitY));
331 /* printf("(spoon2d) spadUnitX=%g spadUnitY=%g\n",
332 graphArray[i].spadUnitX,graphArray[i].spadUnitY); */
333 fscanf(graphFile,"%g %g\n",
334 &(graphArray[i].unitX),
335 &(graphArray[i].unitY));
336 /* printf("(spoon2d) unitX=%g unitY=%g\n",
337 graphArray[i].unitX,graphArray[i].unitY); */
338 fscanf(graphFile,"%d\n",
339 &(graphArray[i].numberOfLists));
340 /* printf("(spoon2d) numberOfLists=%d\n",
341 graphArray[i].numberOfLists); */
342 if (!(aList =
343 (pointListStruct *)malloc(graphArray[i].numberOfLists *
344 sizeof(pointListStruct)))) {
345 fprintf(stderr,"viewAlone: Fatal Error>> Out of memory trying to receive a graph.\n");
346 exit(-1);
347 }
348 graphArray[i].listOfListsOfPoints = aList;
349 for (j=0;
350 j<graphArray[i].numberOfLists;
351 j++, aList++) {
352 /* printf("(spoon2d) list number %d\n",j); */
353 fscanf(graphFile,"%d\n",&(aList->numberOfPoints));
354 /* printf("(spoon2d) number of points %d\n",
355 aList->numberOfPoints); */
356 fscanf(graphFile,"%d %d %d\n",
357 &(aList->pointColor),
358 &(aList->lineColor),
359 &(aList->pointSize));
360 /* printf("(spoon2d) pointColor=%d lineColor=%d pointSize=%d\n",
361 aList->pointColor,aList->lineColor,aList->pointSize); */
362 if (!(aPoint = (pointStruct *)malloc(aList->numberOfPoints *
363 sizeof(pointStruct)))) {
364 fprintf(stderr,"viewAlone: Fatal Error>> Out of memory trying to receive a graph.\n");
365 exit(-1);
366 }
367 aList->listOfPoints = aPoint; /** point to current point list **/
368 for (k=0;
369 k<aList->numberOfPoints;
370 k++,aPoint++)
371 { fscanf(graphFile,"%g %g %g %g\n",
372 &(aPoint->x),
373 &(aPoint->y),
374 &(aPoint->hue),
375 &(aPoint->shade));
376 /* printf("(spoon2d)k=%d x=%g y=%g hue=%g shade=%g\n",
377 k,aPoint->x,aPoint->y,aPoint->hue,aPoint->shade); */
378 }
379 } /* for j, aList */
380 fclose(graphFile);
381 } /* else, opened up a file */
382 } /* if graph.key */
383 } /* for i */
384 } /* makeView2DFromFileData */
385