home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / AWK.ZIP / AWK.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-09  |  10.2 KB  |  291 lines

  1. /**
  2.  * $Revision:   1.1  $
  3.  * $Log:   C:/AWK/AWK.H_V  $
  4.  * 
  5.  *    Rev 1.1   09 Sep 1988 18:33:48   vince
  6.  * MC 5.1 version
  7.  * 
  8.  *    Rev 1.0   09 Sep 1988 18:03:50   vince
  9.  * Original source
  10.  * 
  11.  * awk.h -- Definitions for gawk.
  12.  *
  13.  * Copyright (C) 1986 Free Software Foundation
  14.  *   Written by Paul Rubin, August 1986
  15.  *
  16.  *       Modifications by Andrew D. Estes, July 1988
  17.  */
  18.  
  19. /**
  20.  * GAWK is distributed in the hope that it will be useful, but WITHOUT ANY
  21.  * WARRANTY.  No author or distributor accepts responsibility to anyone
  22.  * for the consequences of using it or for whether it serves any
  23.  * particular purpose or works at all, unless he says so in writing.
  24.  * Refer to the GAWK General Public License for full details.
  25.  * 
  26.  * Everyone is granted permission to copy, modify and redistribute GAWK,
  27.  * but only under the conditions described in the GAWK General Public
  28.  * License.  A copy of this license is supposed to have been given to you
  29.  * along with GAWK so you can know your rights and responsibilities.  It
  30.  * should be in a file named COPYING.  Among other things, the copyright
  31.  * notice and this notice must be preserved on all copies.
  32.  * 
  33.  * In other words, go ahead and share GAWK, but don't try to stop
  34.  * anyone else from sharing it farther.  Help stamp out software hoarding!
  35.  */
  36.  
  37. #define AWKNUM  float
  38.  
  39. #include <ctype.h>
  40. #define is_identchar(c) (isalnum(c) || (c) == '_')
  41.  
  42. #include "obstack.h"
  43. #define obstack_chunk_alloc malloc
  44. #define obstack_chunk_free free
  45. #include <malloc.h>
  46. #include <stdlib.h>
  47.  
  48. typedef enum
  49. {
  50.    /* illegal entry == 0 */
  51.    Node_illegal,                       /* 0 */
  52.  
  53.    /* binary operators  lnode and rnode are the expressions to work on */
  54.    Node_pow,                           /* 1 -ADE- */
  55.    Node_times,                         /* 2 */
  56.    Node_quotient,                      /* 3 */
  57.    Node_mod,                           /* 4 */
  58.    Node_plus,                          /* 5 */
  59.    Node_minus,                         /* 6 */
  60.    Node_cond_pair,                     /* 7: conditional pair (see Node_line_range) jfw */
  61.    Node_subscript,                     /* 8 */
  62.    Node_concat,                        /* 9 */
  63.  
  64.    /* unary operators   subnode is the expression to work on */
  65.    Node_preincrement,                  /* 10 */
  66.    Node_predecrement,                  /* 11 */
  67.    Node_postincrement,                 /* 12 */
  68.    Node_postdecrement,                 /* 13 */
  69.    Node_unary_minus,                   /* 14 */
  70.    Node_field_spec,                    /* 15 */
  71.  
  72.    /* assignments   lnode is the var to assign to, rnode is the exp */
  73.    Node_assign,                        /* 16 */
  74.    Node_assign_pow,                    /* 17 -ADE- */
  75.    Node_assign_times,                  /* 18 */
  76.    Node_assign_quotient,               /* 19 */
  77.    Node_assign_mod,                    /* 20 */
  78.    Node_assign_plus,                   /* 21 */
  79.    Node_assign_minus,                  /* 22 */
  80.    Node_cond_exp,                      /* 23 lnode ? rnode->lnode : rnode->rnode -ADE- */
  81.  
  82.    /* boolean binaries   lnode and rnode are expressions */
  83.    Node_and,                           /* 24 */
  84.    Node_or,                            /* 25 */
  85.  
  86.    /* binary relationals   compares lnode and rnode */
  87.    Node_equal,                         /* 26 */
  88.    Node_notequal,                      /* 27 */
  89.    Node_less,                          /* 28 */
  90.    Node_greater,                       /* 29 */
  91.    Node_leq,                           /* 30 */
  92.    Node_geq,                           /* 31 */
  93.  
  94.    /* unary relationals   works on subnode */
  95.    Node_not,                           /* 32 */
  96.  
  97.    /* match ops (binary)   work on lnode and rnode ??? */
  98.    Node_match,                         /* 33 */
  99.    Node_nomatch,                       /* 34 */
  100.  
  101.    /* data items */
  102.    Node_string,                        /* 35 has stlen, stptr, and stref */
  103.    Node_temp_string,                   /* 36 has stlen, stptr, and stref */
  104.    Node_number,                        /* 37 has numbr */
  105.    Node_regex,                         /* 38 has compiled regexp -ADE- */
  106.  
  107.    /* program structures */
  108.    Node_rule_list,                     /* 39 lnode is a rule, rnode is rest of list */
  109.    Node_rule_node,                     /* 40 lnode is an conditional, rnode is statement */
  110.    Node_statement_list,                /* 41 lnode is a statement, rnode is more list */
  111.    Node_if_branches,                   /* 42 lnode is to run on true, rnode on false */
  112.    Node_expression_list,               /* 43 lnode is an exp, rnode is more list */
  113.  
  114.    /* keywords */
  115.    Node_K_BEGIN,                       /* 44 no stuff */
  116.    Node_K_END,                         /* 45 ditto */
  117.    Node_K_if,                          /* 46 lnode is conditonal, rnode is if_branches */
  118.    Node_K_while,                       /* 47 lnode is condtional, rnode is stuff to run */
  119.    Node_K_for,                         /* 48 lnode is for_struct, rnode is stuff to run */
  120.    Node_K_arrayfor,                    /* 49 lnode is for_struct, rnode is stuff to run */
  121.    Node_K_break,                       /* 50 no subs */
  122.    Node_K_continue,                    /* 51 no stuff */
  123.    Node_K_getline,                     /* 52 lnode is variable, rnode is redirect -ADE- */
  124.    Node_K_print,                       /* 53 lnode is exp_list, rnode is redirect */
  125.    Node_K_printf,                      /* 54 lnode is exp_list, rnode is redirect */
  126.    Node_K_next,                        /* 55 no subs */
  127.    Node_K_exit,                        /* 56 subnode is return value, or NULL */
  128.  
  129.    /* I/O redirection for print statements */
  130.    Node_redirect_output,               /* 57 subnode is where to redirect */
  131.    Node_redirect_append,               /* 58 subnode is where to redirect */
  132.    Node_redirect_pipe,                 /* 59 subnode is where to redirect */
  133.    Node_redirect_input,                /* 60 subnode is where to redirect from */
  134.  
  135.    /* Variables */
  136.    Node_var,                           /* 61 rnode is value, lnode is array stuff */
  137.    Node_var_array,                     /* 62 array is ptr to elements, asize num of eles */
  138.  
  139.    /* Builtins   subnode is explist to work on, proc is func to call */
  140.    Node_builtin,                       /* 63 */
  141.  
  142.    /*
  143.     * pattern: conditional ',' conditional ;  lnode of Node_line_range is the two conditionals (Node_cond_pair), other word (rnode
  144.     * place) is a flag indicating whether or not this range has been entered. (jfw@eddie.mit.edu) 
  145.     */
  146.    Node_line_range,                    /* 61 */
  147. } NODETYPE;
  148.  
  149. typedef struct exp_node {
  150.    NODETYPE type;
  151.    union {
  152.       struct {
  153.          struct exp_node *lptr;
  154.          union {
  155.             struct exp_node *rptr;
  156.             struct exp_node *(*pptr) ();
  157.             struct re_pattern_buffer *preg;
  158.             struct for_loop_header *hd;
  159.             struct ahash **av;
  160.             int r_ent;                 /* range entered (jfw) */
  161.          } r;
  162.       } nodep;
  163.       struct {
  164.          struct exp_node **ap;
  165.          int as;
  166.       } ar;
  167.       struct {
  168.          char *sp;
  169.          short slen, sref;
  170.       } str;
  171.       AWKNUM fltnum;
  172.    } sub;
  173. } NODE;
  174.  
  175. #define lnode   sub.nodep.lptr
  176. #define rnode   sub.nodep.r.rptr
  177.  
  178. #define subnode lnode
  179. #define proc    sub.nodep.r.pptr
  180.  
  181. #define reexp   lnode
  182. #define rereg   sub.nodep.r.preg
  183.  
  184. #define forsub  lnode
  185. #define forloop sub.nodep.r.hd
  186.  
  187. #define array   sub.ar.ap
  188. #define arrsiz  sub.ar.as
  189.  
  190. #define stptr   sub.str.sp
  191. #define stlen   sub.str.slen
  192. #define stref   sub.str.sref
  193.  
  194. #define numbr   sub.fltnum
  195.  
  196. #define var_value lnode
  197. #define var_array sub.nodep.r.av
  198.  
  199. #define condpair lnode
  200. #define triggered sub.nodep.r.r_ent
  201.  
  202. NODE *newnode(), *dupnode();
  203. NODE *node(), *snode(), *make_number(), *make_string(), *make_regex();
  204. NODE *mkrangenode();                   /* to remove the temptation to use sub.nodep.r.rptr as a boolean flag, or to call node()
  205.                                         * with a 0 and hope that it will store correctly as an int. (jfw) */
  206. NODE *tmp_string();
  207. #ifndef FAST
  208. NODE *tmp_number();
  209. #endif
  210. NODE *variable(), *append_right();
  211.  
  212. NODE *tree_eval();
  213.  
  214. struct re_pattern_buffer *make_regexp();
  215. struct re_pattern_buffer *make_regexp_n();
  216. extern NODE *Nnull_string;
  217.  
  218. #ifdef FAST
  219. double atof();
  220. NODE *strforce();
  221. #define force_number(x)         ((x)->type==Node_number ? (x)->numbr : atof((x)->stptr))
  222. #define force_string(x)         ((x)->type==Node_number ? (strforce(x)) : (x))
  223. #define tmp_node(ty)            (global_tmp=(NODE *)obstack_alloc(&temp_strings,sizeof(NODE)),global_tmp->type=ty)
  224. #define tmp_number(n)           (tmp_node(Node_number),global_tmp->numbr=(n),global_tmp)
  225. /*
  226.  * #define tmp_string(s,len)    (tmp_node(Node_temp_string),global_tmp->stref=1, \
  227.  *   global_tmp->stlen=len,global_tmp->stptr=(char *)obstack_alloc(&temp_strings,len+1), \
  228.  *   bcopy(s,global_tmp->stptr,len),global_tmp->stptr[len]='\0',global_tmp) 
  229.  */
  230. NODE *global_tmp;
  231. #else
  232. AWKNUM force_number();
  233. NODE *force_string();
  234. #endif
  235.  
  236. NODE *expression_value;
  237.  
  238. #define HASHSIZE 101
  239.  
  240. typedef struct hashnode HASHNODE;
  241. struct hashnode
  242. {
  243.    HASHNODE *next;
  244.    char *name;
  245.    int length;
  246.    NODE *value;
  247. } *variables[HASHSIZE];
  248.  
  249.  
  250. typedef struct ahash AHASH;
  251. struct ahash
  252. {
  253.    AHASH *next;
  254.    NODE *name, *symbol, *value;
  255. };
  256.  
  257.  
  258. typedef struct for_loop_header
  259. {
  260.    NODE *init;
  261.    NODE *cond;
  262.    NODE *incr;
  263. } FOR_LOOP_HEADER;
  264.  
  265. FOR_LOOP_HEADER *make_for_loop();
  266.  
  267. #define ADD_ONE_REFERENCE(s) ++(s)->stref
  268. #define FREE_ONE_REFERENCE(s) {\
  269.   if (s == Nnull_string) {\
  270.     fprintf(stderr, "Free_Nnull_string %d", (s)->stref);\
  271.   }\
  272.   if (--(s)->stref == 0) {\
  273.         free((char *)((s)->stptr));\
  274.         free((char *)s);\
  275.   }\
  276. }
  277. /* #define FREE_ONE_REFERENCE(s) {if (--(s)->stref == 0) {printf("FREE %x\n",s);free((s)->stptr);free(s);}} */
  278.  
  279. /*
  280.  * %%% VANDYS: Can you say "do it with mirrors"? Sure.  I knew you could. 
  281.  * There isn't exactly a 1:1 correspondence between the BSD routines & the 
  282.  * Sys-V ones, but it's close enough. 
  283.  */
  284. #ifndef BSD
  285. #define index strchr
  286. /* #define alloca malloc *//* MSC has alloca function -ADE- */
  287. #define bcopy(src, dest, count) memmove(dest, src, count)
  288. #define bzero(ptr, count) memset(ptr, '\0', count)
  289. #define bcmp memcmp
  290. #endif
  291.