Skip to main content

About engines

Engines let Spectranext prepare data for a ZX Spectrum program, such as copying a mounted file into the default XFS overlay, extracting values from JSON and HTML, or listing and extracting ZIP archives.

Calling engines​

Invoking an engine (paths, mounts, return codes) is documented on spectranext.h — spectranext_enginecall. This page describes the documented engines and the structured output used by jsonpath and xpath.

Firmware and command compatibility​

Use Spectranext 2.0 or newer with the current SDK. Engine calls use controller command 22, allowing input paths up to 511 bytes, output paths up to 127 bytes, and operation strings up to 255 bytes, excluding the terminating NUL in each case. This accommodates long signed download URLs without copying the archive locally first.

Deprecated command 6 remains available to existing binaries with its original 127-character input limit and original workspace layout. It is not interchangeable with command 22: changing only the command number in code that writes the workspace directly is insufficient. See Controller channels for the layouts and completion rules.

Where results are written​

An output is a plain path without a mount prefix, such as results.bin. It is written through the cartridge's default XFS overlay, whose mount address remains xfs://ram/:

  • With an available USB drive, new output files go to USB.
  • Without USB, new output files go to temporary RAM storage.

Allow enough free space on that destination. Do not assume results are in a private RAM filesystem, or that they disappear on restart: USB output persists. Use a different path from the input and avoid names belonging to the user, because engines can replace output files. See Overlayed Filesystem for storage precedence.

From C:

int8_t r = spectranext_enginecall("0:example.json", "out.bin", "jsonpath '$[*][\"id\",\"title\",\"url\"]'");

The operation string is parsed like argv: the first token is the engine name; remaining tokens are engine-specific (for jsonpath, each token is one JSONPath expression).

The legacy name json is still accepted and behaves the same as jsonpath.


Engines available​

EngineRole
cpCopy an entire file, or a byte range, from a mounted filesystem into the default XFS overlay.
jsonpathLoad JSON from the input file, evaluate one or more JSONPath expressions, write scalar results to the output file using the format below.
xpathLoad HTML from the input file (libxml2 HTML parser with recovery), evaluate one or more XPath 1.0 expressions, write string results in the same column format as jsonpath.
zipList archive members or extract one member by filename into the default XFS overlay, including from mounted HTTP(S) files.
img2spec (planned)Image Spectrumizer — convert a raster image from the input file into Spectrum-friendly art (attributes, possibly screen$, etc.) and write a digestible binary/text bundle. To do — not implemented yet.

Unknown engine names fail with a negative error code (see spectranext_enginecall).


cp engine​

Use cp to make a local copy of a file from the current mount or an explicit mount slot. For example, with an HTTPS filesystem mounted on slot 3:

int8_t result = spectranext_enginecall("3:assets.bin", "assets-cache.bin", "cp");
if (result < 0) {
/* Copy failed; do not use the output. */
}

The output path belongs to the default XFS overlay and has no mount prefix. An existing output file is truncated. Use a different name when copying from the default XFS overlay so that source and destination are separate files.

Copy a byte range​

Supply both an offset and a size to copy part of a file:

/* Copy 16 KiB starting at byte 32 KiB. */
spectranext_enginecall("3:assets.bin", "part.bin", "cp 32768 16384");
OperationMeaning
cpCopy until the input reaches EOF.
cp OFFSET SIZESkip OFFSET bytes, then copy exactly SIZE bytes.

Offset and size are nonnegative byte counts, in decimal or 0x hexadecimal. If the input ends before that range has been copied, the operation fails. A failed copy can leave a partial output file; use it only after a successful result.

The SDK call returns 0 on success and a negative value on failure. It waits for the copy to finish. To keep your program running during the copy, submit the same engine call on controller channel 1. Channel 0 can then read an already-copied file while channel 1 prepares the next one.


Structured output format (JSONPath and XPath)​

Engine output is a binary stream on the default XFS overlay. It is a sequence of length-prefixed strings, written back-to-back:

  1. Length header — 2 bytes, little-endian uint16: byte length of the payload including the terminating NUL (strlen(text) + 1).
  2. Payload — that many bytes: the C string (including the terminating NUL).

Think of the file as a stack of samples: each logical value is one header + string record. There is no extra padding between records.

The jsonpath and xpath engines use this for:

  • A decimal count string (e.g. "9") for how many scalars follow in that column.
  • Then one record per scalar (IDs, titles, URLs, …), in match order.

If you pass several path arguments (jsonpath path1 path2 … or xpath expr1 expr2 …), the engine writes one column per path: for each path it emits count, then all matched values for that path, then moves to the next path.


jsonpath engine​

Inputs​

PieceMeaning
Input file (spectranext_enginecall first argument)Path on the current default mount, or N:path to select mount index N (0…3). The file must contain UTF-8 JSON (object, array, etc.). For HTTP(s)/TNFS app data, mount point 3 is preferred (filesystem mount points).
Output file (second argument)Plain path on the default XFS overlay (no N: prefix). Created/truncated by the engine.
Operation (third argument)After parsing: argv[0] = jsonpath (or json); argv[1]… = one or more JSONPath strings (space-separated in the shell; in C use a single string with spaces).

Each JSONPath must match only scalar values (strings, numbers, booleans, null). If a path matches an object or array, the call fails (non-scalar error).

Scalars are converted to text for output (e.g. numbers formatted as integers when exact; strings copied; null → null, booleans → true / false).

One path, multiple fields per array element​

A single path can select several fields per array element, e.g. $[*]["id","title","url"]. Matches are emitted in order: for each item, id, then title, then url, then the next item’s triple, and so on. The leading count string is the total number of scalar values in that column (for three fields per story, count = 3 × number_of_stories).


xpath engine​

Inputs​

PieceMeaning
Input filePath on the current default mount, or N:path to select a mount explicitly. The file contains HTML (UTF-8), parsed with libxml2’s HTML parser (recover from markup errors), or strict XML when --xml is specified. See mount points.
Output filePlain path on the default XFS overlay (same as jsonpath).
Operationargv[0] = xpath; argv[1]… = one or more XPath 1.0 expressions (quote tokens that contain spaces).

For each expression:

  • A node set result emits count = number of nodes, then the string value of each node (same idea as multiple scalar matches in jsonpath). Attribute nodes yield their value text.
  • String, number, or boolean results emit count = 1 and one text value.

XML and record output​

Use xpath --xml to parse strict XML instead of HTML. Existing calls without --each retain the column format described above.

xpath [--xml] --each "selector" "field1" "field2" ... selects a node set, then evaluates every field relative to each selected node. The source document is parsed once and field expressions are compiled once. Output is a single LE string containing the record count, followed by the field strings for each record in order. There are no per-field counts. Each field uses XPath string conversion: missing nodes become empty strings; multiple nodes yield the first node's string value. position() and last() refer to the selected record set. An empty selection emits "0"; a scalar selector or invalid expression fails. Use a selector predicate such as [position()<=60] to bound the records.

xpath --xml --each "/rss/channel/item[position()<=60]" "plainTitle" "link" "normalize-space(html-text(description))" "imageurl"

The generic html-text(value) extension parses a string as an HTML fragment and returns its text content, decoding HTML entities and removing tags. Empty input returns an empty string. Use normalize-space() around it to collapse whitespace. This does not fetch referenced images or other resources. Values use the existing 4096-byte buffer and UTF-8 folding rules.

This mode is implemented in FuseX and the cartridge engine source; validation for this change used libfusex, not physical hardware. Old FuseX builds must be updated before running applications that use --each or html-text().

The HTML http://www.w3.org/1999/xhtml namespace is registered with prefix html if you need explicit prefixed names in expressions.


img2spec engine (planned)​

Status: to do — not implemented.

Image Spectrumizer — intended to read a bitmap image (format TBD: PNG, JPEG, etc.), quantize and map to Spectrum display constraints (attributes, possibly Timex/chunky modes later), and write a small binary or text bundle through the default overlay suitable for loading or further processing on the Spectrum.


Examples​

JSONPath​

Command​

engine-call 0:example.json out.bin jsonpath '$[*]["id","title","url"]'

Sample input JSON (0:example.json)​

[
{
"id": 47687273,
"title": "Git commands I run before reading any code",
"points": 412,
"user": "grepsedawk",
"time": 1775638422,
"time_ago": "4 hours ago",
"comments_count": 92,
"type": "link",
"url": "https://example.com",
"domain": "example.com"
}
]

With more objects in the array, the same pattern repeats: each object contributes id, title, url in order.

Logical layout of out.bin​

The file is not human-readable text; conceptually the jsonpath engine writes:

[ count as decimal string ]
[ id text ]
[ title text ]
[ url text ]
[ id text ]
[ title text ]
[ url text ]
…

Each bracket is one LE length + NUL-terminated string record as in Structured output format. For example, with one array element above, count is "3" (three scalars), then the string forms of id, title, and url.

C example​

spectranext_enginecall(
"0:example.json",
"out.bin",
"jsonpath '$[*][\"id\",\"title\",\"url\"]'");

XPath​

HTML on an HTTP(s) mount works like any other input path. With news.ycombinator.com mounted at site root on mount point 3 (see HTTP(s) filesystem and mount point conventions), path 3:show fetches https://news.ycombinator.com/show.

Three XPath expressions give three columns (rank text, story title, link href), each with its own count then scalars — same binary layout as multiple jsonpath arguments.

Command​

engine-call 3:show out.bin xpath '//span[@class="rank"]/text()' '//span[@class="titleline"]/a/text()' '//span[@class="titleline"]/a/@href'

Logical layout of out.bin​

Conceptually (each line is one LE length-prefixed string record):

[ count column 0 ]
[ rank text ]
[ rank text ]
…
[ count column 1 ]
[ title text ]
…
[ count column 2 ]
[ href text ]
…

C example​

spectranext_enginecall(
"3:show",
"out.bin",
"xpath "
"'//span[@class=\"rank\"]/text()' "
"'//span[@class=\"titleline\"]/a/text()' "
"'//span[@class=\"titleline\"]/a/@href'");

To inspect a captured file on a host machine, use tools/enginefile-decode.py.


See also​

  • spectranext.h — spectranext_enginecall, mount rules, error codes
  • HTTP(s) filesystem — hosting JSON or HTML on an HTTP(s) mount (e.g. 3:news/1.json, 3:show when HTTP(s) is mounted at index 3)

zip engine​

OperationOutput
zip listDecimal entry count, then a name and decimal uncompressed size in bytes for each member. Every value uses the length-prefixed string format.
zip extract 'folder/game.tap'The exact named member's raw bytes. No prior listing or offsets are required.

Input uses the usual mounted path (N:path); output is a plain path through the default XFS overlay without a mount prefix. Use a different path from the source. Existing output is replaced; failed output is removed. Member names are matched exactly, including case and directory components. Directory entries can appear in listings.

Spectranext 2.0 accepts mounted input paths up to 511 bytes (plus NUL), including full signed URLs on an HTTP(S) mount. No local archive copy is needed.

/* HTTPS archive on slot 3; output files go to the default XFS overlay. */
int8_t result = spectranext_enginecall("3:games.zip", "members.bin", "zip list");
/* Extraction can also be called independently of the listing. */
result = spectranext_enginecall("3:games.zip", "game.tap",
"zip extract 'folder/game.tap'");
/* Use game.tap only when result == 0. */

For a responsive UI, use the same operation with spectranext_enginecall_async() and poll for completion. Submission returns 0 on acceptance; polling returns 0 while pending, a positive value on success, or a negative error.

From the controller console:

engine-call 3:games.zip members.bin zip list
engine-call 3:games.zip game.tap zip extract 'folder/game.tap'

Archives strictly smaller than 1 MiB are buffered in heap. Larger or unknown-size archives switch to bounded-memory sequential reads. Remote listing normally downloads the archive once; extraction normally scans it and reopens it to reach the selected member. Large directories or comments can require an additional pass. No HTTP ranges or temporary XFS archive copy are used.

STORE and DEFLATE extraction stream output and validate its size and CRC. The caller must provide enough free space on the destination storage for the output; the URL must remain usable across reopens.

ZIP64, split archives and encrypted extraction are unsupported. Names are limited to 1,023 bytes; listings are limited to 1 MiB of serialized fields. The API returns -3 for invalid/unsupported archives or a missing/ambiguous member, -2 for allocation/I/O failure, and -8 for HTTP 429.