Lightweight GNU readline alternative - c

I am looking for a GNU readline alternative. It comes with a lot of features but only couple of them are useful to me as explained below -
I am working on a interactive command prompt application (display prompt and accept next user command to be run). I want to implement some editing and history feature for the prompt. So when the user presses UP key it should show the last run command. Also, user should be able to navigate using arrow keys if he need to edit any typo or command switches etc.
On windows something similar already exists, if you use fgets or scanf to get the input on cmd prompt it already maintains history and also lets you edit.
Is there a good option available on linux?

This is an admirable goal I think :-)
Perhaps Linenoise, libedit/editline or tecla would fit the bill?
Of those probably libedit is the most widely used - e.g. postgreqsql client shell and various BSD utilities for Kerberos and ntp (although for the upstream sources it may not be the default line editing library for compilation due the to widespread use of libreadline on Linux). There are a couple of slightly different versions of libedit/editline as you'll see if you read some of those references and do some further research.
Cheers, and good luck with your project.

There is replxx, a BSD licensed alternative to readline.
It works in Linux, BSD, Solaris and Windows.
It has support for features you expect from interactive console programs, namely:
line editing
history
syntax highlighting
hints
UTF-8
user defined key bindings (supporting (shift/ctrl)F1 - F12)
multi-threaded print

I think the modern alternative of GNU Readline is Jupyter Notebook. The idea is that you don't create an executable that links to a line editor library. Instead, you should just provide the kernel and the users can choose their own notebook UI, either CUI, web based GUI, or even an IDE like VS Code.

Related

What windows component implement functionality similar to readline?

In this question I'm asking of one specific bit of functionality of readline:
The Readline library includes additional functions to maintain a list of previously-entered command lines, to recall and perhaps reedit those lines
Now on Windows with Visual Studio you ain't need no stinking readline. You can use fgets and arrow keys will happily recall what you typed previously. Of course you can edit these too.
On linux the very same code (fgets in a loop ) does not work like this. Up arrow is shown as ^[[A and left and right arrows also does not allow you navigating the line as shown by experiment and also described here.
My question is, what part of Windows makes the editing possible?
I think it can be either conhost.exe or how fgets, et al, are implemented. Somehow I suspect that it's the former. In any case, I'd like to know how exactly it works if it's documented anywhere, etc. For example, what other keys, apart from arrows have a special meaning and processed differently instead of being returned as par of the buffer fgets writes to.
The documentation for DOSKEY lists the special keys.
I am not aware of any documentation explaining that in 32-bit Windows this functionality is built into the console and that doskey.exe is merely an interface to it. However, it is easy to confirm that this functionality does not depend on the running console application using the C runtime library or having been launched from the command-line shell.
It is a reasonable guess that the actual code implementing this feature lives inside conhost.exe on current versions of Windows, but of course that's an implementation detail, subject to change without notice. From the programmer's perspective, all that matters is that DOSKEY functionality is present on any console window, and is available whenever the application is in cooked mode.
Note that cooked mode is the default setting. Therefore, console applications will have DOSKEY functionality unless the application specifically disables it.

In Windows, how can I trace in C which files a child process reads and writes?

My goal is to determine when executing a command, precisely which files it reads and writes. On Linux I can do this using ptrace (with work, akin to what strace does) and on FreeBSD and MacOS I can do this with the ktrace system command. What would you use to obtain this information on Windows?
My research so far suggests that I either use the debugger interface (similar to ptrace in many ways) or perhaps ETW. A third alternative is to interpose a DLL to intercept system calls as they are made. Unfortunately, I don't have the experience to guess as to how challenging each of these approaches will be.
Any suggestions?
Unfortunately it seems there is no easy way to intercept file level operations on Windows.
Here are some hints:
you could try to use FileMon from Sysinternals if it is enough for your needs, or try to look at the source of the tool
you could make use of commercial software like Detours - beware, I never used that myself and I'm not sure it really meets your needs
If you want a better understanding and are not frightened at doing it by hand, the Windows way of intercepting file I/O is using a File System Filter Driver. In fact, there is a FilterManager embedded in Windows system that can forward all file system calls to minifilters.
To build it, the interface with the system is provided by the FilterManager, and you have just (...) to code and install the minifilter that does the actual filtering - beware again never tested that ...
As you suggested, this is a fairly simple task to solve with API hooking with DLL injection.
This is a pretty good article about the application: API hooking revealed
I believe you can find more recent articles about the issue.
However, you probably need to use C++ to implement such a utility. By the way, programs can disable DLL injection. For example, I weren't able to use this approach on the trial version of Photoshop.
So, you may want to check if you can inject DLL files in the process you want with an existing solution before you start writing your own.
Please, take a look to the article CDirectoryChangeWatcher - ReadDirectoryChangesW all wrapped up.
It is a very old, but running, way to watch directory changes.
Microsoft owns a bunch of tools called Sysinternals. There is a program called Process Monitor that will show you all the file accesses for a particular process. This is very likely what you want.
Check this particular Stack Overflow question out for your question... This might help you:
Is there something like the Linux ptrace syscall in Windows?
Also, if you are running lower versions like Windows XP then you should check out Process Monitor.
Also, I would like you to check this out...
Monitoring certain system calls done by a process in Windows

How write a simple command line tool in C?

I want to write in C a command line tool with the following requirements:
Few commands (<10)
History management through the arrows key
Capability to delete what I typed previously with baskspace
Capability to add keyborad shortcuts such as ctrl+l to clear the screen
Protable across UNIX systems
I am not asking for code here, but for indications about where to start. I first made some experiments with "scanf" and it quickly become quite complicated. I then looked at ncurses, and it seems also quite low level. Is there any C libraries dedicated to this task, where would you start ?
I then looked at ncurses, and it seems also quite low level
CDK (Curses Development Kit) - high-level wrapper over ncurses. I've had successful experience with it. When you need you always can use ncurses directly.
The GNU Readline library is exactly designed for that.

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)

Linux C: interactive output

I'm developing some kind of mysql monitoring tool so I need interactive output like top command.
Is there any lib that can be used for this?
You may get some good milage out of ncurses http://www.gnu.org/software/ncurses/
Here is a link to the IO-Top source code. It is software that behaves like top, but displays information related to IO, rather than CPU.
http://repo.or.cz/w/iotop.git
ncurses was already mentioned. Another bash approach is using dialog. See http://linux.die.net/man/1/dialog

Resources