1 /* 2 * Internal definitions for states. 3 * Copyright (c) 1997-1998 Markku Rossi. 4 * 5 * Author: Markku Rossi <mtr@iki.fi> 6 */ 7 8 /* 9 * This file is part of GNU Enscript. 10 * 11 * Enscript is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation, either version 3 of the License, or 14 * (at your option) any later version. 15 * 16 * Enscript is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with Enscript. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 25 #ifndef DEFS_H 26 #define DEFS_H 27 28 /* 29 * Config stuffs. 30 */ 31 32 #ifdef HAVE_CONFIG_H 33 #include <config.h> 34 #endif 35 36 #include <stdio.h> 37 #include <ctype.h> 38 39 #ifndef ___P 40 #if PROTOTYPES 41 #define ___P(protos) protos 42 #else /* no PROTOTYPES */ 43 #define ___P(protos) () 44 #endif /* no PROTOTYPES */ 45 #endif 46 47 #if STDC_HEADERS 48 49 #include <stdlib.h> 50 #include <string.h> 51 52 #else /* no STDC_HEADERS */ 53 54 #if HAVE_STDLIB_H 55 #include <stdlib.h> 56 #endif 57 58 #if HAVE_STRING_H 59 #include <string.h> 60 #endif 61 62 #ifndef HAVE_STRCHR 63 #define strchr index 64 #define strrchr rindex 65 #endif 66 char *strchr (); 67 char *strrchr (); 68 69 #ifndef HAVE_STRERROR 70 extern char *strerror ___P ((int)); 71 #endif 72 73 #ifndef HAVE_MEMMOVE 74 extern void *memmove ___P ((void *, void *, size_t)); 75 #endif 76 77 #ifndef HAVE_MEMCPY 78 extern void *memcpy ___P ((void *, void *, size_t)); 79 #endif 80 81 #endif /* no STDC_HEADERS */ 82 83 #if HAVE_UNISTD_H 84 #include <unistd.h> 85 #endif 86 87 #include <errno.h> 88 89 #if HAVE_SYS_TYPES_H 90 #include <sys/types.h> 91 #endif 92 93 #if HAVE_SYS_STAT_H 94 #include <sys/stat.h> 95 #endif 96 97 #include "gettext.h" 98 #define _(String) gettext (String) 99 100 #if HAVE_LC_MESSAGES 101 #include <locale.h> 102 #endif 103 104 #include "regex.h" 105 #include "xalloc.h" 106 #include "strhash.h" 107 108 /* 109 * Types and definitions. 110 */ 111 112 #define RULE_BEGIN ((void *) 0) 113 #define RULE_END ((void *) 1) 114 115 #define INBUFSIZE (20 * 1024) 116 117 #define IS_TRUE(n) ((n)->type != nINTEGER || (n)->u.integer != 0) 118 119 #define REGEXP(regexp) \ 120 ((regexp)->u.re.compiled.fastmap_accurate \ 121 ? (&(regexp)->u.re.compiled) \ 122 : (compile_regexp (regexp), &(regexp)->u.re.compiled)) 123 124 /* Flags for regular expressions. */ 125 #define fRE_CASE_INSENSITIVE 1 126 127 /* Generic linked list. */ 128 129 struct list_item_st 130 { 131 struct list_item_st *next; 132 void *data; 133 }; 134 135 typedef struct list_item_st ListItem; 136 137 struct list_st 138 { 139 ListItem *head; 140 ListItem *tail; 141 }; 142 143 typedef struct list_st List; 144 145 /* State. */ 146 147 struct state_st 148 { 149 char *name; 150 char *super_name; 151 struct state_st *super; 152 List *rules; 153 }; 154 155 typedef struct state_st State; 156 157 158 /* Node. */ 159 160 typedef enum 161 { 162 nVOID, 163 nSTRING, 164 nREGEXP, 165 nINTEGER, 166 nREAL, 167 nSYMBOL, 168 nARRAY 169 } NodeType; 170 171 struct node_st 172 { 173 NodeType type; 174 unsigned int refcount; 175 unsigned int linenum; 176 char *filename; 177 178 union 179 { 180 struct 181 { 182 char *data; 183 unsigned int len; 184 } str; 185 struct 186 { 187 char *data; 188 unsigned int len; 189 unsigned int flags; 190 regex_t compiled; 191 struct re_registers matches; 192 } re; 193 int integer; 194 double real; 195 char *sym; 196 struct 197 { 198 struct node_st **array; 199 unsigned int len; 200 unsigned int allocated; 201 } array; 202 } u; 203 }; 204 205 typedef struct node_st Node; 206 207 /* Cons cell. */ 208 struct cons_st 209 { 210 void *car; 211 void *cdr; 212 }; 213 214 typedef struct cons_st Cons; 215 216 /* Grammar types. */ 217 218 typedef enum 219 { 220 eSTRING, 221 eREGEXP, 222 eINTEGER, 223 eREAL, 224 eSYMBOL, 225 eNOT, 226 eAND, 227 eOR, 228 eFCALL, 229 eASSIGN, 230 eADDASSIGN, 231 eSUBASSIGN, 232 eMULASSIGN, 233 eDIVASSIGN, 234 ePOSTFIXADD, 235 ePOSTFIXSUB, 236 ePREFIXADD, 237 ePREFIXSUB, 238 eARRAYASSIGN, 239 eARRAYREF, 240 eQUESTCOLON, 241 eMULT, 242 eDIV, 243 ePLUS, 244 eMINUS, 245 eLT, 246 eGT, 247 eEQ, 248 eNE, 249 eGE, 250 eLE 251 } ExprType; 252 253 struct expr_st 254 { 255 ExprType type; 256 unsigned int linenum; 257 char *filename; 258 259 union 260 { 261 Node *node; 262 struct expr_st *not; 263 struct 264 { 265 Node *name; 266 List *args; 267 } fcall; 268 struct 269 { 270 Node *sym; 271 struct expr_st *expr; 272 } assign; 273 struct 274 { 275 struct expr_st *expr1; 276 struct expr_st *expr2; 277 struct expr_st *expr3; 278 } arrayassign; 279 struct 280 { 281 struct expr_st *expr1; 282 struct expr_st *expr2; 283 } arrayref; 284 struct 285 { 286 struct expr_st *cond; 287 struct expr_st *expr1; 288 struct expr_st *expr2; 289 } questcolon; 290 struct 291 { 292 struct expr_st *left; 293 struct expr_st *right; 294 } op; 295 } u; 296 }; 297 298 typedef struct expr_st Expr; 299 300 typedef enum 301 { 302 sRETURN, 303 sDEFSUB, 304 sBLOCK, 305 sIF, 306 sEXPR, 307 sWHILE, 308 sFOR 309 } StmtType; 310 311 struct stmt_st 312 { 313 StmtType type; 314 unsigned int linenum; 315 char *filename; 316 317 union 318 { 319 Expr *expr; 320 struct 321 { 322 Node *name; 323 Cons *closure; 324 } defsub; 325 struct 326 { 327 Expr *expr; 328 struct stmt_st *then_stmt; 329 struct stmt_st *else_stmt; 330 } stmt_if; 331 struct 332 { 333 Expr *expr; 334 struct stmt_st *body; 335 } stmt_while; 336 struct 337 { 338 Expr *init; 339 Expr *cond; 340 Expr *incr; 341 struct stmt_st *body; 342 } stmt_for; 343 List *block; 344 } u; 345 }; 346 347 typedef struct stmt_st Stmt; 348 349 struct environment_st 350 { 351 struct environment_st *next; 352 char *name; 353 Node *val; 354 }; 355 356 typedef struct environment_st Environment; 357 358 /* Primitive procedure. */ 359 typedef Node *(*Primitive) ___P ((char *prim_name, List *args, 360 Environment *env, char *filename, 361 unsigned int linenum)); 362 363 /* Variable definition chain. */ 364 struct variable_definition_st 365 { 366 struct variable_definition_st *next; 367 char *sym; 368 char *val; 369 }; 370 371 typedef struct variable_definition_st VariableDef; 372 373 /* Grammar and execution warning levels. */ 374 typedef enum 375 { 376 WARN_LIGHT = 10, 377 WARN_ALL = 100 378 } WarningLevel; 379 380 381 /* 382 * Global variables. 383 */ 384 385 extern char *program; 386 387 extern FILE *yyin; 388 extern FILE *ofp; 389 extern char *defs_file; 390 extern unsigned int linenum; 391 extern char *yyin_name; 392 extern WarningLevel warning_level; 393 extern char *path; 394 extern unsigned int verbose; 395 396 /* Namespaces. */ 397 extern StringHashPtr ns_prims; 398 extern StringHashPtr ns_vars; 399 extern StringHashPtr ns_subs; 400 extern StringHashPtr ns_states; 401 402 extern List *global_stmts; 403 extern List *start_stmts; 404 extern List *startrules; 405 extern List *namerules; 406 407 /* Void node value. There is only nvoid instance. */ 408 extern Node *nvoid; 409 410 extern FILE *ifp; 411 extern char *inbuf; 412 extern unsigned int data_in_buffer; 413 extern unsigned int bufpos; 414 extern int eof_seen; 415 extern char *current_fname; 416 extern unsigned int current_linenum; 417 418 extern struct re_registers *current_match; 419 extern char *current_match_buf; 420 421 /* Options. */ 422 423 extern char *start_state_arg; 424 extern char *start_state; 425 426 427 /* 428 * Prototypes for global functions. 429 */ 430 431 void init_primitives (); 432 433 /* Parser & lexer. */ 434 int yyparse (); 435 int yylex (); 436 void yyerror ___P ((char *msg)); 437 438 /* Generic linked list. */ 439 440 /* Create a new linked list. */ 441 List *list (); 442 443 /* Add a new element <data> to the beginning of list <list>. */ 444 void list_prepend ___P ((List *list, void *data)); 445 446 /* Add a new element <data> to the end of list <list>. */ 447 void list_append ___P ((List *list, void *data)); 448 449 450 /* Node manipulators. */ 451 452 Node *node_alloc ___P ((NodeType type)); 453 454 Node *node_copy ___P ((Node *node)); 455 456 void node_reference ___P ((Node *node)); 457 458 void node_free ___P ((Node *node)); 459 460 void enter_system_variable ___P ((char *name, char *value)); 461 462 void compile_regexp ___P ((Node *regexp)); 463 464 465 /* Grammar constructors. */ 466 467 Stmt *mk_stmt ___P ((StmtType type, void *arg1, void *arg2, void *arg3, 468 void *arg4)); 469 470 Expr *mk_expr ___P ((ExprType type, void *arg1, void *arg2, void *arg3)); 471 472 Cons *cons ___P ((void *car, void *cdr)); 473 474 void define_state ___P ((Node *sym, Node *super, List *rules)); 475 476 /* Execution. */ 477 478 Node *eval_expr ___P ((Expr *expr, Environment *env)); 479 480 Node *eval_statement ___P ((Stmt *stmt, Environment *env, int *return_seen)); 481 482 Node *eval_statement_list ___P ((List *lst, Environment *env, 483 int *return_seen)); 484 485 void process_file ___P ((char *fname)); 486 487 Node *execute_state ___P ((char *name)); 488 489 void load_states_file ___P ((char *name)); 490 491 /* 492 * Lookup state <name> and return its handle. If the state is 493 * undefined, the function tries to autoload it. 494 */ 495 State *lookup_state ___P ((char *name)); 496 497 #endif /* not DEFS_H */ 498