/*
 * This file was generated by the mkinit program.
 */

#include "shell.h"
#include "mystring.h"
#include "init.h"
#include "eval.h"
#include "expand.h"
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "input.h"
#include "error.h"
#include "syntax.h"
#include "parser.h"
#include "redir.h"
#include "memalloc.h"
#include "trap.h"
#include "output.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "cd.h"
#include "var.h"



#undef  ATABSIZE
#define ATABSIZE 39
#undef  ARITH_MAX_PREC
#define ARITH_MAX_PREC 8
#undef  CD_PHYSICAL
#define CD_PHYSICAL 1
#undef  CD_PRINT
#define CD_PRINT 2
#undef  CMDTABLESIZE
#define CMDTABLESIZE 31		/* should be prime */
#undef  ARB
#define ARB 1			/* actual size determined at run time */
#undef  RMESCAPE_ALLOC
#define RMESCAPE_ALLOC	0x1	/* Allocate a new string */
#undef  RMESCAPE_GLOB
#define RMESCAPE_GLOB	0x2	/* Add backslashes for glob */
#undef  RMESCAPE_GROW
#define RMESCAPE_GROW	0x8	/* Grow strings instead of stalloc */
#undef  RMESCAPE_HEAP
#define RMESCAPE_HEAP	0x10	/* Malloc strings instead of stalloc */
#undef  QUOTES_ESC
#define QUOTES_ESC	(EXP_FULL | EXP_CASE)
#undef  GLOB_ALTDIRFUNC
#define GLOB_ALTDIRFUNC 0
#undef  MAXHISTLOOPS
#define MAXHISTLOOPS	4	/* max recursions through fc */
#undef  DEFEDITOR
#define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
#undef  editing
#define editing (Eflag || Vflag)
#undef  IBUFSIZ
#define IBUFSIZ (BUFSIZ + PUNGETC_MAX + 1)
#undef  CUR_DELETE
#define CUR_DELETE 2
#undef  CUR_RUNNING
#define CUR_RUNNING 1
#undef  CUR_STOPPED
#define CUR_STOPPED 0
#undef  DOWAIT_NONBLOCK
#define DOWAIT_NONBLOCK 0
#undef  DOWAIT_BLOCK
#define DOWAIT_BLOCK 1
#undef  DOWAIT_WAITCMD
#define DOWAIT_WAITCMD 2
#undef  DOWAIT_WAITCMD_ALL
#define DOWAIT_WAITCMD_ALL 4
#undef  MAXMBOXES
#define MAXMBOXES 10
#undef  PROFILE
#define PROFILE 0
#undef  MINSIZE
#define MINSIZE SHELL_ALIGN(504)
#undef  DEFINE_OPTIONS
#define DEFINE_OPTIONS
#undef  FAKEEOFMARK
#define FAKEEOFMARK (char *)1
#undef  EMPTY
#define EMPTY -2		/* marks an unused slot in redirtab */
#undef  CLOSED
#define CLOSED -1		/* fd opened for redir needs to be closed */
#undef  S_DFL
#define S_DFL 1			/* default signal handling (SIG_DFL) */
#undef  S_CATCH
#define S_CATCH 2		/* signal is caught */
#undef  S_IGN
#define S_IGN 3			/* signal is ignored (SIG_IGN) */
#undef  S_HARD_IGN
#define S_HARD_IGN 4		/* signal is ignored permenantly */
#undef  S_RESET
#define S_RESET 5		/* temporary - to reset a hard ignored sig */
#undef  OUTBUFSIZ
#define OUTBUFSIZ BUFSIZ
#undef  MEM_OUT
#define MEM_OUT -3		/* output to dynamically allocated memory */
#undef  SKIP1
#define SKIP1	"#-+ 0"
#undef  SKIP2
#define SKIP2	"*0123456789"
#undef  isalnum
#define isalnum _isalnum
#undef  iscntrl
#define iscntrl _iscntrl
#undef  islower
#define islower _islower
#undef  isspace
#define isspace _isspace
#undef  isalpha
#define isalpha _isalpha
#undef  isdigit
#define isdigit _isdigit
#undef  isprint
#define isprint _isprint
#undef  isupper
#define isupper _isupper
#undef  isblank
#define isblank _isblank
#undef  isgraph
#define isgraph _isgraph
#undef  ispunct
#define ispunct _ispunct
#undef  isxdigit
#define isxdigit _isxdigit
#undef  VTABSIZE
#define VTABSIZE 39



extern int loopnest;		/* current loop nesting level */
extern int inps4;
extern int tpip[2];

struct stdin_state {
	off_t seekable;
	int pip[2];
	int pending;
	tcflag_t bufferable;
};

extern struct parsefile basepf;	/* top level input file */
extern char basebuf[IBUFSIZ];	/* buffer for top level input file */
extern struct parsefile *toppf;
extern struct stdin_state stdin_state;

extern struct jmploc main_handler;

struct redirtab {
	struct redirtab *next;
	int renamed[10];
};

extern struct redirtab *redirlist;

extern void clear_traps(union node *);

extern struct localvar_list *localvar_stack;
extern char defoptindvar[];
extern char **environ;



/*
 * Initialization code.
 */

void
init() {

      /* from input.c: */
      {
	      basepf.nextc = basepf.buf = basebuf;
	      basepf.linno = 1;
      }

      /* from trap.c: */
      {
	      sigmode[SIGCHLD - 1] = S_DFL;
	      setsignal(SIGCHLD);
      }

      /* from output.c: */
      {
#ifdef USE_GLIBC_STDIO
	      initstreams();
#endif
      }

      /* from var.c: */
      {
	      char **envp;
	      static char ppid[32] = "PPID=";
	      const char *p;
	      struct stat64 st1, st2;

	      initvar();
	      for (envp = environ ; *envp ; envp++) {
		      p = endofname(*envp);
		      if (p != *envp && *p == '=') {
			      setvareq(*envp, VEXPORT|VTEXTFIXED);
		      }
	      }

	      setvareq(defifsvar, VTEXTFIXED);
	      setvareq(defoptindvar, VTEXTFIXED);

	      fmtstr(ppid + 5, sizeof(ppid) - 5, "%ld", (long) getppid());
	      setvareq(ppid, VTEXTFIXED);

	      p = lookupvar("PWD");
	      if (p)
		      if (*p != '/' || stat64(p, &st1) || stat64(dotdir, &st2) ||
			  st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
			      p = 0;
	      setpwd(p, 0);
      }
}



/*
 * This routine is called when an error or an interrupt occurs in an
 * interactive shell and control is returned to the main command loop
 * but prior to exitshell. 
 */

void
exitreset() {

      /* from eval.c: */
      {
	      if (savestatus >= 0) {
		      if (exception == EXEXIT || evalskip == SKIPFUNCDEF)
			      exitstatus = savestatus;
		      savestatus = -1;
	      }
	      evalskip = 0;
	      loopnest = 0;
	      inps4 = 0;

	      if (tpip[0] >= 0) {
		      close(tpip[0]);
		      close(tpip[1]);
	      }
      }

      /* from expand.c: */
      {
	      ifsfree();
      }

      /* from redir.c: */
      {
	      /*
	       * Discard all saved file descriptors.
	       */
	      unwindredir(0);
      }
}



/*
 * This routine is called when we enter a subshell.
 */

void
forkreset(union node *n) {

      /* from input.c: */
      {
	      popallfiles();
	      if (parsefile->fd > 0) {
		      close(parsefile->fd);
		      parsefile->fd = 0;
	      }
	      if (stdin_state.pip[0]) {
		      close(stdin_state.pip[0]);
		      close(stdin_state.pip[1]);
		      memset(stdin_state.pip, 0, sizeof(stdin_state.pip));
	      }
      }

      /* from main.c: */
      {
	      handler = &main_handler;
      }

      /* from redir.c: */
      {
	      redirlist = NULL;
      }

      /* from trap.c: */
      {
	      clear_traps(n);
      }
}



/*
 * This routine is called in exitshell.
 */

void
postexitreset() {

      /* from input.c: */
      {
	      flush_input();
      }
}



/*
 * This routine is called when an error or an interrupt occurs in an
 * interactive shell and control is returned to the main command loop.
 */

void
reset() {

      /* from input.c: */
      {
	      int c;

	      /* clear input buffer */
	      popallfiles();

	      c = PEOF;
	      if (toppf->nextc - toppf->buf > toppf->unget)
		      c = toppf->nextc[-toppf->unget - 1];
	      while (c != '\n' && c != PEOF && !int_pending())
		      c = pgetc();
      }

      /* from output.c: */
      {
#ifdef notyet
	      out1 = &output;
	      out2 = &errout;
#ifdef USE_GLIBC_STDIO
	      if (memout.stream != NULL)
		      __closememout();
#endif
	      if (memout.buf != NULL) {
		      ckfree(memout.buf);
		      memout.buf = NULL;
	      }
#endif
      }

      /* from var.c: */
      {
	      unwindlocalvars(0);
      }
}
