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 20:24:14 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Matthew Leach <[email-masked]>
>> Cc: [email-masked]
>> Date: Tue, 01 Oct 2019 19:33:39 +0100
>> 
>> >> 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.
>> >
>> > What makes you say that? 
>> 
>> If I run the C program in my previous post in an xterm and resize I get:
>> 
>> getenv: (null) (null)
>> ioctl:  24 80
>> getenv: (null) (null)
>> ioctl:  42 169
>> 
>> You can see that doing getenv() on LINES and COLUMNS returns a null
>> pointer.  I've tested this same behaviour on gnome-terminal and numerous
>> other terminal emulators.
>
> I simply tried "env | fgrep LINES" and I do see the variable, although
> it wasn't there before I launched Emacs.

I would expect the variable to be there when inside Emacs as it has
exported it.  You should find that it won't exist if ran outside Emacs,
for example in xterm.

> 'env' is just another program accessing the environment, right? I have
> no idea why your test program doesn't see that, but then I'm far from
> being an expert on these issues.

Indeed, it does see the variables if I run inside Emacs.  If I run it
outside then they are not there, on my machine anyway.

>> > Emacs exports these variables into the environment that is passed to
>> > child subprocesses, so those subprocesses will definitely see them
>> > using getenv.
>> 
>> I'm no expert on how these things work but it doesn't appear to be the
>> way that it's done, on my machine anyway.  Perhaps there are programs
>> that uses these variables on MacOS or Windows?
>
> I tried the above on a Trisquel GNU/Windows machine, FWIW.

I'm running on Arch GNU/Linux this end.

>> I'm testing this on the latest Emacs Git version. I can see that
>> window-adjust-process-window-size-function is used in term.el however,
>> this doesn't appear to update the environment variables exported to the
>> terminal process.
>
> AFAIR, it's supposed to send the corresponding ioctl command, in
> addition to setting LINES and COLUMNS.
>
>> If I run the same program with term on Emacs git and
>> resize I get:
>> 
>> getenv: 31 80
>> ioctl:  30 89
>> getenv: 31 80
>> ioctl:  30 90
>> getenv: 31 80
>> ioctl:  30 91
>> getenv: 31 80
>> ioctl:  31 92
>> getenv: 31 80
>> ioctl:  31 93
>> getenv: 31 80
>> ioctl:  31 94
>> ...
>> 
>> Notice that the values returned by getenv don't change.
>
> Again, the "env | fgrep LINES" method does show the change when I
> resize the window on the machine I tried this, so I'm unsure why you
> don't see it.

Ah, indeed if I try and resize and print the variable it has updated:

matthew@hopton ~ $ env | grep -i lines
LINES=30
matthew@hopton ~ $ env | grep -i lines
LINES=12

I think the issue is that the environment can't be while a process is
running; the environment variables are fixed when the program has
started. For a ncurses application this presents a problem as LINES and
COLUMNS can't be updated when the window size is changed and the program
handles a SIGWINCH. You can see that on the output above.

>> Given that this functionality is broken, I'm suggesting that we don't
>> export the variables.
>
> I don't agree that it's broken; it isn't for me.  I think someone who
> understands more than I do about this stuff should chime in and
> explain why we see such different results.

I think the above explains what's going on. Perhaps that's why terminal
emulators don't export LINES and COLUMNS to programs so an ioctl() is used
instead?
-- 
Matt