home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / awk.h < prev    next >
Text File  |  1989-04-06  |  15KB  |  539 lines

  1. /*
  2.  * awk.h -- Definitions for gawk. 
  3.  *
  4.  * Copyright (C) 1986 Free Software Foundation Written by Paul Rubin, August
  5.  * 1986 
  6.  *
  7.  * $Log:    awk.h,v $
  8.  * Revision 1.25  88/12/08  15:57:11  david
  9.  * added some #ifdef'd out debugging code
  10.  * 
  11.  * Revision 1.24  88/12/07  19:58:37  david
  12.  * changes for printing current source file in error messages
  13.  * 
  14.  * Revision 1.23  88/12/01  15:07:10  david
  15.  * changes to accomodate source line numbers in error messages
  16.  * 
  17.  * Revision 1.22  88/11/30  15:14:59  david
  18.  * FREE_ONE_REFERENCE macro merged inot do_deref()
  19.  * 
  20.  * Revision 1.21  88/11/29  15:17:01  david
  21.  * minor movement
  22.  * 
  23.  * Revision 1.20  88/11/23  21:36:00  david
  24.  * Arnold: portability addition
  25.  * 
  26.  * Revision 1.19  88/11/22  15:51:23  david
  27.  * changed order of elements in NODE decl. for better packing on sparc and 
  28.  * similar machines
  29.  * 
  30.  * Revision 1.18  88/11/22  13:45:15  david
  31.  * Arnold: changes for case-insensitive matching
  32.  * 
  33.  * Revision 1.17  88/11/15  10:15:28  david
  34.  * Arnold: move a bunch of #include's here
  35.  * 
  36.  * Revision 1.16  88/11/14  21:50:26  david
  37.  * Arnold: get sprintf() declaration right; correct STREQ macro
  38.  * 
  39.  * Revision 1.15  88/11/14  21:24:50  david
  40.  * added extern decl. for field_num
  41.  * 
  42.  * Revision 1.14  88/11/03  15:21:03  david
  43.  * extended flags defines; made force_number safe; added TEMP_FREE define
  44.  * 
  45.  * Revision 1.13  88/11/01  12:52:18  david
  46.  * allowed for vprintf code in awk5.c
  47.  * 
  48.  * Revision 1.12  88/11/01  12:07:27  david
  49.  * cleanup; additions of external declarations; added variable name to node;
  50.  * moved flags from sub.val to node proper
  51.  * 
  52.  * Revision 1.11  88/10/19  21:54:29  david
  53.  * safe_malloc to be used by obstack_alloc
  54.  * Node_val to replace other value types (to be done)
  55.  * emalloc and erealloc macros
  56.  * 
  57.  * Revision 1.10  88/10/17  19:52:50  david
  58.  * Arnold: fix cant_happen(); improve VPRINTF; purge FAST
  59.  * 
  60.  * Revision 1.9  88/10/13  22:02:47  david
  61.  * added some external declarations to make life easier
  62.  * #define VPRINTF for portable variable arg list handling
  63.  * 
  64.  * Revision 1.8  88/10/11  22:19:05  david
  65.  * added external decl.
  66.  * 
  67.  * Revision 1.7  88/06/05  22:15:40  david
  68.  * deleted level member from hashnode structure
  69.  * 
  70.  * Revision 1.6  88/06/05  22:05:25  david
  71.  * added cnt member to NODE structure (doesn't add to size, since val member 
  72.  * dominates)
  73.  * 
  74.  * Revision 1.5  88/05/31  09:29:14  david
  75.  * expunge Node_local_var
  76.  * 
  77.  * Revision 1.4  88/05/27  11:04:07  david
  78.  * changed AWKNUM to double to correspond to nawk
  79.  * 
  80.  * Revision 1.3  88/05/13  22:07:56  david
  81.  * moved some defines here from elsewhere
  82.  * 
  83.  * Revision 1.2  88/05/04  12:17:04  david
  84.  * make_for_loop() now returns a NODE *
  85.  * 
  86.  * Revision 1.1  88/04/08  15:15:25  david
  87.  * Initial revision
  88.  *  Revision 1.6  88/04/08  14:48:25  david changes from Arnold
  89.  * Robbins 
  90.  *
  91.  * Revision 1.5  88/03/23  22:17:23  david mostly delinting -- a couple of bug
  92.  * fixes 
  93.  *
  94.  * Revision 1.4  88/03/18  21:00:05  david Baseline -- hoefully all the
  95.  * functionality of the new awk added. Just debugging and tuning to do. 
  96.  *
  97.  * Revision 1.3  87/11/19  14:34:12  david added a bunch of new Node types added
  98.  * a new union entry to the expnode structure to accomodate function
  99.  * parameter names added a level variable to the symbol structure to keep
  100.  * track of function nesting level 
  101.  *
  102.  * Revision 1.2  87/10/29  21:48:32  david added Node_in_array NODETYPE 
  103.  *
  104.  * Revision 1.1  87/10/27  15:23:07  david Initial revision 
  105.  *
  106.  */
  107.  
  108. /*
  109.  * GAWK is distributed in the hope that it will be useful, but WITHOUT ANY
  110.  * WARRANTY.  No author or distributor accepts responsibility to anyone for
  111.  * the consequences of using it or for whether it serves any particular
  112.  * purpose or works at all, unless he says so in writing. Refer to the GAWK
  113.  * General Public License for full details. 
  114.  *
  115.  * Everyone is granted permission to copy, modify and redistribute GAWK, but
  116.  * only under the conditions described in the GAWK General Public License.  A
  117.  * copy of this license is supposed to have been given to you along with GAWK
  118.  * so you can know your rights and responsibilities.  It should be in a file
  119.  * named COPYING.  Among other things, the copyright notice and this notice
  120.  * must be preserved on all copies. 
  121.  *
  122.  * In other words, go ahead and share GAWK, but don't try to stop anyone else
  123.  * from sharing it farther.  Help stamp out software hoarding! 
  124.  */
  125.  
  126. #define AWKNUM    double
  127.  
  128. #include <stdio.h>
  129. #include <ctype.h>
  130. #include <setjmp.h>
  131. #include <varargs.h>
  132. #include <sys/types.h>
  133. #include <sys/stat.h>
  134. #include <errno.h>
  135.  
  136. #include "regex.h"
  137. #define is_identchar(c) (isalnum(c) || (c) == '_')
  138.  
  139. #ifdef notdef
  140. #define    free    do_free        /* for debugging */
  141. #define    malloc    do_malloc    /* for debugging */
  142. #endif
  143.  
  144. #include "obstack.h"
  145. #define obstack_chunk_alloc safe_malloc
  146. #define obstack_chunk_free free
  147. char *malloc(), *realloc();
  148. char *safe_malloc();
  149. void free();
  150.  
  151. typedef enum {
  152.     /* illegal entry == 0 */
  153.     Node_illegal,        /* 0 */
  154.  
  155.     /* binary operators  lnode and rnode are the expressions to work on */
  156.     Node_times,        /* 1 */
  157.     Node_quotient,        /* 2 */
  158.     Node_mod,        /* 3 */
  159.     Node_plus,        /* 4 */
  160.     Node_minus,        /* 5 */
  161.     Node_cond_pair,        /* 6: conditional pair (see Node_line_range)
  162.                  * jfw */
  163.     Node_subscript,        /* 7 */
  164.     Node_concat,        /* 8 */
  165.  
  166.     /* unary operators   subnode is the expression to work on */
  167.     Node_preincrement,    /* 9 */
  168.     Node_predecrement,    /* 10 */
  169.     Node_postincrement,    /* 11 */
  170.     Node_postdecrement,    /* 12 */
  171.     Node_unary_minus,    /* 13 */
  172.     Node_field_spec,    /* 14 */
  173.  
  174.     /* assignments   lnode is the var to assign to, rnode is the exp */
  175.     Node_assign,        /* 15 */
  176.     Node_assign_times,    /* 16 */
  177.     Node_assign_quotient,    /* 17 */
  178.     Node_assign_mod,    /* 18 */
  179.     Node_assign_plus,    /* 19 */
  180.     Node_assign_minus,    /* 20 */
  181.  
  182.     /* boolean binaries   lnode and rnode are expressions */
  183.     Node_and,        /* 21 */
  184.     Node_or,        /* 22 */
  185.  
  186.     /* binary relationals   compares lnode and rnode */
  187.     Node_equal,        /* 23 */
  188.     Node_notequal,        /* 24 */
  189.     Node_less,        /* 25 */
  190.     Node_greater,        /* 26 */
  191.     Node_leq,        /* 27 */
  192.     Node_geq,        /* 28 */
  193.  
  194.     /* unary relationals   works on subnode */
  195.     Node_not,        /* 29 */
  196.  
  197.     /* match ops (binary)   work on lnode and rnode ??? */
  198.     Node_match,        /* 30 */
  199.     Node_nomatch,        /* 31 */
  200.  
  201.     /* data items */
  202.     Node_string,        /* deprecated: 32 has stlen, stptr, and stref */
  203.     Node_temp_string,    /* deprecated: 33 has stlen, stptr, and stref */
  204.     Node_number,        /* deprecated: 34 has numbr */
  205.  
  206.     /* program structures */
  207.     Node_rule_list,        /* 35 lnode is a rule, rnode is rest of list */
  208.     Node_rule_node,        /* 36 lnode is an conditional, rnode is
  209.                  * statement */
  210.     Node_statement_list,    /* 37 lnode is a statement, rnode is more
  211.                  * list */
  212.     Node_if_branches,    /* 38 lnode is to run on true, rnode on false */
  213.     Node_expression_list,    /* 39 lnode is an exp, rnode is more list */
  214.     Node_param_list,    /* 40 lnode is a variable, rnode is more list */
  215.  
  216.     /* keywords */
  217.     Node_K_BEGIN,        /* 41 no stuff */
  218.     Node_K_END,        /* 42 ditto */
  219.     Node_K_if,        /* 43 lnode is conditonal, rnode is
  220.                  * if_branches */
  221.     Node_K_while,        /* 44 lnode is condtional, rnode is stuff to
  222.                  * run */
  223.     Node_K_for,        /* 45 lnode is for_struct, rnode is stuff to
  224.                  * run */
  225.     Node_K_arrayfor,    /* 46 lnode is for_struct, rnode is stuff to
  226.                  * run */
  227.     Node_K_break,        /* 47 no subs */
  228.     Node_K_continue,    /* 48 no stuff */
  229.     Node_K_print,        /* 49 lnode is exp_list, rnode is redirect */
  230.     Node_K_printf,        /* 50 lnode is exp_list, rnode is redirect */
  231.     Node_K_next,        /* 51 no subs */
  232.     Node_K_exit,        /* 52 subnode is return value, or NULL */
  233.     Node_K_do,        /* 53 lnode is conditional, rnode is stuff to
  234.                  * run */
  235.     Node_K_return,        /* 54 */
  236.     Node_K_delete,        /* 55 */
  237.  
  238.     /* I/O redirection for print statements */
  239.     Node_redirect_output,    /* 56 subnode is where to redirect */
  240.     Node_redirect_append,    /* 57 subnode is where to redirect */
  241.     Node_redirect_pipe,    /* 58 subnode is where to redirect */
  242.     Node_redirect_pipein,    /* 59 subnode is where to redirect */
  243.     Node_redirect_input,    /* 60 subnode is where to redirect */
  244.  
  245.     /* Variables */
  246.     Node_var,        /* 61 rnode is value, lnode is array stuff */
  247.     Node_var_array,        /* 62 array is ptr to elements, asize num of
  248.                  * eles */
  249.  
  250.     /* Builtins   subnode is explist to work on, proc is func to call */
  251.     Node_builtin,        /* 63 */
  252.  
  253.     /*
  254.      * pattern: conditional ',' conditional ;  lnode of Node_line_range
  255.      * is the two conditionals (Node_cond_pair), other word (rnode place)
  256.      * is a flag indicating whether or not this range has been entered.
  257.      * (jfw@eddie.mit.edu) 
  258.      */
  259.     Node_line_range,    /* 64 */
  260.  
  261.     /*
  262.      * boolean test of membership in array lnode is string-valued
  263.      * expression rnode is array name 
  264.      */
  265.     Node_in_array,        /* 65 */
  266.     Node_K_function,    /* 66 lnode is statement list, rnode is
  267.                  * func_params */
  268.     Node_func,        /* 67 lnode is param. list, rnode is
  269.                  * statement list */
  270.     Node_func_call,        /* 68 lnode is name, rnode is expression list */
  271.     Node_K_getline,        /* 69 */
  272.     Node_sub,        /* 70 */
  273.     Node_gsub,        /* 71 */
  274.     Node_K_match,        /* 72 */
  275.     Node_cond_exp,        /* 73 lnode is conditonal, rnode is
  276.                  * if_branches */
  277.     Node_exp,        /* 74 */
  278.     Node_assign_exp,    /* 75 */
  279.     Node_regex,        /* 76 */
  280.     Node_str_num,        /* deprecated: 77 both string and numeric
  281.                  * values are valid
  282.                  */
  283.     Node_val,        /* 78 node is a value - type given by bits in
  284.                  * status - to replace Node_string, Node_num,
  285.                  * Node_temp_str and Node_str_num
  286.                  */
  287.     Node_case_match,    /* 79 case independant regexp match */
  288.     Node_case_nomatch,    /* 80 case independant regexp no match */
  289. } NODETYPE;
  290.  
  291. typedef struct exp_node {
  292.     NODETYPE type;
  293.     union {
  294.         struct {
  295.             union {
  296.                 struct exp_node *lptr;
  297.                 char *param_name;
  298.             } l;
  299.             union {
  300.                 struct exp_node *rptr;
  301.                 struct exp_node *(*pptr) ();
  302.                 struct re_pattern_buffer *preg;
  303.                 struct for_loop_header *hd;
  304.                 struct ahash **av;
  305.                 int r_ent;    /* range entered (jfw) */
  306.             } r;
  307.             int number;
  308.             char *name;
  309.         } nodep;
  310.         struct {
  311.             struct exp_node **ap;
  312.             int as;
  313.         } ar;
  314.         struct {
  315.             char *sp;
  316.             AWKNUM fltnum;    /* this is here for optimal packing of
  317.                      * the structure on many machines
  318.                      */
  319.             short slen;
  320.             unsigned char sref;
  321.         } val;
  322.     } sub;
  323.     unsigned char flags;
  324. #                define    MEM    0x7
  325. #                define    MALLOC    1    /* can be free'd */
  326. #                define    TEMP    2    /* should be free'd */
  327. #                define    PERM    4    /* can't be free'd */
  328. #                define    VAL    0x18
  329. #                define    NUM    8
  330. #                define    STR    16
  331. } NODE;
  332.  
  333. #define lnode    sub.nodep.l.lptr
  334. #define rnode    sub.nodep.r.rptr
  335. #define varname    sub.nodep.name
  336. #define source_file    sub.nodep.name
  337. #define    source_line    sub.nodep.number
  338. #define    param_cnt    sub.nodep.number
  339. #define param    sub.nodep.l.param_name
  340.  
  341. #define subnode    lnode
  342. #define proc    sub.nodep.r.pptr
  343.  
  344. #define reexp    lnode
  345. #define rereg    sub.nodep.r.preg
  346.  
  347. #define forsub    lnode
  348. #define forloop    rnode->sub.nodep.r.hd
  349.  
  350. #define array    sub.ar.ap
  351. #define arrsiz    sub.ar.as
  352.  
  353. #define stptr    sub.val.sp
  354. #define stlen    sub.val.slen
  355. #define stref    sub.val.sref
  356. #define    valstat    flags
  357.  
  358. #define numbr    sub.val.fltnum
  359.  
  360. #define var_value lnode
  361. #define var_array sub.nodep.r.av
  362.  
  363. #define condpair lnode
  364. #define triggered sub.nodep.r.r_ent
  365.  
  366. NODE *newnode(), *dupnode();
  367. NODE *node(), *snode(), *make_number(), *make_string(), *make_name();
  368. NODE *make_param();
  369. NODE *mkrangenode();        /* to remove the temptation to use
  370.                  * sub.nodep.r.rptr as a boolean flag, or to
  371.                  * call node() with a 0 and hope that it will
  372.                  * store correctly as an int. (jfw) */
  373. NODE *tmp_string(), *tmp_number();
  374. NODE *variable(), *append_right();
  375.  
  376. NODE *tree_eval();
  377. NODE **get_lhs();
  378.  
  379. struct re_pattern_buffer *make_regexp();
  380.  
  381. extern NODE **stack_ptr;
  382. extern NODE *Nnull_string;
  383. extern NODE *FS_node, *NF_node, *RS_node, *NR_node;
  384. extern NODE *FILENAME_node, *OFS_node, *ORS_node, *OFMT_node;
  385. extern NODE *FNR_node, *RLENGTH_node, *RSTART_node, *SUBSEP_node;
  386.  
  387. extern struct obstack other_stack;
  388. extern NODE *deref;
  389. extern NODE **fields_arr;
  390. extern int sourceline;
  391. extern char *source;
  392.  
  393. #ifdef USG
  394. int sprintf();
  395. #else
  396. char *sprintf();
  397. #endif
  398. char *strcpy(), *strcat();
  399.  
  400. double atof();
  401. AWKNUM r_force_number();
  402. NODE *r_force_string();
  403.  
  404.  
  405. extern NODE *expression_value;
  406.  
  407. #define HASHSIZE 101
  408.  
  409. typedef struct hashnode HASHNODE;
  410. extern struct hashnode {
  411.     HASHNODE *next;
  412.     char *name;
  413.     int length;
  414.     NODE *value;
  415. };
  416.  
  417. extern struct hashnode *variables[HASHSIZE];
  418.  
  419.  
  420. typedef struct ahash AHASH;
  421. struct ahash {
  422.     AHASH *next;
  423.     NODE *name, *symbol, *value;
  424. };
  425.  
  426.  
  427. typedef struct for_loop_header {
  428.     NODE *init;
  429.     NODE *cond;
  430.     NODE *incr;
  431. } FOR_LOOP_HEADER;
  432.  
  433. NODE *make_for_loop();
  434.  
  435. /* for "for(iggy in foo) {" */
  436. struct search {
  437.     int numleft;
  438.     AHASH **arr_ptr;
  439.     AHASH *bucket;
  440.     NODE *symbol;
  441.     NODE *retval;
  442. };
  443.  
  444. struct search *assoc_scan(), *assoc_next();
  445.  
  446. #define    FREE_TEMP(n)    {\
  447.         if (!((n)->flags & MALLOC)) cant_happen();\
  448.     if ((n)->flags & TEMP) {\
  449.         deref = (n);\
  450.         do_deref();\
  451.     }\
  452. }
  453.  
  454. #ifdef USG
  455. #define index    strchr
  456. #define rindex    strrchr
  457. #define bcmp    memcmp
  458. /* nasty nasty berkelixm */
  459. #define _setjmp    setjmp
  460. #define _longjmp    longjmp
  461. #endif
  462.  
  463. #ifdef OSK
  464. #define bcmp(s1, s2, cnt) memcmp(s1,s2,cnt)
  465. #define bcopy(fr, to, cnt) memcpy(to,fr,cnt)
  466. #define bzero(addr,cnt) memset(addr,'\0',cnt)
  467. /* nasty nasty berkelixm */
  468. #define _setjmp    setjmp
  469. #define _longjmp    longjmp
  470. #endif
  471.  
  472. char *index();
  473.  
  474. /* longjmp return codes, must be nonzero */
  475. /* Continue means either for loop/while continue, or next input record */
  476. #define TAG_CONTINUE 1
  477. /* Break means either for/while break, or stop reading input */
  478. #define TAG_BREAK 2
  479. /* Return means return from a function call; leave value in ret_node */
  480. #define    TAG_RETURN 3
  481.  
  482. /*
  483.  * the loop_tag_valid variable allows continue/break-out-of-context to be
  484.  * caught and diagnosed (jfw) 
  485.  */
  486. #define PUSH_BINDING(stack, x, val) (bcopy ((char *)(x), (char *)(stack), sizeof (jmp_buf)), val++)
  487. #define RESTORE_BINDING(stack, x, val) (bcopy ((char *)(stack), (char *)(x), sizeof (jmp_buf)), val--)
  488.  
  489. /* nasty nasty SunOS-ism */
  490. #ifdef sparc
  491. #include <alloca.h>
  492. #endif
  493.  
  494. extern char *myname;
  495. void msg();
  496. void warning();
  497. void illegal_type();
  498. void fatal();
  499.  
  500. #define    cant_happen()    fatal("line %d, file: %s; bailing out", \
  501.                 __LINE__, __FILE__);
  502.  
  503. /*
  504.  * if you don't have vprintf, but you are BSD, the version defined in
  505.  * awk5.c should do the trick.  Otherwise, use this and cross your fingers.
  506.  */
  507. #if !defined(VPRINTF) && !defined(BSD)
  508. #define vfprintf(fp,fmt,arg)    _doprnt((fmt), (arg), (fp))
  509. #endif
  510.  
  511. extern int errno;
  512. extern char *sys_errlist[];
  513.  
  514. #define    emalloc(var,ty,x,str)    if ((var = (ty) malloc((unsigned)(x))) == NULL)\
  515.                     fatal("%s: %s: can't allocate memory (%s)",\
  516.                     (str), "var", sys_errlist[errno]); else
  517. #define    erealloc(var,ty,x,str)    if((var=(ty)realloc(var,(unsigned)(x)))==NULL)\
  518.                     fatal("%s: %s: can't allocate memory (%s)",\
  519.                     (str), "var", sys_errlist[errno]); else
  520. #ifdef DEBUG
  521. #define    force_number    r_force_number
  522. #define    force_string    r_force_string
  523. #else
  524. /*#define    force_number(n)    (((n)->flags & NUM) ? (n)->numbr : r_force_number(n))*/
  525. #define    force_number    r_force_number
  526. #define    force_string    r_force_string    /* work on this later */
  527. #endif
  528.  
  529. #define    STREQ(a,b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  530. #define HUGE    0x7fffffff
  531.  
  532. extern int node0_valid;
  533. extern int field_num;
  534. extern NODE **get_field();
  535. #define    WHOLELINE    (node0_valid ? fields_arr[0] : *get_field(0))
  536.  
  537. /* extern char casetable[];    /* for case independant regexp */
  538. extern int strict;
  539.