Capture string key with terminfo in C program - c

Nobody know a good way to take the terminfo key string in a C program? Like the infocmp of ncurses. I can't find anything :(
If someone know and want to tell me, I will be very thankful.

"Capturing" the value depends on what you are trying to do with it. The usual application uses the tigetstr function to read the value for a given terminal capability from the terminal database.
To do this, you must first initialize the terminfo interface using the ncurses library, for example using setupterm, setterm or newterm. The choice of function depends on what the program will be doing with the string.
In ncurses-examples, there are a few low-level terminal programs such as demo_terminfo.

Related

save and clear terminal window in linux [duplicate]

I'm having a hard time even googling this, because I don't know the right keywords. Some command-line apps (such as vi and less) take over the whole console screen and present an interactive interface to the user. Upon exiting such an app, the screen is returned to the state it was in before the app was launched. I want to write a program that behaves in this fashion, but again, I don't even know what this is called, so I can't find any documentation for how it's accomplished.
So, my question is threefold:
What keywords can I use to find documentation on this?
If you are so inclined, links to such documentation would be helpful.
Lastly, can I accomplish this in a scripting language like Ruby, or even bash? I have no problem with C, but the environment I work in is more amenable to interpreted languages.
As said in some comments, you are looking for ncurses. The Linux Documentation Project have a very good HOWTO on ncurses for C that I used myself to start on it
https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
The feature you are describing is the alternate screen buffer. I think that [N]Curses will enable this by default. There are certainly curses bindings for Ruby, Python, and other scripting languages.
you can even access ncurses in bash by using the tput program. The whole ncurses library (like curses before it) works by sending escape sequences to the terminal. The xterm program emulates a vt100 terminal (and also a Tektronic terminal) and there were various combinations of characters which would move the cursor, clear the screen, draw various characters etc. These would generally start with an escape character, hence the name: escape sequence. You also sometimes see these escape sequences in people's PS1 shell variables with the \e to provide the escape character; often used to colour the prompt or set the window title.
tput refers to the terminfo database to figure out what the escape sequences are to perform the functions you've asked it to do.
see the manual page, type:
man 5 terminfo
for more details

preventing arrow keys's outputing raw characters like ^[[A when making a shell in C

I am implementing a shell-like program where user types command (defined by me). Just like this.
>cmd
result blah blah blah
>
When I use arrow keys it outputs raw characters like ^[[A.
>^[[A
I also notice sqlite3 behaves like this at least in the version I compiled on my computer.
How to prevent this and let <- and -> keys move cursor left and right?
GNU Readline is a library specifically designed for this task (that is, allowing the user to edit commands typed at an interactive command-driven program). Note that this library is distributed under the GPL (not the LGPL); if that won't work for you, editline is a similar library with a BSD-style license.
I note that you say this is homework, so you might want to ask your instructor whether you are expected to implement cursor motion and line editing yourself. If so, ncurses (as mentioned by jDourlens) is the next step down in terms of abstraction, and if you really want to do everything yourself, read up on termios and the VT-220 control sequences (nearly all terminal emulators used nowadays emulate the VT220 or one of its descendants).
I have found a slightly simpler way to do that. Run rlwrap [your program] instead of [your program].
For example, it works fine with sqlite3 as rlwrap sqlite3
To let the user navigate in cmd with the keyboards arrows you may have to use termcaps.
http://www.gnu.org/software/termutils/manual/termcap-1.3/html_chapter/termcap_2.html
If you want to be easier to deal with termcaps that are a bit complex you shoul use Ncurses.
http://www.gnu.org/software/ncurses/ncurses.html
Good luck to deal with termcaps if you chosse this solution, it's some pain in the ass!
You may take a look to Ncurses. It's a library that let you control almost everything in terminals. The specific function you want is noecho(), which stops terminals from showing users' input.

Access Keystrokes in C

I am trying to access keystrokes in C. I can access alphanumeric keys. How can I access Control, Shift and Alt key?
Plus I read somewhere that sometimes while entering text in console, OS masks backspace key. I would like to know where user pressed backspace key. It's not same as knowing when '\n' was pressed.
GNU C. Ubuntu 11.
Dietrich Epp answered in a comment: use ncurses library.
See also this question
And you might make an X11 client graphical application; in that case use a graphical toolkit library like GTK or Qt
If you want to make a console application, use ncurses or perhaps readline
And your question, when taken literally, has no sense: the strict C standard don't know what a key or a keystroke is (the only I/O operations mentioned in the standard are related to <stdio.h> thru FILE). This is why most people uses additional libraries and standards (in addition of those required by ISO C), eg. Posix...
The simple answer is "you can't", at least not easily or without downloading third party libraries.
Most C programs shouldn't have to know anything about the keyboard or the screen. Standard C is only concerned with reading from and writing to files (the keyboard and screen being special-case files).
Assuming you have a good reason for wanting to access the keyboard directly, you should be looking at the ncurses library (http://www.gnu.org/software/ncurses/ncurses.html). Ncurses knows how many different (virtual) terminals and keyboards work, and it presents a uniform interface to them. It lets you paint the screen and create a substitute graphical interface using only blocks of text.
Since you use Ubuntu, try running the "aptitude" command to see a good example of what ncurses can do.

Can stdio be used while coding for a Kernel...?

I need to build a OS, a very small and basic one, with actually least functionality, coded in C.
Probably a CUI OS which does some memory management and has at least a text editor and a calculator, its just going to be a experimentation about how to make a code that has full and direct control over your hardware.
Still I'll be requiring an interface, that will need input/output functions like printf(&args), scanf(&args). Now my basic question is should I use existing headers or go for coding actually from scratch, and why so ?
I'd be more than very thankful to you guys for and help.
First, you can't link against anything from libc ... you're going to have to code everything from scratch.
Now having worked on a micro-kernel myself, I would not use the actual stdio headers that come with libc since they are going to be cluttered with a lot of extra information that will be either irrelevant for your OS, or will create compiler errors due to missing definitions, etc. What I would do though is keep the function signatures for these standard functions the same ... so in the end you would have a file called stdio.h for your OS, but it would be a very stripped down header file with the basic minimum requirements for your needs, and only having the standard I/O functions you need, with the correct standard signatures.
Keep in mind on the back-end, i.e., in your stdio.c file, you're going to have to point these functions to a custom console-driver or some other type of character drive for your display. Either that, or you could just use them as wrappers for some other kernel-level display printing routine. You are also going to want to make sure that even though you may use a #include <stdio.h> directive in your other OS code modules to access these printing functions, you do not link against libc. This can be done using gcc -ffreestanding.
Just retarget newlib.
printf, scanf, etc relies on implementation specific funcions to get a single char or print a single char. You can then make your stdin and stdout the UART 1 for example.
Kernel itself would not require the printf and scanf functions, if you do not want to keep the kernel in kernel mode and work the apps you have planned for. But for basic printf and scanf features, you can write your own printf and scanf functions, which would provide basic support for printing ans taking input. I do not have much experience on this, but you can try make a console buffer, where the keyboard driver puts the read in ASCII characters (after conversion from scan codes), and then make the printf and scanf work on it. I have one basic implementation were i have wrote a gets instead of scanf and kept things simple. To get integer output you can write an atoi function to convert the string to a number.
To port in other libraries, you need to make the components which the libraries depend on. You need to make the decision if you can code in those support in the kernel so that the libraries could be ported in. If it is more difficult then coding some basic input output functions i think won't be bad at this stage,

NCurses initialization without clearing the screen

I am writing a program that's similar to a shell. Once started up, there's a prompt and you enter in some app-specific commands.
So far this works just fine. However, I want to add support for command history like in Bash, so the user could hit the up or down arrow and see previously entered commands.
I have included the ncurses library, and I have done a simple hello world test with getch() to make sure the up and down arrows are reported correctly.
The thing that's bothering me is that it seems to be a requirement that I call initscr() which will clear the screen in order for me to use getch().
OKAY SO THE QUESTION IS:
Does anybody know a way to use ncurses getch() function without calling initscr() first? If not, can I make it not clear the screen? Basically, I'm looking to have getch() act the same as getchar(), if that makes sense.
Thanks in advance!
EDIT: I think the best example of this is how Python runs in interactive mode.
Curses wants to fully control the screen, and to optimize writes to the screen, especially over slow serial lines. To do this, it needs to know what is on the screen, and the only reasonable way to do that with most terminals is to start from an empty screen and keep track of what you write to the terminal.
Thus, I suspect (n)curses is the wrong tool for your shell. You probably need to go down a step on the abstraction layer and use terminfo and non-blocking reads from the terminal (standard input) instead.
(This is not very helpful. Sorry.)
It might be simpler to use an interface like readline() rather than resorting to full-blown ncurses.
You could just call your program from within rlwrap and have the functionality without the pain...
Here is another discussion about this. The provided solutions are:
"The 'filter()' function lets you use curses as a single-line."
"You can write something equivalent in C, using setupterm to get the
terminal data, and tparm, tputs for formatting and output."
Of course there is the third option to get the ncurses source code and modify it so it doesn't clear the screen anymore.
Have you considered the simple expedient of creating a custom terminfo or termcap entry lacking a sequence to clear the screen and then switching your terminal setting to that right before running your program? You could also just use newterm() and set_term() in ncurses. This used to be easier with termcap, since you could include another terminal and override some of its capabilities.

Resources