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