2010-05-22 14:49:16 +02:00
|
|
|
/* $OpenBSD: history.c,v 1.39 2010/05/19 17:36:08 jasper Exp $ */
|
|
|
|
/* $OpenBSD: trap.c,v 1.23 2010/05/19 17:36:08 jasper Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2011-02-09 20:32:15 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
|
2009-05-16 18:59:42 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
2011-01-30 03:18:20 +01:00
|
|
|
#if HAVE_SYS_FILE_H
|
2009-02-20 14:25:10 +01:00
|
|
|
#include <sys/file.h>
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-07 21:30:41 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.108 2011/03/07 20:30:38 tg Exp $");
|
2008-03-27 18:59:28 +01:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* MirOS: This is the default mapping type, and need not be specified.
|
2009-05-16 20:40:09 +02:00
|
|
|
* IRIX doesn't have this constant.
|
2008-03-27 18:59:28 +01:00
|
|
|
*/
|
|
|
|
#ifndef MAP_FILE
|
|
|
|
#define MAP_FILE 0
|
|
|
|
#endif
|
2006-08-16 01:56:32 +02:00
|
|
|
|
|
|
|
Trap sigtraps[NSIG + 1];
|
2007-06-07 01:28:17 +02:00
|
|
|
static struct sigaction Sigact_ign;
|
2006-08-16 01:56:32 +02:00
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
static int hist_count_lines(unsigned char *, int);
|
|
|
|
static int hist_shrink(unsigned char *, int);
|
|
|
|
static unsigned char *hist_skip_back(unsigned char *,int *,int);
|
|
|
|
static void histload(Source *, unsigned char *, int);
|
2008-07-12 18:56:40 +02:00
|
|
|
static void histinsert(Source *, int, const char *);
|
2005-05-23 05:06:10 +02:00
|
|
|
static void writehistfile(int, char *);
|
|
|
|
static int sprinkle(int);
|
2005-05-23 17:36:56 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• 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
|
|
|
static int hist_execute(char *);
|
2010-07-24 19:08:30 +02:00
|
|
|
static int hist_replace(char **, const char *, const char *, bool);
|
2010-01-25 17:12:57 +01:00
|
|
|
static char **hist_get(const char *, bool, bool);
|
• 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
|
|
|
static char **hist_get_oldest(void);
|
|
|
|
static void histbackup(void);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• 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
|
|
|
static char **current; /* current position in history[] */
|
|
|
|
static int hstarted; /* set after hist_init() called */
|
|
|
|
static Source *hist_source;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2011-02-09 16:08:01 +01:00
|
|
|
/* current history file: name, fd, size */
|
|
|
|
static char *hname;
|
• 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
|
|
|
static int histfd;
|
|
|
|
static int hsize;
|
2006-11-10 06:23:14 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
static const char T_not_in_history[] = "not in history";
|
|
|
|
#define T_history (T_not_in_history + 7)
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
c_fc(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct shf *shf;
|
2007-07-01 23:10:29 +02:00
|
|
|
struct temp *tf;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *p;
|
|
|
|
char *editor = NULL;
|
2010-07-24 19:08:30 +02:00
|
|
|
bool gflag = false, lflag = false, nflag = false, rflag = false,
|
|
|
|
sflag = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
int optc;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *first = NULL, *last = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
char **hfirst, **hlast, **hp;
|
|
|
|
|
|
|
|
if (!Flag(FTALKING_I)) {
|
2010-08-28 22:22:24 +02:00
|
|
|
bi_errorf("history %ss not available", T_function);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt,
|
2006-05-10 20:54:13 +02:00
|
|
|
"e:glnrs0,1,2,3,4,5,6,7,8,9,")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (optc) {
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'e':
|
|
|
|
p = builtin_opt.optarg;
|
• 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
|
|
|
if (ksh_isdash(p))
|
2010-07-24 19:08:30 +02:00
|
|
|
sflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
2006-11-09 15:19:31 +01:00
|
|
|
size_t len = strlen(p);
|
2010-09-14 23:26:19 +02:00
|
|
|
|
|
|
|
/* almost certainly not overflowing */
|
2008-12-13 18:02:18 +01:00
|
|
|
editor = alloc(len + 4, ATEMP);
|
2008-07-12 18:56:40 +02:00
|
|
|
memcpy(editor, p, len);
|
2006-11-09 15:19:31 +01:00
|
|
|
memcpy(editor + len, " $_", 4);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
|
|
|
/* non-AT&T ksh */
|
|
|
|
case 'g':
|
2010-07-24 19:08:30 +02:00
|
|
|
gflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'l':
|
2010-07-24 19:08:30 +02:00
|
|
|
lflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'n':
|
2010-07-24 19:08:30 +02:00
|
|
|
nflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'r':
|
2010-07-24 19:08:30 +02:00
|
|
|
rflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
|
|
|
/* POSIX version of -e - */
|
|
|
|
case 's':
|
2010-07-24 19:08:30 +02:00
|
|
|
sflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/* kludge city - accept -num as -- -num (kind of) */
|
2005-05-23 05:06:10 +02:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
p = shf_smprintf("-%c%s",
|
|
|
|
optc, builtin_opt.optarg);
|
|
|
|
if (!first)
|
|
|
|
first = p;
|
|
|
|
else if (!last)
|
|
|
|
last = p;
|
|
|
|
else {
|
|
|
|
bi_errorf("too many arguments");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
/* Substitute and execute command */
|
|
|
|
if (sflag) {
|
|
|
|
char *pat = NULL, *rep = NULL;
|
|
|
|
|
|
|
|
if (editor || lflag || nflag || rflag) {
|
|
|
|
bi_errorf("can't use -e, -l, -n, -r with -s (-e -)");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for pattern replacement argument */
|
2007-03-04 01:13:17 +01:00
|
|
|
if (*wp && **wp && (p = cstrchr(*wp + 1, '='))) {
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(pat, *wp, ATEMP);
|
2007-03-04 01:13:17 +01:00
|
|
|
rep = pat + (p - *wp);
|
|
|
|
*rep++ = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
wp++;
|
|
|
|
}
|
|
|
|
/* Check for search prefix */
|
|
|
|
if (!first && (first = *wp))
|
|
|
|
wp++;
|
|
|
|
if (last || *wp) {
|
|
|
|
bi_errorf("too many arguments");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hp = first ? hist_get(first, false, false) :
|
|
|
|
hist_get_newest(false);
|
|
|
|
if (!hp)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
|
|
|
return (hist_replace(hp, pat, rep, gflag));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (editor && (lflag || nflag)) {
|
|
|
|
bi_errorf("can't use -l, -n with -e");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!first && (first = *wp))
|
|
|
|
wp++;
|
|
|
|
if (!last && (last = *wp))
|
|
|
|
wp++;
|
|
|
|
if (*wp) {
|
|
|
|
bi_errorf("too many arguments");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (!first) {
|
|
|
|
hfirst = lflag ? hist_get("-16", true, true) :
|
|
|
|
hist_get_newest(false);
|
|
|
|
if (!hfirst)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* can't fail if hfirst didn't fail */
|
|
|
|
hlast = hist_get_newest(false);
|
|
|
|
} else {
|
2011-02-09 16:08:01 +01:00
|
|
|
/*
|
|
|
|
* POSIX says not an error if first/last out of bounds
|
|
|
|
* when range is specified; AT&T ksh and pdksh allow out
|
|
|
|
* of bounds for -l as well.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-03-07 21:30:41 +01:00
|
|
|
hfirst = hist_get(first, tobool(lflag || last), lflag);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!hfirst)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2010-07-24 19:08:30 +02:00
|
|
|
hlast = last ? hist_get(last, true, lflag) :
|
2005-05-23 05:06:10 +02:00
|
|
|
(lflag ? hist_get_newest(false) : hfirst);
|
|
|
|
if (!hlast)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (hfirst > hlast) {
|
|
|
|
char **temp;
|
|
|
|
|
|
|
|
temp = hfirst; hfirst = hlast; hlast = temp;
|
2011-02-09 16:08:01 +01:00
|
|
|
/* POSIX */
|
|
|
|
rflag = !rflag;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List history */
|
|
|
|
if (lflag) {
|
|
|
|
char *s, *t;
|
|
|
|
|
|
|
|
for (hp = rflag ? hlast : hfirst;
|
|
|
|
hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1) {
|
2007-07-22 15:34:52 +02:00
|
|
|
if (!nflag)
|
|
|
|
shf_fprintf(shl_stdout, "%d",
|
|
|
|
hist_source->line - (int)(histptr - hp));
|
|
|
|
shf_putc('\t', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* print multi-line commands correctly */
|
2008-04-19 19:21:55 +02:00
|
|
|
s = *hp;
|
|
|
|
while ((t = strchr(s, '\n'))) {
|
|
|
|
*t = '\0';
|
|
|
|
shf_fprintf(shl_stdout, "%s\n\t", s);
|
|
|
|
*t++ = '\n';
|
|
|
|
s = t;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_fprintf(shl_stdout, "%s\n", s);
|
|
|
|
}
|
|
|
|
shf_flush(shl_stdout);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run editor on selected lines, then run resulting commands */
|
|
|
|
|
|
|
|
tf = maketemp(ATEMP, TT_HIST_EDIT, &e->temps);
|
|
|
|
if (!(shf = tf->shf)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("can't %s temporary file %s: %s",
|
|
|
|
"create", tf->name, strerror(errno));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
for (hp = rflag ? hlast : hfirst;
|
|
|
|
hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
|
|
|
|
shf_fprintf(shf, "%s\n", *hp);
|
|
|
|
if (shf_close(shf) == EOF) {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("can't %s temporary file %s: %s",
|
|
|
|
"write", tf->name, strerror(errno));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore setstr errors here (arbitrary) */
|
|
|
|
setstr(local("_", false), tf->name, KSH_RETURN_ERROR);
|
|
|
|
|
|
|
|
/* XXX: source should not get trashed by this.. */
|
|
|
|
{
|
|
|
|
Source *sold = source;
|
|
|
|
int ret;
|
|
|
|
|
2010-05-22 14:49:16 +02:00
|
|
|
ret = command(editor ? editor : "${FCEDIT:-/bin/ed} $_", 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
source = sold;
|
|
|
|
if (ret)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct stat statb;
|
|
|
|
XString xs;
|
|
|
|
char *xp;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!(shf = shf_open(tf->name, O_RDONLY, 0, 0))) {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("can't %s temporary file %s: %s",
|
|
|
|
"open", tf->name, strerror(errno));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
if (stat(tf->name, &statb) < 0)
|
|
|
|
n = 128;
|
2011-02-09 16:08:01 +01:00
|
|
|
else if (statb.st_size > (1024 * 1048576)) {
|
|
|
|
bi_errorf("%s %s too large: %lu", T_history,
|
|
|
|
"file", (unsigned long)statb.st_size);
|
|
|
|
goto errout;
|
|
|
|
} else
|
2010-09-14 23:26:19 +02:00
|
|
|
n = statb.st_size + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
Xinit(xs, xp, n, hist_source->areap);
|
|
|
|
while ((n = shf_read(xp, Xnleft(xs, xp), shf)) > 0) {
|
|
|
|
xp += n;
|
|
|
|
if (Xnleft(xs, xp) <= 0)
|
|
|
|
XcheckN(xs, xp, Xlength(xs, xp));
|
|
|
|
}
|
|
|
|
if (n < 0) {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("can't %s temporary file %s: %s",
|
|
|
|
"read", tf->name, strerror(shf_errno(shf)));
|
2010-09-14 23:26:19 +02:00
|
|
|
errout:
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_close(shf);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
shf_close(shf);
|
|
|
|
*xp = '\0';
|
|
|
|
strip_nuls(Xstring(xs, xp), Xlength(xs, xp));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (hist_execute(Xstring(xs, xp)));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save cmd in history, execute cmd (cmd gets trashed) */
|
|
|
|
static int
|
|
|
|
hist_execute(char *cmd)
|
|
|
|
{
|
|
|
|
Source *sold;
|
|
|
|
int ret;
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
histbackup();
|
|
|
|
|
|
|
|
for (p = cmd; p; p = q) {
|
|
|
|
if ((q = strchr(p, '\n'))) {
|
2011-02-09 16:08:01 +01:00
|
|
|
/* kill the newline */
|
|
|
|
*q++ = '\0';
|
|
|
|
if (!*q)
|
|
|
|
/* ignore trailing newline */
|
2005-05-23 05:06:10 +02:00
|
|
|
q = NULL;
|
|
|
|
}
|
2008-09-30 21:36:16 +02:00
|
|
|
histsave(&hist_source->line, p, true, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-08-28 22:22:24 +02:00
|
|
|
/* POSIX doesn't say this is done... */
|
|
|
|
shellf("%s\n", p);
|
|
|
|
if (q)
|
|
|
|
/* restore \n (trailing \n not restored) */
|
2005-05-23 05:06:10 +02:00
|
|
|
q[-1] = '\n';
|
|
|
|
}
|
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
/*-
|
2010-07-04 15:36:43 +02:00
|
|
|
* Commands are executed here instead of pushing them onto the
|
2009-10-02 20:08:37 +02:00
|
|
|
* input 'cause POSIX says the redirection and variable assignments
|
2005-05-23 05:06:10 +02:00
|
|
|
* in
|
|
|
|
* X=y fc -e - 42 2> /dev/null
|
|
|
|
* are to effect the repeated commands environment.
|
|
|
|
*/
|
|
|
|
/* XXX: source should not get trashed by this.. */
|
|
|
|
sold = source;
|
2010-05-22 14:49:16 +02:00
|
|
|
ret = command(cmd, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
source = sold;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-07-24 19:08:30 +02:00
|
|
|
hist_replace(char **hp, const char *pat, const char *rep, bool globr)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
if (!pat)
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(line, *hp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
char *s, *s1;
|
|
|
|
int pat_len = strlen(pat);
|
|
|
|
int rep_len = strlen(rep);
|
|
|
|
int len;
|
|
|
|
XString xs;
|
|
|
|
char *xp;
|
2010-07-24 19:08:30 +02:00
|
|
|
bool any_subst = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
Xinit(xs, xp, 128, ATEMP);
|
|
|
|
for (s = *hp; (s1 = strstr(s, pat)) && (!any_subst || globr);
|
|
|
|
s = s1 + pat_len) {
|
2010-07-24 19:08:30 +02:00
|
|
|
any_subst = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
len = s1 - s;
|
|
|
|
XcheckN(xs, xp, len + rep_len);
|
2011-02-09 16:08:01 +01:00
|
|
|
/*; first part */
|
|
|
|
memcpy(xp, s, len);
|
2005-05-23 05:06:10 +02:00
|
|
|
xp += len;
|
2011-02-09 16:08:01 +01:00
|
|
|
/* replacement */
|
|
|
|
memcpy(xp, rep, rep_len);
|
2005-05-23 05:06:10 +02:00
|
|
|
xp += rep_len;
|
|
|
|
}
|
|
|
|
if (!any_subst) {
|
2010-08-28 22:22:24 +02:00
|
|
|
bi_errorf("bad substitution");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
len = strlen(s) + 1;
|
|
|
|
XcheckN(xs, xp, len);
|
|
|
|
memcpy(xp, s, len);
|
|
|
|
xp += len;
|
|
|
|
line = Xclose(xs, xp);
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (hist_execute(line));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get pointer to history given pattern
|
|
|
|
* pattern is a number or string
|
|
|
|
*/
|
|
|
|
static char **
|
2010-01-25 17:12:57 +01:00
|
|
|
hist_get(const char *str, bool approx, bool allow_cur)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char **hp = NULL;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (getn(str, &n)) {
|
|
|
|
hp = histptr + (n < 0 ? n : (n - hist_source->line));
|
2008-06-08 19:14:31 +02:00
|
|
|
if ((ptrdiff_t)hp < (ptrdiff_t)history) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (approx)
|
|
|
|
hp = hist_get_oldest();
|
|
|
|
else {
|
2011-02-09 16:08:01 +01:00
|
|
|
bi_errorf("%s: %s", str, T_not_in_history);
|
2005-05-23 05:06:10 +02:00
|
|
|
hp = NULL;
|
|
|
|
}
|
2008-06-08 19:14:31 +02:00
|
|
|
} else if ((ptrdiff_t)hp > (ptrdiff_t)histptr) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (approx)
|
|
|
|
hp = hist_get_newest(allow_cur);
|
|
|
|
else {
|
2011-02-09 16:08:01 +01:00
|
|
|
bi_errorf("%s: %s", str, T_not_in_history);
|
2005-05-23 05:06:10 +02:00
|
|
|
hp = NULL;
|
|
|
|
}
|
|
|
|
} else if (!allow_cur && hp == histptr) {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("%s: %s", str, "invalid range");
|
2005-05-23 05:06:10 +02:00
|
|
|
hp = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int anchored = *str == '?' ? (++str, 0) : 1;
|
|
|
|
|
|
|
|
/* the -1 is to avoid the current fc command */
|
2007-07-01 23:10:29 +02:00
|
|
|
if ((n = findhist(histptr - history - 1, 0, str, anchored)) < 0)
|
2011-02-09 16:08:01 +01:00
|
|
|
bi_errorf("%s: %s", str, T_not_in_history);
|
2007-07-01 23:10:29 +02:00
|
|
|
else
|
2005-05-23 05:06:10 +02:00
|
|
|
hp = &history[n];
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (hp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a pointer to the newest command in the history */
|
|
|
|
char **
|
2010-01-25 17:12:57 +01:00
|
|
|
hist_get_newest(bool allow_cur)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
if (histptr < history || (!allow_cur && histptr == history)) {
|
|
|
|
bi_errorf("no history (yet)");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2007-10-25 17:34:30 +02:00
|
|
|
return (allow_cur ? histptr : histptr - 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-05-13 20:44:10 +02:00
|
|
|
/* Return a pointer to the oldest command in the history */
|
2005-05-23 05:06:10 +02:00
|
|
|
static char **
|
|
|
|
hist_get_oldest(void)
|
|
|
|
{
|
|
|
|
if (histptr <= history) {
|
|
|
|
bi_errorf("no history (yet)");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (history);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
/*
|
|
|
|
* Back up over last histsave
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
static void
|
|
|
|
histbackup(void)
|
|
|
|
{
|
|
|
|
static int last_line = -1;
|
|
|
|
|
|
|
|
if (histptr >= history && last_line != hist_source->line) {
|
|
|
|
hist_source->line--;
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(*histptr, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
histptr--;
|
|
|
|
last_line = hist_source->line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the current position.
|
|
|
|
*/
|
|
|
|
char **
|
|
|
|
histpos(void)
|
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
return (current);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
histnum(int n)
|
|
|
|
{
|
2007-10-25 17:34:30 +02:00
|
|
|
int last = histptr - history;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (n < 0 || n >= last) {
|
|
|
|
current = histptr;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (last);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
current = &history[n];
|
2009-06-08 22:06:50 +02:00
|
|
|
return (n);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will become unnecessary if hist_get is modified to allow
|
|
|
|
* searching from positions other than the end, and in either
|
|
|
|
* direction.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
findhist(int start, int fwd, const char *str, int anchored)
|
|
|
|
{
|
|
|
|
char **hp;
|
|
|
|
int maxhist = histptr - history;
|
|
|
|
int incr = fwd ? 1 : -1;
|
|
|
|
int len = strlen(str);
|
|
|
|
|
|
|
|
if (start < 0 || start >= maxhist)
|
|
|
|
start = maxhist;
|
|
|
|
|
|
|
|
hp = &history[start];
|
|
|
|
for (; hp >= history && hp <= histptr; hp += incr)
|
|
|
|
if ((anchored && strncmp(*hp, str, len) == 0) ||
|
|
|
|
(!anchored && strstr(*hp, str)))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (hp - history);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
findhistrel(const char *str)
|
|
|
|
{
|
|
|
|
int maxhist = histptr - history;
|
|
|
|
int start = maxhist - 1;
|
2006-11-10 05:03:59 +01:00
|
|
|
int rec;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-10 05:03:59 +01:00
|
|
|
getn(str, &rec);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (rec == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (rec > 0) {
|
|
|
|
if (rec > maxhist)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
|
|
|
return (rec - 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (rec > maxhist)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
|
|
|
return (start + rec + 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set history
|
|
|
|
* this means reallocating the dataspace
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sethistsize(int n)
|
|
|
|
{
|
|
|
|
if (n > 0 && n != histsize) {
|
|
|
|
int cursize = histptr - history;
|
|
|
|
|
|
|
|
/* save most recent history */
|
|
|
|
if (n < cursize) {
|
|
|
|
memmove(history, histptr - n, n * sizeof(char *));
|
|
|
|
cursize = n;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
history = aresize2(history, n, sizeof(char *), APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
histsize = n;
|
|
|
|
histptr = history + cursize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* set history file
|
|
|
|
* This can mean reloading/resetting/starting history file
|
|
|
|
* maintenance
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sethistfile(const char *name)
|
|
|
|
{
|
|
|
|
/* if not started then nothing to do */
|
|
|
|
if (hstarted == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* if the name is the same as the name we have */
|
|
|
|
if (hname && strcmp(hname, name) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* its a new name - possibly
|
|
|
|
*/
|
|
|
|
if (histfd) {
|
|
|
|
/* yes the file is open */
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)close(histfd);
|
2005-05-23 05:06:10 +02:00
|
|
|
histfd = 0;
|
|
|
|
hsize = 0;
|
|
|
|
afree(hname, APERM);
|
|
|
|
hname = NULL;
|
|
|
|
/* let's reset the history */
|
|
|
|
histptr = history - 1;
|
|
|
|
hist_source->line = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hist_init(hist_source);
|
|
|
|
}
|
2006-11-10 06:23:14 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialise the history vector
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_histvec(void)
|
|
|
|
{
|
|
|
|
if (history == (char **)NULL) {
|
|
|
|
histsize = HISTORYSIZE;
|
2010-09-14 23:26:19 +02:00
|
|
|
history = alloc2(histsize, sizeof(char *), APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
histptr = history - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines added by Peter Collinson BSDI(Europe)/Hillside Systems to
|
|
|
|
* a) permit HISTSIZE to control number of lines of history stored
|
|
|
|
* b) maintain a physical history file
|
|
|
|
*
|
|
|
|
* It turns out that there is a lot of ghastly hackery here
|
|
|
|
*/
|
|
|
|
|
2010-01-25 17:12:57 +01:00
|
|
|
#if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY
|
|
|
|
/* do not save command in history but possibly sync */
|
|
|
|
bool
|
|
|
|
histsync(void)
|
|
|
|
{
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
if (histfd) {
|
|
|
|
int lno = hist_source->line;
|
|
|
|
|
|
|
|
hist_source->line++;
|
|
|
|
writehistfile(0, NULL);
|
|
|
|
hist_source->line--;
|
|
|
|
|
|
|
|
if (lno != hist_source->line)
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (changed);
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* save command in history
|
|
|
|
*/
|
|
|
|
void
|
2009-12-12 23:27:10 +01:00
|
|
|
histsave(int *lnp, const char *cmd, bool dowrite MKSH_A_UNUSED, bool ignoredups)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char **hp;
|
|
|
|
char *c, *cp;
|
|
|
|
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(c, cmd, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((cp = strchr(c, '\n')) != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
|
2010-01-25 17:12:57 +01:00
|
|
|
if (ignoredups && !strcmp(c, *histptr)
|
|
|
|
#if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY
|
|
|
|
&& !histsync()
|
|
|
|
#endif
|
|
|
|
) {
|
2008-09-30 21:25:51 +02:00
|
|
|
afree(c, APERM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++*lnp;
|
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
if (histfd && dowrite)
|
2008-09-30 21:25:51 +02:00
|
|
|
writehistfile(*lnp, c);
|
2005-05-23 17:36:56 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
hp = histptr;
|
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
if (++hp >= history + histsize) {
|
|
|
|
/* remove oldest command */
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(*history, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (hp = history; hp < history + histsize - 1; hp++)
|
|
|
|
hp[0] = hp[1];
|
|
|
|
}
|
|
|
|
*hp = c;
|
|
|
|
histptr = hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write history data to a file nominated by HISTFILE
|
|
|
|
* if HISTFILE is unset then history still happens, but
|
|
|
|
* the data is not written to a file
|
|
|
|
* All copies of ksh looking at the file will maintain the
|
|
|
|
* same history. This is ksh behaviour.
|
|
|
|
*
|
|
|
|
* This stuff uses mmap()
|
|
|
|
* if your system ain't got it - then you'll have to undef HISTORYFILE
|
|
|
|
*/
|
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
/*-
|
2005-05-23 05:06:10 +02:00
|
|
|
* Open a history file
|
|
|
|
* Format is:
|
2009-06-10 20:12:51 +02:00
|
|
|
* Bytes 1, 2:
|
|
|
|
* HMAGIC - just to check that we are dealing with
|
|
|
|
* the correct object
|
2005-05-23 05:06:10 +02:00
|
|
|
* Then follows a number of stored commands
|
|
|
|
* Each command is
|
|
|
|
* <command byte><command number(4 bytes)><bytes><null>
|
|
|
|
*/
|
|
|
|
#define HMAGIC1 0xab
|
|
|
|
#define HMAGIC2 0xcd
|
|
|
|
#define COMMAND 0xff
|
|
|
|
|
|
|
|
void
|
|
|
|
hist_init(Source *s)
|
|
|
|
{
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2008-03-05 17:54:21 +01:00
|
|
|
unsigned char *base;
|
2009-08-01 16:07:42 +02:00
|
|
|
int lines, fd, rv = 0;
|
2005-05-23 17:36:56 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (Flag(FTALKING) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hstarted = 1;
|
|
|
|
|
|
|
|
hist_source = s;
|
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2008-07-12 18:56:40 +02:00
|
|
|
if ((hname = str_val(global("HISTFILE"))) == NULL)
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(hname, hname, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-08-01 14:57:07 +02:00
|
|
|
retry:
|
2005-05-23 05:06:10 +02:00
|
|
|
/* we have a file and are interactive */
|
|
|
|
if ((fd = open(hname, O_RDWR|O_CREAT|O_APPEND, 0600)) < 0)
|
|
|
|
return;
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
histfd = savefd(fd);
|
|
|
|
if (histfd != fd)
|
|
|
|
close(fd);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)flock(histfd, LOCK_EX);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-08-08 15:08:53 +02:00
|
|
|
hsize = lseek(histfd, (off_t)0, SEEK_END);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (hsize == 0) {
|
|
|
|
/* add magic */
|
|
|
|
if (sprinkle(histfd)) {
|
|
|
|
hist_finish();
|
|
|
|
return;
|
|
|
|
}
|
2006-08-01 15:43:28 +02:00
|
|
|
} else if (hsize > 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* we have some data
|
|
|
|
*/
|
2008-04-02 18:55:06 +02:00
|
|
|
base = (void *)mmap(NULL, hsize, PROT_READ,
|
2009-08-08 15:08:53 +02:00
|
|
|
MAP_FILE | MAP_PRIVATE, histfd, (off_t)0);
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* check on its validity
|
|
|
|
*/
|
2008-03-05 17:54:21 +01:00
|
|
|
if (base == (unsigned char *)MAP_FAILED ||
|
|
|
|
*base != HMAGIC1 || base[1] != HMAGIC2) {
|
|
|
|
if (base != (unsigned char *)MAP_FAILED)
|
2005-05-23 05:06:10 +02:00
|
|
|
munmap((caddr_t)base, hsize);
|
|
|
|
hist_finish();
|
2008-07-07 00:41:09 +02:00
|
|
|
if (unlink(hname) /* fails */)
|
|
|
|
goto hiniterr;
|
2005-05-23 05:06:10 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (hsize > 2) {
|
|
|
|
lines = hist_count_lines(base+2, hsize-2);
|
|
|
|
if (lines > histsize) {
|
|
|
|
/* we need to make the file smaller */
|
|
|
|
if (hist_shrink(base, hsize))
|
2008-07-07 00:41:09 +02:00
|
|
|
rv = unlink(hname);
|
2005-05-23 05:06:10 +02:00
|
|
|
munmap((caddr_t)base, hsize);
|
|
|
|
hist_finish();
|
2008-07-07 00:41:09 +02:00
|
|
|
if (rv) {
|
|
|
|
hiniterr:
|
2010-08-28 22:22:24 +02:00
|
|
|
bi_errorf("can't %s %s: %s",
|
|
|
|
"unlink HISTFILE", hname,
|
|
|
|
strerror(errno));
|
2008-07-07 00:41:09 +02:00
|
|
|
hsize = 0;
|
|
|
|
return;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
histload(hist_source, base+2, hsize-2);
|
|
|
|
munmap((caddr_t)base, hsize);
|
|
|
|
}
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)flock(histfd, LOCK_UN);
|
|
|
|
hsize = lseek(histfd, (off_t)0, SEEK_END);
|
2005-05-23 17:36:56 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
typedef enum state {
|
|
|
|
shdr, /* expecting a header */
|
|
|
|
sline, /* looking for a null byte to end the line */
|
|
|
|
sn1, /* bytes 1 to 4 of a line no */
|
|
|
|
sn2, sn3, sn4
|
|
|
|
} State;
|
|
|
|
|
|
|
|
static int
|
|
|
|
hist_count_lines(unsigned char *base, int bytes)
|
|
|
|
{
|
|
|
|
State state = shdr;
|
|
|
|
int lines = 0;
|
|
|
|
|
|
|
|
while (bytes--) {
|
|
|
|
switch (state) {
|
|
|
|
case shdr:
|
|
|
|
if (*base == COMMAND)
|
|
|
|
state = sn1;
|
|
|
|
break;
|
|
|
|
case sn1:
|
|
|
|
state = sn2; break;
|
|
|
|
case sn2:
|
|
|
|
state = sn3; break;
|
|
|
|
case sn3:
|
|
|
|
state = sn4; break;
|
|
|
|
case sn4:
|
|
|
|
state = sline; break;
|
|
|
|
case sline:
|
2010-01-29 10:34:31 +01:00
|
|
|
if (*base == '\0') {
|
|
|
|
lines++;
|
|
|
|
state = shdr;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
base++;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (lines);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shrink the history file to histsize lines
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
hist_shrink(unsigned char *oldbase, int oldbytes)
|
|
|
|
{
|
2009-05-27 11:58:24 +02:00
|
|
|
int fd, rv = 0;
|
|
|
|
char *nfile = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct stat statb;
|
|
|
|
unsigned char *nbase = oldbase;
|
|
|
|
int nbytes = oldbytes;
|
|
|
|
|
|
|
|
nbase = hist_skip_back(nbase, &nbytes, histsize);
|
|
|
|
if (nbase == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (nbase == oldbase)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* create temp file
|
|
|
|
*/
|
2009-05-27 11:58:24 +02:00
|
|
|
nfile = shf_smprintf("%s.%d", hname, (int)procpid);
|
2005-08-02 14:26:17 +02:00
|
|
|
if ((fd = open(nfile, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0)
|
2009-05-27 11:58:24 +02:00
|
|
|
goto errout;
|
2007-09-09 21:12:10 +02:00
|
|
|
if (fstat(histfd, &statb) >= 0 &&
|
|
|
|
chown(nfile, statb.st_uid, statb.st_gid))
|
|
|
|
goto errout;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-27 11:58:24 +02:00
|
|
|
if (sprinkle(fd) || write(fd, nbase, nbytes) != nbytes)
|
|
|
|
goto errout;
|
2005-05-23 05:06:10 +02:00
|
|
|
close(fd);
|
2009-05-27 11:58:24 +02:00
|
|
|
fd = -1;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* rename
|
|
|
|
*/
|
2009-05-27 11:58:24 +02:00
|
|
|
if (rename(nfile, hname) < 0) {
|
|
|
|
errout:
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
if (nfile)
|
|
|
|
unlink(nfile);
|
|
|
|
}
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
afree(nfile, ATEMP);
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a pointer to the data 'no' back from the end of the file
|
|
|
|
* return the pointer and the number of bytes left
|
|
|
|
*/
|
|
|
|
static unsigned char *
|
|
|
|
hist_skip_back(unsigned char *base, int *bytes, int no)
|
|
|
|
{
|
|
|
|
int lines = 0;
|
|
|
|
unsigned char *ep;
|
|
|
|
|
|
|
|
for (ep = base + *bytes; --ep > base; ) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* this doesn't really work: the 4 byte line number that
|
|
|
|
* is encoded after the COMMAND byte can itself contain
|
|
|
|
* the COMMAND byte....
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
for (; ep > base && *ep != COMMAND; ep--)
|
|
|
|
;
|
|
|
|
if (ep == base)
|
|
|
|
break;
|
|
|
|
if (++lines == no) {
|
|
|
|
*bytes = *bytes - ((char *)ep - (char *)base);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ep);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load the history structure from the stored data
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
histload(Source *s, unsigned char *base, int bytes)
|
|
|
|
{
|
|
|
|
State state;
|
2008-09-30 21:25:51 +02:00
|
|
|
int lno = 0;
|
|
|
|
unsigned char *line = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (state = shdr; bytes-- > 0; base++) {
|
|
|
|
switch (state) {
|
|
|
|
case shdr:
|
|
|
|
if (*base == COMMAND)
|
|
|
|
state = sn1;
|
|
|
|
break;
|
|
|
|
case sn1:
|
|
|
|
lno = (((*base)&0xff)<<24);
|
|
|
|
state = sn2;
|
|
|
|
break;
|
|
|
|
case sn2:
|
|
|
|
lno |= (((*base)&0xff)<<16);
|
|
|
|
state = sn3;
|
|
|
|
break;
|
|
|
|
case sn3:
|
|
|
|
lno |= (((*base)&0xff)<<8);
|
|
|
|
state = sn4;
|
|
|
|
break;
|
|
|
|
case sn4:
|
|
|
|
lno |= (*base)&0xff;
|
|
|
|
line = base+1;
|
|
|
|
state = sline;
|
|
|
|
break;
|
|
|
|
case sline:
|
|
|
|
if (*base == '\0') {
|
|
|
|
/* worry about line numbers */
|
|
|
|
if (histptr >= history && lno-1 != s->line) {
|
|
|
|
/* a replacement ? */
|
2008-07-12 18:56:40 +02:00
|
|
|
histinsert(s, lno, (char *)line);
|
2006-08-01 16:09:20 +02:00
|
|
|
} else {
|
2008-09-30 21:25:51 +02:00
|
|
|
s->line = lno--;
|
2008-10-14 21:48:27 +02:00
|
|
|
histsave(&lno, (char *)line, false,
|
|
|
|
false);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
state = shdr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a line into the history at a specified number
|
|
|
|
*/
|
|
|
|
static void
|
2008-07-12 18:56:40 +02:00
|
|
|
histinsert(Source *s, int lno, const char *line)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char **hp;
|
|
|
|
|
2008-07-12 18:56:40 +02:00
|
|
|
if (lno >= s->line - (histptr - history) && lno <= s->line) {
|
|
|
|
hp = &histptr[lno - s->line];
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*hp)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(*hp, APERM);
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(*hp, line, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a command to the end of the history file
|
|
|
|
* This *MAY* seem easy but it's also necessary to check
|
|
|
|
* that the history file has not changed in size.
|
|
|
|
* If it has - then some other shell has written to it
|
|
|
|
* and we should read those commands to update our history
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
writehistfile(int lno, char *cmd)
|
|
|
|
{
|
|
|
|
int sizenow;
|
|
|
|
unsigned char *base;
|
2010-07-18 00:09:40 +02:00
|
|
|
unsigned char *news;
|
2005-05-23 05:06:10 +02:00
|
|
|
int bytes;
|
|
|
|
unsigned char hdr[5];
|
|
|
|
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)flock(histfd, LOCK_EX);
|
|
|
|
sizenow = lseek(histfd, (off_t)0, SEEK_END);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (sizenow != hsize) {
|
|
|
|
/*
|
|
|
|
* Things have changed
|
|
|
|
*/
|
|
|
|
if (sizenow > hsize) {
|
|
|
|
/* someone has added some lines */
|
|
|
|
bytes = sizenow - hsize;
|
2008-04-02 18:55:06 +02:00
|
|
|
base = (void *)mmap(NULL, sizenow, PROT_READ,
|
2009-08-08 15:08:53 +02:00
|
|
|
MAP_FILE | MAP_PRIVATE, histfd, (off_t)0);
|
2008-03-05 17:54:21 +01:00
|
|
|
if (base == (unsigned char *)MAP_FAILED)
|
2005-05-23 05:06:10 +02:00
|
|
|
goto bad;
|
2010-07-18 00:09:40 +02:00
|
|
|
news = base + hsize;
|
|
|
|
if (*news != COMMAND) {
|
2005-05-23 05:06:10 +02:00
|
|
|
munmap((caddr_t)base, sizenow);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
hist_source->line--;
|
2010-07-18 00:09:40 +02:00
|
|
|
histload(hist_source, news, bytes);
|
2005-05-23 05:06:10 +02:00
|
|
|
hist_source->line++;
|
|
|
|
lno = hist_source->line;
|
|
|
|
munmap((caddr_t)base, sizenow);
|
|
|
|
hsize = sizenow;
|
|
|
|
} else {
|
|
|
|
/* it has shrunk */
|
|
|
|
/* but to what? */
|
|
|
|
/* we'll give up for now */
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
2010-01-25 17:12:57 +01:00
|
|
|
if (cmd) {
|
|
|
|
/*
|
|
|
|
* we can write our bit now
|
|
|
|
*/
|
|
|
|
hdr[0] = COMMAND;
|
|
|
|
hdr[1] = (lno>>24)&0xff;
|
|
|
|
hdr[2] = (lno>>16)&0xff;
|
|
|
|
hdr[3] = (lno>>8)&0xff;
|
|
|
|
hdr[4] = lno&0xff;
|
|
|
|
bytes = strlen(cmd) + 1;
|
|
|
|
if ((write(histfd, hdr, 5) != 5) ||
|
|
|
|
(write(histfd, cmd, bytes) != bytes))
|
|
|
|
goto bad;
|
|
|
|
hsize = lseek(histfd, (off_t)0, SEEK_END);
|
|
|
|
}
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)flock(histfd, LOCK_UN);
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
2006-08-01 14:57:07 +02:00
|
|
|
bad:
|
2005-05-23 05:06:10 +02:00
|
|
|
hist_finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hist_finish(void)
|
|
|
|
{
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)flock(histfd, LOCK_UN);
|
|
|
|
(void)close(histfd);
|
2005-05-23 05:06:10 +02:00
|
|
|
histfd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add magic to the history file
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sprinkle(int fd)
|
|
|
|
{
|
2007-01-15 03:48:28 +01:00
|
|
|
static const unsigned char mag[] = { HMAGIC1, HMAGIC2 };
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (write(fd, mag, 2) != 2);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2005-05-23 17:36:56 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-06-05 23:47:49 +02:00
|
|
|
#if !HAVE_SYS_SIGNAME
|
|
|
|
static const struct mksh_sigpair {
|
|
|
|
const char *const name;
|
2009-04-07 20:41:37 +02:00
|
|
|
int nr;
|
2007-06-05 23:47:49 +02:00
|
|
|
} mksh_sigpairs[] = {
|
|
|
|
#include "signames.inc"
|
2009-04-07 23:23:28 +02:00
|
|
|
{ NULL, 0 }
|
2007-06-05 23:47:49 +02:00
|
|
|
};
|
2005-05-23 14:01:09 +02:00
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
void
|
|
|
|
inittraps(void)
|
|
|
|
{
|
2007-01-12 01:37:09 +01:00
|
|
|
int i;
|
2007-01-12 02:30:35 +01:00
|
|
|
const char *cs;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-11-01 18:29:05 +01:00
|
|
|
trap_exstat = -1;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Populate sigtraps based on sys_signame and sys_siglist. */
|
|
|
|
for (i = 0; i <= NSIG; i++) {
|
|
|
|
sigtraps[i].signal = i;
|
|
|
|
if (i == SIGERR_) {
|
|
|
|
sigtraps[i].name = "ERR";
|
|
|
|
sigtraps[i].mess = "Error handler";
|
|
|
|
} else {
|
2007-06-05 23:47:49 +02:00
|
|
|
#if HAVE_SYS_SIGNAME
|
2007-01-12 02:30:35 +01:00
|
|
|
cs = sys_signame[i];
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
#else
|
|
|
|
const struct mksh_sigpair *pair = mksh_sigpairs;
|
|
|
|
while ((pair->nr != i) && (pair->name != NULL))
|
|
|
|
++pair;
|
2007-01-12 02:30:35 +01:00
|
|
|
cs = pair->name;
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
#endif
|
2007-01-12 02:30:35 +01:00
|
|
|
if ((cs == NULL) ||
|
|
|
|
(cs[0] == '\0'))
|
2007-01-12 01:37:09 +01:00
|
|
|
sigtraps[i].name = shf_smprintf("%d", i);
|
|
|
|
else {
|
2007-01-12 02:30:35 +01:00
|
|
|
char *s;
|
|
|
|
|
2008-07-12 19:16:03 +02:00
|
|
|
if (!strncasecmp(cs, "SIG", 3))
|
|
|
|
cs += 3;
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(s, cs, APERM);
|
|
|
|
sigtraps[i].name = s;
|
2007-01-12 03:06:34 +01:00
|
|
|
while ((*s = ksh_toupper(*s)))
|
2007-01-12 01:37:09 +01:00
|
|
|
++s;
|
|
|
|
}
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
#if HAVE_SYS_SIGLIST
|
|
|
|
sigtraps[i].mess = sys_siglist[i];
|
|
|
|
#elif HAVE_STRSIGNAL
|
|
|
|
sigtraps[i].mess = strsignal(i);
|
|
|
|
#else
|
|
|
|
sigtraps[i].mess = NULL;
|
|
|
|
#endif
|
|
|
|
if ((sigtraps[i].mess == NULL) ||
|
|
|
|
(sigtraps[i].mess[0] == '\0'))
|
2010-08-28 22:22:24 +02:00
|
|
|
sigtraps[i].mess = shf_smprintf("%s %d",
|
|
|
|
"Signal", i);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-09 16:08:01 +01:00
|
|
|
/* our name for signal 0 */
|
|
|
|
sigtraps[SIGEXIT_].name = "EXIT";
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-11-09 21:32:18 +01:00
|
|
|
(void)sigemptyset(&Sigact_ign.sa_mask);
|
2005-05-23 05:06:10 +02:00
|
|
|
Sigact_ign.sa_flags = 0; /* interruptible */
|
|
|
|
Sigact_ign.sa_handler = SIG_IGN;
|
|
|
|
|
|
|
|
sigtraps[SIGINT].flags |= TF_DFL_INTR | TF_TTY_INTR;
|
|
|
|
sigtraps[SIGQUIT].flags |= TF_DFL_INTR | TF_TTY_INTR;
|
2011-02-09 16:08:01 +01:00
|
|
|
/* SIGTERM is not fatal for interactive */
|
|
|
|
sigtraps[SIGTERM].flags |= TF_DFL_INTR;
|
2005-05-23 05:06:10 +02:00
|
|
|
sigtraps[SIGHUP].flags |= TF_FATAL;
|
|
|
|
sigtraps[SIGCHLD].flags |= TF_SHELL_USES;
|
|
|
|
|
|
|
|
/* these are always caught so we can clean up any temporary files. */
|
|
|
|
setsig(&sigtraps[SIGINT], trapsig, SS_RESTORE_ORIG);
|
|
|
|
setsig(&sigtraps[SIGQUIT], trapsig, SS_RESTORE_ORIG);
|
|
|
|
setsig(&sigtraps[SIGTERM], trapsig, SS_RESTORE_ORIG);
|
|
|
|
setsig(&sigtraps[SIGHUP], trapsig, SS_RESTORE_ORIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alarm_catcher(int sig);
|
|
|
|
|
|
|
|
void
|
|
|
|
alarm_init(void)
|
|
|
|
{
|
|
|
|
sigtraps[SIGALRM].flags |= TF_SHELL_USES;
|
|
|
|
setsig(&sigtraps[SIGALRM], alarm_catcher,
|
|
|
|
SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
2009-12-12 23:27:10 +01:00
|
|
|
alarm_catcher(int sig MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-07-04 15:36:43 +02:00
|
|
|
/* this runs inside interrupt context, with errno saved */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (ksh_tmout_state == TMOUT_READING) {
|
|
|
|
int left = alarm(0);
|
|
|
|
|
|
|
|
if (left == 0) {
|
|
|
|
ksh_tmout_state = TMOUT_LEAVING;
|
|
|
|
intrsig = 1;
|
|
|
|
} else
|
|
|
|
alarm(left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Trap *
|
|
|
|
gettrap(const char *name, int igncase)
|
|
|
|
{
|
2007-01-12 01:37:09 +01:00
|
|
|
int n = NSIG + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
Trap *p;
|
2007-07-31 12:42:15 +02:00
|
|
|
const char *n2;
|
2007-08-20 16:12:29 +02:00
|
|
|
int (*cmpfunc)(const char *, const char *) = strcmp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isdigit(*name)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (getn(name, &n) && 0 <= n && n < NSIG)
|
2007-01-12 01:37:09 +01:00
|
|
|
return (&sigtraps[n]);
|
2007-07-31 12:42:15 +02:00
|
|
|
else
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
n2 = strncasecmp(name, "SIG", 3) ? NULL : name + 3;
|
2007-08-20 16:12:29 +02:00
|
|
|
if (igncase)
|
|
|
|
cmpfunc = strcasecmp;
|
2007-07-31 12:42:15 +02:00
|
|
|
for (p = sigtraps; --n >= 0; p++)
|
|
|
|
if (!cmpfunc(p->name, name) || (n2 && !cmpfunc(p->name, n2)))
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
return (p);
|
2007-01-12 01:37:09 +01:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trap signal handler
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
trapsig(int i)
|
|
|
|
{
|
|
|
|
Trap *p = &sigtraps[i];
|
|
|
|
int errno_ = errno;
|
|
|
|
|
|
|
|
trap = p->set = 1;
|
|
|
|
if (p->flags & TF_DFL_INTR)
|
|
|
|
intrsig = 1;
|
|
|
|
if ((p->flags & TF_FATAL) && !p->trap) {
|
|
|
|
fatal_trap = 1;
|
|
|
|
intrsig = 1;
|
|
|
|
}
|
|
|
|
if (p->shtrap)
|
|
|
|
(*p->shtrap)(i);
|
|
|
|
errno = errno_;
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* called when we want to allow the user to ^C out of something - won't
|
2005-05-23 05:06:10 +02:00
|
|
|
* work if user has trapped SIGINT.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
intrcheck(void)
|
|
|
|
{
|
|
|
|
if (intrsig)
|
|
|
|
runtraps(TF_DFL_INTR|TF_FATAL);
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* called after EINTR to check if a signal with normally causes process
|
2005-05-23 05:06:10 +02:00
|
|
|
* termination has been received.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fatal_trap_check(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Trap *p;
|
|
|
|
|
|
|
|
/* todo: should check if signal is fatal, not the TF_DFL_INTR flag */
|
|
|
|
for (p = sigtraps, i = NSIG+1; --i >= 0; p++)
|
|
|
|
if (p->set && (p->flags & (TF_DFL_INTR|TF_FATAL)))
|
|
|
|
/* return value is used as an exit code */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (128 + p->signal);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Returns the signal number of any pending traps: ie, a signal which has
|
2005-05-23 05:06:10 +02:00
|
|
|
* occurred for which a trap has been set or for which the TF_DFL_INTR flag
|
|
|
|
* is set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
trap_pending(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Trap *p;
|
|
|
|
|
|
|
|
for (p = sigtraps, i = NSIG+1; --i >= 0; p++)
|
|
|
|
if (p->set && ((p->trap && p->trap[0]) ||
|
|
|
|
((p->flags & (TF_DFL_INTR|TF_FATAL)) && !p->trap)))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (p->signal);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-10 20:12:51 +02:00
|
|
|
* run any pending traps. If intr is set, only run traps that
|
2005-05-23 05:06:10 +02:00
|
|
|
* can interrupt commands.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
runtraps(int flag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Trap *p;
|
|
|
|
|
|
|
|
if (ksh_tmout_state == TMOUT_LEAVING) {
|
|
|
|
ksh_tmout_state = TMOUT_EXECUTING;
|
|
|
|
warningf(false, "timed out waiting for input");
|
|
|
|
unwind(LEXIT);
|
|
|
|
} else
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* XXX: this means the alarm will have no effect if a trap
|
2005-05-23 05:06:10 +02:00
|
|
|
* is caught after the alarm() was started...not good.
|
|
|
|
*/
|
|
|
|
ksh_tmout_state = TMOUT_EXECUTING;
|
|
|
|
if (!flag)
|
|
|
|
trap = 0;
|
|
|
|
if (flag & TF_DFL_INTR)
|
|
|
|
intrsig = 0;
|
|
|
|
if (flag & TF_FATAL)
|
|
|
|
fatal_trap = 0;
|
2010-11-01 18:29:05 +01:00
|
|
|
++trap_nested;
|
2005-05-23 05:06:10 +02:00
|
|
|
for (p = sigtraps, i = NSIG+1; --i >= 0; p++)
|
|
|
|
if (p->set && (!flag ||
|
|
|
|
((p->flags & flag) && p->trap == NULL)))
|
2010-11-01 18:29:05 +01:00
|
|
|
runtrap(p, false);
|
|
|
|
if (!--trap_nested)
|
|
|
|
runtrap(NULL, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-01 18:29:05 +01:00
|
|
|
runtrap(Trap *p, bool is_last)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-11-01 18:29:05 +01:00
|
|
|
int old_changed = 0, i;
|
|
|
|
char *trapstr;
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
/* just clean up, see runtraps() above */
|
|
|
|
goto donetrap;
|
|
|
|
i = p->signal;
|
|
|
|
trapstr = p->trap;
|
2005-05-23 05:06:10 +02:00
|
|
|
p->set = 0;
|
2010-11-01 18:29:05 +01:00
|
|
|
if (trapstr == NULL) {
|
|
|
|
/* SIG_DFL */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (p->flags & TF_FATAL) {
|
|
|
|
/* eg, SIGHUP */
|
|
|
|
exstat = 128 + i;
|
|
|
|
unwind(LLEAVE);
|
|
|
|
}
|
|
|
|
if (p->flags & TF_DFL_INTR) {
|
|
|
|
/* eg, SIGINT, SIGQUIT, SIGTERM, etc. */
|
|
|
|
exstat = 128 + i;
|
|
|
|
unwind(LINTR);
|
|
|
|
}
|
2010-11-01 18:29:05 +01:00
|
|
|
goto donetrap;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2010-11-01 18:29:05 +01:00
|
|
|
if (trapstr[0] == '\0')
|
|
|
|
/* SIG_IGN */
|
|
|
|
goto donetrap;
|
2011-02-09 16:08:01 +01:00
|
|
|
if (i == SIGEXIT_ || i == SIGERR_) {
|
|
|
|
/* avoid recursion on these */
|
2005-05-23 05:06:10 +02:00
|
|
|
old_changed = p->flags & TF_CHANGED;
|
|
|
|
p->flags &= ~TF_CHANGED;
|
|
|
|
p->trap = NULL;
|
|
|
|
}
|
2010-11-01 18:29:05 +01:00
|
|
|
if (trap_exstat == -1)
|
|
|
|
trap_exstat = exstat;
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Note: trapstr is fully parsed before anything is executed, thus
|
2005-05-23 05:06:10 +02:00
|
|
|
* no problem with afree(p->trap) in settrap() while still in use.
|
|
|
|
*/
|
2010-05-22 14:49:16 +02:00
|
|
|
command(trapstr, current_lineno);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (i == SIGEXIT_ || i == SIGERR_) {
|
|
|
|
if (p->flags & TF_CHANGED)
|
|
|
|
/* don't clear TF_CHANGED */
|
|
|
|
afree(trapstr, APERM);
|
|
|
|
else
|
|
|
|
p->trap = trapstr;
|
|
|
|
p->flags |= old_changed;
|
|
|
|
}
|
2010-11-01 18:29:05 +01:00
|
|
|
|
|
|
|
donetrap:
|
|
|
|
/* we're the last trap of a sequence executed */
|
|
|
|
if (is_last && trap_exstat != -1) {
|
|
|
|
exstat = trap_exstat;
|
|
|
|
trap_exstat = -1;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear pending traps and reset user's trap handlers; used after fork(2) */
|
|
|
|
void
|
|
|
|
cleartraps(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Trap *p;
|
|
|
|
|
|
|
|
trap = 0;
|
|
|
|
intrsig = 0;
|
|
|
|
fatal_trap = 0;
|
|
|
|
for (i = NSIG+1, p = sigtraps; --i >= 0; p++) {
|
|
|
|
p->set = 0;
|
|
|
|
if ((p->flags & TF_USER_SET) && (p->trap && p->trap[0]))
|
|
|
|
settrap(p, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore signals just before an exec(2) */
|
|
|
|
void
|
|
|
|
restoresigs(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Trap *p;
|
|
|
|
|
|
|
|
for (i = NSIG+1, p = sigtraps; --i >= 0; p++)
|
|
|
|
if (p->flags & (TF_EXEC_IGN|TF_EXEC_DFL))
|
|
|
|
setsig(p, (p->flags & TF_EXEC_IGN) ? SIG_IGN : SIG_DFL,
|
|
|
|
SS_RESTORE_CURR|SS_FORCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-03-04 01:13:17 +01:00
|
|
|
settrap(Trap *p, const char *s)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
sig_t f;
|
|
|
|
|
|
|
|
if (p->trap)
|
|
|
|
afree(p->trap, APERM);
|
2011-02-09 16:08:01 +01:00
|
|
|
/* handles s == NULL */
|
|
|
|
strdupx(p->trap, s, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
p->flags |= TF_CHANGED;
|
|
|
|
f = !s ? SIG_DFL : s[0] ? trapsig : SIG_IGN;
|
|
|
|
|
|
|
|
p->flags |= TF_USER_SET;
|
|
|
|
if ((p->flags & (TF_DFL_INTR|TF_FATAL)) && f == SIG_DFL)
|
|
|
|
f = trapsig;
|
|
|
|
else if (p->flags & TF_SHELL_USES) {
|
|
|
|
if (!(p->flags & TF_ORIG_IGN) || Flag(FTALKING)) {
|
|
|
|
/* do what user wants at exec time */
|
|
|
|
p->flags &= ~(TF_EXEC_IGN|TF_EXEC_DFL);
|
|
|
|
if (f == SIG_IGN)
|
|
|
|
p->flags |= TF_EXEC_IGN;
|
|
|
|
else
|
|
|
|
p->flags |= TF_EXEC_DFL;
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* assumes handler already set to what shell wants it
|
2005-05-23 05:06:10 +02:00
|
|
|
* (normally trapsig, but could be j_sigchld() or SIG_IGN)
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* todo: should we let user know signal is ignored? how? */
|
|
|
|
setsig(p, f, SS_RESTORE_CURR|SS_USER);
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Called by c_print() when writing to a co-process to ensure SIGPIPE won't
|
2005-05-23 05:06:10 +02:00
|
|
|
* kill shell (unless user catches it and exits)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
block_pipe(void)
|
|
|
|
{
|
|
|
|
int restore_dfl = 0;
|
|
|
|
Trap *p = &sigtraps[SIGPIPE];
|
|
|
|
|
|
|
|
if (!(p->flags & (TF_ORIG_IGN|TF_ORIG_DFL))) {
|
|
|
|
setsig(p, SIG_IGN, SS_RESTORE_CURR);
|
|
|
|
if (p->flags & TF_ORIG_DFL)
|
|
|
|
restore_dfl = 1;
|
|
|
|
} else if (p->cursig == SIG_DFL) {
|
|
|
|
setsig(p, SIG_IGN, SS_RESTORE_CURR);
|
2011-02-09 16:08:01 +01:00
|
|
|
/* restore to SIG_DFL */
|
|
|
|
restore_dfl = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (restore_dfl);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called by c_print() to undo whatever block_pipe() did */
|
|
|
|
void
|
|
|
|
restore_pipe(int restore_dfl)
|
|
|
|
{
|
|
|
|
if (restore_dfl)
|
|
|
|
setsig(&sigtraps[SIGPIPE], SIG_DFL, SS_RESTORE_CURR);
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Set action for a signal. Action may not be set if original
|
2005-05-23 05:06:10 +02:00
|
|
|
* action was SIG_IGN, depending on the value of flags and FTALKING.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
setsig(Trap *p, sig_t f, int flags)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
if (p->signal == SIGEXIT_ || p->signal == SIGERR_)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* First time setting this signal? If so, get and note the current
|
2005-05-23 05:06:10 +02:00
|
|
|
* setting.
|
|
|
|
*/
|
|
|
|
if (!(p->flags & (TF_ORIG_IGN|TF_ORIG_DFL))) {
|
|
|
|
sigaction(p->signal, &Sigact_ign, &sigact);
|
|
|
|
p->flags |= sigact.sa_handler == SIG_IGN ?
|
|
|
|
TF_ORIG_IGN : TF_ORIG_DFL;
|
|
|
|
p->cursig = SIG_IGN;
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*-
|
|
|
|
* Generally, an ignored signal stays ignored, except if
|
2005-05-23 05:06:10 +02:00
|
|
|
* - the user of an interactive shell wants to change it
|
|
|
|
* - the shell wants for force a change
|
|
|
|
*/
|
|
|
|
if ((p->flags & TF_ORIG_IGN) && !(flags & SS_FORCE) &&
|
|
|
|
(!(flags & SS_USER) || !Flag(FTALKING)))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
setexecsig(p, flags & SS_RESTORE_MASK);
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
2010-07-04 19:45:17 +02:00
|
|
|
* This is here 'cause there should be a way of clearing
|
|
|
|
* shtraps, but don't know if this is a sane way of doing
|
|
|
|
* it. At the moment, all users of shtrap are lifetime
|
|
|
|
* users (SIGALRM, SIGCHLD, SIGWINCH).
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (!(flags & SS_USER))
|
2007-06-04 23:33:28 +02:00
|
|
|
p->shtrap = (sig_t)NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (flags & SS_SHTRAP) {
|
|
|
|
p->shtrap = f;
|
|
|
|
f = trapsig;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->cursig != f) {
|
|
|
|
p->cursig = f;
|
2008-11-09 21:32:18 +01:00
|
|
|
(void)sigemptyset(&sigact.sa_mask);
|
2011-02-09 16:08:01 +01:00
|
|
|
/* interruptible */
|
|
|
|
sigact.sa_flags = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
sigact.sa_handler = f;
|
|
|
|
sigaction(p->signal, &sigact, NULL);
|
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* control what signal is set to before an exec() */
|
|
|
|
void
|
|
|
|
setexecsig(Trap *p, int restore)
|
|
|
|
{
|
|
|
|
/* XXX debugging */
|
|
|
|
if (!(p->flags & (TF_ORIG_IGN|TF_ORIG_DFL)))
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("setexecsig: unset signal %d(%s)",
|
2005-05-23 05:06:10 +02:00
|
|
|
p->signal, p->name);
|
|
|
|
|
|
|
|
/* restore original value for exec'd kids */
|
|
|
|
p->flags &= ~(TF_EXEC_IGN|TF_EXEC_DFL);
|
|
|
|
switch (restore & SS_RESTORE_MASK) {
|
2011-02-09 16:08:01 +01:00
|
|
|
case SS_RESTORE_CURR:
|
|
|
|
/* leave things as they currently are */
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case SS_RESTORE_ORIG:
|
|
|
|
p->flags |= p->flags & TF_ORIG_IGN ? TF_EXEC_IGN : TF_EXEC_DFL;
|
|
|
|
break;
|
|
|
|
case SS_RESTORE_DFL:
|
|
|
|
p->flags |= TF_EXEC_DFL;
|
|
|
|
break;
|
|
|
|
case SS_RESTORE_IGN:
|
|
|
|
p->flags |= TF_EXEC_IGN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|