From ef1621c18f38e57a32ab6aef8c85cc2bc632b9b6 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 15 Jun 2020 09:03:44 +0200 Subject: [PATCH] Make 'source' a regular built-in The 'source' alias is now converted into a regular built-in command so that 'unalias -a' does not remove it, and something like cmd=source; $cmd name args will now work. This is part of the project to replace default aliases that define essential commands by proper builtins that act identically (except you now get the actual command's name in any error/usage messages). src/cmd/ksh93/data/aliases.c: - Remove 'source' default alias. src/cmd/ksh93/data/builtins.c, src/cmd/ksh93/include/builtins.h: - Define 'source' regular builtin with extra parser ID "SYSSOURCE". Same definition as '.', minus the BLT_SPC flag indicating a special builtin. This preserves the behaviour of 'command .'. - Update sh_optdot[] to include info for 'source --man'. (Note that \f?\f expands to the current command name. This allows several commands to share a single --man page.) src/cmd/ksh93/sh/parse.c: - In the two places that SYSDOT is checked for, also check for SYSSOURCE, making sure the two commands are parsed identically. src/cmd/ksh93/sh.1: - Remove 'source' default alias. - Document 'source' regular builtin. --- NEWS | 5 +++++ TODO | 7 ++++--- src/cmd/ksh93/data/aliases.c | 1 - src/cmd/ksh93/data/builtins.c | 21 +++++++++++---------- src/cmd/ksh93/include/builtins.h | 5 +++-- src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh.1 | 7 +++++-- src/cmd/ksh93/sh/parse.c | 11 +++++++++-- 8 files changed, 38 insertions(+), 21 deletions(-) diff --git a/NEWS b/NEWS index bfedb1360..49be082ca 100644 --- a/NEWS +++ b/NEWS @@ -3,7 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-06-15: + +- The 'source' alias has been converted into a regular built-in command. + 2020-06-14: + - 'read -S' is now able to correctly handle strings with double quotes nested inside of double quotes. diff --git a/TODO b/TODO index cd5da9df8..c89ff07fc 100644 --- a/TODO +++ b/TODO @@ -32,12 +32,13 @@ Fix or remove broken or misguided default aliases: - functions='typeset -f' - integer='typeset -li' - nameref='typeset -n' - - source='command .' + - stop='kill -s STOP' + - suspend='kill -s STOP $$' Keep these default aliases for the benefit of interactive shells: + history='hist -l' + r='hist -s' - + stop='kill -s STOP' - + suspend='kill -s STOP $$' + To avoid interfering with shell functions by those names that POSIX + scripts may set, those should only intialise on interactive shells. ______ Fix currently known bugs affecting shell scripting. These are identified by diff --git a/src/cmd/ksh93/data/aliases.c b/src/cmd/ksh93/data/aliases.c index 387e5ef24..8fedfc5a3 100644 --- a/src/cmd/ksh93/data/aliases.c +++ b/src/cmd/ksh93/data/aliases.c @@ -37,7 +37,6 @@ const struct shtable2 shtab_aliases[] = "integer", NV_NOFREE|BLT_DCL, "typeset -li", "nameref", NV_NOFREE|BLT_DCL, "typeset -n", "r", NV_NOFREE, "hist -s", - "source", NV_NOFREE, "command .", #ifdef SIGTSTP "stop", NV_NOFREE, "kill -s STOP", "suspend", NV_NOFREE, "kill -s STOP $$", diff --git a/src/cmd/ksh93/data/builtins.c b/src/cmd/ksh93/data/builtins.c index f7b5ff49a..533d1a005 100644 --- a/src/cmd/ksh93/data/builtins.c +++ b/src/cmd/ksh93/data/builtins.c @@ -73,6 +73,7 @@ const struct shtable3 shtab_builtins[] = "let", NV_BLTIN|BLT_ENV, bltin(let), "export", NV_BLTIN|BLT_ENV|BLT_SPC|BLT_DCL,bltin(readonly), ".", NV_BLTIN|BLT_ENV|BLT_SPC, bltin(dot_cmd), + "source", NV_BLTIN|BLT_ENV, bltin(dot_cmd), "return", NV_BLTIN|BLT_ENV|BLT_SPC, bltin(return), #if SHOPT_BASH "local", NV_BLTIN|BLT_ENV|BLT_SPC|BLT_DCL,bltin(typeset), @@ -526,11 +527,12 @@ USAGE_LICENSE ; const char sh_optdot[] = -"[-1c?@(#)$Id: \b.\b (AT&T Research) 2000-04-02 $\n]" +"[-1c?@(#)$Id: \b.\b (AT&T Research/ksh93) 2020-06-15 $\n]" USAGE_LICENSE -"[+NAME?\b.\b - execute commands in the current environment]" -"[+DESCRIPTION?\b.\b is a special built-in command that executes commands " - "from a function or a file in the current environment.]" +"[+NAME?\f?\f - execute commands in the current environment]" +"[+DESCRIPTION?\b.\b and \bsource\b are built-in commands that execute " + "commands from a function or a file in the current environment. \b.\b " + "is a special built-in, whereas \bsource\b is a regular built-in.]" "[+?If \aname\a refers to a function defined with the \bfunction\b \aname\a " "syntax, the function executes in the current environment as " "if it had been defined with the \aname\a\b()\b syntax so that " @@ -548,12 +550,11 @@ USAGE_LICENSE "\n" "\n name [arg ...]\n" "\n" -"[+EXIT STATUS?If \aname\a is found, then the exit status is that " - "of the last command executed. Otherwise, since this is a special " - "built-in, an error will cause a non-interactive shell to exit with " - "a non-zero exit status. An interactive shell returns a non-zero exit " - "status to indicate an error.]" - +"[+EXIT STATUS?If \aname\a is found, then the exit status is that of the last " + "command executed. Otherwise, it is non-zero. \b.\b, being a special " + "built-in, will exit the current shell environment or abort execution " + "of the interactive command line upon error, whereas \bsource\b will " + "allow execution to continue.]" "[+SEE ALSO?\bcommand\b(1), \bksh\b(1)]" ; diff --git a/src/cmd/ksh93/include/builtins.h b/src/cmd/ksh93/include/builtins.h index b17e644aa..61fe247f9 100644 --- a/src/cmd/ksh93/include/builtins.h +++ b/src/cmd/ksh93/include/builtins.h @@ -47,9 +47,10 @@ #define SYSLET (shgd->bltin_cmds+12) /* let */ #define SYSEXPORT (shgd->bltin_cmds+13) /* export */ #define SYSDOT (shgd->bltin_cmds+14) /* . */ -#define SYSRETURN (shgd->bltin_cmds+15) /* return */ +#define SYSSOURCE (shgd->bltin_cmds+15) /* source */ +#define SYSRETURN (shgd->bltin_cmds+16) /* return */ #if SHOPT_BASH -# define SYSLOCAL (shgd->bltin_cmds+16) /* local */ +# define SYSLOCAL (shgd->bltin_cmds+17) /* local */ #else # define SYSLOCAL 0 #endif diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index b0f3b9a70..86e6c8199 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -17,4 +17,4 @@ * David Korn * * * ***********************************************************************/ -#define SH_RELEASE "93u+m 2020-06-14" +#define SH_RELEASE "93u+m 2020-06-15" diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1 index e1bbd9105..261ba8239 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -804,8 +804,6 @@ but can be unset or redefined: .TP .B "r=\(fmhist \-s\(fm" .TP -.B "source=\(fmcommand \s+2.\s-2\(fm" -.TP .B "stop=\(fmkill \-s \s-1STOP\s+1\(fm" .TP .B "suspend=\(fmkill \-s \s-1STOP\s+1 $$\(fm" @@ -7125,6 +7123,11 @@ Suspends execution for the number of decimal seconds or fractions of a second given by .IR seconds . .TP +\f3source\fP \f2name\^\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK +Same as +.BR \|.\^ , +except it is not treated as a special built-in command. +.TP \f3times\fP Displays the accumulated user and system CPU times, one line with the times used by the shell and another with those used by all of the shell's child diff --git a/src/cmd/ksh93/sh/parse.c b/src/cmd/ksh93/sh/parse.c index e7222c0f9..223a89637 100644 --- a/src/cmd/ksh93/sh/parse.c +++ b/src/cmd/ksh93/sh/parse.c @@ -1023,7 +1023,11 @@ static struct argnod *assign(Lex_t *lexp, register struct argnod *ap, int type) } else if(n && n!=FUNCTSYM) sh_syntax(lexp); - else if(type!=NV_ARRAY && n!=FUNCTSYM && !(lexp->arg->argflag&ARG_ASSIGN) && !((np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && (nv_isattr(np,BLT_DCL)|| np==SYSDOT))) + else if(type!=NV_ARRAY && + n!=FUNCTSYM && + !(lexp->arg->argflag&ARG_ASSIGN) && + !((np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && + (nv_isattr(np,BLT_DCL) || np==SYSDOT || np==SYSSOURCE))) { array=SH_ARRAY; if(fcgetc(n)==LPAREN) @@ -1078,7 +1082,10 @@ static struct argnod *assign(Lex_t *lexp, register struct argnod *ap, int type) if(array || n!=FUNCTSYM) sh_syntax(lexp); } - if((n!=FUNCTSYM) && !(lexp->arg->argflag&ARG_ASSIGN) && !((np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && (nv_isattr(np,BLT_DCL)||np==SYSDOT))) + if((n!=FUNCTSYM) && + !(lexp->arg->argflag&ARG_ASSIGN) && + !((np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && + (nv_isattr(np,BLT_DCL) || np==SYSDOT || np==SYSSOURCE))) { struct argnod *arg = lexp->arg; if(n!=0)