Is there an equivalent of getch() function in Mac? - c

I am not able to find the equivalent header file for conio.h (for C programs) in Mac.
I was wondering if there is any option for getch() function in Mac?
I want to use it such that user can give options and program will go forward without pressing enter.

You probably want to look at ncurses. It has a lot in common with conio. You can get its documentation with "man ncurses".

Related

system("cls"); not working in C language

system("cls") is no working in C language . I added conio.h header file but it always say cls not found. I am using xcode. But same code executes perfectly on visual studio.
That's because system("cls"); has nothing to do with the c language.
The conio.h header is as far as I know an old MS-DOS header, you can't use it portably. The system() function executes external programs from within a c program, cls is an MS-DOS program to clear the text buffer of a MS-DOS console.
In your picture, it's clear that you are not executing the program in an MS-DOS console so it wont work.
Using external programs is almost always a bad idea, except if those programs are guaranteed to be installed with your's1. The reason is that any program that relies on other programs being available in the target environment, will fail when the external programs aren't.
I understand that it's easy to see a lot of code using non standard tricks like system("cls"), but if you find good learning resources that will not be the case. Try to study each and every function you learn and determine whether or not it's a standard function and a good practice to use it the way you see it.
1TeX distributions work like that, they are just several programs that exchange text, following very closely the UNIX Philosophy. But they are all distributed together.
You are trying to run cls command (to clear the screen) on the console using the system(). cls command only exists on DOS or command prompt on Windows.
system("cls");
If your program is running on Bash on Linux or MacOSX, you can try clear.
system("clear");
It's not working because the system() is a library function of stdlib.
You need to use #include<stdlib.h> in order to use system("cls") in C.

Capture string key with terminfo in C program

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.

How to display a custom prompt during the execution of C program?

I'm trying to emulate a terminal using a C program in Linux and need my program to display a custom prompt while the program executes. Is there a way to display it using my C program? (I can always try to printf "My-prompt" every line manually, but I'm looking for a better way). Also I can't use any additional libraries other than the basic ones so GNU Readline library and editline library wouldn't work (as suggested in another thread).
for example:
user#mypc:~$ ./a.out
my_custom_prompt>3+5
my_custom_prompt>8
my_custom_prompt>exit
user#mypc:~$
I believe what the OP wants is to simply have the "prompt" printed along with any program output, without having to add this manually every time. There is a way to do this, if you write a wrapper function on top of printf to do this, and call that instead of printf directly.
Probably this will help: http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html
In your example, you already have got a terminal. You want to write a command-line interface with a prompt, not a terminal.
I can always try to printf "My-prompt" every line manually, but I'm looking for a better way
There’s nothing wrong with this approach. You have a loop which prints the prompt and waits for input afterwards. As Kunerd said in the comment, one line of code.
Normally, a prompt is printed to stderr rather than stdout. This has the advantage, that the prompt appears before a newline is written, as stderr is unbuffered (and in combination with piping and redirection it seems reasonable to me, that this stuff doesn’t go to the same stream as the actual output).
Also I can't use any additional libraries other than the basic ones so GNU Readline library and Editline library wouldn't work
Doing this in a way strictly conforming to the C standard and not using any libraries but the standard one makes things like line editing (other than using backspace) or a command history (close to) impossible. If that’s OK for you, look for fgets etc. and keep in mind, that stdin is usually line-buffered.
POSIX specifies some additional properties of terminals, see e.g. http://pubs.opengroup.org/onlinepubs/9699919799/. Maybe curses is also of interest for you.
Perhaps you're looking for fgets() documentation?

How to get the last key pressed without stopping the C program?

Writing an application with command line interface and I would like to know at any time if F1 or ESC or an arrow key is pressed. What is the simplest way of doing this? I would like to avoid using a readline type library.
This is a Linux specific question; the program is not multithreaded.
There is no way to do this in the C standard, but C implementations on various operating systems usually have some extension to do this.
On Windows, you can use getch(). On Linux and Unix, look at this question:
Non-blocking getch(), ncurses
Also, this is the very first question in the "System Dependencies" section in the C FAQ list:
19.1
An implementation of kbhit() for Linux is presented in Beginning Linux Programming page 167. You can read it on-line at the link provided.
EDIT: I mention kbhit() because it was posted as a solution before it was made clear that the question related to Linux. Unfortunately the solution has been deleted, which is unfortunate. The principle is that when kbhit() returns non-zero, a subsequent blocking character-oriented read call will not block. This is only true for character oriented input; getchar() and other standard functions that read stdio are typically line-oriented, so block until newline.
Multiple threads?

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