I want to know where exactly (terminal, keyboard driver, libc, linux kernel, ....?) is stored the text which the user enters in a console application before pressing enter key.
So the use case is like that:
The user opens a terminal or runs any other console program.
The above program invokes some library call to read input from STDIN like gets, scanf functions (ultimately invokes read system call).
The user enters some text and ultimately presses enter.
The above program gets the text into the buffer.
Screenshot of this scenario until pressing enter key is given in the following screenshot 1
I'd like to know:
Where the text is stored before the user presses enter (keyboard driver, kernel, terminal, etc.?).
How long is that buffer (I assume it is a buffer).
Related
linux terminals display output on a (configurable) scrolling window. User configuration allows control of both the scrolling and the size of the window (in number of rows and columns).
The fact that we can scroll back to areas that went off screen, then select that text for cut+paste, means that there is an accessible buffer where that content can be reached directly and specifically.
Is there a process, using linux/bash built-ins or utilites, which would permit the user to access specific segments of that content buffer (which are otherwise accessible via mouse selection) from within a bash script (preferrably directly ... and, if not, possibly via prompted mouse interraction) ?
If unable to do that selectively, is there a way, again from within a bash script, to get a snapshot of that full 2000-line buffer and save it to a script-defined file, where it could then be manipulated ?
How can I acknowledge the reading of an active std:cin call of a running C++ program from within another console?
Please: This IS NO DUPLICATE of e.g. Simulating ENTER keypress in bash script, because I don't want to input text to the program itself but to the std:cin that is running within a loop of that program.
So, the running program waits in the console by saying "Enter option:". Manually I would enter something and then press enter.
Entering text via
echo "some text" > /proc/18553/fd/0
works but I cannot redirect the enter-key to enter/end the text as I can by physically hitting the enter key.
This fellow here has or had the same problem:
Send a string and Enter key to a Node.js script [closed]
The script should treat 'enter' as sending the users string but instead it just prints '^M' to the terminal.
I'm not sure why this is happening suddenly, it wasn't happening yesterday. Something has changed about this particular terminal session because if I open a new terminal window it works as expected in that.
Any ideas?
I'm using iterm on Mac (xterm?)
TL;DR: just type the reset command on the OS shell whenever the terminal starts acting funny.
Terminal emulator programs (as iterm) feature complicated internal states in order to provide literally decades of features to allow prettier programs on the terminal.
Terminals started as keyboards that would physically print the typed text into paper, back in mainframe era - and "stdin" and "stdout" concepts: a single stream going forward with all data that is typed and another with all data that is printed out, are up to today, more than 50 years later, the default way to interact with the text terminal.
Internal state changes by programs that deal with the terminal in different ways (like being able to read a keypress without waiting for "enter"), should be reverted when the programs terminate. But due to errors and bugs that is not always the case.
I don't know which possible state would change "ˆM" to be displayed instead of a CR code being applied to the terminal. There are tens or hundreds of other possible misbehaviors, including completely messing up all characters. The reset command in modern *nixes (MacOS, Linux, BSDs) will fix everything.
Although, of course, if "enter" is not being applied, it won't be possible to issue the command at all from the os shell. You will have to start a new terminal session then. From within a Python program, if you happen to make a call to some code that will always break the terminal, you might run "reset" as a subprocess by calling os.system('reset').
In particular, "ˆM" represents "ctrl + M", which is a control character with value "13" (0x0d in hex). It is the value for the "Carriage Return" control code (AKA. "return", "enter", "CR"). If you never did this: try pressing "ctrl+M", and see that it behaves the same as "enter". It is being displayed in "human readable form" instead of acting as a control character in your case.
(regarding xterm and iterm: all they have in common is that they are both "terminal emulator" programs, xterm being one of the oldest existing such programs, which probably pioneered a lot of adaptations from text-only video modes to a graphic environment. "iterm" is just a normal modern app which implements terminal functionality)
In IDLE, there's no clear screen, and after reading the forums the best method is plainly to print a ton of "\n"s. However, after playing with IDLE, I made a discovery. When my game was waiting for an input after printing out instructions using print statements, instead of inputting any useful character, I entered 'control-z,' and IDLE began to remove the text that was display by the print statements, one by one.
My main question is, how do I manually in the code itself enter 'control-z', so I can utilize this functionality?
If you're confused by my story, here's some example code and try it yourself by hitting control-z.
print("Some Text")
print("More Text")
input("The Input (Hit Control - Z Here):")
^Z is a bit of a mess. The ascii control char ^D is usually interpreted as EOT, end of transmission, which on Unix and many other systems means end of file, close the application. Ascii ^Z is meant to be interpreted as SUB, substitute, whatever that means. Editors ofter use it as UNDO (meaning undo a ^X cut). Microsoft (and a few other old systems) at least sometimes interprets ^Z as end of file, with the same effect as ^D on *nix.
The Windows console closes a text app after ^Z . ^D is passes on to the app. IDLE, as a cross-platform app, closes on ^D. IDLE used to close on ^Z on Windows, but now, for me, it only erases the prompt. (I don't know if this alternative is intended.) I do not see the progressive deletion you report. What OS and what Python version are you running?
To answer your main question: you can't. input is usually used in assignment statements: string = input('prompt'). The way to imitate input statements is to directly assign 'user input': s = 'simulated user input'. However, this does not work for characters that get intercepted by the programs managing the input window and never sent to the python program.
IDLE's Shell generally imitates Python's interactive console. The latter (at least on Windows) makes everything, except the current input, read-only. Shell follows suite. Imitation is especially strict as regards executing user code. It is intended that user code tested in IDLE should run in Python without IDLE. It would be wrong for IDLE to clear the interactive shell in response to user code when Python cannot.
For editor and output windows, ^A (select all) followed by Backspace (delete), Delete, or ^X (cut) do clear the window.
Shell does, however, has more editing commands than many (most? all?) consoles and a menu system. These additions are allowed since they are interactive only and not accessible from user code. There have been various proposals and patches to enable clearing part or all of the shell window. https://bugs.python.org/issue6143 has some of the discussions and proposals.
I wrote a simple programe to manage command line user interface.
To do so, I use Nodejs Readline API to read the process.stdin Stream.
My problem is that if a user start typing, then press backspace, the complete line is deleted and fallback to default prompt style; until the user press enter. It looks like that:
What's your phone number?:
# User start typing
What's your phone number?: 123
# User press backspace
> 12
(you could test it running the example the repo examples folder)
Is there a way to prevent this behavior? (or to somehow hide it)