1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * A copy of the CDDL is also available via the Internet at
11  * http://www.opensource.org/licenses/cddl1.txt
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 
24 /*
25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 #ifndef	_DEFS_H
33 #define	_DEFS_H
34 
35 #if defined(sun)
36 #pragma ident	"@(#)defs.h	1.28	08/01/29 SMI"
37 #endif
38 
39 /*
40  * Copyright 2008-2020 J. Schilling
41  *
42  * @(#)defs.h	1.212 20/10/08 2008-2020 J. Schilling
43  */
44 
45 /*
46  * Some compilers may not support enough command line arguments.
47  * This include file permits to put the configuration inti conf.h
48  * instead of having it in the Makefile.
49  */
50 #ifdef	DO_CONF_H
51 #include	"conf.h"
52 #endif
53 
54 #ifdef	__cplusplus
55 extern "C" {
56 #endif
57 
58 /*
59  *	UNIX shell
60  */
61 
62 /* execute flags */
63 #define		XEC_EXECED	01	/* Forked cmd with recursive execute */
64 #define		XEC_LINKED	02	/* Forked lhs of "|" or "&"	    */
65 #define		XEC_NOSTOP	04	/* Do no jobcontrol for this cmd    */
66 #define		XEC_NOBLTIN	010	/* Do not execute functions / bltins */
67 #define		XEC_STDINSAV	020	/* STDIN_FILENO was moved away	    */
68 #define		XEC_ALLOCJOB	040	/* A job slot was already allocated */
69 #define		XEC_HASARGV	0100	/* t->comarg holds expanded argv[]  */
70 #define		XEC_INFUNC	01000	/* We are inside a function call    */
71 
72 /* endjobs flags */
73 #define		JOB_STOPPED	01
74 #define		JOB_RUNNING	02
75 
76 /* Environ flags */
77 #define		ENV_NOFREE	01
78 
79 /* error exits from various parts of shell */
80 #define		ERROR		1	/* Standard shell error/exit code */
81 #define		SYNBAD		2	/* Shell error/exit for bad syntax */
82 #ifdef	DO_POSIX_TEST
83 #define		ETEST		2	/* POSIX test(1) error exit code  */
84 #else
85 #define		ETEST		ERROR	/* historical test(1) error exit code */
86 #endif
87 #define		SIGFAIL 	2000	/* Default shell exit code on signal */
88 #define		SIGFLG		0200	/* $? == SIGFLG + signo */
89 #define		C_NOEXEC	126	/* Shell error/exit for exec error */
90 #define		C_NOTFOUND	127	/* Shell error/exit for exec notfound */
91 #ifdef	DO_POSIX_EXIT
92 #define		ERR_NOEXEC	C_NOEXEC
93 #define		ERR_NOTFOUND	C_NOTFOUND
94 #else
95 #define		ERR_NOEXEC	ERROR
96 #define		ERR_NOTFOUND	ERROR
97 #endif
98 
99 /* command tree */
100 #define		FPIN		0x0100	/* PIPE from stdin		*/
101 #define		FPOU		0x0200	/* PIPE to stdout		*/
102 #define		FAMP		0x0400	/* Forked because of "cmd &"	*/
103 #define		COMMSK		0x10F0	/* Node type mask, see below	*/
104 #define		CNTMSK		0x000F	/* Count mask - no longer used	*/
105 #define		IOFMSK		0x000F	/* I/O fd# mask for pipes	*/
106 
107 #define		TCOM		0x0000	/* some kind of command node 	*/
108 #define		TPAR		0x0010	/* "()" parentized cmd node	*/
109 #define		TFIL		0x0020	/* PIPE "|" filter node		*/
110 #define		TLST		0x0030	/* ";" separated command list	*/
111 #define		TIF		0x0040	/* if ... then ... node		*/
112 #define		TWH		0x0050	/* "while" loop node		*/
113 #define		TUN		0x0060	/* "until" loop node		*/
114 #define		TSW		0x0070	/* "case command node		*/
115 #define		TAND		0x0080	/* "&&" command node		*/
116 #define		TORF		0x0090	/* "||" command node		*/
117 #define		TFORK		0x00A0	/* node running forked cmd	*/
118 #define		TNOFORK		0x10A0	/* node running avoid fork cmd	*/
119 #define		TFOR		0x00B0	/* for ... do .. done node	*/
120 #define		TSELECT		0x10B0	/* select ... do .. done node	*/
121 #define		TFND		0x00C0	/* function definition node	*/
122 #define		TTIME		0x00D0	/* "time" node			*/
123 #define		TNOT		0x10D0	/* "!" node			*/
124 #define		TSETIO		0x00E0	/* node to redirect io		*/
125 
126 /*
127  * execute table
128  *
129  * Numbers for builtin commands
130  */
131 #define		SYSSET		1
132 #define		SYSCD		2
133 #define		SYSEXEC		3
134 
135 #ifdef RES	/*	include login code	*/
136 #define		SYSLOGIN	4
137 #else
138 #define		SYSNEWGRP 	4
139 #endif
140 
141 #define		SYSTRAP		5
142 #define		SYSEXIT		6
143 #define		SYSSHFT		7
144 #define		SYSWAIT		8
145 #define		SYSCONT		9
146 #define		SYSBREAK	10
147 #define		SYSEVAL		11
148 #define		SYSDOT		12
149 #define		SYSRDONLY	13
150 #define		SYSTIMES	14
151 #define		SYSXPORT	15
152 #define		SYSNULL		16
153 #define		SYSREAD		17
154 #define		SYSTST		18
155 
156 #ifndef RES	/*	exclude umask code	*/
157 #define		SYSUMASK 	20
158 #define		SYSULIMIT 	21
159 #endif
160 
161 #define		SYSECHO		22
162 #define		SYSHASH		23
163 #define		SYSPWD		24
164 #define		SYSRETURN	25
165 #define		SYSUNS		26
166 #define		SYSMEM		27
167 #define		SYSTYPE  	28
168 #define		SYSGETOPT	29
169 #define		SYSJOBS		30
170 #define		SYSFGBG		31
171 #define		SYSKILL		32
172 #define		SYSSUSP		33
173 #define		SYSSTOP		34
174 
175 #define		SYSHISTORY	35
176 #define		SYSSAVEHIST	36
177 #define		SYSMAP		37
178 #define		SYSREPEAT	38
179 
180 #define		SYSDIRS		39
181 #define		SYSPOPD		40
182 #define		SYSPUSHD	41
183 
184 #define		SYSDOSH		42
185 #define		SYSALIAS	43
186 #define		SYSUNALIAS	44
187 
188 #define		SYSTRUE		45
189 #define		SYSFALSE	46
190 
191 #define		SYSALLOC	47
192 
193 #define		SYSBUILTIN	48
194 #define		SYSFIND		49
195 #define		SYSEXPR		50
196 #define		SYSSYNC		51
197 #define		SYSPGRP		52
198 #define		SYSERRSTR	53
199 #define		SYSPRINTF	54
200 #define		SYSCOMMAND	55
201 #define		SYSLOCAL	56
202 #define		SYSFC		57
203 
204 #define		SYSLOADABLE	255	/* For all dynamically loaded cmds   */
205 #define		SYSMAX		255	/* Must fit in low 8 ENTRY.data bits */
206 
207 /*
208  * Sysnode flags for builtin commands:
209  */
210 #define		BLT_SPC		1	/* A special builtin */
211 #define		BLT_INT		2	/* A shell intrinsic */
212 #define		BLT_DEL		8	/* Builtin was deleted */
213 
214 /*
215  * Flags for cd/pwd reladed builtin commands:
216  */
217 #define		CHDIR_L		1	/* Use logical mode (symlinks kept)   */
218 #define		CHDIR_P		2	/* Use physcal mode (expand symlinks) */
219 
220 /*
221  * Binary operators in "test"
222  */
223 #define	TEST_SNEQ	1		/* !=  string not equal */
224 #define	TEST_AND	2		/* -a  and */
225 #define	TEST_EF		3		/* -ef file exist + equal */
226 #define	TEST_NT		4		/* -nt file newer than */
227 #define	TEST_OR		5		/* -o  or */
228 #define	TEST_OT		6		/* -ot file older than */
229 #define	TEST_SEQ	7		/* =   string equal */
230 
231 #define	TEST_EQ		10		/* -eq integer equal		== */
232 #define	TEST_GE		11		/* -ge integer greater or equal >= */
233 #define	TEST_GT		12		/* -gt integer greater		>  */
234 #define	TEST_LE		13		/* -le integer less or equal	<= */
235 #define	TEST_LT		14		/* -lt integer less than	<  */
236 #define	TEST_NE		15		/* -ne integer not equal	!= */
237 
238 /* used for input and output of shell */
239 #define		INIO 		19
240 
241 /* io nodes */
242 #define		USERIO		10	/* User definable fds in range 0..9 */
243 #define		IOUFD		15	/* mask for UNIX file descriptor #  */
244 #define		IODOC		0x0010	/* << here document		    */
245 #define		IOPUT		0x0020	/* >  redirection		    */
246 #define		IOAPP		0x0040	/* >> redirection		    */
247 #define		IOMOV		0x0080	/* <& or >& redirection		    */
248 #define		IORDW		0x0100	/* <> redirection open with O_RDWR  */
249 #define		IOSTRIP		0x0200	/* <<-word: strip leading tabs	    */
250 #define		IODOC_SUBST	0x0400	/* <<\word: no substitution	    */
251 #define		IOCLOB		0x1000	/* >| clobber files		    */
252 #define		IOBARRIER	0x2000	/* tmpfile link/unlink barrier	    */
253 
254 #define		INPIPE		0	/* Input fd index in pipe fd array  */
255 #define		OTPIPE		1	/* Output fd index in pipe fd array */
256 
257 /* arg list terminator */
258 #define		ENDARGS		0
259 
260 #ifdef	SCHILY_INCLUDES
261 #include	<schily/mconfig.h>
262 #include	<schily/unistd.h>
263 #include	<schily/getopt.h>
264 #include	<schily/stdlib.h>	/* malloc()/free()... */
265 #include	<schily/limits.h>
266 #include	<schily/maxpath.h>
267 #include	<schily/signal.h>
268 #include	<schily/types.h>
269 #include	<schily/utypes.h>
270 #include	<schily/time.h>
271 #include	<schily/string.h>
272 #undef	index
273 
274 /* locale support */
275 #include	"ctype.h"
276 #include	<schily/ctype.h>
277 #include	<schily/nlsdefs.h>
278 #include	<schily/wchar.h>	/* includes stdio.h */
279 #include	<schily/wctype.h>	/* needed before we use wchar_t */
280 #undef	feof				/* to make mode.h compile with K&R */
281 
282 #include	<schily/setjmp.h>
283 #include	<schily/jmpdefs.h>
284 
285 #ifdef DO_SYSBUILTIN
286 /*
287  * Note that if we do not #define DO_SYSBUILTIN, we will never have
288  * a HAVE_LOADABLE_LIBS #define
289  */
290 #include	<schily/dlfcn.h>	/* to #define HAVE_LOADABLE_LIBS */
291 #endif
292 
293 #include 	"mac.h"
294 #include	"mode.h"
295 #include	"name.h"
296 #include	"bosh.h"
297 
298 #ifndef	HAVE_SYS_ACCT_H
299 #undef	ACCT
300 #endif
301 
302 #else	/* SCHILY_INCLUDES */
303 
304 #include	<unistd.h>
305 #include 	"mac.h"
306 #include	"mode.h"
307 #include	"name.h"
308 #include	"bosh.h"
309 #include	<signal.h>
310 #include	<sys/types.h>
311 #include	<inttypes.h>
312 #define	UInt32_t	uint32_t
313 #define	UInt16_t	uint16_t
314 #define	UIntmax_t	uintmax_t
315 #define	Intmax_t	intmax_t
316 #define	UIntptr_t	uintptr_t
317 #define	Intptr_t	intptr_t
318 #include	<string.h>
319 #undef	index
320 
321 /* locale support */
322 #include	"ctype.h"
323 #include	<ctype.h>
324 #include	<locale.h>
325 
326 #include	<stdlib.h>
327 #include	<limits.h>
328 
329 #include	<setjmp.h>
330 
331 #define	PROTOTYPES
332 #define	__PR(a)	a
333 #define	EXPORT
334 #define	LOCAL	static
335 
336 /*
337  * This is a static configuration for SunOS-5.11.
338  * For other platforms, use the dynamic SCHILY_INCLUDES environment.
339  */
340 #define	HAVE_LIBGEN_H
341 #define	HAVE_GMATCH
342 #define	HAVE_UCONTEXT_H
343 #define	HAVE_ACCESS_E_OK
344 #define	HAVE_STRSIGNAL
345 #define	HAVE_STR2SIG
346 #define	HAVE_SIG2STR
347 #define	HAVE_MEMCHR
348 #define	HAVE_MEMSET
349 #define	HAVE_MEMCPY
350 #define	HAVE_MEMMOVE
351 #define	HAVE_SBRK
352 #define	HAVE_GETPGID
353 #define	HAVE_GETSID
354 
355 #define	HAVE_ISASTREAM
356 #define	HAVE_SIGINFO_H
357 #define	HAVE_SIGINFO_T
358 #define	HAVE_SIGALTSTACK
359 #define	HAVE_STACK_T
360 #define	HAVE_STROPTS_H
361 #define	HAVE_STRTOLL
362 #define	HAVE_SYS_PROCSET_H
363 #define	HAVE_TCGETPGRP
364 #define	HAVE_TCSETPGRP
365 
366 #endif	/* ! SCHILY_INCLUDES */
367 
368 #define	CH	(char)
369 #define	UCH	(unsigned char)
370 #define	C	(char *)
371 #define	UC	(unsigned char *)
372 #define	CP	(char **)
373 #define	UCP	(unsigned char **)
374 #define	CPP	(char ***)
375 #define	UCPP	(unsigned char ***)
376 
377 #ifndef	__NORETURN
378 #define	__NORETURN
379 #endif
380 
381 #ifdef	RETSIGTYPE	/* From schily/mconfig.h */
382 typedef	RETSIGTYPE	(*sigtype) __PR((int));
383 typedef	RETSIGTYPE	sigret;
384 
385 #else	/* RETSIGTYPE */
386 #define	VOID_SIGS
387 #ifdef	VOID_SIGS
388 typedef	void	(*sigtype) __PR((int));
389 typedef	void	sigret;
390 #else
391 typedef	int	(*sigtype) __PR((int));
392 typedef	int	sigret;
393 #endif	/* VOID_SIGS */
394 #endif	/* RETSIGTYPE */
395 
396 /*
397  * Global data structure
398  */
399 extern bosh_t	bosh;
400 
401 /* id's */
402 extern pid_t	mypid;
403 extern pid_t	mypgid;
404 extern pid_t	mysid;
405 
406 /* getopt */
407 #define	_sp		opt_sp	/* Use variable name from new getopt() */
408 #ifndef	__CYGWIN__
409 /*
410  *	Cygwin uses a nonstandard:
411  *
412  *		__declspec(dllexport)
413  *	or
414  *		__declspec(dllimport)
415  *
416  *	that is in conflict with our standard definition.
417  */
418 extern int		optind;
419 extern int		opterr;
420 extern int		optopt;
421 extern char 		*optarg;
422 #endif
423 
424 #ifdef	STAK_DEBUG
425 #ifndef	DO_SYSALLOC
426 #define	DO_SYSALLOC
427 #endif
428 #endif	/* !STAK_DEBUG */
429 
430 #ifdef	NO_INTERACTIVE
431 #undef	INTERACTIVE
432 #undef	DO_SYSFC
433 #endif
434 
435 #ifdef	NO_SYSATEXPR
436 #undef	DO_SYSATEXPR
437 #endif
438 
439 #ifdef	NO_SYSFIND
440 #undef	DO_SYSFIND
441 #endif
442 
443 #ifdef	NO_SYSPRINTF
444 #undef	DO_SYSPRINTF
445 #endif
446 
447 #ifdef	NO_VFORK
448 #undef	HAVE_VFORK
449 #endif
450 
451 #ifdef	NO_PIPE_PARENT
452 #undef	DO_PIPE_PARENT
453 #endif
454 
455 /* Function prototypes */
456 
457 /*
458  * args.c
459  */
460 extern	void		prversion	__PR((void));
461 extern	int		options		__PR((int argc, unsigned char **argv));
462 extern	void		setopts		__PR((void));
463 extern	void		setargs		__PR((unsigned char *argi[]));
464 extern	struct dolnod	*freeargs	__PR((struct dolnod *blk));
465 extern	void		clearup		__PR((void));
466 extern	struct dolnod	*savargs	__PR((int funcnt));
467 extern	void 		restorargs	__PR((struct dolnod *olddolh,
468 							int funcnt));
469 extern	struct dolnod	*useargs	__PR((void));
470 extern	int		optval		__PR((unsigned char *flagc));
471 extern	unsigned char	*lookopt	__PR((unsigned char *name));
472 
473 /*
474  * blok.c
475  */
476 extern	void	addblok		__PR((unsigned int));
477 #ifdef	DO_SYSALLOC
478 extern	void	chkmem		__PR((void));
479 #endif
480 
481 /*
482  * bltin.c
483  */
484 extern	void	builtin		__PR((int type, int argc, unsigned char **argv,
485 						struct trenod *t, int xflags));
486 
487 /*
488  * cmd.c
489  */
490 extern	struct trenod *makefork	__PR((int flgs, struct trenod *i));
491 extern	struct trenod *cmd	__PR((int sym, int flg));
492 
493 /*
494  * echo.c
495  */
496 extern	int	echo		__PR((int argc, unsigned char **argv));
497 extern unsigned char *escape_char __PR((unsigned char *cp, unsigned char *res,
498 					int echomode));
499 
500 
501 /*
502  * error.c
503  *
504  * error() and failed_real() used to have the __NORETURN tag, but since we
505  * support the "command" builtin, we need to be able to prevent the exit
506  * on error (or longjmp to prompt) behavior via (flags & noexit) != 0.
507  */
508 extern	void	error		__PR((const char *s));
509 extern	void	failed_real	__PR((int err, unsigned char *s1,
510 						const char *s2,
511 						unsigned char *s3));
512 extern	void	failure_real	__PR((int err, unsigned char *s1,
513 						const char *s2,
514 						unsigned char *s3,
515 						int gflag));
516 
517 extern	void	exvalsh		__PR((int xno));
518 extern	void	exitsh		__PR((int xno)) __NORETURN;
519 #ifdef	DO_DOT_SH_PARAMS
520 extern	void	exval_clear	__PR((void));
521 extern	void	exval_sig	__PR((void));
522 extern	void	exval_set	__PR((int xno));
523 #else
524 #define	exval_clear()
525 #define	exval_sig()
526 #define	exval_set(a)
527 #endif
528 extern	void	rmtemp		__PR((struct ionod *base));
529 extern	void	rmfunctmp	__PR((struct ionod *base));
530 
531 
532 /*
533  * expand.c
534  */
535 extern	int	expand		__PR((unsigned char *as, int rcnt));
536 extern	void	makearg		__PR((struct argnod *));
537 
538 /*
539  * fault.c
540  */
541 extern	void	done		__PR((int sig)) __NORETURN;
542 extern	void	fault		__PR((int sig));
543 extern	int	handle		__PR((int sig, sigtype func));
544 extern	void	stdsigs		__PR((void));
545 extern	void	oldsigs		__PR((int dofree));
546 #ifdef	HAVE_VFORK
547 extern	void	restoresigs	__PR((void));
548 #endif
549 extern	void	chktrap		__PR((void));
550 extern	void	systrap		__PR((int argc, char **argv));
551 extern	void	sh_sleep	__PR((unsigned int ticks));
552 extern	void 	init_sigval	 __PR((void));
553 
554 
555 /*
556  * func.c
557  */
558 extern	void	freefunc	__PR((struct namnod  *n));
559 extern	void	freetree	__PR((struct trenod  *n));
560 extern	void	prcmd		__PR((struct trenod *t));
561 extern	void	prtree		__PR((struct trenod *t, char *label));
562 extern	void	prf		__PR((struct trenod *t));
563 
564 #ifndef	HAVE_GMATCH
565 extern	int	gmatch		__PR((const char *, const char *));
566 #else
567 #ifdef	HAVE_LIBGEN_H
568 #include <libgen.h>
569 #endif
570 #endif
571 
572 /*
573  * hashserv.c
574  */
575 extern	short	pathlook	__PR((unsigned char *com, int flg,
576 						struct argnod *arg));
577 extern	void	zaphash		__PR((void));
578 extern	void	zapcd		__PR((void));
579 extern	void	hashpr		__PR((void));
580 extern	void	set_dotpath	__PR((void));
581 extern	void	hash_func	__PR((unsigned char *name));
582 extern	void	func_unhash	__PR((unsigned char *name));
583 extern	short	hash_cmd	__PR((unsigned char *name));
584 extern	int	what_is_path	__PR((unsigned char *name, int verbose));
585 extern	int	chk_access	__PR((unsigned char *name,
586 						mode_t mode, int regflag));
587 
588 /*
589  * hashcmd.c
590  */
591 extern	void	hashcmd		__PR((void));
592 
593 /*
594  * io.c
595  */
596 extern	void	initf		__PR((int fd));
597 extern	int	estabf		__PR((unsigned char *s));
598 extern	void	push		__PR((struct fileblk *af));
599 extern	int	pop		__PR((void));
600 extern	int	poptemp		__PR((void));
601 extern	int	gpoptemp	__PR((void));
602 extern	void	chkpipe		__PR((int *pv));
603 extern	int	chkopen		__PR((unsigned char *idf, int mode));
604 extern	void	renamef		__PR((int f1, int f2));
605 extern	int	create		__PR((unsigned char *s, int iof));
606 extern	int	tmpfil		__PR((struct tempblk *tb));
607 extern	void	copy		__PR((struct ionod *ioparg));
608 extern	int	link_iodocs	__PR((struct ionod *i));
609 extern	void	swap_iodoc_nm	__PR((struct ionod *i));
610 extern	int	savefd		__PR((int fd));
611 extern	void	restore		__PR((int last));
612 
613 /*
614  * jobs.c
615  */
616 extern	char	*code2str	__PR((int code));
617 extern	void	collect_fg_job	__PR((void));
618 extern	void	freejobs	__PR((void));
619 extern	int	settgid		__PR((pid_t new, pid_t expexted));
620 extern	void	startjobs	__PR((void));
621 extern	int	endjobs		__PR((int check_if));
622 extern	void	allocjob	__PR((char *_cmd, unsigned char *cwd,
623 							int monitor));
624 extern	void	clearjobs	__PR((void));
625 extern	void	makejob		__PR((int monitor, int fg));
626 /*
627  * This is a partial type declaration for struct job. After this line,
628  * we may use struct job * in a parameter list.
629  */
630 extern	struct job *
631 		postjob		__PR((pid_t pid, int fg, int blt));
632 extern	void	deallocjob	__PR((struct job *jp));
633 extern	void	clearcurjob	__PR((void));
634 extern	void	*curjob		__PR((void));
635 extern	pid_t	curpgid		__PR((void));
636 extern	void	setjobpgid	__PR((pid_t pgid));
637 extern	void	setjobfd	__PR((int fd, int sfd));
638 extern	void	resetjobfd	__PR((void));
639 extern	void	sysjobs		__PR((int argc, unsigned char *argv[]));
640 extern	void	sysfgbg		__PR((int argc, char *argv[]));
641 extern	void	syswait		__PR((int argc, char *argv[]));
642 extern	void	sysstop		__PR((int argc, char *argv[]));
643 extern	void	syskill		__PR((int argc, char *argv[]));
644 extern	void	syssusp		__PR((int argc, char *argv[]));
645 extern	void	syspgrp		__PR((int argc, char *argv[]));
646 extern	int	sysprintf	__PR((int argc, unsigned char *argv[]));
647 extern	void	hupforegnd	__PR((void));
648 extern	pid_t	wait_status	__PR((pid_t id,
649 					int *codep, int *statusp, int opts));
650 extern	void	prtime		__PR((struct job *jp));
651 #ifndef	HAVE_GETRUSAGE
652 extern	int	getrusage	__PR((int who, struct rusage *r_usage));
653 #endif
654 extern	void	shmcreate	__PR((void));
655 
656 /*
657  * macro.c
658  */
659 #define	M_PARM		1	/* Normal parameter expansion	*/
660 #define	M_COMMAND	2	/* Command substitution		*/
661 #define	M_DOLAT		4	/* $@ was expanded		*/
662 #define	M_ARITH		8	/* $(()) was expanded		*/
663 #define	M_SPLIT		(M_PARM|M_COMMAND|M_DOLAT|M_ARITH)
664 #define	M_NOCOMSUBST	0x100	/* Do not expand ` ` and $()	*/
665 extern	unsigned char *macro	__PR((unsigned char *as));
666 extern	unsigned char *_macro	__PR((unsigned char *as));
667 extern	void	subst		__PR((int in, int ot));
668 
669 /*
670  * main.c
671  */
672 extern	void	chkpr		__PR((void));
673 extern	void	settmp		__PR((void));
674 extern	void	chkmail		__PR((void));
675 extern	void	setmail		__PR((unsigned char *));
676 #define	setmode		set_imode	/* Conflicts w. FreeBSD libc function */
677 extern	void	setmode		__PR((int prof));
678 extern	void	secpolicy_print __PR((int level, const char *msg));
679 
680 /*
681  * name.c
682  */
683 extern	int	syslook		__PR((unsigned char *w,
684 						const struct sysnod syswds[],
685 						int n));
686 extern	const struct sysnod *
687 		sysnlook	__PR((unsigned char *w,
688 					const struct sysnod syswds[], int n));
689 extern	void	setlist		__PR((struct argnod *arg, int xp));
690 extern	void	setname		__PR((unsigned char *nam, int xp));
691 extern	void	replace		__PR((unsigned char **a, unsigned char *v));
692 extern	void	dfault		__PR((struct namnod *n, unsigned char *v));
693 extern	void	assign		__PR((struct namnod *n, unsigned char *v));
694 extern	int	readvar		__PR((int namec, unsigned char **names));
695 extern	void	assnum		__PR((unsigned char **p, long i));
696 extern	unsigned char *make	__PR((unsigned char *v));
697 extern	struct namnod *lookup	__PR((unsigned char *nam));
698 extern	void	namscan		__PR((void (*fn)(struct namnod *n)));
699 extern	void	printfunc	__PR((struct namnod *n));
700 extern	void	printnam	__PR((struct namnod *n));
701 #ifdef	DO_LINENO
702 extern	unsigned char *linenoval __PR((void));
703 #endif
704 extern	void	printro		__PR((struct namnod *n));
705 extern	void	printpro	__PR((struct namnod *n));
706 extern	void	printexp	__PR((struct namnod *n));
707 extern	void	printpexp	__PR((struct namnod *n));
708 extern	void	printlocal	__PR((struct namnod *n));
709 extern	void	exportenv	__PR((struct namnod *n));
710 extern	void	deexportenv	__PR((struct namnod *n));
711 extern	void	pushval		__PR((struct namnod *n, void *t));
712 extern	void	popvars		__PR((void));
713 extern	void	poplvars	__PR((void));
714 extern	void	popval		__PR((struct namnod *n));
715 extern	void	setup_env	__PR((void));
716 extern	unsigned char **local_setenv __PR((int flg));
717 extern	unsigned char **get_envptr __PR((void));
718 extern	struct namnod *findnam	__PR((unsigned char *nam));
719 
720 #define	UNSET_FUNC	1
721 #define	UNSET_VAR	2
722 extern	void	unset_name	__PR((unsigned char *name, int uflg));
723 #ifdef INTERACTIVE
724 extern	char	*getcurenv	__PR((char *name));
725 extern	void	ev_insert	__PR((char *name));
726 #endif
727 
728 /*
729  * print.c
730  */
731 extern	void	prp		__PR((void));
732 extern	void	prs		__PR((unsigned char *as));
733 extern	void	prc		__PR((unsigned char c));
734 extern	void	prwc		__PR((wchar_t c));
735 extern	void	clock2tv	__PR((clock_t t, struct timeval	*tp));
736 extern	void	prt		__PR((long t));
737 extern	void	prtv		__PR((struct timeval *tp, int digs, int lf));
738 extern	void	prn		__PR((int n));
739 extern	void	itos		__PR((int n));
740 extern	void	sitos		__PR((int n));
741 extern	int	stoi		__PR((unsigned char *icp));
742 extern	int	stosi		__PR((unsigned char *icp));
743 extern	int	sltos		__PR((long n));
744 extern	int	slltos		__PR((Intmax_t n));
745 extern	int	ltos		__PR((long n));
746 
747 extern	void	flushb		__PR((void));
748 extern	void	unprs_buff	__PR((int));
749 extern	void	prc_buff	__PR((unsigned char c));
750 extern	int	prs_buff	__PR((unsigned char *s));
751 extern	void	prs_cntl	__PR((unsigned char *s));
752 extern	void	qprs_buff	__PR((unsigned char *s));
753 extern	void	prull_buff	__PR((UIntmax_t lc));
754 extern	void	prl_buff	__PR((long l));
755 extern	void	prn_buff	__PR((int n));
756 extern	int	setb		__PR((int fd));
757 extern	unsigned char *endb	__PR((void));
758 
759 
760 /*
761  * pwd.c
762  */
763 extern	int	lchdir		__PR((char *path));
764 extern	int	sh_hop_dirs	__PR((char *name, char **np));
765 extern	int	lstatat		__PR((char *name, struct stat *buf, int flag));
766 extern	void	cwd		__PR((unsigned char *dir,
767 					unsigned char *cwdbase));
768 extern	int	cwdrel2abs	__PR((void));
769 extern	unsigned char *cwdget	__PR((int cdflg));
770 extern	unsigned char *cwdset	__PR((void));
771 extern	void	cwdprint	__PR((int cdflg));
772 extern	void	ocwdnod		__PR((void));
773 extern	struct argnod *push_dir	__PR((unsigned char *name));
774 extern	struct argnod *pop_dir	__PR((int offset));
775 extern	void	init_dirs	__PR((void));
776 extern	int	pr_dirs		__PR((int minlen, int cdflg));
777 
778 /*
779  * service.c
780  */
781 extern	short		initio	__PR((struct ionod *iop, int save));
782 extern	unsigned char *simple	__PR((unsigned char *s));
783 extern	unsigned char *getpath	__PR((unsigned char *s));
784 extern	int		pathopen __PR((unsigned char *path,
785 						unsigned char *name));
786 extern	unsigned char *catpath	__PR((unsigned char *path,
787 						unsigned char *name));
788 extern	unsigned char *nextpath	__PR((unsigned char *path));
789 extern	void		execa	__PR((unsigned char *at[], short pos,
790 						int isvfork,
791 						unsigned char *av0));
792 extern	void		trim	__PR((unsigned char *at));
793 extern	void		trims	__PR((unsigned char *at));
794 extern	unsigned char *mactrim	__PR((unsigned char *at));
795 extern	unsigned char **scan	__PR((int argn));
796 extern	int 		getarg	__PR((struct comnod *ac));
797 
798 extern	void	suspacct	__PR((void));
799 extern	void	preacct		__PR((unsigned char *cmdadr));
800 extern	void	doacct		__PR((void));
801 
802 /*
803  * setbrk.c
804  */
805 extern	unsigned char *setbrk	__PR((int));
806 
807 /*
808  * stak.c
809  */
810 #define	free	shfree
811 
812 #define	GROWSTAK(a)	if ((a) >= brkend) \
813 				(a) = growstak(a);
814 /*
815  * "a" needs to be a char * object
816  */
817 #define	GROWSTAKL(a, l)	do { if (((a) + (l)) >= brkend) {	\
818 				(a) += (l);			\
819 				(a) = growstak(a);		\
820 				(a) -= (l);			\
821 			} } while (0);
822 #define	GROWSTAK2(a, o)	do { if ((a) >= brkend) {		\
823 				char *oa = (char *)(a);		\
824 				(a) = growstak(a);		\
825 				(o) += (char *)(a) - oa;	\
826 			} } while (0);
827 #define	GROWSTAKTOP()	do { if (staktop >= brkend)		\
828 				(void) growstak(staktop);	\
829 			} while (0);
830 #define	GROWSTAKTOPL(l)	do { if ((staktop + (l)) >= brkend) {	\
831 				staktop += (l);			\
832 				(void) growstak(staktop);	\
833 				staktop -= (l);			\
834 			} } while (0);
835 
836 extern	void		*alloc		__PR((size_t));
837 extern	void		free		__PR((void *ap));
838 extern	void		libc_free	__PR((void *ap));
839 extern	unsigned char *getstak		__PR((Intptr_t asize));
840 extern	unsigned char *locstak		__PR((void));
841 extern	unsigned char *growstak		__PR((unsigned char *newtop));
842 extern	unsigned char *savstak		__PR((void));
843 extern	unsigned char *endstak		__PR((unsigned char *argp));
844 extern	void		tdystak		__PR((unsigned char *x,
845 							struct ionod *iosav));
846 extern	void		stakchk		__PR((void));
847 extern	unsigned char *cpystak		__PR((unsigned char *));
848 extern	unsigned char *movstrstak	__PR((unsigned char *a,
849 							unsigned char *b));
850 extern	unsigned char *memcpystak	__PR((unsigned char *s1,
851 							unsigned char *s2,
852 							int n));
853 
854 /*
855  * strexpr.c
856  */
857 extern	Intmax_t	strexpr	__PR((unsigned char *arg, int *errp));
858 
859 /*
860  * string.c
861  */
862 extern	unsigned char *movstr	__PR((unsigned char *a, unsigned char *b));
863 extern	int		any	__PR((wchar_t c, unsigned char *s));
864 extern	int		anys	__PR((unsigned char *c, unsigned char *s));
865 extern	int		clen	__PR((unsigned char *c));
866 extern	int		cf	__PR((unsigned char *s1, unsigned char *s2));
867 extern	int		length	__PR((unsigned char *as));
868 extern	unsigned char *movstrn	__PR((unsigned char *a,
869 						unsigned char *b, int n));
870 
871 /*
872  * signames.c
873  */
874 #ifndef	HAVE_STRSIGNAL
875 extern	char	*strsignal	__PR((int sig));
876 #endif
877 #ifndef	HAVE_STR2SIG
878 extern	int	str2sig		__PR((const char *s, int *sigp));
879 #endif
880 #ifndef	HAVE_SIG2STR
881 extern	int	sig2str		__PR((int sig, char *s));
882 #endif
883 #ifndef	SIG2STR_MAX		/* From Solaris signal.h */
884 #define	SIG2STR_MAX	32
885 #endif
886 
887 /*
888  * test.c
889  */
890 extern	int	test		__PR((int argn, unsigned char *com[]));
891 #ifdef	DO_SYSATEXPR
892 extern	void	expr		__PR((int argn, unsigned char *com[]));
893 #endif
894 
895 /*
896  * ulimit.c
897  */
898 extern	void	sysulimit	__PR((int argc, unsigned char **argv));
899 
900 /*
901  * umask.c
902  */
903 extern	void	sysumask	__PR((int argc, char **argv));
904 
905 /*
906  * alias.c
907  */
908 #ifdef	DO_SYSALIAS
909 extern	void	sysalias	__PR((int argc, unsigned char **argv));
910 extern	void	sysunalias	__PR((int argc, unsigned char **argv));
911 #endif
912 
913 /*
914  * builtin.c
915  */
916 #ifdef	DO_SYSBUILTIN
917 extern	void	sysbuiltin	__PR((int argc, unsigned char **argv));
918 extern	struct sysnod2 *sh_findbuiltin	__PR((unsigned char *name));
919 #endif
920 
921 /*
922  * find.c
923  */
924 #ifdef	DO_SYSFIND
925 extern	int	sysfind		__PR((int argc, Uchar **argv, bosh_t *));
926 #endif
927 
928 /*
929  * optget.c
930  */
931 extern	void	optinit		__PR((struct optv *optv));
932 extern	int	optget		__PR((int argc, unsigned char **argv,
933 					struct optv *optv,
934 					const char *optstring));
935 extern	void	optbad		__PR((int argc, unsigned char **argv,
936 					struct optv *optv));
937 extern	int	optnext		__PR((int argc, unsigned char **argv,
938 					struct optv *optv,
939 					const char *optstring,
940 					const char *use));
941 extern	int	optskip		__PR((int argc, unsigned char **argv,
942 					const char *use));
943 
944 /*
945  * word.c
946  */
947 extern	int		word	__PR((void));
948 extern	unsigned char	*match_arith	__PR((unsigned char *argp));
949 extern	unsigned int	skipwc	__PR((void));
950 extern	unsigned int	nextwc	__PR((void));
951 extern	unsigned char	*readw	__PR((wchar_t d));
952 extern	unsigned int	readwc	__PR((void));
953 extern	int		isbinary __PR((struct fileblk *f));
954 extern	unsigned char	*do_tilde __PR((unsigned char *arg));
955 
956 /*
957  * xec.c
958  */
959 extern	int	execute		__PR((struct trenod *argt, int xflags,
960 						int errorflg,
961 						int *pf1, int *pf2));
962 extern	unsigned char *ps_macro	__PR((unsigned char *as, int perm));
963 extern	void	execexp		__PR((unsigned char *s, Intptr_t f,
964 						int xflags));
965 extern	int	callsh		__PR((int ac, char **av));
966 
967 
968 #define		_cf(a, b)	cf((unsigned char *)(a), (unsigned char *)(b))
969 #define		attrib(n, f)	((n)->namflg |= f)
970 #define		round(a, b)	(((Intptr_t)(((char *)(a)+b)-1))&~((b)-1))
971 #define		closepipe(x)	(close(x[INPIPE]), close(x[OTPIPE]))
972 #define		eq(a, b)	(_cf(a, b) == 0)
973 #undef		max
974 #define		max(a, b)	((a) > (b)?(a):(b))
975 #define		assert(x)
976 #define		_gettext(s)	(unsigned char *)gettext(s)
977 
978 /*
979  * Exit shell or longjmp before next prompt.
980  *
981  * macros using failed_real(). Only s2 is gettext'd with both functions.
982  *
983  * Called from "fatal errors", from locations where either a real exit() is
984  * expected or from an interactive command where a longjmp() to the next prompt
985  * is expected.
986  */
987 #define		failed(s1, s2)		failed_real(ERROR, s1, s2, NULL)
988 #define		failedx(e, s1, s2)	failed_real(e, s1, s2, NULL)
989 #define		bfailed(s1, s2, s3)	failed_real(ERROR, s1, s2, s3)
990 #define		bfailedx(e, s1, s2, s3)	failed_real(e, s1, s2, s3)
991 
992 /*
993  * Prepare non-zero $? for this command.
994  *
995  * macros using failure_real(). s1 and s2 is gettext'd with gfailure(), but
996  * only s2 is gettext'd with failure().
997  *
998  * From a normal error that usually does not cause an exit() of the shell.
999  * Except when "set -e" has been issued, we just set up $? and return.
1000  */
1001 #define		failure(s1, s2)		failure_real(ERROR, s1, s2, NULL, 0)
1002 #define		failurex(e, s1, s2)	failure_real(e, s1, s2, NULL, 0)
1003 #define		bfailure(s1, s2, s3)	failure_real(ERROR, s1, s2, s3, 0)
1004 #define		bfailurex(e, s1, s2, s3) failure_real(e, s1, s2, s3, 0)
1005 #define		gfailure(s1, s2)	failure_real(ERROR, s1, s2, NULL, 1)
1006 #define		gfailurex(e, s1, s2)	failure_real(e, s1, s2, NULL, 1)
1007 #define		gbfailure(s1, s2, s3)	failure_real(ERROR, s1, s2, s3, 1)
1008 #define		gbfailurex(e, s1, s2, s3) failure_real(e, s1, s2, s3, 1)
1009 
1010 /*
1011  * Failure macros for builtin commands that historically aborted scripts
1012  * in case of syntax errors or "fatal errors".
1013  *
1014  * Should we make this runtime settable?
1015  */
1016 #ifdef	DO_POSIX_FAILURE
1017 #define		Failure(s1, s2)		failure(s1, s2)
1018 #define		Failurex(e, s1, s2)	failurex(e, s1, s2)
1019 #define		BFailure(s1, s2, s3)	bfailure(s1, s2, s3)
1020 #define		BFailurex(e, s1, s2, s3) bfailurex(e, s1, s2, s3)
1021 #define		Error(s1)		gfailure(UC s1, NULL)
1022 #else
1023 #define		Failure(s1, s2)		failed(s1, s2)
1024 #define		Failurex(e, s1, s2)	failedx(e, s1, s2)
1025 #define		BFailure(s1, s2, s3)	bfailed(s1, s2, s3)
1026 #define		BFailurex(e, s1, s2, s3) bfailedx(e, s1, s2, s3)
1027 #define		Error(s1)		error(s1)
1028 #endif
1029 
1030 /* temp files and io */
1031 extern int		output;
1032 extern int		ioset;
1033 extern struct ionod	*iotemp; /* files to be deleted sometime */
1034 extern struct ionod	*xiotemp; /* limit for files to be deleted sometime */
1035 extern struct ionod	*fiotemp; /* function files to be deleted sometime */
1036 extern struct ionod	*iopend; /* documents waiting to be read at NL */
1037 extern struct fdsave	fdmap[];
1038 extern int savpipe;
1039 
1040 /* substitution */
1041 extern int		dolc;
1042 extern unsigned char	**dolv;
1043 extern struct dolnod	*argfor;
1044 extern struct argnod	*gchain;
1045 
1046 /* stak stuff */
1047 #include		"stak.h"
1048 
1049 /*
1050  * If non-ANSI C, make const go away.  We bring it back
1051  * at the end of the file to avoid side-effects.
1052  */
1053 #ifndef __STDC__
1054 #define	const
1055 #endif
1056 
1057 /* string constants */
1058 extern const char		atline[];
1059 extern const char		readmsg[];
1060 extern const char		selectmsg[];
1061 extern const char		colon[];
1062 extern const char		minus[];
1063 extern const char		nullstr[];
1064 extern const char		sptbnl[];
1065 extern const char		unexpected[];
1066 extern const char		endoffile[];
1067 extern const char		synmsg[];
1068 
1069 /* name tree and words */
1070 extern const struct sysnod	reserved[];
1071 extern const int		no_reserved;
1072 extern const struct sysnod	commands[];
1073 extern const int		no_commands;
1074 extern const struct sysnod	test_ops[];
1075 extern const int		no_test_ops;
1076 
1077 extern int			wdval;
1078 extern int			wdnum;
1079 extern int			fndef;
1080 extern int			nohash;
1081 extern struct argnod		*wdarg;
1082 extern int			wdset;
1083 extern BOOL			reserv;
1084 
1085 /* prompting */
1086 extern const char		stdprompt[];
1087 extern const char		supprompt[];
1088 extern const char		profile[];
1089 extern const char		sysprofile[];
1090 extern const char		rcfile[];
1091 extern const char		sysrcfile[];
1092 extern const char		globalname[];
1093 extern const char		localname[];
1094 extern const char		fcedit[];
1095 
1096 /* locale testing */
1097 extern const char		localedir[];
1098 extern int			localedir_exists;
1099 
1100 /* built in names */
1101 extern struct namnod		fngnod;
1102 extern struct namnod		cdpnod;
1103 extern struct namnod		envnod;
1104 extern struct namnod		fcenod;
1105 extern struct namnod		ifsnod;
1106 extern struct namnod		homenod;
1107 extern struct namnod		pwdnod;
1108 extern struct namnod		opwdnod;
1109 extern struct namnod		linenonod;
1110 extern struct namnod		mailnod;
1111 extern struct namnod		pathnod;
1112 extern struct namnod		ppidnod;
1113 extern struct namnod		ps1nod;
1114 extern struct namnod		ps2nod;
1115 extern struct namnod		ps3nod;
1116 extern struct namnod		ps4nod;
1117 extern struct namnod		mchknod;
1118 extern struct namnod		repnod;
1119 extern struct namnod		acctnod;
1120 extern struct namnod		mailpnod;
1121 extern struct namnod		timefmtnod;
1122 extern struct namnod		*optindnodep;
1123 
1124 /* special names */
1125 extern unsigned char		flagadr[];
1126 extern unsigned char		*pcsadr;
1127 extern unsigned char		*pidadr;
1128 extern unsigned char		*cmdadr;
1129 
1130 /* names always present */
1131 extern const char		defpath[];
1132 extern const char		defppath[];
1133 extern const char		linenoname[];
1134 extern const char		mailname[];
1135 extern const char		homename[];
1136 extern const char		pathname[];
1137 extern const char		ppidname[];
1138 extern const char		cdpname[];
1139 extern const char		envname[];
1140 extern const char		fcename[];
1141 extern const char		ifsname[];
1142 extern const char		ps1name[];
1143 extern const char		ps2name[];
1144 extern const char		ps3name[];
1145 extern const char		ps4name[];
1146 extern const char		mchkname[];
1147 extern const char		opwdname[];
1148 extern const char		pwdname[];
1149 extern const char		repname[];
1150 extern const char		acctname[];
1151 extern const char		mailpname[];
1152 extern const char		timefmtname[];
1153 
1154 /* transput */
1155 extern unsigned char		tmpout[];
1156 extern int 			tmpout_offset;
1157 extern unsigned int		serial;
1158 
1159 /*
1160  * allow plenty of room for size for temp file name:
1161  * "/tmp/sh"(7) + <pid> (<=6) + <unsigned int #> (<=10) + \0 (1)
1162  */
1163 #define		TMPOUTSZ	32
1164 
1165 extern struct fileblk	*standin;
1166 
1167 #define		input		(standin->fdes)
1168 #define		eof		(standin->feof)
1169 
1170 #define	peekc	peekc_		/* AIX has a hidden peekc() in libc */
1171 extern int			peekc;
1172 extern int			peekn;
1173 extern unsigned char		*comdiv;
1174 extern const char		devnull[];
1175 
1176 /*
1177  * flags
1178  */
1179 #define		noexec		01		/* set -n noexec */
1180 #define		intflg		02		/* set -i interactive */
1181 #define		prompt		04
1182 #define		setflg		010		/* set -u nounset */
1183 #define		errflg		020		/* set -e errexit */
1184 #define		ttyflg		040		/* in + out is a tty */
1185 #define		forked		0100		/* *forked child */
1186 #define		oneflg		0200		/* set -t onecmd */
1187 #define		rshflg		0400		/* set -r restrictive */
1188 #define		subsh		01000		/* Is a subshell */
1189 #define		stdflg		02000		/* set -s stdin */
1190 #define		STDFLG		's'
1191 #define		execpr		04000		/* set -x xtrace */
1192 #define		readpr		010000		/* set -v verbose */
1193 #define		keyflg		020000		/* set -k keyword */
1194 #define		hashflg		040000		/* set -h hashall */
1195 #define		vforked		0100000		/* A vforked child */
1196 #define		nofngflg	0200000		/* set -f noglob */
1197 #define		exportflg	0400000		/* set -a allexport */
1198 #define		monitorflg	01000000	/* set -m monitor */
1199 #define		jcflg		02000000	/* Do jobcontrol */
1200 #define		privflg		04000000	/* set -p Keep privs */
1201 #define		forcexit	010000000	/* Terminate shell */
1202 #define		jcoff		020000000	/* Tmp jobcontrol off */
1203 #define		pfshflg		040000000	/* set -P pfexec() support */
1204 #define		ppath		0100000000	/* Use POSIX default PATH */
1205 #define		noexit		0200000000	/* Inhibit exit from builtins */
1206 #define		nofuncs		0400000000	/* Inhibit functions */
1207 
1208 /*
1209  * We currently support up to 4x 30 flag bits, but we currently only use
1210  * 2x 30 flag bits.
1211  * If we start to use 'fl3' and 'fl4', we need to ass support to args.c
1212  */
1213 #define		fl2		010000000000	/* If in flagval: see flags2 */
1214 #define		fl3		020000000000	/* If in flagval: see flags3 */
1215 #define		fl4		(fl2 | fl3)	/* If in flagval: see flags4 */
1216 
1217 /*
1218  * flags2
1219  */
1220 #define		fdpipeflg	01		/* e.g. 2| pipe from stderr */
1221 #define		timeflg		02		/* set -o time		*/
1222 #define		systime		04		/* "time pipeline"	*/
1223 #define		fullexitcodeflg	010		/* set -o fullexitcode	*/
1224 #define		hashcmdsflg	020		/* set -o hashcmds	*/
1225 #define		hostpromptflg	040		/* set -o hostprompt	*/
1226 #define		noclobberflg	0100		/* set -o noclobber	*/
1227 #define		bgniceflg	0200		/* set -o bgnice	*/
1228 #define		ignoreeofflg	0400		/* set -o ignoreeof	*/
1229 #define		notifyflg	01000		/* set -o notify / -b	*/
1230 #define		versflg		02000		/* set -V (print version) */
1231 #define		globalaliasflg	04000		/* set -o globalaliases */
1232 #define		localaliasflg	010000		/* set -o localaliases  */
1233 #define		aliasownerflg	020000		/* set -o aliasowner=   */
1234 #define		viflg		040000		/* set -o vi VI cmdln. edit  */
1235 #define		vedflg		0100000		/* set -o ved VED cmdln. edit */
1236 #define		posixflg	0200000		/* set -o posix		*/
1237 #define		promptcmdsubst	0400000		/* set -o promptcmdsubst */
1238 #define		globskipdot	01000000	/* set -o globskipdot	*/
1239 
1240 extern unsigned long		flags;		/* Flags for set(1) and more */
1241 extern unsigned long		flags2;		/* Second set of flags */
1242 extern int			exflag;		/* Use _exit(), not exit() */
1243 extern int			rwait;		/* flags read waiting */
1244 #ifdef	DO_POSIX_SET
1245 extern int			dashdash;	/* flags set -- encountered */
1246 #endif
1247 
1248 /* error exits from various parts of shell */
1249 extern jmp_buf			subshell;
1250 extern jmp_buf			errshell;
1251 extern jmps_t			*dotshell;
1252 
1253 /* fault handling */
1254 extern unsigned			brkincr;
1255 #define		MINTRAP		0
1256 #define		EXITTRAP	0		/* trap 0 == trap EXIT	*/
1257 #ifdef	DO_ERR_TRAP
1258 #define		MAXTRAP		(NSIG+1)
1259 #define		ERRTRAP		(MAXTRAP-1)
1260 #else
1261 #define		MAXTRAP		NSIG
1262 #endif
1263 #define		MAX_SIG		NSIG
1264 
1265 #define		TRAPSET		2		/* Mark incoming signal/fault */
1266 #define		SIGSET		4		/* Mark fault w. no trapcmd */
1267 #define		SIGMOD		8		/* Signal with trap(1) set */
1268 #define		SIGIGN		16		/* Signal is ignored	*/
1269 #define		SIGCLR		32		/* From oldsigs() w. vfork() */
1270 #define		SIGINP		64		/* Signal in line editor inp */
1271 
1272 extern BOOL			trapnote;
1273 extern int			traprecurse;
1274 extern int			trapsig;
1275 extern unsigned char		*trapcom[];
1276 
1277 /* name tree and words */
1278 #ifdef	HAVE_ENVIRON_DEF
1279 /*
1280  * Warning: HP-UX and Linux have "extern char **environ;" in unistd.h
1281  * We should not use our own definitions here.
1282  */
1283 #else
1284 extern char			**environ;
1285 #endif
1286 
1287 #define		NUMBUFLEN	21	/* big enough for 64 bits */
1288 #if	NUMBUFLEN < SIG2STR_MAX
1289 #undef		NUMBUFLEN
1290 #define		NUMBUFLEN	(SIG2STR_MAX-1)
1291 #endif
1292 extern unsigned char		numbuf[];
1293 extern const char		export[];
1294 extern const char		duperr[];
1295 extern const char		readonly[];
1296 
1297 /* execflgs */
1298 extern struct excode		ex;
1299 extern struct excode		retex;
1300 #ifdef	DO_DOL_SLASH
1301 extern	int			*excausep;
1302 #endif
1303 extern int			exitval;
1304 extern int			retval;
1305 extern BOOL			execbrk;
1306 extern BOOL			dotbrk;
1307 extern int			loopcnt;
1308 extern int			breakcnt;
1309 extern int			funcnt;
1310 extern int			dotcnt;
1311 extern void			*localp;
1312 extern int			localcnt;
1313 extern int			tried_to_exit;
1314 
1315 /* fault */
1316 extern int			*intrptr;
1317 
1318 /* messages */
1319 extern const char		mailmsg[];
1320 extern const char		coredump[];
1321 extern const char		badopt[];
1322 extern const char		emptystack[];
1323 extern const char		badparam[];
1324 extern const char		unset[];
1325 extern const char		badsub[];
1326 extern const char		nospace[];
1327 extern const char		nostack[];
1328 extern const char		notfound[];
1329 extern const char		badtrap[];
1330 extern const char		baddir[];
1331 extern const char		badoff[];
1332 extern const char		badshift[];
1333 extern const char		restricted[];
1334 extern const char		execpmsg[];
1335 extern const char		notid[];
1336 extern const char 		badulimit[];
1337 extern const char 		ulimit[];
1338 extern const char		wtfailed[];
1339 extern const char		badcreate[];
1340 extern const char		eclobber[];
1341 extern const char		nofork[];
1342 extern const char		noswap[];
1343 extern const char		piperr[];
1344 extern const char		badopen[];
1345 extern const char		badnum[];
1346 extern const char		badsig[];
1347 extern const char		badid[];
1348 extern const char		arglist[];
1349 extern const char		txtbsy[];
1350 extern const char		toobig[];
1351 extern const char		badexec[];
1352 extern const char		badfile[];
1353 extern const char		badreturn[];
1354 extern const char		badexport[];
1355 extern const char		badunset[];
1356 extern const char		nohome[];
1357 extern const char		badperm[];
1358 extern const char		mssgargn[];
1359 extern const char		toomanyargs[];
1360 extern const char		libacc[];
1361 extern const char		libbad[];
1362 extern const char		libscn[];
1363 extern const char		libmax[];
1364 extern const char		emultihop[];
1365 extern const char		nulldir[];
1366 extern const char		enotdir[];
1367 extern const char		eisdir[];
1368 extern const char		enoent[];
1369 extern const char		eacces[];
1370 extern const char		enolink[];
1371 extern const char		exited[];
1372 extern const char		running[];
1373 extern const char		ambiguous[];
1374 extern const char		nosuchjob[];
1375 extern const char		nosuchpid[];
1376 extern const char		nosuchpgid[];
1377 extern const char		usage[];
1378 extern const char		nojc[];
1379 extern const char		killuse[];
1380 extern const char		jobsuse[];
1381 extern const char		aliasuse[];
1382 extern const char		unaliasuse[];
1383 extern const char		repuse[];
1384 extern const char		builtinuse[];
1385 extern const char		stopuse[];
1386 extern const char		trapuse[];
1387 extern const char		ulimuse[];
1388 extern const char		limuse[];
1389 extern const char		nocurjob[];
1390 extern const char		loginsh[];
1391 extern const char		jobsstopped[];
1392 extern const char		jobsrunning[];
1393 extern const char		nlorsemi[];
1394 extern const char		signalnum[];
1395 extern const char		badpwd[];
1396 extern const char		badlocale[];
1397 extern const char		nobracket[];
1398 extern const char		noparen[];
1399 extern const char		noarg[];
1400 extern const char		unimplemented[];
1401 extern const char		divzero[];
1402 
1403 /*
1404  * 'builtin' error messages
1405  */
1406 extern const char		btest[];
1407 extern const char		badop[];
1408 extern const char		badumask[];
1409 
1410 /*
1411  * Shell name
1412  */
1413 extern const unsigned char	shname[];
1414 extern	    unsigned char	*shvers;
1415 
1416 /*	fork constant	*/
1417 #define		FORKLIM 	32
1418 
1419 extern address			end[];
1420 
1421 extern int			eflag;
1422 extern int			ucb_builtins;
1423 
1424 /*
1425  * Find out if it is time to go away.
1426  * `trapnote' is set to SIGSET when fault is seen and
1427  * no trap has been set.
1428  */
1429 #define		sigchk()	if (trapnote & SIGSET)	{		\
1430 					exval_sig();			\
1431 					if (!traprecurse) {		\
1432 						exitsh(exitval ?	\
1433 						    exitval : SIGFAIL); \
1434 					}				\
1435 				}
1436 
1437 #define		exitset()	(retex = ex, retval = exitval)
1438 
1439 /* Multibyte characters */
1440 unsigned char *readw	__PR((wchar_t));
1441 #define	MULTI_BYTE_MAX MB_LEN_MAX
1442 
1443 /*
1444  * Make sure we have a definition for this.  If not, take a very conservative
1445  * guess.
1446  * POSIX requires the max pathname component lenght to be defined in limits.h
1447  * If variable, it may be undefined. If undefined, there should be
1448  * a definition for _POSIX_NAME_MAX in limits.h or in unistd.h
1449  * As _POSIX_NAME_MAX is defined to 14, we cannot use it.
1450  */
1451 #ifndef NAME_MAX
1452 #ifdef FILENAME_MAX
1453 #define	NAME_MAX	FILENAME_MAX
1454 #else
1455 #define	NAME_MAX	256
1456 #endif
1457 #endif
1458 
1459 #ifndef PATH_MAX
1460 #ifdef FILENAME_MAX
1461 #define	PATH_MAX	FILENAME_MAX
1462 #else
1463 #define	PATH_MAX	1024
1464 #endif
1465 #endif
1466 
1467 #if	defined(HAVE_GETPGID) || defined(HAVE_SETPGID)
1468 #define	POSIXJOBS
1469 #endif
1470 
1471 #if	!defined(HAVE_GETPGID) && defined(HAVE_BSD_GETPGRP)
1472 #define	getpgid	getpgrp
1473 #define	getsid	getpgrp
1474 #endif
1475 #if	!defined(HAVE_GETSID) && defined(HAVE_BSD_GETPGRP)
1476 #define	getsid	getpgrp
1477 #endif
1478 #if	!defined(HAVE_SETPGID) && defined(HAVE_BSD_SETPGRP)
1479 #define	setpgid	setpgrp
1480 #endif
1481 #if	!defined(HAVE_SETSID) && defined(HAVE_BSD_GETPGRP)
1482 #define	setsid	setpgrp(getpid())
1483 #endif
1484 
1485 #if	!defined(HAVE_GETPGID) && !defined(HAVE_BSD_GETPGRP)
1486 /*
1487  * FreeBSD (anything that is BSD-4.4 derived)
1488  * does not conform to POSIX and is not old BSD conforming either.
1489  * Note that this seems to have been changed between 1995 and 2000
1490  *
1491  * getpgrp()/setpgrp() are not POSIX on BSD-4.4
1492  *
1493  * setpgrp() is old BSD compliant,
1494  * getpgrp() is not BSD compliant but it is POSIX compliant
1495  *
1496  * setpgid() is OK (POSIX compliant)
1497  * getpgid() is missing!
1498  *
1499  * The builtin command 'pgrp' will not work correctly for this reason.
1500  *
1501  *	BSD:
1502  *
1503  *	getpgrp(pid)		-> pgid for pid
1504  *	setpgrp(pid, pgid)	-> set pgid of pid
1505  *
1506  *	POSIX:
1507  *
1508  *	getpgid(pid)		-> pgid for pid
1509  *	setpgid(pid, pgid)	-> set pgid of pid
1510  *	getpgrp(void)		-> pgid for $$
1511  *	setpgrp(void)		-> setpgid(0,0)
1512  *
1513  *	4.4-BSD:
1514  *
1515  *	getpgid(pid)		-> is missing!
1516  *	setpgid(pid, pgid)	-> set pgid of pid
1517  *	getpgrp(void)		-> ????
1518  *	setpgrp(pid, pgid)	-> set pgid of pid
1519  */
1520 #define	getpgid(a)	getpgrp()
1521 #endif
1522 
1523 #if	!defined(HAVE_GETSID) && !defined(HAVE_BSD_GETPGRP)
1524 #define	getsid	getpgid
1525 #endif
1526 
1527 #ifdef	SCHILY_INCLUDES
1528 
1529 #ifdef	USE_BYTES
1530 #define	memset(s, c, n)		fillbytes(s, n, c)
1531 #define	memchr(s, c, n)		findbytes(s, n, c)
1532 #define	memcpy(s1, s2, n)	movebytes(s2, s1, n)
1533 #define	memmove(s1, s2, n)	movebytes(s2, s1, n)
1534 #endif
1535 
1536 #if !defined(HAVE_MEMSET) && !defined(memset)
1537 #define	memset(s, c, n)		fillbytes(s, n, c)
1538 #endif
1539 #if !defined(HAVE_MEMCHR) && !defined(memchr)
1540 #define	memchr(s, c, n)		findbytes(s, n, c)
1541 #endif
1542 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
1543 #define	memcpy(s1, s2, n)	movebytes(s2, s1, n)
1544 #endif
1545 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
1546 #define	memmove(s1, s2, n)	movebytes(s2, s1, n)
1547 #endif
1548 
1549 /*
1550  * <schily/libport.h> is needed for various stuff that may be missing on the
1551  * current platform and that is implemented in libschily for portability.
1552  * If we are missing memset(), memchr(), memcpy() or memmove(), we use the
1553  * *bytes() functions from libschily that have prototypes in <schily/schily.h>.
1554  * There are several historic name conflicts in libschily and the Bourne Shell
1555  * that we need to take care of.
1556  */
1557 #define	BOOL	JS_BOOL			/* Bourne Shell uses other BOOL size */
1558 #undef	peekc				/* First remove AIX cludge	*/
1559 #define	peekc	js_peekc		/* The Bourne Shell has int peekc */
1560 #define	error	js_error		/* Bourne Shell has own error()	*/
1561 #define	flush	js_flush		/* Bourne Shell has own flush()	*/
1562 #define	getperm	js_getperm		/* Bourne Shell modified getperm() */
1563 #define	eaccess	__no_eaccess__		/* libgen.h / -lgen contain eaccess() */
1564 #include	<schily/schily.h>	/* Includes <schily/libport.h>	*/
1565 #undef	eaccess				/* No longer needed		*/
1566 #undef	BOOL				/* Back to BOOL from Bourne Shell */
1567 #undef	peekc				/* Remove schily/schily.h cludge */
1568 #define	peekc	peekc_			/* AIX has a hidden peekc() in libc */
1569 #undef	error				/* Reestablish Bourne Shell error() */
1570 #undef	flush				/* Reestablish Bourne Shell flush() */
1571 #undef	getperm				/* Reestablish Bourne Shell getperm() */
1572 
1573 #endif	/* SCHILY_INCLUDES */
1574 
1575 #ifdef	__cplusplus
1576 }
1577 #endif
1578 
1579 #endif	/* _DEFS_H */
1580