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