17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1573d361Snakanon * Common Development and Distribution License (the "License").
6*1573d361Snakanon * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*1573d361Snakanon * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * UNIX shell
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "defs.h"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /* ======== storage allocation ======== */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate unsigned char *
getstak(asize)407c478bd9Sstevel@tonic-gate getstak(asize) /* allocate requested stack */
417c478bd9Sstevel@tonic-gate int asize;
427c478bd9Sstevel@tonic-gate {
43965005c8Schin unsigned char *oldstak;
44965005c8Schin int size;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate size = round(asize, BYTESPERWORD);
477c478bd9Sstevel@tonic-gate oldstak = stakbot;
487c478bd9Sstevel@tonic-gate staktop = stakbot += size;
497c478bd9Sstevel@tonic-gate if (staktop >= brkend)
507c478bd9Sstevel@tonic-gate growstak(staktop);
517c478bd9Sstevel@tonic-gate return(oldstak);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * set up stack for local use
567c478bd9Sstevel@tonic-gate * should be followed by `endstak'
577c478bd9Sstevel@tonic-gate */
587c478bd9Sstevel@tonic-gate unsigned char *
locstak()597c478bd9Sstevel@tonic-gate locstak()
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate if (brkend - stakbot < BRKINCR)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate if (setbrk(brkincr) == -1)
647c478bd9Sstevel@tonic-gate error(nostack);
657c478bd9Sstevel@tonic-gate if (brkincr < BRKMAX)
667c478bd9Sstevel@tonic-gate brkincr += 256;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate return(stakbot);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate void
growstak(newtop)727c478bd9Sstevel@tonic-gate growstak(newtop)
737c478bd9Sstevel@tonic-gate unsigned char *newtop;
747c478bd9Sstevel@tonic-gate {
75965005c8Schin unsigned incr;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate incr = (unsigned)round(newtop - brkend + 1, BYTESPERWORD);
787c478bd9Sstevel@tonic-gate if (brkincr > incr)
797c478bd9Sstevel@tonic-gate incr = brkincr;
807c478bd9Sstevel@tonic-gate if (setbrk(incr) == -1)
817c478bd9Sstevel@tonic-gate error(nospace);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate unsigned char *
savstak()857c478bd9Sstevel@tonic-gate savstak()
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate assert(staktop == stakbot);
887c478bd9Sstevel@tonic-gate return(stakbot);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate unsigned char *
endstak(unsigned char * argp)92965005c8Schin endstak(unsigned char *argp) /* tidy up after `locstak' */
937c478bd9Sstevel@tonic-gate {
94965005c8Schin unsigned char *oldstak;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (argp >= brkend)
977c478bd9Sstevel@tonic-gate growstak(argp);
987c478bd9Sstevel@tonic-gate *argp++ = 0;
997c478bd9Sstevel@tonic-gate oldstak = stakbot;
1007c478bd9Sstevel@tonic-gate stakbot = staktop = (unsigned char *)round(argp, BYTESPERWORD);
1017c478bd9Sstevel@tonic-gate if (staktop >= brkend)
1027c478bd9Sstevel@tonic-gate growstak(staktop);
1037c478bd9Sstevel@tonic-gate return(oldstak);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
106965005c8Schin void
tdystak(unsigned char * x)107965005c8Schin tdystak(unsigned char *x) /* try to bring stack back to x */
1087c478bd9Sstevel@tonic-gate {
109*1573d361Snakanon struct blk *next;
110*1573d361Snakanon
1117c478bd9Sstevel@tonic-gate while ((unsigned char *)stakbsy > x)
1127c478bd9Sstevel@tonic-gate {
113*1573d361Snakanon next = stakbsy->word;
1147c478bd9Sstevel@tonic-gate free(stakbsy);
115*1573d361Snakanon stakbsy = next;
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate staktop = stakbot = max(x, stakbas);
1187c478bd9Sstevel@tonic-gate rmtemp(x);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
121965005c8Schin void
stakchk(void)122965005c8Schin stakchk(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate if ((brkend - stakbas) > BRKINCR + BRKINCR)
1257c478bd9Sstevel@tonic-gate setbrk(-BRKINCR);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate unsigned char *
cpystak(x)1297c478bd9Sstevel@tonic-gate cpystak(x)
1307c478bd9Sstevel@tonic-gate unsigned char *x;
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate return(endstak(movstrstak(x, locstak())));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate unsigned char *
movstrstak(unsigned char * a,unsigned char * b)136965005c8Schin movstrstak(unsigned char *a, unsigned char *b)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate do
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate if (b >= brkend)
1417c478bd9Sstevel@tonic-gate growstak(b);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate while (*b++ = *a++);
1447c478bd9Sstevel@tonic-gate return(--b);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * Copy s2 to s1, always copy n bytes.
1497c478bd9Sstevel@tonic-gate * Return s1
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate unsigned char *
memcpystak(unsigned char * s1,unsigned char * s2,int n)152965005c8Schin memcpystak(unsigned char *s1, unsigned char *s2, int n)
1537c478bd9Sstevel@tonic-gate {
154965005c8Schin unsigned char *os1 = s1;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate while (--n >= 0) {
1577c478bd9Sstevel@tonic-gate if (s1 >= brkend)
1587c478bd9Sstevel@tonic-gate growstak(s1);
1597c478bd9Sstevel@tonic-gate *s1++ = *s2++;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate return (os1);
1627c478bd9Sstevel@tonic-gate }
163