Subject: Re: bug#37564: [PATCH] don't export LINES and COLUMNS env vars in term to fix ncurses applications

From: Matthew Leach <[email-masked]>
Date: Tue, 01 Oct 2019 16:47:35 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Matthew Leach <[email-masked]>
>> Date: Mon, 30 Sep 2019 20:59:04 +0100
>> 
>> The attached patch removes the exporting of the LINES and COLUMNS
>> environment variables in term-mode.  Exporting these variables causes
>> issues for ncurses applications.  For example, when running:
>> 
>> emacs -Q
>> M-x term
>> <ret> (to select /bin/bash)
>> htop (to run htop)
>> 
>> and resizing the window (especially making it smaller) can make the
>> program impossible to read.
>
> Thanks, but I don't think we can make this change unconditionally,
> because not all applications that heed LINES and COLUMNS use ncurses.

I'm curious as to which programs you are referring? AFAIK, if a program
tries to read the LINES and COLUMNS environment variables, using
`getenv()', they don't exist. Running 'echo $LINES' on a bash terminal
seems to actually do an ioctl to obtain the value of LINES.

Nevertheless, if a program does read the LINES and COLUMNS variables,
these values will be wrong if the window has been resized (try and
compile the attached C snippet and run in term mode while resizing the
window). Should that be considered as a separate bug?

> I wonder if we can do better than just providing a defcustom.

I could make this a defcustom if that would get the patch in?

Thanks,
-- 
Matt
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/ioctl.h>

void handle_sig()
{
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    printf("getenv: %s %s\n", getenv("LINES"), getenv("COLUMNS"));
    printf("ioctl:  %d %d\n", w.ws_row, w.ws_col);
}

int main()
{
    signal(SIGWINCH, handle_sig);
    fgetc(stdin);
    return 0;
}