File: defs.h

package info (click to toggle)
enscript 1.6.5.90-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 7,264 kB
  • ctags: 4,824
  • sloc: ansic: 33,705; sh: 5,383; makefile: 649; yacc: 457; lex: 428; perl: 340; lisp: 109; sed: 16
file content (497 lines) | stat: -rw-r--r-- 8,389 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * Internal definitions for states.
 * Copyright (c) 1997-1998 Markku Rossi.
 *
 * Author: Markku Rossi <mtr@iki.fi>
 */

/*
 * This file is part of GNU Enscript.
 *
 * Enscript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Enscript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Enscript.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef DEFS_H
#define DEFS_H

/*
 * Config stuffs.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <ctype.h>

#ifndef ___P
#if PROTOTYPES
#define ___P(protos) protos
#else /* no PROTOTYPES */
#define ___P(protos) ()
#endif /* no PROTOTYPES */
#endif

#if STDC_HEADERS

#include <stdlib.h>
#include <string.h>

#else /* no STDC_HEADERS */

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

#if HAVE_STRING_H
#include <string.h>
#endif

#ifndef HAVE_STRCHR
#define strchr index
#define strrchr rindex
#endif
char *strchr ();
char *strrchr ();

#ifndef HAVE_STRERROR
extern char *strerror ___P ((int));
#endif

#ifndef HAVE_MEMMOVE
extern void *memmove ___P ((void *, void *, size_t));
#endif

#ifndef HAVE_MEMCPY
extern void *memcpy ___P ((void *, void *, size_t));
#endif

#endif /* no STDC_HEADERS */

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <errno.h>

#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#include "gettext.h"
#define _(String) gettext (String)

#if HAVE_LC_MESSAGES
#include <locale.h>
#endif

#include "regex.h"
#include "xalloc.h"
#include "strhash.h"

/*
 * Types and definitions.
 */

#define RULE_BEGIN	((void *) 0)
#define RULE_END	((void *) 1)

#define INBUFSIZE	(20 * 1024)

#define IS_TRUE(n) ((n)->type != nINTEGER || (n)->u.integer != 0)

#define REGEXP(regexp) \
  ((regexp)->u.re.compiled.fastmap_accurate			\
   ? (&(regexp)->u.re.compiled)					\
   : (compile_regexp (regexp), &(regexp)->u.re.compiled))

/* Flags for regular expressions. */
#define fRE_CASE_INSENSITIVE	1

/* Generic linked list. */

struct list_item_st
{
  struct list_item_st *next;
  void *data;
};

typedef struct list_item_st ListItem;

struct list_st
{
  ListItem *head;
  ListItem *tail;
};

typedef struct list_st List;

/* State. */

struct state_st
{
  char *name;
  char *super_name;
  struct state_st *super;
  List *rules;
};

typedef struct state_st State;


/* Node. */

typedef enum
{
  nVOID,
  nSTRING,
  nREGEXP,
  nINTEGER,
  nREAL,
  nSYMBOL,
  nARRAY
} NodeType;

struct node_st
{
  NodeType type;
  unsigned int refcount;
  unsigned int linenum;
  char *filename;

  union
  {
    struct
    {
      char *data;
      unsigned int len;
    } str;
    struct
    {
      char *data;
      unsigned int len;
      unsigned int flags;
      regex_t compiled;
      struct re_registers matches;
    } re;
    int integer;
    double real;
    char *sym;
    struct
    {
      struct node_st **array;
      unsigned int len;
      unsigned int allocated;
    } array;
  } u;
};

typedef struct node_st Node;

/* Cons cell. */
struct cons_st
{
  void *car;
  void *cdr;
};

typedef struct cons_st Cons;

/* Grammar types. */

typedef enum
{
  eSTRING,
  eREGEXP,
  eINTEGER,
  eREAL,
  eSYMBOL,
  eNOT,
  eAND,
  eOR,
  eFCALL,
  eASSIGN,
  eADDASSIGN,
  eSUBASSIGN,
  eMULASSIGN,
  eDIVASSIGN,
  ePOSTFIXADD,
  ePOSTFIXSUB,
  ePREFIXADD,
  ePREFIXSUB,
  eARRAYASSIGN,
  eARRAYREF,
  eQUESTCOLON,
  eMULT,
  eDIV,
  ePLUS,
  eMINUS,
  eLT,
  eGT,
  eEQ,
  eNE,
  eGE,
  eLE
} ExprType;

struct expr_st
{
  ExprType type;
  unsigned int linenum;
  char *filename;

  union
  {
    Node *node;
    struct expr_st *not;
    struct
    {
      Node *name;
      List *args;
    } fcall;
    struct
    {
      Node *sym;
      struct expr_st *expr;
    } assign;
    struct
    {
      struct expr_st *expr1;
      struct expr_st *expr2;
      struct expr_st *expr3;
    } arrayassign;
    struct
    {
      struct expr_st *expr1;
      struct expr_st *expr2;
    } arrayref;
    struct
    {
      struct expr_st *cond;
      struct expr_st *expr1;
      struct expr_st *expr2;
    } questcolon;
    struct
    {
      struct expr_st *left;
      struct expr_st *right;
    } op;
  } u;
};

typedef struct expr_st Expr;

typedef enum
{
  sRETURN,
  sDEFSUB,
  sBLOCK,
  sIF,
  sEXPR,
  sWHILE,
  sFOR
} StmtType;

struct stmt_st
{
  StmtType type;
  unsigned int linenum;
  char *filename;

  union
  {
    Expr *expr;
    struct
    {
      Node *name;
      Cons *closure;
    } defsub;
    struct
    {
      Expr *expr;
      struct stmt_st *then_stmt;
      struct stmt_st *else_stmt;
    } stmt_if;
    struct
    {
      Expr *expr;
      struct stmt_st *body;
    } stmt_while;
    struct
    {
      Expr *init;
      Expr *cond;
      Expr *incr;
      struct stmt_st *body;
    } stmt_for;
    List *block;
  } u;
};

typedef struct stmt_st Stmt;

struct environment_st
{
  struct environment_st *next;
  char *name;
  Node *val;
};

typedef struct environment_st Environment;

/* Primitive procedure. */
typedef Node *(*Primitive) ___P ((char *prim_name, List *args,
				  Environment *env, char *filename,
				  unsigned int linenum));

/* Variable definition chain. */
struct variable_definition_st
{
  struct variable_definition_st *next;
  char *sym;
  char *val;
};

typedef struct variable_definition_st VariableDef;

/* Grammar and execution warning levels. */
typedef enum
{
  WARN_LIGHT = 10,
  WARN_ALL = 100
} WarningLevel;


/*
 * Global variables.
 */

extern char *program;

extern FILE *yyin;
extern FILE *ofp;
extern char *defs_file;
extern unsigned int linenum;
extern char *yyin_name;
extern WarningLevel warning_level;
extern char *path;
extern unsigned int verbose;

/* Namespaces. */
extern StringHashPtr ns_prims;
extern StringHashPtr ns_vars;
extern StringHashPtr ns_subs;
extern StringHashPtr ns_states;

extern List *global_stmts;
extern List *start_stmts;
extern List *startrules;
extern List *namerules;

/* Void node value.  There is only nvoid instance. */
extern Node *nvoid;

extern FILE *ifp;
extern char *inbuf;
extern unsigned int data_in_buffer;
extern unsigned int bufpos;
extern int eof_seen;
extern char *current_fname;
extern unsigned int current_linenum;

extern struct re_registers *current_match;
extern char *current_match_buf;

/* Options. */

extern char *start_state_arg;
extern char *start_state;


/*
 * Prototypes for global functions.
 */

void init_primitives ();

/* Parser & lexer. */
int yyparse ();
int yylex ();
void yyerror ___P ((char *msg));

/* Generic linked list. */

/* Create a new linked list. */
List *list ();

/* Add a new element <data> to the beginning of list <list>. */
void list_prepend ___P ((List *list, void *data));

/* Add a new element <data> to the end of list <list>. */
void list_append ___P ((List *list, void *data));


/* Node manipulators. */

Node *node_alloc ___P ((NodeType type));

Node *node_copy ___P ((Node *node));

void node_reference ___P ((Node *node));

void node_free ___P ((Node *node));

void enter_system_variable ___P ((char *name, char *value));

void compile_regexp ___P ((Node *regexp));


/* Grammar constructors. */

Stmt *mk_stmt ___P ((StmtType type, void *arg1, void *arg2, void *arg3,
		     void *arg4));

Expr *mk_expr ___P ((ExprType type, void *arg1, void *arg2, void *arg3));

Cons *cons ___P ((void *car, void *cdr));

void define_state ___P ((Node *sym, Node *super, List *rules));

/* Execution. */

Node *eval_expr ___P ((Expr *expr, Environment *env));

Node *eval_statement ___P ((Stmt *stmt, Environment *env, int *return_seen));

Node *eval_statement_list ___P ((List *lst, Environment *env,
				 int *return_seen));

void process_file ___P ((char *fname));

Node *execute_state ___P ((char *name));

void load_states_file ___P ((char *name));

/*
 * Lookup state <name> and return its handle.  If the state is
 * undefined, the function tries to autoload it.
 */
State *lookup_state ___P ((char *name));

#endif /* not DEFS_H */