I'm running an Ubuntu 16.04 system and installed clang-4.0 and lldb-4.0 from the designated repos:
deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-4.0 main
deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-4.0 main
For some reason, hitting the tab key enters a literal tab instead of triggering command completion, and ctrl-a and ctrl-a enter ^A and ^E instead of taking the cursor to the beginning or end of the line. Hitting ctrl-c gives ^C instead of quitting the program. The arrow keys also don't work correctly. For example, left arrow gives ^[[D.
Any ideas?
Related
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.
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.
My embedded system has one background program which generates some output message to console.
When it's not finished, the login program starts and prompt login string to same console as well.
My question is, when the login prompt string comes out, the output message generated by the background program is not aligned like this:
Embedded System login: msg_line1...
msg_line2...
msg_line3...
The expected output should be:
Embedded System login: msg_line1...
msg_line2...
msg_line3...
msg_line4...
Have no idea how to resolve the problem...
Could anyone help?
THX!!
Configure your terminal program to autolinefeed, so that it generates the \r internally whenever it receives a \n.
use a redirection for your background program
YourBackStuff.sh >/tmp/back_out.1 2>/tmp/Back_out.2 &
or refresh the screen.
Notice that only the appearance is modified, the real output ou current application/shell (so not the background one) is correct, only the terminal show your all the info received. So
YourFrontApp.sh | tee /tmp/front_out.1
cat /tmp/front_out.1
will produce a correct display (when background process stop)
Looks to me like your messages are sent with a new-line character (represented by \n in C) at the end of each line. This requests a new line from the terminal, which it duly provides. It does not request a carriage return (i.e. "go back to the left hand edge"). This would be represented in C as a \r.
To fix it, you need to set your terminal application to interpret \n as \n\r - it will have a setting for "treat newlines as newline+CR" or some such.
I have this program in C that reads the input like this:
cod1 = getch ();
if (kbhit())
cod2 = getch ();
I can read every Ctrl+Char possible sequences, except for Ctrl+C, that closes the program - that is OK, and Ctrl+S, that simple is not catch. But I wanted to make Ctrl+S to be the save function in my program; how could I do that? Furthermore, is it possible to read Alt+Char characters? Because it reads it as a regular character, e.g., Alt+A is read with the same codes as A.
Your problem is that input probably gets eaten by terminal emulator.
For example Alt+<Whatever> is often reserved for menu shortcuts (e.g. Alt+F opens File menu). Matching characters are often hilighted once you hold Alt (F get's underscored in File).
Ctrl+S is reserved for Stops all output on screen (XOFF) (again your terminal emulator does that).
As for using Alt+<...> as shortcuts in your command line application. As far as I'm concerned holding Alt doesn't affect character received, it just sets flags which are hard to access in console. Even in GUI application (in Windows) it's quite tricky and you have to use function like GetAsyncState() to check whether alt was pressed.
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" &