Andres Martinelli
Andres Martinelli

Reputation: 173

NCURSES - missprinting new lines when printing in tty mode

I have this program:

#include <ncurses.h>

SCREEN * sstderr;
SCREEN * sstdout;

int main() {
    sstderr = newterm(NULL, stderr, NULL);
    noecho();
    sstdout = newterm(NULL, stdout, stdin);
    set_term(sstdout);

    addstr("PRESS A KEY");
    getch();
    def_prog_mode();
    endwin();

    system("ls -l");

    getchar();
    reset_prog_mode();
    refresh();
    addstr("Press another key");
    getch();
    set_term(sstdout);
    endwin();
    set_term(sstderr);
    endwin();
}

every line that is in the output of the 'ls -l' command gets missprinted like this:

drwxr-xr-x   2 root root    4096 Feb 11 09:22 bin
                        drwxr-xr-x   3 root root    4096 Mar  6  2016 boot
                                                drwxr-xr-x  18 root root    3380 Feb 23 00:12 dev
                                                                        drwxr-xr-x 113 root root   12288 Apr 25 10:45 etc
...

I tried using def_shell_mode() just before the initscr() line (or newterm() in my case), and reset_shell_mode() just before system("ls -l"); but the problem persists. the only way I can kind of fix this is using

system("reset");

just before the system("ls -l"); line.

Anyone know what the real problem is, and how I can fix it without that "reset" call?

Thanks!

Upvotes: 2

Views: 433

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54515

Your program initializes the same terminal into curses mode twice. But the first time it sets the terminal into raw mode starting from cooked mode. The second time it is already in raw mode. It doesn't matter (much) that those are separate streams, but that they're connected to the same terminal driver.

Having initialized the second screen (for standard output), and then doing a "restore", nothing happens because it restores to raw mode.

You could "fix" it by switching back to the standard-error screen before doing endwin. Offhand, you'd have problems copying the shell-mode terminal settings from one screen to another.

Upvotes: 1

Related Questions