2

I've been trying to find information for a few days now. To be clear my goal is to create an ncurses-like C library. I'm fully aware of ANSI escape sequence and how to use them. However I want portability hence the reason why I'm interested in terminfo databases. I've read parts of documentations such as:

It allowed me to understand the theory and principles but in practice I can't find any example of how to access/interface the database in a programming language like C.

Currently the only way that comes to my mind would be loading the terminfo database binary file in memory and manually perform access to it, but it seems odd that I should build my own scheme and that there isn't a more easy / practical / official way to access these god damn databases.

bu5hman
  • 4,756
  • You can access it via the lower-level functions from the terminfo/tinfo library, which is ... part of ncurses ;-). Or you can use your own code to handle both the text and the compiled form of the terminfo entries, and also the termcap library on systems which do not support terminfo (FreeBSD). Good luck! –  Mar 17 '20 at 15:59
  • Thank you for your answer. – Curiosus Hominem Mar 17 '20 at 16:42

1 Answers1

3

To access the termcap database on FreeBSD, there is a way that comes in the operating system. The FreeBSD C library has an API for accessing capabilities databases. So getcap() et al. are the functions to call to access the termcap database at a low level. (There are in fact lower-level APIs, but they do not abstract away, as the getcap() API does, the fact that capabilities databases can be either variable-length-record flat files or proper indexed binary database files, built with the cap_mkdb program.)

At a higher level are tgetent(), tputs(), et al.. (These also exist on terminfo operating systems, such as many Linux-based operating systems.) These are, however, part of the termcap ncurses library that is in the FreeBSD C library; and you are trying to do your own "ncurses-like" library. (On the Linux-based operating systems they are part of the terminfo ncurses library.)

An example of accessing terminfo database records without going through ncurses' own terminfo API is the unibilium library. NeoVIM uses this.

On a higher level …

The idea that terminfo/termcap is more portable is really stretched to the breaking point and beyond in the second decade of the 21st century. You almost certainly won't ever encounter a real video terminal that isn't ECMA-48:1976 conformant, let alone a paper terminal. And the terminfo abstractions, which often do not actually match what real video terminals do, are in some ways an impediment to portability as they force a somewhat contorted way of working.

This is especially true of terminal input, which in fact has been ECMA-48 (with some state machine bodges for RXVT, Interix, the Linux KVT, and the SCO Console) since the early 1980s, and for which the termcap/terminfo model of matching fixed strings is a very poor fit. But the ideas of "local"/"xmit" calculator keypads, "cursor addressing mode", and only 3 forms of cursor manifestation, amongst others, also do not match the way that terminals actually work.

If you are going to reinvent ncurses, then please at least do not copy its colour-pairs model. That barely aligns with the ECMA-48 and AIXterm colour systems, let alone the ITU T.416 Indexed and Direct colour mechanisms that terminals have had for over a quarter of a century.

Further reading

JdeBP
  • 68,745