Basic input/output using pipes |
The configuration and the compilation of the minimal plug-in is described in the chapter about plug-ins. We will now study the source file minimal/src/minimal.cpp. Essentially, the main routine is given by
int
main () {
display-startup-banner
while (true) {
read-input
display-output
}
return 0;
}
By default, TeXmacs just send a '\n'-terminated string to the application as the input. Consequently, the code for read-input is given by
char buffer[100];
cin.getline (buffer, 100, '\n');
The output part is more complicated, since TeXmacs needs to have a secure way for knowing whether the output has finished. This is accomplished by encapsulating each piece of output (in our case both the display banner and the interactive output) inside a block of the form
DATA_BEGINformat:messageDATA_END
Here DATA_BEGIN and DATA_END stand for special control characters:
#define DATA_BEGIN ((char) 2)
#define DATA_END ((char) 5)
#define DATA_ESCAPE ((char) 27)
The DATA_ESCAPE is used for producing the DATA_BEGIN and DATA_END characters in the message using the rewriting rules
DATA_ESCAPE
⟶
DATA_BEGIN
DATA_ESCAPE
⟶
DATA_END
DATA_ESCAPE
⟶
DATA_ESCAPE
The format specifies the format of the message. For instance, in our example, the code of display-startup-banner is given by
cout << DATA_BEGIN << "verbatim:";
cout << "Hi there!";
cout << DATA_END;
fflush (stdout);
Similarly, the code of display-output is given by
cout << DATA_BEGIN << "verbatim:";
cout << "You typed " << buffer;
cout << DATA_END;
fflush (stdout);
(plugin-configure myapp
(:require (url-exists-in-path?
"myapp"))
(:launch "myapp
–texmacs")
(:session "Myapp"))
In the case when you do not have the possibility to modify the source code of myapp, you typically have to write an input/output filter tm_myapp for performing the appropriate rewritings. By looking at the standard plug-ins distributed with TeXmacs in
$TEXMACS_PATH/plugins
you can find several examples of how this can be done.