Using Terminal Control Codes with C - c

I am interested in creating a terminal-based text interface in C without the use of a library like ncurses. I know that through using tput and a variety of escape codes, it is possible to create such an interface. However, I am uncertain of how to use tput or similar commands in C.
First, I am wondering what the best option is for implementing something like this in C without external libraries (so it can be compiled and run on a bare bone system).
Second, if using tput is the best option, how can I call these commands from C?
I understand that using a pre-existing library, such as ncurses would greatly simplify the process, but I would like to create my program without them.
Thank you in advance.

The standard C library does not provide any functions that can be used instead of the functions in ncurses. You'll have to use ncurses or another third party library that provides the equivalent functionality.

Related

Can I use OpenGL for graphics in my C program?

I want to use a library for making some shapes in my C program. Can I use gl.h in my C program if I install it? Will it be compatible or is it designed for languages other than C?
You normally dont install gl.h. It is just the header to a library. The library-implementation should already be provided by your OS/drivers eg in some .DLL-files. The header gl.h can just be #include'ed so you can call the functions.
You did not tell us your OS, but there are tutorials for each, eg for windows: have a look at the tutorials on the right side of http://nehe.gamedev.net/

How to write an application that uses the terminal as GUI? (in C)

I'd like to write an application (in C) that uses the terminal of a *nix OS as its GUI. I mean an application of the kinds of emacs, vi etc. that take up the whole terminal window and jump back to the input prompt after quitting.
How would you go about doing that, preferably in C? Can someone point me to a tutorial covering that?
You need to use ncurses:
http://en.wikipedia.org/wiki/Ncurses
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
It is available on all major distros.
Well, actually this is not GUI (graphic user interface) but a text based interface. You can use the library ncurses to create such applications in C.
Use a library like ncurses, it is specifically designed for this purpose.
Throwing in alternate solutions so that this question thread does not look so monotonic:
the slang library (mc uses it, for example)

What libraries are needed for graphics like vim or nano?

What library is used to make a static terminal window like vim, nano, irssi or aptitude as opposed to a scrolling terminal?
I'm using C, but I'd be interested in libraries for other languages (for example, a C++ specific library).
I believe the library you are looking for is Curses (or NCurses). There is also PDCurses for cross-platform (includind Win32) development.
I also remember the days of Conio on DOS based systems.
Curses like library are usually used for that. pdcurses for windows, most *nixes come with a native version, or e.g. ncurses
That would be ncurses.
You can also use the termcap library (curses provides this) to lookup the terminal control strings for the terminal you're connected to, and send them yourself to implement whatever you like. Or, if you don't mind requiring a modern terminal that supports at least a common subset of the ANSI/ECMA standard, you can simply hard-code the standard terminal escapes.

Approaches to a GUI for a Large C Program

In our Bioinformatics lab we've recently been asked to create a GUI for a program written (and optimized) in C. Any GUI we designed would need to be able to feed input to and receive output from the C program, while also being easily portable to both Windows and Mac. What are ways to go about doing this?
If your looking for a GUI toolkit that works on windows/mac in C, have you considered GTK?
Download QT or wxWidgets.
Why do the hard work when someone already has and will let you use it for free? I prefer QT myself, btw.
Edit: It does mean using C++ but it will work perfectly with C code.
Use a separate program.
It doesn't break the existing code - especially important if the code has bio knowledge that a programmer might not know/understand/test.
Gui's change often, in a couple of years you are going to be rewriting the gui for NewSuperOsToolkit(tm) but the underlying worker code won't change. We have atmospheric modelling code that I'm sure was originally written in latin.
You keep the ability to run the engine code as a batch, in parallel on MPI in the cloud, and a bunch of other ways you haven't thought of.
You could write the front end GUIs in Java, and have them feed input and receive output from your C programs. I've known a couple of groups here at work that do something similar with C# and C code (but they don't have the multi-platform restraint).
This way you don't necessarily have to create your GUIs using a C toolkit.
Depending on the nature of the program, you could create an entirely separate GUI application (in any language you prefer), and fork/execute the existing program from it, redirecting it's stdin and stdout to your GUI program. Depending on how the existing program works, this could work well, or be very cumbersome.
You could also extend the existing program with GUI code in something like GTK (which has a C API), or you could use Qt (which is C++, but there's usually no problems calling C functions from C++ if you define them as 'extern "C"').
There are a number of scripting with languages that can make this easy. I'd go for tcl/tk it works on mac pretty much out of the box, and is cross platform across pretty much any machine you can think of.
Also GTK is great and a number of scripting languages have bindings for it.
You probably don't want to write your gui in c if you don't have to. use a rapid develoment scripting language.
If you want a native GUI, then go with wxWidgets or QT. (I use wxWidgets ...)
The alternative is to have an HTML based interface using Javascript and CSS. You avoid GUI libraries altogether, you get cross-platform support and the outcome is better on some dimensions, worse on others.
If you really want a standalone user interface, you can integrate it into a binary with something like WebKit.

interact (stdin/out) with command line programs at runtime in C

I think the thing I want to do is called GUI/command line wrapping sftp(1). I need an easy way to start that program and react on its output while running. Additionally I have to be able to send input to it, full interaction is required.
I tried forkpty (emulated TTY), but there wasn't one good example findable using forkpty for that job, instead several warnings about overflows in in arguments and advisories not to use it. Another weird thing about this was the windowsize argument...
Please either give me one or many example(s) on how to call & interact with command line programs in C or another way of integrating sftp in an iPhone GUI
Rejoice! Expect was created to solve exactly your problem. It's based on Tcl, which is not so pleasant, but the tool is pleasant, it's really well designed, and there's a good book by Don Libes, who created the tool.
Expect scripts are written in Tcl, but it is totally easy to integrate a Tcl script into a C program such that other parts of the C program don't even know that Tcl is being used.
Have you used any of the popular scripting languages Ruby/Python/Perl/etc? They all have pretty full featured libraires for opening and communicating with other processes.
the subprocess module in python for example, or Popen in Ruby... there would also be lots of reference material around the web to help you out.
If a GUI was also required you could look at GTK extensions
Instead of calling sftp(1), how about using libssh? It has full sftp subsystem support.

Resources