Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu? - c

I've written a tiny program in Ansi C on Windows first, and I compiled it on Ubuntu with the built-in GCC now.
The program is simple:
read the line from console with scanf().
Analyze the string and calculate.
But something weird happens. When I try to move the cursor, it prints four characters:
pressing Up prints "^[[A"
pressing Dn prints "^[[B"
pressing Rt prints "^[[C"
pressing Lt prints "^[[D"
How can this be avoided?
Why does it print these 4 characters instead of moving the cursor?

Because that's what the keyboard actually sends to the PC (more precisely, what the terminal prints for what it actually receives from the keyboard). bash for example gets those values, deciphers them and understands that you want to move around, so it will either move the cursor (in case of left/right) or use its history to fetch previous commands (up/down). So you can't expect your program to magically support arrow keys.
However, reading from standard input from the terminal already supports left/right arrow keys (I believe, but I'm not in Linux right now to test and make sure). So my guess is that there is another issue interfering. One possible cause could be that one of your modifier keys is stuck? Perhaps ALT, CTRL or SUPER?

For those who are coming from the osx (mac) try changing the shells to bash
Terminal -> Preferences -> Shells open with -> [select] Command (complete path)
then paste
/bin/bash

This might be because the user account is created in shell. You can change it to bash by two ways.
Permament solution is -
sudo chsh -s /bin/bash ${username}
To get this solution working you will have to logout and login
Temporary solution is everytime when you login into the ubuntu server type bash and hit return.

If it's under a docker container, run /bin/bash . This helped me solve the problem.

Additionally to what Shahbaz mentioned, I realized that pressing enter (thus sending an empty command) can fix the problem. This is usually necessary after using CTRLC to cancel a command.

On MacOS Terminal for me was enough to uncheck "Scroll alternate screen" for the issue to disappear. See screenshot of the preferences below.

You can (re)bind keys. Add this at the bottom of your .profile, .zshrc or whatever shell config you have.
bindkey -e
bindkey '\e\e[C' forward-word
bindkey '\e\e[D' backward-word

i think simple way is we can just do
kill %%
because this sometimes happen because of background processes.

Related

GDB monitor inside TMUX garbled

I was debugging in terminal using gdb, and I typed inlayout src,then I entered the UI mode.
Most of the time, it worked fine, but sometimes, the whole terminal text code got mixed.(I post the picture below.).
what's more, sometimes I typed in some shell command in gdb like !pmap <pid>, its output's format was also strange.
I tried to set TERM to screen-256color but it doesn't work.
I solved it in some sense.
I redirect the output of my program to another tty.

How to enable logging in xterm

Is it possible to turn on logging feature by default in xterm?
Lets say for example, I have example program in c that give an output in xterm everytime i ran the program from default bash terminal in linux. And I want to save the output that shows in xterm into a file everytime the programs is run.
I'm using centos7_x86_64 fyi
Thanks.
In Windowmaker:
Hold down ctrl + left click in xterm window (on the terminal text), click on "log to file".
A cool thing to do is when you're coding, log the terminal, and then when you cat the Xterm log, you see coding in fast forward. If you wanted you could video it.
Note that there are also other menus in xterm, accessible using Ctrl+Left Click, Ctrl+Middle Click, Ctrl+Right Click.
Log file will be in the directory that you launched Xterm from, and will be in the format: Xterm.log.<hostname>.<date>.<time>.log.
This is a very good question, there's no reason to mark down a question like this.
It can be done in two ways:
Using script:
xterm -e script mylogfile -c "someCommand -i input_file -o output_file -f someArg"
Redirect to a file:
xterm -e 'someCommand --arguments 2>&1> /path/to/mylogfile'
I do essentially this with "terminal-window", mrxvt, "hcm" and "pypty".
terminal-window wraps mrxvt, just filling in some commandline options. mrxvt is a lightweight, multitabbed, nonunicode terminal emulator not dissimilar to xterm.
hcm is a GUI that makes it easy to run a shell (or other command) on a remote host. It can also start an mrxvt with remote ssh's without requiring the GUI if you prefer (using hcm-term).
pypty is a /usr/bin/script reimplementation that is written in Python. It is not significantly different from /usr/bin/script, except it gives a "dated files mode", that allows you to have one file per pseudo terminal per day. So if you leave a shell logged in overnight, you get one file per day - this tends to make it easier to find what you're looking for.
All this combines to give you pseudo terminal logging with great ease. Commands are run on remote hosts, but logged locally. Just start a "fancy terminal-window" (or use hcm-term), and everything you see on the screen plus control characters (but not nonechoed passwords) will be logged under ~/.hcm/logs/<year>/<month>/<day>/* .
Also, if you hit the shell button in the lower right of mrxvt, you get another ssh session into the same remote host, which is also logged locally (to a different file under ~/.hcm/logs/...). When I started making use of that feature, I had no idea how much I would grow to like it.
You can obtain them from http://stromberg.dnsalias.org/~strombrg/hcm/ There's a video there that shows how easy it is to set up and get started with.
BTW, fancy terminal-window sets up $PS0 or "trap DEBUG" to give you command start times and finish times. It's great for post mortems. It does this without replacing any of the usual bash startup files.
I wrote terminal-window, hcm and pypty, but I can't take credit for mrxvt. :)
HTH

Stopping Linux console from echoing input during program execution

I'm writing a C program that requires to hide the characters a user types from the screen during the program execution. For example, when running the following loop
while (1)
{
//do some work
}
the console displays the blinking cursor (that's good). BUT, when the user types keys on the keyboard, these keys are being echoed out to the console. To visualize it better:
Step 1: Starting the program
root#debian:/home/root# ./program
_
Step 2: User types some characters (even though he shouldn't)
root#debian:/home/root# ./program
AdajfsaSJ_
The characters get echoed on the console. How can I stop this? I know it's theoretically possible, but I can't find out how to implement it.
If you need a better example for what I want to achieve, use the screen command on an empty serial port. screen /dev/tty30 for example. This empties the console and runs the program, HOWEVER, the user is not able to enter any characters (there's a blinking white cursor block and no keyboard characters are being echoed to the console). That's what I need.
Any insight would help, thanks!
Use termios() to turn off the ECHO flag of the terminal.
To turn off the text cursor, use the termcap library to control the cursor visibility.

How do I copy everything from my terminal to a file including the stdout and the prompt using C?

I know how to get the stdout into a file using dup/dup2 system calls, but how do I get the entire output that would be normally shown on my terminal(including the prompt that says my username along with the $ symbol and the current working directory) to a file?
Yes you can, but this may be difficult in many details (depending on your expert level). For the shell to behave normally (I would mean exactly as in a terminal), then it needs to interact with a terminal (special system object). So you need to create a program that behave like a terminal, this what pseudo-terminals devices (/dev) are intended for. Read documentation about this to implement it but roughly, your application should behave like the user so should be connected to the slave side of the pseudo-terminal, and the shell to the master side of the pseudo-terminal. Then you can easily log real inputs made by the user and catch outputs made by the shell.
Can't comment cause of low reputation.
I would say there is no way to do that inside a code in C. Instead, you could use bash for example to redirect everything to a file, and leave the code in C as it is.
In this way you have all the info you want to save: prompt, current directory, call to the program (including flags), and of course the output of the program.
Well, you can do:
-For bash prompt PS1: Echo expanded PS1 (in case you want it expanded, if not there is a simple way to do it just echong PS1)
- For executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time
- Standard output and error output: Redirect stderr and stdout in a Bash script
And that's all you want to capture, I think.
Look up the script command in Unix systems. If you want to capture all keyboard and std in/out for a command, use the script executable. If you want to see how it's done, look up the source.

in Gnome/metacity can we bind a key to a command that requires user input from the keyboard?

All,
I have a little utility written in C that requires the user to press a key to make a menu selection. The code is predictably simple:
system("stty raw");
save_ID = getchar();
system("stty cooked");
It grabs the keystroke and doesn't need to wait for ENTER. It works fine from the command line, however, when I try to bind the command to a hotkey in Gnome/metacity, it waits for the keystroke alright, but the keystroke seems to be processed not as as input to the command, but as if it were a separate command. Eg. if my keystroke is "1" and I'm in a terminal I see:
"zsh: command not found: 1"
I've tried it without the "system" lines, but nothing changed. Is there perhaps some special version of "getchar" that works with the GUI? Incidentally, a non-interactive version of the program works fine when bound to a hotkey, so the program is basically functional, it just can't be used interactively.
Thanks
I believe GTK may interest you, although I've never personally used it.
http://www.gtk.org/
.
You should also be able to run gnome-terminal. Using /opt/appFoo/appFoo as the command to run your app normally, you would run
gnome-terminal -e "/opt/appFoo/appFoo" &

Resources