plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

pic.h (10313B)


      1 #include <string.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 
      5 #ifndef PI
      6 #define PI 3.1415926535897932384626433832795028841971693993751
      7 #endif
      8 
      9 #define	MAXWID	8.5	/* default limits max picture to 8.5 x 11; */
     10 #define	MAXHT	11	/* change to taste without peril */
     11 
     12 #define	dprintf	if(dbg)printf
     13 
     14 extern	char	errbuf[1000];
     15 
     16 #define	ERROR	tpicsnprintf(errbuf, sizeof errbuf,
     17 #define	FATAL	), yyerror(errbuf), exit(1)
     18 #define	WARNING	), yyerror(errbuf)
     19 
     20 #define	DEFAULT	0
     21 
     22 #define	HEAD1	1
     23 #define	HEAD2	2
     24 #define	HEAD12	(HEAD1+HEAD2)
     25 #define	INVIS	4
     26 #define	CW_ARC	8	/* clockwise arc */
     27 #define	DOTBIT	16	/* line styles */
     28 #define	DASHBIT	32
     29 #define	FILLBIT	64	/* gray-fill on boxes, etc. */
     30 
     31 #define	CENTER	01	/* text attributes */
     32 #define	LJUST	02
     33 #define	RJUST	04
     34 #define	ABOVE	010
     35 #define	BELOW	020
     36 #define	SPREAD	040
     37 
     38 #define	SCALE	1.0	/* default scale: units/inch */
     39 #define	WID	0.75	/* default width for boxes and ellipses */
     40 #define	WID2	0.375
     41 #define	HT	0.5	/* default height and line length */
     42 #define	HT2	(HT/2)
     43 #define	HT5	(HT/5)
     44 #define	HT10	(HT/10)
     45 
     46 /* these have to be like so, so that we can write */
     47 /* things like R & V, etc. */
     48 #define	H	0
     49 #define	V	1
     50 #define	R_DIR	0
     51 #define	U_DIR	1
     52 #define	L_DIR	2
     53 #define	D_DIR	3
     54 #define	ishor(n)	(((n) & V) == 0)
     55 #define	isvert(n)	(((n) & V) != 0)
     56 #define	isright(n)	((n) == R_DIR)
     57 #define	isleft(n)	((n) == L_DIR)
     58 #define	isdown(n)	((n) == D_DIR)
     59 #define	isup(n)		((n) == U_DIR)
     60 
     61 typedef	float	ofloat;	/* for o_val[] in obj;  could be double */
     62 
     63 typedef struct obj {	/* stores various things in variable length */
     64 	int	o_type;
     65 	int	o_count;	/* number of things */
     66 	int	o_nobj;		/* index in objlist */
     67 	int	o_mode;		/* hor or vert */
     68 	float	o_x;		/* coord of "center" */
     69 	float	o_y;
     70 	int	o_nt1;		/* 1st index in text[] for this object */
     71 	int	o_nt2;		/* 2nd; difference is #text strings */
     72 	int	o_attr;		/* HEAD, CW, INVIS, etc., go here */
     73 	int	o_size;		/* linesize */
     74 	int	o_nhead;	/* arrowhead style */
     75 	struct symtab *o_symtab; /* symtab for [...] */
     76 	float	o_ddval;	/* value of dot/dash expression */
     77 	float	o_fillval;	/* gray scale value */
     78 	ofloat	o_val[1];	/* actually this will be > 1 in general */
     79 				/* type is not always FLOAT!!!! */
     80 } obj;
     81 
     82 typedef union {		/* the yacc stack type */
     83 	int	i;
     84 	char	*p;
     85 	obj	*o;
     86 	double	f;
     87 	struct symtab *st;
     88 } YYSTYPE;
     89 
     90 extern	YYSTYPE	yylval, yyval;
     91 
     92 struct symtab {
     93 	char	*s_name;
     94 	int	s_type;
     95 	YYSTYPE	s_val;
     96 	struct symtab *s_next;
     97 };
     98 
     99 typedef struct {	/* attribute of an object */
    100 	int	a_type;
    101 	int	a_sub;
    102 	YYSTYPE	a_val;
    103 } Attr;
    104 
    105 typedef struct {
    106 	int	t_type;		/* CENTER, LJUST, etc. */
    107 	char	t_op;		/* optional sign for size changes */
    108 	char	t_size;		/* size, abs or rel */
    109 	char	*t_val;
    110 } Text;
    111 
    112 #define	String	01
    113 #define	Macro	02
    114 #define	File	04
    115 #define	Char	010
    116 #define	Thru	020
    117 #define	Free	040
    118 
    119 typedef struct {	/* input source */
    120 	int	type;	/* Macro, String, File */
    121 	char	*sp;	/* if String or Macro */
    122 } Src;
    123 
    124 extern	Src	src[], *srcp;	/* input source stack */
    125 
    126 typedef struct {
    127 	FILE	*fin;
    128 	char	*fname;
    129 	int	lineno;
    130 } Infile;
    131 
    132 extern	Infile	infile[], *curfile;
    133 
    134 #define	MAXARGS	20
    135 typedef struct {	/* argument stack */
    136 	char	*argstk[MAXARGS];	/* pointers to args */
    137 	char	*argval;	/* points to space containing args */
    138 } Arg;
    139 
    140 extern	int	dbg;
    141 extern	obj	**objlist;
    142 extern	int	nobj, nobjlist;
    143 extern	Attr	*attr;
    144 extern	int	nattr, nattrlist;
    145 extern	Text	*text;
    146 extern	int	ntextlist;
    147 extern	int	ntext;
    148 extern	int	ntext1;
    149 extern	double	curx, cury;
    150 extern	int	hvmode;
    151 extern	int	codegen;
    152 extern	int	PEseen;
    153 extern	double	deltx, delty;
    154 extern	int	lineno;
    155 extern	int	synerr;
    156 
    157 extern	double	xmin, ymin, xmax, ymax;
    158 
    159 struct pushstack {
    160 	double	p_x;
    161 	double	p_y;
    162 	int	p_hvmode;
    163 	double	p_xmin;
    164 	double	p_ymin;
    165 	double	p_xmax;
    166 	double	p_ymax;
    167 	struct symtab *p_symtab;
    168 };
    169 extern	struct pushstack stack[];
    170 extern	int	nstack;
    171 extern	int	cw;
    172 
    173 
    174 #define	Log10(x) errcheck(log10(x), "log")
    175 #define	Exp(x)	errcheck(exp(x), "exp")
    176 #define	Sqrt(x)	errcheck(sqrt(x), "sqrt")
    177 
    178 
    179 char*		addnewline(char *p)	/* add newline to end of p */;
    180 obj*		addpos(obj *p, obj *q);
    181 void		addtattr(int sub)		/* add text attrib to existing item */;
    182 void		arc(double xc, double yc, double x0, double y0, double x1, double y1)	/* draw arc with center xc,yc */;
    183 void		arc_extreme(double x0, double y0, double x1, double y1, double xc, double yc);
    184 obj*		arcgen(int type)	/* handles circular and (eventually) elliptical arcs */;
    185 void		arrow(double x0, double y0, double x1, double y1, double w, double h, double ang, int nhead) 	/* draw arrow (without shaft) */ /* head wid w, len h, rotated ang */ /* and drawn with nhead lines */;
    186 int		baldelim(int c, char *s)	/* replace c by balancing entry in s */;
    187 void		blockadj(obj *p)	/* adjust coords in block starting at p */;
    188 obj*		blockgen(obj *p, obj *q)	/* handles [...] */;
    189 obj*		boxgen(void);
    190 void		checkscale(char *s)	/* if s is "scale", adjust default variables */;
    191 obj*		circgen(int type);
    192 void		copy(void)	/* begin input from file, etc. */;
    193 void		copydef(struct symtab *p)	/* remember macro symtab ptr */;
    194 void		copyfile(char *s)	/* remember file to start reading from */;
    195 struct symtab*	copythru(char *s)	/* collect the macro name or body for thru */;
    196 void		copyuntil(char *s)	/* string that terminates a thru */;
    197 int		curdir(void)	/* convert current dir (hvmode) to RIGHT, LEFT, etc. */;
    198 void		definition(char *s)	/* collect definition for s and install */ /* definitions picked up lexically */;
    199 char*		delimstr(char *s)	/* get body of X ... X */ 	/* message if too big */;
    200 void		do_thru(void)	/* read one line, make into a macro expansion */;
    201 void		dodef(struct symtab *stp)	/* collect args and switch input to defn */;
    202 void		dot(void);
    203 void		dotbox(double x0, double y0, double x1, double y1, int ddtype, double ddval)	/* dotted or dashed box */;
    204 void		dotext(obj *p)	/* print text strings of p in proper vertical spacing */;
    205 void		dotline(double x0, double y0, double x1, double y1, int ddtype, double ddval);
    206 void		dotline(double x0, double y0, double x1, double y1, int ddtype, double ddval) /* dotted line */;
    207 void		ellipse(double x, double y, double r1, double r2);
    208 void		endfor(void)	/* end one iteration of for loop */;
    209 void		eprint(void)	/* try to print context around error */;
    210 double		errcheck(double x, char *s);
    211 void		exprsave(double f);
    212 void		extreme(double x, double y)	/* record max and min x and y values */;
    213 void		fillend(void);
    214 void		fillstart(double v)	/* only choose black, light grey (.75), or white, for now */;
    215 obj*		fixpos(obj *p, double x, double y);
    216 void		forloop(char *, double, double, int, double, char *)	/* set up a for loop */;
    217 void		fpecatch(int arg);
    218 void		freedef(char *s)	/* free definition for string s */;
    219 void		freesymtab(struct symtab *p)	/* free space used by symtab at p */;
    220 int		getarg(char *p)	/* pick up single argument, store in p, return length */;
    221 YYSTYPE		getblk(obj *p, char *s)	/* find union type for s in p */;
    222 double		getblkvar(obj *p, char *s)	/* find variable s2 in block p */;
    223 obj*		getblock(obj *p, char *s)	/* find variable s in block p */;
    224 double		getcomp(obj *p, int t)	/* return component of a position */;
    225 void		getdata(void);
    226 obj*		getfirst(int n, int t)	/* find n-th occurrence of type t */;
    227 double		getfval(char *s)	/* return float value of variable s */;
    228 obj*		gethere(void)	/* make a place for curx,cury */;
    229 obj*		getlast(int n, int t)	/* find n-th previous occurrence of type t */;
    230 obj*		getpos(obj *p, int corner)	/* find position of point */;
    231 YYSTYPE		getvar(char *s)	/* return value of variable s (usually pointer) */;
    232 char *		grow(char *ptr, char *name, int num, int size)	/* make array bigger */;
    233 char*		ifstat(double expr, char *thenpart, char *elsepart);
    234 int		input(void);
    235 void		label(char *s, int t, int nh)	/* text s of type t nh half-lines up */;
    236 obj*		leftthing(int c)	/* called for {... or [... */ 		/* really ought to be separate functions */;
    237 obj*		linegen(int type);
    238 struct symtab*	lookup(char *s)	/* find s in symtab */;
    239 int		main(int argc, char **argv);
    240 void		makeattr(int type, int sub, YYSTYPE val)	/* add attribute type and val */;
    241 obj*		makebetween(double f, obj *p1, obj* p2)	/* make position between p1 and p2 */;
    242 void		makefattr(int type, int sub, double f)	/* double attr */;
    243 void		makeiattr(int type, int i)	/* int attr */;
    244 obj*		makenode(int type, int n);
    245 void		makeoattr(int type, obj *o)	/* obj* attr */;
    246 obj*		makepos(double x, double y)	/* make a position cell */;
    247 void		maketattr(int sub, char *p)	/* text attribute: takes two */;
    248 struct symtab*	makevar(char *s, int t, YYSTYPE v)	/* make variable named s in table */ 	/* assumes s is static or from tostring */;
    249 void		makevattr(char *p)	/* varname attribute */;
    250 obj*		movegen(void);
    251 int		nextchar(void);
    252 void		nextfor(void)	/* do one iteration of a for loop */;
    253 void		pbstr(char *s);
    254 void		popsrc(void)	/* restore an old one */;
    255 void		print(void);
    256 void		printexpr(double f)	/* print expression for debugging */;
    257 void		printlf(int line, char *name);
    258 void		printpos(obj *p)	/* print position for debugging */;
    259 void		pushsrc(int type, char *ptr)	/* new input source */;
    260 int		quadrant(double x, double y);
    261 void		reset(void);
    262 void		resetvar(void)	/* reset variables listed */;
    263 obj*		rightthing(obj *p, int c)	/* called for ... ] or ... } */;
    264 void		savetext(int t, char *s)	/* record text elements for current object */;
    265 void		setdefaults(void)	/* set default sizes for variables like boxht */;
    266 int		setdir(int n)	/* set direction (hvmode) from LEFT, RIGHT, etc. */;
    267 void		setfval(char *s, double f)	/* set variable s to f */;
    268 void		shell_exec(void)	/* do it */;
    269 void		shell_init(void)	/* set up to interpret a shell command */;
    270 void		shell_text(char *s)	/* add string to command being collected */;
    271 void		space(double x0, double y0, double x1, double y1)	/* set limits of page */;
    272 void		spline(double x, double y, double/*sic*/ n, float *p, int dashed, double ddval);
    273 char*		sprintgen(char *fmt);
    274 obj*		subpos(obj *p, obj *q);
    275 obj*		textgen(void);
    276 char*		tostring(char *s);
    277 void		troff(char *s);
    278 obj*		troffgen(char *s)	/* save away a string of troff commands */;
    279 void		undefine(char *s)	/* undefine macro */;
    280 int		unput(int c);
    281 int		whatpos(obj *p, int corner, double *px, double *py)	/* what is the position (no side effect) */;
    282 void		yyerror(char *s);
    283 int		yyparse(void);
    284 void	tpicsnprintf(char*, int, const char*, ...);
    285 
    286 #include "tex.h"