#include <ast.h>
#include <error.h>
#include "sh_config.h"
#include <sys/stat.h>
#ifdef _hdr_unistd
# include <unistd.h>
#endif
#ifdef _hdr_fcntl
# include <fcntl.h>
#endif
#ifndef O_CREAT
# ifdef _sys_file
# include <sys/file.h>
# endif
#endif
#ifndef S_ISDIR
# define S_ISDIR(m) (((m)&S_IFMT)==S_IFDIR)
#endif
#ifndef S_ISREG
# define S_ISREG(m) (((m)&S_IFMT)==S_IFREG)
#endif
#ifndef S_ISCHR
# define S_ISCHR(m) (((m)&S_IFMT)==S_IFCHR)
#endif
#ifndef S_ISBLK
# define S_ISBLK(m) (((m)&S_IFMT)==S_IFBLK)
#endif
#ifdef S_IFIFO
# ifndef S_ISFIFO
# define S_ISFIFO(m) (((m)&S_IFMT)==S_IFIFO)
# endif
#endif
#ifndef S_IRUSR
# define S_IRUSR (S_IREAD)
#endif
#ifndef S_IWUSR
# define S_IWUSR (S_IWRITE)
#endif
#ifndef S_IXUSR
# define S_IXUSR (S_IEXEC)
#endif
#ifndef S_IRGRP
# define S_IRGRP (S_IREAD>>3)
#endif
#ifndef S_IWGRP
# define S_IWGRP (S_IWRITE>>3)
#endif
#ifndef S_IXGRP
# define S_IXGRP (S_IEXEC>>3)
#endif
#ifndef S_IROTH
# define S_IROTH (S_IREAD>>6)
#endif
#ifndef S_IWOTH
# define S_IWOTH (S_IWRITE>>6)
#endif
#ifndef S_IXOTH
# define S_IXOTH (S_IEXEC>>6)
#endif
#define RW_ALL (S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH)
#ifndef NFILE
# define NFILE 20
#endif
#ifndef IOBSIZE
# define IOBSIZE 1024
#endif
#define EOF (-1)
#define MAXTRY 12
#ifdef SEVENBIT
# define STRIP 0177
#else
# define STRIP 0377
#endif
#define TMPSIZ 20
#define ERRIO 2
#define USERIO 10
#define FCIO (NFILE-1)
#ifdef KSHELL
# define INIO (NFILE-2)
# define TMPIO (NFILE-3)
# define CINPIPE (NFILE-4)
# define CINPIPE2 (NFILE-5)
# define COTPIPE (NFILE-6)
# define MAXFILES (NFILE-USERIO)
# define F_STRING ((unsigned char)NFILE)
# define F_INFINITE 0x7fff
#endif
struct fileblk
{
char *ptr;
char *base;
char *last;
off_t fseek;
int flag;
unsigned char fdes;
#ifdef KSHELL
char ftype;
int flast;
char **feval;
struct fileblk *fstak;
unsigned flin;
#endif
};
#define filenum(fp) ((int)(fp->fdes))
#define fnobuff(fp) ((fp)->flag&IONBF)
#define IOREAD 0001
#define IOWRT 0002
#define IONBF 0004
#define IOFREE 0010
#define IOEOF 0020
#define IOERR 0040
#define IORW 0100
#define IOSLOW 0200
#define IOEDIT 0400
extern struct fileblk *io_ftable[NFILE+USERIO];
#ifdef FNDELAY
# ifdef EAGAIN
# if EAGAIN!=EWOULDBLOCK
# undef EAGAIN
# define EAGAIN EWOULDBLOCK
# endif
# else
# define EAGAIN EWOULDBLOCK
# endif
# ifndef O_NONBLOCK
# define O_NONBLOCK FNDELAY
# endif
#endif
#ifndef O_CREAT
# define O_CREAT 0400
# define O_TRUNC 01000
# define O_APPEND 010
#endif
#ifndef O_RDWR
# define O_RDONLY 0
# define O_WRONLY 01
# define O_RDWR 02
#endif
#ifdef NOFCNTL
# define open myopen
# define F_DUPFD 0
# define F_GETFD 1
# define F_SETFD 2
#endif
#ifndef R_OK
# define F_OK 0
# define X_OK 1
# define W_OK 2
# define R_OK 4
#endif
#ifndef SEEK_SET
# define SEEK_SET 0
# define SEEK_CUR 1
# define SEEK_END 2
#endif
#define INPIPE 0
#define OTPIPE 1
#define DUPFLG 0100
#ifdef KSHELL
#define F_ISSTRING 1
#define F_ISFILE 2
#define F_ISALIAS 3
#define F_ISEVAL 4
#define F_ISEVAL2 5
union io_eval
{
int fd;
char **com;
};
#define io_unreadc(c) (st.peekn |= (c)|MARK)
#define input (st.curin)
#define output (sh.curout)
#define newline() p_char(NL)
#define fisopen(fd) (io_access(fd,F_OK)==0)
#define fiswrite(fd) (io_access(fd,W_OK)==0)
#define fisread(fd) (io_access(fd,R_OK)==0)
#define fiseof(fp) ((fp)->flag&IOEOF)
#define fiserror(fp) ((fp)->flag&IOERR)
#define nextchar(fp) (*((fp)->ptr))
#define finbuff(fp) ((fp)->last - (fp)->ptr)
#ifndef clearerr
# define clearerr(fp) ((fp)->flag &= ~(IOERR|IOEOF))
#endif
struct filesave
{
short org_fd;
short dup_fd;
};
#ifdef PROTO
extern void io_clear(struct fileblk*);
extern void io_fclose(int);
extern int io_getc(int);
extern void io_init(int,struct fileblk*,char*);
extern int io_intr(struct fileblk*);
extern void io_push(struct fileblk*);
extern int io_pop(int);
extern int io_mktmp(char*);
extern off_t io_seek(int,off_t,int);
extern int io_readbuff(struct fileblk*);
extern int io_readc(void);
extern int io_renumber(int,int);
extern void io_sync(void);
extern int io_movefd(int);
extern void io_popen(int[]);
extern void io_pclose(int[]);
extern void io_restore(int);
struct ionod;
extern int io_redirect(struct ionod*,int);
extern void io_save(int,int);
extern void io_linkdoc(struct ionod*);
extern void io_swapdoc(struct ionod*);
extern int io_fopen(const char*);
extern void io_sopen(char*);
extern int io_access(int,int);
extern int io_nextc(void);
extern int ispipe(int);
#else
extern void io_clear();
extern void io_fclose();
extern int io_getc();
extern void io_init();
extern int io_intr();
extern void io_push();
extern int io_pop();
extern int io_mktmp();
extern off_t io_seek();
extern int io_readbuff();
extern int io_readc();
extern int io_renumber();
extern void io_sync();
extern int io_movefd();
extern void io_popen();
extern void io_pclose();
extern void io_restore();
extern int io_redirect();
extern void io_save();
extern void io_rmtemp();
extern void io_linkdoc();
extern void io_swapdoc();
extern int io_fopen();
extern void io_sopen();
extern int io_access();
extern int io_nextc();
extern int ispipe();
#endif
extern void io_settemp();
extern char _sibuf[];
extern char _sobuf[];
extern struct fileblk io_stdin;
extern struct fileblk io_stdout;
extern char io_tmpname[];
extern const char e_create[];
extern const char e_file[];
extern const char e_open[];
extern const char e_pipe[];
extern const char e_flimit[];
extern const char e_fexists[];
extern const char e_unknown[];
extern const char e_endoffile[];
extern const char e_devnull[];
extern const char e_profile[];
extern const char e_suidprofile[];
extern const char e_sysprofile[];
extern const char e_devfdNN[];
#ifdef SUID_EXEC
extern const char e_suidexec[];
#endif
#endif