xref: /csrg-svn/contrib/sc/vmtbl.c (revision 45337)
1*45337Sbostic #ifdef PSC
2*45337Sbostic # include <stdio.h>
3*45337Sbostic # include "sc.h"
4*45337Sbostic # ifndef FALSE
5*45337Sbostic #  define	FALSE	0
6*45337Sbostic #  define	TRUE	1
7*45337Sbostic # endif /* !FALSE */
8*45337Sbostic # undef	error
9*45337Sbostic # define error(msg)	fprintf(stderr, msg);
10*45337Sbostic #else /* PSC */
11*45337Sbostic # include <curses.h>
12*45337Sbostic # include "sc.h"
13*45337Sbostic #endif /* PSC */
14*45337Sbostic 
15*45337Sbostic extern	char	*malloc();
16*45337Sbostic extern	char	*realloc();
17*45337Sbostic 
18*45337Sbostic #if defined(BSD42) || defined(BSD43)
19*45337Sbostic #define	memcpy(dest, source, len)	bcopy(source, dest, (unsigned int)len);
20*45337Sbostic #define	memset(dest, zero, len)		bzero((dest), (unsigned int)(len));
21*45337Sbostic #endif
22*45337Sbostic 
23*45337Sbostic /*
24*45337Sbostic  * check to see if *rowp && *colp are currently allocated, if not expand the
25*45337Sbostic  * current size if we can.
26*45337Sbostic  */
27*45337Sbostic #ifndef PSC
28*45337Sbostic void
checkbounds(rowp,colp)29*45337Sbostic checkbounds(rowp, colp)
30*45337Sbostic int	*rowp;
31*45337Sbostic int	*colp;
32*45337Sbostic {
33*45337Sbostic 	if (*rowp < 0)
34*45337Sbostic 		*rowp = 0;
35*45337Sbostic 	else if (*rowp >= maxrows)
36*45337Sbostic 	{	if (*colp >= maxcols)
37*45337Sbostic 		{	if (!growtbl(GROWBOTH, *rowp, *colp))
38*45337Sbostic 			{	*rowp = maxrows -1;
39*45337Sbostic 				*colp = maxcols -1;
40*45337Sbostic 			}
41*45337Sbostic 			return;
42*45337Sbostic 		}
43*45337Sbostic 		else
44*45337Sbostic 		{	if (!growtbl(GROWROW, *rowp, 0))
45*45337Sbostic 				*rowp = maxrows-1;
46*45337Sbostic 			return;
47*45337Sbostic 		}
48*45337Sbostic 	}
49*45337Sbostic 	if (*colp < 0)
50*45337Sbostic 		*colp = 0;
51*45337Sbostic 	else if (*colp >= maxcols)
52*45337Sbostic 	{	if (!growtbl(GROWCOL, 0, *colp));
53*45337Sbostic 			*colp = maxcols-1;
54*45337Sbostic 	}
55*45337Sbostic }
56*45337Sbostic #endif /* !PSC */
57*45337Sbostic 
58*45337Sbostic 
59*45337Sbostic #define GROWALLOC(newptr, oldptr, nelem, type, msg) \
60*45337Sbostic 	if (oldptr == (type *)NULL) \
61*45337Sbostic 		newptr = (type *)malloc((unsigned)(nelem*sizeof(type))); \
62*45337Sbostic 	else \
63*45337Sbostic 		newptr = (type *)realloc((char *)oldptr, \
64*45337Sbostic 					 (unsigned)(nelem*sizeof(type))); \
65*45337Sbostic 	if (newptr == (type *)NULL) \
66*45337Sbostic 	{   error(msg); \
67*45337Sbostic 	    return(FALSE); \
68*45337Sbostic 	} \
69*45337Sbostic 	oldptr = newptr /* wait incase we can't alloc */
70*45337Sbostic 
71*45337Sbostic static	char	nolonger[] = "The table can't be any longer";
72*45337Sbostic static	char	nowider[] = "The table can't be any wider";
73*45337Sbostic 
74*45337Sbostic /*
75*45337Sbostic  * grow the main && auxiliary tables (reset maxrows/maxcols as needed)
76*45337Sbostic  * toprow &&/|| topcol tell us a better guess of how big to become.
77*45337Sbostic  * we return TRUE if we could grow, FALSE if not....
78*45337Sbostic  */
79*45337Sbostic int
growtbl(rowcol,toprow,topcol)80*45337Sbostic growtbl(rowcol, toprow, topcol)
81*45337Sbostic int	rowcol;
82*45337Sbostic int	toprow, topcol;
83*45337Sbostic {
84*45337Sbostic 	struct ent ***tbl2;
85*45337Sbostic 	int	*fwidth2;
86*45337Sbostic 	int	*precision2;
87*45337Sbostic 	char	*col_hidden2;
88*45337Sbostic 	char	*row_hidden2;
89*45337Sbostic 	int	newrows, newcols;
90*45337Sbostic 	int	i;
91*45337Sbostic 
92*45337Sbostic #ifndef PSC
93*45337Sbostic 	newrows = maxrows;
94*45337Sbostic #endif /* !PSC */
95*45337Sbostic 
96*45337Sbostic 	newcols = maxcols;
97*45337Sbostic 	if (rowcol == GROWNEW)
98*45337Sbostic 	{
99*45337Sbostic #ifndef PSC
100*45337Sbostic 		maxrows = toprow = 0;
101*45337Sbostic 		/* when we first start up, fill the screen w/ cells */
102*45337Sbostic 		{	int startval;
103*45337Sbostic 			startval = LINES - RESROW;
104*45337Sbostic 			newrows = startval > MINROWS ? startval : MINROWS;
105*45337Sbostic 			startval = ((COLS) - RESCOL) / DEFWIDTH;
106*45337Sbostic 			newcols = startval > MINCOLS ? startval : MINCOLS;
107*45337Sbostic 		}
108*45337Sbostic #else
109*45337Sbostic 		newcols = MINCOLS;
110*45337Sbostic #endif /* !PSC */
111*45337Sbostic 		maxcols = topcol = 0;
112*45337Sbostic 	}
113*45337Sbostic #ifndef PSC
114*45337Sbostic 	/* set how much to grow */
115*45337Sbostic 	if ((rowcol == GROWROW) || (rowcol == GROWBOTH))
116*45337Sbostic 	{	if (toprow > maxrows)
117*45337Sbostic 			newrows = GROWAMT + toprow;
118*45337Sbostic 		else
119*45337Sbostic 			newrows += GROWAMT;
120*45337Sbostic 	}
121*45337Sbostic #endif /* !PSC */
122*45337Sbostic 	if ((rowcol == GROWCOL) || (rowcol == GROWBOTH))
123*45337Sbostic 	{	if ((rowcol == GROWCOL) && ((maxcols == ABSMAXCOLS) ||
124*45337Sbostic 					(topcol >= ABSMAXCOLS)))
125*45337Sbostic 		{	error(nowider);
126*45337Sbostic 			return(FALSE);
127*45337Sbostic 		}
128*45337Sbostic 
129*45337Sbostic 		if (topcol > maxcols)
130*45337Sbostic 			newcols = GROWAMT + topcol;
131*45337Sbostic 		else
132*45337Sbostic 			newcols += GROWAMT;
133*45337Sbostic 
134*45337Sbostic 		if (newcols > ABSMAXCOLS)
135*45337Sbostic 			newcols = ABSMAXCOLS;
136*45337Sbostic 	}
137*45337Sbostic 
138*45337Sbostic #ifndef PSC
139*45337Sbostic 	if ((rowcol == GROWROW) || (rowcol == GROWBOTH) || (rowcol == GROWNEW))
140*45337Sbostic 	{
141*45337Sbostic 		GROWALLOC(row_hidden2, row_hidden, newrows, char, nolonger);
142*45337Sbostic 		memset(row_hidden+maxrows, 0, (newrows-maxrows)*sizeof(char));
143*45337Sbostic 
144*45337Sbostic 		/* alloc tbl row pointers */
145*45337Sbostic 		GROWALLOC(tbl2, tbl, newrows, struct ent **, nolonger);
146*45337Sbostic 		memset(tbl+maxrows, 0, (newrows-maxrows)*(sizeof(struct ent **)));
147*45337Sbostic 	}
148*45337Sbostic #endif /* !PSC */
149*45337Sbostic 
150*45337Sbostic 	if ((rowcol == GROWCOL) || (rowcol == GROWBOTH) || (rowcol == GROWNEW))
151*45337Sbostic 	{
152*45337Sbostic 		GROWALLOC(fwidth2, fwidth, newcols, int, nowider);
153*45337Sbostic 		GROWALLOC(precision2, precision, newcols, int, nowider);
154*45337Sbostic #ifdef PSC
155*45337Sbostic 		memset(fwidth+maxcols, 0, (newcols-maxcols)*sizeof(int));
156*45337Sbostic 		memset(precision+maxcols, 0, (newcols-maxcols)*sizeof(int));
157*45337Sbostic 	}
158*45337Sbostic #else
159*45337Sbostic 		GROWALLOC(col_hidden2, col_hidden, newcols, char, nowider);
160*45337Sbostic 		memset(col_hidden+maxcols, 0, (newcols-maxcols)*sizeof(char));
161*45337Sbostic 		for (i = maxcols; i < newcols; i++) {
162*45337Sbostic 			fwidth[i] = DEFWIDTH;
163*45337Sbostic 			precision[i] = DEFPREC;
164*45337Sbostic 		}
165*45337Sbostic 
166*45337Sbostic 		/* [re]alloc the space for each row */
167*45337Sbostic 		for (i = 0; i < maxrows; i++)
168*45337Sbostic 		{
169*45337Sbostic 		    if ((tbl[i] = (struct ent **)realloc((char *)tbl[i],
170*45337Sbostic 			(unsigned)(newcols * sizeof(struct ent **)))) == (struct ent **)0)
171*45337Sbostic 			{	error(nowider);
172*45337Sbostic 				return(FALSE);
173*45337Sbostic 			}
174*45337Sbostic 		    memset((char *)ATBL(tbl,i, maxcols), 0,
175*45337Sbostic 			   (newcols-maxcols)*sizeof(struct ent **));
176*45337Sbostic 		}
177*45337Sbostic 	}
178*45337Sbostic 	else
179*45337Sbostic 		i = maxrows;
180*45337Sbostic 
181*45337Sbostic 	/* fill in the bottom of the table */
182*45337Sbostic 	for (; i < newrows; i++)
183*45337Sbostic 	{	if ((tbl[i] = (struct ent **)malloc((unsigned)(newcols *
184*45337Sbostic 				sizeof(struct ent **)))) == (struct ent **)0)
185*45337Sbostic 		{	error(nowider);
186*45337Sbostic 			return(FALSE);
187*45337Sbostic 		}
188*45337Sbostic 		memset((char *)tbl[i], 0, newcols*sizeof(struct ent **));
189*45337Sbostic 	}
190*45337Sbostic 
191*45337Sbostic 	FullUpdate++;
192*45337Sbostic 	maxrows = newrows;
193*45337Sbostic #endif /* PSC */
194*45337Sbostic 
195*45337Sbostic 	maxcols = newcols;
196*45337Sbostic 	return(TRUE);
197*45337Sbostic }
198