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