terminal keeps screwing up when coding

Red Squirrel

No Lifer
May 24, 2003
70,669
13,835
126
www.anyf.ca
At random when I'm coding and I exit my program that I'm working on, the terminal totally screws up. When I hit enter instead of actually doing a return it just repeats the prompt on the same line. If I try to type anything it does not show up, but does register since if I type a command it takes it.

This is VERY irritating as I have to keep rebooting my server. Is there a way to stop this? A program should not affect the terminal permanently after it's been exited so this is a serious bug in bash.
 

Kakumba

Senior member
Mar 13, 2006
610
0
0
can you give an example of what causes this? bash is generally pretty stable, so for you to have found a whole new case which causes problems is unlikely, though I guess it is possible.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
It could be that your program is printing some nasty control characters to the terminal -- that happens to me sometimes when I have an overrun on a printf() or something. The only way I know to fix it is to use a new terminal -- but you say it requires a reboot to fix...?
 

crontab

Member
Dec 20, 2000
160
0
0
what editor are you using? is the executable you're running at the command line? Is it outputting unfriendly characters?

usually typing in "reset" or "stty echo" will fix the terminal problems you're experiencing.
 

Red Squirrel

No Lifer
May 24, 2003
70,669
13,835
126
www.anyf.ca
It seems to happen when I ctrl+c out of my app. The app was to test multithreading and was just outputting a bunch of 0's and 1's and braces. Reset would not work either, but I did not try the stty echo so I'll try that next time it happens.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
A program should not affect the terminal permanently after it's been exited so this is a serious bug in bash.

It has nothing to do with bash, it's the terminal that's got the problem and even then it's not really a problem with the terminal. AFAIK nothing guarantees that any terminal will keep and restore state across process invocations.

If reset doesn't fix it the only solution I know of is to open a new terminal.
 

Red Squirrel

No Lifer
May 24, 2003
70,669
13,835
126
www.anyf.ca
It happened again. stty echo seems to have fixed it. I'll be sure to save that one as it may be handy!

Seems to happen when I ctrl+c out of a "press any key to continue"
 

Red Squirrel

No Lifer
May 24, 2003
70,669
13,835
126
www.anyf.ca
Found the issue. It was if I CTRL+C out of this code:

unsigned int GetChar()
{
#ifdef WIN32
return getch();
#endif
#ifdef linux
int ch;
struct termios oldt, newt;
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
return ch;
#endif
}



If I remove the ECHO flag it seems to fix it. Thought I'd post this if ever someone runs into the same issue.

Only problem is this makes the program echo keys...
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
If you're going to use that for "Press any key to continue" you better put a signal handler in for SIGINT to restore the state of the terminal.

Alternatively you could just get rid of all that crazy tcsetattr stuff and just do getchar ;)