terminalc++14terminfotermcap

If else expression in terminfo parameterized strings


I am implementing a parser in C++ for parameterized strings(which are used for specifying some terminal capabilities for a terminal). Then I came across this on the man page of terminfo:

%? expr %t thenpart %e elsepart %;
   This forms an if-then-else.  The %e elsepart is optional.  Usually the %? expr part  pushes
   a value onto the stack, and %t pops it from the stack, testing if it is nonzero (true).  If
   it is zero (false), control passes to the %e (else) part.

So, my question is:
As the man page says, that the expr part in the string can usually push values onto the stack, so what other things can be done in the expr part, that is, what other operations can be performed in the expr part except for pushing values on the stack ?


Solution

  • short: "anything"

    long: terminfo expressions use up to 9 parameters (for the obvious reason, digits 1-9 which in turn is the likely reason for sgr having 9 parameters).

    In the given example,

    %? expr %t thenpart %e elsepart %;

    think of it as a program

    %?
    expr
    %t
    thenpart
    %e
    elsepart
    %;

    that can enclose other if-then-else pieces. However, the purpose of terminfo is to transform those parameters into a string to send to the terminal. A literal character would be "output". You could put literal characters as the thenpart, and those would be used for output. Or you could compute some expression and save it temporarily, e.g., on the stack. Doing that in both thenpart and elsepart lets you pop the stack after the if-then-else and either use it for output, or in some further expression.

    xterm uses the feature in setf, setb and sgr (see link to "xterm-basic").