[BACK]Return to awk.h CVS log [TXT][DIR] Up to [local] / src / usr.bin / awk

Annotation of src/usr.bin/awk/awk.h, Revision 1.27

1.27    ! millert     1: /*     $OpenBSD: awk.h,v 1.26 2020/06/26 15:57:39 millert Exp $        */
1.1       tholo       2: /****************************************************************
1.5       kstailey    3: Copyright (C) Lucent Technologies 1997
1.1       tholo       4: All Rights Reserved
                      5:
                      6: Permission to use, copy, modify, and distribute this software and
                      7: its documentation for any purpose and without fee is hereby
                      8: granted, provided that the above copyright notice appear in all
                      9: copies and that both that the copyright notice and this
                     10: permission notice and warranty disclaimer appear in supporting
1.5       kstailey   11: documentation, and that the name Lucent Technologies or any of
                     12: its entities not be used in advertising or publicity pertaining
                     13: to distribution of the software without specific, written prior
                     14: permission.
                     15:
                     16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
                     17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
                     18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
                     19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
                     21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
                     22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
                     23: THIS SOFTWARE.
1.1       tholo      24: ****************************************************************/
                     25:
1.11      millert    26: #include <assert.h>
1.20      millert    27: #include <stdint.h>
1.21      millert    28: #include <stdbool.h>
1.23      millert    29: #if __STDC_VERSION__ <= 199901L
                     30: #define noreturn __dead
                     31: #else
                     32: #include <stdnoreturn.h>
                     33: #endif
1.11      millert    34:
1.1       tholo      35: typedef double Awkfloat;
                     36:
                     37: /* unsigned char is more trouble than it's worth */
                     38:
                     39: typedef        unsigned char uschar;
                     40:
1.20      millert    41: #define        xfree(a)        { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
                     42: /*
                     43:  * We sometimes cheat writing read-only pointers to NUL-terminate them
                     44:  * and then put back the original value
                     45:  */
                     46: #define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
1.1       tholo      47:
1.18      millert    48: #define        NN(p)   ((p) ? (p) : "(null)")  /* guaranteed non-null for DPRINTF
1.10      millert    49: */
1.1       tholo      50: #define        DEBUG
                     51: #ifdef DEBUG
1.26      millert    52: #      define  DPRINTF(...)    if (dbg) printf(__VA_ARGS__)
1.1       tholo      53: #else
1.26      millert    54: #      define  DPRINTF(...)
1.1       tholo      55: #endif
                     56:
1.21      millert    57: extern enum compile_states {
                     58:        RUNNING,
                     59:        COMPILING,
                     60:        ERROR_PRINTING
                     61: } compile_time;
                     62:
                     63: extern bool    safe;           /* false => unsafe, true => safe */
1.24      millert    64: extern bool    do_posix;       /* true if POSIXLY_CORRECT set */
1.1       tholo      65:
1.5       kstailey   66: #define        RECSIZE (8 * 1024)      /* sets limit on records, fields, etc., etc. */
                     67: extern int     recsize;        /* size of current record, orig RECSIZE */
1.1       tholo      68:
1.22      millert    69: extern char    EMPTY[];        /* this avoid -Wwritable-strings issues */
1.1       tholo      70: extern char    **FS;
                     71: extern char    **RS;
                     72: extern char    **ORS;
                     73: extern char    **OFS;
                     74: extern char    **OFMT;
                     75: extern Awkfloat *NR;
                     76: extern Awkfloat *FNR;
                     77: extern Awkfloat *NF;
                     78: extern char    **FILENAME;
                     79: extern char    **SUBSEP;
                     80: extern Awkfloat *RSTART;
                     81: extern Awkfloat *RLENGTH;
                     82:
                     83: extern char    *record;        /* points to $0 */
                     84: extern int     lineno;         /* line number in awk program */
                     85: extern int     errorflag;      /* 1 if error has occurred */
1.21      millert    86: extern bool    donefld;        /* true if record broken into fields */
                     87: extern bool    donerec;        /* true if record is valid (no fld has changed */
1.1       tholo      88: extern int     dbg;
                     89:
1.20      millert    90: extern const char *patbeg;     /* beginning of pattern matched */
1.1       tholo      91: extern int     patlen;         /* length of pattern matched.  set in b.c */
                     92:
                     93: /* Cell:  all information about a variable or constant */
                     94:
                     95: typedef struct Cell {
                     96:        uschar  ctype;          /* OCELL, OBOOL, OJUMP, etc. */
                     97:        uschar  csub;           /* CCON, CTEMP, CFLD, etc. */
                     98:        char    *nval;          /* name, for variables only */
                     99:        char    *sval;          /* string value */
                    100:        Awkfloat fval;          /* value as number */
1.15      millert   101:        int      tval;          /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
                    102:        char    *fmt;           /* CONVFMT/OFMT value used to convert from number */
1.1       tholo     103:        struct Cell *cnext;     /* ptr to next if chained */
                    104: } Cell;
                    105:
1.5       kstailey  106: typedef struct Array {         /* symbol table array */
1.1       tholo     107:        int     nelem;          /* elements in table right now */
                    108:        int     size;           /* size of tab */
                    109:        Cell    **tab;          /* hash table pointers */
                    110: } Array;
                    111:
                    112: #define        NSYMTAB 50      /* initial size of a symbol table */
                    113: extern Array   *symtab;
                    114:
                    115: extern Cell    *nrloc;         /* NR */
                    116: extern Cell    *fnrloc;        /* FNR */
1.16      millert   117: extern Cell    *fsloc;         /* FS */
1.1       tholo     118: extern Cell    *nfloc;         /* NF */
1.16      millert   119: extern Cell    *ofsloc;        /* OFS */
                    120: extern Cell    *orsloc;        /* ORS */
                    121: extern Cell    *rsloc;         /* RS */
1.1       tholo     122: extern Cell    *rstartloc;     /* RSTART */
                    123: extern Cell    *rlengthloc;    /* RLENGTH */
1.16      millert   124: extern Cell    *subseploc;     /* SUBSEP */
1.17      millert   125: extern Cell    *symtabloc;     /* SYMTAB */
1.1       tholo     126:
                    127: /* Cell.tval values: */
                    128: #define        NUM     01      /* number value is valid */
                    129: #define        STR     02      /* string value is valid */
                    130: #define DONTFREE 04    /* string space is not freeable */
                    131: #define        CON     010     /* this is a constant */
                    132: #define        ARR     020     /* this is an array */
                    133: #define        FCN     040     /* this is a function name */
                    134: #define FLD    0100    /* this is a field $1, $2, ... */
                    135: #define        REC     0200    /* this is $0 */
1.15      millert   136: #define CONVC  0400    /* string was converted from number via CONVFMT */
                    137: #define CONVO  01000   /* string was converted from number via OFMT */
1.1       tholo     138:
                    139:
                    140: /* function types */
                    141: #define        FLENGTH 1
                    142: #define        FSQRT   2
                    143: #define        FEXP    3
                    144: #define        FLOG    4
                    145: #define        FINT    5
                    146: #define        FSYSTEM 6
                    147: #define        FRAND   7
                    148: #define        FSRAND  8
                    149: #define        FSIN    9
                    150: #define        FCOS    10
                    151: #define        FATAN   11
                    152: #define        FTOUPPER 12
                    153: #define        FTOLOWER 13
                    154: #define        FFLUSH  14
1.12      pyr       155: #define FAND   15
                    156: #define FFOR   16
                    157: #define FXOR   17
                    158: #define FCOMPL 18
                    159: #define FLSHIFT        19
                    160: #define FRSHIFT        20
1.25      millert   161: #define FSYSTIME       21
                    162: #define FSTRFTIME      22
1.27    ! millert   163: #define FMKTIME        23
1.1       tholo     164:
                    165: /* Node:  parse tree is made of nodes, with Cell's at bottom */
                    166:
                    167: typedef struct Node {
                    168:        int     ntype;
                    169:        struct  Node *nnext;
                    170:        int     lineno;
                    171:        int     nobj;
1.6       millert   172:        struct  Node *narg[1];  /* variable: actual size set by calling malloc */
1.1       tholo     173: } Node;
                    174:
                    175: #define        NIL     ((Node *) 0)
                    176:
                    177: extern Node    *winner;
                    178: extern Node    *nullstat;
                    179: extern Node    *nullnode;
                    180:
                    181: /* ctypes */
                    182: #define OCELL  1
                    183: #define OBOOL  2
                    184: #define OJUMP  3
                    185:
                    186: /* Cell subtypes: csub */
                    187: #define        CFREE   7
                    188: #define CCOPY  6
                    189: #define CCON   5
                    190: #define CTEMP  4
1.18      millert   191: #define CNAME  3
1.1       tholo     192: #define CVAR   2
                    193: #define CFLD   1
                    194: #define        CUNK    0
                    195:
                    196: /* bool subtypes */
                    197: #define BTRUE  11
                    198: #define BFALSE 12
                    199:
                    200: /* jump subtypes */
                    201: #define JEXIT  21
                    202: #define JNEXT  22
                    203: #define        JBREAK  23
                    204: #define        JCONT   24
                    205: #define        JRET    25
                    206: #define        JNEXTFILE       26
                    207:
                    208: /* node types */
                    209: #define NVALUE 1
                    210: #define NSTAT  2
                    211: #define NEXPR  3
                    212:
                    213:
1.4       millert   214: extern int     pairstack[], paircnt;
1.1       tholo     215:
                    216: #define notlegal(n)    (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
                    217: #define isvalue(n)     ((n)->ntype == NVALUE)
                    218: #define isexpr(n)      ((n)->ntype == NEXPR)
                    219: #define isjump(n)      ((n)->ctype == OJUMP)
                    220: #define isexit(n)      ((n)->csub == JEXIT)
                    221: #define        isbreak(n)      ((n)->csub == JBREAK)
                    222: #define        iscont(n)       ((n)->csub == JCONT)
1.9       millert   223: #define        isnext(n)       ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
1.1       tholo     224: #define        isret(n)        ((n)->csub == JRET)
1.5       kstailey  225: #define isrec(n)       ((n)->tval & REC)
                    226: #define isfld(n)       ((n)->tval & FLD)
1.1       tholo     227: #define isstr(n)       ((n)->tval & STR)
                    228: #define isnum(n)       ((n)->tval & NUM)
                    229: #define isarr(n)       ((n)->tval & ARR)
1.5       kstailey  230: #define isfcn(n)       ((n)->tval & FCN)
1.1       tholo     231: #define istrue(n)      ((n)->csub == BTRUE)
                    232: #define istemp(n)      ((n)->csub == CTEMP)
                    233: #define        isargument(n)   ((n)->nobj == ARG)
1.5       kstailey  234: /* #define freeable(p) (!((p)->tval & DONTFREE)) */
                    235: #define freeable(p)    ( ((p)->tval & (STR|DONTFREE)) == STR )
1.1       tholo     236:
                    237: /* structures used by regular expression matching machinery, mostly b.c: */
                    238:
1.11      millert   239: #define NCHARS (256+3)         /* 256 handles 8-bit chars; 128 does 7-bit */
1.1       tholo     240:                                /* watch out in match(), etc. */
1.18      millert   241: #define        HAT     (NCHARS+2)      /* matches ^ in regular expr */
1.1       tholo     242: #define NSTATES        32
                    243:
                    244: typedef struct rrow {
1.6       millert   245:        long    ltype;  /* long avoids pointer warnings on 64-bit */
1.1       tholo     246:        union {
                    247:                int i;
                    248:                Node *np;
1.9       millert   249:                uschar *up;
1.1       tholo     250:        } lval;         /* because Al stores a pointer in it! */
1.4       millert   251:        int     *lfollow;
1.1       tholo     252: } rrow;
                    253:
                    254: typedef struct fa {
1.19      millert   255:        unsigned int    **gototab;
                    256:        uschar  *out;
1.9       millert   257:        uschar  *restr;
1.19      millert   258:        int     **posns;
                    259:        int     state_count;
1.21      millert   260:        bool    anchor;
1.1       tholo     261:        int     use;
                    262:        int     initstat;
                    263:        int     curstat;
                    264:        int     accept;
1.6       millert   265:        struct  rrow re[1];     /* variable: actual size set by calling malloc */
1.1       tholo     266: } fa;
                    267:
                    268:
                    269: #include "proto.h"