waterfogSW
waterfogSW

Reputation: 5

How screen handling program(eg.vi, less) work?

Suppose I'm reading a document in vi. When I reach the end of the screen and press the arrow down key, a new screen including the contents of the next line is printed in the terminal.

I wonder how this process is implemented.

Is vi process raise SIGTSTP and call signal handler to continue the process with new screen including next line?

Upvotes: 0

Views: 41

Answers (1)

Martin Rosenau
Martin Rosenau

Reputation: 18523

Is ... process raise SIGTSTP and call signal handler ...

No.

The program first performs a tcsetattr() (which internally performs a special ioctl()) so the keyboard input is not handled line-wise but key-wise.

Then it prints out 25 lines.

Now it simply uses read() to wait for a key pressed.

If you press the DOWN key, one additional line is printed.

If you press the UP key (in the case of less), it is a bit more tricky; depending on the terminal used, some "escape sequence" (a special combination of bytes) is written to the screen using write(). This will cause the screen to be scrolled down. Then another "escape sequence" is written which places the cursor to the top left of the screen and one line of text is printed at the top of the screen.

When this is done, the program waits for the next key using read() ...

Signals are not involved.

Upvotes: 2

Related Questions