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