0

I'm writing a terminal, and was trying to use steam locomotive as a test bed, and ran into an unusual problem. Even in a regular terminal like xterm or mate-terminal, the output is different if you run the application, i.e. sl verses if you log it to a file i.e. sl > sl.txt then cat it cat sl.txt

This is particularly pronounced on sl-h where in the first few seconds, codes are output which would make the X/O for the railroad crossing which if catted show incorrectly putting the X/O at the beginning of the line.

bad truncated output from SL

It seems that there is an ioctl that's happening that somehow changes newline behavior to advance to the next line, but not return back to the beginning. I.e. strace sl-h 2> sllog.txt shows: ioctl(1, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig -icanon echo ...}) = 0

How would you be able to detect this as a terminal? What systems are in place? Why wouldn't ncurses just use escape sequences to convey this information? There's a lot of questions that I'm not sure how to handle in my terminal.

Charles Lohr
  • 695
  • 1
  • 8
  • 23

1 Answers1

1

ncurses (like Unix curses) puts the terminal into raw mode for two reasons:

  • ability to read single characters (and ignore signals such as quit that might be bound to them), and
  • ability to manage the output, including movement around the screen with fewer characters than a simple printf.

In raw mode, the Unix-style conversion of line-feed to carriage-return and line-feed is disabled. That lets ncurses move directly down by one row (in the same column) with a single characters. Cursor addressing would typically use 6 or more characters. Even a cursor-down would use 3 characters.

Incidentally, that treatment of line-feed is the reason why the terminal descriptions often have the cursor-down capability assigned to a line-feed.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Then a better question is how can a terminal detect that the slave on the other end of the PTS is requesting raw mode? – Charles Lohr Jul 30 '18 at 21:33
  • The terminal mode can be checked using [**`tcgetattr`**](http://pubs.opengroup.org/onlinepubs/007904875/functions/tcgetattr.html) – Thomas Dickey Jul 30 '18 at 22:15
  • You can't practically poll that every time you want to draw a character to the screen. It also seems that terminals like sl and xterm don't do this... I can't seem to find the mechanism within them to detect the change. – Charles Lohr Jul 31 '18 at 16:45
  • The terminal ***driver*** does the transformation of line-feed, etc., so the terminal sees only the raw stream of data. – Thomas Dickey Jul 31 '18 at 21:48
  • Hmm... me, being the terminal, I have to at some point decide to move the cursor down a line when I see a character 10... And I have to decide whether or not I want to move the cursor to the beginning of the line. – Charles Lohr Aug 01 '18 at 02:14