home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gawk-2.15.6-base.tgz / gawk-2.15.6-base.tar / fsf / gawk / node.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  9KB  |  468 lines

  1. /*
  2.  * node.c -- routines for node management
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991-1995 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Progamming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  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.
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. extern double strtod();
  29.  
  30. AWKNUM
  31. r_force_number(n)
  32. register NODE *n;
  33. {
  34.     register char *cp;
  35.     register char *cpend;
  36.     char save;
  37.     char *ptr;
  38.     unsigned int newflags;
  39.  
  40. #ifdef DEBUG
  41.     if (n == NULL)
  42.         cant_happen();
  43.     if (n->type != Node_val)
  44.         cant_happen();
  45.     if(n->flags == 0)
  46.         cant_happen();
  47.     if (n->flags & NUM)
  48.         return n->numbr;
  49. #endif
  50.  
  51.     /* all the conditionals are an attempt to avoid the expensive strtod */
  52.  
  53.     n->numbr = 0.0;
  54.     n->flags |= NUM;
  55.  
  56.     if (n->stlen == 0)
  57.         return 0.0;
  58.  
  59.     cp = n->stptr;
  60.     if (isalpha(*cp))
  61.         return 0.0;
  62.  
  63.     cpend = cp + n->stlen;
  64.     while (cp < cpend && isspace(*cp))
  65.         cp++;
  66.     if (cp == cpend || isalpha(*cp))
  67.         return 0.0;
  68.  
  69.     if (n->flags & MAYBE_NUM) {
  70.         newflags = NUMBER;
  71.         n->flags &= ~MAYBE_NUM;
  72.     } else
  73.         newflags = 0;
  74.     if (cpend - cp == 1) {
  75.         if (isdigit(*cp)) {
  76.             n->numbr = (AWKNUM)(*cp - '0');
  77.             n->flags |= newflags;
  78.         }
  79.         return n->numbr;
  80.     }
  81.  
  82.     errno = 0;
  83.     save = *cpend;
  84.     *cpend = '\0';
  85.     n->numbr = (AWKNUM) strtod((const char *)cp, &ptr);
  86.  
  87.     /* POSIX says trailing space is OK for NUMBER */
  88.     while (isspace(*ptr))
  89.         ptr++;
  90.     *cpend = save;
  91.     /* the >= should be ==, but for SunOS 3.5 strtod() */
  92.     if (errno == 0 && ptr >= cpend)
  93.         n->flags |= newflags;
  94.     else
  95.         errno = 0;
  96.  
  97.     return n->numbr;
  98. }
  99.  
  100. /*
  101.  * the following lookup table is used as an optimization in force_string
  102.  * (more complicated) variations on this theme didn't seem to pay off, but 
  103.  * systematic testing might be in order at some point
  104.  */
  105. static const char *values[] = {
  106.     "0",
  107.     "1",
  108.     "2",
  109.     "3",
  110.     "4",
  111.     "5",
  112.     "6",
  113.     "7",
  114.     "8",
  115.     "9",
  116. };
  117. #define    NVAL    (sizeof(values)/sizeof(values[0]))
  118.  
  119. NODE *
  120. r_force_string(s)
  121. register NODE *s;
  122. {
  123.     char buf[128];
  124.     register char *sp = buf;
  125.     double val;
  126.  
  127. #ifdef DEBUG
  128.     if (s == NULL) cant_happen();
  129.     if (s->type != Node_val) cant_happen();
  130.     if ((s->flags & STR) && (s->stfmt == -1 || s->stfmt == CONVFMTidx)) return s;
  131.     if (!(s->flags & NUM)) cant_happen();
  132.     if (s->stref != 0) ; /*cant_happen();*/
  133. #endif
  134.  
  135.     /* not an integral value, or out of range */
  136.     if ((val = double_to_int(s->numbr)) != s->numbr
  137.         || val < LONG_MIN || val > LONG_MAX) {
  138. #ifdef GFMT_WORKAROUND
  139.         NODE *dummy, *r;
  140.         unsigned short oflags;
  141.         extern NODE *format_tree P((const char *, int, NODE *));
  142.         extern NODE **fmt_list;          /* declared in eval.c */
  143.  
  144.         /* create dummy node for a sole use of format_tree */
  145.         getnode(dummy);
  146.         dummy->lnode = s;
  147.         dummy->rnode = NULL;
  148.         oflags = s->flags;
  149.         s->flags |= PERM; /* prevent from freeing by format_tree() */
  150.         r = format_tree(CONVFMT, fmt_list[CONVFMTidx]->stlen, dummy);
  151.         s->flags = oflags;
  152.         s->stfmt = (char)CONVFMTidx;
  153.         s->stlen = r->stlen;
  154.         s->stptr = r->stptr;
  155.         freenode(r);        /* Do not free_temp(r)!  We want */
  156.         freenode(dummy);    /* to keep s->stptr == r->stpr.  */
  157.  
  158.         goto no_malloc;
  159. #else
  160.         /*
  161.          * no need for a "replacement" formatting by gawk,
  162.          * just use sprintf
  163.          */
  164.         sprintf(sp, CONVFMT, s->numbr);
  165.         s->stlen = strlen(sp);
  166.         s->stfmt = (char)CONVFMTidx;
  167. #endif /* GFMT_WORKAROUND */
  168.     } else {
  169.         /* integral value */
  170.             /* force conversion to long only once */
  171.         register long num = (long) val;
  172.         if (num < NVAL && num >= 0) {
  173.             sp = (char *) values[num];
  174.             s->stlen = 1;
  175.         } else {
  176.             (void) sprintf(sp, "%ld", num);
  177.             s->stlen = strlen(sp);
  178.         }
  179.         s->stfmt = -1;
  180.     }
  181.     emalloc(s->stptr, char *, s->stlen + 2, "force_string");
  182.     memcpy(s->stptr, sp, s->stlen+1);
  183. no_malloc:
  184.     s->stref = 1;
  185.     s->flags |= STR;
  186.     return s;
  187. }
  188.  
  189. /*
  190.  * Duplicate a node.  (For strings, "duplicate" means crank up the
  191.  * reference count.)
  192.  */
  193. NODE *
  194. dupnode(n)
  195. NODE *n;
  196. {
  197.     register NODE *r;
  198.  
  199.     if (n->flags & TEMP) {
  200.         n->flags &= ~TEMP;
  201.         n->flags |= MALLOC;
  202.         return n;
  203.     }
  204.     if ((n->flags & (MALLOC|STR)) == (MALLOC|STR)) {
  205.         if (n->stref < 255)
  206.             n->stref++;
  207.         return n;
  208.     }
  209.     getnode(r);
  210.     *r = *n;
  211.     r->flags &= ~(PERM|TEMP);
  212.     r->flags |= MALLOC;
  213.     if (n->type == Node_val && (n->flags & STR)) {
  214.         r->stref = 1;
  215.         emalloc(r->stptr, char *, r->stlen + 2, "dupnode");
  216.         memcpy(r->stptr, n->stptr, r->stlen);
  217.         r->stptr[r->stlen] = '\0';
  218.     }
  219.     return r;
  220. }
  221.  
  222. /* this allocates a node with defined numbr */
  223. NODE *
  224. mk_number(x, flags)
  225. AWKNUM x;
  226. unsigned int flags;
  227. {
  228.     register NODE *r;
  229.  
  230.     getnode(r);
  231.     r->type = Node_val;
  232.     r->numbr = x;
  233.     r->flags = flags;
  234. #ifdef DEBUG
  235.     r->stref = 1;
  236.     r->stptr = 0;
  237.     r->stlen = 0;
  238. #endif
  239.     return r;
  240. }
  241.  
  242. /*
  243.  * Make a string node.
  244.  */
  245. NODE *
  246. make_str_node(s, len, flags)
  247. char *s;
  248. size_t len;
  249. int flags;
  250. {
  251.     register NODE *r;
  252.  
  253.     getnode(r);
  254.     r->type = Node_val;
  255.     r->flags = (STRING|STR|MALLOC);
  256.     if (flags & ALREADY_MALLOCED)
  257.         r->stptr = s;
  258.     else {
  259.         emalloc(r->stptr, char *, len + 2, s);
  260.         memcpy(r->stptr, s, len);
  261.     }
  262.     r->stptr[len] = '\0';
  263.            
  264.     if (flags & SCAN) {    /* scan for escape sequences */
  265.         char *pf;
  266.         register char *ptm;
  267.         register int c;
  268.         register char *end;
  269.  
  270.         end = &(r->stptr[len]);
  271.         for (pf = ptm = r->stptr; pf < end;) {
  272.             c = *pf++;
  273.             if (c == '\\') {
  274.                 c = parse_escape(&pf);
  275.                 if (c < 0) {
  276.                     if (do_lint)
  277.                         warning("backslash at end of string");
  278.                     c = '\\';
  279.                 }
  280.                 *ptm++ = c;
  281.             } else
  282.                 *ptm++ = c;
  283.         }
  284.         len = ptm - r->stptr;
  285.         erealloc(r->stptr, char *, len + 1, "make_str_node");
  286.         r->stptr[len] = '\0';
  287.         r->flags |= PERM;
  288.     }
  289.     r->stlen = len;
  290.     r->stref = 1;
  291.     r->stfmt = -1;
  292.  
  293.     return r;
  294. }
  295.  
  296. NODE *
  297. tmp_string(s, len)
  298. char *s;
  299. size_t len;
  300. {
  301.     register NODE *r;
  302.  
  303.     r = make_string(s, len);
  304.     r->flags |= TEMP;
  305.     return r;
  306. }
  307.  
  308.  
  309. #define NODECHUNK    100
  310.  
  311. NODE *nextfree = NULL;
  312.  
  313. NODE *
  314. more_nodes()
  315. {
  316.     register NODE *np;
  317.  
  318.     /* get more nodes and initialize list */
  319.     emalloc(nextfree, NODE *, NODECHUNK * sizeof(NODE), "newnode");
  320.     for (np = nextfree; np < &nextfree[NODECHUNK - 1]; np++) {
  321.         np->flags = 0;
  322.         np->nextp = np + 1;
  323.     }
  324.     np->nextp = NULL;
  325.     np = nextfree;
  326.     nextfree = nextfree->nextp;
  327.     return np;
  328. }
  329.  
  330. #ifdef DEBUG
  331. void
  332. freenode(it)
  333. NODE *it;
  334. {
  335. #ifdef MPROF
  336.     it->stref = 0;
  337.     free((char *) it);
  338. #else    /* not MPROF */
  339.     /* add it to head of freelist */
  340.     it->nextp = nextfree;
  341.     nextfree = it;
  342. #endif    /* not MPROF */
  343. }
  344. #endif    /* DEBUG */
  345.  
  346. void
  347. unref(tmp)
  348. register NODE *tmp;
  349. {
  350.     if (tmp == NULL)
  351.         return;
  352.     if (tmp->flags & PERM)
  353.         return;
  354.     if (tmp->flags & (MALLOC|TEMP)) {
  355.         tmp->flags &= ~TEMP;
  356.         if (tmp->flags & STR) {
  357.             if (tmp->stref > 1) {
  358.                 if (tmp->stref != 255)
  359.                     tmp->stref--;
  360.                 return;
  361.             }
  362.             free(tmp->stptr);
  363.         }
  364.         freenode(tmp);
  365.     }
  366. }
  367.  
  368. /*
  369.  * Parse a C escape sequence.  STRING_PTR points to a variable containing a
  370.  * pointer to the string to parse.  That pointer is updated past the
  371.  * characters we use.  The value of the escape sequence is returned. 
  372.  *
  373.  * A negative value means the sequence \ newline was seen, which is supposed to
  374.  * be equivalent to nothing at all. 
  375.  *
  376.  * If \ is followed by a null character, we return a negative value and leave
  377.  * the string pointer pointing at the null character. 
  378.  *
  379.  * If \ is followed by 000, we return 0 and leave the string pointer after the
  380.  * zeros.  A value of 0 does not mean end of string.  
  381.  *
  382.  * Posix doesn't allow \x.
  383.  */
  384.  
  385. int
  386. parse_escape(string_ptr)
  387. char **string_ptr;
  388. {
  389.     register int c = *(*string_ptr)++;
  390.     register int i;
  391.     register int count;
  392.  
  393.     switch (c) {
  394.     case 'a':
  395.         return BELL;
  396.     case 'b':
  397.         return '\b';
  398.     case 'f':
  399.         return '\f';
  400.     case 'n':
  401.         return '\n';
  402.     case 'r':
  403.         return '\r';
  404.     case 't':
  405.         return '\t';
  406.     case 'v':
  407.         return '\v';
  408.     case '\n':
  409.         return -2;
  410.     case 0:
  411.         (*string_ptr)--;
  412.         return -1;
  413.     case '0':
  414.     case '1':
  415.     case '2':
  416.     case '3':
  417.     case '4':
  418.     case '5':
  419.     case '6':
  420.     case '7':
  421.         i = c - '0';
  422.         count = 0;
  423.         while (++count < 3) {
  424.             if ((c = *(*string_ptr)++) >= '0' && c <= '7') {
  425.                 i *= 8;
  426.                 i += c - '0';
  427.             } else {
  428.                 (*string_ptr)--;
  429.                 break;
  430.             }
  431.         }
  432.         return i;
  433.     case 'x':
  434.         if (do_lint) {
  435.             static int didwarn;
  436.  
  437.             if (! didwarn) {
  438.                 didwarn = 1;
  439.                 warning("Posix does not allow \"\\x\" escapes");
  440.             }
  441.         }
  442.         if (do_posix)
  443.             return ('x');
  444.         if (! isxdigit((*string_ptr)[1])) {
  445.             warning("no hex digits in \\x escape sequence");
  446.             return ('x');
  447.         }
  448.         i = 0;
  449.         while (1) {
  450.             if (isxdigit((c = *(*string_ptr)++))) {
  451.                 i *= 16;
  452.                 if (isdigit(c))
  453.                     i += c - '0';
  454.                 else if (isupper(c))
  455.                     i += c - 'A' + 10;
  456.                 else
  457.                     i += c - 'a' + 10;
  458.             } else {
  459.                 (*string_ptr)--;
  460.                 break;
  461.             }
  462.         }
  463.         return i;
  464.     default:
  465.         return c;
  466.     }
  467. }
  468.