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