Skip to content

Use the curses API rather than depend on the tput command #1460

Open
@krader1961

Description

@krader1961

Issue #1459 caused me to notice that this block of code is, at best, confusing:

// Avoid an error from the 2>/dev/null redirection in a restricted shell.
bool r = sh_isoption(shp, SH_RESTRICTED);
if (r) sh_offoption(shp, SH_RESTRICTED);
sh_trap(shp, ".sh.subscript=$(tput cuu1 2>/dev/null)", 0);
if (r) sh_onoption(shp, SH_RESTRICTED);
pp = nv_getval(VAR_sh_subscript);
if (pp) {
// It should be impossible for the cursor up string to be truncated.
if (strlcpy(CURSOR_UP, pp, sizeof(CURSOR_UP)) >= sizeof(CURSOR_UP)) abort();
}
nv_unset(VAR_sh_subscript);

The problem is that it is assigning to shell var .sh.subscript a value having absolutely nothing to do with the documented meaning of that var. The code could have used literally any arbitrary, otherwise ostensibly private, var to temporarily hold the value returned by tput cuu1. Abusing .sh.subscript is gross and unnecessary. It's also dangerous since, theoretically, it could have been set prior to that block of code being run. Which, again, illustrates that the old AST team relied far too much on knowledge of the existing implementation rather than first principals when writing such code.

We should modify ksh to not use the tput command in this fashion. Ksh should be using the curses API to get this info rather than running an external command. Note that there are only two places in the code that use an external tput command in this fashion. The other being in emacs_escape() to run tput clear when [ctrl-L] is seen. Which means that if a tput command wasn't found when the build was configured you can't use [ctrl-L] to clear the screen. Which seems suboptimal and needlessly expensive. Not to mention that even if tput was found when the build was configured there is no guarantee it will be in $PATH when it is needed. Which explains the 2>/dev/null redirection in the ed_setup() function. But not why the same redirection isn't present in the emacs_escape() function.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions