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

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

kash: Eliminate the 'temp' node field in nfile so we can share node trees later.

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