Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set $LINES and $COLUMNS #459

Open
jyn514 opened this issue Aug 14, 2019 · 6 comments
Open

Set $LINES and $COLUMNS #459

jyn514 opened this issue Aug 14, 2019 · 6 comments
Labels

Comments

@jyn514
Copy link
Contributor

jyn514 commented Aug 14, 2019

These are used by shell scripts to adapt their output to the terminal. Bash seems to implement these with ioctl directly instead of storing them as variables, see the following strace:

ioctl(0, TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, TCGETS, {B38400 opost isig icanon echo ...}) = 0
write(1, "49\n", 3) 
...
fcntl(0, F_GETFL)                       = 0x2 (flags O_RDWR)
ioctl(0, TIOCGWINSZ, {ws_row=49, ws_col=93, ws_xpixel=0, ws_ypixel=0}) = 0
ioctl(0, TIOCSWINSZ, {ws_row=49, ws_col=93, ws_xpixel=0, ws_ypixel=0}) = 0

Note that these variables are not required by POSIX.

@andychu andychu changed the title OSH does not set LINES and COLUMNS Set $LINES and $COLUMNS Aug 14, 2019
@andychu
Copy link
Contributor

andychu commented Aug 14, 2019

Yeah good idea, I think readline does this, but the Python readline binding we forked doesn't (since Python never did it). Want to try flipping this flag in native/line_input.c and see what happens?

https://tiswww.case.edu/php/chet/readline/readline.html


Variable: int rl_change_environment
    If this variable is set to a non-zero value, and Readline is handling SIGWINCH, Readline will modify the LINES and COLUMNS environment variables upon receipt of a SIGWINCH

    The default value of rl_change_environment is 1. 

@andychu
Copy link
Contributor

andychu commented Aug 14, 2019

Oh wait that might not work... we are handling SIGWINCH ourselves :-/ I guess we can do it in Python code.

I also filed #460 -- building with other libs would clarify the interface -- i.e. who handles SIGWINCH. Although I guess I'm not opposed to making $LINES and $COLUMNS work only if you're building against GNU readline.

@andychu
Copy link
Contributor

andychu commented Aug 14, 2019

Actually looking at the source, bash does this itself in variables.c, at least in POSIX mode.

It also lets the user overwrite LINES and COLUMNS which as I recently learned from $PWD is fraught with complication ...

/* Set the environment variables $LINES and $COLUMNS in response to                                                                                                                                                                           
   a window size change. */                                                                                                                                                                                                                   
void                                                                                                                                                                                                                                          
sh_set_lines_and_columns (lines, cols)          
#if defined (READLINE) && defined (STRICT_POSIX)                                                                                                                                                                                              
  { "LINES", sv_winsize },                                                                                                                                                                                                                    
#endif   

The flow is pretty messy:

lib/readline/shell.c:sh_set_lines_and_columns (int lines, int cols)
lib/readline/terminal.c:    sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
lib/sh/winsize.c:extern void sh_set_lines_and_columns __P((int, int));
lib/sh/winsize.c:      sh_set_lines_and_columns (win.ws_row, win.ws_col);

@andychu
Copy link
Contributor

andychu commented Aug 14, 2019

I am not sure I have cycles now to look at this, but if it will let us put some interesting / important program here I could reprioritize it. Which scripts are using it?

https://github.com/oilshell/oil/wiki/Shell-Programs-That-Run-Under-OSH

@jyn514
Copy link
Contributor Author

jyn514 commented Aug 14, 2019

I haven't found any programs using this in the wild. I came across this because I thought a program was using $COLUMNS but it had just hard-coded the width to 100 columns.

@andychu
Copy link
Contributor

andychu commented Jan 22, 2022

Hm GNU readline also sets this, but I said bash does above? As usual it seems like there is a lot of fighting over globals ...

https://tiswww.case.edu/php/chet/readline/readline.html

Variable: int rl_change_environment
    If this variable is set to a non-zero value, and Readline is handling SIGWINCH, Readline will modify the LINES and COLUMNS environment variables upon receipt of a SIGWINCH

    The default value of rl_change_environment is 1. 

I tested OSH and it doesn't have LINES, so are we overriding the default?

Or maybe the problem is that the environment variable is set, but it has to be copied into the shell's memory on SIGWINCH! doh yeah I think that's it.

bash does this:

andy@lenny:~$ echo $COLUMNS
118
andy@lenny:~$ echo $COLUMNS
143

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants