xref: /original-bsd/bin/sh/nodes.c.pat (revision b3c06cab)
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.2 (Berkeley) 05/04/95
11 */
12
13#include <stdlib.h>
14/*
15 * Routine for dealing with parsed shell commands.
16 */
17
18#include "shell.h"
19#include "nodes.h"
20#include "memalloc.h"
21#include "machdep.h"
22#include "mystring.h"
23
24
25int     funcblocksize;		/* size of structures in function */
26int     funcstringsize;		/* size of strings in node */
27pointer funcblock;		/* block to allocate function from */
28char   *funcstring;		/* block to allocate strings from */
29
30%SIZES
31
32
33STATIC void calcsize __P((union node *));
34STATIC void sizenodelist __P((struct nodelist *));
35STATIC union node *copynode __P((union node *));
36STATIC struct nodelist *copynodelist __P((struct nodelist *));
37STATIC char *nodesavestr __P((char *));
38
39
40
41/*
42 * Make a copy of a parse tree.
43 */
44
45union node *
46copyfunc(n)
47	union node *n;
48{
49	if (n == NULL)
50		return NULL;
51	funcblocksize = 0;
52	funcstringsize = 0;
53	calcsize(n);
54	funcblock = ckmalloc(funcblocksize + funcstringsize);
55	funcstring = funcblock + funcblocksize;
56	return copynode(n);
57}
58
59
60
61STATIC void
62calcsize(n)
63	union node *n;
64{
65	%CALCSIZE
66}
67
68
69
70STATIC void
71sizenodelist(lp)
72	struct nodelist *lp;
73{
74	while (lp) {
75		funcblocksize += ALIGN(sizeof(struct nodelist));
76		calcsize(lp->n);
77		lp = lp->next;
78	}
79}
80
81
82
83STATIC union node *
84copynode(n)
85	union node *n;
86{
87	union node *new;
88
89	%COPY
90	return new;
91}
92
93
94STATIC struct nodelist *
95copynodelist(lp)
96	struct nodelist *lp;
97{
98	struct nodelist *start;
99	struct nodelist **lpp;
100
101	lpp = &start;
102	while (lp) {
103		*lpp = funcblock;
104		funcblock += ALIGN(sizeof(struct nodelist));
105		(*lpp)->n = copynode(lp->n);
106		lp = lp->next;
107		lpp = &(*lpp)->next;
108	}
109	*lpp = NULL;
110	return start;
111}
112
113
114
115STATIC char *
116nodesavestr(s)
117	char   *s;
118{
119	register char *p = s;
120	register char *q = funcstring;
121	char   *rtn = funcstring;
122
123	while ((*q++ = *p++) != '\0')
124		continue;
125	funcstring = q;
126	return rtn;
127}
128
129
130
131/*
132 * Free a parse tree.
133 */
134
135void
136freefunc(n)
137	union node *n;
138{
139	if (n)
140		ckfree(n);
141}
142