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

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

kash: Hammering on threaded mode.

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