Skip to content

Commit

Permalink
Rebased against ksh from OpenBSD 6.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Apr 14, 2018
2 parents ee67198 + bb71e15 commit e52701b
Show file tree
Hide file tree
Showing 34 changed files with 557 additions and 937 deletions.
23 changes: 7 additions & 16 deletions NOTES
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
$OpenBSD: NOTES,v 1.14 2016/01/29 11:50:40 tb Exp $
$OpenBSD: NOTES,v 1.16 2018/01/12 14:20:57 jca Exp $

General features of at&t ksh88 that are not (yet) in pdksh:
- exported aliases and functions (not in ksh93).
- set -t.
- signals/traps not cleared during functions.
- trap DEBUG, local ERR and EXIT traps in functions.
- ERRNO parameter.
- doesn't have posix file globbing (eg, [[:alpha:]], etc.).
- use of an `agent' to execute unreadable/setuid/setgid shell scripts
(don't ask).
- read/select aren't hooked in to the command line editor
- the last command of a pipeline is not run in the parent shell

Known bugs (see also BUG-REPORTS and PROJECTS files):
Known bugs (see also PROJECTS files):
Variable parsing, Expansion:
- some specials behave differently when unset (eg, IFS behaves like
" \t\n") others lose their special meaning. IFS/PATH taken care of,
Expand All @@ -26,15 +23,14 @@ Known bugs (see also BUG-REPORTS and PROJECTS files):
Commands,Execution:
- setting special parameters that have side effects when
changed/restored (ie, HISTFILE, OPTIND, RANDOM) in front
of a command (eg, HISTFILE=/foo/bar echo hi) effects the parent
of a command (eg, HISTFILE=/foo/bar echo hi) affects the parent
shell. Note that setting other (not so special) parameters
does not effect the parent shell.
does not affect the parent shell.
- `echo hi | exec cat -n' causes at&t to exit, `exec echo hi | cat -n'
does not. pdksh exits for neither. Don't think POSIX requires
an exit, but not sure.
- `echo foo | read bar; echo $bar' prints foo in at&t ksh, nothing
in pdksh (ie, the read is done in a separate process in pdksh).
Misc:

Known problems not caused by ksh:
- after stoping a job, emacs/vi is not re-entered. Hitting return
Expand Down Expand Up @@ -241,23 +237,21 @@ Oddities in ksh (pd & at&t):
- when tracing (set -x), and a command's stderr is redirected, the trace
output is also redirected. so "set -x; echo foo 2> /tmp/O > /dev/null"
will create /tmp/foo with the lines "+ > /dev/null" and "+ echo foo".
- undocumented at&t ksh feature: FPATH is searched after PATH if no
executable is found, even if typeset -uf wasn't used.
- undocumented at&t ksh88, documented in ksh93: FPATH is searched
after PATH if no executable is found, even if typeset -uf wasn't used.

POSIX sh questions (references are to POSIX 1003.2-1992)
- arithmetic expressions: how are empty expressions treated?
(eg, echo $(( ))). at&t ksh (and now pdksh) echo 0.
Same question goes for `test "" -eq 0' - does this generate an error
or, if not, what is the exit code?
- should tilde expansion occur after :'s in the word part of ${..=..}?
(me thinks it should)
- if a signal is received during the execution of a built-in,
does the builtin command exit or the whole shell?
- is it legal to execute last command of pipeline in current
execution environment (eg, can "echo foo | read bar" set
bar?)
- what action should be taken if there is an error doing a dup due
to system limits (eg, not enough feil destriptors): is this
to system limits (eg, not enough file destriptors): is this
a "redirection error" (in which case a script will exit iff the
error occured while executing a special built-in)?
IMHO, shell should exit script. Couldn't find a blanket statement
Expand All @@ -280,9 +274,6 @@ POSIX sh bugs (references are to POSIX 1003.2-1992)
functions don't do the save/restore automatically). Restoring
OPTIND is kind of dumb since getopts may have been in the middle
of parsing a group of flags (eg, -abc).
- unclear whether arithmetic expressions (eg, $((..))) should
understand C integer constants (ie, 0x123, 0177). at&t ksh doesn't
and neither does pdksh.
- `...` definition (3.6.3) says nothing about backslash followed by
a newline, which sh and at&t ksh strip out completely. e.g.,
$ show-args `echo 'X
Expand Down
49 changes: 2 additions & 47 deletions PROJECTS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$OpenBSD: PROJECTS,v 1.8 2015/09/14 09:42:33 nicm Exp $
$OpenBSD: PROJECTS,v 1.9 2018/01/08 12:08:17 jca Exp $

Things to be done in pdksh (see also the NOTES file):

Expand All @@ -22,31 +22,11 @@ Things to be done in pdksh (see also the NOTES file):

(ulimit also needs to be examined to check that it fits the posix style)

* test suite
Ideally, as the builtin utilities are being POSIXized, short tests
should be written to be used in regression testing. The tests
directory contains some tests, but many more need to be written.

* internationalization
Need to handle with the LANG and LC_* environment variables. This
involves changes to ensure <ctype.h> macros are being used (currently
uses its own macros in many places), figuring out how to deal with
bases (for integer arithmetic, eg, 12#1A), and (the nasty one) doing
string look ups for error messages, etc.. It probably isn't worth
translating strings to other languages yet as the code is likely
to change a lot in the near future, but it would be good to have the
code set up so string tables can be used.

* trap code
* add the DEBUG trap.
* fix up signal handling code. In particular, fatal vs tty signals,
have signal routine to call to check for pending/fatal traps, etc.

* parsing
* the time keyword needs to be hacked to accept options (!) since
POSIX says it shall accept the -p option and must skip a -- argument
(end of options). Yuck.

* lexing
the lexing may need a re-write since it currently doesn't parse $( .. ),
$(( .. )), (( ... )) properly.
Expand All @@ -68,30 +48,10 @@ Things to be done in pdksh (see also the NOTES file):
in general, treatment of OPTIND/OPTARG,

* history
There are two versions of the history code, COMPLEX_HISTORY and
EASY_HISTORY, which need to be merged. COMPLEX does at&t style history
where the history file is written after each command and checked when
ever looking through the history (in case another shell has added
something). EASY simply reads the history file at startup and writes
it before exiting.
* re-write the COMPLEX_HISTORY code so mmap() not needed (currently
can't be used on machines without mmap()).
* Add multiline knowledge to COMPLEX_HISTORY (see EASY_HISTORY
stuff).
* change COMPLEX_HISTORY code so concurrent history files are
controlled by an option (set -o history-concurrent?). Delete
the EASY_HISTORY code.
* Add multiline knowledge
* bring history code up to POSIX standards (see POSIX description
of fc, etc.).

* documentation
Some sort of tutorial with examples would be good. Texinfo is probably
the best medium for this. Also, the man page could be converted to
texinfo (if the tutorial and man page are put in the same texinfo
page, they should be somewhat distinct - i.e., the tutorial should
be a separate thread - but there should be cross references between the
two).

* miscellaneous
* POSIX specifies what happens when various kinds of errors occur
in special built-ins commands vs regular commands (builtin or
Expand All @@ -104,8 +64,3 @@ Things to be done in pdksh (see also the NOTES file):
* merge the emacs and vi code (should reduce the size of the shell and
make maintenance easier); handle SIGWINCH while editing a line.
[John Rochester is working on the merge]

* add POSIX globbing (eg, [[:alnum:]]), see POSIX.2:2.8.3.2.

* teach shf_vfprintf() about long long's (%lld); also make %p use
long longs if appropriate.
14 changes: 7 additions & 7 deletions alloc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: alloc.c,v 1.17 2017/08/15 17:57:57 jca Exp $ */
/* $OpenBSD: alloc.c,v 1.19 2018/01/16 22:52:32 jca Exp $ */

/* Public domain, like most of the rest of ksh */

Expand Down Expand Up @@ -45,11 +45,11 @@ alloc(size_t size, Area *ap)

/* ensure that we don't overflow by allocating space for link */
if (size > SIZE_MAX - sizeof(struct link))
internal_errorf(1, "unable to allocate memory");
internal_errorf("unable to allocate memory");

l = calloc(1, sizeof(struct link) + size);
l = malloc(sizeof(struct link) + size);
if (l == NULL)
internal_errorf(1, "unable to allocate memory");
internal_errorf("unable to allocate memory");
l->next = ap->freelist;
l->prev = NULL;
if (ap->freelist)
Expand All @@ -73,7 +73,7 @@ areallocarray(void *ptr, size_t nmemb, size_t size, Area *ap)
/* condition logic cloned from calloc() */
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
nmemb > 0 && SIZE_MAX / nmemb < size) {
internal_errorf(1, "unable to allocate memory");
internal_errorf("unable to allocate memory");
}

return aresize(ptr, nmemb * size, ap);
Expand All @@ -89,15 +89,15 @@ aresize(void *ptr, size_t size, Area *ap)

/* ensure that we don't overflow by allocating space for link */
if (size > SIZE_MAX - sizeof(struct link))
internal_errorf(1, "unable to allocate memory");
internal_errorf("unable to allocate memory");

l = P2L(ptr);
lprev = l->prev;
lnext = l->next;

l2 = realloc(l, sizeof(struct link) + size);
if (l2 == NULL)
internal_errorf(1, "unable to allocate memory");
internal_errorf("unable to allocate memory");
if (lprev)
lprev->next = l2;
else
Expand Down
19 changes: 7 additions & 12 deletions c_ksh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: c_ksh.c,v 1.51 2017/09/03 11:52:01 jca Exp $ */
/* $OpenBSD: c_ksh.c,v 1.59 2018/03/15 16:51:29 anton Exp $ */

/*
* built-in Korn commands: c_*
Expand Down Expand Up @@ -941,8 +941,8 @@ c_alias(char **wp)
afree(ap->val.s, APERM);
}
/* ignore values for -t (at&t ksh does this) */
newval = tflag ? search(alias, path, X_OK, NULL) :
val;
newval = tflag ? search(alias, search_path, X_OK, NULL)
: val;
if (newval) {
ap->val.s = str_save(newval, APERM);
ap->flag |= ALLOC|ISSET;
Expand Down Expand Up @@ -1068,7 +1068,6 @@ c_jobs(char **wp)
return rv;
}

#ifdef JOBS
int
c_fgbg(char **wp)
{
Expand All @@ -1092,7 +1091,6 @@ c_fgbg(char **wp)
*/
return (bg || Flag(FPOSIX)) ? 0 : rv;
}
#endif

struct kill_info {
int num_width;
Expand Down Expand Up @@ -1184,7 +1182,7 @@ c_kill(char **wp)
shprintf("%s%s", p, sigtraps[i].name);
shprintf("\n");
} else {
int mess_width = 0, w, i;
int mess_width = 0, w;
struct kill_info ki = {
.num_width = 1,
.name_width = 0,
Expand All @@ -1194,7 +1192,8 @@ c_kill(char **wp)
ki.num_width++;

for (i = 0; i < NSIG; i++) {
w = sigtraps[i].name ? strlen(sigtraps[i].name) :
w = sigtraps[i].name ?
(int)strlen(sigtraps[i].name) :
ki.num_width;
if (w > ki.name_width)
ki.name_width = w;
Expand Down Expand Up @@ -1274,7 +1273,7 @@ c_getopts(char **wp)
}

if (genv->loc->next == NULL) {
internal_errorf(0, "c_getopts: no argv");
internal_warningf("%s: no argv", __func__);
return 1;
}
/* Which arguments are we parsing... */
Expand Down Expand Up @@ -1385,9 +1384,7 @@ const struct builtin kshbuiltins [] = {
{"+command", c_command},
{"echo", c_print},
{"*=export", c_typeset},
#ifdef HISTORY
{"+fc", c_fc},
#endif /* HISTORY */
{"+getopts", c_getopts},
{"+jobs", c_jobs},
{"+kill", c_kill},
Expand All @@ -1398,10 +1395,8 @@ const struct builtin kshbuiltins [] = {
{"=typeset", c_typeset},
{"+unalias", c_unalias},
{"whence", c_whence},
#ifdef JOBS
{"+bg", c_fgbg},
{"+fg", c_fgbg},
#endif
#ifdef EMACS
{"bind", c_bind},
#endif
Expand Down
16 changes: 8 additions & 8 deletions c_sh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: c_sh.c,v 1.60 2017/07/22 09:37:21 anton Exp $ */
/* $OpenBSD: c_sh.c,v 1.62 2017/12/27 13:02:57 millert Exp $ */

/*
* built-in Bourne commands
Expand Down Expand Up @@ -201,7 +201,7 @@ c_dot(char **wp)

if ((cp = wp[builtin_opt.optind]) == NULL)
return 0;
file = search(cp, path, R_OK, &err);
file = search(cp, search_path, R_OK, &err);
if (file == NULL) {
bi_errorf("%s: %s", cp, err ? strerror(err) : "not found");
return 1;
Expand Down Expand Up @@ -251,7 +251,7 @@ int
c_read(char **wp)
{
int c = 0;
int expand = 1, history = 0;
int expand = 1, savehist = 0;
int expanding;
int ecode = 0;
char *cp;
Expand All @@ -275,7 +275,7 @@ c_read(char **wp)
expand = 0;
break;
case 's':
history = 1;
savehist = 1;
break;
case 'u':
if (!*(cp = builtin_opt.optarg))
Expand Down Expand Up @@ -321,7 +321,7 @@ c_read(char **wp)
* coproc_readw_close(fd);
*/

if (history)
if (savehist)
Xinit(xs, xp, 128, ATEMP);
expanding = 0;
Xinit(cs, cp, 128, ATEMP);
Expand Down Expand Up @@ -349,7 +349,7 @@ c_read(char **wp)
}
break;
}
if (history) {
if (savehist) {
Xcheck(xs, xp);
Xput(xs, xp, c);
}
Expand All @@ -362,7 +362,7 @@ c_read(char **wp)
/* set prompt in case this is
* called from .profile or $ENV
*/
set_prompt(PS2, NULL);
set_prompt(PS2);
pprompt(prompt, 0);
}
} else if (c != EOF)
Expand Down Expand Up @@ -405,7 +405,7 @@ c_read(char **wp)
}

shf_flush(shf);
if (history) {
if (savehist) {
Xput(xs, xp, '\0');
source->line++;
histsave(source->line, Xstring(xs, xp), 1);
Expand Down
Loading

0 comments on commit e52701b

Please sign in to comment.