home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / ash02emx.zip / nodes.c.pat < prev    next >
Text File  |  1997-12-25  |  5KB  |  180 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  *
  36.  *    @(#)nodes.c.pat    5.2 (Berkeley) 3/8/91
  37.  *
  38.  *    /b/source/CVS/src/bin/sh/nodes.c.pat,v 1.3 1993/03/23 00:28:55 cgd Exp
  39.  */
  40.  
  41. /*
  42.  * Routine for dealing with parsed shell commands.
  43.  */
  44.  
  45. #include "shell.h"
  46. #include "nodes.h"
  47. #include "memalloc.h"
  48. #include "machdep.h"
  49. #include "mystring.h"
  50.  
  51.  
  52. int funcblocksize;        /* size of structures in function */
  53. int funcstringsize;        /* size of strings in node */
  54. #ifdef __STDC__
  55. pointer funcblock;        /* block to allocate function from */
  56. #else
  57. char *funcblock;        /* block to allocate function from */
  58. #endif
  59. char *funcstring;        /* block to allocate strings from */
  60.  
  61. %SIZES
  62.  
  63.  
  64. #ifdef __STDC__
  65. STATIC void calcsize(union node *);
  66. STATIC void sizenodelist(struct nodelist *);
  67. STATIC union node *copynode(union node *);
  68. STATIC struct nodelist *copynodelist(struct nodelist *);
  69. STATIC char *nodesavestr(char *);
  70. #else
  71. STATIC void calcsize();
  72. STATIC void sizenodelist();
  73. STATIC union node *copynode();
  74. STATIC struct nodelist *copynodelist();
  75. STATIC char *nodesavestr();
  76. #endif
  77.  
  78.  
  79.  
  80. /*
  81.  * Make a copy of a parse tree.
  82.  */
  83.  
  84. union node *
  85. copyfunc(n)
  86.       union node *n;
  87.       {
  88.       if (n == NULL)
  89.         return NULL;
  90.       funcblocksize = 0;
  91.       funcstringsize = 0;
  92.       calcsize(n);
  93.       funcblock = ckmalloc(funcblocksize + funcstringsize);
  94.       funcstring = funcblock + funcblocksize;
  95.       return copynode(n);
  96. }
  97.  
  98.  
  99.  
  100. STATIC void
  101. calcsize(n)
  102.       union node *n;
  103.       {
  104.       %CALCSIZE
  105. }
  106.  
  107.  
  108.  
  109. STATIC void
  110. sizenodelist(lp)
  111.       struct nodelist *lp;
  112.       {
  113.       while (lp) {
  114.         funcblocksize += ALIGN(sizeof (struct nodelist));
  115.         calcsize(lp->n);
  116.         lp = lp->next;
  117.       }
  118. }
  119.  
  120.  
  121.  
  122. STATIC union node *
  123. copynode(n)
  124.       union node *n;
  125.       {
  126.       union node *new;
  127.  
  128.       %COPY
  129.       return new;
  130. }
  131.  
  132.  
  133. STATIC struct nodelist *
  134. copynodelist(lp)
  135.       struct nodelist *lp;
  136.       {
  137.       struct nodelist *start;
  138.       struct nodelist **lpp;
  139.  
  140.       lpp = &start;
  141.       while (lp) {
  142.         *lpp = funcblock;
  143.         funcblock += ALIGN(sizeof (struct nodelist));
  144.         (*lpp)->n = copynode(lp->n);
  145.         lp = lp->next;
  146.         lpp = &(*lpp)->next;
  147.       }
  148.       *lpp = NULL;
  149.       return start;
  150. }
  151.  
  152.  
  153.  
  154. STATIC char *
  155. nodesavestr(s)
  156.       char *s;
  157.       {
  158.       register char *p = s;
  159.       register char *q = funcstring;
  160.       char *rtn = funcstring;
  161.  
  162.       while (*q++ = *p++);
  163.       funcstring = q;
  164.       return rtn;
  165. }
  166.  
  167.  
  168.  
  169. /*
  170.  * Free a parse tree.
  171.  */
  172.  
  173. void
  174. freefunc(n)
  175.       union node *n;
  176.       {
  177.       if (n)
  178.         ckfree(n);
  179. }
  180.