1 /*
2 
3  *      Copyright (c) 1984, 1985, 1986 AT&T
4  *      All Rights Reserved
5 
6  *      THIS IS UNPUBLISHED PROPRIETARY SOURCE
7  *      CODE OF AT&T.
8  *      The copyright notice above does not
9  *      evidence any actual or intended
10  *      publication of such source code.
11 
12  */
13 /* @(#)linknod.c	1.1 */
14 
15 /*
16  *   LINKNOD.C
17  *
18  *   Programmer:  D. G. Korn
19  *
20  *        Owner:  D. A. Lambeth
21  *
22  *         Date:  April 17, 1980
23  *
24  *
25  *
26  *   LINKNOD (NODP, ROOT)
27  *
28  *        Link the node given by NODP into the memory tree
29  *        given by ROOT.
30  *
31  *   RMNVAL (NV)
32  *
33  *        Remove freeable space associated with the Nodval NV.
34  *
35  *
36  *
37  *   See Also:  findnod(III), gettree(III)
38  */
39 
40 #include	"name.h"
41 #include	<stdio.h>
42 #include        "flags.h"
43 
44 void	linknod();
45 void	rmnval ();
46 
47 extern struct Namaray *growaray();
48 extern struct Namnod *mak_nod();
49 extern unsigned	chkid();
50 extern void	free();
51 
52 /*
53  *   LINKNOD (NODP, ROOT)
54  *
55  *        struct Namnod *NODP;
56  *
57  *        struct Amemory *ROOT;
58  *
59  *   Link the Namnod pointed to by NODP into the memory tree
60  *   denoted by ROOT.  If ROOT contains another Namnod with
61  *   the same namid as NODP, an array is created, with the
62  *   previously inserted Namnod as its first element and NODP as
63  *   its second.  If a previously inserted node of the same namid
64  *   already denotes an array of n elements, NODP becomes the
65  *   n+1st element.
66  */
67 
68 #ifdef KSHELL
69 /* save code space */
linknod(nodp,root)70 void	linknod(nodp,root)
71 register struct Namnod *nodp;
72 register struct Amemory *root;
73 {
74 	register int i = chkid(nodp->namid);
75 	i &= root->memsize-1;
76 	nodp->namnxt = root->memhead[i];
77 	root->memhead[i] = nodp;
78 }
79 #else
linknod(nodp,root)80 void	linknod(nodp,root)
81 struct Namnod *nodp;
82 struct Amemory *root;
83 {
84 	register struct Namnod *np,*nq,**npp;
85 	struct Nodval *nv;
86 	struct Namaray *ap;
87 	int dot;
88 	char *cp = nodp->namid;
89 	int i = chkid(cp);
90 
91 	i &= root->memsize-1;
92 	nodp->namnxt = NULL;
93 	for(npp= &root->memhead[i],np= *npp;np;npp= &np->namnxt,np= *npp)
94 		if(strcmp(cp,np->namid)==0)
95 		{
96 			if (!(attest (np, ARRAY)))
97 			{
98 				nq = mak_nod(cp);
99 				nq->namnxt = np->namnxt;
100 				*npp = nq;
101 				nq->value.namflg =  np->value.namflg|ARRAY;
102 				nq->value.namval.aray = ap = growaray((struct Namaray *)NULL,0);
103 				ap->val[0] = &np->value;
104 				nq->namsz = np->namsz;
105 				np = nq;
106 			}
107 			ap = arayp (np);
108 			dot = ++ap->adot;
109 			if (dot > ap->maxi)
110 				np->value.namval.aray = ap = growaray(ap,dot);
111 			if (nv = ap->val[dot])
112 				if (freeble (nv))
113 					rmnval (unmark (nv));
114 			ap->val[dot] = &nodp->value;
115 			return;
116 		}
117 	*npp = nodp;
118 }
119 #endif	/* KSHELL */
120 
121 
122 /*
123  *   RMNVAL (NV)
124  *
125  *        struct Nodval *NV;
126  *
127  *   Remove freeable string space attached to NV, and then
128  *   free the Nodval structure itself.
129  *
130  */
131 
rmnval(nv)132 void	rmnval (nv)
133 struct Nodval *nv;
134 {
135 	register int flag = nv->namflg;
136 	register union Namval *up = &nv->namval;
137 	register char *cp;
138 
139 	if (!(flag & N_FREE))
140 	{
141 		if (flag & IN_DIR)
142 		up = up->up;
143 		if (flag & INT_GER)
144 			if ((flag & L_FLAG) && (up->lp != NULL))
145 				free ((char*)(up->lp));
146 			else if ((cp = up->cp) != NULL)
147 				free (cp);
148 	}
149 	free ((char*)nv);
150 	return;
151 }
152