source: trunk/src/kash/shinstance.h@ 3435

Last change on this file since 3435 was 3435, checked in by bird, 5 years ago

kash: Remove vfork code, we've never used it and we wont need it if we replace fork() with pthreads.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 16.8 KB
Line 
1/* $Id: shinstance.h 3435 2020-09-02 20:30:54Z bird $ */
2/** @file
3 * The shell instance and it's methods.
4 */
5
6/*
7 * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
8 *
9 *
10 * This file is part of kBuild.
11 *
12 * kBuild is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * kBuild is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with kBuild; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#ifndef ___shinstance_h
29#define ___shinstance_h
30
31#include <stdio.h> /* BUFSIZ */
32#include <signal.h> /* NSIG */
33#ifndef _MSC_VER
34# include <termios.h>
35# include <sys/types.h>
36# include <sys/ioctl.h>
37# include <sys/resource.h>
38#endif
39#include <errno.h>
40#ifdef _MSC_VER
41# define EWOULDBLOCK 140
42#endif
43
44#include "shtypes.h"
45#include "shthread.h"
46#include "shfile.h"
47#include "shheap.h"
48#include "shell.h"
49#include "output.h"
50#include "options.h"
51
52#include "expand.h"
53#include "exec.h"
54#include "var.h"
55#include "show.h"
56
57#ifdef _MSC_VER
58# define strcasecmp stricmp
59# define strncasecmp strnicmp
60#endif
61
62/**
63 * A child process.
64 */
65typedef struct shchild
66{
67 pid_t pid; /**< The pid. */
68#if K_OS == K_OS_WINDOWS
69 void *hChild; /**< The process handle. */
70#endif
71} shchild;
72
73/* memalloc.c */
74#define MINSIZE 504 /* minimum size of a block */
75struct stack_block {
76 struct stack_block *prev;
77 char space[MINSIZE];
78};
79
80/* input.c */
81struct strpush {
82 struct strpush *prev; /* preceding string on stack */
83 char *prevstring;
84 int prevnleft;
85 int prevlleft;
86 struct alias *ap; /* if push was associated with an alias */
87};
88
89/*
90 * The parsefile structure pointed to by the global variable parsefile
91 * contains information about the current file being read.
92 */
93struct parsefile {
94 struct parsefile *prev; /* preceding file on stack */
95 int linno; /* current line */
96 int fd; /* file descriptor (or -1 if string) */
97 int nleft; /* number of chars left in this line */
98 int lleft; /* number of chars left in this buffer */
99 char *nextc; /* next char in buffer */
100 char *buf; /* input buffer */
101 struct strpush *strpush; /* for pushing strings at this level */
102 struct strpush basestrpush; /* so pushing one is fast */
103};
104
105/* exec.c */
106#define CMDTABLESIZE 31 /* should be prime */
107#define ARB 1 /* actual size determined at run time */
108
109struct tblentry {
110 struct tblentry *next; /* next entry in hash chain */
111 union param param; /* definition of builtin function */
112 short cmdtype; /* index identifying command */
113 char rehash; /* if set, cd done since entry created */
114 char cmdname[ARB]; /* name of command */
115};
116
117/* expand.c */
118/*
119 * Structure specifying which parts of the string should be searched
120 * for IFS characters.
121 */
122struct ifsregion {
123 struct ifsregion *next; /* next region in list */
124 int begoff; /* offset of start of region */
125 int endoff; /* offset of end of region */
126 int inquotes; /* search for nul bytes only */
127};
128
129
130/**
131 * A shell instance.
132 *
133 * This is the core structure of the shell, it contains all
134 * the data associated with a shell process except that it's
135 * running in a thread and not a separate process.
136 */
137struct shinstance
138{
139 struct shinstance *next; /**< The next shell instance. */
140 struct shinstance *prev; /**< The previous shell instance. */
141 struct shinstance *parent; /**< The parent shell instance. */
142 pid_t pid; /**< The (fake) process id of this shell instance. */
143 shtid tid; /**< The thread identifier of the thread for this shell. */
144 shfdtab fdtab; /**< The file descriptor table. */
145 shsigaction_t sigactions[NSIG]; /**< The signal actions registered with this shell instance. */
146 shsigset_t sigmask; /**< Our signal mask. */
147 char **shenviron; /**< The environment vector. */
148 int num_children; /**< Number of children in the array. */
149 shchild *children; /**< The child array. */
150
151 /* alias.c */
152#define ATABSIZE 39
153 struct alias *atab[ATABSIZE];
154
155 /* cd.c */
156 char *curdir; /**< current working directory */
157 char *prevdir; /**< previous working directory */
158 char *cdcomppath;
159 int getpwd_first; /**< static in getpwd. (initialized to 1!) */
160
161 /* error.h */
162 struct jmploc *handler;
163 int exception;
164 int exerrno/* = 0 */; /**< Last exec error */
165 int volatile suppressint;
166 int volatile intpending;
167
168 /* error.c */
169 char errmsg_buf[16]; /**< static in errmsg. (bss) */
170
171 /* eval.h */
172 char *commandname; /**< currently executing command */
173 int exitstatus; /**< exit status of last command */
174 int back_exitstatus;/**< exit status of backquoted command */
175 struct strlist *cmdenviron; /**< environment for builtin command */
176 int funcnest; /**< depth of function calls */
177 int evalskip; /**< set if we are skipping commands */
178 int skipcount; /**< number of levels to skip */
179 int loopnest; /**< current loop nesting level */
180
181 /* expand.c */
182 char *expdest; /**< output of current string */
183 struct nodelist *argbackq; /**< list of back quote expressions */
184 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
185 struct ifsregion *ifslastp; /**< last struct in list */
186 struct arglist exparg; /**< holds expanded arg list */
187 char *expdir; /**< Used by expandmeta. */
188
189 /* exec.h */
190 const char *pathopt; /**< set by padvance */
191
192 /* exec.c */
193 struct tblentry *cmdtable[CMDTABLESIZE];
194 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
195
196 /* input.h */
197 int plinno/* = 1 */;/**< input line number */
198 int parsenleft; /**< number of characters left in input buffer */
199 char *parsenextc; /**< next character in input buffer */
200 int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
201
202 /* input.c */
203 int parselleft; /**< copy of parsefile->lleft */
204 struct parsefile basepf; /**< top level input file */
205 char basebuf[BUFSIZ];/**< buffer for top level input file */
206 struct parsefile *parsefile/* = &basepf*/; /**< current input file */
207#ifndef SMALL
208 EditLine *el; /**< cookie for editline package */
209#endif
210
211 /* jobs.h */
212 pid_t backgndpid/* = -1 */; /**< pid of last background process */
213 int job_warning; /**< user was warned about stopped jobs */
214
215 /* jobs.c */
216 struct job *jobtab; /**< array of jobs */
217 int njobs; /**< size of array */
218 int jobs_invalid; /**< set in child */
219 int initialpgrp; /**< pgrp of shell on invocation */
220 int curjob/* = -1*/;/**< current job */
221 int ttyfd/* = -1*/;
222 int jobctl; /**< job control enabled / disabled */
223 char *cmdnextc;
224 int cmdnleft;
225
226
227 /* mail.c */
228#define MAXMBOXES 10
229 int nmboxes; /**< number of mailboxes */
230 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
231
232 /* main.h */
233 int rootpid; /**< pid of main shell. */
234 int rootshell; /**< true if we aren't a child of the main shell. */
235 struct shinstance *psh_rootshell; /**< The root shell pointer. (!rootshell) */
236
237 /* memalloc.h */
238 char *stacknxt/* = stackbase.space*/;
239 int stacknleft/* = MINSIZE*/;
240 int sstrnleft;
241 int herefd/* = -1 */;
242
243 /* memalloc.c */
244 struct stack_block stackbase;
245 struct stack_block *stackp/* = &stackbase*/;
246 struct stackmark *markp;
247
248 /* myhistedit.h */
249 int displayhist;
250#ifndef SMALL
251 History *hist;
252 EditLine *el;
253#endif
254
255 /* output.h */
256 struct output output;
257 struct output errout;
258 struct output memout;
259 struct output *out1;
260 struct output *out2;
261
262 /* output.c */
263#define OUTBUFSIZ BUFSIZ
264#define MEM_OUT -3 /**< output to dynamically allocated memory */
265
266 /* options.h */
267 struct optent optlist[NOPTS];
268 char *minusc; /**< argument to -c option */
269 char *arg0; /**< $0 */
270 struct shparam shellparam; /**< $@ */
271 char **argptr; /**< argument list for builtin commands */
272 char *optionarg; /**< set by nextopt */
273 char *optptr; /**< used by nextopt */
274
275 /* parse.h */
276 int tokpushback;
277 int whichprompt; /**< 1 == PS1, 2 == PS2 */
278
279 /* parser.c */
280 int noalias/* = 0*/;/**< when set, don't handle aliases */
281 struct heredoc *heredoclist; /**< list of here documents to read */
282 int parsebackquote; /**< nonzero if we are inside backquotes */
283 int doprompt; /**< if set, prompt the user */
284 int needprompt; /**< true if interactive and at start of line */
285 int lasttoken; /**< last token read */
286 char *wordtext; /**< text of last word returned by readtoken */
287 int checkkwd; /**< 1 == check for kwds, 2 == also eat newlines */
288 struct nodelist *backquotelist;
289 union node *redirnode;
290 struct heredoc *heredoc;
291 int quoteflag; /**< set if (part of) last token was quoted */
292 int startlinno; /**< line # where last token started */
293
294 /* redir.c */
295 struct redirtab *redirlist;
296 int fd0_redirected/* = 0*/;
297
298 /* show.c */
299 char tracebuf[1024];
300 size_t tracepos;
301 int tracefd;
302
303 /* trap.h */
304 int pendingsigs; /**< indicates some signal received */
305
306 /* trap.c */
307 char gotsig[NSIG]; /**< indicates specified signal received */
308 char *trap[NSIG+1]; /**< trap handler commands */
309 char sigmode[NSIG]; /**< current value of signal */
310
311 /* var.h */
312 struct localvar *localvars;
313 struct var vatty;
314 struct var vifs;
315 struct var vmail;
316 struct var vmpath;
317 struct var vpath;
318#ifdef _MSC_VER
319 struct var vpath2;
320#endif
321 struct var vps1;
322 struct var vps2;
323 struct var vps4;
324#ifndef SMALL
325 struct var vterm;
326 struct var vhistsize;
327#endif
328 struct var voptind;
329#ifdef PC_OS2_LIBPATHS
330 struct var libpath_vars[4];
331#endif
332#ifdef SMALL
333# define VTABSIZE 39
334#else
335# define VTABSIZE 517
336#endif
337 struct var *vartab[VTABSIZE];
338
339 /* builtins.h */
340
341 /* bltin/test.c */
342 char **t_wp;
343 struct t_op const *t_wp_op;
344
345};
346
347
348extern shinstance *sh_create_root_shell(shinstance *, int, char **, char **);
349
350/* environment & pwd.h */
351char *sh_getenv(shinstance *, const char *);
352char **sh_environ(shinstance *);
353const char *sh_gethomedir(shinstance *, const char *);
354
355/* signals */
356#define SH_SIG_UNK ((shsig_t)(intptr_t)-199)
357#define SH_SIG_DFL ((shsig_t)(intptr_t)SIG_DFL)
358#define SH_SIG_IGN ((shsig_t)(intptr_t)SIG_IGN)
359#define SH_SIG_ERR ((shsig_t)(intptr_t)SIG_ERR)
360#ifdef _MSC_VER
361# define SA_RESTART 0x02
362# define SIG_BLOCK 1
363# define SIG_UNBLOCK 2
364# define SIG_SETMASK 3
365
366# define SIGHUP 1 /* _SIGHUP_IGNORE */
367/*# define SIGINT 2 */
368# define SIGQUIT 3 /* _SIGQUIT_IGNORE */
369/*# define SIGILL 4 */
370/*# define SIGFPE 8 */
371/*# define SIGSEGV 11 */
372# define SIGPIPE 13 /* _SIGPIPE_IGNORE */
373/*# define SIGTERM 15 */
374# define SIGTTIN 16 /* _SIGIOINT_IGNORE */
375# define SIGTSTP 17 /* _SIGSTOP_IGNORE */
376# define SIGTTOU 18
377# define SIGCONT 20
378/*# define SIGBREAK 21 */
379/*# define SIGABRT 22 */
380const char *strsignal(int iSig);
381#endif /* _MSC_VER */
382#ifndef HAVE_SYS_SIGNAME
383extern const char * const sys_signame[NSIG];
384#endif
385
386int sh_sigaction(shinstance *, int, const struct shsigaction *, struct shsigaction *);
387shsig_t sh_signal(shinstance *, int, shsig_t);
388int sh_siginterrupt(shinstance *, int, int);
389void sh_sigemptyset(shsigset_t *);
390void sh_sigfillset(shsigset_t *);
391void sh_sigaddset(shsigset_t *, int);
392void sh_sigdelset(shsigset_t *, int);
393int sh_sigismember(shsigset_t const *, int);
394int sh_sigprocmask(shinstance *, int, shsigset_t const *, shsigset_t *);
395SH_NORETURN_1 void sh_abort(shinstance *) SH_NORETURN_2;
396void sh_raise_sigint(shinstance *);
397int sh_kill(shinstance *, pid_t, int);
398int sh_killpg(shinstance *, pid_t, int);
399
400/* times */
401#include <time.h>
402#ifdef _MSC_VER
403 typedef struct shtms
404 {
405 clock_t tms_utime;
406 clock_t tms_stime;
407 clock_t tms_cutime;
408 clock_t tms_cstime;
409 } shtms;
410#else
411# include <sys/times.h>
412 typedef struct tms shtms;
413#endif
414clock_t sh_times(shinstance *, shtms *);
415int sh_sysconf_clk_tck(void);
416
417/* wait / process */
418int sh_add_child(shinstance *psh, pid_t pid, void *hChild);
419#ifdef _MSC_VER
420# include <process.h>
421# define WNOHANG 1 /* Don't hang in wait. */
422# define WUNTRACED 2 /* Tell about stopped, untraced children. */
423# define WCONTINUED 4 /* Report a job control continued process. */
424# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
425# define WCOREFLAG 0200
426# define _WSTATUS(x) (_W_INT(x) & 0177)
427# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
428# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
429# define WSTOPSIG(x) (_W_INT(x) >> 8)
430# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
431# define WTERMSIG(x) (_WSTATUS(x))
432# define WIFEXITED(x) (_WSTATUS(x) == 0)
433# define WEXITSTATUS(x) (_W_INT(x) >> 8)
434# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
435# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
436# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
437# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
438#else
439# include <sys/wait.h>
440# ifdef __HAIKU__
441# define WCOREDUMP(x) WIFCORED(x)
442# endif
443#endif
444pid_t sh_fork(shinstance *);
445pid_t sh_waitpid(shinstance *, pid_t, int *, int);
446SH_NORETURN_1 void sh__exit(shinstance *, int) SH_NORETURN_2;
447int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
448uid_t sh_getuid(shinstance *);
449uid_t sh_geteuid(shinstance *);
450gid_t sh_getgid(shinstance *);
451gid_t sh_getegid(shinstance *);
452pid_t sh_getpid(shinstance *);
453pid_t sh_getpgrp(shinstance *);
454pid_t sh_getpgid(shinstance *, pid_t);
455int sh_setpgid(shinstance *, pid_t, pid_t);
456
457/* tc* */
458pid_t sh_tcgetpgrp(shinstance *, int);
459int sh_tcsetpgrp(shinstance *, int, pid_t);
460
461/* sys/resource.h */
462#ifdef _MSC_VER
463 typedef int64_t shrlim_t;
464 typedef struct shrlimit
465 {
466 shrlim_t rlim_cur;
467 shrlim_t rlim_max;
468 } shrlimit;
469# define RLIMIT_CPU 0
470# define RLIMIT_FSIZE 1
471# define RLIMIT_DATA 2
472# define RLIMIT_STACK 3
473# define RLIMIT_CORE 4
474# define RLIMIT_RSS 5
475# define RLIMIT_MEMLOCK 6
476# define RLIMIT_NPROC 7
477# define RLIMIT_NOFILE 8
478# define RLIMIT_SBSIZE 9
479# define RLIMIT_VMEM 10
480# define RLIM_NLIMITS 11
481# define RLIM_INFINITY (0x7fffffffffffffffLL)
482#else
483 typedef rlim_t shrlim_t;
484 typedef struct rlimit shrlimit;
485#endif
486int sh_getrlimit(shinstance *, int, shrlimit *);
487int sh_setrlimit(shinstance *, int, const shrlimit *);
488
489/* string.h */
490const char *sh_strerror(shinstance *, int);
491
492#ifdef DEBUG
493# define TRACE2(param) trace param
494# define TRACE2V(param) tracev param
495#else
496# define TRACE2(param) do { } while (0)
497# define TRACE2V(param) do { } while (0)
498#endif
499
500#endif
Note: See TracBrowser for help on using the repository browser.