Clean up ncurses mess in terminal after a crash

后端 未结 5 1402
南方客
南方客 2020-12-14 03:59

I am drawing a TUI using ncurses. The trouble is that whenever my program gets seg-fault, my terminal is left in mess. I can not see what I am typing. Its a pain since I am

相关标签:
5条回答
  • 2020-12-14 04:11

    Command,

    stty sane^J
    

    did the job.

    UPDATE: On some terminals stty sane also works.

    0 讨论(0)
  • 2020-12-14 04:14

    The command

    reset
    

    also worked for me on Ubuntu, probably overkill though. What worked best was setting an alias like:

    alias 'clean'='stty sane;clear;'
    

    in my .bash_aliases as I found myself needing to do this alot in debugging.

    0 讨论(0)
  • 2020-12-14 04:19

    I had this problem recently on a Mac OSX terminal. Following set of commands worked whereas stty sane did not.

    stty discard '^O'
    stty dsusp '^Y'
    stty eof '^D'
    stty eol '^@'
    stty eol2 '^@'
    stty erase '^?'
    stty intr '^C'
    stty kill '^U'
    stty lnext '^V'
    stty min 1
    stty quit '^\'
    stty reprint '^R'
    stty start '^Q'
    stty status '^T'
    stty stop '^S'
    stty susp '^Z'
    stty time 0
    stty werase '^W'
    
    0 讨论(0)
  • 2020-12-14 04:22

    ncurses (any curses implementation) sets the terminal modes to raw and noecho while running, and allows applications to simulate these using the raw and noraw, echo and noecho functions. It does this for performance, to avoid waiting when switching between these modes.

    When an application calls endwin, ncurses restores the terminal modes. It can also do this for reset_shell_mode, though endwin is used far more often.

    If your application crashes, or exits without restoring the terminal modes using endwin, the most obvious problem is that you cannot see what you are typing, and that pressing enter does not work.

    ncurses provides a signal handler to catch the user-initiated signals SIGINT, SIGTERM, and will cleanup when those are caught. It does not try to catch SIGSEGV because at that point, your application is dead and trying to resurrect it to repair things is counter productive.

    Some people might advise using stty sane to restore the terminal modes. That "works", but on Unix platforms is likely to leave your erase key set to an unexpected value. It happens to work as expected for Linux- and modern BSD-systems.

    However, beyond that, ncurses normally resets

    • colors (default colors for the terminal)
    • line-drawing (disabled)
    • mouse protocol (to disable it)

    If your application uses any of these features, then the reset command is the appropriate choice. It usually clears the screen as well (perhaps not what was wanted). And it uses fewer characters:

    resetcontrolJ
    stty sanecontrolJ

    Further reading:

    • Signal handlers in curs_initscr(3x)
    • Portability in tput(1)
    • reset - reinitialization
    0 讨论(0)
  • 2020-12-14 04:25

    Write a signal handler for SIGSEGV, etc. that calls endwin().

    0 讨论(0)
提交回复
热议问题