1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2026-01-06 09:41:07 +01:00

18242: add -d option to read for specifying delimiter to terminate input

This commit is contained in:
Oliver Kiddle 2003-02-17 10:07:53 +00:00
parent 06d25a35b0
commit 5164c6a3c9
4 changed files with 42 additions and 12 deletions

View file

@ -1,3 +1,9 @@
2003-02-17 Oliver Kiddle <opk@zsh.org>
* 18242: Src/builtin.c, Completion/Zsh/Command/_read,
Doc/Zsh/builtins.yo: add -d option to read for specifying
delimiter to terminate input instead of newline
2003-02-14 Peter Stephenson <pws@csr.com>
* 18243: Src/Zle/zle_main.c: On Solaris, poll the tty for

View file

@ -11,6 +11,7 @@ _arguments -s -A "-*" -S \
'(-p -k -s -u -z)-q[read y or n character from terminal]' \
'(-q)-k+[specify number of characters to read]:: :_guard "[0-9]#" "number of characters"' \
'(-q -z)-t+[test if input is available before reading]:: :_guard "[0-9.]#" "timeout (seconds)"' \
'(-q)-d[specify delimiter to terminate input instead of newline]:delimiter' \
'(-q -s -u -p -t)-z[read entry from editor buffer stack]' \
'(-E)-e[input read is echoed and not assigned]' \
'(-e)-E[input read is echoed]' \

View file

@ -824,8 +824,8 @@ contain symbolic links.
alias(r)(fc -e -)
findex(read)
vindex(IFS, use of)
ifzman(xitem(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [ tt(-k) [ var(num) ] ]))
item(ifnzman(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [ tt(-k) [ var(num) ] ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ] [ var(name) ... ])(
ifzman(xitem(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [ tt(-k) [ var(num) ] ] [ tt(-d) var(delim) ]))
item(ifnzman(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [ tt(-k) [ var(num) ] ] [ tt(-d) var(delim) ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ] [ var(name) ... ])(
vindex(REPLY, use of)
vindex(reply, use of)
Read one line and break it into fields using the characters
@ -899,6 +899,10 @@ Input is read from file descriptor var(n).
item(tt(-p))(
Input is read from the coprocess.
)
item(tt(-d) var(delim))(
Input is terminated by the first character of var(delim) instead of
by newline.
)
item(tt(-t) [ var(num) ])(
Test if input is available before attempting to read. If var(num)
is present, it must begin with a digit and will be evaluated

View file

@ -106,7 +106,7 @@ static struct builtin builtins[] =
BUILTIN("pushln", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "nrl", NULL),
BUILTIN("read", 0, bin_read, 0, -1, 0, "cek:%lnpqrst:%zu:AE", NULL),
BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE", NULL),
BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
@ -4120,9 +4120,9 @@ bin_read(char *name, char **args, Options ops, int func)
int readchar = -1, val, resettty = 0;
struct ttyinfo saveti;
char d;
char delim = '\n';
if ((OPT_HASARG(ops,c='k') || OPT_HASARG(ops,c='b'))) {
if (OPT_HASARG(ops,c='k')) {
char *eptr, *optarg = OPT_ARG(ops,c);
nchars = (int)zstrtol(optarg, &eptr, 10);
if (*eptr) {
@ -4223,17 +4223,36 @@ bin_read(char *name, char **args, Options ops, int func)
return 1;
}
}
if (OPT_ISSET(ops,'d')) {
delim = *OPT_ARG(ops,'d');
if (SHTTY != -1) {
struct ttyinfo ti;
gettyinfo(&ti);
saveti = ti;
resettty = 1;
#ifdef HAS_TIO
ti.tio.c_lflag &= ~ICANON;
ti.tio.c_cc[VMIN] = 1;
ti.tio.c_cc[VTIME] = 0;
#else
ti.sgttyb.sg_flags |= CBREAK;
#endif
settyinfo(&ti);
}
}
if (OPT_ISSET(ops,'s') && SHTTY != -1) {
struct ttyinfo ti;
gettyinfo(&ti);
saveti = ti;
if (! resettty) {
saveti = ti;
resettty = 1;
}
#ifdef HAS_TIO
ti.tio.c_lflag &= ~ECHO;
#else
ti.sgttyb.sg_flags &= ~ECHO;
#endif
settyinfo(&ti);
resettty = 1;
}
/* handle prompt */
@ -4352,11 +4371,11 @@ bin_read(char *name, char **args, Options ops, int func)
c = zread(izle, &readchar);
/* \ at the end of a line indicates a continuation *
* line, except in raw mode (-r option) */
if (bslash && c == '\n') {
if (bslash && c == delim) {
bslash = 0;
continue;
}
if (c == EOF || c == '\n')
if (c == EOF || c == delim)
break;
/*
* `first' is non-zero if any separator we encounter is a
@ -4393,7 +4412,7 @@ bin_read(char *name, char **args, Options ops, int func)
}
}
signal_setmask(s);
if (c == '\n' || c == EOF)
if (c == delim || c == EOF)
gotnl = 1;
*bptr = '\0';
/* dispose of word appropriately */
@ -4455,11 +4474,11 @@ bin_read(char *name, char **args, Options ops, int func)
c = zread(izle, &readchar);
/* \ at the end of a line introduces a continuation line, except in
raw mode (-r option) */
if (bslash && c == '\n') {
if (bslash && c == delim) {
bslash = 0;
continue;
}
if (c == EOF || (c == '\n' && !zbuf))
if (c == EOF || (c == delim && !zbuf))
break;
if (!bslash && isep(c) && bptr == buf) {
if (iwsep(c))