summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md17
-rw-r--r--benchmarks/term_write_bench.c5
-rw-r--r--include/term/term.h71
-rw-r--r--include/term/term_input.h6
-rw-r--r--meson.build6
-rw-r--r--src/compat.h75
-rw-r--r--src/escape.c4
-rw-r--r--src/internal.h20
-rw-r--r--src/meson.build8
-rw-r--r--src/osc/misc.c2
-rw-r--r--src/screen.c47
-rw-r--r--src/snapshot.c14
-rw-r--r--src/term.c5
-rw-r--r--src/text.c14
-rw-r--r--tests/api_args_test.c140
-rw-r--r--tests/dcs_test.c53
-rw-r--r--tests/meson.build18
-rw-r--r--tests/model.c8
-rw-r--r--tests/model.h5
-rw-r--r--tests/model_row_test.c2
-rw-r--r--tests/model_test.c2
-rw-r--r--tests/osc_test.c32
-rw-r--r--tests/term_test.c195
-rw-r--r--tests/version_test.c27
-rw-r--r--tools/amalgamate.py2
25 files changed, 683 insertions, 95 deletions
diff --git a/README.md b/README.md
index 1294116..c77883a 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
term is a headless terminal core: the parser, screen ring, logical-line
history store, write core, and viewport snapshots. It is a C11 library
-with no dependency but libm.
+with no dependency but libm and, on POSIX, the thread library (`-pthread`).
| Path | Contents |
| --- | --- |
@@ -23,14 +23,16 @@ Pick one of these:
sets the lowest instruction set, such as `-march=x86-64-v3`.
- **Amalgamation**: run `tools/amalgamate.py . OUT`. It writes
`OUT/term.c` and `OUT/term.h`. Compile `term.c` with any C11 compiler
- and link libm. A Rust `build.rs` needs one
+ and link libm and the thread library: `cc -std=c11 -pthread -c term.c`
+ on POSIX. A Rust `build.rs` needs one
`cc::Build::new().file("term.c").compile("term")`.
- **Sources**: compile `src/*.c` and `src/osc/*.c` once, with `src` and
- `include/term` on the include path.
+ `include/term` on the include path. Link the thread library too.
-The library needs nothing else. The `*-amalgamation` tests run the term
-tests against the generated `term.c`, and `term-cross-test` compiles it
-for aarch64, macOS, and Windows.
+The library needs nothing else. On Windows the writer lock is an SRWLOCK
+from the system library, so it needs no extra link. The `*-amalgamation`
+tests run the term tests against the generated `term.c`, and
+`term-cross-test` compiles it for aarch64, macOS, and Windows.
### SIMD level
@@ -68,7 +70,8 @@ no flags to get them:
A host wires the terminal in four places:
1. Create it with `term_new()`. Set `term_options.callbacks.reply` to
- send replies to the program.
+ send replies to the program. Set `term_options.name` to the name
+ programs read with XTVERSION and XTGETTCAP. It defaults to `term`.
2. Feed program output with `term_feed()`.
3. Translate key and mouse events into `struct term_key_event` and
`struct term_mouse_event`. Encode them with `term_input_encode_key()`
diff --git a/benchmarks/term_write_bench.c b/benchmarks/term_write_bench.c
index 50853f6..7649ed8 100644
--- a/benchmarks/term_write_bench.c
+++ b/benchmarks/term_write_bench.c
@@ -152,7 +152,10 @@ int main(int argc, char **argv)
.size = { .x = cols_val, .y = 40 },
.scrollback = BENCH_SCROLLBACK,
});
- if (!t) return EXIT_FAILURE;
+ if (!t) {
+ fprintf(stderr, "term_new failed: TERM_COLS must be 0 to %d\n", TERM_MAX_COLS);
+ return EXIT_FAILURE;
+ }
/* manyline has bare LFs: each one also returns the cursor to
* column 0, as a tty with ONLCR does. */
unsigned flags = !strcmp(cases[c].name, "manyline") ? TERM_FEED_ONLCR : 0;
diff --git a/include/term/term.h b/include/term/term.h
index dc41114..d8758e7 100644
--- a/include/term/term.h
+++ b/include/term/term.h
@@ -17,8 +17,9 @@
*
* Threads. Any thread can call any function. Each call that reads or changes
* a terminal enters its writer section, so calls on one terminal run one at a
- * time. A snapshot belongs to one thread at a time. term_free() is the last
- * call on a terminal.
+ * time. A thread that waits for the section sleeps. On POSIX the section is a
+ * mutex with priority inheritance where the platform has it. A snapshot
+ * belongs to one thread at a time. term_free() is the last call on a terminal.
*/
#include <stdbool.h>
@@ -41,6 +42,16 @@ struct term_snapshot;
#define TERM_SCROLLBACK_DEFAULT 10000
/*
+ * The version of this header. A host compares TERM_VERSION_CODE with
+ * term_version() to check that the library matches the header.
+ */
+#define TERM_VERSION(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch))
+#define TERM_VERSION_MAJOR 0
+#define TERM_VERSION_MINOR 1
+#define TERM_VERSION_PATCH 0
+#define TERM_VERSION_CODE TERM_VERSION(TERM_VERSION_MAJOR, TERM_VERSION_MINOR, TERM_VERSION_PATCH)
+
+/*
* Colors. A term_color is either a direct color (alpha 0xFF, then RGB) or a
* palette slot: an index 0-255, the default foreground, or the default
* background. A slot resolves when a snapshot is painted, so a theme change
@@ -139,20 +150,36 @@ struct term_callbacks {
void (*trace)(void *ud, const char *line);
};
-/* Options for term_new(). An all-zero value is valid. */
+/*
+ * Options for term_new(). An all-zero value is valid. A size component of 0
+ * selects 80 columns or 24 rows. A negative component, or columns above
+ * TERM_MAX_COLS, makes term_new() fail. A negative scrollback fails too.
+ * cursor_style must be a value of enum term_cursor_style.
+ */
struct term_options {
- struct term_vector2i size; /* columns and rows; each is at least 1 */
+ struct term_vector2i size; /* columns and rows */
int scrollback; /* history lines; 0 selects TERM_SCROLLBACK_DEFAULT */
struct term_theme theme;
enum term_cursor_style cursor_style;
bool cursor_steady; /* false: the cursor blinks */
+ /*
+ * The terminal's name. Programs read it with XTVERSION and XTGETTCAP.
+ * It is also the title of a notification that has none. NULL or ""
+ * selects "term". Otherwise 1 to 63 printable ASCII bytes, or term_new()
+ * returns NULL. term_new() copies the name.
+ */
+ const char *name;
struct term_callbacks callbacks;
+ /* Must be zero. term_new() returns NULL if any word is nonzero, so a
+ * later version can give a word a meaning. */
+ uint32_t reserved[8];
};
-/* NULL when memory runs out. */
/* The Unicode version of the width tables, such as "18.0.0". A host can
* pass it to programs as UNICODE_VERSION. */
TERM_API const char *term_unicode_version(void);
+/* The TERM_VERSION_CODE the library was built with. */
+TERM_API unsigned term_version(void);
/* The length of the prefix of buf made of bytes in [0x20, hi). With hi
* 0x7f, it finds a run of printable ASCII in UTF-8 text, so a host can
@@ -165,18 +192,21 @@ TERM_API size_t term_scan_printable_span(const char *buf, size_t len, unsigned c
* and comparisons. */
TERM_API const char *term_isa(void);
+/* NULL when memory runs out or an option is invalid. */
TERM_API struct term *term_new(const struct term_options *opt);
TERM_API void term_free(struct term *t);
/* Hold the writer section across several calls, so that no other thread
- * changes the terminal between them. The section is a spin lock: keep it
- * short. The thread that holds it can call every function, and it can enter
- * the section again. */
+ * changes the terminal between them. The section is a mutex, and a waiting
+ * thread sleeps. Keep it short. The thread that holds it can call every
+ * function, and it can enter the section again. Each term_lock() needs one
+ * term_unlock(). */
TERM_API void term_lock(struct term *t);
TERM_API void term_unlock(struct term *t);
-/* Change the size in cells. Returns false when memory runs out; the terminal
- * is then unchanged. The screen reflows to the new width. */
+/* Change the size in cells. Returns false, and the terminal is unchanged,
+ * when a component is below 1, when cols is above TERM_MAX_COLS, or when
+ * memory runs out. The screen reflows to the new width. */
TERM_API bool term_resize(struct term *t, struct term_vector2i size);
/* Replace the theme. A different theme also drops the colors that the
@@ -197,7 +227,12 @@ enum term_feed_flags {
TERM_FEED_ONLCR = 1u << 0,
};
-TERM_API void term_feed(struct term *t, const char *bytes, size_t len, unsigned flags);
+/*
+ * Feed bytes from the program. Returns false, and changes nothing, when t is
+ * NULL, when bytes is NULL and len is not 0, or when flags has a bit that is
+ * not in enum term_feed_flags. A len of 0 is a no-op that returns true.
+ */
+TERM_API bool term_feed(struct term *t, const char *bytes, size_t len, unsigned flags);
/* Report a focus change. The terminal replies only in focus-event mode. */
TERM_API void term_focus(struct term *t, bool focused);
@@ -408,11 +443,16 @@ struct term_view {
const uint32_t *palette; /* TERM_PALETTE_SIZE ARGB entries */
};
+/* The view of s, NULL for a NULL s. The view and view->palette keep their
+ * address until term_snapshot_free(); their contents change on each update. */
TERM_API const struct term_view *term_snapshot_view(const struct term_snapshot *s);
-/* The cells of viewport row y: view->size.x entries. */
+/* The cells of viewport row y: view->size.x entries. NULL when y is outside
+ * the viewport or s is NULL. An update can move the cells, so the pointer is
+ * valid only until the next term_snapshot_update() or term_snapshot_free() of s. */
TERM_API const struct term_cell *term_snapshot_row(const struct term_snapshot *s, int y);
/* An identity for the content of row y. Two rows with the same key hold the
- * same cells, also when the content moved to another row. */
+ * same cells, also when the content moved to another row. 0 when y is outside
+ * the viewport or s is NULL. */
TERM_API uint64_t term_snapshot_row_key(const struct term_snapshot *s, int y);
/* Number of rows in history above the screen. 0 if alternate screen or no history. */
@@ -423,10 +463,13 @@ TERM_API int term_history_rows(const struct term *t);
* an empty string; NULL means allocation failed or t is NULL. Release the
* result with term_text_free(). */
TERM_API char *term_history_text(struct term *t, int max_rows);
+/* The selected columns of viewport row y, end exclusive. An empty span when y
+ * is outside the viewport or s is NULL. */
TERM_API struct term_span term_snapshot_row_selection(const struct term_snapshot *s, int y);
/* The code points of a cluster cell. Returns the number of code points and
- * writes at most cap of them. */
+ * writes at most cap of them. Writes nothing when cps is NULL or cap is 0.
+ * Returns 0 when s is NULL. */
TERM_API int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t *cps, int cap);
/* The ARGB value of a color in a view. */
diff --git a/include/term/term_input.h b/include/term/term_input.h
index c317bea..19d9355 100644
--- a/include/term/term_input.h
+++ b/include/term/term_input.h
@@ -1,5 +1,5 @@
-#ifndef TERM_INPUT_TYPES_H
-#define TERM_INPUT_TYPES_H
+#ifndef TERM_INPUT_H
+#define TERM_INPUT_H
#include <stdbool.h>
#include <stddef.h>
@@ -199,4 +199,4 @@ struct term_input_result {
}
#endif
-#endif /* TERM_INPUT_TYPES_H */
+#endif /* TERM_INPUT_H */
diff --git a/meson.build b/meson.build
index d35e800..e25bf13 100644
--- a/meson.build
+++ b/meson.build
@@ -15,6 +15,8 @@ project('term', 'c',
cc = meson.get_compiler('c')
m_dep = cc.find_library('m', required: false)
+# The writer lock is a pthread mutex on POSIX and an SRWLOCK on Windows.
+thread_dep = dependency('threads')
if get_option('arch_flag') != ''
add_project_arguments(get_option('arch_flag'), language: 'c')
endif
@@ -29,7 +31,7 @@ subdir('src')
term_dep = declare_dependency(
include_directories: term_inc,
link_with: term_lib.get_static_lib(),
- dependencies: m_dep,
+ dependencies: [m_dep, thread_dep],
)
meson.override_dependency('term', term_dep)
@@ -37,7 +39,7 @@ meson.override_dependency('term', term_dep)
term_internal_dep = declare_dependency(
include_directories: [term_inc, term_private_inc],
link_with: term_lib.get_static_lib(),
- dependencies: m_dep,
+ dependencies: [m_dep, thread_dep],
)
# The short forms of term.h that the term tests use. A host project can use
diff --git a/src/compat.h b/src/compat.h
index cb62679..6c3302f 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -14,6 +14,19 @@
#include <stdlib.h>
#include <string.h>
+#if defined(_WIN32)
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include <windows.h>
+#else
+#include <pthread.h>
+#include <unistd.h>
+#endif
+
#if defined(__GNUC__) || defined(__clang__)
#define TERM_COMPILER_GNU 1
#elif defined(_MSC_VER)
@@ -184,6 +197,68 @@ static inline void term_cpu_relax(void)
#endif
}
+/* The writer lock. POSIX uses a mutex with priority inheritance where the
+ * platform has it, so a waiter lifts the thread that holds the lock. Windows
+ * uses an SRWLOCK. Neither nests, so term_write_begin() counts re-entry. */
+#if defined(_WIN32)
+typedef SRWLOCK term_mutex;
+#else
+typedef pthread_mutex_t term_mutex;
+/* Strict C11 can hide PTHREAD_PRIO_INHERIT in pthread.h, as zig's aarch64
+ * glibc headers do. Every POSIX system that has the option uses 1 for it. */
+#if !defined(PTHREAD_PRIO_INHERIT)
+#define PTHREAD_PRIO_INHERIT 1
+#endif
+#endif
+
+/* Returns false when the lock cannot be made. */
+static inline bool term_mutex_init(term_mutex *m)
+{
+#if defined(_WIN32)
+ InitializeSRWLock(m);
+ return true;
+#elif defined(_POSIX_THREAD_PRIO_INHERIT) && _POSIX_THREAD_PRIO_INHERIT > 0
+ pthread_mutexattr_t attr;
+ if (pthread_mutexattr_init(&attr) != 0)
+ return false;
+ bool ok = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) == 0 &&
+ pthread_mutex_init(m, &attr) == 0;
+ pthread_mutexattr_destroy(&attr);
+ return ok;
+#else
+ return pthread_mutex_init(m, NULL) == 0;
+#endif
+}
+
+/* A normal mutex fails only when it is broken, so the result is ignored. */
+static inline void term_mutex_lock(term_mutex *m)
+{
+#if defined(_WIN32)
+ AcquireSRWLockExclusive(m);
+#else
+ (void)pthread_mutex_lock(m);
+#endif
+}
+
+static inline void term_mutex_unlock(term_mutex *m)
+{
+#if defined(_WIN32)
+ ReleaseSRWLockExclusive(m);
+#else
+ (void)pthread_mutex_unlock(m);
+#endif
+}
+
+/* An SRWLOCK has no destroy. */
+static inline void term_mutex_destroy(term_mutex *m)
+{
+#if defined(_WIN32)
+ (void)m;
+#else
+ (void)pthread_mutex_destroy(m);
+#endif
+}
+
/* strdup is POSIX, so the core copies strings itself. */
static inline char *term_strdup(const char *s)
{
diff --git a/src/escape.c b/src/escape.c
index da0449a..dceb7a6 100644
--- a/src/escape.c
+++ b/src/escape.c
@@ -834,7 +834,9 @@ static inline TERM_ALWAYS_INLINE void dispatch_csi(struct term *t, const struct
case 'q': {
if (csi->prefix == '>' && !csi->intermediate &&
(!csi->nparams || !csi->params[0])) {
- send_response(t, "\033P>|wsap_term\033\\");
+ char resp[96];
+ snprintf(resp, sizeof(resp), "\033P>|%s\033\\", t->name);
+ send_response(t, resp);
} else if (!csi->prefix && csi->intermediate == ' ') {
term_sc_set_cursor_style(t, csi->nparams > 0 ? csi->params[0] : 0);
}
diff --git a/src/internal.h b/src/internal.h
index 8c02c91..676413c 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -108,7 +108,8 @@ struct resize_scratch {
struct term {
- _Atomic uint64_t seq;
+ /* The writer section. A waiting thread sleeps on it. */
+ term_mutex write_lock;
_Atomic uint64_t writer_tid;
uint32_t write_depth;
int cols;
@@ -208,6 +209,8 @@ struct term {
bool custom_tabs;
char osc_buf[256];
+ /* The host's name. A DCS sequence can change tcap_name, not this. */
+ char name[64];
char tcap_name[64];
char *osc_heap;
@@ -375,18 +378,7 @@ static inline void term_write_begin(struct term *t)
t->write_depth++;
return;
}
- uint64_t s;
- for (;;) {
- s = atomic_load_explicit(&t->seq, memory_order_relaxed);
- if (s & 1) {
- term_cpu_relax();
- continue;
- }
- if (atomic_compare_exchange_weak_explicit(&t->seq, &s, s + 1,
- memory_order_acquire,
- memory_order_relaxed))
- break;
- }
+ term_mutex_lock(&t->write_lock);
atomic_store_explicit(&t->writer_tid, self, memory_order_relaxed);
t->write_depth = 1;
}
@@ -396,7 +388,7 @@ static inline void term_write_end(struct term *t)
if (!t) return;
if (--t->write_depth == 0) {
atomic_store_explicit(&t->writer_tid, 0, memory_order_relaxed);
- atomic_fetch_add_explicit(&t->seq, 1, memory_order_release);
+ term_mutex_unlock(&t->write_lock);
}
}
diff --git a/src/meson.build b/src/meson.build
index 694ee0e..7684275 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,5 +1,9 @@
subdir('osc')
+# The SONAME carries the major version. An ABI break bumps it, so an old
+# binary keeps the old library.
+term_soversion = meson.project_version().split('.')[0]
+
# The x86-64 SIMD kernels pick their level at run time (see simd.h), so the
# library needs no ISA flags. -march in c_args sets the lowest level.
term_lib = both_libraries('term',
@@ -21,8 +25,10 @@ term_lib = both_libraries('term',
'text_run.c',
term_osc_src,
include_directories: term_private_inc,
- dependencies: [m_dep],
+ dependencies: [m_dep, thread_dep],
gnu_symbol_visibility: 'hidden',
+ version: meson.project_version(),
+ soversion: term_soversion,
install: true
)
diff --git a/src/osc/misc.c b/src/osc/misc.c
index 5d6b506..74b35cf 100644
--- a/src/osc/misc.c
+++ b/src/osc/misc.c
@@ -53,7 +53,7 @@ void term_osc_misc_dispatch(struct term *t, long code, const char *params)
break;
case 9:
if (t->cb.notify && strncmp(params, "4;", 2))
- t->cb.notify(t->cb.ud, "wsap_term", params);
+ t->cb.notify(t->cb.ud, t->name, params);
break;
case 22:
free(t->pointer_shape);
diff --git a/src/screen.c b/src/screen.c
index 87d6f2c..b99948d 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -808,12 +808,11 @@ void term_screen_set_alternate(struct term *t, bool enabled)
}
}
+/* The caller has checked that requested is not negative. */
static int scrollback_lines(int requested)
{
if (requested == 0)
return TERM_SCROLLBACK_DEFAULT;
- if (requested < 0)
- return 0;
return requested > 1000000 ? 1000000 : requested;
}
@@ -841,22 +840,45 @@ bool term_tables_ready(void)
return atomic_load_explicit(&tables_state, memory_order_relaxed) == 2;
}
+/* A name is 1 to 63 printable ASCII bytes. DCS replies echo it. */
+static bool name_valid(const char *s)
+{
+ for (size_t i = 0; s[i]; i++) {
+ unsigned char c = (unsigned char)s[i];
+ if (i >= 63 || c < 0x20 || c > 0x7e)
+ return false;
+ }
+ return true;
+}
+
struct term *term_new(const struct term_options *opt)
{
tables_init();
static const struct term_options defaults;
if (!opt)
opt = &defaults;
- int cols = opt->size.x < 1 ? 80 : opt->size.x;
- int rows = opt->size.y < 1 ? 24 : opt->size.y;
- if (cols > TERM_MAX_COLS)
- cols = TERM_MAX_COLS;
+ for (size_t i = 0; i < sizeof(opt->reserved) / sizeof(opt->reserved[0]); i++)
+ if (opt->reserved[i])
+ return NULL;
+ if (opt->size.x < 0 || opt->size.y < 0 || opt->size.x > TERM_MAX_COLS)
+ return NULL;
+ if (opt->scrollback < 0 || (unsigned)opt->cursor_style > TERM_CURSOR_BAR)
+ return NULL;
+ const char *name = opt->name && *opt->name ? opt->name : "term";
+ if (!name_valid(name))
+ return NULL;
+ int cols = opt->size.x == 0 ? 80 : opt->size.x;
+ int rows = opt->size.y == 0 ? 24 : opt->size.y;
static _Atomic uint64_t next_serial = 1;
struct term *t = calloc(1, sizeof(*t));
if (!t)
return NULL;
- atomic_init(&t->seq, 0);
+ if (!term_mutex_init(&t->write_lock)) {
+ free(t);
+ return NULL;
+ }
+ memcpy(t->name, name, strlen(name) + 1);
atomic_init(&t->writer_tid, 0);
t->write_depth = 0;
t->serial = atomic_fetch_add_explicit(&next_serial, 1, memory_order_relaxed);
@@ -871,11 +893,13 @@ struct term *term_new(const struct term_options *opt)
t->next_row_uid = 1;
if (!screen_setup(&t->scr, cols, rows, &t->next_row_uid)) {
+ term_mutex_destroy(&t->write_lock);
free(t);
return NULL;
}
if (!term_hist_init(&t->hist, t->sb_capacity, 0)) {
screen_free(&t->scr);
+ term_mutex_destroy(&t->write_lock);
free(t);
return NULL;
}
@@ -895,14 +919,14 @@ struct term *term_new(const struct term_options *opt)
t->cur_fg = TERM_COLOR_FG;
t->cur_bg = TERM_COLOR_BG;
t->cursor_visible = true;
- t->default_cursor_style = opt->cursor_style > TERM_CURSOR_BAR ? TERM_CURSOR_BLOCK : opt->cursor_style;
+ t->default_cursor_style = opt->cursor_style;
t->default_cursor_blink = !opt->cursor_steady;
t->cursor_style = t->default_cursor_style;
t->cursor_blink = t->default_cursor_blink;
t->autowrap = true;
t->charsets[0] = t->charsets[1] = 'B';
t->saved = term_saved_cursor_initial();
- memcpy(t->tcap_name, "wsap_term", sizeof("wsap_term"));
+ memcpy(t->tcap_name, t->name, sizeof(t->name));
term_tabs_default(t);
t->full_gen = 1;
memset(t->text_width.lo, 0xFF, sizeof(t->text_width.lo));
@@ -932,6 +956,7 @@ void term_free(struct term *t)
free(t->tabs);
term_osc_cleanup(t);
term_gstore_free(t->gstore);
+ term_mutex_destroy(&t->write_lock);
free(t);
}
@@ -1523,10 +1548,8 @@ bool term_resize(struct term *t, struct term_vector2i size)
return false;
int cols = size.x;
int rows = size.y;
- if (cols < 1 || rows < 1)
+ if (cols < 1 || rows < 1 || cols > TERM_MAX_COLS)
return false;
- if (cols > TERM_MAX_COLS)
- cols = TERM_MAX_COLS;
term_write_begin(t);
if (cols == t->cols && rows == t->rows) {
term_write_end(t);
diff --git a/src/snapshot.c b/src/snapshot.c
index d763425..0bbf545 100644
--- a/src/snapshot.c
+++ b/src/snapshot.c
@@ -314,34 +314,36 @@ bool term_snapshot_update(struct term_snapshot *s, struct term *t)
const struct term_view *term_snapshot_view(const struct term_snapshot *s)
{
- return &s->view;
+ return s ? &s->view : NULL;
}
const struct term_cell *term_snapshot_row(const struct term_snapshot *s, int y)
{
- if (y < 0 || y >= s->view.size.y)
+ if (!s || y < 0 || y >= s->view.size.y)
return NULL;
return s->cells + (size_t)y * s->view.size.x;
}
uint64_t term_snapshot_row_key(const struct term_snapshot *s, int y)
{
- if (y < 0 || y >= s->view.size.y)
+ if (!s || y < 0 || y >= s->view.size.y)
return 0;
return s->keys[y];
}
struct term_span term_snapshot_row_selection(const struct term_snapshot *s, int y)
{
- if (y < 0 || y >= s->view.size.y)
+ if (!s || y < 0 || y >= s->view.size.y)
return (struct term_span){ 0 };
return s->selection[y];
}
int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t *cps, int cap)
{
+ if (!s)
+ return 0;
if (!(text & TERM_TEXT_CLUSTER)) {
- if (cap > 0)
+ if (cps && cap > 0)
cps[0] = text;
return 1;
}
@@ -350,7 +352,7 @@ int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t
return 0;
int n = (int)s->arena[off];
int copy = n < cap ? n : cap;
- if (copy > 0)
+ if (cps && copy > 0)
memcpy(cps, &s->arena[off + 1], (size_t)copy * sizeof(*cps));
return n;
}
diff --git a/src/term.c b/src/term.c
index bd0394e..5839da3 100644
--- a/src/term.c
+++ b/src/term.c
@@ -8,6 +8,11 @@ const char *term_unicode_version(void)
return TERM_UNICODE_DATA_VERSION;
}
+unsigned term_version(void)
+{
+ return TERM_VERSION_CODE;
+}
+
void term_lock(struct term *t)
{
term_write_begin(t);
diff --git a/src/text.c b/src/text.c
index e41a2e5..17f3f4f 100644
--- a/src/text.c
+++ b/src/text.c
@@ -190,10 +190,11 @@ static inline bool try_text_run(struct term *t, const char *buf, size_t len,
return wrote;
}
+/* The feed flags this build knows. term_feed() rejects any other bit. */
+#define TERM_FEED_KNOWN TERM_FEED_ONLCR
+
static void term_feed_inner(struct term *t, const char *buf, size_t len, unsigned flags)
{
- if (!t || !buf || len == 0)
- return;
t->transport_newline = (flags & TERM_FEED_ONLCR) != 0;
uint16_t cur_flags = term_current_flags(t);
@@ -366,13 +367,16 @@ static void term_feed_inner(struct term *t, const char *buf, size_t len, unsigne
}
}
-void term_feed(struct term *t, const char *buf, size_t len, unsigned flags)
+bool term_feed(struct term *t, const char *buf, size_t len, unsigned flags)
{
- if (!t || !buf || len == 0)
- return;
+ if (!t || (!buf && len > 0) || (flags & ~TERM_FEED_KNOWN))
+ return false;
+ if (len == 0)
+ return true;
term_write_begin(t);
term_feed_inner(t, buf, len, flags);
term_write_end(t);
+ return true;
}
void term_text_repeat(struct term *t, int count)
diff --git a/tests/api_args_test.c b/tests/api_args_test.c
new file mode 100644
index 0000000..18af728
--- /dev/null
+++ b/tests/api_args_test.c
@@ -0,0 +1,140 @@
+#include "term_api.h"
+#include <assert.h>
+#include <stdbool.h>
+#include <string.h>
+
+/* A feed that returns false must not move the cursor or change a cell. */
+static void expect_unchanged(struct term *t, int x, int y, uint32_t cp)
+{
+ assert(tt_cursor_x(t) == x);
+ assert(tt_cursor_y(t) == y);
+ assert(tt_cell(t, 0, 0)->text == cp);
+}
+
+static void test_feed_unknown_flag(void)
+{
+ struct term *t = tt_new(10, 3);
+ assert(t);
+ assert(term_feed(t, "abc", 3, 0));
+ expect_unchanged(t, 3, 0, 'a');
+
+ assert(!term_feed(t, "xy", 2, 1u << 1));
+ expect_unchanged(t, 3, 0, 'a');
+ assert(!term_feed(t, "xy", 2, TERM_FEED_ONLCR | (1u << 4)));
+ expect_unchanged(t, 3, 0, 'a');
+ assert(!term_feed(t, "xy", 2, 1u << 31));
+ expect_unchanged(t, 3, 0, 'a');
+ /* Unknown bits are rejected even when there are no bytes. */
+ assert(!term_feed(t, "", 0, 1u << 1));
+
+ assert(term_feed(t, "\nz", 2, TERM_FEED_ONLCR));
+ assert(tt_cursor_x(t) == 1);
+ assert(tt_cursor_y(t) == 1);
+ term_free(t);
+}
+
+static void test_feed_null_buffer(void)
+{
+ struct term *t = tt_new(10, 3);
+ assert(t);
+ assert(term_feed(t, "abc", 3, 0));
+
+ assert(!term_feed(t, NULL, 1, 0));
+ expect_unchanged(t, 3, 0, 'a');
+ assert(!term_feed(t, NULL, 4096, TERM_FEED_ONLCR));
+ expect_unchanged(t, 3, 0, 'a');
+
+ /* A zero length is a no-op, so a NULL buffer is fine. */
+ assert(term_feed(t, NULL, 0, 0));
+ assert(term_feed(t, "", 0, TERM_FEED_ONLCR));
+ expect_unchanged(t, 3, 0, 'a');
+
+ assert(!term_feed(NULL, "a", 1, 0));
+ assert(!term_feed(NULL, NULL, 0, 0));
+ term_free(t);
+}
+
+static void test_new_rejects_bad_options(void)
+{
+ struct term_options opt;
+
+ memset(&opt, 0, sizeof(opt));
+ opt.size.x = -1;
+ assert(!term_new(&opt));
+
+ memset(&opt, 0, sizeof(opt));
+ opt.size.y = -1;
+ assert(!term_new(&opt));
+
+ memset(&opt, 0, sizeof(opt));
+ opt.size.x = TERM_MAX_COLS + 1;
+ assert(!term_new(&opt));
+
+ memset(&opt, 0, sizeof(opt));
+ opt.scrollback = -1;
+ assert(!term_new(&opt));
+
+ memset(&opt, 0, sizeof(opt));
+ opt.cursor_style = (enum term_cursor_style)3;
+ assert(!term_new(&opt));
+
+ memset(&opt, 0, sizeof(opt));
+ opt.cursor_style = (enum term_cursor_style)-1;
+ assert(!term_new(&opt));
+}
+
+static void test_new_accepts_defaults(void)
+{
+ struct term_options opt;
+
+ memset(&opt, 0, sizeof(opt));
+ struct term *t = term_new(&opt);
+ assert(t);
+ assert(tt_cols(t) == 80);
+ assert(tt_rows(t) == 24);
+ assert(term_cursor(t).style == TERM_CURSOR_BLOCK);
+ term_free(t);
+
+ memset(&opt, 0, sizeof(opt));
+ opt.size.x = TERM_MAX_COLS;
+ opt.size.y = 1;
+ opt.cursor_style = TERM_CURSOR_BAR;
+ t = term_new(&opt);
+ assert(t);
+ assert(tt_cols(t) == TERM_MAX_COLS);
+ assert(tt_rows(t) == 1);
+ assert(term_cursor(t).style == TERM_CURSOR_BAR);
+ term_free(t);
+}
+
+static void test_resize_rejects_too_wide(void)
+{
+ struct term *t = tt_new(10, 3);
+ assert(t);
+ assert(term_feed(t, "abc", 3, 0));
+
+ assert(!tt_resize(t, TERM_MAX_COLS + 1, 3));
+ assert(tt_cols(t) == 10);
+ assert(tt_rows(t) == 3);
+ expect_unchanged(t, 3, 0, 'a');
+
+ assert(!tt_resize(t, 10, 0));
+ assert(!tt_resize(t, 0, 3));
+ assert(tt_cols(t) == 10);
+ assert(tt_rows(t) == 3);
+
+ assert(tt_resize(t, TERM_MAX_COLS, 3));
+ assert(tt_cols(t) == TERM_MAX_COLS);
+ assert(tt_rows(t) == 3);
+ term_free(t);
+}
+
+int main(void)
+{
+ test_feed_unknown_flag();
+ test_feed_null_buffer();
+ test_new_rejects_bad_options();
+ test_new_accepts_defaults();
+ test_resize_rejects_too_wide();
+ return 0;
+}
diff --git a/tests/dcs_test.c b/tests/dcs_test.c
index 0e67f78..d9455be 100644
--- a/tests/dcs_test.c
+++ b/tests/dcs_test.c
@@ -176,8 +176,8 @@ static void test_xtgettcap(void)
{
struct term *t = new_term(80, 24);
assert_tcap(t, "kind", "\033[1;2B");
- assert_tcap(t, "TN", "wsap_term");
- assert_tcap(t, "name", "wsap_term");
+ assert_tcap(t, "TN", "term");
+ assert_tcap(t, "name", "term");
assert_tcap(t, "Co", "16777216");
assert_tcap(t, "colors", "16777216");
assert_tcap(t, "RGB", "8/8/8");
@@ -230,7 +230,7 @@ static void test_xtgettcap_multi(void)
snprintf(req, sizeof(req), "\033P+q%s;%s\033\\", hk, htn);
snprintf(want, sizeof(want), "\033P1+r%s=%s;%s=%s\033\\",
- hk, hv, htn, "777361705f7465726d");
+ hk, hv, htn, "7465726d");
response_len = 0;
write_seq(t, req);
expect_response(want);
@@ -383,7 +383,51 @@ static void test_xtversion(void)
struct term *t = new_term(80, 24);
response_len = 0;
write_seq(t, "\033[>0q");
- expect_response("\033P>|wsap_term\033\\");
+ expect_response("\033P>|term\033\\");
+ term_free(t);
+}
+
+/* term_options.name feeds XTVERSION and the TN and name capabilities. */
+static void test_host_name(void)
+{
+ struct term_options opt = {
+ .size = { .x = 80, .y = 24 },
+ .scrollback = TEST_SCROLLBACK,
+ .name = "myterm",
+ .callbacks = { .reply = respond },
+ };
+ struct term *t = term_new(&opt);
+ assert(t);
+ response_len = 0;
+ write_seq(t, "\033[>0q");
+ expect_response("\033P>|myterm\033\\");
+ assert_tcap(t, "TN", "myterm");
+ assert_tcap(t, "name", "myterm");
+
+ /* A DCS +p sets the capability value. The XTVERSION name does not change. */
+ response_len = 0;
+ write_seq(t, "\033P+p6f74686572\033\\");
+ expect_no_response();
+ assert_tcap(t, "TN", "other");
+ response_len = 0;
+ write_seq(t, "\033[>0q");
+ expect_response("\033P>|myterm\033\\");
+ term_free(t);
+
+ /* NULL and "" both select the default. */
+ opt.name = "";
+ t = term_new(&opt);
+ assert(t);
+ response_len = 0;
+ write_seq(t, "\033[>0q");
+ expect_response("\033P>|term\033\\");
+ term_free(t);
+ opt.name = NULL;
+ t = term_new(&opt);
+ assert(t);
+ response_len = 0;
+ write_seq(t, "\033[>0q");
+ expect_response("\033P>|term\033\\");
term_free(t);
}
@@ -467,6 +511,7 @@ int main(void)
test_synchronized_output();
test_ignored_dcs();
test_xtversion();
+ test_host_name();
test_bel_terminator();
test_dcs_long_query();
test_dcs_abort_c0();
diff --git a/tests/meson.build b/tests/meson.build
index 69f3c49..4fda075 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -30,6 +30,7 @@ term_tests = [
['term-resize-failure-test', files('resize_failure_test.c'), [dl_dep], ['extlib']],
['dcs-test', files('dcs_test.c'), [], []],
['osc-test', files('osc_test.c'), [], []],
+ ['api-args-test', files('api_args_test.c'), [], []],
]
test_helpers_inc = include_directories('helpers')
@@ -76,20 +77,20 @@ term_amalgamation = custom_target(
term_amalgamation_lib = static_library(
'term-amalgamation',
term_amalgamation[0],
- dependencies: [m_dep],
+ dependencies: [m_dep, thread_dep],
override_options: ['c_std=c11', 'warning_level=3', 'werror=true'],
build_by_default: false,
)
foreach entry : term_tests
if entry[0] in ['term-test', 'spec-test', 'view-test', 'dcs-test', 'osc-test',
'history-text-test', 'term-input-test', 'model-test', 'model-row-test',
- 'resize-property-test']
+ 'resize-property-test', 'api-args-test']
test(entry[0] + '-amalgamation',
executable(entry[0] + '-amalgamation', entry[1],
c_args: test_c_args,
include_directories: [term_inc, test_helpers_inc],
link_with: term_amalgamation_lib,
- dependencies: [m_dep] + entry[2],
+ dependencies: [m_dep, thread_dep] + entry[2],
build_by_default: false),
env: test_env, suite: ['unit', 'amalgamation'], timeout: 180)
endif
@@ -117,6 +118,17 @@ if cc.has_link_argument('-Wl,--wrap=realloc') and cc.has_link_argument('-Wl,--wr
suite: 'unit',
)
endif
+
+# The project version comes in as a define, so the header macros cannot
+# drift from meson.build.
+version_test = executable('version-test', 'version_test.c',
+ c_args: test_c_args + ['-DTERM_TEST_PROJECT_VERSION="@0@"'.format(meson.project_version())],
+ dependencies: [term_internal_dep],
+)
+test('version-test', version_test,
+ env: test_env,
+ suite: 'unit',
+)
test('term-cross-test', bash,
args: [files('cross_test.sh'), meson.project_source_root()],
suite: 'portability',
diff --git a/tests/model.c b/tests/model.c
index 7934d4f..6c9f288 100644
--- a/tests/model.c
+++ b/tests/model.c
@@ -72,6 +72,7 @@ enum { GROUND, ESC, CSI, STR, STR_ESC, CHARSET, HASH, DOCS };
enum { OSC, DCS, IGNORE };
struct model {
+ char name[64]; /* XTVERSION reply */
int cols, rows, limit;
struct row **scr;
struct row **hist;
@@ -1290,7 +1291,7 @@ static void resize_plain(struct model *m, int cols, int rows)
static bool resize(struct model *m, int cols, int rows)
{
- if (cols > TERM_MAX_COLS) cols = TERM_MAX_COLS;
+ if (cols > TERM_MAX_COLS) return false;
if (cols == m->cols && rows == m->rows) return true;
if (cols > MAX_TABS) return false;
m->top = 0;
@@ -1625,7 +1626,7 @@ static void csi_dispatch(struct model *m, unsigned char final)
break;
case 'q':
if (m->prefix == '>' && !m->inter && (!m->nparam || !m->param[0])) {
- reply(m, "\033P>|wsap_term\033\\");
+ reply(m, "\033P>|%s\033\\", m->name);
} else if (!m->prefix && m->inter == ' ') {
int ps = m->nparam ? m->param[0] : 0;
if (ps == 0) {
@@ -1971,10 +1972,11 @@ static void feed_byte(struct model *m, unsigned char c)
/* ---- public ---- */
-struct model *model_new(struct term_vector2i size, int history)
+struct model *model_new(struct term_vector2i size, int history, const char *name)
{
struct model *m = calloc(1, sizeof(*m));
if (!m) abort();
+ snprintf(m->name, sizeof(m->name), "%s", name && *name ? name : "term");
m->cols = size.x < 1 ? 80 : size.x;
m->rows = size.y < 1 ? 24 : size.y;
m->limit = history_limit(history);
diff --git a/tests/model.h b/tests/model.h
index a6b5572..ca2bf96 100644
--- a/tests/model.h
+++ b/tests/model.h
@@ -28,8 +28,9 @@ struct model_cell {
struct model;
-/* history is the history limit, as in term_options.scrollback. */
-struct model *model_new(struct term_vector2i size, int history);
+/* history is the history limit, as in term_options.scrollback. name is
+ * as in term_options.name. */
+struct model *model_new(struct term_vector2i size, int history, const char *name);
void model_free(struct model *m);
void model_feed(struct model *m, const char *s, size_t len, unsigned flags);
bool model_resize(struct model *m, struct term_vector2i size);
diff --git a/tests/model_row_test.c b/tests/model_row_test.c
index 7afca05..ad4ac2c 100644
--- a/tests/model_row_test.c
+++ b/tests/model_row_test.c
@@ -5,7 +5,7 @@
int main(void)
{
- struct model *m = model_new((struct term_vector2i){ .x = 10, .y = 3 }, 10);
+ struct model *m = model_new((struct term_vector2i){ .x = 10, .y = 3 }, 10, NULL);
model_feed(m, "A\r\nB\r\nC\r\nD", strlen("A\r\nB\r\nC\r\nD"), 0);
assert(model_history(m) == 1);
assert(model_row(m, -1) == NULL);
diff --git a/tests/model_test.c b/tests/model_test.c
index 6af30b5..d3988c6 100644
--- a/tests/model_test.c
+++ b/tests/model_test.c
@@ -256,7 +256,7 @@ static int run(const struct script *s, enum chunking chunks, uint64_t chunk_seed
.callbacks = { .ud = &got, .reply = on_reply },
};
struct term *t = term_new(&opt);
- struct model *m = model_new(s->size, s->history);
+ struct model *m = model_new(s->size, s->history, NULL);
struct term_snapshot *snap = term_snapshot_new();
if (!t || !snap) abort();
uint64_t saved_rng = rng;
diff --git a/tests/osc_test.c b/tests/osc_test.c
index 41a9247..9d08157 100644
--- a/tests/osc_test.c
+++ b/tests/osc_test.c
@@ -104,6 +104,35 @@ static void reset_captures(void)
notify_count = 0;
}
+/* The notification default is term_options.name. A DCS +p does not change it. */
+static void test_host_name_notify(void)
+{
+ struct term_options opt = {
+ .size = { .x = 80, .y = 24 },
+ .scrollback = TEST_SCROLLBACK,
+ .name = "myterm",
+ .callbacks = { .notify = capture_notify },
+ };
+ struct term *t = term_new(&opt);
+ assert(t);
+ reset_captures();
+ write_seq(t, "\033]9;hello\007");
+ assert(notify_count == 1);
+ assert(!strcmp(notify_title, "myterm"));
+ assert(!strcmp(notify_body, "hello"));
+
+ write_seq(t, "\033P+p6f74686572\033\\");
+ reset_captures();
+ write_seq(t, "\033]9;again\007");
+ assert(notify_count == 1);
+ assert(!strcmp(notify_title, "myterm"));
+
+ write_seq(t, "\033]777;notify;Title;Body\007");
+ assert(notify_count == 2);
+ assert(!strcmp(notify_title, "Title"));
+ term_free(t);
+}
+
static void test_osc52(void)
{
struct term *t = osc_test_term();
@@ -289,7 +318,7 @@ static void test_osc_misc(void)
write_seq(t, "\033]9;hello there\033\\");
assert(notify_count == 1);
- assert(!strcmp(notify_title, "wsap_term"));
+ assert(!strcmp(notify_title, "term"));
assert(!strcmp(notify_body, "hello there"));
write_seq(t, "\033]9;4;3;50\007");
assert(notify_count == 1);
@@ -450,6 +479,7 @@ int main(void)
test_osc8();
test_sgr_reset_keeps_link();
test_osc_misc();
+ test_host_name_notify();
test_osc_colors();
test_osc_legacy_titles();
test_palette_and_theme();
diff --git a/tests/term_test.c b/tests/term_test.c
index 9dbe902..235e9df 100644
--- a/tests/term_test.c
+++ b/tests/term_test.c
@@ -4,6 +4,7 @@
#include <assert.h>
#include <limits.h>
#include <pthread.h>
+#include <sched.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
@@ -1440,6 +1441,55 @@ static void test_snapshot_follows_new_terminal(void)
term_snapshot_free(s);
}
+/* The getters take a NULL snapshot and an out-of-range row like a NULL
+ * terminal: they return the empty value. The view keeps its address. */
+static void test_snapshot_null_and_range(void)
+{
+ uint32_t cps[4] = { 0xAAAA, 0xAAAA, 0xAAAA, 0xAAAA };
+ assert(term_snapshot_view(NULL) == NULL);
+ assert(term_snapshot_row(NULL, 0) == NULL);
+ assert(term_snapshot_row_key(NULL, 0) == 0);
+ struct term_span none = term_snapshot_row_selection(NULL, 0);
+ assert(none.begin == 0 && none.end == 0);
+ assert(term_snapshot_cluster(NULL, 'a', cps, 4) == 0);
+ assert(term_snapshot_cluster(NULL, TERM_TEXT_CLUSTER, cps, 4) == 0);
+ assert(cps[0] == 0xAAAA);
+
+ struct term *t = tt_new(4, 2);
+ tt_write(t, "e\xcc\x81", 3);
+ struct term_snapshot *s = term_snapshot_new();
+ assert(term_snapshot_update(s, t));
+
+ const struct term_view *view = term_snapshot_view(s);
+ const uint32_t *palette = view->palette;
+ assert(term_snapshot_update(s, t));
+ assert(term_snapshot_view(s) == view && view->palette == palette);
+
+ assert(term_snapshot_row(s, -1) == NULL);
+ assert(term_snapshot_row(s, 2) == NULL);
+ assert(term_snapshot_row_key(s, -1) == 0);
+ assert(term_snapshot_row_key(s, 2) == 0);
+ struct term_span off = term_snapshot_row_selection(s, 2);
+ assert(off.begin == 0 && off.end == 0);
+
+ /* A cluster cell gives its count with no buffer, and with cap 0 it
+ * writes nothing. */
+ uint32_t text = term_snapshot_row(s, 0)[0].text;
+ assert(text & TERM_TEXT_CLUSTER);
+ assert(term_snapshot_cluster(s, text, NULL, 0) == 2);
+ assert(term_snapshot_cluster(s, text, cps, 0) == 2);
+ assert(cps[0] == 0xAAAA);
+ assert(term_snapshot_cluster(s, text, NULL, 4) == 2);
+ /* A plain code point is one code point, and cap 0 writes nothing. */
+ assert(term_snapshot_cluster(s, 'a', NULL, 0) == 1);
+ assert(term_snapshot_cluster(s, 'a', cps, 0) == 1);
+ assert(cps[0] == 0xAAAA);
+ assert(term_snapshot_cluster(s, 'a', cps, 4) == 1 && cps[0] == 'a');
+
+ term_snapshot_free(s);
+ term_free(t);
+}
+
static void test_string_payloads(void)
{
static const char *prefixes[] = {"\033]0;", "\033]7;file://host/", "\033P"};
@@ -1485,6 +1535,32 @@ static void test_string_payloads(void)
}
}
+/* term_new() refuses a name that a DCS reply could echo unsafely. */
+static void test_host_name_rejected(void)
+{
+ static const char *const bad[] = {
+ "\033x", "a\tb", "\177", "\300", "a\nb",
+ };
+ char longest[65];
+ memset(longest, 'a', 64);
+ longest[64] = 0;
+ char fits[64];
+ memset(fits, 'b', 63);
+ fits[63] = 0;
+
+ for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) {
+ struct term_options opt = { .size = { .x = 10, .y = 3 }, .name = bad[i] };
+ assert(!term_new(&opt));
+ }
+ struct term_options opt = { .size = { .x = 10, .y = 3 }, .name = longest };
+ assert(!term_new(&opt));
+
+ opt.name = fits;
+ struct term *t = term_new(&opt);
+ assert(t);
+ term_free(t);
+}
+
static void test_newline_policy_survives_reset(void)
{
static const char *const resets[] = { "\033c", "\033[!p", "\033[20l" };
@@ -1762,21 +1838,22 @@ struct concurrent_worker_args {
_Atomic bool done;
};
+static const char concurrent_stream[] =
+ "The quick brown fox jumps over the lazy dog.\r\n"
+ "\033[31mRed text\033[0m and \033[32mgreen text\033[0m.\r\n"
+ "Some unicode: 漢字, ひらがな, and emojis: 😀🚀🎉\r\n"
+ "\033[2J\033[HCleared and reset\r\n"
+ "Line 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\n"
+ "\033]7;file:///tmp/dir\007\033]22;pointer\007"
+ "\033]8;;https://example.com\007link\033]8;;\007\r\n";
+
static void *concurrent_feed_worker(void *arg)
{
struct concurrent_worker_args *w = arg;
- static const char stream[] =
- "The quick brown fox jumps over the lazy dog.\r\n"
- "\033[31mRed text\033[0m and \033[32mgreen text\033[0m.\r\n"
- "Some unicode: 漢字, ひらがな, and emojis: 😀🚀🎉\r\n"
- "\033[2J\033[HCleared and reset\r\n"
- "Line 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\n"
- "\033]7;file:///tmp/dir\007\033]22;pointer\007"
- "\033]8;;https://example.com\007link\033]8;;\007\r\n";
- size_t len = strlen(stream);
+ size_t len = strlen(concurrent_stream);
for (int i = 0; i < 2000; i++) {
- term_feed(w->t, stream, len, 0);
+ term_feed(w->t, concurrent_stream, len, 0);
}
atomic_store(&w->done, true);
return NULL;
@@ -1784,10 +1861,11 @@ static void *concurrent_feed_worker(void *arg)
static void test_concurrent_feed_and_snapshot(void)
{
- struct term *t = term_new(&(struct term_options){
+ struct term_options opt = {
.size = { .x = 80, .y = 24 },
.scrollback = 1000,
- });
+ };
+ struct term *t = term_new(&opt);
assert(t != NULL);
struct concurrent_worker_args w = { .t = t, .done = false };
@@ -1828,13 +1906,104 @@ static void test_concurrent_feed_and_snapshot(void)
bool ok = term_snapshot_update(snap, t);
assert(ok);
+ /* The reader changed only the view and the selection. The screen and
+ * the history must match a terminal that got the same bytes on one
+ * thread. */
+ struct term *ref = term_new(&opt);
+ assert(ref != NULL);
+ for (int i = 0; i < 2000; i++)
+ tt_write(ref, concurrent_stream, strlen(concurrent_stream));
+ assert(tt_cols(t) == tt_cols(ref) && tt_rows(t) == tt_rows(ref));
+ assert(tt_history(t) == tt_history(ref));
+ assert(tt_cursor_x(t) == tt_cursor_x(ref) && tt_cursor_y(t) == tt_cursor_y(ref));
+ char *got = malloc((size_t)tt_cols(t) + 1);
+ char *want = malloc((size_t)tt_cols(ref) + 1);
+ assert(got && want);
+ for (int y = 0; y < tt_rows(t); y++) {
+ row_text(t, y, got);
+ row_text(ref, y, want);
+ assert(!strcmp(got, want));
+ }
+ free(got);
+ free(want);
+ char *text = term_history_text(t, 1000);
+ char *text_ref = term_history_text(ref, 1000);
+ assert(text && text_ref && !strcmp(text, text_ref));
+ term_text_free(text);
+ term_text_free(text_ref);
+
+ term_free(ref);
term_snapshot_free(snap);
term_free(t);
}
+static void test_reserved_options_must_be_zero(void)
+{
+ struct term_options opt = { .size = { .x = 10, .y = 2 } };
+ struct term *t = term_new(&opt);
+ assert(t != NULL);
+ term_free(t);
+
+ size_t words = sizeof(opt.reserved) / sizeof(opt.reserved[0]);
+ for (size_t i = 0; i < words; i++) {
+ opt.reserved[i] = 1;
+ assert(term_new(&opt) == NULL);
+ opt.reserved[i] = 0;
+ }
+ opt.reserved[words - 1] = 0x80000000u;
+ assert(term_new(&opt) == NULL);
+ opt.reserved[words - 1] = 0;
+ t = term_new(&opt);
+ assert(t != NULL);
+ term_free(t);
+}
+
+struct lock_probe {
+ struct term *t;
+ _Atomic bool entered;
+};
+
+static void *lock_probe_worker(void *arg)
+{
+ struct lock_probe *p = arg;
+ term_lock(p->t);
+ atomic_store(&p->entered, true);
+ term_unlock(p->t);
+ return NULL;
+}
+
+/* term_lock() nests on the owning thread. Another thread waits until the
+ * last term_unlock(). */
+static void test_lock_reentrant_across_threads(void)
+{
+ struct term *t = term_new(&(struct term_options){
+ .size = { .x = 80, .y = 24 },
+ });
+ assert(t != NULL);
+
+ term_lock(t);
+ term_lock(t);
+ assert(tt_cols(t) == 80);
+ term_unlock(t);
+
+ struct lock_probe p = { .t = t, .entered = false };
+ pthread_t th;
+ assert(pthread_create(&th, NULL, lock_probe_worker, &p) == 0);
+ for (int i = 0; i < 1000; i++)
+ sched_yield();
+ assert(!atomic_load(&p.entered));
+ term_unlock(t);
+
+ assert(pthread_join(th, NULL) == 0);
+ assert(atomic_load(&p.entered));
+ term_free(t);
+}
+
int main(void)
{
+ test_reserved_options_must_be_zero();
test_concurrent_feed_and_snapshot();
+ test_lock_reentrant_across_threads();
test_grow_past_reservation();
test_skipped_cells_read_blank();
test_csi_text_chunks();
@@ -1859,7 +2028,9 @@ int main(void)
test_alt_shrink_then_flood();
test_alt_erase_scrollback();
test_snapshot_follows_new_terminal();
+ test_snapshot_null_and_range();
test_bulk_rows_survive_edits();
+ test_host_name_rejected();
assert(!strcmp(TERM_UNICODE_DATA_VERSION, "18.0.0"));
assert(!strcmp(term_unicode_version(), TERM_UNICODE_DATA_VERSION));
diff --git a/tests/version_test.c b/tests/version_test.c
new file mode 100644
index 0000000..0eb889e
--- /dev/null
+++ b/tests/version_test.c
@@ -0,0 +1,27 @@
+#include "term/term.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+/* The project version from meson.build. The build defines it. */
+#ifndef TERM_TEST_PROJECT_VERSION
+#error "define TERM_TEST_PROJECT_VERSION as the project version, such as \"0.1.0\""
+#endif
+
+int main(void)
+{
+ char text[32];
+ snprintf(text, sizeof(text), "%d.%d.%d",
+ TERM_VERSION_MAJOR, TERM_VERSION_MINOR, TERM_VERSION_PATCH);
+ assert(!strcmp(text, TERM_TEST_PROJECT_VERSION));
+
+ int major, minor, patch;
+ assert(sscanf(TERM_TEST_PROJECT_VERSION, "%d.%d.%d", &major, &minor, &patch) == 3);
+ assert(TERM_VERSION_CODE == TERM_VERSION(major, minor, patch));
+ assert(term_version() == TERM_VERSION_CODE);
+
+ assert(TERM_VERSION(0, 2, 0) > TERM_VERSION(0, 1, 255));
+ assert(TERM_VERSION(1, 0, 0) > TERM_VERSION(0, 255, 255));
+ return 0;
+}
diff --git a/tools/amalgamate.py b/tools/amalgamate.py
index f2cdf3b..09c6c91 100644
--- a/tools/amalgamate.py
+++ b/tools/amalgamate.py
@@ -3,7 +3,7 @@
term.h joins the public headers. term.c joins every source file and puts
each private header in at its first include. A host compiles term.c once,
-with any C11 compiler, and links libm.
+with any C11 compiler, and links libm and the thread library.
amalgamate.py TERM_DIR OUT_DIR [DEPFILE]