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

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

kash: New parser allocator for non-forked-mode.

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