1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Backport 'printf -v' from ksh 93v-

'printf' on bash and zsh has a popular -v option that allows
assigning formatted output directly to variables without using a
command substitution. This is much faster and avoids snags with
stripping final linefeeds. AT&T had replicated this feature in the
abandoned 93v- beta version. This backports it with a few tweaks
and one user-visible improvement.

The 93v- version prohibited specifying a variable name with an
array subscript, such as printf -v var\[3\] foo. This works fine on
bash and zsh, so I see no reason why this should not work on ksh,
as nv_putval() deals with array subscripts just fine.

src/cmd/ksh93/bltins/print.c: b_print():
- While processing the -v option when called as printf, get a
  pointer to the variable, creating it if necessary. Pass only the
  NV_VARNAME flag to enforce a valid variable name, and not (as
  93v- does) the NV_NOARRAY flag to prohibit array subscripts.
- If a variable was given, set the output file to an internal
  string buffer and jump straight to processing the format.
- After processing the format, assign the contents to the string
  buffer to the variable.

src/cmd/ksh93/data/builtins.c:
- Document the new option, adding a warning that unquoted square
  brackets may trigger pathname expansion.
This commit is contained in:
Martijn Dekker 2021-11-18 23:36:28 +01:00
parent fb8308243c
commit bd9752e43c
5 changed files with 48 additions and 6 deletions

View file

@ -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<<fd)) && (sh_inuse(shp,fd) || (shp->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);

View file

@ -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"

View file

@ -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. */

View file

@ -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))