/*
 * 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 <unistd.h>
#include "input.h"
#include "error.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 + 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 struct parsefile basepf;	/* top level input file */
extern char basebuf[IBUFSIZ];	/* buffer for top level input file */

extern struct jmploc main_handler;

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

extern struct redirtab *redirlist;

extern char *trap[NSIG];

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



/*
 * Initialization code.
 */

void
init() {

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/input.c: */
      {
	      basepf.nextc = basepf.buf = basebuf;
	      basepf.linno = 1;
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/trap.c: */
      {
	      sigmode[SIGCHLD - 1] = S_DFL;
	      setsignal(SIGCHLD);
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/output.c: */
      {
#ifdef USE_GLIBC_STDIO
	      initstreams();
#endif
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/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(".", &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 /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/eval.c: */
      {
	      if (savestatus >= 0) {
		      if (exception == EXEXIT || evalskip == SKIPFUNCDEF)
			      exitstatus = savestatus;
		      savestatus = -1;
	      }
	      evalskip = 0;
	      loopnest = 0;
	      inps4 = 0;
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/expand.c: */
      {
	      ifsfree();
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/redir.c: */
      {
	      /*
	       * Discard all saved file descriptors.
	       */
	      unwindredir(0);
      }
}



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

void
forkreset() {

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/input.c: */
      {
	      popallfiles();
	      if (parsefile->fd > 0) {
		      close(parsefile->fd);
		      parsefile->fd = 0;
	      }
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/main.c: */
      {
	      handler = &main_handler;
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/redir.c: */
      {
	      redirlist = NULL;
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/trap.c: */
      {
	      char **tp;

	      INTOFF;
	      for (tp = trap ; tp < &trap[NSIG] ; tp++) {
		      if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
			      ckfree(*tp);
			      *tp = NULL;
			      if (tp != &trap[0])
				      setsignal(tp - trap);
		      }
	      }
	      trapcnt = 0;
	      INTON;
      }
}



/*
 * 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 /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/input.c: */
      {
	      /* clear input buffer */
	      basepf.lleft = basepf.nleft = 0;
	      basepf.unget = 0;
	      popallfiles();
      }

      /* from /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/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 /home/BWI/src/cygwin/dash/dash-0.5.12-4.x86_64/src/dash-0.5.12/src/var.c: */
      {
	      unwindlocalvars(0);
      }
}
