2009-03-22 18:47:38 +01:00
|
|
|
/* $OpenBSD: main.c,v 1.45 2009/01/29 23:27:26 jaredy Exp $ */
|
2006-05-10 20:54:13 +02:00
|
|
|
/* $OpenBSD: tty.c,v 1.9 2006/03/14 22:08:01 deraadt Exp $ */
|
|
|
|
/* $OpenBSD: io.c,v 1.22 2006/03/17 16:30:13 millert Exp $ */
|
2009-03-22 18:31:17 +01:00
|
|
|
/* $OpenBSD: table.c,v 1.13 2009/01/17 22:06:44 millert Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009
|
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2006-08-23 00:49:38 +02:00
|
|
|
#define EXTERN
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2006-11-09 00:45:47 +01:00
|
|
|
#if HAVE_LANGINFO_CODESET
|
2006-11-05 13:11:14 +01:00
|
|
|
#include <langinfo.h>
|
2006-11-09 00:45:47 +01:00
|
|
|
#endif
|
|
|
|
#if HAVE_SETLOCALE_CTYPE
|
2006-11-05 13:11:14 +01:00
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-16 20:40:09 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.127 2009/05/16 18:40:07 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2006-11-16 14:35:07 +01:00
|
|
|
#if !HAVE_SETRESUGID
|
|
|
|
extern uid_t kshuid;
|
|
|
|
extern gid_t kshgid, kshegid;
|
|
|
|
#endif
|
|
|
|
|
2008-12-13 18:02:18 +01:00
|
|
|
static void reclaim(void);
|
2006-08-23 00:49:38 +02:00
|
|
|
static void remove_temps(struct temp *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static const char initifs[] = "IFS= \t\n";
|
|
|
|
|
|
|
|
static const char initsubs[] = "${PS2=> } ${PS3=#? } ${PS4=+ }";
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
static const char *initcoms[] = {
|
2006-08-23 00:49:38 +02:00
|
|
|
"typeset", "-r", initvsn, NULL,
|
2005-05-23 05:06:10 +02:00
|
|
|
"typeset", "-x", "SHELL", "PATH", "HOME", NULL,
|
2009-03-14 19:12:55 +01:00
|
|
|
"typeset", "-i10", "COLUMNS=0", "LINES=0", "OPTIND=1", NULL,
|
|
|
|
"typeset", "-Ui10", "PGRP", "PPID", "RANDOM", "USER_ID", NULL,
|
2008-12-29 22:34:22 +01:00
|
|
|
"eval", "typeset -i10 SECONDS=\"${SECONDS-0}\" TMOUT=\"${TMOUT-0}\"",
|
|
|
|
NULL,
|
2007-06-17 02:50:09 +02:00
|
|
|
"alias", "integer=typeset -i", "local=typeset", NULL,
|
2005-05-23 05:06:10 +02:00
|
|
|
"alias",
|
2005-06-09 00:22:24 +02:00
|
|
|
"hash=alias -t", /* not "alias -t --": hash -r needs to work */
|
|
|
|
"type=whence -v",
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef notyet_MKSH_UNEMPLOYED
|
|
|
|
/* the alias list must be constant, for the regression test suite */
|
2005-06-09 00:22:24 +02:00
|
|
|
"stop=kill -STOP",
|
|
|
|
"suspend=kill -STOP $$",
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-06-09 00:22:24 +02:00
|
|
|
"autoload=typeset -fu",
|
|
|
|
"functions=typeset -f",
|
|
|
|
"history=fc -l",
|
|
|
|
"nohup=nohup ",
|
|
|
|
"r=fc -e -",
|
2007-08-13 21:39:21 +02:00
|
|
|
"source=PATH=$PATH:. command .",
|
2005-06-09 00:22:24 +02:00
|
|
|
"login=exec login",
|
|
|
|
NULL,
|
2005-05-26 01:31:08 +02:00
|
|
|
/* this is what at&t ksh seems to track, with the addition of emacs */
|
2005-05-23 05:06:10 +02:00
|
|
|
"alias", "-tU",
|
2005-06-09 00:22:24 +02:00
|
|
|
"cat", "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
|
|
|
|
"make", "mv", "pr", "rm", "sed", "sh", "vi", "who", NULL,
|
2005-05-23 05:06:10 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int initio_done;
|
|
|
|
|
2008-10-15 12:25:01 +02:00
|
|
|
static struct env env;
|
|
|
|
struct env *e = &env;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
main(int argc, const char *argv[])
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-07-17 15:56:51 +02:00
|
|
|
int argi, i;
|
2005-05-23 05:06:10 +02:00
|
|
|
Source *s;
|
|
|
|
struct block *l;
|
|
|
|
int restricted, errexit;
|
2008-03-01 03:21:38 +01:00
|
|
|
const char **wp;
|
2005-05-23 05:06:10 +02:00
|
|
|
pid_t ppid;
|
2005-07-04 14:47:13 +02:00
|
|
|
struct tbl *vp;
|
|
|
|
struct stat s_stdin;
|
2006-11-09 23:08:08 +01:00
|
|
|
#if !defined(_PATH_DEFPATH) && defined(_CS_PATH)
|
|
|
|
size_t k;
|
|
|
|
char *cp;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
#if !HAVE_ARC4RANDOM
|
|
|
|
change_random((unsigned long)time(NULL) * getpid());
|
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* make sure argv[] is sane */
|
|
|
|
if (!*argv) {
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *empty_argv[] = {
|
|
|
|
"mksh", NULL
|
|
|
|
};
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-12 15:58:16 +01:00
|
|
|
argv = empty_argv;
|
2005-05-23 05:06:10 +02:00
|
|
|
argc = 1;
|
|
|
|
}
|
|
|
|
kshname = *argv;
|
|
|
|
|
2008-12-13 18:02:18 +01:00
|
|
|
ainit(&aperm); /* initialise permanent Area */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* set up base environment */
|
|
|
|
env.type = E_NONE;
|
2008-12-13 18:02:18 +01:00
|
|
|
ainit(&env.area);
|
2005-05-23 05:06:10 +02:00
|
|
|
newblock(); /* set up global l->vars and l->funs */
|
|
|
|
|
|
|
|
/* Do this first so output routines (eg, errorf, shellf) can work */
|
|
|
|
initio();
|
|
|
|
|
|
|
|
argi = parse_args(argv, OF_FIRSTTIME, NULL);
|
|
|
|
if (argi < 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
initvar();
|
|
|
|
|
|
|
|
initctypes();
|
|
|
|
|
|
|
|
inittraps();
|
|
|
|
|
|
|
|
coproc_init();
|
|
|
|
|
|
|
|
/* set up variable and command dictionaries */
|
2006-01-29 21:04:54 +01:00
|
|
|
ktinit(&taliases, APERM, 0);
|
|
|
|
ktinit(&aliases, APERM, 0);
|
2007-01-11 01:32:31 +01:00
|
|
|
#ifndef MKSH_SMALL
|
2006-01-29 21:04:54 +01:00
|
|
|
ktinit(&homedirs, APERM, 0);
|
2007-01-11 01:32:31 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* define shell keywords */
|
|
|
|
initkeywords();
|
|
|
|
|
|
|
|
/* define built-in commands */
|
2008-12-29 21:52:10 +01:00
|
|
|
ktinit(&builtins, APERM, 64); /* must be 2^n (currently 44 builtins) */
|
2007-08-20 00:06:27 +02:00
|
|
|
for (i = 0; mkshbuiltins[i].name != NULL; i++)
|
|
|
|
builtin(mkshbuiltins[i].name, mkshbuiltins[i].func);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
init_histvec();
|
|
|
|
|
2006-11-09 23:08:08 +01:00
|
|
|
#ifdef _PATH_DEFPATH
|
2005-05-23 05:06:10 +02:00
|
|
|
def_path = _PATH_DEFPATH;
|
2006-11-09 23:08:08 +01:00
|
|
|
#else
|
2006-08-24 22:32:53 +02:00
|
|
|
#ifdef _CS_PATH
|
2007-01-12 03:06:34 +01:00
|
|
|
if ((k = confstr(_CS_PATH, NULL, 0)) != (size_t)-1 && k > 0 &&
|
2008-12-13 18:02:18 +01:00
|
|
|
confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1)
|
2007-01-12 03:06:34 +01:00
|
|
|
def_path = cp;
|
2006-11-09 23:08:08 +01:00
|
|
|
else
|
|
|
|
#endif
|
2007-02-13 14:36:18 +01:00
|
|
|
/*
|
|
|
|
* this is uniform across all OSes unless it
|
|
|
|
* breaks somewhere; don't try to optimise,
|
|
|
|
* e.g. add stuff for Interix or remove /usr
|
|
|
|
* for HURD, because e.g. Debian GNU/HURD is
|
|
|
|
* "keeping a regular /usr"; this is supposed
|
|
|
|
* to be a sane 'basic' default PATH
|
|
|
|
*/
|
2006-11-09 23:08:08 +01:00
|
|
|
def_path = "/bin:/usr/bin:/sbin:/usr/sbin";
|
2005-06-24 17:36:57 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Set PATH to def_path (will set the path global variable).
|
|
|
|
* (import of environment below will probably change this setting).
|
|
|
|
*/
|
2005-07-04 14:47:13 +02:00
|
|
|
vp = global("PATH");
|
|
|
|
/* setstr can't fail here */
|
|
|
|
setstr(vp, def_path, KSH_RETURN_ERROR);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Turn on nohup by default for now - will change to off
|
|
|
|
* by default once people are aware of its existence
|
|
|
|
* (at&t ksh does not have a nohup option - it always sends
|
|
|
|
* the hup).
|
|
|
|
*/
|
|
|
|
Flag(FNOHUP) = 1;
|
|
|
|
|
2005-07-04 14:47:13 +02:00
|
|
|
/* Turn on brace expansion by default. AT&T kshs that have
|
2005-07-04 14:27:28 +02:00
|
|
|
* alternation always have it on.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
Flag(FBRACEEXPAND) = 1;
|
|
|
|
|
|
|
|
/* Set edit mode to emacs by default, may be overridden
|
|
|
|
* by the environment or the user. Also, we want tab completion
|
|
|
|
* on in vi by default. */
|
|
|
|
change_flag(FEMACS, OF_SPECIAL, 1);
|
2007-06-15 23:55:20 +02:00
|
|
|
#ifndef MKSH_NOVI
|
2005-05-23 05:06:10 +02:00
|
|
|
Flag(FVITABCOMPLETE) = 1;
|
2007-06-15 23:55:20 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-10-20 21:29:25 +02:00
|
|
|
#ifdef MKSH_BINSHREDUCED
|
2008-09-17 21:31:30 +02:00
|
|
|
/* Set FPOSIX if we're called as -sh or /bin/sh or so */
|
|
|
|
{
|
|
|
|
const char *cc;
|
|
|
|
|
|
|
|
cc = kshname;
|
|
|
|
i = 0; argi = 0;
|
|
|
|
while (cc[i] != '\0')
|
|
|
|
if ((cc[i++] | 2) == '/')
|
|
|
|
argi = i;
|
|
|
|
if (((cc[argi] | 0x20) == 's') && ((cc[argi + 1] | 0x20) == 'h'))
|
|
|
|
change_flag(FPOSIX, OF_FIRSTTIME, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* import environment */
|
|
|
|
if (environ != NULL)
|
2007-03-04 01:13:17 +01:00
|
|
|
for (wp = (const char **)environ; *wp != NULL; wp++)
|
2005-06-09 00:22:24 +02:00
|
|
|
typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
kshpid = procpid = getpid();
|
|
|
|
typeset(initifs, 0, 0, 0, 0); /* for security */
|
|
|
|
|
|
|
|
/* assign default shell variable values */
|
|
|
|
substitute(initsubs, 0);
|
|
|
|
|
|
|
|
/* Figure out the current working directory and set $PWD */
|
|
|
|
{
|
|
|
|
struct stat s_pwd, s_dot;
|
|
|
|
struct tbl *pwd_v = global("PWD");
|
|
|
|
char *pwd = str_val(pwd_v);
|
|
|
|
char *pwdx = pwd;
|
|
|
|
|
|
|
|
/* Try to use existing $PWD if it is valid */
|
|
|
|
if (pwd[0] != '/' ||
|
|
|
|
stat(pwd, &s_pwd) < 0 || stat(".", &s_dot) < 0 ||
|
|
|
|
s_pwd.st_dev != s_dot.st_dev ||
|
|
|
|
s_pwd.st_ino != s_dot.st_ino)
|
|
|
|
pwdx = NULL;
|
|
|
|
set_current_wd(pwdx);
|
|
|
|
if (current_wd[0])
|
|
|
|
simplify_path(current_wd);
|
|
|
|
/* Only set pwd if we know where we are or if it had a
|
|
|
|
* bogus value
|
|
|
|
*/
|
|
|
|
if (current_wd[0] || pwd != null)
|
|
|
|
/* setstr can't fail here */
|
|
|
|
setstr(pwd_v, current_wd, KSH_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
ppid = getppid();
|
2008-11-30 11:33:40 +01:00
|
|
|
#if !HAVE_ARC4RANDOM || !defined(MKSH_SMALL)
|
2007-10-25 17:23:10 +02:00
|
|
|
change_random(((unsigned long)kshname) ^
|
|
|
|
((unsigned long)time(NULL) * kshpid * ppid));
|
2008-11-30 11:33:40 +01:00
|
|
|
#endif
|
2007-08-12 15:42:23 +02:00
|
|
|
#if HAVE_ARC4RANDOM
|
|
|
|
Flag(FARC4RANDOM) = 2; /* use arc4random(3) until $RANDOM is written */
|
2005-05-23 16:22:03 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
for (wp = initcoms; *wp != NULL; wp++) {
|
2005-05-23 05:06:10 +02:00
|
|
|
shcomexec(wp);
|
2007-06-15 22:52:19 +02:00
|
|
|
while (*wp != NULL)
|
|
|
|
wp++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2006-11-10 07:27:09 +01:00
|
|
|
safe_prompt = (ksheuid = geteuid()) ? "$ " : "# ";
|
2005-07-04 14:47:13 +02:00
|
|
|
vp = global("PS1");
|
|
|
|
/* Set PS1 if unset or we are root and prompt doesn't contain a # */
|
|
|
|
if (!(vp->flag & ISSET) ||
|
|
|
|
(!ksheuid && !strchr(str_val(vp), '#')))
|
|
|
|
/* setstr can't fail here */
|
|
|
|
setstr(vp, safe_prompt, KSH_RETURN_ERROR);
|
2009-03-14 19:12:55 +01:00
|
|
|
setint(global("PGRP"), (mksh_uari_t)(kshpgrp = getpgrp()));
|
|
|
|
setint(global("PPID"), (mksh_uari_t)ppid);
|
|
|
|
setint(global("USER_ID"), (mksh_uari_t)ksheuid);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Set this before parsing arguments */
|
2006-11-16 14:35:07 +01:00
|
|
|
#if HAVE_SETRESUGID
|
2006-11-10 07:27:09 +01:00
|
|
|
Flag(FPRIVILEGED) = getuid() != ksheuid || getgid() != getegid();
|
2006-11-16 14:35:07 +01:00
|
|
|
#else
|
|
|
|
Flag(FPRIVILEGED) = (kshuid = getuid()) != ksheuid ||
|
|
|
|
(kshgid = getgid()) != (kshegid = getegid());
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* this to note if monitor is set on command line (see below) */
|
|
|
|
Flag(FMONITOR) = 127;
|
|
|
|
argi = parse_args(argv, OF_CMDLINE, NULL);
|
|
|
|
if (argi < 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (Flag(FCOMMAND)) {
|
|
|
|
s = pushs(SSTRING, ATEMP);
|
|
|
|
if (!(s->start = s->str = argv[argi++]))
|
|
|
|
errorf("-c requires an argument");
|
2009-05-16 20:40:09 +02:00
|
|
|
#ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT
|
2009-04-05 13:44:56 +02:00
|
|
|
/* compatibility to MidnightBSD 0.1 /bin/sh (not desired) */
|
|
|
|
if (argv[argi] && !strcmp(argv[argi], "--"))
|
|
|
|
++argi;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
if (argv[argi])
|
|
|
|
kshname = argv[argi++];
|
|
|
|
} else if (argi < argc && !Flag(FSTDIN)) {
|
|
|
|
s = pushs(SFILE, ATEMP);
|
|
|
|
s->file = kshname = argv[argi++];
|
2005-06-09 00:22:24 +02:00
|
|
|
s->u.shf = shf_open(s->file, O_RDONLY, 0,
|
|
|
|
SHF_MAPHI | SHF_CLEXEC);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (s->u.shf == NULL) {
|
|
|
|
exstat = 127; /* POSIX */
|
|
|
|
errorf("%s: %s", s->file, strerror(errno));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Flag(FSTDIN) = 1;
|
|
|
|
s = pushs(SSTDIN, ATEMP);
|
|
|
|
s->file = "<stdin>";
|
|
|
|
s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0),
|
|
|
|
NULL);
|
|
|
|
if (isatty(0) && isatty(2)) {
|
|
|
|
Flag(FTALKING) = Flag(FTALKING_I) = 1;
|
|
|
|
/* The following only if isatty(0) */
|
|
|
|
s->flags |= SF_TTY;
|
|
|
|
s->u.shf->flags |= SHF_INTERRUPT;
|
|
|
|
s->file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This bizarreness is mandated by POSIX */
|
2005-07-04 14:47:13 +02:00
|
|
|
if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) &&
|
|
|
|
Flag(FTALKING))
|
|
|
|
reset_nonblock(0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-01-12 11:18:22 +01:00
|
|
|
/* initialise job control */
|
2009-04-05 14:35:32 +02:00
|
|
|
j_init();
|
|
|
|
/* Do this after j_init(), as tty_fd is not initialised until then */
|
2006-11-12 11:44:42 +01:00
|
|
|
if (Flag(FTALKING)) {
|
2007-02-10 22:59:15 +01:00
|
|
|
#ifndef MKSH_ASSUME_UTF8
|
2007-01-06 18:08:14 +01:00
|
|
|
#define isuc(x) (((x) != NULL) && \
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
(stristr((x), "UTF-8") || stristr((x), "utf8")))
|
2006-11-12 11:44:42 +01:00
|
|
|
/* Check if we're in a UTF-8 locale */
|
2008-12-04 19:11:08 +01:00
|
|
|
if (!UTFMODE) {
|
2008-03-28 19:47:52 +01:00
|
|
|
const char *ccp;
|
2008-03-01 14:57:36 +01:00
|
|
|
|
2008-05-04 03:58:15 +02:00
|
|
|
#if HAVE_SETLOCALE_CTYPE
|
2008-03-28 19:47:52 +01:00
|
|
|
ccp = setlocale(LC_CTYPE, "");
|
2006-11-12 11:44:42 +01:00
|
|
|
#if HAVE_LANGINFO_CODESET
|
2008-03-28 19:47:52 +01:00
|
|
|
if (!isuc(ccp))
|
|
|
|
ccp = nl_langinfo(CODESET);
|
2008-05-04 03:58:15 +02:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
ccp = getenv("LC_ALL");
|
|
|
|
if (!ccp || !*ccp) {
|
|
|
|
ccp = getenv("LC_CTYPE");
|
|
|
|
if (!ccp || !*ccp)
|
|
|
|
ccp = getenv("LANG");
|
|
|
|
}
|
2006-11-12 11:44:42 +01:00
|
|
|
#endif
|
2008-12-04 19:11:08 +01:00
|
|
|
UTFMODE = isuc(ccp);
|
2006-11-12 11:44:42 +01:00
|
|
|
}
|
2007-01-06 18:08:14 +01:00
|
|
|
#undef isuc
|
2007-02-10 22:59:15 +01:00
|
|
|
#else
|
2008-12-04 19:11:08 +01:00
|
|
|
UTFMODE = 1;
|
2006-11-12 11:44:42 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
x_init();
|
2006-11-12 11:44:42 +01:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
l = e->loc;
|
|
|
|
l->argv = &argv[argi - 1];
|
|
|
|
l->argc = argc - argi;
|
2007-03-04 01:13:17 +01:00
|
|
|
l->argv[0] = kshname;
|
2005-05-23 05:06:10 +02:00
|
|
|
getopts_reset(1);
|
|
|
|
|
|
|
|
/* Disable during .profile/ENV reading */
|
|
|
|
restricted = Flag(FRESTRICTED);
|
|
|
|
Flag(FRESTRICTED) = 0;
|
|
|
|
errexit = Flag(FERREXIT);
|
|
|
|
Flag(FERREXIT) = 0;
|
|
|
|
|
|
|
|
/* Do this before profile/$ENV so that if it causes problems in them,
|
|
|
|
* user will know why things broke.
|
|
|
|
*/
|
|
|
|
if (!current_wd[0] && Flag(FTALKING))
|
|
|
|
warningf(false, "Cannot determine current working directory");
|
|
|
|
|
|
|
|
if (Flag(FLOGIN)) {
|
|
|
|
include(KSH_SYSTEM_PROFILE, 0, NULL, 1);
|
|
|
|
if (!Flag(FPRIVILEGED))
|
|
|
|
include(substitute("$HOME/.profile", 0), 0,
|
|
|
|
NULL, 1);
|
|
|
|
}
|
|
|
|
if (Flag(FPRIVILEGED))
|
|
|
|
include("/etc/suid_profile", 0, NULL, 1);
|
2006-06-21 21:27:35 +02:00
|
|
|
else if (Flag(FTALKING)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
char *env_file;
|
|
|
|
|
|
|
|
/* include $ENV */
|
2006-05-08 13:42:36 +02:00
|
|
|
env_file = substitute(substitute("${ENV:-~/.mkshrc}", 0),
|
|
|
|
DOTILDE);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*env_file != '\0')
|
|
|
|
include(env_file, 0, NULL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restricted) {
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *restr_com[] = {
|
2005-05-23 05:06:10 +02:00
|
|
|
"typeset", "-r", "PATH",
|
|
|
|
"ENV", "SHELL",
|
|
|
|
NULL
|
|
|
|
};
|
2007-03-04 01:13:17 +01:00
|
|
|
shcomexec(restr_com);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* After typeset command... */
|
|
|
|
Flag(FRESTRICTED) = 1;
|
|
|
|
}
|
|
|
|
if (errexit)
|
|
|
|
Flag(FERREXIT) = 1;
|
|
|
|
|
|
|
|
if (Flag(FTALKING)) {
|
|
|
|
hist_init(s);
|
|
|
|
alarm_init();
|
|
|
|
} else
|
|
|
|
Flag(FTRACKALL) = 1; /* set after ENV */
|
|
|
|
|
|
|
|
shell(s, true); /* doesn't return */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
include(const char *name, int argc, const char **argv, int intr_ok)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
Source *volatile s = NULL;
|
|
|
|
struct shf *shf;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char **volatile old_argv;
|
2005-05-23 05:06:10 +02:00
|
|
|
volatile int old_argc;
|
|
|
|
int i;
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (shf == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (argv) {
|
|
|
|
old_argv = e->loc->argv;
|
|
|
|
old_argc = e->loc->argc;
|
|
|
|
} else {
|
|
|
|
old_argv = NULL;
|
|
|
|
old_argc = 0;
|
|
|
|
}
|
|
|
|
newenv(E_INCL);
|
|
|
|
i = sigsetjmp(e->jbuf, 0);
|
|
|
|
if (i) {
|
|
|
|
quitenv(s ? s->u.shf : NULL);
|
|
|
|
if (old_argv) {
|
|
|
|
e->loc->argv = old_argv;
|
|
|
|
e->loc->argc = old_argc;
|
|
|
|
}
|
|
|
|
switch (i) {
|
|
|
|
case LRETURN:
|
|
|
|
case LERROR:
|
|
|
|
return exstat & 0xff; /* see below */
|
|
|
|
case LINTR:
|
|
|
|
/* intr_ok is set if we are including .profile or $ENV.
|
|
|
|
* If user ^Cs out, we don't want to kill the shell...
|
|
|
|
*/
|
|
|
|
if (intr_ok && (exstat - 128) != SIGTERM)
|
|
|
|
return 1;
|
2005-06-09 00:34:03 +02:00
|
|
|
/* FALLTHRU */
|
2005-05-23 05:06:10 +02:00
|
|
|
case LEXIT:
|
|
|
|
case LLEAVE:
|
|
|
|
case LSHELL:
|
|
|
|
unwind(i);
|
2005-06-09 00:34:03 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("include: %d", i);
|
2005-06-09 00:34:03 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argv) {
|
|
|
|
e->loc->argv = argv;
|
|
|
|
e->loc->argc = argc;
|
|
|
|
}
|
|
|
|
s = pushs(SFILE, ATEMP);
|
|
|
|
s->u.shf = shf;
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(s->file, name, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
i = shell(s, false);
|
|
|
|
quitenv(s->u.shf);
|
|
|
|
if (old_argv) {
|
|
|
|
e->loc->argv = old_argv;
|
|
|
|
e->loc->argc = old_argc;
|
|
|
|
}
|
|
|
|
return i & 0xff; /* & 0xff to ensure value not -1 */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
command(const char *comm)
|
|
|
|
{
|
|
|
|
Source *s;
|
|
|
|
|
|
|
|
s = pushs(SSTRING, ATEMP);
|
|
|
|
s->start = s->str = comm;
|
|
|
|
return shell(s, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* run the commands from the input source, returning status.
|
|
|
|
*/
|
|
|
|
int
|
2005-06-09 00:22:24 +02:00
|
|
|
shell(Source * volatile s, volatile int toplevel)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
volatile int wastty = s->flags & SF_TTY;
|
|
|
|
volatile int attempts = 13;
|
|
|
|
volatile int interactive = Flag(FTALKING) && toplevel;
|
|
|
|
Source *volatile old_source = source;
|
|
|
|
int i;
|
|
|
|
|
2007-04-15 12:45:59 +02:00
|
|
|
s->flags |= SF_FIRST; /* enable UTF-8 BOM check */
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
newenv(E_PARSE);
|
|
|
|
if (interactive)
|
|
|
|
really_exit = 0;
|
|
|
|
i = sigsetjmp(e->jbuf, 0);
|
|
|
|
if (i) {
|
|
|
|
switch (i) {
|
|
|
|
case LINTR: /* we get here if SIGINT not caught or ignored */
|
|
|
|
case LERROR:
|
|
|
|
case LSHELL:
|
|
|
|
if (interactive) {
|
|
|
|
if (i == LINTR)
|
2007-07-22 15:34:52 +02:00
|
|
|
shellf("\n");
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Reset any eof that was read as part of a
|
|
|
|
* multiline command.
|
|
|
|
*/
|
|
|
|
if (Flag(FIGNOREEOF) && s->type == SEOF &&
|
|
|
|
wastty)
|
|
|
|
s->type = SSTDIN;
|
|
|
|
/* Used by exit command to get back to
|
|
|
|
* top level shell. Kind of strange since
|
|
|
|
* interactive is set if we are reading from
|
|
|
|
* a tty, but to have stopped jobs, one only
|
|
|
|
* needs FMONITOR set (not FTALKING/SF_TTY)...
|
|
|
|
*/
|
|
|
|
/* toss any input we have so far */
|
|
|
|
s->start = s->str = null;
|
|
|
|
break;
|
|
|
|
}
|
2005-06-09 00:34:03 +02:00
|
|
|
/* FALLTHRU */
|
2005-05-23 05:06:10 +02:00
|
|
|
case LEXIT:
|
|
|
|
case LLEAVE:
|
|
|
|
case LRETURN:
|
|
|
|
source = old_source;
|
|
|
|
quitenv(NULL);
|
|
|
|
unwind(i); /* keep on going */
|
2005-06-09 00:34:03 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
|
|
|
source = old_source;
|
|
|
|
quitenv(NULL);
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("shell: %d", i);
|
2005-06-09 00:34:03 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
if (trap)
|
|
|
|
runtraps(0);
|
|
|
|
|
|
|
|
if (s->next == NULL) {
|
|
|
|
if (Flag(FVERBOSE))
|
|
|
|
s->flags |= SF_ECHO;
|
|
|
|
else
|
|
|
|
s->flags &= ~SF_ECHO;
|
|
|
|
}
|
|
|
|
if (interactive) {
|
|
|
|
j_notify();
|
|
|
|
set_prompt(PS1, s);
|
|
|
|
}
|
|
|
|
t = compile(s);
|
|
|
|
if (t != NULL && t->type == TEOF) {
|
|
|
|
if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
|
|
|
|
shellf("Use 'exit' to leave ksh\n");
|
|
|
|
s->type = SSTDIN;
|
|
|
|
} else if (wastty && !really_exit &&
|
|
|
|
j_stopped_running()) {
|
|
|
|
really_exit = 1;
|
|
|
|
s->type = SSTDIN;
|
|
|
|
} else {
|
2006-01-30 13:37:24 +01:00
|
|
|
/* this for POSIX which says EXIT traps
|
2005-05-23 05:06:10 +02:00
|
|
|
* shall be taken in the environment
|
|
|
|
* immediately after the last command
|
|
|
|
* executed.
|
|
|
|
*/
|
|
|
|
if (toplevel)
|
|
|
|
unwind(LEXIT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t && (!Flag(FNOEXEC) || (s->flags & SF_TTY)))
|
2009-03-22 18:47:38 +01:00
|
|
|
exstat = execute(t, 0, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (t != NULL && t->type != TEOF && interactive && really_exit)
|
|
|
|
really_exit = 0;
|
|
|
|
|
2008-12-13 18:02:18 +01:00
|
|
|
reclaim();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
quitenv(NULL);
|
|
|
|
source = old_source;
|
|
|
|
return exstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return to closest error handler or shell(), exit if none found */
|
|
|
|
void
|
|
|
|
unwind(int i)
|
|
|
|
{
|
|
|
|
/* ordering for EXIT vs ERR is a bit odd (this is what at&t ksh does) */
|
|
|
|
if (i == LEXIT || (Flag(FERREXIT) && (i == LERROR || i == LINTR) &&
|
|
|
|
sigtraps[SIGEXIT_].trap)) {
|
|
|
|
runtrap(&sigtraps[SIGEXIT_]);
|
|
|
|
i = LLEAVE;
|
|
|
|
} else if (Flag(FERREXIT) && (i == LERROR || i == LINTR)) {
|
|
|
|
runtrap(&sigtraps[SIGERR_]);
|
|
|
|
i = LLEAVE;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
switch (e->type) {
|
|
|
|
case E_PARSE:
|
|
|
|
case E_FUNC:
|
|
|
|
case E_INCL:
|
|
|
|
case E_LOOP:
|
|
|
|
case E_ERRH:
|
|
|
|
siglongjmp(e->jbuf, i);
|
2005-06-09 00:34:03 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
case E_NONE:
|
|
|
|
if (i == LINTR)
|
|
|
|
e->flags |= EF_FAKE_SIGDIE;
|
2005-06-09 00:34:03 +02:00
|
|
|
/* FALLTHRU */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
|
|
|
quitenv(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
newenv(int type)
|
|
|
|
{
|
|
|
|
struct env *ep;
|
2009-04-07 21:43:28 +02:00
|
|
|
char *cp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-07 21:43:28 +02:00
|
|
|
/*
|
|
|
|
* struct env includes ALLOC_ITEM for alignment constraints
|
|
|
|
* so first get the actually used memory, then assign it
|
|
|
|
*/
|
|
|
|
cp = alloc(sizeof (struct env) - ALLOC_SIZE, ATEMP);
|
|
|
|
ep = (void *)(cp - ALLOC_SIZE); /* undo what alloc() did */
|
|
|
|
/* initialise public members of struct env (not the ALLOC_ITEM) */
|
2008-12-13 18:02:18 +01:00
|
|
|
ainit(&ep->area);
|
2009-04-07 21:43:28 +02:00
|
|
|
ep->oenv = e;
|
2005-05-23 05:06:10 +02:00
|
|
|
ep->loc = e->loc;
|
|
|
|
ep->savefd = NULL;
|
|
|
|
ep->temps = NULL;
|
2009-04-07 21:43:28 +02:00
|
|
|
ep->type = type;
|
|
|
|
ep->flags = 0;
|
|
|
|
/* jump buffer is invalid because flags == 0 */
|
2005-05-23 05:06:10 +02:00
|
|
|
e = ep;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
quitenv(struct shf *shf)
|
|
|
|
{
|
|
|
|
struct env *ep = e;
|
2009-04-07 21:43:28 +02:00
|
|
|
char *cp;
|
2005-05-23 05:06:10 +02:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (ep->oenv && ep->oenv->loc != ep->loc)
|
|
|
|
popblock();
|
|
|
|
if (ep->savefd != NULL) {
|
|
|
|
for (fd = 0; fd < NUFILE; fd++)
|
|
|
|
/* if ep->savefd[fd] < 0, means fd was closed */
|
|
|
|
if (ep->savefd[fd])
|
|
|
|
restfd(fd, ep->savefd[fd]);
|
2005-06-09 00:22:24 +02:00
|
|
|
if (ep->savefd[2]) /* Clear any write errors */
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_reopen(2, SHF_WR, shl_out);
|
|
|
|
}
|
|
|
|
/* Bottom of the stack.
|
|
|
|
* Either main shell is exiting or cleanup_parents_env() was called.
|
|
|
|
*/
|
|
|
|
if (ep->oenv == NULL) {
|
|
|
|
if (ep->type == E_NONE) { /* Main shell exiting? */
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FTALKING))
|
|
|
|
hist_finish();
|
2006-11-10 06:23:14 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
j_exit();
|
|
|
|
if (ep->flags & EF_FAKE_SIGDIE) {
|
|
|
|
int sig = exstat - 128;
|
|
|
|
|
|
|
|
/* ham up our death a bit (at&t ksh
|
|
|
|
* only seems to do this for SIGTERM)
|
|
|
|
* Don't do it for SIGQUIT, since we'd
|
|
|
|
* dump a core..
|
|
|
|
*/
|
|
|
|
if ((sig == SIGINT || sig == SIGTERM) &&
|
2008-05-15 17:24:11 +02:00
|
|
|
(kshpgrp == kshpid)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
setsig(&sigtraps[sig], SIG_DFL,
|
2005-06-09 00:22:24 +02:00
|
|
|
SS_RESTORE_CURR | SS_FORCE);
|
2005-05-23 05:06:10 +02:00
|
|
|
kill(0, sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shf)
|
|
|
|
shf_close(shf);
|
2008-12-13 18:02:18 +01:00
|
|
|
reclaim();
|
2005-05-23 05:06:10 +02:00
|
|
|
exit(exstat);
|
|
|
|
}
|
|
|
|
if (shf)
|
|
|
|
shf_close(shf);
|
2008-12-13 18:02:18 +01:00
|
|
|
reclaim();
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
e = e->oenv;
|
2009-04-07 21:43:28 +02:00
|
|
|
|
|
|
|
/* free the struct env - tricky due to the ALLOC_ITEM inside */
|
|
|
|
cp = (void *)ep;
|
|
|
|
afree(cp + ALLOC_SIZE, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called after a fork to cleanup stuff left over from parents environment */
|
|
|
|
void
|
|
|
|
cleanup_parents_env(void)
|
|
|
|
{
|
|
|
|
struct env *ep;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Don't clean up temporary files - parent will probably need them.
|
|
|
|
* Also, can't easily reclaim memory since variables, etc. could be
|
|
|
|
* anywhere.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* close all file descriptors hiding in savefd */
|
|
|
|
for (ep = e; ep; ep = ep->oenv) {
|
|
|
|
if (ep->savefd) {
|
|
|
|
for (fd = 0; fd < NUFILE; fd++)
|
|
|
|
if (ep->savefd[fd] > 0)
|
|
|
|
close(ep->savefd[fd]);
|
2008-12-13 18:02:18 +01:00
|
|
|
afree(ep->savefd, &ep->area);
|
2005-05-23 05:06:10 +02:00
|
|
|
ep->savefd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e->oenv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called just before an execve cleanup stuff temporary files */
|
|
|
|
void
|
|
|
|
cleanup_proc_env(void)
|
|
|
|
{
|
|
|
|
struct env *ep;
|
|
|
|
|
|
|
|
for (ep = e; ep; ep = ep->oenv)
|
|
|
|
remove_temps(ep->temps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove temp files and free ATEMP Area */
|
|
|
|
static void
|
2008-12-13 18:02:18 +01:00
|
|
|
reclaim(void)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
remove_temps(e->temps);
|
|
|
|
e->temps = NULL;
|
2008-12-13 18:02:18 +01:00
|
|
|
afreeall(&e->area);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_temps(struct temp *tp)
|
|
|
|
{
|
|
|
|
for (; tp != NULL; tp = tp->next)
|
2007-10-25 17:19:16 +02:00
|
|
|
if (tp->pid == procpid)
|
2005-05-23 05:06:10 +02:00
|
|
|
unlink(tp->name);
|
|
|
|
}
|
|
|
|
|
2007-01-12 11:18:22 +01:00
|
|
|
/* Initialise tty_fd. Used for saving/reseting tty modes upon
|
2005-05-23 05:06:10 +02:00
|
|
|
* foreground job completion and for setting up tty process group.
|
|
|
|
*/
|
|
|
|
void
|
2008-12-29 21:47:16 +01:00
|
|
|
tty_init(bool init_ttystate, bool need_tty)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-12-29 21:47:16 +01:00
|
|
|
bool do_close = true;
|
2005-06-09 00:22:24 +02:00
|
|
|
int tfd;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (tty_fd >= 0) {
|
|
|
|
close(tty_fd);
|
|
|
|
tty_fd = -1;
|
|
|
|
}
|
|
|
|
tty_devtty = 1;
|
|
|
|
|
2007-07-01 21:04:53 +02:00
|
|
|
#ifdef _UWIN
|
|
|
|
if (isatty(3))
|
|
|
|
tfd = 3;
|
|
|
|
else
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((tfd = open("/dev/tty", O_RDWR, 0)) < 0) {
|
2006-05-10 20:54:13 +02:00
|
|
|
tty_devtty = 0;
|
2008-12-29 21:47:16 +01:00
|
|
|
if (need_tty)
|
|
|
|
warningf(false,
|
|
|
|
"No controlling tty (open /dev/tty: %s)",
|
|
|
|
strerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (tfd < 0) {
|
2008-12-29 21:47:16 +01:00
|
|
|
do_close = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (isatty(0))
|
|
|
|
tfd = 0;
|
|
|
|
else if (isatty(2))
|
|
|
|
tfd = 2;
|
|
|
|
else {
|
2008-12-29 21:47:16 +01:00
|
|
|
if (need_tty)
|
|
|
|
warningf(false,
|
|
|
|
"Can't find tty file descriptor");
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((tty_fd = fcntl(tfd, F_DUPFD, FDBASE)) < 0) {
|
2008-12-29 21:47:16 +01:00
|
|
|
if (need_tty)
|
|
|
|
warningf(false, "j_ttyinit: dup of tty fd failed: %s",
|
|
|
|
strerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (fcntl(tty_fd, F_SETFD, FD_CLOEXEC) < 0) {
|
2008-12-29 21:47:16 +01:00
|
|
|
if (need_tty)
|
|
|
|
warningf(false,
|
|
|
|
"j_ttyinit: can't set close-on-exec flag: %s",
|
|
|
|
strerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
close(tty_fd);
|
|
|
|
tty_fd = -1;
|
|
|
|
} else if (init_ttystate)
|
|
|
|
tcgetattr(tty_fd, &tty_state);
|
|
|
|
if (do_close)
|
|
|
|
close(tfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_close(void)
|
|
|
|
{
|
|
|
|
if (tty_fd >= 0) {
|
|
|
|
close(tty_fd);
|
|
|
|
tty_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A shell error occurred (eg, syntax error, etc.) */
|
|
|
|
void
|
|
|
|
errorf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
shl_stdout_ok = 0; /* debugging: note that stdout not valid */
|
|
|
|
exstat = 1;
|
2008-11-10 20:33:08 +01:00
|
|
|
if (*fmt != 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
error_prefix(true);
|
|
|
|
va_start(va, fmt);
|
|
|
|
shf_vfprintf(shl_out, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
shf_putchar('\n', shl_out);
|
|
|
|
}
|
|
|
|
shf_flush(shl_out);
|
|
|
|
unwind(LERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* like errorf(), but no unwind is done */
|
|
|
|
void
|
2006-11-10 02:13:52 +01:00
|
|
|
warningf(bool fileline, const char *fmt, ...)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
error_prefix(fileline);
|
|
|
|
va_start(va, fmt);
|
|
|
|
shf_vfprintf(shl_out, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
shf_putchar('\n', shl_out);
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used by built-in utilities to prefix shell and utility name to message
|
|
|
|
* (also unwinds environments for special builtins).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
bi_errorf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
shl_stdout_ok = 0; /* debugging: note that stdout not valid */
|
|
|
|
exstat = 1;
|
2008-11-10 20:33:08 +01:00
|
|
|
if (*fmt != 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
error_prefix(true);
|
|
|
|
/* not set when main() calls parse_args() */
|
|
|
|
if (builtin_argv0)
|
|
|
|
shf_fprintf(shl_out, "%s: ", builtin_argv0);
|
|
|
|
va_start(va, fmt);
|
|
|
|
shf_vfprintf(shl_out, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
shf_putchar('\n', shl_out);
|
|
|
|
}
|
|
|
|
shf_flush(shl_out);
|
|
|
|
/* POSIX special builtins and ksh special builtins cause
|
|
|
|
* non-interactive shells to exit.
|
|
|
|
* XXX odd use of KEEPASN; also may not want LERROR here
|
|
|
|
*/
|
2005-07-04 14:27:28 +02:00
|
|
|
if (builtin_flag & SPEC_BI) {
|
2005-05-23 05:06:10 +02:00
|
|
|
builtin_argv0 = NULL;
|
|
|
|
unwind(LERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when something that shouldn't happen does */
|
2008-11-15 09:42:36 +01:00
|
|
|
void
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_verrorf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
shf_fprintf(shl_out, "internal error: ");
|
|
|
|
shf_vfprintf(shl_out, fmt, ap);
|
|
|
|
shf_putchar('\n', shl_out);
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
void
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf(const char *fmt, ...)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_verrorf(fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
unwind(LERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
internal_warningf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
internal_verrorf(fmt, va);
|
2005-05-23 05:06:10 +02:00
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* used by error reporting functions to print "ksh: .kshrc[25]: " */
|
|
|
|
void
|
2006-11-10 02:13:52 +01:00
|
|
|
error_prefix(bool fileline)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
/* Avoid foo: foo[2]: ... */
|
|
|
|
if (!fileline || !source || !source->file ||
|
|
|
|
strcmp(source->file, kshname) != 0)
|
|
|
|
shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
|
|
|
|
if (fileline && source && source->file != NULL) {
|
|
|
|
shf_fprintf(shl_out, "%s[%d]: ", source->file,
|
|
|
|
source->errline > 0 ? source->errline : source->line);
|
|
|
|
source->errline = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* printf to shl_out (stderr) with flush */
|
|
|
|
void
|
|
|
|
shellf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
if (!initio_done) /* shl_out may not be set up yet... */
|
|
|
|
return;
|
|
|
|
va_start(va, fmt);
|
|
|
|
shf_vfprintf(shl_out, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* printf to shl_stdout (stdout) */
|
|
|
|
void
|
|
|
|
shprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
if (!shl_stdout_ok)
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("shl_stdout not valid");
|
2005-05-23 05:06:10 +02:00
|
|
|
va_start(va, fmt);
|
|
|
|
shf_vfprintf(shl_stdout, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
|
|
|
|
int
|
|
|
|
can_seek(int fd)
|
|
|
|
{
|
|
|
|
struct stat statb;
|
|
|
|
|
|
|
|
return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
|
|
|
|
SHF_UNBUF : 0;
|
|
|
|
}
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
struct shf shf_iob[3];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
initio(void)
|
|
|
|
{
|
|
|
|
shf_fdopen(1, SHF_WR, shl_stdout); /* force buffer allocation */
|
|
|
|
shf_fdopen(2, SHF_WR, shl_out);
|
|
|
|
shf_fdopen(2, SHF_WR, shl_spare); /* force buffer allocation */
|
|
|
|
initio_done = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A dup2() with error checking */
|
|
|
|
int
|
2008-04-01 22:40:22 +02:00
|
|
|
ksh_dup2(int ofd, int nfd, bool errok)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-04-01 22:40:22 +02:00
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-04-01 22:40:22 +02:00
|
|
|
if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF))
|
2005-05-23 05:06:10 +02:00
|
|
|
errorf("too many files open in shell");
|
|
|
|
|
2008-04-01 22:40:22 +02:00
|
|
|
#ifdef __ultrix
|
|
|
|
if (rv >= 0)
|
|
|
|
fcntl(nfd, F_SETFD, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* move fd from user space (0<=fd<10) to shell space (fd>=10),
|
|
|
|
* set close-on-exec flag.
|
|
|
|
*/
|
2007-06-07 01:28:17 +02:00
|
|
|
short
|
2006-05-10 20:54:13 +02:00
|
|
|
savefd(int fd)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-06-07 01:28:17 +02:00
|
|
|
int nfd = fd;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 &&
|
|
|
|
errno == EBADF)
|
|
|
|
return -1;
|
|
|
|
if (nfd < 0 || nfd > SHRT_MAX)
|
|
|
|
errorf("too many files open in shell");
|
2005-05-23 05:06:10 +02:00
|
|
|
fcntl(nfd, F_SETFD, FD_CLOEXEC);
|
2007-06-07 01:28:17 +02:00
|
|
|
return ((short)nfd);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
restfd(int fd, int ofd)
|
|
|
|
{
|
|
|
|
if (fd == 2)
|
|
|
|
shf_flush(&shf_iob[fd]);
|
|
|
|
if (ofd < 0) /* original fd closed */
|
|
|
|
close(fd);
|
|
|
|
else if (fd != ofd) {
|
|
|
|
ksh_dup2(ofd, fd, true); /* XXX: what to do if this fails? */
|
|
|
|
close(ofd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
openpipe(int *pv)
|
|
|
|
{
|
2006-05-10 20:54:13 +02:00
|
|
|
int lpv[2];
|
|
|
|
|
|
|
|
if (pipe(lpv) < 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
errorf("can't create pipe - try again");
|
2006-05-10 20:54:13 +02:00
|
|
|
pv[0] = savefd(lpv[0]);
|
|
|
|
if (pv[0] != lpv[0])
|
|
|
|
close(lpv[0]);
|
|
|
|
pv[1] = savefd(lpv[1]);
|
|
|
|
if (pv[1] != lpv[1])
|
|
|
|
close(lpv[1]);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
closepipe(int *pv)
|
|
|
|
{
|
|
|
|
close(pv[0]);
|
|
|
|
close(pv[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
|
|
|
|
* a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
check_fd(const char *name, int mode, const char **emsgp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int fd, fl;
|
|
|
|
|
2008-07-09 23:32:45 +02:00
|
|
|
if (name[0] == 'p' && !name[1])
|
|
|
|
return (coproc_getfd(mode, emsgp));
|
|
|
|
for (fd = 0; ksh_isdigit(*name); ++name)
|
|
|
|
fd = (fd * 10) + *name - '0';
|
|
|
|
if (*name || fd >= FDBASE) {
|
|
|
|
if (emsgp)
|
|
|
|
*emsgp = "illegal file descriptor name";
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if ((fl = fcntl(fd, F_GETFL, 0)) < 0) {
|
|
|
|
if (emsgp)
|
|
|
|
*emsgp = "bad file descriptor";
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
fl &= O_ACCMODE;
|
|
|
|
/* X_OK is a kludge to disable this check for dups (x<&1):
|
|
|
|
* historical shells never did this check (XXX don't know what
|
|
|
|
* posix has to say).
|
|
|
|
*/
|
|
|
|
if (!(mode & X_OK) && fl != O_RDWR && (
|
|
|
|
((mode & R_OK) && fl != O_RDONLY) ||
|
|
|
|
((mode & W_OK) && fl != O_WRONLY))) {
|
|
|
|
if (emsgp)
|
|
|
|
*emsgp = (fl == O_WRONLY) ?
|
|
|
|
"fd not open for reading" :
|
|
|
|
"fd not open for writing";
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (fd);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called once from main */
|
|
|
|
void
|
|
|
|
coproc_init(void)
|
|
|
|
{
|
|
|
|
coproc.read = coproc.readw = coproc.write = -1;
|
|
|
|
coproc.njobs = 0;
|
|
|
|
coproc.id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called by c_read() when eof is read - close fd if it is the co-process fd */
|
|
|
|
void
|
|
|
|
coproc_read_close(int fd)
|
|
|
|
{
|
|
|
|
if (coproc.read >= 0 && fd == coproc.read) {
|
|
|
|
coproc_readw_close(fd);
|
|
|
|
close(coproc.read);
|
|
|
|
coproc.read = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called by c_read() and by iosetup() to close the other side of the
|
|
|
|
* read pipe, so reads will actually terminate.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
coproc_readw_close(int fd)
|
|
|
|
{
|
|
|
|
if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
|
|
|
|
close(coproc.readw);
|
|
|
|
coproc.readw = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called by c_print when a write to a fd fails with EPIPE and by iosetup
|
|
|
|
* when co-process input is dup'd
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
coproc_write_close(int fd)
|
|
|
|
{
|
|
|
|
if (coproc.write >= 0 && fd == coproc.write) {
|
|
|
|
close(coproc.write);
|
|
|
|
coproc.write = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called to check for existence of/value of the co-process file descriptor.
|
|
|
|
* (Used by check_fd() and by c_read/c_print to deal with -p option).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
coproc_getfd(int mode, const char **emsgp)
|
|
|
|
{
|
|
|
|
int fd = (mode & R_OK) ? coproc.read : coproc.write;
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
return fd;
|
|
|
|
if (emsgp)
|
|
|
|
*emsgp = "no coprocess";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called to close file descriptors related to the coprocess (if any)
|
|
|
|
* Should be called with SIGCHLD blocked.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
coproc_cleanup(int reuse)
|
|
|
|
{
|
|
|
|
/* This to allow co-processes to share output pipe */
|
|
|
|
if (!reuse || coproc.readw < 0 || coproc.read < 0) {
|
|
|
|
if (coproc.read >= 0) {
|
|
|
|
close(coproc.read);
|
|
|
|
coproc.read = -1;
|
|
|
|
}
|
|
|
|
if (coproc.readw >= 0) {
|
|
|
|
close(coproc.readw);
|
|
|
|
coproc.readw = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (coproc.write >= 0) {
|
|
|
|
close(coproc.write);
|
|
|
|
coproc.write = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct temp *
|
2008-12-13 18:02:18 +01:00
|
|
|
maketemp(Area *ap, Temp_type type, struct temp **tlist)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct temp *tp;
|
|
|
|
int len;
|
|
|
|
int fd;
|
|
|
|
char *pathname;
|
|
|
|
const char *dir;
|
|
|
|
|
|
|
|
dir = tmpdir ? tmpdir : "/tmp";
|
2008-03-25 22:34:45 +01:00
|
|
|
#if HAVE_MKSTEMP
|
2005-05-23 05:06:10 +02:00
|
|
|
len = strlen(dir) + 6 + 10 + 1;
|
2008-03-25 22:34:45 +01:00
|
|
|
#else
|
|
|
|
pathname = tempnam(dir, "mksh.");
|
|
|
|
len = ((pathname == NULL) ? 0 : strlen(pathname)) + 1;
|
|
|
|
#endif
|
2008-12-13 18:02:18 +01:00
|
|
|
tp = alloc(sizeof (struct temp) + len, ap);
|
2008-03-25 22:34:45 +01:00
|
|
|
tp->name = (char *)&tp[1];
|
|
|
|
#if !HAVE_MKSTEMP
|
|
|
|
if (pathname == NULL)
|
|
|
|
tp->name[0] = '\0';
|
|
|
|
else {
|
|
|
|
memcpy(tp->name, pathname, len);
|
|
|
|
free(pathname);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pathname = tp->name;
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->shf = NULL;
|
|
|
|
tp->type = type;
|
2008-03-25 22:34:45 +01:00
|
|
|
#if HAVE_MKSTEMP
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_snprintf(pathname, len, "%s/mksh.XXXXXXXXXX", dir);
|
|
|
|
if ((fd = mkstemp(pathname)) >= 0)
|
2008-03-25 22:34:45 +01:00
|
|
|
#else
|
|
|
|
if (tp->name[0] && (fd = open(tp->name, O_CREAT | O_RDWR, 0600)) >= 0)
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->shf = shf_fdopen(fd, SHF_WR, NULL);
|
|
|
|
tp->pid = procpid;
|
|
|
|
|
|
|
|
tp->next = *tlist;
|
|
|
|
*tlist = tp;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INIT_TBLS 8 /* initial table size (power of 2) */
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
static void texpand(struct table *, int);
|
2006-11-10 04:23:50 +01:00
|
|
|
static int tnamecmp(const void *, const void *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
hash(const char *n)
|
|
|
|
{
|
|
|
|
unsigned int h = 0;
|
|
|
|
|
|
|
|
while (*n != '\0')
|
|
|
|
h = 2*h + *n++;
|
|
|
|
return h * 32821; /* scatter bits */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-12-13 18:02:18 +01:00
|
|
|
ktinit(struct table *tp, Area *ap, int tsize)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
tp->areap = ap;
|
|
|
|
tp->tbls = NULL;
|
|
|
|
tp->size = tp->nfree = 0;
|
|
|
|
if (tsize)
|
|
|
|
texpand(tp, tsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
texpand(struct table *tp, int nsize)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tbl *tblp, **p;
|
|
|
|
struct tbl **ntblp, **otblp = tp->tbls;
|
|
|
|
int osize = tp->size;
|
|
|
|
|
2008-12-13 18:02:18 +01:00
|
|
|
ntblp = alloc(nsize * sizeof (struct tbl *), tp->areap);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; i < nsize; i++)
|
|
|
|
ntblp[i] = NULL;
|
|
|
|
tp->size = nsize;
|
2005-06-09 00:22:24 +02:00
|
|
|
tp->nfree = 8 * nsize / 10; /* table can get 80% full */
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->tbls = ntblp;
|
|
|
|
if (otblp == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < osize; i++)
|
|
|
|
if ((tblp = otblp[i]) != NULL) {
|
2005-06-09 00:22:24 +02:00
|
|
|
if ((tblp->flag & DEFINED)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
for (p = &ntblp[hash(tblp->name) &
|
2005-06-09 00:22:24 +02:00
|
|
|
(tp->size - 1)]; *p != NULL; p--)
|
|
|
|
if (p == ntblp) /* wrap */
|
2005-05-23 05:06:10 +02:00
|
|
|
p += tp->size;
|
|
|
|
*p = tblp;
|
|
|
|
tp->nfree--;
|
|
|
|
} else if (!(tblp->flag & FINUSE)) {
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(tblp, tp->areap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(otblp, tp->areap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* table */
|
|
|
|
/* name to enter */
|
|
|
|
/* hash(n) */
|
|
|
|
struct tbl *
|
2006-01-29 21:04:54 +01:00
|
|
|
ktsearch(struct table *tp, const char *n, unsigned int h)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl **pp, *p;
|
|
|
|
|
|
|
|
if (tp->size == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* search for name in hashed table */
|
2005-06-09 00:22:24 +02:00
|
|
|
for (pp = &tp->tbls[h & (tp->size - 1)]; (p = *pp) != NULL; pp--) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*p->name == *n && strcmp(p->name, n) == 0 &&
|
2005-06-09 00:22:24 +02:00
|
|
|
(p->flag & DEFINED))
|
2005-05-23 05:06:10 +02:00
|
|
|
return p;
|
2005-06-09 00:22:24 +02:00
|
|
|
if (pp == tp->tbls) /* wrap */
|
2005-05-23 05:06:10 +02:00
|
|
|
pp += tp->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* table */
|
|
|
|
/* name to enter */
|
|
|
|
/* hash(n) */
|
|
|
|
struct tbl *
|
2006-01-29 21:04:54 +01:00
|
|
|
ktenter(struct table *tp, const char *n, unsigned int h)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl **pp, *p;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (tp->size == 0)
|
|
|
|
texpand(tp, INIT_TBLS);
|
2006-08-01 15:43:28 +02:00
|
|
|
Search:
|
2005-05-23 05:06:10 +02:00
|
|
|
/* search for name in hashed table */
|
2005-06-09 00:22:24 +02:00
|
|
|
for (pp = &tp->tbls[h & (tp->size - 1)]; (p = *pp) != NULL; pp--) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*p->name == *n && strcmp(p->name, n) == 0)
|
|
|
|
return p; /* found */
|
2005-06-09 00:22:24 +02:00
|
|
|
if (pp == tp->tbls) /* wrap */
|
2005-05-23 05:06:10 +02:00
|
|
|
pp += tp->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tp->nfree <= 0) { /* too full */
|
2005-06-09 00:22:24 +02:00
|
|
|
texpand(tp, 2 * tp->size);
|
2005-05-23 05:06:10 +02:00
|
|
|
goto Search;
|
|
|
|
}
|
|
|
|
/* create new tbl entry */
|
|
|
|
len = strlen(n) + 1;
|
2008-12-13 18:02:18 +01:00
|
|
|
p = alloc(offsetof(struct tbl, name[0]) + len, tp->areap);
|
2005-05-23 05:06:10 +02:00
|
|
|
p->flag = 0;
|
|
|
|
p->type = 0;
|
|
|
|
p->areap = tp->areap;
|
|
|
|
p->u2.field = 0;
|
|
|
|
p->u.array = NULL;
|
|
|
|
memcpy(p->name, n, len);
|
|
|
|
|
|
|
|
/* enter in tp->tbls */
|
|
|
|
tp->nfree--;
|
|
|
|
*pp = p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-29 21:04:54 +01:00
|
|
|
ktwalk(struct tstate *ts, struct table *tp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
ts->left = tp->size;
|
|
|
|
ts->next = tp->tbls;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl *
|
2006-01-29 21:04:54 +01:00
|
|
|
ktnext(struct tstate *ts)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
while (--ts->left >= 0) {
|
|
|
|
struct tbl *p = *ts->next++;
|
2005-06-09 00:22:24 +02:00
|
|
|
if (p != NULL && (p->flag & DEFINED))
|
2005-05-23 05:06:10 +02:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-10 04:23:50 +01:00
|
|
|
tnamecmp(const void *p1, const void *p2)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const struct tbl *a = *((struct tbl * const *)p1);
|
|
|
|
const struct tbl *b = *((struct tbl * const *)p2);
|
2006-11-10 04:23:50 +01:00
|
|
|
|
|
|
|
return (strcmp(a->name, b->name));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl **
|
2006-01-29 21:04:54 +01:00
|
|
|
ktsort(struct table *tp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-10 04:23:50 +01:00
|
|
|
size_t i;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct tbl **p, **sp, **dp;
|
|
|
|
|
2008-12-13 18:02:18 +01:00
|
|
|
p = alloc((tp->size + 1) * sizeof (struct tbl *), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
sp = tp->tbls; /* source */
|
|
|
|
dp = p; /* dest */
|
2006-11-10 04:23:50 +01:00
|
|
|
for (i = 0; i < (size_t)tp->size; i++)
|
2005-06-09 00:22:24 +02:00
|
|
|
if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
|
|
|
|
((*dp)->flag & ARRAY)))
|
2005-05-23 05:06:10 +02:00
|
|
|
dp++;
|
2006-11-10 04:23:50 +01:00
|
|
|
qsort(p, (i = dp - p), sizeof (void *), tnamecmp);
|
2005-05-23 05:06:10 +02:00
|
|
|
p[i] = NULL;
|
|
|
|
return p;
|
|
|
|
}
|