home *** CD-ROM | disk | FTP | other *** search
/ ftp.uni-stuttgart.de/pub/systems/acorn/ / Acorn.tar / Acorn / acornet / dev / gawk.spk / gawk-2154 / c / node < prev    next >
Text File  |  1994-01-04  |  8KB  |  431 lines

  1. /*
  2.  * node.c -- routines for node management
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993 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.     register long num = 0;
  126.  
  127. #ifdef DEBUG
  128.     if (s == NULL) cant_happen();
  129.     if (s->type != Node_val) cant_happen();
  130.     if (s->flags & STR) return s;
  131.     if (!(s->flags & NUM)) cant_happen();
  132.     if (s->stref != 0) ; /*cant_happen();*/
  133. #endif
  134.  
  135.         /* avoids floating point exception in DOS*/
  136.         if ( s->numbr <= LONG_MAX && s->numbr >= -LONG_MAX)
  137.         num = (long)s->numbr;
  138.     if ((AWKNUM) num == s->numbr) {    /* integral value */
  139.         if (num < NVAL && num >= 0) {
  140.             sp = (char *) values[num];
  141.             s->stlen = 1;
  142.         } else {
  143.             (void) sprintf(sp, "%ld", num);
  144.             s->stlen = strlen(sp);
  145.         }
  146.         s->stfmt = -1;
  147.     } else {
  148.         NUMTOSTR(sp, CONVFMT, s->numbr);
  149.         s->stlen = strlen(sp);
  150.         s->stfmt = (char)CONVFMTidx;
  151.     }
  152.     s->stref = 1;
  153.     emalloc(s->stptr, char *, s->stlen + 2, "force_string");
  154.     memcpy(s->stptr, sp, s->stlen+1);
  155.     s->flags |= STR;
  156.     return s;
  157. }
  158.  
  159. /*
  160.  * Duplicate a node.  (For strings, "duplicate" means crank up the
  161.  * reference count.)
  162.  */
  163. NODE *
  164. dupnode(n)
  165. NODE *n;
  166. {
  167.     register NODE *r;
  168.  
  169.     if (n->flags & TEMP) {
  170.         n->flags &= ~TEMP;
  171.         n->flags |= MALLOC;
  172.         return n;
  173.     }
  174.     if ((n->flags & (MALLOC|STR)) == (MALLOC|STR)) {
  175.         if (n->stref < 255)
  176.             n->stref++;
  177.         return n;
  178.     }
  179.     getnode(r);
  180.     *r = *n;
  181.     r->flags &= ~(PERM|TEMP);
  182.     r->flags |= MALLOC;
  183.     if (n->type == Node_val && (n->flags & STR)) {
  184.         r->stref = 1;
  185.         emalloc(r->stptr, char *, r->stlen + 2, "dupnode");
  186.         memcpy(r->stptr, n->stptr, r->stlen+1);
  187.     }
  188.     return r;
  189. }
  190.  
  191. /* this allocates a node with defined numbr */
  192. NODE *
  193. mk_number(x, flags)
  194. AWKNUM x;
  195. unsigned int flags;
  196. {
  197.     register NODE *r;
  198.  
  199.     getnode(r);
  200.     r->type = Node_val;
  201.     r->numbr = x;
  202.     r->flags = flags;
  203. #ifdef DEBUG
  204.     r->stref = 1;
  205.     r->stptr = 0;
  206.     r->stlen = 0;
  207. #endif
  208.     return r;
  209. }
  210.  
  211. /*
  212.  * Make a string node.
  213.  */
  214. NODE *
  215. make_str_node(s, len, flags)
  216. char *s;
  217. size_t len;
  218. int flags;
  219. {
  220.     register NODE *r;
  221.  
  222.     getnode(r);
  223.     r->type = Node_val;
  224.     r->flags = (STRING|STR|MALLOC);
  225.     if (flags & ALREADY_MALLOCED)
  226.         r->stptr = s;
  227.     else {
  228.         emalloc(r->stptr, char *, len + 2, s);
  229.         memcpy(r->stptr, s, len);
  230.     }
  231.     r->stptr[len] = '\0';
  232.            
  233.     if (flags & SCAN) {    /* scan for escape sequences */
  234.         char *pf;
  235.         register char *ptm;
  236.         register int c;
  237.         register char *end;
  238.  
  239.         end = &(r->stptr[len]);
  240.         for (pf = ptm = r->stptr; pf < end;) {
  241.             c = *pf++;
  242.             if (c == '\\') {
  243.                 c = parse_escape(&pf);
  244.                 if (c < 0) {
  245.                     if (do_lint)
  246.                         warning("backslash at end of string");
  247.                     c = '\\';
  248.                 }
  249.                 *ptm++ = c;
  250.             } else
  251.                 *ptm++ = c;
  252.         }
  253.         len = ptm - r->stptr;
  254.         erealloc(r->stptr, char *, len + 1, "make_str_node");
  255.         r->stptr[len] = '\0';
  256.         r->flags |= PERM;
  257.     }
  258.     r->stlen = len;
  259.     r->stref = 1;
  260.     r->stfmt = -1;
  261.  
  262.     return r;
  263. }
  264.  
  265. NODE *
  266. tmp_string(s, len)
  267. char *s;
  268. size_t len;
  269. {
  270.     register NODE *r;
  271.  
  272.     r = make_string(s, len);
  273.     r->flags |= TEMP;
  274.     return r;
  275. }
  276.  
  277.  
  278. #define NODECHUNK    100
  279.  
  280. NODE *nextfree = NULL;
  281.  
  282. NODE *
  283. more_nodes()
  284. {
  285.     register NODE *np;
  286.  
  287.     /* get more nodes and initialize list */
  288.     emalloc(nextfree, NODE *, NODECHUNK * sizeof(NODE), "newnode");
  289.     for (np = nextfree; np < &nextfree[NODECHUNK - 1]; np++)
  290.         np->nextp = np + 1;
  291.     np->nextp = NULL;
  292.     np = nextfree;
  293.     nextfree = nextfree->nextp;
  294.     return np;
  295. }
  296.  
  297. #ifdef DEBUG
  298. void
  299. freenode(it)
  300. NODE *it;
  301. {
  302. #ifdef MPROF
  303.     it->stref = 0;
  304.     free((char *) it);
  305. #else    /* not MPROF */
  306.     /* add it to head of freelist */
  307.     it->nextp = nextfree;
  308.     nextfree = it;
  309. #endif    /* not MPROF */
  310. }
  311. #endif    /* DEBUG */
  312.  
  313. void
  314. unref(tmp)
  315. register NODE *tmp;
  316. {
  317.     if (tmp == NULL)
  318.         return;
  319.     if (tmp->flags & PERM)
  320.         return;
  321.     if (tmp->flags & (MALLOC|TEMP)) {
  322.         tmp->flags &= ~TEMP;
  323.         if (tmp->flags & STR) {
  324.             if (tmp->stref > 1) {
  325.                 if (tmp->stref != 255)
  326.                     tmp->stref--;
  327.                 return;
  328.             }
  329.             free(tmp->stptr);
  330.         }
  331.         freenode(tmp);
  332.     }
  333. }
  334.  
  335. /*
  336.  * Parse a C escape sequence.  STRING_PTR points to a variable containing a
  337.  * pointer to the string to parse.  That pointer is updated past the
  338.  * characters we use.  The value of the escape sequence is returned. 
  339.  *
  340.  * A negative value means the sequence \ newline was seen, which is supposed to
  341.  * be equivalent to nothing at all. 
  342.  *
  343.  * If \ is followed by a null character, we return a negative value and leave
  344.  * the string pointer pointing at the null character. 
  345.  *
  346.  * If \ is followed by 000, we return 0 and leave the string pointer after the
  347.  * zeros.  A value of 0 does not mean end of string.  
  348.  *
  349.  * Posix doesn't allow \x.
  350.  */
  351.  
  352. int
  353. parse_escape(string_ptr)
  354. char **string_ptr;
  355. {
  356.     register int c = *(*string_ptr)++;
  357.     register int i;
  358.     register int count;
  359.  
  360.     switch (c) {
  361.     case 'a':
  362.         return BELL;
  363.     case 'b':
  364.         return '\b';
  365.     case 'f':
  366.         return '\f';
  367.     case 'n':
  368.         return '\n';
  369.     case 'r':
  370.         return '\r';
  371.     case 't':
  372.         return '\t';
  373.     case 'v':
  374.         return '\v';
  375.     case '\n':
  376.         return -2;
  377.     case 0:
  378.         (*string_ptr)--;
  379.         return -1;
  380.     case '0':
  381.     case '1':
  382.     case '2':
  383.     case '3':
  384.     case '4':
  385.     case '5':
  386.     case '6':
  387.     case '7':
  388.         i = c - '0';
  389.         count = 0;
  390.         while (++count < 3) {
  391.             if ((c = *(*string_ptr)++) >= '0' && c <= '7') {
  392.                 i *= 8;
  393.                 i += c - '0';
  394.             } else {
  395.                 (*string_ptr)--;
  396.                 break;
  397.             }
  398.         }
  399.         return i;
  400.     case 'x':
  401.         if (do_lint) {
  402.             static int didwarn;
  403.  
  404.             if (! didwarn) {
  405.                 didwarn = 1;
  406.                 warning("Posix does not allow \"\\x\" escapes");
  407.             }
  408.         }
  409.         if (do_posix)
  410.             return ('x');
  411.         i = 0;
  412.         while (1) {
  413.             if (isxdigit((c = *(*string_ptr)++))) {
  414.                 i *= 16;
  415.                 if (isdigit(c))
  416.                     i += c - '0';
  417.                 else if (isupper(c))
  418.                     i += c - 'A' + 10;
  419.                 else
  420.                     i += c - 'a' + 10;
  421.             } else {
  422.                 (*string_ptr)--;
  423.                 break;
  424.             }
  425.         }
  426.         return i;
  427.     default:
  428.         return c;
  429.     }
  430. }
  431.