xref: /original-bsd/local/toolchest/ksh/sh/stak.c (revision bbb96de4)
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 /* @(#)stak.c	1.1 */
14 /*
15  * UNIX shell
16  *
17  * S. R. Bourne
18  * Rewritten by David Korn
19  * AT&T Bell Laboratories
20  *
21  */
22 
23 #include	"defs.h"
24 #include	"stak.h"
25 #include	"brkincr.h"
26 
27 extern char	*movstr();
28 extern void	setbrk();
29 extern void	rmtemp();
30 extern void	free();
31 
32 
33 
34 /* ========	storage allocation	======== */
35 
36 /*
37  * allocate requested stack
38  */
39 
40 STKPTR	getstak(asize)
41 int 	asize;
42 {
43 	register STKPTR oldstak;
44 	register int 	size;
45 	size=round(asize,BYTESPERWORD);
46 	oldstak=stakbot;
47 	staktop = stakbot += size;
48 	if(staktop >= brkend)
49 		setbrk(round(staktop-brkend,BRKINCR));
50 	return(oldstak);
51 }
52 
53 /*
54  * set up stack for local use
55  * should be followed by `endstak'
56  */
57 
58 STKPTR	locstak()
59 {
60 	if(brkend-stakbot<BRKINCR)
61 		setbrk(BRKINCR);
62 	return(staktop=stakbot);
63 }
64 
65 
66 /*
67  * tidy up after `locstak'
68  */
69 
70 STKPTR	endstak(argp)
71 register char *argp;
72 {
73 	register STKPTR oldstak;
74 	*argp++=0;
75 	oldstak=stakbot; stakbot=staktop=(STKPTR) round(argp,BYTESPERWORD);
76 	return(oldstak);
77 }
78 
79 /*
80  * try to bring stack back to x
81  */
82 
83 void	tdystak(x)
84 register STKPTR  x;
85 {
86 	while(ADR(stakbsy)>ADR(x))
87 	{
88 		free((char*)stakbsy);
89 		stakbsy = stakbsy->word;
90 	}
91 	staktop=stakbot=max(ADR(x),ADR(stakbas));
92 	if(iotemp > (IOPTR)x)
93 		rmtemp((IOPTR)x);
94 }
95 
96 stakchk()
97 {
98 	register int size = -3*BRKINCR;
99 #ifdef INT16
100 	if((brkend-stakbas) >= BRKMAX)
101 		size = BRKMAX;
102 	else
103 #endif /* INT16 */
104 		size += round(brkend-stakbas,BRKINCR);
105 	if(size > 0)
106 		setbrk(-size);
107 }
108 
109 STKPTR	cpystak(x)
110 STKPTR		x;
111 {
112 	return(endstak(movstr(x,((char*)locstak()))));
113 }
114