I was trying to implement this great response to my question about getting the terminal size with ANSI escape sequences. It didn't work, so I tried to see what the differences between the proposed code and mine were. I don't know if it is the main problem, but I followed the breadcrumbs to the one obvious differences (which I have also been able to replicate in a minimal example) - I use VMIN = 0, and the solution uses VMIN = 1.
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>
#define SIZE 100
int main ( void) {
int ch = 0;
int i = 0;
struct termios original, changed;
// change terminal settings
tcgetattr( STDIN_FILENO, &original);
changed = original;
changed.c_lflag &= ~( ICANON | ECHO);
changed.c_cc[VMIN] = 1;
changed.c_cc[VTIME] = 0;
tcsetattr( STDIN_FILENO, TCSANOW, &changed);
printf ( "\033[9999;9999H"); // cursor should move as far as it can
printf ( "\033[6n"); // ask for cursor position
printf ( "\033[2J"); //clear screen
printf ( "\033[1;1H"); // move to upper left corner
while ( ( ch = getchar ()) != 'R') { // R terminates the response
if ( EOF == ch)
break;
if ( isprint ( ch)) // print out only normal chars to not mess up display
printf("stdin[%d]\t==\t%d\t==\t%c\n", i, ch, ch);
else
printf("stdin[%d]\t==\t%d\t==\t\n", i, ch);
i++;
}
// restore terminal settings
tcsetattr( STDIN_FILENO, TCSANOW, &original);
return 0;
}
Here is a slightly shortened version of the proposed solution which showcases the problem. If you keep VMIN at 1, everything will work fine. However, if you set it to 0, you will lose the first part of ESC[rows;colsR
, and it will only get printed out after the program finishes.
My actual code is too big and fragmented to post here, but what I am experiencing is a total freeze of the program if I set VMIN to 1 (I am read()-ing STDIN(1) in an infinite loop), and nothing happens when I run \033[6n
(as if stdin is empty - I can get nothing out with getchar nor fread nor read)
If you have any info about this peculiarity, please share.
Thank you.