GNU TeXmacsUser manualReferenceAboutEnglish·Français·Deutsch
Linking your system as a dynamic library

Instead of connecting your system to TeXmacs using a pipe, it is also possible to connect it as a dynamically linked library. Although communication through pipes is usually easier to implement, more robust and compatible with gradual output, the second option is faster.

Warning 1. Dynamic linking is only available if TeXmacs was compiled with the macro TM_DYNAMIC_LINKING defined (the autotools configuration script defines it when dlopen is available), and it is not supported under Windows. Otherwise, the connection fails with the message Dynamic linking not implemented. A more recent description of dynamic linking, with a complete example, can be found in the section on dynamic libraries.

1.Connections via dynamically linked libraries

Let us now describe the steps you have to go through in order to link your system as a dynamic library.

  1. Modify the architecture of your system in such a way that the main part of it can be linked as a shared library; your binary should typically become a very small program, which handles verbatim input and output, and which is linked with your shared library at runtime.

  2. Include the header file $TEXMACS_PATH/include/TeXmacs.h in the source of your system and write the input/output routines as required by the TeXmacs communication protocol, as explained below.

  3. Include a line of the form

    (:link "libmyplugin.so" "myplugin_exports" "init")

    in the plugin-configure instruction of your file init-myplugin.scm. Here libmyplugin.so is the corresponding shared library, which is searched in $LD_LIBRARY_PATH (the lib subdirectory of your plug-in is automatically added to this path), myplugin_exports is the name of the exported data structure which will be used by TeXmacs in order to link your system, and init some initialization string for your package.

  4. Proceed in a similar way as in the case of communication by pipes.

In older versions of TeXmacs, the configuration used the instructions package-declare and package-format; these no longer exist.

2.The TeXmacs communication protocol

The TeXmacs communication protocol is used for linking libraries dynamically to TeXmacs. The file $TEXMACS_PATH/include/TeXmacs.h contains the declarations of all data structures used by the protocol. In principle, a succession of different protocols is foreseen. Each of these protocols has the abstract data structures TeXmacs_exports and package_exports in common, with information about the versions of the protocol, TeXmacs and your package.

The \(n\)-th concrete version of the communication protocol should provide two data structures TeXmacs_exports_n and package_exports_n. The first structure contains all routines and data of TeXmacs, which may be necessary for the package. The second structure contains all routines and data of your package, which should be visible inside TeXmacs. Only the first version of the protocol has been implemented so far.

When a session is started, TeXmacs (see

System/Link/dyn_link.cpp

) opens the library, looks up the symbol given in the :link option using dlsym, and interprets it as a pointer to a structure of type package_exports_1. Notice that the comments at the beginning of

TeXmacs.h

, which mention a function get_my_package and a Scheme command package_declare, describe an older mechanism which is not used anymore.

3.Version 1 of the TeXmacs communication protocol

In the first version of the TeXmacs communication protocol, your package should export an instance of the following data structure:

typedef struct package_exports_1 {
  char* version_protocol; /* "TeXmacs communication protocol 1" */
  char* version_package;
  char* (*install) (TeXmacs_exports_1* TeXmacs,
                    char* options, char** errors);
  char* (*evaluate) (char* what, char* session, char** errors);
} package_exports_1;

The string version_protocol should contain "TeXmacs communication protocol 1" and the string version_package the version of your package.

The routine install will be called once by TeXmacs in order to initialize your system with options options (the initialization string of the :link option). It communicates the routines exported by TeXmacs to your system in the form of TeXmacs. The routine should return a status message like

"yourcas-version successfully linked to TeXmacs"

If installation failed, then you should set *errors to an error message: as soon as *errors is not NULL, the installation is considered to have failed.

The routine evaluate is used to evaluate the expression what inside a TeXmacs-session with name session. The input what is serialized in the same way as for pipes. The routine should return the result of the evaluation, encoded in the same way as the output of an application which communicates through pipes, that is, using DATA_BEGIN-DATA_END blocks (for instance "\2verbatim:Hello\5"). If NULL is returned, then the contents of *errors is used instead (or the string "Error" if *errors is NULL).

Remark 2. TeXmacs copies the strings returned by the routines install and evaluate and the error messages, but it does not free them (see

System/Link/dyn_link.cpp

). The package is therefore responsible for the memory management of these strings, for instance by reusing a static buffer, as in the dynlink example plug-in. The comments in

TeXmacs.h

which state that these strings are freed by TeXmacs are not accurate.

The first version of the TeXmacs communication protocol also requires TeXmacs to export an instance of the data structure

typedef struct TeXmacs_exports_1 {
  char* version_protocol; /* "TeXmacs communication protocol 1" */
  char* version_TeXmacs;
} TeXmacs_exports_1;

The string version_protocol contains the version "TeXmacs communication protocol 1" of the protocol and version_TeXmacs the current version of TeXmacs.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".