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
|
|
|
/*-
|
2012-03-28 00:36:53 +02:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
|
|
|
* 2011, 2012
|
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
|
|
|
|
2012-11-30 20:02:10 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.128 2012/11/30 19:02:07 tg Exp $");
|
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
|
2011-12-31 01:23:42 +01:00
|
|
|
static int histload(Source *, unsigned char *, size_t);
|
|
|
|
static int writehistline(int, int, const char *);
|
|
|
|
static void writehistfile(int, const char *);
|
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-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);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-12-31 01:23:42 +01:00
|
|
|
static bool hstarted; /* set after hist_init() called */
|
• 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 Source *hist_source;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2011-12-10 15:12:17 +01:00
|
|
|
/*XXX imake style */
|
|
|
|
#if defined(__linux)
|
|
|
|
#define caddr_cast(x) ((void *)(x))
|
|
|
|
#else
|
|
|
|
#define caddr_cast(x) ((caddr_t)(x))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* several OEs do not have these constants */
|
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED caddr_cast(-1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* some OEs need the default mapping type specified */
|
|
|
|
#ifndef MAP_FILE
|
|
|
|
#define MAP_FILE 0
|
|
|
|
#endif
|
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
/* current history file: name, fd, size */
|
|
|
|
static char *hname;
|
2011-12-30 21:35:29 +01:00
|
|
|
static int histfd = -1;
|
2011-12-31 01:23:42 +01:00
|
|
|
static off_t histfsize;
|
2006-11-10 06:23:14 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
static const char Tnot_in_history[] = "not in history";
|
|
|
|
#define Thistory (Tnot_in_history + 7)
|
2011-02-09 16:08:01 +01:00
|
|
|
|
2011-12-31 01:23:42 +01:00
|
|
|
static const char TFCEDIT_dollaru[] = "${FCEDIT:-/bin/ed} $_";
|
|
|
|
#define Tspdollaru (TFCEDIT_dollaru + 18)
|
|
|
|
|
|
|
|
/* HISTSIZE default: size of saved history, persistent or standard */
|
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
#define MKSH_DEFHISTSIZE 255
|
|
|
|
#else
|
|
|
|
#define MKSH_DEFHISTSIZE 2047
|
|
|
|
#endif
|
|
|
|
/* maximum considered size of persistent history file */
|
|
|
|
#define MKSH_MAXHISTFSIZE ((off_t)1048576 * 96)
|
|
|
|
|
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;
|
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;
|
2011-12-31 01:23:42 +01:00
|
|
|
const char *p, *first = NULL, *last = NULL;
|
|
|
|
char **hfirst, **hlast, **hp, *editor = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!Flag(FTALKING_I)) {
|
2011-09-07 17:24:22 +02:00
|
|
|
bi_errorf("history %ss not available", Tfunction);
|
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);
|
2011-12-31 01:23:42 +01:00
|
|
|
memcpy(editor + len, Tspdollaru, 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) {
|
2011-12-31 01:23:42 +01:00
|
|
|
char *pat = NULL, *rep = NULL, *line;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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);
|
2011-12-31 01:23:42 +01:00
|
|
|
/* hist_replace */
|
|
|
|
if (!pat)
|
|
|
|
strdupx(line, *hp, ATEMP);
|
|
|
|
else {
|
|
|
|
char *s, *s1;
|
|
|
|
size_t len, pat_len, rep_len;
|
|
|
|
XString xs;
|
|
|
|
char *xp;
|
|
|
|
bool any_subst = false;
|
|
|
|
|
|
|
|
pat_len = strlen(pat);
|
|
|
|
rep_len = strlen(rep);
|
|
|
|
Xinit(xs, xp, 128, ATEMP);
|
|
|
|
for (s = *hp; (s1 = strstr(s, pat)) &&
|
|
|
|
(!any_subst || gflag); s = s1 + pat_len) {
|
|
|
|
any_subst = true;
|
|
|
|
len = s1 - s;
|
|
|
|
XcheckN(xs, xp, len + rep_len);
|
|
|
|
/*; first part */
|
|
|
|
memcpy(xp, s, len);
|
|
|
|
xp += len;
|
|
|
|
/* replacement */
|
|
|
|
memcpy(xp, rep, rep_len);
|
|
|
|
xp += rep_len;
|
|
|
|
}
|
|
|
|
if (!any_subst) {
|
|
|
|
bi_errorf("bad substitution");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
len = strlen(s) + 1;
|
|
|
|
XcheckN(xs, xp, len);
|
|
|
|
memcpy(xp, s, len);
|
|
|
|
xp += len;
|
|
|
|
line = Xclose(xs, xp);
|
|
|
|
}
|
|
|
|
return (hist_execute(line));
|
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",
|
2012-04-14 18:07:48 +02:00
|
|
|
"create", tf->tffn, 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",
|
2012-04-14 18:07:48 +02:00
|
|
|
"write", tf->tffn, 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) */
|
2012-04-14 18:07:48 +02:00
|
|
|
setstr(local("_", false), tf->tffn, KSH_RETURN_ERROR);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* XXX: source should not get trashed by this.. */
|
|
|
|
{
|
|
|
|
Source *sold = source;
|
|
|
|
int ret;
|
|
|
|
|
2011-12-31 01:23:42 +01:00
|
|
|
ret = command(editor ? editor : TFCEDIT_dollaru, 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;
|
2011-12-31 01:23:42 +01:00
|
|
|
ssize_t n;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-04-14 18:07:48 +02:00
|
|
|
if (!(shf = shf_open(tf->tffn, O_RDONLY, 0, 0))) {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("can't %s temporary file %s: %s",
|
2012-04-14 18:07:48 +02:00
|
|
|
"open", tf->tffn, strerror(errno));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 18:07:48 +02:00
|
|
|
if (stat(tf->tffn, &statb) < 0)
|
2010-09-14 23:26:19 +02:00
|
|
|
n = 128;
|
2011-12-31 01:23:42 +01:00
|
|
|
else if ((off_t)statb.st_size > MKSH_MAXHISTFSIZE) {
|
2011-09-07 17:24:22 +02:00
|
|
|
bi_errorf("%s %s too large: %lu", Thistory,
|
2011-02-09 16:08:01 +01:00
|
|
|
"file", (unsigned long)statb.st_size);
|
|
|
|
goto errout;
|
|
|
|
} else
|
2012-03-29 21:23:01 +02:00
|
|
|
n = (size_t)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",
|
2012-04-14 18:07:48 +02:00
|
|
|
"read", tf->tffn, 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)
|
|
|
|
{
|
2011-12-31 01:23:42 +01:00
|
|
|
static int last_line = -1;
|
2005-05-23 05:06:10 +02:00
|
|
|
Source *sold;
|
|
|
|
int ret;
|
|
|
|
char *p, *q;
|
|
|
|
|
2011-12-31 01:23:42 +01:00
|
|
|
/* Back up over last histsave */
|
|
|
|
if (histptr >= history && last_line != hist_source->line) {
|
|
|
|
hist_source->line--;
|
|
|
|
afree(*histptr, APERM);
|
|
|
|
histptr--;
|
|
|
|
last_line = hist_source->line;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-09-07 17:24:22 +02:00
|
|
|
bi_errorf("%s: %s", str, Tnot_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-09-07 17:24:22 +02:00
|
|
|
bi_errorf("%s: %s", str, Tnot_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-09-07 17:24:22 +02:00
|
|
|
bi_errorf("%s: %s", str, Tnot_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
|
|
|
}
|
|
|
|
|
2012-05-04 22:49:08 +02:00
|
|
|
#if !defined(MKSH_NO_CMDLINE_EDITING) && !MKSH_S_NOVI
|
2011-12-31 01:23:42 +01:00
|
|
|
/* current position in history[] */
|
|
|
|
static char **current;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
}
|
2011-12-31 01:23:42 +01:00
|
|
|
#endif
|
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)
|
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
char **hp;
|
|
|
|
int maxhist = histptr - history;
|
|
|
|
int incr = fwd ? 1 : -1;
|
|
|
|
size_t len = strlen(str);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* set history; this means reallocating the dataspace
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
void
|
2011-12-30 22:00:32 +01:00
|
|
|
sethistsize(mksh_ari_t n)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
if (n > 0 && n != histsize) {
|
|
|
|
int cursize = histptr - history;
|
|
|
|
|
|
|
|
/* save most recent history */
|
|
|
|
if (n < cursize) {
|
2011-12-31 01:23:42 +01:00
|
|
|
memmove(history, histptr - n + 1, n * sizeof(char *));
|
|
|
|
cursize = n - 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* set history file; this can mean reloading/resetting/starting
|
|
|
|
* history file maintenance
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
sethistfile(const char *name)
|
|
|
|
{
|
|
|
|
/* if not started then nothing to do */
|
2011-12-31 01:23:42 +01:00
|
|
|
if (hstarted == false)
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* if the name is the same as the name we have */
|
|
|
|
if (hname && strcmp(hname, name) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* it's a new name - possibly
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-12-30 21:35:29 +01:00
|
|
|
if (histfd != -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/* yes the file is open */
|
2009-08-08 15:08:53 +02:00
|
|
|
(void)close(histfd);
|
2011-12-30 21:35:29 +01:00
|
|
|
histfd = -1;
|
2011-12-31 01:23:42 +01:00
|
|
|
histfsize = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
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
|
|
|
|
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* initialise the history vector
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_histvec(void)
|
|
|
|
{
|
|
|
|
if (history == (char **)NULL) {
|
2011-12-31 01:23:42 +01:00
|
|
|
histsize = MKSH_DEFHISTSIZE;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* It turns out that there is a lot of ghastly hackery here
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
2011-12-30 21:35:29 +01:00
|
|
|
if (histfd != -1) {
|
2010-01-25 17:12:57 +01:00
|
|
|
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
|
2011-12-30 21:35:29 +01:00
|
|
|
if (dowrite && histfd != -1)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* 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.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
2011-11-26 19:19:00 +01:00
|
|
|
* This stuff uses mmap()
|
2011-12-31 01:23:42 +01:00
|
|
|
*
|
|
|
|
* This stuff is so totally broken it must eventually be
|
|
|
|
* redesigned, without mmap, better checks, support for
|
|
|
|
* larger files, etc. and handle partially corrupted files
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
|
2011-02-09 16:08:01 +01:00
|
|
|
/*-
|
2011-11-26 19:19:00 +01:00
|
|
|
* Open a history file
|
|
|
|
* Format is:
|
|
|
|
* Bytes 1, 2:
|
|
|
|
* HMAGIC - just to check that we are dealing with the correct object
|
|
|
|
* Then follows a number of stored commands
|
|
|
|
* Each command is
|
2011-12-31 01:23:42 +01:00
|
|
|
* <command byte><command number(4 octets, big endian)><bytes><NUL>
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-12-31 01:23:42 +01:00
|
|
|
#define HMAGIC1 0xAB
|
|
|
|
#define HMAGIC2 0xCD
|
|
|
|
#define COMMAND 0xFF
|
|
|
|
|
2012-04-01 05:23:08 +02:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2011-12-31 01:23:42 +01:00
|
|
|
static const unsigned char sprinkle[2] = { HMAGIC1, HMAGIC2 };
|
2012-04-01 05:23:08 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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;
|
2011-12-31 01:23:42 +01:00
|
|
|
int lines, fd;
|
|
|
|
enum { hist_init_first, hist_init_retry, hist_init_restore } hs;
|
2005-05-23 17:36:56 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (Flag(FTALKING) == 0)
|
|
|
|
return;
|
|
|
|
|
2011-12-31 01:23:42 +01:00
|
|
|
hstarted = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
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);
|
2011-12-31 01:23:42 +01:00
|
|
|
hs = hist_init_first;
|
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 */
|
2011-12-31 01:23:42 +01:00
|
|
|
if ((fd = open(hname, O_RDWR | O_CREAT | O_APPEND, 0600)) < 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
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
|
|
|
|
2012-03-28 00:36:53 +02:00
|
|
|
mksh_lockfd(histfd);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-12-31 01:23:42 +01:00
|
|
|
histfsize = lseek(histfd, (off_t)0, SEEK_END);
|
|
|
|
if (histfsize > MKSH_MAXHISTFSIZE || hs == hist_init_restore) {
|
|
|
|
/* we ignore too large files but still append to them */
|
|
|
|
/* we also don't need to re-read after truncation */
|
|
|
|
goto hist_init_tail;
|
|
|
|
} else if (histfsize > 2) {
|
|
|
|
/* we have some data, check its validity */
|
|
|
|
base = (void *)mmap(NULL, (size_t)histfsize, PROT_READ,
|
2009-08-08 15:08:53 +02:00
|
|
|
MAP_FILE | MAP_PRIVATE, histfd, (off_t)0);
|
2011-12-31 01:23:42 +01:00
|
|
|
if (base == (unsigned char *)MAP_FAILED)
|
|
|
|
goto hist_init_fail;
|
|
|
|
if (base[0] != HMAGIC1 || base[1] != HMAGIC2) {
|
|
|
|
munmap(caddr_cast(base), (size_t)histfsize);
|
|
|
|
goto hist_init_fail;
|
|
|
|
}
|
|
|
|
/* load _all_ data */
|
|
|
|
lines = histload(hist_source, base + 2, (size_t)histfsize - 2);
|
|
|
|
munmap(caddr_cast(base), (size_t)histfsize);
|
|
|
|
/* check if the file needs to be truncated */
|
|
|
|
if (lines > histsize && histptr >= history) {
|
|
|
|
/* you're fucked up with the current code, trust me */
|
|
|
|
char *nhname, **hp;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
/* create temporary file */
|
|
|
|
nhname = shf_smprintf("%s.%d", hname, (int)procpid);
|
|
|
|
if ((fd = open(nhname, O_RDWR | O_CREAT | O_TRUNC |
|
|
|
|
O_EXCL, 0600)) < 0) {
|
|
|
|
/* just don't truncate then, meh. */
|
|
|
|
goto hist_trunc_dont;
|
|
|
|
}
|
|
|
|
if (fstat(histfd, &sb) >= 0 &&
|
|
|
|
chown(nhname, sb.st_uid, sb.st_gid)) {
|
|
|
|
/* abort the truncation then, meh. */
|
|
|
|
goto hist_trunc_abort;
|
|
|
|
}
|
|
|
|
/* we definitively want some magic in that file */
|
|
|
|
if (write(fd, sprinkle, 2) != 2)
|
|
|
|
goto hist_trunc_abort;
|
|
|
|
/* and of course the entries */
|
|
|
|
hp = history;
|
|
|
|
while (hp < histptr) {
|
|
|
|
if (!writehistline(fd,
|
|
|
|
s->line - (histptr - hp), *hp))
|
|
|
|
goto hist_trunc_abort;
|
|
|
|
++hp;
|
|
|
|
}
|
|
|
|
/* now unlock, close both, rename, rinse, repeat */
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2005-05-23 05:06:10 +02:00
|
|
|
hist_finish();
|
2011-12-31 01:23:42 +01:00
|
|
|
if (rename(nhname, hname) < 0) {
|
|
|
|
hist_trunc_abort:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
unlink(nhname);
|
|
|
|
if (fd != -1)
|
|
|
|
goto hist_trunc_dont;
|
|
|
|
/* darn! restore histfd and pray */
|
|
|
|
}
|
|
|
|
hs = hist_init_restore;
|
|
|
|
hist_trunc_dont:
|
|
|
|
afree(nhname, ATEMP);
|
|
|
|
if (hs == hist_init_restore)
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
} else if (histfsize != 0) {
|
|
|
|
/* negative or too small... */
|
|
|
|
hist_init_fail:
|
|
|
|
/* ... or mmap failed or illegal */
|
|
|
|
hist_finish();
|
|
|
|
/* nuke the bogus file then retry, at most once */
|
|
|
|
if (!unlink(hname) && hs != hist_init_retry) {
|
|
|
|
hs = hist_init_retry;
|
2005-05-23 05:06:10 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
2011-12-31 01:23:42 +01:00
|
|
|
if (hs != hist_init_retry)
|
|
|
|
bi_errorf("can't %s %s: %s",
|
|
|
|
"unlink HISTFILE", hname, strerror(errno));
|
|
|
|
histfsize = 0;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* size 0, add magic to the history file */
|
|
|
|
if (write(histfd, sprinkle, 2) != 2) {
|
|
|
|
hist_finish();
|
|
|
|
return;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-31 01:23:42 +01:00
|
|
|
histfsize = lseek(histfd, (off_t)0, SEEK_END);
|
|
|
|
hist_init_tail:
|
2012-03-28 00:36:53 +02:00
|
|
|
mksh_unlkfd(histfd);
|
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
|
|
|
/*
|
2011-11-26 19:19:00 +01:00
|
|
|
* load the history structure from the stored data
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-12-31 01:23:42 +01:00
|
|
|
static int
|
2011-12-30 22:00:32 +01:00
|
|
|
histload(Source *s, unsigned char *base, size_t bytes)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-12-31 01:23:42 +01:00
|
|
|
int lno = 0, lines = 0;
|
|
|
|
unsigned char *cp;
|
|
|
|
|
|
|
|
histload_loop:
|
|
|
|
/* !bytes check as some systems (older FreeBSDs) have buggy memchr */
|
|
|
|
if (!bytes || (cp = memchr(base, COMMAND, bytes)) == NULL)
|
|
|
|
return (lines);
|
|
|
|
/* advance base pointer past COMMAND byte */
|
|
|
|
bytes -= ++cp - base;
|
|
|
|
base = cp;
|
|
|
|
/* if there is no full string left, don't bother with the rest */
|
|
|
|
if (bytes < 5 || (cp = memchr(base + 4, '\0', bytes - 4)) == NULL)
|
|
|
|
return (lines);
|
|
|
|
/* load the stored line number */
|
|
|
|
lno = ((base[0] & 0xFF) << 24) | ((base[1] & 0xFF) << 16) |
|
|
|
|
((base[2] & 0xFF) << 8) | (base[3] & 0xFF);
|
|
|
|
/* store away the found line (@base[4]) */
|
|
|
|
++lines;
|
|
|
|
if (histptr >= history && lno - 1 != s->line) {
|
|
|
|
/* a replacement? */
|
|
|
|
char **hp;
|
|
|
|
|
|
|
|
if (lno >= s->line - (histptr - history) && lno <= s->line) {
|
|
|
|
hp = &histptr[lno - s->line];
|
|
|
|
if (*hp)
|
|
|
|
afree(*hp, APERM);
|
|
|
|
strdupx(*hp, (char *)(base + 4), APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-12-31 01:23:42 +01:00
|
|
|
} else {
|
|
|
|
s->line = lno--;
|
|
|
|
histsave(&lno, (char *)(base + 4), false, false);
|
|
|
|
}
|
|
|
|
/* advance base pointer past NUL */
|
|
|
|
bytes -= ++cp - base;
|
|
|
|
base = cp;
|
|
|
|
/* repeat until no more */
|
|
|
|
goto histload_loop;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-11-26 19:19:00 +01: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 (re)read those commands to update our history
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static void
|
2011-12-31 01:23:42 +01:00
|
|
|
writehistfile(int lno, const char *cmd)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
off_t sizenow;
|
2011-12-31 01:23:42 +01:00
|
|
|
size_t bytes;
|
|
|
|
unsigned char *base, *news;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-03-28 00:36:53 +02:00
|
|
|
mksh_lockfd(histfd);
|
2009-08-08 15:08:53 +02:00
|
|
|
sizenow = lseek(histfd, (off_t)0, SEEK_END);
|
2011-12-31 01:23:42 +01:00
|
|
|
if (sizenow < histfsize) {
|
|
|
|
/* the file has shrunk; give up */
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
/* ignore changes when the file is too large */
|
|
|
|
sizenow <= MKSH_MAXHISTFSIZE
|
|
|
|
&&
|
|
|
|
/* the size has changed, we need to do read updates */
|
|
|
|
sizenow > histfsize
|
|
|
|
) {
|
|
|
|
/* both sizenow and histfsize are <= MKSH_MAXHISTFSIZE */
|
|
|
|
bytes = (size_t)(sizenow - histfsize);
|
|
|
|
base = (void *)mmap(NULL, (size_t)sizenow, PROT_READ,
|
|
|
|
MAP_FILE | MAP_PRIVATE, histfd, (off_t)0);
|
|
|
|
if (base == (unsigned char *)MAP_FAILED)
|
|
|
|
goto bad;
|
|
|
|
news = base + (size_t)histfsize;
|
|
|
|
if (*news == COMMAND) {
|
2005-05-23 05:06:10 +02:00
|
|
|
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;
|
2011-12-31 01:23:42 +01:00
|
|
|
} else
|
|
|
|
bytes = 0;
|
|
|
|
munmap(caddr_cast(base), (size_t)sizenow);
|
|
|
|
if (!bytes)
|
2005-05-23 05:06:10 +02:00
|
|
|
goto bad;
|
|
|
|
}
|
2011-12-31 01:23:42 +01:00
|
|
|
if (cmd && !writehistline(histfd, lno, cmd)) {
|
|
|
|
bad:
|
|
|
|
hist_finish();
|
|
|
|
return;
|
2010-01-25 17:12:57 +01:00
|
|
|
}
|
2011-12-31 01:23:42 +01:00
|
|
|
histfsize = lseek(histfd, (off_t)0, SEEK_END);
|
2012-03-28 00:36:53 +02:00
|
|
|
mksh_unlkfd(histfd);
|
2011-12-31 01:23:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
writehistline(int fd, int lno, const char *cmd)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
unsigned char hdr[5];
|
|
|
|
|
|
|
|
hdr[0] = COMMAND;
|
|
|
|
hdr[1] = (lno >> 24) & 0xFF;
|
|
|
|
hdr[2] = (lno >> 16) & 0xFF;
|
|
|
|
hdr[3] = (lno >> 8) & 0xFF;
|
|
|
|
hdr[4] = lno & 0xFF;
|
|
|
|
n = strlen(cmd) + 1;
|
|
|
|
return (write(fd, hdr, 5) == 5 && write(fd, cmd, n) == n);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hist_finish(void)
|
|
|
|
{
|
2012-05-05 20:04:20 +02:00
|
|
|
if (histfd >= 0) {
|
|
|
|
mksh_unlkfd(histfd);
|
|
|
|
(void)close(histfd);
|
|
|
|
}
|
2011-12-30 21:35:29 +01:00
|
|
|
histfd = -1;
|
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
|
|
|
|
2011-12-31 01:23:42 +01: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;
|
2011-09-07 17:24:22 +02:00
|
|
|
if (i == ksh_SIGERR) {
|
2005-05-23 05:06:10 +02:00
|
|
|
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;
|
|
|
|
|
2012-04-06 15:29:01 +02:00
|
|
|
/* this is not optimal, what about SIGSIG1? */
|
|
|
|
if ((cs[0] & 0xDF) == 'S' &&
|
|
|
|
(cs[1] & 0xDF) == 'I' &&
|
|
|
|
(cs[2] & 0xDF) == 'G' &&
|
|
|
|
cs[3] != '\0') {
|
|
|
|
/* skip leading "SIG" */
|
2008-07-12 19:16:03 +02:00
|
|
|
cs += 3;
|
2012-04-06 15:29:01 +02:00
|
|
|
}
|
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 */
|
2011-09-07 17:24:22 +02:00
|
|
|
sigtraps[ksh_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 *
|
2012-04-06 15:29:01 +02:00
|
|
|
gettrap(const char *cs, bool igncase)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-04-06 15:29:01 +02:00
|
|
|
int i;
|
2005-05-23 05:06:10 +02:00
|
|
|
Trap *p;
|
2012-04-06 15:29:01 +02:00
|
|
|
char *as;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-04-06 15:29:01 +02:00
|
|
|
if (ksh_isdigit(*cs)) {
|
|
|
|
return ((getn(cs, &i) && 0 <= i && i < NSIG) ?
|
|
|
|
(&sigtraps[i]) : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this breaks SIGSIG1, but we do that above anyway */
|
|
|
|
if ((cs[0] & 0xDF) == 'S' &&
|
|
|
|
(cs[1] & 0xDF) == 'I' &&
|
|
|
|
(cs[2] & 0xDF) == 'G' &&
|
|
|
|
cs[3] != '\0') {
|
|
|
|
/* skip leading "SIG" */
|
|
|
|
cs += 3;
|
2007-07-31 12:42:15 +02:00
|
|
|
}
|
2012-04-06 15:29:01 +02:00
|
|
|
if (igncase) {
|
|
|
|
char *s;
|
2007-07-31 12:42:15 +02:00
|
|
|
|
2012-04-06 15:29:01 +02:00
|
|
|
strdupx(as, cs, ATEMP);
|
|
|
|
cs = s = as;
|
|
|
|
while ((*s = ksh_toupper(*s)))
|
|
|
|
++s;
|
|
|
|
} else
|
|
|
|
as = NULL;
|
|
|
|
|
|
|
|
p = sigtraps;
|
|
|
|
for (i = 0; i <= NSIG; i++) {
|
|
|
|
if (!strcmp(p->name, cs))
|
|
|
|
goto found;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
p = NULL;
|
|
|
|
found:
|
|
|
|
afree(as, ATEMP);
|
|
|
|
return (p);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trap signal handler
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
trapsig(int i)
|
|
|
|
{
|
|
|
|
Trap *p = &sigtraps[i];
|
2012-11-30 20:02:10 +01:00
|
|
|
int eno = errno;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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);
|
2012-11-30 20:02:10 +01:00
|
|
|
errno = eno;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
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 */
|
2012-10-21 23:39:06 +02:00
|
|
|
exstat = (int)ksh_min(128U + (unsigned)i, 255U);
|
2005-05-23 05:06:10 +02:00
|
|
|
unwind(LLEAVE);
|
|
|
|
}
|
|
|
|
if (p->flags & TF_DFL_INTR) {
|
|
|
|
/* eg, SIGINT, SIGQUIT, SIGTERM, etc. */
|
2012-10-21 23:39:06 +02:00
|
|
|
exstat = (int)ksh_min(128U + (unsigned)i, 255U);
|
2005-05-23 05:06:10 +02:00
|
|
|
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-09-07 17:24:22 +02:00
|
|
|
if (i == ksh_SIGEXIT || i == ksh_SIGERR) {
|
2011-02-09 16:08:01 +01:00
|
|
|
/* 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)
|
2012-10-21 23:39:06 +02:00
|
|
|
trap_exstat = exstat & 0xFF;
|
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);
|
2011-09-07 17:24:22 +02:00
|
|
|
if (i == ksh_SIGEXIT || i == ksh_SIGERR) {
|
2005-05-23 05:06:10 +02:00
|
|
|
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;
|
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
if (p->signal == ksh_SIGEXIT || p->signal == ksh_SIGERR)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-06-24 21:47:11 +02:00
|
|
|
memset(&sigact, 0, sizeof(sigact));
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 00:36:53 +02:00
|
|
|
|
|
|
|
#if HAVE_PERSISTENT_HISTORY || defined(DF)
|
|
|
|
/*
|
|
|
|
* File descriptor locking and unlocking functions.
|
|
|
|
* Could use some error handling, but hey, this is only
|
|
|
|
* advisory locking anyway, will often not work over NFS,
|
|
|
|
* and you are SOL if this fails...
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
mksh_lockfd(int fd)
|
|
|
|
{
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
/* flock is not interrupted by signals */
|
|
|
|
(void)flock(fd, LOCK_EX);
|
|
|
|
#elif HAVE_FLOCK
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* e.g. on Linux */
|
|
|
|
do {
|
|
|
|
rv = flock(fd, LOCK_EX);
|
|
|
|
} while (rv == 1 && errno == EINTR);
|
|
|
|
#elif HAVE_LOCK_FCNTL
|
|
|
|
int rv;
|
|
|
|
struct flock lks;
|
|
|
|
|
|
|
|
memset(&lks, 0, sizeof(lks));
|
|
|
|
lks.l_type = F_WRLCK;
|
|
|
|
do {
|
|
|
|
rv = fcntl(fd, F_SETLKW, &lks);
|
|
|
|
} while (rv == 1 && errno == EINTR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-29 01:07:47 +02:00
|
|
|
/* designed to not define mksh_unlkfd if none triggered */
|
|
|
|
#if HAVE_FLOCK
|
2012-03-28 00:36:53 +02:00
|
|
|
void
|
|
|
|
mksh_unlkfd(int fd)
|
|
|
|
{
|
|
|
|
(void)flock(fd, LOCK_UN);
|
2012-03-29 01:07:47 +02:00
|
|
|
}
|
2012-03-28 00:36:53 +02:00
|
|
|
#elif HAVE_LOCK_FCNTL
|
2012-03-29 01:07:47 +02:00
|
|
|
void
|
|
|
|
mksh_unlkfd(int fd)
|
|
|
|
{
|
2012-03-28 00:36:53 +02:00
|
|
|
struct flock lks;
|
|
|
|
|
|
|
|
memset(&lks, 0, sizeof(lks));
|
|
|
|
lks.l_type = F_UNLCK;
|
|
|
|
(void)fcntl(fd, F_SETLKW, &lks);
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-29 01:07:47 +02:00
|
|
|
#endif
|