Logo Search packages:      
Sourcecode: kbuild version File versions  Download package

shinstance.h

Go to the documentation of this file.
/* $Id: shinstance.h 1240 2007-10-10 02:32:54Z bird $ */
/** @file
 *
 * The shell instance and it's methods.
 *
 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
 *
 *
 * This file is part of kBuild.
 *
 * kBuild is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * kBuild is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kBuild; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef ___shinstance_h
#define ___shinstance_h

#include <stdio.h> /* BUFSIZ */
#include <signal.h> /* NSIG */
#ifndef _MSC_VER
# include <termios.h>
# include <sys/ioctl.h>
# include <sys/resource.h>
#endif
#include <errno.h>
#ifdef _MSC_VER
# define EWOULDBLOCK    512
#endif

#include "shtypes.h"
#include "shthread.h"
#include "shfile.h"
#include "output.h"
#include "options.h"

#include "expand.h"
#include "exec.h"
#include "var.h"

#ifdef _MSC_VER
# define strcasecmp stricmp
# define strncasecmp strnicmp
#endif


/* memalloc.c */
#define MINSIZE 504           /* minimum size of a block */
struct stack_block {
      struct stack_block *prev;
      char space[MINSIZE];
};

/* input.c */
struct strpush {
      struct strpush *prev;   /* preceding string on stack */
      char *prevstring;
      int prevnleft;
      int prevlleft;
      struct alias *ap; /* if push was associated with an alias */
};

/*
 * The parsefile structure pointed to by the global variable parsefile
 * contains information about the current file being read.
 */
struct parsefile {
      struct parsefile *prev; /* preceding file on stack */
      int linno;        /* current line */
      int fd;                 /* file descriptor (or -1 if string) */
      int nleft;        /* number of chars left in this line */
      int lleft;        /* number of chars left in this buffer */
      char *nextc;            /* next char in buffer */
      char *buf;        /* input buffer */
      struct strpush *strpush; /* for pushing strings at this level */
      struct strpush basestrpush; /* so pushing one is fast */
};

/* exec.c */
#define CMDTABLESIZE 31       /* should be prime */
#define ARB 1                 /* actual size determined at run time */

struct tblentry {
      struct tblentry *next;  /* next entry in hash chain */
      union param param;      /* definition of builtin function */
      short cmdtype;          /* index identifying command */
      char rehash;            /* if set, cd done since entry created */
      char cmdname[ARB];      /* name of command */
};

/* expand.c */
/*
 * Structure specifying which parts of the string should be searched
 * for IFS characters.
 */
struct ifsregion {
      struct ifsregion *next; /* next region in list */
      int begoff;       /* offset of start of region */
      int endoff;       /* offset of end of region */
      int inquotes;           /* search for nul bytes only */
};


/**
 * A shell instance.
 *
 * This is the core structure of the shell, it contains all
 * the data associated with a shell process except that it's
 * running in a thread and not a separate process.
 */
00122 struct shinstance
{
00124     struct shinstance  *next;           /**< The next shell instance. */
00125     struct shinstance  *prev;           /**< The previous shell instance. */
00126     struct shinstance  *parent;         /**< The parent shell instance. */
00127     pid_t               pid;            /**< The (fake) process id of this shell instance. */
00128     shtid               tid;            /**< The thread identifier of the thread for this shell. */
00129     shfdtab             fdtab;          /**< The file descriptor table. */

    /* alias.c */
#define ATABSIZE 39
    struct alias       *atab[ATABSIZE];

    /* cd.c */
00136     char               *curdir;         /**< current working directory */
00137     char               *prevdir;        /**< previous working directory */
    char               *cdcomppath;
00139     int                 getpwd_first;   /**< static in getpwd. (initialized to 1!) */

    /* error.h */
    struct jmploc      *handler;
    int                 exception;
00144     int                 exerrno/* = 0 */; /**< Last exec error */
    int volatile        suppressint;
    int volatile        intpending;

    /* error.c */
00149     char                errmsg_buf[16]; /**< static in errmsg. (bss) */

    /* eval.h */
00152     char               *commandname;    /**< currently executing command */
00153     int                 exitstatus;     /**< exit status of last command */
00154     int                 back_exitstatus;/**< exit status of backquoted command */
00155     struct strlist     *cmdenviron;     /**< environment for builtin command */
00156     int                 funcnest;       /**< depth of function calls */
00157     int                 evalskip;       /**< set if we are skipping commands */
00158     int                 skipcount;      /**< number of levels to skip */
00159     int                 loopnest;       /**< current loop nesting level */

    /* eval.c */
    int                 vforked;

    /* expand.c */
00165     char               *expdest;        /**< output of current string */
00166     struct nodelist    *argbackq;       /**< list of back quote expressions */
00167     struct ifsregion    ifsfirst;       /**< first struct in list of ifs regions */
00168     struct ifsregion   *ifslastp;       /**< last struct in list */
00169     struct arglist      exparg;         /**< holds expanded arg list */
00170     char               *expdir;         /**< Used by expandmeta. */

    /* exec.h */
00173     const char         *pathopt;        /**< set by padvance */

    /* exec.c */
    struct tblentry    *cmdtable[CMDTABLESIZE];
00177     int                 builtinloc/* = -1*/;    /**< index in path of %builtin, or -1 */

    /* input.h */
00180     int                 plinno/* = 1 */;/**< input line number */
00181     int                 parsenleft;     /**< number of characters left in input buffer */
00182     char               *parsenextc;     /**< next character in input buffer */
00183     int                 init_editline/* = 0 */;     /**< 0 == not setup, 1 == OK, -1 == failed */

    /* input.c */
00186     int                 parselleft;     /**< copy of parsefile->lleft */
00187     struct parsefile    basepf;         /**< top level input file */
00188     char                basebuf[BUFSIZ];/**< buffer for top level input file */
00189     struct parsefile   *parsefile/* = &basepf*/;    /**< current input file */
#ifndef SMALL
00191     EditLine           *el;             /**< cookie for editline package */
#endif

    /* jobs.h */
00195     pid_t               backgndpid/* = -1 */;   /**< pid of last background process */
00196     int                 job_warning;    /**< user was warned about stopped jobs */

    /* jobs.c */
00199     struct job         *jobtab;         /**< array of jobs */
00200     int                 njobs;          /**< size of array */
00201     int                 jobs_invalid;   /**< set in child */
00202     int                 initialpgrp;    /**< pgrp of shell on invocation */
00203     int                 curjob/* = -1*/;/**< current job */
    int                 ttyfd/* = -1*/;
00205     int                 jobctl;         /**< job control enabled / disabled */
    char               *cmdnextc;
    int                 cmdnleft;


    /* mail.c */
#define MAXMBOXES 10
00212     int                 nmboxes;        /**< number of mailboxes */
00213     time_t              mailtime[MAXMBOXES]; /**< times of mailboxes */

    /* main.h */
00216     int                 rootpid;        /**< pid of main shell. */
00217     int                 rootshell;      /**< true if we aren't a child of the main shell. */
00218     struct shinstance  *psh_rootshell;  /**< The root shell pointer. (!rootshell) */

    /* memalloc.h */
    char               *stacknxt/* = stackbase.space*/;
    int                 stacknleft/* = MINSIZE*/;
    int                 sstrnleft;
    int                 herefd/* = -1 */;

    /* memalloc.c */
    struct stack_block  stackbase;
    struct stack_block *stackp/* = &stackbase*/;
    struct stackmark   *markp;

    /* myhistedit.h */
    int                 displayhist;
#ifndef SMALL
    History            *hist;
    EditLine           *el;
#endif

    /* output.h */
    struct output       output;
    struct output       errout;
    struct output       memout;
    struct output      *out1;
    struct output      *out2;

    /* output.c */
#define OUTBUFSIZ BUFSIZ
00247 #define MEM_OUT -3                      /**< output to dynamically allocated memory */

    /* options.h */
    struct optent       optlist[NOPTS];
00251     char               *minusc;         /**< argument to -c option */
00252     char               *arg0;           /**< $0 */
00253     struct shparam      shellparam;     /**< $@ */
00254     char              **argptr;         /**< argument list for builtin commands */
00255     char               *optionarg;      /**< set by nextopt */
00256     char               *optptr;         /**< used by nextopt */

    /* parse.h */
    int                 tokpushback;
00260     int                 whichprompt;    /**< 1 == PS1, 2 == PS2 */

    /* parser.c */
00263     int                 noalias/* = 0*/;/**< when set, don't handle aliases */
00264     struct heredoc     *heredoclist;    /**< list of here documents to read */
00265     int                 parsebackquote; /**< nonzero if we are inside backquotes */
00266     int                 doprompt;       /**< if set, prompt the user */
00267     int                 needprompt;     /**< true if interactive and at start of line */
00268     int                 lasttoken;      /**< last token read */
00269     char               *wordtext;       /**< text of last word returned by readtoken */
00270     int                 checkkwd;       /**< 1 == check for kwds, 2 == also eat newlines */
    struct nodelist    *backquotelist;
    union node         *redirnode;
    struct heredoc     *heredoc;
00274     int                 quoteflag;      /**< set if (part of) last token was quoted */
00275     int                 startlinno;     /**< line # where last token started */

    /* redir.c */
    struct redirtab    *redirlist;
    int                 fd0_redirected/* = 0*/;

    /* trap.h */
00282     int                 pendingsigs;    /**< indicates some signal received */

    /* trap.c */
00285     char                gotsig[NSIG];   /**< indicates specified signal received */
00286     char               *trap[NSIG+1];   /**< trap handler commands */
00287     char                sigmode[NSIG];  /**< current value of signal */

    /* var.h */
    struct localvar    *localvars;
    struct var          vatty;
    struct var          vifs;
    struct var          vmail;
    struct var          vmpath;
    struct var          vpath;
#ifdef _MSC_VER
    struct var          vpath2;
#endif
    struct var          vps1;
    struct var          vps2;
    struct var          vps4;
#ifndef SMALL
    struct var          vterm;
    struct var          vhistsize;
#endif
    struct var          voptind;
#ifdef PC_OS2_LIBPATHS
    struct var          libpath_vars[4];
#endif
#ifdef SMALL
# define VTABSIZE 39
#else
# define VTABSIZE 517
#endif
    struct var         *vartab[VTABSIZE];

    /* builtins.h */

    /* bltin/test.c */
    char              **t_wp;
    struct t_op const  *t_wp_op;

};


extern shinstance *sh_create_root_shell(shinstance *, int, char **);

/* environment & pwd.h */
char *sh_getenv(shinstance *, const char *);
char **sh_environ(shinstance *);
const char *sh_gethomedir(shinstance *, const char *);

/* signals */
typedef void (*shsig_t)(shinstance *, int);
#ifdef _MSC_VER
    typedef uint32_t shsigset_t;
#else
    typedef sigset_t shsigset_t;
#endif
struct shsigaction
{
    shsig_t     sh_handler;
    shsigset_t  sh_mask;
    int         sh_flags;
};
#define SH_SIG_DFL ((shsig_t)SIG_DFL)
#define SH_SIG_IGN ((shsig_t)SIG_IGN)
#ifdef _MSC_VER
#   define SIG_BLOCK         1
#   define SIG_UNBLOCK       2
#   define SIG_SETMASK       3
#   define SIGHUP            5
#   define SIGQUIT           9
#   define SIGPIPE          12
#   define SIGTTOU          17
#   define SIGTSTP          18
#   define SIGTTIN          19
#   define SIGCONT          20
#   define sys_siglist      sys_signame
#endif /* _MSC_VER */
#ifdef __sun__
#   define sys_siglist      _sys_siglist
#endif
#ifndef HAVE_SYS_SIGNAME
extern char sys_signame[NSIG][16];
#endif

int sh_sigaction(shinstance *, int, const struct shsigaction *, struct shsigaction *);
shsig_t sh_signal(shinstance *, int, shsig_t);
int sh_siginterrupt(shinstance *, int, int);
void sh_sigemptyset(shsigset_t *);
int sh_sigprocmask(shinstance *, int, shsigset_t const *, shsigset_t *);
void sh_abort(shinstance *) __attribute__((__noreturn__));
void sh_raise_sigint(shinstance *);
int sh_kill(shinstance *, pid_t, int);
int sh_killpg(shinstance *, pid_t, int);

/* times */
#include <time.h>
#ifdef _MSC_VER
    typedef struct shtms
    {
        clock_t tms_utime;
        clock_t tms_stime;
        clock_t tms_cutime;
        clock_t tms_cstime;
    } shtms;
#else
#   include <sys/times.h>
    typedef struct tms shtms;
#endif
clock_t sh_times(shinstance *, shtms *);
int sh_sysconf_clk_tck(void);

/* wait / process */
#ifdef _MSC_VER
#   include <process.h>
#   define WNOHANG         1       /* Don't hang in wait. */
#   define WUNTRACED       2       /* Tell about stopped, untraced children. */
#   define WCONTINUED      4       /* Report a job control continued process. */
#   define _W_INT(w)       (*(int *)&(w))  /* Convert union wait to int. */
#   define WCOREFLAG       0200
#   define _WSTATUS(x)     (_W_INT(x) & 0177)
#   define _WSTOPPED       0177            /* _WSTATUS if process is stopped */
#   define WIFSTOPPED(x)   (_WSTATUS(x) == _WSTOPPED)
#   define WSTOPSIG(x)     (_W_INT(x) >> 8)
#   define WIFSIGNALED(x)  (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
#   define WTERMSIG(x)     (_WSTATUS(x))
#   define WIFEXITED(x)    (_WSTATUS(x) == 0)
#   define WEXITSTATUS(x)  (_W_INT(x) >> 8)
#   define WIFCONTINUED(x) (x == 0x13)     /* 0x13 == SIGCONT */
#   define WCOREDUMP(x)    (_W_INT(x) & WCOREFLAG)
#   define W_EXITCODE(ret, sig)    ((ret) << 8 | (sig))
#   define W_STOPCODE(sig)         ((sig) << 8 | _WSTOPPED)
#else
#   include <sys/wait.h>
#endif
pid_t sh_fork(shinstance *);
pid_t sh_waitpid(shinstance *, pid_t, int *, int);
void sh__exit(shinstance *, int) __attribute__((__noreturn__));;
int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
uid_t sh_getuid(shinstance *);
uid_t sh_geteuid(shinstance *);
gid_t sh_getgid(shinstance *);
gid_t sh_getegid(shinstance *);
pid_t sh_getpid(shinstance *);
pid_t sh_getpgrp(shinstance *);
pid_t sh_getpgid(shinstance *, pid_t);
int sh_setpgid(shinstance *, pid_t, pid_t);

/* tc* */
pid_t sh_tcgetpgrp(shinstance *, int);
int sh_tcsetpgrp(shinstance *, int, pid_t);

/* sys/resource.h */
#ifdef _MSC_VER
    typedef int64_t shrlim_t;
    typedef struct shrlimit
    {
        shrlim_t   rlim_cur;
        shrlim_t   rlim_max;
    } shrlimit;
#   define RLIMIT_CPU     0
#   define RLIMIT_FSIZE   1
#   define RLIMIT_DATA    2
#   define RLIMIT_STACK   3
#   define RLIMIT_CORE    4
#   define RLIMIT_RSS     5
#   define RLIMIT_MEMLOCK 6
#   define RLIMIT_NPROC   7
#   define RLIMIT_NOFILE  8
#   define RLIMIT_SBSIZE  9
#   define RLIMIT_VMEM    10
#   define RLIM_NLIMITS   11
#   define RLIM_INFINITY  (0x7fffffffffffffffLL)
#else
    typedef rlim_t          shrlim_t;
    typedef struct rlimit   shrlimit;
#endif
int sh_getrlimit(shinstance *, int, shrlimit *);
int sh_setrlimit(shinstance *, int, const shrlimit *);

#endif

Generated by  Doxygen 1.6.0   Back to index