Annotation of OpenXM_contrib/pari/src/graph/Gnuplot.h, Revision 1.1.1.1
1.1 maekawa 1: /* This header should be included in one C file only! */
2:
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <setjmp.h>
6:
7: #ifdef __cplusplus
8: extern "C" {
9: #endif
10:
11: /* CAT2:
12: * This macro catenates 2 tokens together.
13: */
14: /* STRINGIFY:
15: * This macro surrounds its token with double quotes.
16: */
17: #ifndef CAT2
18: # if 42 == 1
19: # define CAT2(a,b)a/**/b
20: # define CAT3(a,b,c)a/**/b/**/c
21: # define CAT4(a,b,c,d)a/**/b/**/c/**/d
22: # define CAT5(a,b,c,d,e)a/**/b/**/c/**/d/**/e
23: # define STRINGIFY(a)"a"
24: /* If you can get stringification with catify, tell me how! */
25: # endif
26: # if 42 == 42
27: # define CAT2(a,b)a ## b
28: # define CAT3(a,b,c)a ## b ## c
29: # define CAT4(a,b,c,d)a ## b ## c ## d
30: # define CAT5(a,b,c,d,e)a ## b ## c ## d ## e
31: # define StGiFy(a)# a
32: # define STRINGIFY(a)StGiFy(a)
33: # define SCAT2(a,b)StGiFy(a) StGiFy(b)
34: # define SCAT3(a,b,c)StGiFy(a) StGiFy(b) StGiFy(c)
35: # define SCAT4(a,b,c,d)StGiFy(a) StGiFy(b) StGiFy(c) StGiFy(d)
36: # define SCAT5(a,b,c,d,e)StGiFy(a) StGiFy(b) StGiFy(c) StGiFy(d) StGiFy(e)
37: # endif
38: # ifndef CAT2
39: # include "Bletch: How does this C preprocessor catenate tokens?"
40: # endif
41: #endif /* CAT2 */
42:
43:
44: #define TERM_CAN_MULTIPLOT 1 /* tested if stdout not redirected */
45: #define TERM_CANNOT_MULTIPLOT 2 /* tested if stdout is redirected */
46: #define TERM_BINARY 4 /* open output file with "b" */
47:
48: #ifndef NO_JUNK_SMALL
49:
50: /* Compatibility with the old gnuplot: */
51: extern FILE *outfile;
52: FILE *outfile = NULL;
53:
54: extern FILE *gpoutfile;
55: FILE *gpoutfile = NULL;
56:
57: static outfile_set;
58: static void
59: set_gpoutfile(void)
60: {
61: outfile = stdout;
62: gpoutfile = stdout;
63: }
64:
65: #define SET_OUTFILE (outfile_set++ ? 1 : (set_gpoutfile(), 1))
66:
67: extern int encoding;
68: int encoding = 0;
69: extern float xoffset; /* x origin */
70: extern float yoffset; /* y origin */
71: float xoffset = 0.0; /* x origin */
72: float yoffset = 0.0; /* y origin */
73: extern int multiplot;
74: int multiplot = 0;
75:
76: extern char *outstr;
77: #define MAX_ID_LEN 50
78: /* char outstr[MAX_ID_LEN+1] = "STDOUT"; */
79: char *outstr = NULL;
80: extern double ticscale; /* scale factor for tic marks (was (0..1])*/
81: double ticscale = 1.0; /* scale factor for tic mark */
82:
83: char *input_line = NULL;
84: int inline_num; /* from command.c */
85:
86: float xsize=1.0, ysize=1.0;
87: double pointsize=1.0; /* During test! */
88:
89: int interactive; /* from plot.c */
90: char *infile_name; /* from plot.c */
91: extern char default_font[];
92: char default_font[MAX_ID_LEN+1] = "\0"; /* Entry added by DJL */
93:
94: typedef int TBOOLEAN;
95:
96: enum DATA_TYPES {
97: INTGR, CMPLX
98: };
99:
100: #if !(defined(ATARI)&&defined(__GNUC__)&&defined(_MATH_H)) && !(defined(MTOS)&&defined(__GNUC__)&&defined(_MATH_H)) /* FF's math.h has the type already */
101: struct cmplx {
102: double real, imag;
103: };
104: #endif
105:
106: struct value {
107: enum DATA_TYPES type;
108: union {
109: int int_val;
110: struct cmplx cmplx_val;
111: } v;
112: };
113:
114: struct lexical_unit { /* produced by scanner */
115: TBOOLEAN is_token; /* true if token, false if a value */
116: struct value l_val;
117: int start_index; /* index of first char in token */
118: int length; /* length of token in chars */
119: };
120:
121: /* char *token; */
122: #define MAX_TOKENS 20
123: extern struct lexical_unit *token;
124: struct lexical_unit tokens[MAX_TOKENS]; /* We only process options,
125: there should not be many */
126: struct lexical_unit *token = tokens;
127: long c_token = 0, num_tokens = 0;
128: char term_options[200] = "";
129:
130: /* Here are the only missing functions: */
131:
132: struct value*
133: const_express(struct value*v)
134: {
135: if (token[c_token].is_token)
136: croak("Expect a number, got a string");
137: *v = token[c_token++].l_val;
138: return v;
139: }
140:
141: void*
142: gp_alloc(unsigned long size, char *usage)
143: {
144: return malloc(size);
145: }
146:
147: void*
148: gp_realloc(void *old, unsigned long size, char *usage)
149: {
150: return realloc(old,size);
151: }
152:
153: void
154: bail_to_command_line()
155: {
156: croak("panic: gnuplot");
157: }
158:
159: #endif /* NO_JUNK_SMALL */
160:
161: /* Cannot pull the whole plot.h, too many contradictions. */
162:
163: #ifdef __ZTC__
164: typedef int (*FUNC_PTR)(...);
165: #else
166: typedef int (*FUNC_PTR)();
167: #endif
168:
169: struct TERMENTRY {
170: char *name;
171: #if defined(_Windows) && !defined(WIN32)
172: char GPFAR description[80]; /* to make text go in FAR segment */
173: #else
174: char *description;
175: #endif
176: unsigned int xmax,ymax,v_char,h_char,v_tic,h_tic;
177: FUNC_PTR options,init,reset,text,scale,graphics,move,vector,linetype,
178: put_text,text_angle,justify_text,point,arrow,set_font,
179: pointsize;
180: int flags;
181: FUNC_PTR suspend,resume,fillbox,linewidth;
182: };
183:
184: #ifdef _Windows
185: # define termentry TERMENTRY far
186: #else
187: # define termentry TERMENTRY
188: #endif
189:
190: extern struct termentry *term;
191: struct termentry *term;
192:
193: #define RETVOID
194: #define RETINT , 1
195:
196: #define F_0 void(*)()
197: #define F_1 void(*)(int)
198: #define F_1I int(*)(int)
199: #define F_1D void(*)(double)
200: #define F_1IP int(*)(char*)
201: #define F_2 void(*)(unsigned int,unsigned int)
202: #define F_2D int(*)(double,double)
203: #define F_3 void(*)(unsigned int,unsigned int,int)
204: #define F_3T void(*)(int,int,char*)
205: #define F_4 void(*)(int,int,int,int)
206: #define F_5 void(*)(int,int,int,int,int)
207:
208: #define CALL_G_METH0(method) CALL_G_METH(method,0,(),RETVOID)
209: #define CALL_G_METH1(method,arg1) CALL_G_METH(method,1,(arg1),RETVOID)
210: #define CALL_G_METH1I(method,arg1) CALL_G_METH(method,1I,(arg1),RETINT)
211: #define CALL_G_METH1D(method,arg1) CALL_G_METH(method,1D,(arg1),RETVOID)
212: #define CALL_G_METH1IP(method,arg1) CALL_G_METH(method,1IP,(arg1),RETINT)
213: #define CALL_G_METH2(method,arg1,arg2) \
214: CALL_G_METH(method,2,((arg1),(arg2)),RETVOID)
215: #define CALL_G_METH2D(method,arg1,arg2) \
216: CALL_G_METH(method,2D,((arg1),(arg2)),RETINT)
217: #define CALL_G_METH3(method,arg1,arg2,arg3) \
218: CALL_G_METH(method,3,((arg1),(arg2),(arg3)),RETVOID)
219: #define CALL_G_METH3T(method,arg1,arg2,arg3) \
220: CALL_G_METH(method,3T,((arg1),(arg2),(arg3)),RETVOID)
221: #define CALL_G_METH4(method,arg1,arg2,arg3,arg4) \
222: CALL_G_METH(method,4,((arg1),(arg2),(arg3),(arg4)),RETVOID)
223: #define CALL_G_METH5(method,arg1,arg2,arg3,arg4,arg5) \
224: CALL_G_METH(method,5,((arg1),(arg2),(arg3),(arg4),(arg5)),RETVOID)
225:
226: #define CALL_G_METH(method,mult,args,returnval) ( \
227: (term==0) ? ( \
228: croak("No terminal specified") returnval \
229: ) : \
230: (*(CAT2(F_,mult))term->method)args \
231: )
232:
233: #define GET_G_FLAG(mask) ( \
234: (term==0) ? ( \
235: croak("No terminal specified") RETINT \
236: ) : \
237: (term->flags & (mask)))
238:
239: #ifdef DONT_POLLUTE_INIT
240: # define gptable_init() CALL_G_METH0(init)
241: #else
242: # define init() CALL_G_METH0(init)
243: # define gptable_init init
244: #endif
245: #define reset() CALL_G_METH0(reset)
246: #define text() CALL_G_METH0(text)
247: #define options() CALL_G_METH0(options)
248: #define graphics() CALL_G_METH0(graphics)
249: #define linetype(lt) CALL_G_METH1(linetype,lt)
250: #define justify_text(mode) CALL_G_METH1I(justify_text,mode)
251: #define text_angle(ang) CALL_G_METH1I(text_angle,ang)
252: #define scale(xs,ys) CALL_G_METH2D(scale,xs,ys)
253: #define move(x,y) CALL_G_METH2(move,x,y)
254: #define vector(x,y) CALL_G_METH2(vector,x,y)
255: #define put_text(x,y,str) CALL_G_METH3T(put_text,x,y,str)
256: #define point(x,y,p) CALL_G_METH3(point,x,y,p)
257: #define arrow(sx,sy,ex,ey,head) CALL_G_METH5(arrow,sx,sy,ex,ey,head)
258: #define set_font(font) CALL_G_METH1IP(set_font,font)
259: #define setpointsize(size) CALL_G_METH1D(pointsize,size)
260: #define suspend() CALL_G_METH0(suspend)
261: #define resume() CALL_G_METH0(resume)
262: #define fillbox(sx,sy,ex,ey,head) CALL_G_METH5(fillbox,sx,sy,ex,ey,head)
263: #define linewidth(size) CALL_G_METH1D(linewidth,size)
264: #define can_multiplot() GET_G_FLAG(TERM_CAN_MULTIPLOT)
265: #define cannot_multiplot() GET_G_FLAG(TERM_CANNOT_MULTIPLOT)
266: #define is_binary() GET_G_FLAG(TERM_BINARY)
267:
268: #define termprop(prop) (term->prop)
269: #define termset(term) my_change_term(term,strlen(term))
270:
271: struct termentry * change_term(char*,int);
272:
273: #define TTABLE_STARTPLOT 0
274: #define TTABLE_ENDPLOT 1
275: #define TTABLE_STARTMPLOT 2
276: #define TTABLE_ENDMPLOT 3
277: #define TTABLE_INIT 4
278: #define TTABLE_LIST 5
279: #define TTABLE_COUNT 6
280:
281: typedef void (*TSET_FP)(char *s);
282: typedef void (*TST_END_FP)(void);
283: typedef void (*SET_SIZES_t)(double x, double y);
284: typedef double (*GET_SIZES_t)(int flag);
285:
286: struct t_ftable {
287: int loaded;
288: FUNC_PTR change_term_p;
289: TSET_FP term_set_outputp;
290: SET_SIZES_t set_sizesp;
291: GET_SIZES_t get_sizesp;
292: TST_END_FP term_funcs[TTABLE_COUNT];
293: };
294:
295: #ifdef DYNAMIC_PLOTTING /* Can load plotting DLL later */
296:
297: UNKNOWN_null()
298: {
299: croak("gnuplot-like plotting environment not loaded yet");
300: }
301:
302: static void myterm_table_not_loaded_v(void);
303: static void myterm_table_not_loaded(char*);
304: static int myterm_table_not_loaded_u();
305: static void myterm_table_not_loaded_vdd(double x, double y);
306: static double myterm_table_not_loaded_di(int flag);
307:
308: #if 0
309: static int ftable_warned;
310: static void
311: tmp_my_term_init
312: {
313: if (!warned++)
314: warn("This runtime link with gnuplot-shim does not implement midlevel start/end functions");
315: shim_myinit();
316: }
317: #endif
318:
319: static struct t_ftable my_term_ftable =
320: {
321: 0, &myterm_table_not_loaded_u, &myterm_table_not_loaded,
322: &myterm_table_not_loaded_vdd,
323: &myterm_table_not_loaded_di,
324: {&myterm_table_not_loaded_v, &myterm_table_not_loaded_v,
325: &myterm_table_not_loaded_v, &myterm_table_not_loaded_v,
326: &myterm_table_not_loaded_v, &myterm_table_not_loaded_v}
327: };
328:
329: static struct t_ftable *my_term_ftablep = &my_term_ftable;
330:
331: static void
332: myterm_table_not_loaded_v(void)
333: {
334: if (!my_term_ftablep->loaded) {
335: UNKNOWN_null();
336: return;
337: }
338: croak("This runtime link with gnuplot-shim does not implement midlevel start/end functions");
339: }
340:
341: static void
342: myterm_table_not_loaded(char *s)
343: {
344: myterm_table_not_loaded_v();
345: }
346:
347: static void
348: myterm_table_not_loaded_vdd(double x, double y)
349: {
350: myterm_table_not_loaded_v();
351: }
352:
353: static double
354: myterm_table_not_loaded_di(int flag)
355: {
356: myterm_table_not_loaded_v();
357: }
358:
359: static int
360: myterm_table_not_loaded_u()
361: {
362: myterm_table_not_loaded_v();
363: return 0;
364: }
365:
366: # define change_term (*my_term_ftablep->change_term_p)
367: # define term_set_output (*my_term_ftablep->term_set_outputp)
368: # define term_start_plot (*my_term_ftablep->term_funcs[TTABLE_STARTPLOT])
369: # define term_end_plot (*my_term_ftablep->term_funcs[TTABLE_ENDPLOT])
370: # define term_start_multiplot (*my_term_ftablep->term_funcs[TTABLE_STARTMPLOT])
371: # define term_end_multiplot (*my_term_ftablep->term_funcs[TTABLE_ENDMPLOT])
372: # define term_init (*my_term_ftablep->term_funcs[TTABLE_INIT])
373: # define list_terms (*my_term_ftablep->term_funcs[TTABLE_LIST])
374: # define plotsizes_scale (*my_term_ftablep->set_sizesp)
375: # define plotsizes_scale_get (*my_term_ftablep->get_sizesp)
376:
377: # define scaled_xmax() ((int)termprop(xmax)*plotsizes_scale_get(0))
378: # define scaled_ymax() ((int)termprop(ymax)*plotsizes_scale_get(1))
379:
380: #define USE_FUNCTION_FROM_TABLE
381:
382: static struct termentry *
383: my_change_term(char*s,int l)
384: {
385: SET_OUTFILE;
386: if (!my_term_ftablep->change_term_p)
387: UNKNOWN_null();
388: return term = (struct termentry *)(*my_term_ftablep->change_term_p)(s,l);
389: }
390:
391: static struct termentry dummy_term_tbl[] = {
392: {"unknown", "Unknown terminal type - not a plotting device",
393: 100, 100, 1, 1,
394: 1, 1, UNKNOWN_null, UNKNOWN_null, UNKNOWN_null,
395: UNKNOWN_null, UNKNOWN_null, UNKNOWN_null, UNKNOWN_null, UNKNOWN_null,
396: UNKNOWN_null, UNKNOWN_null, UNKNOWN_null,
397: UNKNOWN_null, UNKNOWN_null, UNKNOWN_null, UNKNOWN_null, UNKNOWN_null, 0,
398: UNKNOWN_null, UNKNOWN_null, UNKNOWN_null, UNKNOWN_null},
399: };
400:
401: #define set_term_funcp(change_p, term_p) set_term_funcp2((change_p), 0)
402: /* #define set_term_funcp3(change_p, term_p, tchange) \
403: set_term_funcp2((change_p), (tchange)) */
404:
405: /* This function should be called before any graphic code can be used... */
406: void
407: set_term_funcp2(FUNC_PTR change_p, TSET_FP tchange)
408: {
409: SET_OUTFILE;
410: my_term_ftable.change_term_p = change_p;
411: my_term_ftable.loaded = 1;
412: if (tchange) {
413: my_term_ftable.term_set_outputp = tchange;
414: }
415: }
416:
417: /* Used from Math::Pari */
418: void
419: set_term_funcp3(FUNC_PTR change_p, void *term_p, TSET_FP tchange)
420: {
421: set_term_funcp2(change_p, tchange);
422: }
423:
424: void
425: set_term_ftable(struct t_ftable *p)
426: {
427: SET_OUTFILE;
428: my_term_ftablep = p;
429: }
430:
431: #else /* !DYNAMIC_PLOTTING */
432:
433: extern struct termentry term_tbl[];
434:
435: # define my_change_term change_term
436: # define my_term_tbl term_tbl
437:
438: extern void term_set_output(char *s);
439: extern void term_start_plot(void);
440: extern void term_end_plot(void);
441: extern void term_start_multiplot(void);
442: extern void term_end_multiplot(void);
443: extern void term_init(void);
444: extern void list_terms(void);
445:
446: static void
447: plotsizes_scale(double x, double y) { xsize=x; ysize=y; }
448:
449: static double
450: plotsizes_get(int flag) { return (flag ? ysize : xsize); }
451:
452: struct t_ftable my_term_ftable =
453: {
454: 1, (FUNC_PTR)&change_term, &term_set_output,
455: &plotsizes_scale, &plotsizes_get,
456: {&term_start_plot, &term_end_plot,
457: &term_start_multiplot, &term_end_multiplot, &term_init, &list_terms}
458: };
459:
460: struct t_ftable *get_term_ftable() { SET_OUTFILE; return &my_term_ftable; }
461: void set_term_ftable() { SET_OUTFILE; }
462:
463:
464: void
465: set_term_funcp3(FUNC_PTR change_p, void *term_p, TSET_FP tchange)
466: {
467: SET_OUTFILE;
468: my_term_ftable.change_term_p = change_p;
469: my_term_ftable.loaded = 1;
470: if (tchange) {
471: my_term_ftable.term_set_outputp = tchange;
472: }
473: }
474:
475: #define scaled_xmax() ((int)termprop(xmax)*xsize)
476: #define scaled_ymax() ((int)termprop(ymax)*ysize)
477:
478: #endif /* !DYNAMIC_PLOTTING */
479:
480: #define int_get_term_ftable() ((IV)get_term_ftable())
481: #define int_set_term_ftable(a) (v_set_term_ftable((void*)a))
482:
483: void
484: v_set_term_ftable(void *a) { set_term_ftable((struct t_ftable*)a); }
485:
486: void
487: setup_gpshim(void) { SET_OUTFILE; }
488:
489: #ifdef SET_OPTIONS_FROM_STRING
490: /* This sets the tokens for the options */
491: void
492: set_tokens_string(char *start)
493: {
494: char *s = start;
495: char *tstart;
496: int is_real, is_integer, has_exp;
497:
498: num_tokens = 0;
499: while (num_tokens < MAX_TOKENS) {
500: while (*s == ' ' || *s == '\t' || *s == '\n')
501: s++;
502: if (!*s)
503: return;
504: tstart = s;
505: if (*s == ',') {
506: s++;
507: is_integer = is_real = 0;
508: goto process;
509: }
510: is_integer = is_real = ((*s) != 0);
511: if (*s == '+' || *s == '-')
512: s++;
513: has_exp = 0;
514: while (*s && !(*s == ' ' || *s == '\t' || *s == '\n')) {
515: if (!(*s <= '9' && *s >= '0')) {
516: if (*s == '.') {
517: if (!is_integer)
518: is_real = 0;
519: else if (is_integer == 1 && !(s[1] <= '9' && s[1] >= '0'))
520: is_real = 0;
521: } else if (*s == 'e' || *s == 'E') {
522: if (has_exp)
523: is_real = 0;
524: has_exp = 1;
525: if (s[1] == '+' || s[1] == '-')
526: s++;
527: } else if (*s == ',' && (is_integer || is_real))
528: break;
529: else
530: is_real = 0;
531: is_integer = 0;
532: } else if (is_integer)
533: is_integer++;
534: s++;
535: }
536: process:
537: token[num_tokens].start_index = tstart - input_line;
538: token[num_tokens].length = s - tstart;
539: if (is_integer) {
540: token[num_tokens].is_token = 0;
541: token[num_tokens].l_val.type = INTGR;
542: token[num_tokens].l_val.v.int_val = atoi(tstart);
543: } else if (is_real) {
544: token[num_tokens].is_token = 0;
545: token[num_tokens].l_val.type = CMPLX;
546: token[num_tokens].l_val.v.cmplx_val.real = atof(tstart);
547: token[num_tokens].l_val.v.cmplx_val.imag = 0;
548: } else {
549: token[num_tokens].is_token = 1;
550: }
551: num_tokens++;
552: }
553: if (num_tokens >= MAX_TOKENS) {
554: char buf[80];
555: sprintf(buf, "panic: more than %d tokens for options", MAX_TOKENS);
556: croak(buf);
557: }
558: }
559:
560: void
561: set_options_from(char *s)
562: {
563: char *o = input_line;
564:
565: input_line = s; /* for error reports */
566: set_tokens_string(s);
567: options();
568: input_line = o;
569: c_token = num_tokens = 0;
570: }
571: #endif
572:
573: #ifdef GNUPLOT_OUTLINE_STDOUT
574: int
575: StartOutput() {}
576:
577: int
578: EndOutput() {}
579:
580: int
581: OutLine(char *s)
582: {
583: return fprintf(stdout, "%s", s);
584: }
585: #endif
586:
587: #ifdef __cplusplus
588: }
589: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>