GNU TeXmacsUser manualReferenceAboutEnglish
URLs, resolution and concretization

This page describes the C++ class url of

System/Classes/url.hpp

and

url.cpp

. The user level view and the Scheme routines are documented in the URL system; the treatment of tmfs URLs in internals of the TeXmacs file system.

1.Representation

class url
(a name, a path or a pattern)

A url is a reference counted handle (CONCRETE) on a url_rep, whose only field is a tree t. Two URLs are equal if their trees are equal; u[i] returns the i-th child of the tree as a URL and as_tree and as_url convert in both directions. The tree has one of the following forms:

an atomic string

One component of a name, such as "doc" or "a.tm". The special atoms "." (url_here), ".." (url_parent) and "…" (url_ancestor) denote the current directory, the parent directory and any ancestor directory; the empty atom marks a trailing slash.

(concat u1 u2)

The concatenation u1/u2. Concatenations are kept right nested: a/b/c is (concat a (concat b c)).

(or u1 u2)

A search path: the first of u1 and u2 which exists, or all of them, depending on the operation. Also right nested.

(root protocol)

The root of a name space: default (the local file system), file, http, https, ftp, doi, mailto, tmfs, blank (a name starting with //) and, on Android, content. (root ramdisc contents) is a ramdisc, a pseudo-file whose contents are stored in the URL itself.

(wildcard)

Any sequence of components (printed **).

(wildcard pattern)

One component matching a pattern with *.

(none)

The empty URL (url_none, printed {}), which is also the result of failed operations. The default constructor url () returns it.

So /usr/share is (concat (root default) (concat usr share)) and https://www.texmacs.org/x is (concat (root https) (concat www.texmacs.org x)).

The predicates of

url.hpp

test these forms (is_none, is_atomic, is_concat, is_or, is_root, is_wildcard, …) and the root of a name (is_rooted, is_rooted_web, is_rooted_tmfs, is_rooted_blank, is_ramdisc). is_name is true for rootless names without paths or wildcards and is_rooted_name for a root followed by such a name; the latter is the form of a resolved URL.

2.Parsing

URLs are parsed from strings by url_general (name, type), where type is one of

URL_SYSTEM

The conventions of the operating system: components are separated by / (and also by \ on Windows), and search paths by : (; on Windows). Used by url_system, for names which come from the system (command line, environment variables, file dialogs).

URL_UNIX

Unix conventions on all systems. Used by url_unix and by the constructors url (const char*), url (string) and url (dir, name), that is, for all names written in the source code.

URL_STANDARD

Web conventions (url_standard).

URL_CLEAN_UNIX

Like URL_UNIX, but without the heuristics described below.

In the system and Unix formats, a component ~ is replaced by the value of $HOME and a component $VAR by the value of the environment variable, which is itself parsed in the system format (so a variable holding a search path yields an or). An unset or empty variable yields url_none, which makes the whole URL empty. Components containing * become wildcards.

url_general first recognizes explicit protocols (local:, file://, http://, https://, ftp://, doi:, mailto:, tmfs://, // and, on Android, content://). It then applies heuristics, in this order: a string containing the path separator is a search path; a string starting with / (on Windows: a drive letter or \\) is an absolute local name; on Windows, /c/… in the Unix format is the drive c:; except in the clean Unix format, names starting with www. or ftp. are web addresses. Anything else is a relative name.

3.Printing

as_string (u, type) prints a URL in one of the formats above (the default is the system format). Components of non-local roots are always printed with /; non-trivial subexpressions are put between braces, so that a/{b:c} denotes a/b:a/c. The shortcuts as_system_string, as_unix_string and as_standard_string fix the format; operator << on a tm_ostream uses the system format. On Windows, local names are printed with drive letters (c:\…) or as UNC names (\\host\…).

4.Operations

Concatenation

u1 * u2 (also with a string or const char* on the right, parsed in the Unix format). If u2 is rooted, the result is u2, except that a local absolute name or a // name after a web URL stays on the same host and protocol. A trailing .. removes the last component; the parent of a root, of a web host or (on Windows) of a drive is itself. . is neutral and url_none is absorbing.

Search paths

u1 | u2, which removes immediate duplicates; an empty side (url_none) is simply dropped. expand distributes concatenations over or and replaces … by the list of all ancestors; factor is its inverse and also sorts; sort sorts the alternatives.

Components

head (the directory, u * ".."), tail (the last component), suffix (lower case, without a trailing ~ or #, empty for names without a dot or starting with one), basename, glue and unglue (add or remove characters at the end of the last component), unblank (remove a trailing slash).

Relative names

relative (base, u) is head (base) * u; delta (base, u) computes a relative name such that relative (base, delta (base, u)) == u when both are on the same root and host, and returns u itself otherwise.

Roots

get_root, unroot, reroot (u, protocol).

Descent and security

descends (u, base) tests whether u lies below one of the alternatives of base. is_secure (u) tests whether u lies below $TEXMACS_SECURE_PATH, which the boot sets to its previous value followed by $TEXMACS_PATH and $TEXMACS_HOME_PATH (see paths, directories and settings). In documents from secure locations, the typesetter executes scripts (such as extern) without first asking the Scheme predicate secure? (

Typeset/Env/env_exec.cpp

); the environment computes this flag from the file name of the document (edit_env_rep::secure). new_buffer_rep::secure is initialized in the same way but not read anywhere.

5.Resolution

An URL with search paths, wildcards or a relative name denotes a set of candidate resources. Resolution finds the existing ones:

complete (u, filter)

All candidates which pass the filter, as an or of rooted names. Relative names are interpreted with respect to $PWD (or $HOME if PWD is not set).

resolve (u, filter)

The first candidate only, or url_none. The default filter is "fr": a readable regular file. Alternatives of an or are tried in order, so the order of a search path is the order of precedence.

exists (u)

The same as resolve (u, "r") being non-empty; has_permission (u, filter) is the general version.

resolve_in_path (u), exists_in_path (u)

Look for an executable program, with the shell command which if it works on the system (use_which, determined at boot time), and otherwise in $PATH (also in $TEXMACS_PATH/bin on Windows and Android; on Windows, exists_in_path tries the suffixes .bat, .exe and .com).

descendance (u), subdirectories (u)

All subdirectories of the directories of a path, used for the style and package menus and for the style search paths.

The filter is a string of letters which are all tested by is_of_type (files and caches): f regular file, d directory, l symbolic link, r readable, w writable, x executable. The empty filter accepts everything, so that resolve (u, "") just builds the first candidate name; this is how save_string chooses where to write a file which does not exist yet. Local candidates are returned with the default root even if they were written with file://.

Wildcards can only be expanded for local files (anything else fails with an error): ** matches any sequence of directories, including the empty one, and a pattern matches one directory entry. Directory listings come from read_directory and are therefore cached for the system directories.

6.Concretization

A resolved URL is turned into a name which the operating system understands by

concretize_url (u)

Local names (roots default, file, blank) are rerooted to default; web URLs are downloaded with get_from_web, tmfs URLs are fetched from their Scheme handler with get_from_server, ramdiscs are written to a temporary file with get_from_ramdisc; . and .. become $PWD and its parent. Anything else yields url_none.

concretize (u, quiet)

The same, printed as a system string; a warning is printed on failure unless quiet is set.

materialize (u, filter)

Resolution followed by concretization.

sys_concretize (u)

(

file.hpp

) The concretized name quoted for the shell, used by the system (cmd, u, …) helpers.

For remote resources the result is a temporary file in the session's temporary directory, so concretized names must be used at once and not stored. The three fetch functions live in

System/Files/web_files.cpp

:

get_from_web (u)

doi: names are first rewritten to https://www.doi.org/…. With Qt 6 the file is downloaded with qt_download_file; otherwise with wget or, if it is not installed, curl. An empty or missing result means failure.

get_from_server (u)

Calls tmfs-load and saves the result in a temporary file; see the tmfs internals.

get_from_ramdisc (u)

Writes the contents stored in the URL to a temporary file.

Web and ramdisc results are kept in a small in-memory cache which maps the URL to its temporary file and remembers the 25 most recently used entries; web_cache_invalidate (u) forgets one entry, so that the next access downloads the file again. tmfs results are not cached. The cache is not saved between sessions, but generated files which should survive a session, such as downloaded images, go through make_file (files and caches).

7.Pitfalls

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".