![]() |
Programs, web requests, messages and timing | ![]() |
This page describes the utilities of
System/Misc/sys_utils.hpp
, the HTTP requests of
System/Files/web_files.hpp
, the output streams of
System/Files/tm_ostream.hpp
, the timer of
System/Classes/tm_timer.hpp
and the server log of
System/Misc/server_log.hpp
. Pipes and sockets to plug-ins are described in the plug-in machinery.
Runs a shell command and returns its exit status. On Unix the output is discarded (the command is run as cmd > /dev/null 2>&1); with the option -verbose the output is captured and printed on the debug-shell channel. The variants system (cmd, u1, …) of
file.hpp
append concretized and shell quoted file names.
The same, but the output is redirected to temporary files, which are then read into out (and err). In the two-argument form, standard error goes to out as well.
The output of the command as a string; the second one removes trailing newlines.
Runs a program without a shell (posix_spawnp on Unix), feeding the strings in to the file descriptors fd_in and collecting the outputs of fd_out; the result is the exit status followed by the outputs. Not available on Android.
Starts a command with popen and reads its standard output in a detached thread. async_eval_pending (), called by the interpose handler of the server, delivers finished results by calling the Scheme call_back with the output. A second variant stores the result in variables given by reference instead.
A portable poll (select on Windows), for at most 64 descriptors.
Environment variables, through the platform layer. get_env ("PWD") falls back to $HOME if PWD is not set.
get_printing_cmd and set_printing_cmd hold the print command (by default lp, or the bundled SumatraPDF on Windows).
Whether scripts in insecure documents are never accepted, accepted after a prompt, or always accepted (0, 1, 2), set from the security preference.
A printable stack trace, used in crash reports (
unix_stacktrace.cpp
and its Windows and Android counterparts).
http_post, http_post_json and http_post_query send an HTTP POST request with headers and a body (a string, a tree converted to JSON, or form fields) and return the answer; the async_http_post… variants deliver it later through the asynchronous mechanism above, and http_from_json parses an answer. With Qt 6 they are implemented with the Qt network classes (qt_http_post in
Plugins/Qt6
); otherwise they build a curl command line. Downloads of web files are described in URLs, resolution and concretization.
tm_ostream is a reference counted output stream with operator << for strings, numbers and formatted trees. Its representations write to a FILE*, to a string, to a buffer, or to a debug channel:
Standard output and error. redirect replaces the representation; the -log-file option uses it to send both to a file. buffer and unbuffer temporarily collect the output in a string (exported to Scheme as cout-buffer and cout-unbuffer).
std_error, failed_error, boot_error, io_error, std_warning, io_warning, debug_std, debug_io, debug_boot, debug_shell, … are streams on named channels. Writing to them calls debug_message (
Kernel/Abstractions/basic.cpp
), which prints the message on cout with the prefix TeXmacs] channel,, stores it in the global list debug_messages and, once the editor runs, calls the Scheme function notify-debug-message. The debugging console and the message windows read this list with get-debug-messages. The list is never shortened.
Whether debugging output is produced at all is decided by the caller, by testing the flags set with the -debug-… options (DEBUG_STD, DEBUG_IO, DEBUG_BENCH, …).
raw_time () is the wall clock time in milliseconds, texmacs_time () the number of milliseconds since the program started (used for time stamps such as the last visit of a buffer), and cpu_time_ms () the processor time of the process (used by the idle monitor of the server). bench_start (task) and bench_cumul (task) accumulate the time spent in a task (nested calls are counted once), bench_end, bench_print and bench_reset report and reset; the reports are printed only with -debug-bench.
System/Misc/server_log.hpp
defines log levels from log_emergency to log_debug and the macros SLOG, SLOGI, SLOGW, SLOGE and their SERRNO_… variants, which add the system error message. server_log_write is implemented per platform. When TeXmacs runs as a server and its standard output is not a terminal, messages go to the system log (syslog on Linux, os_log on macOS); otherwise they are printed on the stream of their level with a date, the process and the user id. See also the TeXmacs server.
On Unix, system (cmd, out) appends > tmp 2>&1 to the command. A redirection of standard error inside cmd, such as the 2> /dev/null used in many calls of eval_system, is overridden by the final 2>&1, so error messages end up in the result anyway (Plugins/Unix/unix_sys_utils.cpp:31-40). Code which tests the result of eval_system for emptiness must take this into account.
In async_eval_system, the reading thread and the main thread share the flag done and the output buffer without any synchronization (
System/Misc/sys_utils.cpp
, async_read_output and async_eval_pending), which is a data race. The exit status is always reported as 0, standard error is discarded, and the kill flag is only tested before the command starts, so a running command cannot be stopped.
tm_poll silently ignores descriptors beyond the 64th.
unbuffer assumes that the stream is buffered; calling it (or cout-unbuffer) without a preceding buffer crashes.
server_get_stream has no default case and returns nothing for an unknown level.