/*
 * etsh - an   enhanced port of the Version 6 (V6) Thompson shell
 *  tsh - an unenhanced port of the Version 6 (V6) Thompson shell
 */
/*-
 * Copyright (c) 2004-2019
 *	Jeffrey Allen Neitzel <jan (at) etsh (dot) nl>.
 *	All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JEFFREY ALLEN NEITZEL ``AS IS'', AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL JEFFREY ALLEN NEITZEL BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *	@(#)$Id: defs.h,v 1.10 2019/03/18 05:30:32 jneitzel Exp $
 */
/*
 * The following license covers the SIZE_T_EOVERFLOW(num, siz) macro.  My
 * license, see Jeffrey Allen Neitzel <jan (at) etsh (dot) nl>, on lines
 * 5 - 31 of this file, covers it as well.
 */
/*-
 * Copyright (c) 1980, 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * the American National Standards Committee X3, on Information
 * Processing Systems.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$OpenBSD: malloc.3,v 1.122 2018/12/05 17:11:59 schwarze Exp $
 */

#ifndef	DEFS_H
#define	DEFS_H

/*
 * required header files
 */
#include "config.h"

/*
 * S_SPLINT_S guard needed by splint(1)
 */
#ifndef	S_SPLINT_S
#include <sys/stat.h>
#endif
#include <sys/types.h>
#ifndef	S_SPLINT_S
#include <sys/uio.h>
#include <sys/wait.h>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif

#ifdef	PATH_MAX
#if PATH_MAX > 1024
#define	PATHMAX		1024
#else
#define	PATHMAX		PATH_MAX
#endif
#else
#define	PATHMAX		1024
#endif

#ifdef	ARG_MAX
#define	GAVMAX		ARG_MAX
#else
#define	GAVMAX		262144
#endif
#define	GAVMULT		2U	/* base GAVNEW reallocation multiplier        */
#define	GAVNEW		128U	/* base # of new arguments per gav allocation */

#ifdef	_POSIX_OPEN_MAX
#define	FDFREEMIN	_POSIX_OPEN_MAX
#else
#define	FDFREEMIN	20	/* Value is the same as _POSIX_OPEN_MAX.    */
#endif
#define	FDFREEMAX	1024	/* Arbitrary maximum value for fd_free().   */

#define	SOURCEMAX	64	/* Maximum # of nested source invocations   */
#define	ALIASMAX	2	/* Maximum # of nested aliases              */

#define	_LINESIZ	2072		/* LINSIZ was 1000 in orig V6 Shell */
#define	_LINEMAX	(_LINESIZ - 1)
#define	LINEMAX		(_LINEMAX - 6)	/* equal to 2065                    */
#define	_WORDSIZ	1036		/* ARGSIZ was 50   ...              */
#define	_WORDMAX	(_WORDSIZ - 1)
#define	WORDMAX		(_WORDMAX - 6)	/* equal to 1029                    */
#define	TREEMAX		512		/* TRESIZ was 100  ...              */

#define	ASCII		0177
#define	QUOTE		0200

#define	SBUFMM(m)	((32 * (m)) + 1)/* small buffer max multiplier       */
#define	DOLMAX		SBUFMM(1)	/* used by etsh(1) and tsh(1) shells */
#define	LABELMAX	SBUFMM(2)	/* used by goto(1) utility           */

/*
 * Following standard conventions, file descriptors 0, 1, and 2 are used
 * for standard input, standard output, and standard error respectively.
 */
#define	FD0		STDIN_FILENO
#define	FD1		STDOUT_FILENO
#define	FD2		STDERR_FILENO

#define	F_GZ		1		/* `-s'  primary for if(1) utility  */
#define	F_OT		2		/* `-ot' ...                        */
#define	F_NT		3		/* `-nt' ...                        */
#define	F_EF		4		/* `-ef' ...                        */

#define	DOLSUB		true
#define	FORKED		true
#define	RETERR		true

#define	EQUAL(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)
#define	IS_DIGIT(d, c)	((d) >= 0 && (d) <= 9 && "0123456789"[(d) % 10] == (c))
#define	IS_LIBEXEC(e)	(e(cmd, "fd2") || e(cmd, "goto") || e(cmd, "if"))

/*
 * The following macro is derived from the malloc(3) manual page,
 * /usr/src/lib/libc/stdlib/malloc.3, on OpenBSD 6.4-current.
 */
#define	SIZE_T_EOVERFLOW(num, siz)	\
if ((siz) != 0 && (num) > SIZE_MAX / (siz)) {\
	sh_err(ESTATUS, FMT2S, getmyname(), strerror(EOVERFLOW));\
	/*NOTREACHED*/\
}

/*
 * tsh(1) does not search for glob, fd2, goto, or if via PATH.
 * It expects to find these utilities in LIBEXECDIRTSH which is set
 * at build time, defaulting to $(PREFIX)/libexec/$(ETSH_VERSION)/tsh.
 * The glob(1), fd2(1), and if(1) utilities expect the same.
 */
#define	GLOB_PATH	LIBEXECDIRTSH/**/"/glob"
#define	FD2_PATH	LIBEXECDIRTSH/**/"/fd2"
#define	GOTO_PATH	LIBEXECDIRTSH/**/"/goto"
#define	IF_PATH		LIBEXECDIRTSH/**/"/if"

/*
 * special character literals
 */
#define	BANG		'!'
#define	BEL		'\a'
#define	COLON		':'
#define	DOLLAR		'$'
#define	DOT		'.'
#define	EOL		'\n'
#define	EOS		'\0'
#define	HASH		'#'
#define	SLASH		'/'
#define	SPC		' '
#define	TAB		'\t'
#define	BQUOT		'\\'
#define	DQUOT		'"'
#define	SQUOT		'\''

#define	LPARENTHESIS	'('
#define	RPARENTHESIS	')'
#define	SEMICOLON	';'
#define	AMPERSAND	'&'
#define	VERTICALBAR	'|'
#define	CARET		'^'
#define	LESSTHAN	'<'
#define	GREATERTHAN	'>'

#define	ASTERISK	'*'
#define	QUESTION	'?'
#define	LBRACKET	'['
#define	RBRACKET	']'
#define	HYPHEN		'-'

/*
 * special string literals
 */
#define	BANCHARS	" \t$\"'\\();&|^<>*?[\n"
#define	EOC		";&\n"
#define	GLOBCHARS	"*?["
#define	QUOTPACK	"\"'"
#define	REDIRERR	"(<>"
#define	WORDPACK	" \t\"'();&|^<>\n"

/*
 * typedefs and related macros
 */
/*
 * The system on which we build must have u_char and u_int;
 * otherwise, we handle it ourselves.
 */
#if HAVE_BSD_STYLE == 0
typedef	unsigned char	u_char;
typedef	unsigned int	u_int;
#endif
#define	U_CHAR(c)	((u_char)(c))
#define	U_CPTR(p)	((u_char *)(p))

#ifndef	lint
#ifndef	ETSH_ATTR
# if __GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4
#  define	ETSH_ATTR	__attribute__((__used__))
# elif defined(__GNUC__)
#  define	ETSH_ATTR	__attribute__((__unused__))
# else
#  define	ETSH_ATTR	/* nothing */
# endif
#endif	/* !ETSH_ATTR */
#endif	/* !lint */

#endif	/* !DEFS_H */
