xref: /original-bsd/bin/sh/nodes.c.pat (revision a3d56443)
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * %sccs.include.redist.c%
9 *
10 *	@(#)nodes.c.pat	8.1 (Berkeley) 05/31/93
11 */
12
13/*
14 * Routine for dealing with parsed shell commands.
15 */
16
17#include "shell.h"
18#include "nodes.h"
19#include "memalloc.h"
20#include "machdep.h"
21#include "mystring.h"
22
23
24int funcblocksize;		/* size of structures in function */
25int funcstringsize;		/* size of strings in node */
26#ifdef __STDC__
27pointer funcblock;		/* block to allocate function from */
28#else
29char *funcblock;		/* block to allocate function from */
30#endif
31char *funcstring;		/* block to allocate strings from */
32
33%SIZES
34
35
36#ifdef __STDC__
37STATIC void calcsize(union node *);
38STATIC void sizenodelist(struct nodelist *);
39STATIC union node *copynode(union node *);
40STATIC struct nodelist *copynodelist(struct nodelist *);
41STATIC char *nodesavestr(char *);
42#else
43STATIC void calcsize();
44STATIC void sizenodelist();
45STATIC union node *copynode();
46STATIC struct nodelist *copynodelist();
47STATIC char *nodesavestr();
48#endif
49
50
51
52/*
53 * Make a copy of a parse tree.
54 */
55
56union node *
57copyfunc(n)
58      union node *n;
59      {
60      if (n == NULL)
61	    return NULL;
62      funcblocksize = 0;
63      funcstringsize = 0;
64      calcsize(n);
65      funcblock = ckmalloc(funcblocksize + funcstringsize);
66      funcstring = funcblock + funcblocksize;
67      return copynode(n);
68}
69
70
71
72STATIC void
73calcsize(n)
74      union node *n;
75      {
76      %CALCSIZE
77}
78
79
80
81STATIC void
82sizenodelist(lp)
83      struct nodelist *lp;
84      {
85      while (lp) {
86	    funcblocksize += ALIGN(sizeof (struct nodelist));
87	    calcsize(lp->n);
88	    lp = lp->next;
89      }
90}
91
92
93
94STATIC union node *
95copynode(n)
96      union node *n;
97      {
98      union node *new;
99
100      %COPY
101      return new;
102}
103
104
105STATIC struct nodelist *
106copynodelist(lp)
107      struct nodelist *lp;
108      {
109      struct nodelist *start;
110      struct nodelist **lpp;
111
112      lpp = &start;
113      while (lp) {
114	    *lpp = funcblock;
115	    funcblock += ALIGN(sizeof (struct nodelist));
116	    (*lpp)->n = copynode(lp->n);
117	    lp = lp->next;
118	    lpp = &(*lpp)->next;
119      }
120      *lpp = NULL;
121      return start;
122}
123
124
125
126STATIC char *
127nodesavestr(s)
128      char *s;
129      {
130      register char *p = s;
131      register char *q = funcstring;
132      char *rtn = funcstring;
133
134      while (*q++ = *p++);
135      funcstring = q;
136      return rtn;
137}
138
139
140
141/*
142 * Free a parse tree.
143 */
144
145void
146freefunc(n)
147      union node *n;
148      {
149      if (n)
150	    ckfree(n);
151}
152