~vcs-imports/gawk/master

« back to all changes in this revision

Viewing changes to awk.h

  • Committer: Arnold D. Robbins
  • Date: 2010-07-16 09:41:09 UTC
  • Revision ID: git-v1:8c042f99cc7465c86351d21331a129111b75345d
Tags: gawk-3.0.0
Move to gawk-3.0.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 * Copyright (C) 1986, 1988, 1989, 1991-1995 the Free Software Foundation, Inc.
7
7
 * 
8
8
 * This file is part of GAWK, the GNU implementation of the
9
 
 * AWK Progamming Language.
 
9
 * AWK Programming Language.
10
10
 * 
11
11
 * GAWK is free software; you can redistribute it and/or modify
12
12
 * it under the terms of the GNU General Public License as published by
19
19
 * GNU General Public License for more details.
20
20
 * 
21
21
 * You should have received a copy of the GNU General Public License
22
 
 * along with GAWK; see the file COPYING.  If not, write to
23
 
 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
24
24
 */
25
25
 
26
26
/* ------------------------------ Includes ------------------------------ */
27
 
#include "config.h"
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#include <config.h>
 
30
#endif
28
31
 
29
32
#include <stdio.h>
30
 
#ifndef LIMITS_H_MISSING
 
33
#ifdef HAVE_LIMITS_H
31
34
#include <limits.h>
32
 
#endif
 
35
#endif /* HAVE_LIMITS_H */
33
36
#include <ctype.h>
34
37
#include <setjmp.h>
 
38
#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
 
39
#include <stdarg.h>
 
40
#else
35
41
#include <varargs.h>
 
42
#endif
 
43
#include <signal.h>
36
44
#include <time.h>
37
45
#include <errno.h>
38
 
#if !defined(errno) && !defined(MSDOS) && !defined(OS2)
 
46
#if ! defined(errno) && ! defined(MSDOS) && ! defined(OS2)
39
47
extern int errno;
40
48
#endif
41
 
#ifdef __GNU_LIBRARY__
42
 
#ifndef linux
 
49
#ifdef HAVE_SIGNUM_H
43
50
#include <signum.h>
44
51
#endif
45
 
#endif
46
52
 
47
53
/* ----------------- System dependencies (with more includes) -----------*/
48
54
 
49
 
#if !defined(VMS) || (!defined(VAXC) && !defined(__DECC))
 
55
/* This section is the messiest one in the file, not a lot that can be done */
 
56
 
 
57
#ifdef __STDC__
 
58
#define P(s)    s
 
59
#define MALLOC_ARG_T size_t
 
60
#else   /* not __STDC__ */
 
61
#define P(s)    ()
 
62
#define MALLOC_ARG_T unsigned
 
63
#define volatile
 
64
#define const
 
65
#endif  /* not __STDC__ */
 
66
 
 
67
#if ! defined(VMS) || (! defined(VAXC) && ! defined(__DECC))
50
68
#include <sys/types.h>
51
69
#include <sys/stat.h>
52
70
#else   /* VMS w/ VAXC or DECC */
53
71
#include <types.h>
54
72
#include <stat.h>
55
73
#include <file.h>       /* avoid <fcntl.h> in io.c */
56
 
#endif
57
 
 
58
 
#include <signal.h>
59
 
 
60
 
#ifdef __STDC__
61
 
#define P(s)    s
62
 
#define MALLOC_ARG_T size_t
63
 
#else
64
 
#define P(s)    ()
65
 
#define MALLOC_ARG_T unsigned
66
 
#define volatile
67
 
#define const
68
 
#endif
69
 
 
70
 
#ifndef SIGTYPE
71
 
#define SIGTYPE void
72
 
#endif
73
 
 
74
 
#ifdef SIZE_T_MISSING
75
 
typedef unsigned int size_t;
76
 
#endif
77
 
 
78
 
#ifndef SZTC
79
 
#define SZTC
80
 
#define INTC
81
 
#endif
 
74
#ifdef __DECC
 
75
/* DEC C implies DECC$SHR, which doesn't have the %g problem of VAXCRTL */
 
76
#undef GFMT_WORKAROUND
 
77
#endif
 
78
#endif  /* VMS w/ VAXC or DECC */
82
79
 
83
80
#ifdef STDC_HEADERS
84
81
#include <stdlib.h>
 
82
#else   /* not STDC_HEADERS */
 
83
#include "protos.h"
 
84
#endif  /* not STDC_HEADERS */
 
85
 
 
86
#ifdef HAVE_STRING_H
85
87
#include <string.h>
 
88
#ifdef NEED_MEMORY_H
 
89
#include <memory.h>
 
90
#endif  /* NEED_MEMORY_H */
 
91
#else   /* not HAVE_STRING_H */
 
92
#ifdef HAVE_STRINGS_H
 
93
#include <strings.h>
 
94
#endif  /* HAVE_STRINGS_H */
 
95
#endif  /* not HAVE_STRING_H */
 
96
 
86
97
#ifdef NeXT
 
98
#if __GNUC__ < 2 || __GNUC_MINOR__ < 7
87
99
#include <libc.h>
 
100
#endif
88
101
#undef atof
89
 
#else
 
102
#define getopt GNU_getopt
 
103
#define GFMT_WORKAROUND
 
104
#endif  /* NeXT */
 
105
 
90
106
#if defined(atarist) || defined(VMS)
91
107
#include <unixlib.h>
92
 
#else   /* atarist || VMS */
93
 
#if !defined(MSDOS) && !defined(_MSC_VER)
 
108
#endif  /* atarist || VMS */
 
109
 
 
110
#if HAVE_UNISTD_H
94
111
#include <unistd.h>
95
 
#endif  /* MSDOS */
96
 
#endif  /* atarist || VMS */
97
 
#endif  /* Next */
98
 
#else   /* STDC_HEADERS */
99
 
#include "protos.h"
100
 
#endif  /* STDC_HEADERS */
101
 
 
102
 
#if defined(ultrix) && !defined(Ultrix41)
103
 
extern char * getenv P((char *name));
104
 
extern double atof P((char *s));
105
 
#endif
106
 
 
107
 
#ifndef __GNUC__
108
 
#ifdef sparc
109
 
/* nasty nasty SunOS-ism */
110
 
#include <alloca.h>
111
 
#ifdef lint
112
 
extern char *alloca();
113
 
#endif
114
 
#else /* not sparc */
115
 
#if !defined(alloca) && !defined(ALLOCA_PROTO)
116
 
#if defined(_MSC_VER)
117
 
#include <malloc.h>
118
 
#else
119
 
#ifdef __hpux
120
 
void *alloca ();
121
 
#else
122
 
extern char *alloca();
123
 
#endif /* __hpux */
124
 
#endif /* _MSC_VER */
125
 
#endif
126
 
#endif /* sparc */
127
 
#endif /* __GNUC__ */
128
 
 
129
 
#ifdef HAVE_UNDERSCORE_SETJMP
130
 
/* nasty nasty berkelixm */
131
 
#define setjmp  _setjmp
132
 
#define longjmp _longjmp
133
 
#endif
134
 
 
135
 
/*
136
 
 * if you don't have vprintf, try this and cross your fingers.
137
 
 */
138
 
#if defined(VPRINTF_MISSING)
 
112
#endif  /* HAVE_UNISTD_H */
 
113
 
 
114
#ifndef HAVE_VPRINTF
 
115
/* if you don't have vprintf, try this and cross your fingers. */
 
116
#ifdef  HAVE_DOPRNT
139
117
#define vfprintf(fp,fmt,arg)    _doprnt((fmt), (arg), (fp))
140
 
#endif
 
118
#else   /* not HAVE_DOPRNT */
 
119
you
 
120
lose
 
121
#endif  /* not HAVE_DOPRNT */
 
122
#endif  /* HAVE_VPRINTF */
141
123
 
142
124
#ifdef VMS
143
 
/* some macros to redirect to code in vms/vms_misc.c */
144
 
#define exit            vms_exit
145
 
#define open            vms_open
146
 
#define strerror        vms_strerror
147
 
#define strdup          vms_strdup
148
 
extern void  exit P((int));
149
 
extern int   open P((const char *,int,...));
150
 
extern char *strerror P((int));
151
 
extern char *strdup P((const char *str));
152
 
extern int   vms_devopen P((const char *,int));
153
 
# ifndef NO_TTY_FWRITE
154
 
#define fwrite          tty_fwrite
155
 
#define fclose          tty_fclose
156
 
extern size_t fwrite P((const void *,size_t,size_t,FILE *));
157
 
extern int    fclose P((FILE *));
158
 
# endif
159
 
extern FILE *popen P((const char *,const char *));
160
 
extern int   pclose P((FILE *));
161
 
extern void vms_arg_fixup P((int *,char ***));
162
 
/* some things not in STDC_HEADERS */
163
 
extern size_t gnu_strftime P((char *,size_t,const char *,const struct tm *));
164
 
extern int unlink P((const char *));
165
 
extern int getopt P((int,char **,char *));
166
 
extern int isatty P((int));
167
 
#ifndef fileno
168
 
extern int fileno P((FILE *));
169
 
#endif
170
 
extern int close(), dup(), dup2(), fstat(), read(), stat();
171
 
extern int getpgrp P((void));
 
125
#include "vms/redirect.h"
172
126
#endif  /*VMS*/
173
127
 
 
128
#ifdef atarist
 
129
#include "atari/redirect.h"
 
130
#endif
 
131
 
174
132
#define GNU_REGEX
175
133
#ifdef GNU_REGEX
176
134
#include "regex.h"
183
141
} Regexp;
184
142
#define RESTART(rp,s)   (rp)->regs.start[0]
185
143
#define REEND(rp,s)     (rp)->regs.end[0]
186
 
#else   /* GNU_REGEX */
 
144
#define SUBPATSTART(rp,s,n)     (rp)->regs.start[n]
 
145
#define SUBPATEND(rp,s,n)       (rp)->regs.end[n]
187
146
#endif  /* GNU_REGEX */
188
147
 
189
 
#ifdef atarist
190
 
#define read _text_read /* we do not want all these CR's to mess our input */
191
 
extern int _text_read (int, char *, int);
192
 
#ifndef __MINT__
193
 
#undef NGROUPS_MAX
194
 
#endif /* __MINT__ */
195
 
#endif
196
 
 
197
 
#ifndef DEFPATH
198
 
#define DEFPATH ".:/usr/local/lib/awk:/usr/lib/awk"
199
 
#endif
200
 
 
201
 
#ifndef ENVSEP
202
 
#define ENVSEP  ':'
203
 
#endif
204
 
 
205
 
extern double double_to_int P((double d));
206
 
 
207
148
/* ------------------ Constants, Structures, Typedefs  ------------------ */
 
149
 
 
150
#ifndef AWKNUM
208
151
#define AWKNUM  double
209
 
 
210
 
typedef enum {
 
152
#endif
 
153
 
 
154
#ifndef TRUE
 
155
/* a bit hackneyed, but what the heck */
 
156
#define TRUE    1
 
157
#define FALSE   0
 
158
#endif
 
159
 
 
160
/* Figure out what '\a' really is. */
 
161
#ifdef __STDC__
 
162
#define BELL    '\a'            /* sure makes life easy, don't it? */
 
163
#else
 
164
#       if 'z' - 'a' == 25      /* ascii */
 
165
#               if 'a' != 97    /* machine is dumb enough to use mark parity */
 
166
#                       define BELL     '\207'
 
167
#               else
 
168
#                       define BELL     '\07'
 
169
#               endif
 
170
#       else
 
171
#               define BELL     '\057'
 
172
#       endif
 
173
#endif
 
174
 
 
175
typedef enum nodevals {
211
176
        /* illegal entry == 0 */
212
177
        Node_illegal,
213
178
 
223
188
        Node_exp,
224
189
 
225
190
        /* unary operators   subnode is the expression to work on */
226
 
/*10*/  Node_preincrement,
 
191
        Node_preincrement,
227
192
        Node_predecrement,
228
193
        Node_postincrement,
229
194
        Node_postdecrement,
235
200
        Node_assign_times,
236
201
        Node_assign_quotient,
237
202
        Node_assign_mod,
238
 
/*20*/  Node_assign_plus,
 
203
        Node_assign_plus,
239
204
        Node_assign_minus,
240
205
        Node_assign_exp,
241
206
 
249
214
        Node_less,
250
215
        Node_greater,
251
216
        Node_leq,
252
 
/*30*/  Node_geq,
 
217
        Node_geq,
253
218
        Node_match,
254
219
        Node_nomatch,
255
220
 
265
230
        Node_param_list,        /* lnode is a variable, rnode is more list */
266
231
 
267
232
        /* keywords */
268
 
/*40*/  Node_K_if,              /* lnode is conditonal, rnode is if_branches */
 
233
        Node_K_if,              /* lnode is conditonal, rnode is if_branches */
269
234
        Node_K_while,           /* lnode is condtional, rnode is stuff to run */
270
235
        Node_K_for,             /* lnode is for_struct, rnode is stuff to run */
271
236
        Node_K_arrayfor,        /* lnode is for_struct, rnode is stuff to run */
272
237
        Node_K_break,           /* no subs */
273
 
        Node_K_continue,        /* no stuff */
 
238
        Node_K_continue,        /* no subs */
274
239
        Node_K_print,           /* lnode is exp_list, rnode is redirect */
275
240
        Node_K_printf,          /* lnode is exp_list, rnode is redirect */
276
241
        Node_K_next,            /* no subs */
277
242
        Node_K_exit,            /* subnode is return value, or NULL */
278
 
/*50*/  Node_K_do,              /* lnode is conditional, rnode stuff to run */
279
 
        Node_K_return,
280
 
        Node_K_delete,
281
 
        Node_K_getline,
 
243
        Node_K_do,              /* lnode is conditional, rnode stuff to run */
 
244
        Node_K_return,          /* lnode is return value */
 
245
        Node_K_delete,          /* lnode is array, rnode is subscript */
 
246
        Node_K_getline,         /* lnode is opt var, rnode is redirection */
282
247
        Node_K_function,        /* lnode is statement list, rnode is params */
 
248
        Node_K_nextfile,        /* no subs */
283
249
 
284
250
        /* I/O redirection for print statements */
285
251
        Node_redirect_output,   /* subnode is where to redirect */
289
255
        Node_redirect_input,    /* subnode is where to redirect */
290
256
 
291
257
        /* Variables */
292
 
/*60*/  Node_var,               /* rnode is value, lnode is array stuff */
293
 
        Node_var_array,         /* array is ptr to elements, asize num of
294
 
                                 * eles */
 
258
        Node_var,               /* rnode is value, lnode is array stuff */
 
259
        Node_var_array,         /* array is ptr to elements, asize num of eles */
295
260
        Node_val,               /* node is a value - type in flags */
296
261
 
297
262
        /* Builtins   subnode is explist to work on, proc is func to call */
314
279
        Node_func_call,         /* lnode is name, rnode is argument list */
315
280
 
316
281
        Node_cond_exp,          /* lnode is conditonal, rnode is if_branches */
317
 
        Node_regex,
318
 
/*70*/  Node_hashnode,
319
 
        Node_ahash,
320
 
        Node_NF,
 
282
        Node_regex,             /* a regexp, text, compiled, flags, etc */
 
283
        Node_hashnode,          /* an identifier in the symbol table */
 
284
        Node_ahash,             /* an array element */
 
285
        Node_NF,                /* variables recognized in the grammar */
321
286
        Node_NR,
322
287
        Node_FNR,
323
288
        Node_FS,
327
292
        Node_OFS,
328
293
        Node_ORS,
329
294
        Node_OFMT,
330
 
        Node_CONVFMT,
331
 
        Node_K_nextfile
 
295
        Node_CONVFMT
332
296
} NODETYPE;
333
297
 
334
298
/*
345
309
                        } l;
346
310
                        union {
347
311
                                struct exp_node *rptr;
348
 
                                struct exp_node *(*pptr) ();
 
312
                                struct exp_node *(*pptr)();
349
313
                                Regexp *preg;
350
314
                                struct for_loop_header *hd;
351
315
                                struct exp_node **av;
358
322
                        } x;
359
323
                        short number;
360
324
                        unsigned char reflags;
361
 
#                       define  CASE    1
362
 
#                       define  CONST   2
363
 
#                       define  FS_DFLT 4
 
325
#                               define  CASE    1
 
326
#                               define  CONST   2
 
327
#                               define  FS_DFLT 4
364
328
                } nodep;
365
329
                struct {
366
330
                        AWKNUM fltnum;  /* this is here for optimal packing of
368
332
                                         */
369
333
                        char *sp;
370
334
                        size_t slen;
371
 
                        unsigned char sref;
 
335
                        long sref;
372
336
                        int idx;
373
337
                } val;
374
338
                struct {
392
356
        } sub;
393
357
        NODETYPE type;
394
358
        unsigned short flags;
395
 
#                       define  MALLOC  1       /* can be free'd */
396
 
#                       define  TEMP    2       /* should be free'd */
397
 
#                       define  PERM    4       /* can't be free'd */
398
 
#                       define  STRING  8       /* assigned as string */
399
 
#                       define  STR     16      /* string value is current */
400
 
#                       define  NUM     32      /* numeric value is current */
401
 
#                       define  NUMBER  64      /* assigned as number */
402
 
#                       define  MAYBE_NUM 128   /* user input:  if NUMERIC then
 
359
#               define  MALLOC  1       /* can be free'd */
 
360
#               define  TEMP    2       /* should be free'd */
 
361
#               define  PERM    4       /* can't be free'd */
 
362
#               define  STRING  8       /* assigned as string */
 
363
#               define  STR     16      /* string value is current */
 
364
#               define  NUM     32      /* numeric value is current */
 
365
#               define  NUMBER  64      /* assigned as number */
 
366
#               define  MAYBE_NUM 128   /* user input: if NUMERIC then
403
367
                                                 * a NUMBER */
404
 
#                       define  ARRAYMAXED 256  /* array is at max size */
405
 
        char *vname;    /* variable's name */
 
368
#               define  ARRAYMAXED 256  /* array is at max size */
 
369
#               define  SCALAR     512  /* used as scalar, can't be array */
 
370
        char *vname;    /* variable's name */
406
371
} NODE;
407
372
 
408
373
#define lnode   sub.nodep.l.lptr
440
405
#define condpair lnode
441
406
#define triggered sub.nodep.r.r_ent
442
407
 
443
 
#ifdef DONTDEF
444
 
int primes[] = {31, 61, 127, 257, 509, 1021, 2053, 4099, 8191, 16381};
445
 
#endif
446
 
 
 
408
/* a regular for loop */
447
409
typedef struct for_loop_header {
448
410
        NODE *init;
449
411
        NODE *cond;
460
422
 
461
423
/* for faster input, bypass stdio */
462
424
typedef struct iobuf {
 
425
        const char *name;
463
426
        int fd;
464
427
        char *buf;
465
428
        char *off;
468
431
        int cnt;
469
432
        long secsiz;
470
433
        int flag;
471
 
#       define          IOP_IS_TTY      1
472
 
#       define          IOP_IS_INTERNAL 2
473
 
#       define          IOP_NO_FREE     4
 
434
#               define  IOP_IS_TTY      1
 
435
#               define  IOP_IS_INTERNAL 2
 
436
#               define  IOP_NO_FREE     4
474
437
} IOBUF;
475
438
 
476
439
typedef void (*Func_ptr)();
477
440
 
478
 
/*
479
 
 * structure used to dynamically maintain a linked-list of open files/pipes
480
 
 */
 
441
/* structure used to dynamically maintain a linked-list of open files/pipes */
481
442
struct redirect {
482
443
        unsigned int flag;
483
 
#               define          RED_FILE        1
484
 
#               define          RED_PIPE        2
485
 
#               define          RED_READ        4
486
 
#               define          RED_WRITE       8
487
 
#               define          RED_APPEND      16
488
 
#               define          RED_NOBUF       32
489
 
#               define          RED_USED        64
490
 
#               define          RED_EOF         128
 
444
#               define  RED_FILE        1
 
445
#               define  RED_PIPE        2
 
446
#               define  RED_READ        4
 
447
#               define  RED_WRITE       8
 
448
#               define  RED_APPEND      16
 
449
#               define  RED_NOBUF       32
 
450
#               define  RED_USED        64      /* closed temporarily to reuse fd */
 
451
#               define  RED_EOF         128
491
452
        char *value;
492
453
        FILE *fp;
 
454
        FILE *ifp;      /* input fp, needed for PIPES_SIMULATED */
493
455
        IOBUF *iop;
494
456
        int pid;
495
457
        int status;
499
461
 
500
462
/* structure for our source, either a command line string or a source file */
501
463
struct src {
502
 
        enum srctype { CMDLINE = 1, SOURCEFILE } stype;
503
 
        char *val;
 
464
       enum srctype { CMDLINE = 1, SOURCEFILE } stype;
 
465
       char *val;
504
466
};
505
467
 
506
468
/* longjmp return codes, must be nonzero */
512
474
#define TAG_RETURN 3
513
475
 
514
476
#ifndef INT_MAX
515
 
#define INT_MAX (~(1 << (sizeof (int) * 8 - 1)))
 
477
#define INT_MAX ((int)(~(1 << (sizeof (int) * 8 - 1))))
516
478
#endif
517
479
#ifndef LONG_MAX
518
 
#define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
 
480
#define LONG_MAX ((long)(~(1L << (sizeof (long) * 8 - 1))))
519
481
#endif
520
482
#ifndef ULONG_MAX
521
483
#define ULONG_MAX (~(unsigned long)0)
522
484
#endif
523
485
#ifndef LONG_MIN
524
 
#define LONG_MIN (-LONG_MAX - 1)
 
486
#define LONG_MIN ((long)(-LONG_MAX - 1L))
525
487
#endif
526
 
#define HUGE    INT_MAX 
 
488
#define HUGE    LONG_MAX 
527
489
 
528
490
/* -------------------------- External variables -------------------------- */
529
491
/* gawk builtin variables */
531
493
extern long NR;
532
494
extern long FNR;
533
495
extern int IGNORECASE;
534
 
extern char *RS;
 
496
extern int RS_is_null;
535
497
extern char *OFS;
536
498
extern int OFSlen;
537
499
extern char *ORS;
540
502
extern char *CONVFMT;
541
503
extern int CONVFMTidx;
542
504
extern int OFMTidx;
543
 
extern NODE *FS_node, *NF_node, *RS_node, *NR_node;
544
 
extern NODE *FILENAME_node, *OFS_node, *ORS_node, *OFMT_node;
545
 
extern NODE *CONVFMT_node;
546
 
extern NODE *FNR_node, *RLENGTH_node, *RSTART_node, *SUBSEP_node;
547
 
extern NODE *IGNORECASE_node;
548
 
extern NODE *FIELDWIDTHS_node;
549
 
 
 
505
extern NODE *CONVFMT_node, *FIELDWIDTHS_node, *FILENAME_node;
 
506
extern NODE *FNR_node, *FS_node, *IGNORECASE_node, *NF_node;
 
507
extern NODE *NR_node, *OFMT_node, *OFS_node, *ORS_node, *RLENGTH_node;
 
508
extern NODE *RSTART_node, *RS_node, *RT_node, *SUBSEP_node; 
550
509
extern NODE **stack_ptr;
551
510
extern NODE *Nnull_string;
552
511
extern NODE **fields_arr;
554
513
extern char *source;
555
514
extern NODE *expression_value;
556
515
 
 
516
#if __GNUC__ < 2
557
517
extern NODE *_t;        /* used as temporary in tree_eval */
558
 
 
559
 
extern const char *myname;
 
518
#endif
560
519
 
561
520
extern NODE *nextfree;
562
521
extern int field0_valid;
563
 
extern int do_unix;
 
522
extern int do_traditional;
564
523
extern int do_posix;
565
524
extern int do_lint;
 
525
extern int do_lint_old;
 
526
extern int do_intervals;
566
527
extern int in_begin_rule;
567
528
extern int in_end_rule;
568
529
 
 
530
extern const char *myname;
 
531
 
 
532
extern char quote;
 
533
extern char *defpath;
 
534
extern char envsep;
 
535
 
 
536
extern char casetable[];        /* for case-independent regexp matching */
 
537
 
569
538
/* ------------------------- Pseudo-functions ------------------------- */
570
539
 
571
540
#define is_identchar(c) (isalnum(c) || (c) == '_')
572
541
 
573
 
 
574
 
#ifndef MPROF
 
542
#ifdef MPROF
 
543
#define getnode(n)      emalloc(n, NODE *, sizeof(NODE), "getnode")
 
544
#ifndef DEBUG
 
545
#define freenode(n)     free(n)
 
546
#endif
 
547
#else   /* not MPROF */
575
548
#define getnode(n)      if (nextfree) n = nextfree, nextfree = nextfree->nextp;\
576
549
                        else n = more_nodes()
577
 
#define freenode(n)     ((n)->nextp = nextfree, nextfree = (n))
578
 
#else
579
 
#define getnode(n)      emalloc(n, NODE *, sizeof(NODE), "getnode")
580
 
#define freenode(n)     free(n)
 
550
#ifndef DEBUG
 
551
#define freenode(n)     ((n)->flags &= ~SCALAR, (n)->nextp = nextfree, nextfree = (n))
581
552
#endif
 
553
#endif  /* not MPROF */
582
554
 
583
555
#ifdef DEBUG
584
 
#define tree_eval(t)    r_tree_eval(t)
 
556
#define tree_eval(t)    r_tree_eval(t, FALSE)
 
557
#define m_tree_eval(t, iscond)  r_tree_eval(t, iscond)
585
558
#define get_lhs(p, a)   r_get_lhs((p), (a))
586
559
#undef freenode
587
560
#else
588
561
#define get_lhs(p, a)   ((p)->type == Node_var ? (&(p)->var_value) : \
589
562
                        r_get_lhs((p), (a)))
590
 
#define tree_eval(t)    (_t = (t),_t == NULL ? Nnull_string : \
591
 
                        (_t->type == Node_param_list ? r_tree_eval(_t) : \
 
563
#define tree_eval(t)    m_tree_eval(t, FALSE)
 
564
#if __GNUC__ >= 2
 
565
#define m_tree_eval(t, iscond) \
 
566
                        ({NODE * _t = (t);                 \
 
567
                           if (_t == NULL)                 \
 
568
                               _t = Nnull_string;          \
 
569
                           else {                          \
 
570
                               switch(_t->type) {          \
 
571
                               case Node_val:              \
 
572
                                   break;                  \
 
573
                               case Node_var:              \
 
574
                                   _t = _t->var_value;     \
 
575
                                   break;                  \
 
576
                               default:                    \
 
577
                                   _t = r_tree_eval(_t, iscond);\
 
578
                                   break;                  \
 
579
                               }                           \
 
580
                           }                               \
 
581
                           _t;})
 
582
#else
 
583
#define m_tree_eval(t, iscond)  (_t = (t), _t == NULL ? Nnull_string : \
 
584
                        (_t->type == Node_param_list ? \
 
585
                          r_tree_eval(_t, iscond) : \
592
586
                        (_t->type == Node_val ? _t : \
593
587
                        (_t->type == Node_var ? _t->var_value : \
594
 
                        r_tree_eval(_t)))))
595
 
#endif
 
588
                          r_tree_eval(_t, iscond)))))
 
589
#endif /* __GNUC__ */
 
590
#endif /* not DEBUG */
596
591
 
597
592
#define make_number(x)  mk_number((x), (unsigned int)(MALLOC|NUM|NUMBER))
598
593
#define tmp_number(x)   mk_number((x), (unsigned int)(MALLOC|TEMP|NUM|NUMBER))
599
594
 
600
 
#define free_temp(n)    do {if ((n)->flags&TEMP) { unref(n); }} while (0)
601
 
#define make_string(s,l)        make_str_node((s), SZTC (l),0)
 
595
#define free_temp(n)    do { if ((n)->flags&TEMP) { unref(n); }} while (FALSE)
 
596
#define make_string(s, l)       make_str_node((s), (size_t) (l), FALSE)
602
597
#define         SCAN                    1
603
598
#define         ALREADY_MALLOCED        2
604
599
 
605
600
#define cant_happen()   fatal("internal error line %d, file: %s", \
606
601
                                __LINE__, __FILE__);
607
602
 
608
 
#if defined(__STDC__) && !defined(NO_TOKEN_PASTING)
609
 
#define emalloc(var,ty,x,str)   (void)((var=(ty)malloc((MALLOC_ARG_T)(x))) ||\
610
 
                                 (fatal("%s: %s: can't allocate memory (%s)",\
611
 
                                        (str), #var, strerror(errno)),0))
612
 
#define erealloc(var,ty,x,str)  (void)((var=(ty)realloc((char *)var,\
613
 
                                                  (MALLOC_ARG_T)(x))) ||\
614
 
                                 (fatal("%s: %s: can't allocate memory (%s)",\
615
 
                                        (str), #var, strerror(errno)),0))
616
 
#else /* __STDC__ */
617
 
#define emalloc(var,ty,x,str)   (void)((var=(ty)malloc((MALLOC_ARG_T)(x))) ||\
618
 
                                 (fatal("%s: %s: can't allocate memory (%s)",\
619
 
                                        (str), "var", strerror(errno)),0))
620
 
#define erealloc(var,ty,x,str)  (void)((var=(ty)realloc((char *)var,\
621
 
                                                  (MALLOC_ARG_T)(x))) ||\
622
 
                                 (fatal("%s: %s: can't allocate memory (%s)",\
623
 
                                        (str), "var", strerror(errno)),0))
624
 
#endif /* __STDC__ */
 
603
#ifdef HAVE_STRINGIZE
 
604
#define emalloc(var,ty,x,str)   (void)((var=(ty)malloc((MALLOC_ARG_T)(x))) ||\
 
605
                                 (fatal("%s: %s: can't allocate memory (%s)",\
 
606
                                        (str), #var, strerror(errno)),0))
 
607
#define erealloc(var,ty,x,str)  (void)((var=(ty)realloc((char *)var,\
 
608
                                                  (MALLOC_ARG_T)(x))) ||\
 
609
                                 (fatal("%s: %s: can't allocate memory (%s)",\
 
610
                                        (str), #var, strerror(errno)),0))
 
611
#else /* HAVE_STRINGIZE */
 
612
#define emalloc(var,ty,x,str)   (void)((var=(ty)malloc((MALLOC_ARG_T)(x))) ||\
 
613
                                 (fatal("%s: %s: can't allocate memory (%s)",\
 
614
                                        (str), "var", strerror(errno)),0))
 
615
#define erealloc(var,ty,x,str)  (void)((var=(ty)realloc((char *)var,\
 
616
                                                  (MALLOC_ARG_T)(x))) ||\
 
617
                                 (fatal("%s: %s: can't allocate memory (%s)",\
 
618
                                        (str), "var", strerror(errno)),0))
 
619
#endif /* HAVE_STRINGIZE */
625
620
 
626
621
#ifdef DEBUG
627
622
#define force_number    r_force_number
630
625
#ifdef lint
631
626
extern AWKNUM force_number();
632
627
#endif
 
628
#if __GNUC__ >= 2
 
629
#define force_number(n) ({NODE *_tn = (n);\
 
630
                        (_tn->flags & NUM) ?_tn->numbr : r_force_number(_tn);})
 
631
#define force_string(s) ({NODE *_ts = (s);\
 
632
                          ((_ts->flags & STR) && \
 
633
                           (_ts->stfmt == -1 || _ts->stfmt == CONVFMTidx)) ?\
 
634
                          _ts : r_force_string(_ts);})
 
635
#else
633
636
#ifdef MSDOS
634
637
extern double _msc51bug;
635
 
#define force_number(n) (_msc51bug=(_t = (n),(_t->flags & NUM) ? _t->numbr : r_force_number(_t)))
 
638
#define force_number(n) (_msc51bug=(_t = (n),\
 
639
                          (_t->flags & NUM) ? _t->numbr : r_force_number(_t)))
636
640
#else /* not MSDOS */
637
 
#define force_number(n) (_t = (n),(_t->flags & NUM) ? _t->numbr : r_force_number(_t))
638
 
#endif /* MSDOS */
639
 
#define force_string(s) (_t = (s),((_t->flags & STR) && (_t->stfmt == -1 || _t->stfmt == CONVFMTidx))? _t : r_force_string(_t))
 
641
#define force_number(n) (_t = (n),\
 
642
                         (_t->flags & NUM) ? _t->numbr : r_force_number(_t))
 
643
#endif /* not MSDOS */
 
644
#define force_string(s) (_t = (s),((_t->flags & STR) && \
 
645
                                   (_t->stfmt == -1 || \
 
646
                                    _t->stfmt == CONVFMTidx))? \
 
647
                         _t : r_force_string(_t))
 
648
#endif /* not __GNUC__ */
640
649
#endif /* not DEBUG */
641
650
 
642
651
#define STREQ(a,b)      (*(a) == *(b) && strcmp((a), (b)) == 0)
643
 
#define STREQN(a,b,n)   ((n)&& *(a)== *(b) && strncmp((a), (b), SZTC (n)) == 0)
 
652
#define STREQN(a,b,n)   ((n) && *(a)== *(b) && \
 
653
                         strncmp((a), (b), (size_t) (n)) == 0)
644
654
 
645
655
/* ------------- Function prototypes or defs (as appropriate) ------------- */
646
656
 
653
663
extern void do_delete P((NODE *symbol, NODE *tree));
654
664
extern void assoc_scan P((NODE *symbol, struct search *lookat));
655
665
extern void assoc_next P((struct search *lookat));
656
 
/* awk.tab.c */
 
666
/* awktab.c */
657
667
extern char *tokexpand P((void));
658
668
extern char nextc P((void));
659
669
extern NODE *node P((NODE *left, NODETYPE op, NODE *right));
660
670
extern NODE *install P((char *name, NODE *value));
661
671
extern NODE *lookup P((const char *name));
662
 
extern NODE *variable P((char *name, int can_free));
 
672
extern NODE *variable P((char *name, int can_free, NODETYPE type));
663
673
extern int yyparse P((void));
664
674
/* builtin.c */
 
675
extern double double_to_int P((double d));
665
676
extern NODE *do_exp P((NODE *tree));
 
677
extern NODE *do_fflush P((NODE *tree));
666
678
extern NODE *do_index P((NODE *tree));
667
679
extern NODE *do_int P((NODE *tree));
668
680
extern NODE *do_length P((NODE *tree));
686
698
extern NODE *do_match P((NODE *tree));
687
699
extern NODE *do_gsub P((NODE *tree));
688
700
extern NODE *do_sub P((NODE *tree));
 
701
extern NODE *do_gensub P((NODE *tree));
689
702
/* eval.c */
690
703
extern int interpret P((NODE *volatile tree));
691
 
extern NODE *r_tree_eval P((NODE *tree));
 
704
extern NODE *r_tree_eval P((NODE *tree, int iscond));
692
705
extern int cmp_nodes P((NODE *t1, NODE *t2));
693
706
extern NODE **r_get_lhs P((NODE *ptr, Func_ptr *assign));
694
707
extern void set_IGNORECASE P((void));
701
714
extern void set_record P((char *buf, int cnt, int freeold));
702
715
extern void reset_record P((void));
703
716
extern void set_NF P((void));
704
 
extern NODE **get_field P((int num, Func_ptr *assign));
 
717
extern NODE **get_field P((long num, Func_ptr *assign));
705
718
extern NODE *do_split P((NODE *tree));
706
719
extern void set_FS P((void));
707
720
extern void set_FS_if_not_FIELDWIDTHS P((void));
708
721
extern void set_RS P((void));
709
722
extern void set_FIELDWIDTHS P((void));
 
723
extern int using_fieldwidths P((void));
 
724
/* gawkmisc.c */
 
725
extern char *gawk_name P((const char *filespec));
 
726
extern void os_arg_fixup P((int *argcp, char ***argvp));
 
727
extern int os_devopen P((const char *name, int flag));
 
728
extern int optimal_bufsize P((int fd, struct stat *sbuf));
 
729
extern int ispath P((const char *file));
 
730
extern int isdirpunct P((int c));
710
731
/* io.c */
711
732
extern void set_FNR P((void));
712
733
extern void set_NR P((void));
719
740
extern int pathopen P((const char *file));
720
741
extern NODE *do_getline P((NODE *tree));
721
742
extern void do_nextfile P((void));
722
 
/* iop.c */
723
 
extern int optimal_bufsize P((int fd));
724
 
extern IOBUF *iop_alloc P((int fd));
725
 
extern int get_a_record P((char **out, IOBUF *iop, int rs, int *errcode));
 
743
extern IOBUF *iop_alloc P((int fd, const char *name));
726
744
/* main.c */
727
745
extern int main P((int argc, char **argv));
728
 
extern Regexp *mk_re_parse P((char *s, int ignorecase));
729
746
extern void load_environ P((void));
730
747
extern char *arg_assign P((char *arg));
731
 
extern SIGTYPE catchsig P((int sig, int code));
 
748
extern RETSIGTYPE catchsig P((int sig, int code));
732
749
/* msg.c */
733
750
extern void err P((const char *s, const char *emsg, va_list argp));
734
751
#if _MSC_VER == 510
737
754
extern void warning P((va_list va_alist, ...));
738
755
extern void fatal P((va_list va_alist, ...));
739
756
#else
 
757
#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
 
758
extern void msg (char *mesg, ...);
 
759
extern void error (char *mesg, ...);
 
760
extern void warning (char *mesg, ...);
 
761
extern void fatal (char *mesg, ...);
 
762
#else
740
763
extern void msg ();
741
764
extern void error ();
742
765
extern void warning ();
743
766
extern void fatal ();
744
767
#endif
 
768
#endif
745
769
/* node.c */
746
770
extern AWKNUM r_force_number P((NODE *n));
747
771
extern NODE *r_force_string P((NODE *s));
764
788
extern Regexp *re_update P((NODE *t));
765
789
extern void resyntax P((int syntax));
766
790
extern void resetup P((void));
 
791
extern int avoid_dfa P((NODE *re, char *str, size_t len));      /* temporary */
767
792
 
768
 
/* strcase.c */
769
 
extern int strcasecmp P((const char *s1, const char *s2));
 
793
/* strncasecmp.c */
770
794
extern int strncasecmp P((const char *s1, const char *s2, register size_t n));
771
795
 
772
 
#ifdef atarist
 
796
#if defined(atarist)
 
797
#if defined(PIPES_SIMULATED)
773
798
/* atari/tmpnam.c */
774
799
extern char *tmpnam P((char *buf));
775
800
extern char *tempnam P((const char *path, const char *base));
776
 
#endif
777
 
 
778
 
/* Figure out what '\a' really is. */
779
 
#ifdef __STDC__
780
 
#define BELL    '\a'            /* sure makes life easy, don't it? */
781
 
#else
782
 
#       if 'z' - 'a' == 25      /* ascii */
783
 
#               if 'a' != 97    /* machine is dumb enough to use mark parity */
784
 
#                       define BELL     '\207'
785
 
#               else
786
 
#                       define BELL     '\07'
787
 
#               endif
788
 
#       else
789
 
#               define BELL     '\057'
790
 
#       endif
791
 
#endif
792
 
 
793
 
extern char casetable[];        /* for case-independent regexp matching */
 
801
#else
 
802
#include <wait.h>
 
803
#endif
 
804
#include <fcntl.h>
 
805
#define INVALID_HANDLE  (__SMALLEST_VALID_HANDLE - 1)
 
806
#else
 
807
#define INVALID_HANDLE (-1)
 
808
#endif /* atarist */
 
809
 
 
810
#ifndef STATIC
 
811
#define STATIC static
 
812
#endif
 
813
 
 
814
#ifdef C_ALLOCA
 
815
/* The __hpux check is to avoid conflicts with bison's definition of
 
816
   alloca() in awktab.c.*/
 
817
#if (defined(__STDC__) && __STDC__) || defined (__hpux)
 
818
extern void *alloca P((unsigned));
 
819
#else
 
820
extern char *alloca P((unsigned));
 
821
#endif
 
822
#endif