diff --git a/NEWS b/NEWS index e56a66c0a..6718ede09 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-11-18: + +- The printf built-in command now supports a -v option as on bash and zsh. + This allows you to assign formatted output directly to a variable. + 2021-11-16: - By default, arithmetic expressions in ksh no longer interpret a number diff --git a/src/cmd/ksh93/bltins/print.c b/src/cmd/ksh93/bltins/print.c index 11f240174..1360dff71 100644 --- a/src/cmd/ksh93/bltins/print.c +++ b/src/cmd/ksh93/bltins/print.c @@ -1,7 +1,7 @@ /*********************************************************************** * * * This software is part of the ast package * -* Copyright (c) 1982-2012 AT&T Intellectual Property * +* Copyright (c) 1982-2014 AT&T Intellectual Property * * Copyright (c) 2020-2021 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 1.0 * @@ -176,6 +176,7 @@ int b_print(int argc, char *argv[], Shbltin_t *context) const char *options, *msg = e_file+4; char *format = 0; int sflag = 0, nflag=0, rflag=0, vflag=0; + Namval_t *vname=0; Optdisc_t disc; exitval = 0; disc.version = OPT_VERSION; @@ -236,11 +237,21 @@ int b_print(int argc, char *argv[], Shbltin_t *context) else if(!sh_iovalidfd(shp,fd)) fd = -1; else if(!(shp->inuse_bits&(1<gd->hist_ptr && fd==sffileno(shp->gd->hist_ptr->histfp)))) - fd = -1; break; case 'v': - vflag='v'; + if(argc < 0) + { + /* prepare variable for printf -v varname */ + vname = nv_open(opt_info.arg, shp->var_tree, NV_VARNAME); + if(!vname) + { + errormsg(SH_DICT, ERROR_exit(2), e_create, opt_info.arg); + UNREACHABLE(); + } + } + else + vflag='v'; break; case 'C': vflag='C'; @@ -288,6 +299,13 @@ skip: /* handle special case of '-' operand for print */ if(argc>0 && *argv && strcmp(*argv,"-")==0 && strcmp(argv[-1],"--")) argv++; + if(vname) + { + if(!shp->strbuf2) + shp->strbuf2 = sfstropen(); + outfile = shp->strbuf2; + goto printf_v; + } skip2: if(fd < 0) { @@ -314,6 +332,7 @@ skip2: } /* turn off share to guarantee atomic writes for printf */ n = sfset(outfile,SF_SHARE|SF_PUBLIC,0); +printf_v: if(format) { /* printf style print */ @@ -363,7 +382,9 @@ skip2: if(sfputc(outfile,'\n') < 0) exitval = 1; } - if(sflag) + if(vname) + nv_putval(vname, sfstruse(outfile), 0); + else if(sflag) { hist_flush(shp->gd->hist_ptr); sh_offstate(SH_HISTORY); diff --git a/src/cmd/ksh93/data/builtins.c b/src/cmd/ksh93/data/builtins.c index 4aafd6e3e..9e245697d 100644 --- a/src/cmd/ksh93/data/builtins.c +++ b/src/cmd/ksh93/data/builtins.c @@ -1193,7 +1193,7 @@ const char sh_optprint[] = ; const char sh_optprintf[] = -"[-1c?\n@(#)$Id: printf (ksh 93u+m) 2021-09-13 $\n]" +"[-1c?\n@(#)$Id: printf (ksh 93u+m) 2021-11-18 $\n]" "[--catalog?" SH_DICT "]" "[+NAME?printf - write formatted output]" "[+DESCRIPTION?\bprintf\b writes each \astring\a operand to " @@ -1358,6 +1358,9 @@ const char sh_optprintf[] = "time conversions will be treated as if \bnow\b were supplied.]" "[+?\bprintf\b is equivalent to \bprint -f\b which allows additional " "options to be specified.]" +"[v]:[name?Put the output in the variable \aname\a instead of writing to " + "standard output. \aname\a may include an array subscript (note that " + "the square brackets should be quoted to avoid pathname expansion).]" "\n" "\nformat [string ...]\n" "\n" diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index d2209c3ed..6f0e909a5 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-11-16" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-11-18" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index b3b51f390..03c6fe555 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -1298,5 +1298,18 @@ got="$($SHELL -i "$hist_error_leak" 2>&1)" [[ $exp == "$got" ]] || err_exit "file descriptor leak after substitution error in hist builtin" \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +# ====== +# printf -v works as of 2021-11-18 +integer ver=.sh.version +exp=ok$'\f'0000$ver$'\n' +printf -v got 'ok\f%012d\n' $ver 2>/dev/null +[[ $got == "$exp" ]] || err_exit "printf -v not working" \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +unset got +printf -v 'got[1][two][3]' 'ok\f%012d\n' $ver 2>/dev/null +[[ ${got[1]["two"][3]} == "$exp" ]] || err_exit "printf -v not working with array subscripts" \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +unset got ver + # ====== exit $((Errors<125?Errors:125))