1*35191Smarc /*
2*35191Smarc 
3*35191Smarc  *      Copyright (c) 1984, 1985, 1986 AT&T
4*35191Smarc  *      All Rights Reserved
5*35191Smarc 
6*35191Smarc  *      THIS IS UNPUBLISHED PROPRIETARY SOURCE
7*35191Smarc  *      CODE OF AT&T.
8*35191Smarc  *      The copyright notice above does not
9*35191Smarc  *      evidence any actual or intended
10*35191Smarc  *      publication of such source code.
11*35191Smarc 
12*35191Smarc  */
13*35191Smarc /* @(#)growaray.c	1.1 */
14*35191Smarc 
15*35191Smarc /*
16*35191Smarc  *   GROWARAY.C
17*35191Smarc  *
18*35191Smarc  *   Programmer:  D. G. Korn
19*35191Smarc  *
20*35191Smarc  *        Owner:  D. A. Lambeth
21*35191Smarc  *
22*35191Smarc  *         Date:  April 17, 1980
23*35191Smarc  *
24*35191Smarc  *
25*35191Smarc  *
26*35191Smarc  *   GROWARAY (ARP, MAXI)
27*35191Smarc  *
28*35191Smarc  *        Create or expand the size of an array of Namnods, ARP,
29*35191Smarc  *        such that MAXI is a legal index into ARP.
30*35191Smarc  *
31*35191Smarc  *   SETDOT (NODE, INDEX)
32*35191Smarc  *
33*35191Smarc  *        Set the current index of the array NODE to be INDEX.
34*35191Smarc  *        (library only)
35*35191Smarc  *
36*35191Smarc  *
37*35191Smarc  *
38*35191Smarc  *   See Also:  linknod(III)
39*35191Smarc  */
40*35191Smarc 
41*35191Smarc #include	"name.h"
42*35191Smarc #include        "flags.h"
43*35191Smarc 
44*35191Smarc struct Namaray *growaray();
45*35191Smarc int	arsize ();
46*35191Smarc #ifndef KSHELL
47*35191Smarc void	setdot ();
48*35191Smarc #endif	/* KSHELL */
49*35191Smarc 
50*35191Smarc #define round(a,b)	((a+b-1)&~(b-1))
51*35191Smarc extern char	*malloc();
52*35191Smarc extern char	*itos();
53*35191Smarc extern void	failed();
54*35191Smarc extern void	free();
55*35191Smarc 
56*35191Smarc /*
57*35191Smarc  *   GROWARAY (ARP, MAXI)
58*35191Smarc  *
59*35191Smarc  *        struct Namaray *ARP;
60*35191Smarc  *
61*35191Smarc  *        int MAXI;
62*35191Smarc  *
63*35191Smarc  *        Increase the size of the array of Namnods given by ARP
64*35191Smarc  *        so that MAXI is a legal index.  If ARP is NULL, an array
65*35191Smarc  *        of the required size is allocated.  A pointer to the
66*35191Smarc  *        allocated Namaray structure is returned.
67*35191Smarc  *
68*35191Smarc  *        MAXI becomes the current index of the array.
69*35191Smarc  */
70*35191Smarc 
growaray(arp,maxi)71*35191Smarc struct Namaray *growaray(arp,maxi)
72*35191Smarc struct Namaray *arp;
73*35191Smarc {
74*35191Smarc 	register struct Namaray *ap,*aq;
75*35191Smarc 	register int cursize, i;
76*35191Smarc 	register int newsize = arsize (maxi);
77*35191Smarc 	cursize = ((arp == NULL) ? 0 : arsize ((int)arp->maxi));
78*35191Smarc 	if (maxi >= ARRMAX)
79*35191Smarc 		failed (itos(maxi), subscript);
80*35191Smarc 	if (((aq = ap = arp) == NULL) || (newsize > cursize))
81*35191Smarc 	{
82*35191Smarc 		ap = (struct Namaray *)malloc((unsigned)(sizeof(struct Namaray)
83*35191Smarc 				+ (newsize-1)*sizeof(struct Nodval*)));
84*35191Smarc 		ap->maxi = maxi;
85*35191Smarc 		for(i=0;i < newsize;i++)
86*35191Smarc 			ap->val[i] = NULL;
87*35191Smarc 		if(aq)
88*35191Smarc 		{
89*35191Smarc 			for(i=0;i <= aq->maxi;i++)
90*35191Smarc 				ap->val[i] = aq->val[i];
91*35191Smarc 			free((char *)aq);
92*35191Smarc 		}
93*35191Smarc 	}
94*35191Smarc 	else
95*35191Smarc         	if (maxi > ap->maxi)
96*35191Smarc 			ap->maxi = maxi;
97*35191Smarc 	ap->adot = maxi;
98*35191Smarc 	return(ap);
99*35191Smarc }
100*35191Smarc 
101*35191Smarc #ifndef KSHELL
102*35191Smarc 
103*35191Smarc /*
104*35191Smarc  *   SETDOT (NODE, INDEX)
105*35191Smarc  *
106*35191Smarc  *        struct Namnod *NODE;
107*35191Smarc  *
108*35191Smarc  *        int INDEX;
109*35191Smarc  *
110*35191Smarc  *   Given an array of Namnods NODE, set the current index of NODE
111*35191Smarc  *   to INDEX.  Trap if INDEX is out of bounds.  Otherwise allocate
112*35191Smarc  *   a Nodval for the INDEXth element of NODE, if necessary.
113*35191Smarc  */
114*35191Smarc 
setdot(node,index)115*35191Smarc void	setdot (node, index)
116*35191Smarc struct Namnod *node;
117*35191Smarc int index;
118*35191Smarc {
119*35191Smarc 	register struct Nodval *nv;
120*35191Smarc 	register struct Namaray *ap = arayp (node);
121*35191Smarc 
122*35191Smarc 	if ((index > ap->maxi) || (index < 0))
123*35191Smarc 		failed (node->namid, subscript);
124*35191Smarc 	else
125*35191Smarc 		ap->adot = index;
126*35191Smarc 	if (ap->val[index] == NULL)
127*35191Smarc 	{
128*35191Smarc 		nv = (struct Nodval*)malloc (sizeof (struct Nodval));
129*35191Smarc 		nv->namflg = node->value.namflg & ~ARRAY;
130*35191Smarc 		nv->namval.cp = NULL;
131*35191Smarc 		ap->val[index] = nv;
132*35191Smarc 	}
133*35191Smarc 	return;
134*35191Smarc }
135*35191Smarc #endif	/* KSHELL */
136*35191Smarc 
137*35191Smarc /*
138*35191Smarc  *   ARSIZE (MAXI)
139*35191Smarc  *
140*35191Smarc  *        int MAXI;
141*35191Smarc  *
142*35191Smarc  *   Calculate the amount of space to be allocated to hold
143*35191Smarc  *   an array into which MAXI is a legal index.  The number of
144*35191Smarc  *   elements that will actually fit into the array (> MAXI
145*35191Smarc  *   but <= ARRMAX) is returned.
146*35191Smarc  *
147*35191Smarc  *   ALGORITHM:  The size of an array can be incremented in
148*35191Smarc  *               lots of ARRINCR elements.  Internal size is thus
149*35191Smarc  *               the least multiple of ARRINCR that is greater than
150*35191Smarc  *               MAXI.  (Note that 0-origin indexing is used.)
151*35191Smarc  */
152*35191Smarc 
arsize(maxi)153*35191Smarc int	arsize (maxi)
154*35191Smarc register int maxi;
155*35191Smarc {
156*35191Smarc 	register int i = round(maxi+1,ARRINCR);
157*35191Smarc 	return (i>ARRMAX?ARRMAX:i);
158*35191Smarc }
159