2

While developing a line editor in C++, I'm using getch() to capture each key input, and see \r(13 in ASCII) as the Enter key. However, when pasting text that contains multiple lines, the program mistakenly interprets \r within the text as an Enter key press.

I've noticed that in some shells, when pasting multiple lines, they automatically handle the input as new lines, without executing each line individually. I'm curious about how they achieve this. Do they detect key presses without relying on getch()?

Is there a cross-platform way in C or C++ to handle this issue effectively?

3
  • Could you add some codes and sample inputs to help illustrate? Also note that \r, i.e., carriage return, itself is not newline, instead, what it does is to simply move the cursor back to the begin of line. If my memory is not that rusted, in Windows, when you press "enter" on keyboard, you get a "\r\n", while in Linux it's only '\n' Commented Aug 27, 2024 at 14:54
  • In your question, you wrote: "I've noticed that in some shells, when pasting multiple lines, they automatically handle the input as new lines, without executing each line individually." -- Can you provide an example of this, by specifying the exact program which does this, while also specifying the operating system on which this happens? Commented Aug 27, 2024 at 15:16
  • Thank you for your response. I now realize that, I've been mixing up the concepts of shell and console. Those shells actually execute each line individually. Commented Aug 28, 2024 at 11:59

1 Answer 1

1

Seems to me you are mixing the concepts of Shell and console here.

Firstly, no matter whether you are typing or pasting something, you effectively trigger an interrupt. How is the interrupt handled depends on what process you are currently running.

For your situation, certainly the terminal/console is the one currently running, the interrupt is handled by the console, hence you instantly get the character you just typed shows on the display.

Up to now, whatever you typed/pasted is not executed, they are simply buffered until a "enter/return" is pressed, which could be a single '\n' or "\r\n", in Linux and Windows, respectively, then Shell takes over and execute your instructions.

So for your statement:

they automatically handle the input as new lines, without executing each line individually.

The reason is obvious for now, hopefully:

  • They are displayed as multiple lines because they are themselves really multiple lines, the console process simply shows whatever it is.
  • The first line is not executed immediately because you haven't pushed the return on your keyboard, only when the interrupt of return/enter signals and get processed by your console, will the Shell take over and execute. That said, when you pressed return, they'll still be executed as separate commands, unless '\' is appended at end of each line.

EDIT: I found this answer gives some nice references on different terminal buffering schemes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for clarifying that! I had mixed up the concepts. It makes sense now that each line is executed individually when I press enter. I really appreciate your help!
No worries mate, glad if it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.