home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NGAWK1.ZIP / AWK.H < prev    next >
C/C++ Source or Header  |  1988-07-17  |  9KB  |  280 lines

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