xref: /original-bsd/old/sh/stak.c (revision 3839ed90)
1 #ifndef lint
2 static char sccsid[] = "@(#)stak.c	4.2 08/11/83";
3 #endif
4 
5 #
6 /*
7  * UNIX shell
8  *
9  * S. R. Bourne
10  * Bell Telephone Laboratories
11  *
12  */
13 
14 #include	"defs.h"
15 
16 STKPTR		stakbot=nullstr;
17 
18 
19 
20 /* ========	storage allocation	======== */
21 
22 STKPTR	getstak(asize)
23 	INT		asize;
24 {	/* allocate requested stack */
25 	REG STKPTR	oldstak;
26 	REG INT		size;
27 
28 	size=round(asize,BYTESPERWORD);
29 	oldstak=stakbot;
30 	staktop = stakbot += size;
31 	return(oldstak);
32 }
33 
34 STKPTR	locstak()
35 {	/* set up stack for local use
36 	 * should be followed by `endstak'
37 	 */
38 	IF brkend-stakbot<BRKINCR
39 	THEN	setbrk(brkincr);
40 		IF brkincr < BRKMAX
41 		THEN	brkincr += 256;
42 		FI
43 	FI
44 	return(stakbot);
45 }
46 
47 STKPTR	savstak()
48 {
49 	assert(staktop==stakbot);
50 	return(stakbot);
51 }
52 
53 STKPTR	endstak(argp)
54 	REG STRING	argp;
55 {	/* tidy up after `locstak' */
56 	REG STKPTR	oldstak;
57 	*argp++=0;
58 	oldstak=stakbot; stakbot=staktop=round(argp,BYTESPERWORD);
59 	return(oldstak);
60 }
61 
62 VOID	tdystak(x)
63 	REG STKPTR 	x;
64 {
65 	/* try to bring stack back to x */
66 	WHILE ADR(stakbsy)>ADR(x)
67 	DO free(stakbsy);
68 	   stakbsy = stakbsy->word;
69 	OD
70 	staktop=stakbot=max(ADR(x),ADR(stakbas));
71 	rmtemp(x);
72 }
73 
74 stakchk()
75 {
76 	IF (brkend-stakbas)>BRKINCR+BRKINCR
77 	THEN	setbrk(-BRKINCR);
78 	FI
79 }
80 
81 STKPTR	cpystak(x)
82 	STKPTR		x;
83 {
84 	return(endstak(movstr(x,locstak())));
85 }
86