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