Compare commits
35 Commits
f04cc2c61e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
7566f9af82
|
|||
|
7425355d01
|
|||
|
d8f785eae5
|
|||
|
31ae400852
|
|||
|
b69d3f8692
|
|||
|
9a26284a64
|
|||
|
0f20cce9c8
|
|||
|
017cb1d570
|
|||
|
1642d387f3
|
|||
|
af889b733e
|
|||
|
51231d84ba
|
|||
|
6c47e384f5
|
|||
|
914e743dc4
|
|||
|
37a8f16235
|
|||
|
9fe576ae9e
|
|||
|
5c02778ff8
|
|||
|
e40d56152d
|
|||
|
21379d4c02
|
|||
|
9268fb8eba
|
|||
|
b01df19b80
|
|||
|
09e635cf97
|
|||
|
7560e8700e
|
|||
|
1930f138d4
|
|||
|
32cbb15266
|
|||
|
149938cc44
|
|||
|
62f8a7d05f
|
|||
|
492815c8fc
|
|||
|
aacf1b1d47
|
|||
|
49d7cb12bb
|
|||
|
fdf845d0bd
|
|||
|
75fc6f1c37
|
|||
|
8a8437634a
|
|||
|
e78b410a6a
|
|||
|
bf44e827e8
|
|||
|
8386af0420
|
@@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 3.0...3.27)
|
||||
project (liberty C CXX)
|
||||
|
||||
# Moar warnings
|
||||
set (CMAKE_CXX_STANDARD 11)
|
||||
if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
# -Wunused-function is pretty annoying here, as everything is static
|
||||
set (wdisabled "-Wno-unused-function")
|
||||
@@ -9,7 +10,7 @@ if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
endif ()
|
||||
|
||||
# Dependencies
|
||||
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
||||
set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
|
||||
include (AddThreads)
|
||||
|
||||
find_package (PkgConfig REQUIRED)
|
||||
@@ -34,7 +35,7 @@ foreach (extra iconv rt)
|
||||
endforeach ()
|
||||
|
||||
# Build some unit tests
|
||||
include_directories (${PROJECT_SOURCE_DIR})
|
||||
include_directories ("${PROJECT_SOURCE_DIR}")
|
||||
enable_testing ()
|
||||
set (tests liberty proto xdg)
|
||||
|
||||
@@ -56,7 +57,7 @@ endforeach ()
|
||||
# --- Tools --------------------------------------------------------------------
|
||||
|
||||
# Test the AsciiDoc manual page generator for a successful parse
|
||||
set (ASCIIMAN ${PROJECT_SOURCE_DIR}/tools/asciiman.awk)
|
||||
set (ASCIIMAN "${PROJECT_SOURCE_DIR}/tools/asciiman.awk")
|
||||
add_custom_command (OUTPUT libertyxdr.7
|
||||
COMMAND env LC_ALL=C awk -f ${ASCIIMAN}
|
||||
"${PROJECT_SOURCE_DIR}/libertyxdr.adoc" > libertyxdr.7
|
||||
@@ -64,10 +65,14 @@ add_custom_command (OUTPUT libertyxdr.7
|
||||
COMMENT "Generating man page for libertyxdr" VERBATIM)
|
||||
add_custom_target (docs ALL DEPENDS libertyxdr.7)
|
||||
|
||||
# Test the --help/--version to AsciiDoc convertor
|
||||
add_test (test-help2adoc
|
||||
env LC_ALL=C "${PROJECT_SOURCE_DIR}/tests/help2adoc.sh")
|
||||
|
||||
# Test CMake script parsing
|
||||
add_test (test-cmake-parser
|
||||
env LC_ALL=C awk -f ${PROJECT_SOURCE_DIR}/tools/cmake-parser.awk
|
||||
-f ${PROJECT_SOURCE_DIR}/tools/cmake-dump.awk ${CMAKE_CURRENT_LIST_FILE})
|
||||
env LC_ALL=C awk -f "${PROJECT_SOURCE_DIR}/tools/cmake-parser.awk"
|
||||
-f "${PROJECT_SOURCE_DIR}/tools/cmake-dump.awk" ${CMAKE_CURRENT_LIST_FILE})
|
||||
|
||||
# Test protocol code generation
|
||||
set (lxdrgen_outputs)
|
||||
@@ -76,15 +81,15 @@ foreach (backend c cpp go mjs swift)
|
||||
list (APPEND lxdrgen_outputs ${lxdrgen_base}.${backend})
|
||||
add_custom_command (OUTPUT ${lxdrgen_base}.${backend}
|
||||
COMMAND env LC_ALL=C awk
|
||||
-f ${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk
|
||||
-f ${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk
|
||||
-f "${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk"
|
||||
-f "${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk"
|
||||
-v PrefixCamel=ProtoGen
|
||||
${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr
|
||||
"${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr"
|
||||
> ${lxdrgen_base}.${backend}
|
||||
DEPENDS
|
||||
${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk
|
||||
${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk
|
||||
${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr
|
||||
"${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk"
|
||||
"${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk"
|
||||
"${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr"
|
||||
COMMENT "Generating test protocol code (${backend})" VERBATIM)
|
||||
endforeach ()
|
||||
add_custom_target (test-lxdrgen-outputs ALL DEPENDS ${lxdrgen_outputs})
|
||||
@@ -104,6 +109,7 @@ else ()
|
||||
add_executable (test-lxdrgen-cpp tests/lxdrgen.cpp
|
||||
${lxdrgen_base}.cpp tools/lxdrgen-cpp-posix.cpp)
|
||||
endif ()
|
||||
target_link_libraries (test-lxdrgen-cpp ${common_libraries})
|
||||
target_include_directories (test-lxdrgen-cpp PUBLIC ${PROJECT_BINARY_DIR})
|
||||
add_test (NAME test-lxdrgen-cpp COMMAND test-lxdrgen-cpp)
|
||||
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014 - 2024, Přemysl Eric Janouch <p@janouch.name>
|
||||
Copyright (c) 2014 - 2025, Přemysl Eric Janouch <p@janouch.name>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
14
README.adoc
14
README.adoc
@@ -15,8 +15,8 @@ mess that are header files.
|
||||
The API is intentionally unstable, which allows for easy refactoring.
|
||||
|
||||
All development is done on Linux, but other POSIX-compatible operating systems
|
||||
should be generally supported as well. They have an extremely low priority,
|
||||
however, and I'm not testing them at all, perhaps with the exception of macOS.
|
||||
should be generally supported as well. They have a lower priority, however,
|
||||
and don't receive as much testing.
|
||||
|
||||
Tools
|
||||
-----
|
||||
@@ -36,6 +36,10 @@ cmake-dump.awk::
|
||||
This can be used in conjunction with the previous script to dump CMake
|
||||
scripts in a normalized format for further processing.
|
||||
|
||||
help2adoc.awk::
|
||||
Produces AsciiDoc manual pages from --version/--help output.
|
||||
These can then be processed by _asciiman.awk_.
|
||||
|
||||
lxdrgen.awk::
|
||||
Protocol code generator for a variant of XDR,
|
||||
which is link:libertyxdr.adoc[documented separately].
|
||||
@@ -45,8 +49,9 @@ lxdrgen-c.awk::
|
||||
LibertyXDR backend that builds on top of the C pseudolibrary.
|
||||
|
||||
lxdrgen-cpp.awk::
|
||||
lxdrgen-cpp-win32.cpp::
|
||||
lxdrgen-cpp-posix.cpp::
|
||||
lxdrgen-cpp-qt.cpp::
|
||||
lxdrgen-cpp-win32.cpp::
|
||||
LibertyXDR backend for C++, primarily targeting Win32 and its wide strings.
|
||||
Link the result together with one of the accompanied source files.
|
||||
|
||||
@@ -63,6 +68,9 @@ lxdrgen-mjs.awk::
|
||||
lxdrgen-swift.awk::
|
||||
LibertyXDR backend for the Swift programming language.
|
||||
|
||||
wdye::
|
||||
Compiled Lua-based Expect-like utility, intended purely for build checks.
|
||||
|
||||
Contributing and Support
|
||||
------------------------
|
||||
Use https://git.janouch.name/p/liberty to report any bugs, request features,
|
||||
|
||||
@@ -38,3 +38,47 @@ function (icon_for_win32 ico pngs pngs_raw)
|
||||
DEPENDS ${pngs} ${pngs_raw}
|
||||
COMMENT "Generating Windows program icon" VERBATIM)
|
||||
endfunction ()
|
||||
|
||||
function (icon_to_iconset_size name svg size iconset outputs)
|
||||
math (EXPR _size2x "${size} * 2")
|
||||
set (_dimensions "${size}x${size}")
|
||||
set (_png1x "${iconset}/icon_${_dimensions}.png")
|
||||
set (_png2x "${iconset}/icon_${_dimensions}@2x.png")
|
||||
set (${outputs} "${_png1x};${_png2x}" PARENT_SCOPE)
|
||||
|
||||
set (_find_program_REQUIRE)
|
||||
if (NOT ${CMAKE_VERSION} VERSION_LESS 3.18.0)
|
||||
set (_find_program_REQUIRE REQUIRED)
|
||||
endif ()
|
||||
|
||||
find_program (rsvg_convert_EXECUTABLE rsvg-convert ${_find_program_REQUIRE})
|
||||
add_custom_command (OUTPUT "${_png1x}" "${_png2x}"
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory "${iconset}"
|
||||
COMMAND ${rsvg_convert_EXECUTABLE} "--output=${_png1x}"
|
||||
"--width=${size}" "--height=${size}" -- "${svg}"
|
||||
COMMAND ${rsvg_convert_EXECUTABLE} "--output=${_png2x}"
|
||||
"--width=${_size2x}" "--height=${_size2x}" -- "${svg}"
|
||||
DEPENDS "${svg}"
|
||||
COMMENT "Generating ${name} ${_dimensions} icons" VERBATIM)
|
||||
endfunction ()
|
||||
function (icon_to_icns svg output_basename output)
|
||||
get_filename_component (_name "${output_basename}" NAME_WE)
|
||||
set (_iconset "${PROJECT_BINARY_DIR}/${_name}.iconset")
|
||||
set (_icon "${PROJECT_BINARY_DIR}/${output_basename}")
|
||||
set (${output} "${_icon}" PARENT_SCOPE)
|
||||
|
||||
set (_icon_png_list)
|
||||
foreach (_icon_size 16 32 128 256 512)
|
||||
icon_to_iconset_size ("${_name}" "${svg}"
|
||||
"${_icon_size}" "${_iconset}" _icon_pngs)
|
||||
list (APPEND _icon_png_list ${_icon_pngs})
|
||||
endforeach ()
|
||||
|
||||
# XXX: This will not normally work from within Nix.
|
||||
add_custom_command (OUTPUT "${_icon}"
|
||||
COMMAND iconutil -c icns -o "${_icon}" "${_iconset}"
|
||||
DEPENDS ${_icon_png_list}
|
||||
COMMENT "Generating ${_name} icon" VERBATIM)
|
||||
set_source_files_properties ("${_icon}" PROPERTIES
|
||||
MACOSX_PACKAGE_LOCATION Resources)
|
||||
endfunction ()
|
||||
|
||||
@@ -1381,7 +1381,7 @@ struct mpd_client
|
||||
|
||||
// Protocol:
|
||||
|
||||
bool got_hello; ///< Got the OK MPD hello message
|
||||
char *got_hello; ///< Version from OK MPD hello message
|
||||
|
||||
bool idling; ///< Sent idle as the last command
|
||||
unsigned idling_subsystems; ///< Subsystems we're idling for
|
||||
@@ -1482,7 +1482,7 @@ mpd_client_reset (struct mpd_client *self)
|
||||
str_reset (&self->read_buffer);
|
||||
str_reset (&self->write_buffer);
|
||||
|
||||
self->got_hello = false;
|
||||
cstr_set (&self->got_hello, NULL);
|
||||
self->idling = false;
|
||||
self->idling_subsystems = 0;
|
||||
self->in_list = false;
|
||||
@@ -1549,7 +1549,8 @@ mpd_client_parse_hello (struct mpd_client *self, const char *line)
|
||||
|
||||
// TODO: call "on_connected" now. We should however also set up a timer
|
||||
// so that we don't wait on this message forever.
|
||||
return self->got_hello = true;
|
||||
cstr_set (&self->got_hello, xstrdup (line + sizeof hello - 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
@@ -1593,6 +1594,8 @@ mpd_client_parse_kv (char *line, char **value)
|
||||
static void
|
||||
mpd_client_update_poller (struct mpd_client *self)
|
||||
{
|
||||
if (self->state != MPD_CONNECTED)
|
||||
return;
|
||||
poller_fd_set (&self->socket_event,
|
||||
self->write_buffer.len ? (POLLIN | POLLOUT) : POLLIN);
|
||||
}
|
||||
@@ -1634,30 +1637,30 @@ mpd_client_on_ready (const struct pollfd *pfd, void *user_data)
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static bool
|
||||
mpd_client_must_quote_char (char c)
|
||||
{
|
||||
return (unsigned char) c <= ' ' || c == '"' || c == '\'';
|
||||
}
|
||||
|
||||
static bool
|
||||
mpd_client_must_quote (const char *s)
|
||||
{
|
||||
if (!*s)
|
||||
return true;
|
||||
for (; *s; s++)
|
||||
if (mpd_client_must_quote_char (*s))
|
||||
if ((unsigned char) *s <= ' ' || *s == '"' || *s == '\'')
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
mpd_client_must_escape_in_quote (char c)
|
||||
{
|
||||
return c == '"' || c == '\'' || c == '\\';
|
||||
}
|
||||
|
||||
static void
|
||||
mpd_client_quote (const char *s, struct str *output)
|
||||
{
|
||||
str_append_c (output, '"');
|
||||
for (; *s; s++)
|
||||
{
|
||||
if (mpd_client_must_quote_char (*s))
|
||||
if (mpd_client_must_escape_in_quote (*s))
|
||||
str_append_c (output, '\\');
|
||||
str_append_c (output, *s);
|
||||
}
|
||||
|
||||
@@ -86,24 +86,30 @@ xdg_xsettings_update (struct xdg_xsettings *self, Display *dpy)
|
||||
// TODO: We're supposed to lock the server.
|
||||
// TODO: We're supposed to trap X errors.
|
||||
char *selection = xstrdup_printf ("_XSETTINGS_S%d", DefaultScreen (dpy));
|
||||
Window owner
|
||||
= XGetSelectionOwner (dpy, XInternAtom (dpy, selection, True));
|
||||
Atom selection_atom = XInternAtom (dpy, selection, True);
|
||||
free (selection);
|
||||
if (!selection_atom)
|
||||
return;
|
||||
|
||||
Window owner = XGetSelectionOwner (dpy, selection_atom);
|
||||
if (!owner)
|
||||
return;
|
||||
|
||||
Atom xsettings_atom = XInternAtom (dpy, "_XSETTINGS_SETTINGS", True);
|
||||
if (!xsettings_atom)
|
||||
return;
|
||||
|
||||
Atom actual_type = None;
|
||||
int actual_format = 0;
|
||||
unsigned long nitems = 0, bytes_after = 0;
|
||||
unsigned char *buffer = NULL;
|
||||
Atom xsettings = XInternAtom (dpy, "_XSETTINGS_SETTINGS", True);
|
||||
int status = XGetWindowProperty (dpy,
|
||||
owner,
|
||||
xsettings,
|
||||
xsettings_atom,
|
||||
0L,
|
||||
LONG_MAX,
|
||||
False,
|
||||
xsettings,
|
||||
xsettings_atom,
|
||||
&actual_type,
|
||||
&actual_format,
|
||||
&nitems,
|
||||
@@ -112,7 +118,7 @@ xdg_xsettings_update (struct xdg_xsettings *self, Display *dpy)
|
||||
if (status != Success || !buffer)
|
||||
return;
|
||||
|
||||
if (actual_type != xsettings
|
||||
if (actual_type != xsettings_atom
|
||||
|| actual_format != 8
|
||||
|| nitems < 12)
|
||||
goto fail;
|
||||
|
||||
@@ -24,12 +24,12 @@
|
||||
// It is surprisingly hard to find a good library to handle Unicode shenanigans,
|
||||
// and there's enough of those for it to be impractical to reimplement them.
|
||||
//
|
||||
// GLib ICU libunistring utf8proc
|
||||
// Decently sized . . x x
|
||||
// Grapheme breaks . x . x
|
||||
// Character width x . x x
|
||||
// Locale handling . . x .
|
||||
// Liberal license . x . x
|
||||
// GLib ICU libunistring utf8proc libgrapheme
|
||||
// Decently sized . . x x x
|
||||
// Grapheme breaks . x . x x
|
||||
// Character width x . x x .
|
||||
// Locale handling . . x . .
|
||||
// Liberal license . x . x x
|
||||
//
|
||||
// Also note that the ICU API is icky and uses UTF-16 for its primary encoding.
|
||||
//
|
||||
@@ -205,8 +205,8 @@ static void
|
||||
line_editor_start (struct line_editor *self, char prompt)
|
||||
{
|
||||
self->alloc = 16;
|
||||
self->line = xcalloc (sizeof *self->line, self->alloc);
|
||||
self->w = xcalloc (sizeof *self->w, self->alloc);
|
||||
self->line = xcalloc (self->alloc, sizeof *self->line);
|
||||
self->w = xcalloc (self->alloc, sizeof *self->w);
|
||||
self->len = 0;
|
||||
self->point = 0;
|
||||
self->prompt = prompt;
|
||||
@@ -1884,6 +1884,8 @@ x11_init (struct poller *poller, struct attrs *app_attrs, size_t app_attrs_len)
|
||||
if (!(g_xui.dpy = XkbOpenDisplay
|
||||
(NULL, &g_xui.xkb_base_event_code, NULL, NULL, NULL, NULL)))
|
||||
exit_fatal ("cannot open display");
|
||||
if (!XftInit (NULL))
|
||||
print_warning ("Fontconfig initialization failed");
|
||||
if (!XftDefaultHasRender (g_xui.dpy))
|
||||
exit_fatal ("XRender is not supported");
|
||||
if (!(g_xui.x11_im = XOpenIM (g_xui.dpy, NULL, NULL, NULL)))
|
||||
@@ -1912,8 +1914,6 @@ x11_init (struct poller *poller, struct attrs *app_attrs, size_t app_attrs_len)
|
||||
g_xui.x11_xsettings = xdg_xsettings_make ();
|
||||
xdg_xsettings_update (&g_xui.x11_xsettings, g_xui.dpy);
|
||||
|
||||
if (!FcInit ())
|
||||
print_warning ("Fontconfig initialization failed");
|
||||
if (!(g_xui.xft_fonts = x11_font_open (0)))
|
||||
exit_fatal ("cannot open a font");
|
||||
|
||||
|
||||
27
liberty.c
27
liberty.c
@@ -292,7 +292,8 @@ xreallocarray (void *o, size_t n, size_t m)
|
||||
static char *
|
||||
xstrdup (const char *s)
|
||||
{
|
||||
return strcpy (xmalloc (strlen (s) + 1), s);
|
||||
size_t len = strlen (s) + 1;
|
||||
return memcpy (xmalloc (len), s, len);
|
||||
}
|
||||
|
||||
static char *
|
||||
@@ -315,7 +316,7 @@ xstrndup (const char *s, size_t n)
|
||||
#define ARRAY(type, name) type *name; size_t name ## _len, name ## _alloc;
|
||||
#define ARRAY_INIT_SIZED(a, n) \
|
||||
BLOCK_START \
|
||||
(a) = xcalloc (sizeof *(a), (a ## _alloc) = (n)); \
|
||||
(a) = xcalloc ((a ## _alloc) = (n), sizeof *(a)); \
|
||||
(a ## _len) = 0; \
|
||||
BLOCK_END
|
||||
#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
|
||||
@@ -397,7 +398,7 @@ strv_make (void)
|
||||
struct strv self;
|
||||
self.alloc = 4;
|
||||
self.len = 0;
|
||||
self.vector = xcalloc (sizeof *self.vector, self.alloc);
|
||||
self.vector = xcalloc (self.alloc, sizeof *self.vector);
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -1208,7 +1209,10 @@ async_make (struct async_manager *manager)
|
||||
}
|
||||
|
||||
/// Only allowed from the main thread once the job has been started but before
|
||||
/// the results have been dispatched
|
||||
/// the results have been dispatched.
|
||||
///
|
||||
/// Note that it may in practice lead to memory leakage, although that's
|
||||
/// an implementation issue: https://eissing.org/icing/posts/rip_pthread_cancel/
|
||||
static void
|
||||
async_cancel (struct async *self)
|
||||
{
|
||||
@@ -4500,7 +4504,7 @@ struct config_item
|
||||
}
|
||||
value; ///< The value of this item
|
||||
|
||||
struct config_schema *schema; ///< Schema describing this value
|
||||
const struct config_schema *schema; ///< Schema describing this value
|
||||
void *user_data; ///< User value attached by schema owner
|
||||
};
|
||||
|
||||
@@ -4652,7 +4656,7 @@ config_item_object (void)
|
||||
|
||||
static bool
|
||||
config_schema_accepts_type
|
||||
(struct config_schema *self, enum config_item_type type)
|
||||
(const struct config_schema *self, enum config_item_type type)
|
||||
{
|
||||
if (self->type == type)
|
||||
return true;
|
||||
@@ -4665,7 +4669,7 @@ config_schema_accepts_type
|
||||
|
||||
static bool
|
||||
config_item_validate_by_schema (struct config_item *self,
|
||||
struct config_schema *schema, struct error **e)
|
||||
const struct config_schema *schema, struct error **e)
|
||||
{
|
||||
struct error *error = NULL;
|
||||
if (!config_schema_accepts_type (schema, self->type))
|
||||
@@ -4686,7 +4690,7 @@ static bool
|
||||
config_item_set_from (struct config_item *self, struct config_item *source,
|
||||
struct error **e)
|
||||
{
|
||||
struct config_schema *schema = self->schema;
|
||||
const struct config_schema *schema = self->schema;
|
||||
if (!schema)
|
||||
{
|
||||
// Easy, we don't know what this item is
|
||||
@@ -4832,7 +4836,8 @@ config_item_write_kv_pair (struct config_writer *self,
|
||||
str_append_printf (self->output,
|
||||
"%s# %s\n", indent, value->schema->comment);
|
||||
|
||||
bool can_use_word = true;
|
||||
char *end = NULL;
|
||||
bool can_use_word = ((void) strtoll (key, &end, 10), end == key);
|
||||
for (const char *p = key; *p; p++)
|
||||
if (!config_tokenizer_is_word_char (*p))
|
||||
can_use_word = false;
|
||||
@@ -5482,7 +5487,7 @@ end:
|
||||
|
||||
/// "user_data" is passed to allow its immediate use in validation callbacks
|
||||
static struct config_item *
|
||||
config_schema_initialize_item (struct config_schema *schema,
|
||||
config_schema_initialize_item (const struct config_schema *schema,
|
||||
struct config_item *parent, void *user_data, struct error **warning,
|
||||
struct error **e)
|
||||
{
|
||||
@@ -5539,7 +5544,7 @@ keep_current:
|
||||
/// Assign schemas and user_data to multiple items at once;
|
||||
/// feel free to copy over and modify to suit your particular needs
|
||||
static void
|
||||
config_schema_apply_to_object (struct config_schema *schema_array,
|
||||
config_schema_apply_to_object (const struct config_schema *schema_array,
|
||||
struct config_item *object, void *user_data)
|
||||
{
|
||||
while (schema_array->name)
|
||||
|
||||
@@ -93,10 +93,12 @@ and always-present field, which must be a tag *enum*:
|
||||
case CAR: void;
|
||||
case LORRY: i8 axles;
|
||||
case PLANE: i8 engines;
|
||||
default: void;
|
||||
};
|
||||
|
||||
All possible enumeration values must be named, and there is no *case*
|
||||
fall-through.
|
||||
There is no *case* fall-through.
|
||||
Unless *default* is present, only the listed enumeration values are valid.
|
||||
Any *default* must currently be empty.
|
||||
|
||||
Framing
|
||||
-------
|
||||
|
||||
@@ -8,7 +8,7 @@ syn region libertyxdrBlockComment start=+/[*]+ end=+[*]/+
|
||||
syn match libertyxdrComment "//.*"
|
||||
syn match libertyxdrIdentifier "\<[[:alpha:]][[:alnum:]_]*\>"
|
||||
syn match libertyxdrNumber "\<0\>\|\(-\|\<\)[1-9][[:digit:]]*\>"
|
||||
syn keyword libertyxdrKeyword const enum struct union switch case
|
||||
syn keyword libertyxdrKeyword const enum struct union switch case default
|
||||
syn keyword libertyxdrType bool u8 u16 u32 u64 i8 i16 i32 i64 string void
|
||||
|
||||
let b:current_syntax = "libertyxdr"
|
||||
|
||||
214
tests/help2adoc.sh
Executable file
214
tests/help2adoc.sh
Executable file
@@ -0,0 +1,214 @@
|
||||
#!/bin/sh -e
|
||||
# This test very exactly matches the output,
|
||||
# but help2adoc is more or less feature-complete already.
|
||||
self=$(realpath "$0")
|
||||
help2adoc=$(realpath "$(dirname "$0")/../tools/help2adoc.awk")
|
||||
|
||||
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
test_oneline_help() {
|
||||
cat <<END
|
||||
Usage: $self [--brightness [+-]BRIGHTNESS] [--input NAME] [--restart]
|
||||
END
|
||||
}
|
||||
|
||||
test_oneline_version() {
|
||||
cat <<'END'
|
||||
eizoctl 1.0
|
||||
END
|
||||
}
|
||||
|
||||
test_oneline_out() {
|
||||
cat <<'END'
|
||||
eizoctl(1)
|
||||
==========
|
||||
:doctype: manpage
|
||||
:manmanual: eizoctl Manual
|
||||
:mansource: eizoctl 1.0
|
||||
|
||||
Name
|
||||
----
|
||||
eizoctl - manual page for eizoctl 1.0
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
*eizoctl* [**--brightness** [+-]__BRIGHTNESS__] [**--input** __NAME__] [**--restart**]
|
||||
END
|
||||
}
|
||||
|
||||
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
test_simple_help() {
|
||||
cat <<'END'
|
||||
Usage: elksmart-comm [OPTION]... [COMMAND...]
|
||||
Usage: elksmart-comm
|
||||
Transmit or receive infrared commands.
|
||||
|
||||
-d, --debug elksmart-comm will run in debug mode
|
||||
-f, --frequency HZ frequency (38000 Hz by default)
|
||||
-n, --nec use the NEC transmission format
|
||||
-h, --help display this help and exit
|
||||
-V, --version output version information and exit
|
||||
END
|
||||
}
|
||||
|
||||
test_simple_version() {
|
||||
cat <<'END'
|
||||
elksmart-comm (usb-drivers) dev
|
||||
END
|
||||
}
|
||||
|
||||
test_simple_out() {
|
||||
cat <<'END'
|
||||
elksmart-comm(1)
|
||||
================
|
||||
:doctype: manpage
|
||||
:manmanual: elksmart-comm Manual
|
||||
:mansource: usb-drivers dev
|
||||
|
||||
Name
|
||||
----
|
||||
elksmart-comm - manual page for elksmart-comm dev
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
*elksmart-comm* [__OPTION__]... [__COMMAND__...]
|
||||
|
||||
*elksmart-comm*
|
||||
|
||||
|
||||
Description
|
||||
-----------
|
||||
Transmit or receive infrared commands.
|
||||
|
||||
|
||||
*-d*, **--debug**::
|
||||
**elksmart-comm** will run in debug mode
|
||||
|
||||
*-f*, **--frequency** __HZ__::
|
||||
frequency (38000 Hz by default)
|
||||
|
||||
*-n*, **--nec**::
|
||||
use the NEC transmission format
|
||||
|
||||
*-h*, **--help**::
|
||||
display this help and exit
|
||||
|
||||
*-V*, **--version**::
|
||||
output version information and exit
|
||||
END
|
||||
}
|
||||
|
||||
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
test_wild_help() {
|
||||
cat <<'END'
|
||||
Usage:
|
||||
wild [option]… <command>… — Be wild
|
||||
What's happening?
|
||||
-f, --frequency hz-2-foo frequency to --foo at
|
||||
--foo=bar
|
||||
Foobar.
|
||||
Boo far.
|
||||
|
||||
Subsection:
|
||||
--help
|
||||
--version
|
||||
Oh my.
|
||||
|
||||
Major section:
|
||||
And now for something completely different.
|
||||
Very wild
|
||||
END
|
||||
}
|
||||
|
||||
test_wild_version() {
|
||||
cat <<'END'
|
||||
wild 1
|
||||
Copies left and right.
|
||||
END
|
||||
}
|
||||
|
||||
test_wild_out() {
|
||||
cat <<'END'
|
||||
wild(1)
|
||||
=======
|
||||
:doctype: manpage
|
||||
:manmanual: wild Manual
|
||||
:mansource: wild 1
|
||||
|
||||
Name
|
||||
----
|
||||
wild - manual page for wild 1
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
*wild* [__option__]... <__command__>...
|
||||
|
||||
|
||||
Description
|
||||
-----------
|
||||
Be **wild**
|
||||
|
||||
What's happening?
|
||||
|
||||
*-f*, **--frequency** __hz-2-foo__::
|
||||
frequency to **--foo** at
|
||||
|
||||
*--foo*=__bar__::
|
||||
Foobar.
|
||||
Boo far.
|
||||
|
||||
|
||||
Subsection
|
||||
~~~~~~~~~~
|
||||
|
||||
*--help*::
|
||||
|
||||
*--version*::
|
||||
Oh my.
|
||||
|
||||
|
||||
Major section
|
||||
-------------
|
||||
And now for something completely different.
|
||||
Very wild
|
||||
END
|
||||
}
|
||||
|
||||
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
run() {
|
||||
echo "-- help2adoc/$1"
|
||||
local selfquoted=$(echo "$self" | sed 's/\\/&&/g')
|
||||
local output=$(TEST=$1 awk -f "$help2adoc" -v Target="$selfquoted")
|
||||
local expect="$($1_out)"
|
||||
if [ "$output" = "$expect" ]
|
||||
then return
|
||||
fi
|
||||
|
||||
echo "== Expected"
|
||||
sed 's/^/ /' <<-END
|
||||
$expect
|
||||
END
|
||||
echo "== Received"
|
||||
sed 's/^/ /' <<-END
|
||||
$output
|
||||
END
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [ -z "$TEST" ]
|
||||
then
|
||||
run test_oneline
|
||||
run test_simple
|
||||
run test_wild
|
||||
echo "-- OK"
|
||||
elif [ "$1" = "--help" ]
|
||||
then ${TEST}_help
|
||||
elif [ "$1" = "--version" ]
|
||||
then ${TEST}_version
|
||||
else
|
||||
echo "Wrong usage"
|
||||
exit 1
|
||||
fi
|
||||
@@ -636,7 +636,7 @@ test_config_validate_nonnegative
|
||||
return false;
|
||||
}
|
||||
|
||||
static struct config_schema g_config_test[] =
|
||||
static const struct config_schema g_config_test[] =
|
||||
{
|
||||
{ .name = "foo",
|
||||
.comment = "baz",
|
||||
@@ -647,7 +647,7 @@ static struct config_schema g_config_test[] =
|
||||
.type = CONFIG_ITEM_INTEGER,
|
||||
.validate = test_config_validate_nonnegative,
|
||||
.default_ = "1" },
|
||||
{ .name = "foobar",
|
||||
{ .name = "123",
|
||||
.type = CONFIG_ITEM_STRING,
|
||||
.default_ = "\"qux\\x01`\" \"\"`a`" },
|
||||
{}
|
||||
@@ -676,10 +676,11 @@ test_config (void)
|
||||
config_item_destroy (invalid);
|
||||
|
||||
hard_assert (!strcmp ("qux\001`a",
|
||||
config_item_get (config.root, "top.foobar", NULL)->value.string.str));
|
||||
config_item_get (config.root, "top.123", NULL)->value.string.str));
|
||||
|
||||
struct str s = str_make ();
|
||||
config_item_write (config.root, true, &s);
|
||||
print_debug ("%s", s.str);
|
||||
struct config_item *parsed = config_item_parse (s.str, s.len, false, NULL);
|
||||
hard_assert (parsed);
|
||||
config_item_destroy (parsed);
|
||||
|
||||
@@ -25,5 +25,7 @@ struct Struct {
|
||||
union Onion switch (Enum tag) {
|
||||
case NOTHING:
|
||||
void;
|
||||
default:
|
||||
void;
|
||||
} o;
|
||||
};
|
||||
|
||||
@@ -200,7 +200,7 @@ function inline(line) {
|
||||
}
|
||||
|
||||
# Returns 1 iff the left-over $0 should be processed further.
|
||||
function process(firstline, posattrs, namedattrs) {
|
||||
function process(firstline, posattrs, namedattrs, ok) {
|
||||
if (readattribute(firstline))
|
||||
return 0
|
||||
if (getline <= 0) {
|
||||
@@ -281,9 +281,11 @@ function process(firstline, posattrs, namedattrs) {
|
||||
while ($0) {
|
||||
sub(/^[[:space:]]+/, "")
|
||||
sub(/^[+]$/, "")
|
||||
if (!process($0) && getline <= 0)
|
||||
fatal("unexpected EOF")
|
||||
if (match($0, /^[[:space:]]*[*][[:space:]]+/))
|
||||
if (!process($0) && (ok = getline) <= 0) {
|
||||
if (ok < 0)
|
||||
fatal("getline failed")
|
||||
$0 = ""
|
||||
} else if (match($0, /^[[:space:]]*[*][[:space:]]+/))
|
||||
break
|
||||
}
|
||||
print ".RE"
|
||||
@@ -318,9 +320,11 @@ function process(firstline, posattrs, namedattrs) {
|
||||
while ($0) {
|
||||
sub(/^[[:space:]]+/, "")
|
||||
sub(/^[+]$/, "")
|
||||
if (!process($0) && getline <= 0)
|
||||
fatal("unexpected EOF")
|
||||
if (match($0, /::$/))
|
||||
if (!process($0) && (ok = getline) <= 0) {
|
||||
if (ok < 0)
|
||||
fatal("getline failed")
|
||||
$0 = ""
|
||||
} else if (match($0, /::$/))
|
||||
break
|
||||
}
|
||||
print ".RE"
|
||||
|
||||
@@ -75,7 +75,7 @@ function line_ending() {
|
||||
# it doesn't seem to be worth the effort.
|
||||
function expand(s, v) {
|
||||
v = s
|
||||
while (match(v, /\\*[$](|ENV|CACHE)[{]/)) {
|
||||
while (match(v, /\\*[$](ENV|CACHE)?[{]/)) {
|
||||
if (index(substr(v, RSTART), "$") % 2 != 0) {
|
||||
warning("variable expansion is not supported: " s)
|
||||
return s
|
||||
|
||||
234
tools/help2adoc.awk
Normal file
234
tools/help2adoc.awk
Normal file
@@ -0,0 +1,234 @@
|
||||
# help2adoc.awk: convert --version/--help to AsciiDoc manual pages
|
||||
#
|
||||
# Copyright (c) 2024, Přemysl Eric Janouch <p@janouch.name>
|
||||
# SPDX-License-Identifier: 0BSD
|
||||
#
|
||||
# Usage: awk -f help2adoc.awk -v Target=cat
|
||||
#
|
||||
# This is not intended to produce great output, merely useful output,
|
||||
# if only because there is no real standard of what the input should look like.
|
||||
#
|
||||
# The only target that needs to work is liberty's own opt_handler.
|
||||
# The expected input format is roughly that of GNU utilites.
|
||||
|
||||
function fatal(message) {
|
||||
print "// " message
|
||||
print "fatal error: " message > "/dev/stderr"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# The input model of this script is that function take the next line on $0,
|
||||
# read further lines as necessary, and leave the next line in $0 again.
|
||||
function readline( ok) {
|
||||
if ((ok = (Command | getline)) < 0)
|
||||
fatal("read error")
|
||||
if (!ok)
|
||||
exit
|
||||
}
|
||||
|
||||
function emboldenoptions(line) {
|
||||
# -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
|
||||
sub(/^-[^-=,[:space:]{[<]/, "*&*", line)
|
||||
while (match(line, /[^-_[:alnum:]*'+]-[^-=,[:space:]{[<]/)) {
|
||||
line = substr(line, 1, RSTART) \
|
||||
"**" substr(line, RSTART + 1, RLENGTH - 1) "**" \
|
||||
substr(line, RSTART + RLENGTH)
|
||||
}
|
||||
sub(/^--[-_[:alnum:]]+/, "*&*", line)
|
||||
while (match(line, /[^-_[:alnum:]*'+]--[-_[:alnum:]]+/)) {
|
||||
line = substr(line, 1, RSTART) \
|
||||
"**" substr(line, RSTART + 1, RLENGTH - 1) "**" \
|
||||
substr(line, RSTART + RLENGTH)
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
function formatinline(line, programname, last, i) {
|
||||
# Go the extra step of emboldening the program name at word boundaries.
|
||||
programname = ProgramName
|
||||
gsub(/[][\\.^$(){}|*+?]/, "\\\\&", programname)
|
||||
if (match(line, "^" programname "[^-_[:alnum:]*'+/]")) {
|
||||
line = "**" substr(line, RSTART, RLENGTH - 1) "**" \
|
||||
substr(line, RSTART + RLENGTH - 1)
|
||||
}
|
||||
while (match(line, "[^-_[:alnum:]*'+/]" programname "[^-_[:alnum:]*'+/]")) {
|
||||
line = substr(line, 1, RSTART) \
|
||||
"**" substr(line, RSTART + 1, RLENGTH - 2) "**" \
|
||||
substr(line, RSTART + RLENGTH - 1)
|
||||
}
|
||||
if (match(line, "[^-_[:alnum:]*'+/]" programname "$")) {
|
||||
line = substr(line, 1, RSTART) \
|
||||
"**" substr(line, RSTART + 1, RLENGTH - 1) "**"
|
||||
}
|
||||
return emboldenoptions(line)
|
||||
}
|
||||
|
||||
function printusage(usage, description) {
|
||||
gsub(/…/, "...", usage)
|
||||
gsub(/—|–/, "-", usage)
|
||||
|
||||
# --help output will more likely than not simply include argv[0],
|
||||
# or perhaps program_invocation_short_name (not addressed here).
|
||||
if (substr(usage, 1, length(Target) + 1) == Target " ")
|
||||
usage = ProgramName substr(usage, length(Target) + 1)
|
||||
|
||||
# A lot of GNOME software includes the description here.
|
||||
if (match(usage, / +- +/) && usage !~ / - [^[:alnum:]]/) {
|
||||
description = substr(usage, RSTART + RLENGTH)
|
||||
usage = substr(usage, 1, RSTART - 1)
|
||||
}
|
||||
|
||||
while (match(usage, /[^-_[:alnum:]*'+.][[:alnum:]][-_[:alnum:]]+/)) {
|
||||
usage = substr(usage, 1, RSTART) \
|
||||
"__" substr(usage, RSTART + 1, RLENGTH - 1) "__" \
|
||||
substr(usage, RSTART + RLENGTH)
|
||||
}
|
||||
sub(/^[^[:space:]]+/, "*&*", usage)
|
||||
print emboldenoptions(usage)
|
||||
print ""
|
||||
|
||||
if (description) {
|
||||
flushsections()
|
||||
print formatinline(description)
|
||||
print ""
|
||||
}
|
||||
}
|
||||
|
||||
# We're going with Setext headers, because that's what asciiman.awk supports.
|
||||
function printheader(text, underline) {
|
||||
print text
|
||||
gsub(/./, underline, text)
|
||||
print text
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
if (!Target)
|
||||
fatal("missing Target")
|
||||
|
||||
TargetQuoted = Target
|
||||
gsub(/'/, "'\\''", TargetQuoted)
|
||||
TargetQuoted = "'" TargetQuoted "'"
|
||||
|
||||
# Remaining --version lines could be about copyright (GNU),
|
||||
# or something else entirely.
|
||||
Command = TargetQuoted " --version"
|
||||
if ((Command | getline) > 0) {
|
||||
# GNU --version output can place the package name in parentheses.
|
||||
Package = $0
|
||||
if (match($0, /[[:space:]][(][^)]*[)]/)) {
|
||||
Package = substr($0, RSTART + 2, RLENGTH - 3) \
|
||||
substr($0, RSTART + RLENGTH)
|
||||
sub(/[[:space:]]+[(][^)]*[)]/, "")
|
||||
}
|
||||
|
||||
Version = $0
|
||||
sub(/[[:space:]]+[^[:space:]]+$/, "")
|
||||
Name = $0
|
||||
} else {
|
||||
fatal("failed to get --version output")
|
||||
}
|
||||
|
||||
if (Name !~ /[[:space:]]/)
|
||||
ProgramName = Name
|
||||
else if (match(Target, /[^\/]+$/))
|
||||
ProgramName = substr(Target, RSTART, RLENGTH)
|
||||
|
||||
printheader(ProgramName "(1)", "=")
|
||||
print ":doctype: manpage"
|
||||
print ":manmanual: " Name " Manual"
|
||||
print ":mansource: " Package
|
||||
print ""
|
||||
printheader("Name", "-")
|
||||
print ProgramName " - manual page for " Version
|
||||
print ""
|
||||
|
||||
close(Command)
|
||||
Command = TargetQuoted " --help"
|
||||
if ((Command | getline) <= 0)
|
||||
fatal("failed to get --help output")
|
||||
|
||||
NextSection = "Description"
|
||||
NextSubsection = ""
|
||||
|
||||
# The SYNOPSIS section is mandatory, so just put it there.
|
||||
printheader("Synopsis", "-")
|
||||
while (1) {
|
||||
if (match($0, /^[Uu]sage:[[:space:]]*/)) {
|
||||
if (($0 = substr($0, RSTART + RLENGTH)))
|
||||
printusage($0)
|
||||
} else if (match($0, /^[[:space:]]+/) && !/^[[:space:]]*-/) {
|
||||
if (($0 = substr($0, RSTART + RLENGTH)))
|
||||
printusage($0)
|
||||
} else if ($0) {
|
||||
break
|
||||
}
|
||||
readline()
|
||||
}
|
||||
while (1) {
|
||||
if (match($0, /^[[:alpha:]][-[:alnum:][:space:]]+:$/)) {
|
||||
# We don't flush sections here,
|
||||
# so that we don't unnecessarily enforce DESCRIPTION first.
|
||||
NextSection = substr($0, RSTART, RLENGTH - 1)
|
||||
} else if (match($0, /^ [[:alpha:]][-[:alnum:][:space:]]+:$/)) {
|
||||
flushsections()
|
||||
NextSubsection = substr($0, RSTART + 1, RLENGTH - 2)
|
||||
} else if (match($0, /^ +-/)) {
|
||||
flushsections()
|
||||
parseoption(substr($0, RSTART + RLENGTH - 1))
|
||||
continue
|
||||
} else if ($0) {
|
||||
flushsections()
|
||||
|
||||
# That will be probably interpreted as a literal block.
|
||||
if (!/^[[:space:]]/)
|
||||
$0 = formatinline($0)
|
||||
print
|
||||
} else {
|
||||
print
|
||||
}
|
||||
readline()
|
||||
}
|
||||
}
|
||||
|
||||
function flushsections() {
|
||||
if (NextSection) {
|
||||
print ""
|
||||
printheader(NextSection, "-")
|
||||
NextSection = ""
|
||||
}
|
||||
if (NextSubsection) {
|
||||
print ""
|
||||
printheader(NextSubsection, "~")
|
||||
NextSubsection = ""
|
||||
}
|
||||
}
|
||||
|
||||
function parseoption(line, usage) {
|
||||
# Often enough you will see it separated with only one space,
|
||||
# which will simply not work for us.
|
||||
if (match(line, /[[:space:]]{2,}/)) {
|
||||
usage = substr(line, 1, RSTART - 1)
|
||||
line = substr(line, RSTART + RLENGTH)
|
||||
} else {
|
||||
usage = line
|
||||
line = ""
|
||||
}
|
||||
|
||||
usage = emboldenoptions(usage)
|
||||
while (match(usage, /[=<, ][[:alnum:]][-_[:alnum:]]*/)) {
|
||||
usage = substr(usage, 1, RSTART) \
|
||||
"__" substr(usage, RSTART + 1, RLENGTH - 1) "__" \
|
||||
substr(usage, RSTART + RLENGTH)
|
||||
}
|
||||
|
||||
print ""
|
||||
print usage "::"
|
||||
if (line)
|
||||
print "\t" formatinline(line)
|
||||
|
||||
readline()
|
||||
while (match($0, /^ +[^-[:space:]]|^ {7,}./)) {
|
||||
print "\t" formatinline(substr($0, RSTART + RLENGTH - 1))
|
||||
readline()
|
||||
}
|
||||
}
|
||||
23
tools/lxdrgen-cpp-qt.cpp
Normal file
23
tools/lxdrgen-cpp-qt.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
// lxdrgen-cpp-qt.cpp: Qt support code for lxdrgen-cpp.awk.
|
||||
//
|
||||
// Copyright (c) 2024, Přemysl Eric Janouch <p@janouch.name>
|
||||
// SPDX-License-Identifier: 0BSD
|
||||
#include <QString>
|
||||
#include <string>
|
||||
|
||||
namespace LibertyXDR {
|
||||
|
||||
bool utf8_to_wstring(const uint8_t *utf8, size_t length, std::wstring &wide) {
|
||||
QByteArrayView view(reinterpret_cast<const char *>(utf8), length);
|
||||
if (!view.isValidUtf8())
|
||||
return false;
|
||||
wide = QString::fromUtf8(view).toStdWString();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wstring_to_utf8(const std::wstring &wide, std::string &utf8) {
|
||||
utf8 = QString::fromStdWString(wide).toUtf8().toStdString();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace LibertyXDR
|
||||
@@ -1,6 +1,6 @@
|
||||
# lxdrgen-go.awk: Go backend for lxdrgen.awk.
|
||||
#
|
||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
|
||||
# Copyright (c) 2022 - 2024, Přemysl Eric Janouch <p@janouch.name>
|
||||
# SPDX-License-Identifier: 0BSD
|
||||
#
|
||||
# This backend also enables proxying to other endpoints using JSON.
|
||||
@@ -145,7 +145,7 @@ function codegen_begin( funcname) {
|
||||
print "// " funcname " tries to serialize a string value,"
|
||||
print "// appending it to the end of a byte stream."
|
||||
print "func " funcname "(data []byte, s string) ([]byte, bool) {"
|
||||
print "\tif len(s) > math.MaxUint32 {"
|
||||
print "\tif int64(len(s)) > math.MaxUint32 {"
|
||||
print "\t\treturn nil, false"
|
||||
print "\t}"
|
||||
print "\tdata = binary.BigEndian.AppendUint32(data, uint32(len(s)))"
|
||||
@@ -301,9 +301,11 @@ function codegen_marshal(type, f, marshal) {
|
||||
"\t}\n"
|
||||
}
|
||||
|
||||
function codegen_struct_field_marshal(d, cg, camel, f, marshal) {
|
||||
function codegen_struct_field_marshal(d, cg, isaccessor, camel, f, marshal) {
|
||||
camel = snaketocamel(d["name"])
|
||||
f = "s." camel
|
||||
if (isaccessor)
|
||||
f = f "()"
|
||||
if (!d["isarray"]) {
|
||||
append(cg, "marshal",
|
||||
"\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \
|
||||
@@ -333,7 +335,7 @@ function codegen_struct_field_marshal(d, cg, camel, f, marshal) {
|
||||
}
|
||||
|
||||
function codegen_struct_field(d, cg, camel, f, serialize, deserialize) {
|
||||
codegen_struct_field_marshal(d, cg)
|
||||
codegen_struct_field_marshal(d, cg, 0)
|
||||
|
||||
camel = snaketocamel(d["name"])
|
||||
f = "s." camel
|
||||
@@ -384,15 +386,11 @@ function codegen_struct_field(d, cg, camel, f, serialize, deserialize) {
|
||||
}
|
||||
}
|
||||
|
||||
function codegen_struct_tag(d, cg, camel, f) {
|
||||
codegen_struct_field_marshal(d, cg)
|
||||
function codegen_struct_tag(d, cg) {
|
||||
codegen_struct_field_marshal(d, cg, 1)
|
||||
|
||||
camel = snaketocamel(d["name"])
|
||||
f = "s." camel
|
||||
append(cg, "fields", "\t" camel " " CodegenGoType[d["type"]] \
|
||||
" `json:\"" decapitalize(camel) "\"`\n")
|
||||
append(cg, "serialize", sprintf(CodegenSerialize[d["type"]], f))
|
||||
# Do not deserialize here, that is already done by the containing union.
|
||||
# Do not serialize or deserialize here,
|
||||
# that is already done by the containing union.
|
||||
}
|
||||
|
||||
function codegen_struct(name, cg, gotype) {
|
||||
@@ -450,13 +448,18 @@ function codegen_union_struct(name, casename, cg, scg, structname, init) {
|
||||
structname = name snaketocamel(casename)
|
||||
codegen_struct(structname, scg)
|
||||
|
||||
init = CodegenGoType[structname] "{" cg["tagname"] \
|
||||
": " decapitalize(cg["tagname"]) "}"
|
||||
print "func (u *" CodegenGoType[structname] ") " cg["tagname"] "() " \
|
||||
CodegenGoType[cg["tagtype"]] " {"
|
||||
print "\treturn " CodegenGoType[cg["tagtype"]] snaketocamel(casename)
|
||||
print "}"
|
||||
print ""
|
||||
|
||||
init = CodegenGoType[structname] "{}"
|
||||
append(cg, "unmarshal",
|
||||
"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
|
||||
"\t\ts := " init "\n" \
|
||||
"\t\terr = json.Unmarshal(data, &s)\n" \
|
||||
"\t\tu.Interface = &s\n")
|
||||
"\t\tu.Variant = &s\n")
|
||||
append(cg, "serialize",
|
||||
"\tcase *" CodegenGoType[structname] ":\n" \
|
||||
indent(sprintf(CodegenSerialize[structname], "union")))
|
||||
@@ -464,20 +467,23 @@ function codegen_union_struct(name, casename, cg, scg, structname, init) {
|
||||
"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
|
||||
"\t\ts := " init "\n" \
|
||||
indent(sprintf(CodegenDeserialize[structname], "s")) \
|
||||
"\t\tu.Interface = &s\n")
|
||||
"\t\tu.Variant = &s\n")
|
||||
}
|
||||
|
||||
function codegen_union(name, cg, exhaustive, gotype, tagvar) {
|
||||
gotype = PrefixCamel name
|
||||
# This must be a struct, so that UnmarshalJSON can create concrete types.
|
||||
print "type " gotype " struct {"
|
||||
print "\tInterface any"
|
||||
print "\tVariant interface {"
|
||||
print "\t\t" cg["tagname"] "() " CodegenGoType[cg["tagtype"]]
|
||||
print "\t}"
|
||||
print "}"
|
||||
print ""
|
||||
|
||||
# This cannot be a pointer method, it wouldn't work recursively.
|
||||
CodegenIsMarshaler[name] = 1
|
||||
print "func (u " gotype ") MarshalJSON() ([]byte, error) {"
|
||||
print "\treturn u.Interface.(json.Marshaler).MarshalJSON()"
|
||||
print "\treturn u.Variant.(json.Marshaler).MarshalJSON()"
|
||||
print "}"
|
||||
print ""
|
||||
|
||||
@@ -499,13 +505,15 @@ function codegen_union(name, cg, exhaustive, gotype, tagvar) {
|
||||
print "}"
|
||||
print ""
|
||||
|
||||
# XXX: Consider changing the interface into an AppendTo/ConsumeFrom one,
|
||||
# that would eliminate these type case switches entirely.
|
||||
# On the other hand, it would make it possible to send unsuitable structs.
|
||||
# XXX: Consider rather testing the type for having an AppendTo method,
|
||||
# which would eliminate this type case switch entirely.
|
||||
print "func (u *" gotype ") AppendTo(data []byte) ([]byte, bool) {"
|
||||
print "\tok := true"
|
||||
print "\tswitch union := u.Interface.(type) {"
|
||||
print sprintf(CodegenSerialize[cg["tagtype"]],
|
||||
"u.Variant." cg["tagname"] "()") \
|
||||
"\tswitch union := u.Variant.(type) {"
|
||||
print cg["serialize"] "\tdefault:"
|
||||
print "\t\t_ = union"
|
||||
print "\t\treturn nil, false"
|
||||
print "\t}"
|
||||
print "\treturn data, ok"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# lxdrgen.awk: an XDR-derived code generator for network protocols.
|
||||
#
|
||||
# Copyright (c) 2022 - 2023, Přemysl Eric Janouch <p@janouch.name>
|
||||
# Copyright (c) 2022 - 2025, Přemysl Eric Janouch <p@janouch.name>
|
||||
# SPDX-License-Identifier: 0BSD
|
||||
#
|
||||
# Usage: env LC_ALL=C awk -f lxdrgen.awk -f lxdrgen-{c,go,mjs}.awk \
|
||||
@@ -218,7 +218,7 @@ function defstruct( name, d, cg) {
|
||||
}
|
||||
|
||||
function defunion( name, tag, tagtype, tagvalue, cg, scg, d, a, i,
|
||||
unseen, exhaustive) {
|
||||
unseen, defaulted, exhaustive) {
|
||||
delete cg[0]
|
||||
delete scg[0]
|
||||
delete d[0]
|
||||
@@ -249,9 +249,22 @@ function defunion( name, tag, tagtype, tagvalue, cg, scg, d, a, i,
|
||||
if (!unseen[tagvalue]--)
|
||||
fatal("no such value or duplicate case: " tagtype "." tagvalue)
|
||||
codegen_struct_tag(tag, scg)
|
||||
} else if (accept("default")) {
|
||||
if (tagvalue)
|
||||
codegen_union_struct(name, tagvalue, cg, scg)
|
||||
|
||||
expect(accept(":"))
|
||||
if (defaulted)
|
||||
fatal("duplicate default")
|
||||
|
||||
tagvalue = ""
|
||||
defaulted = 1
|
||||
} else if (tagvalue) {
|
||||
if (readfield(d))
|
||||
codegen_struct_field(d, scg)
|
||||
} else if (defaulted) {
|
||||
if (readfield(d))
|
||||
fatal("default must not contain fields")
|
||||
} else {
|
||||
fatal("union fields must fall under a case")
|
||||
}
|
||||
@@ -259,11 +272,17 @@ function defunion( name, tag, tagtype, tagvalue, cg, scg, d, a, i,
|
||||
if (tagvalue)
|
||||
codegen_union_struct(name, tagvalue, cg, scg)
|
||||
|
||||
# Unseen cases are simply not recognized/allowed.
|
||||
# Unseen cases are only recognized/allowed when default is present.
|
||||
exhaustive = 1
|
||||
for (i in unseen)
|
||||
if (i && unseen[i])
|
||||
exhaustive = 0
|
||||
if (i && unseen[i]) {
|
||||
if (defaulted) {
|
||||
codegen_struct_tag(tag, scg)
|
||||
codegen_union_struct(name, i, cg, scg)
|
||||
} else {
|
||||
exhaustive = 0
|
||||
}
|
||||
}
|
||||
|
||||
Types[name] = "union"
|
||||
codegen_union(name, cg, exhaustive)
|
||||
|
||||
42
tools/wdye/CMakeLists.txt
Normal file
42
tools/wdye/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
cmake_minimum_required (VERSION 3.18)
|
||||
project (wdye VERSION 1 DESCRIPTION "What did you expect?" LANGUAGES C)
|
||||
|
||||
set (CMAKE_C_STANDARD 99)
|
||||
set (CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set (CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
# -Wunused-function is pretty annoying here, as everything is static
|
||||
set (options -Wall -Wextra -Wno-unused-function)
|
||||
add_compile_options ("$<$<CXX_COMPILER_ID:GNU>:${options}>")
|
||||
add_compile_options ("$<$<CXX_COMPILER_ID:Clang>:${options}>")
|
||||
|
||||
set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../../cmake")
|
||||
|
||||
find_package (Curses)
|
||||
find_package (PkgConfig REQUIRED)
|
||||
pkg_search_module (lua REQUIRED
|
||||
lua53 lua5.3 lua-5.3 lua54 lua5.4 lua-5.4 lua>=5.3)
|
||||
|
||||
option (WITH_CURSES "Offer terminal sequences using Curses" "${CURSES_FOUND}")
|
||||
|
||||
# -liconv may or may not be a part of libc
|
||||
find_path (iconv_INCLUDE_DIRS iconv.h)
|
||||
|
||||
include_directories (BEFORE "${PROJECT_BINARY_DIR}" ${iconv_INCLUDE_DIRS})
|
||||
file (CONFIGURE OUTPUT "${PROJECT_BINARY_DIR}/config.h" CONTENT [[
|
||||
#define PROGRAM_NAME "${PROJECT_NAME}"
|
||||
#define PROGRAM_VERSION "${PROJECT_VERSION}"
|
||||
#cmakedefine WITH_CURSES
|
||||
]])
|
||||
|
||||
add_executable (wdye wdye.c)
|
||||
target_include_directories (wdye PUBLIC ${lua_INCLUDE_DIRS})
|
||||
target_link_directories (wdye PUBLIC ${lua_LIBRARY_DIRS})
|
||||
target_link_libraries (wdye PUBLIC ${lua_LIBRARIES})
|
||||
if (WITH_CURSES)
|
||||
target_include_directories (wdye PUBLIC ${CURSES_INCLUDE_DIRS})
|
||||
target_link_libraries (wdye PUBLIC ${CURSES_LIBRARIES})
|
||||
endif ()
|
||||
|
||||
add_test (NAME wdye COMMAND wdye "${PROJECT_SOURCE_DIR}/test.lua")
|
||||
include (CTest)
|
||||
33
tools/wdye/test.lua
Normal file
33
tools/wdye/test.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
for k, v in pairs(wdye) do _G[k] = v end
|
||||
|
||||
-- The terminal echoes back, we don't want to read the same stuff twice.
|
||||
local cat = spawn {"sh", "-c", "cat > /dev/null", environ={TERM="xterm"}}
|
||||
assert(cat, "failed to spawn process")
|
||||
assert(cat.term.key_left, "bad terminfo")
|
||||
|
||||
cat:send("Hello\r")
|
||||
local m = expect(cat:exact {"Hello\r", function (p) return p[0] end})
|
||||
assert(m == "Hello\r", "exact match failed, or value expansion mismatch")
|
||||
|
||||
local t = table.pack(expect(timeout {.5, 42}))
|
||||
assert(#t == 1 and t[1] == 42, "timeout match failed, or value mismatch")
|
||||
|
||||
cat:send("abc123\r")
|
||||
expect(cat:regex {"A(.*)3", nocase=true, function (p)
|
||||
assert(p[0] == "abc123", "wrong regex group #0")
|
||||
assert(p[1] == "bc12", "wrong regex group #1")
|
||||
end})
|
||||
|
||||
assert(not cat:wait (true), "process reports exiting early")
|
||||
|
||||
-- Send EOF (^D), test method chaining.
|
||||
cat:send("Closing...\r"):send("\004")
|
||||
local v = expect(cat:eof {true},
|
||||
cat:default {.5, function (p) error "expected EOF, got a timeout" end})
|
||||
|
||||
assert(cat.pid > 0, "process has no ID")
|
||||
local s1, exit, signal = cat:wait ()
|
||||
assert(s1 == 0 and exit == 0 and not signal, "unexpected exit status")
|
||||
assert(cat.pid < 0, "process still has an ID")
|
||||
local s2 = cat:wait (true)
|
||||
assert(s1 == s2, "exit status not remembered")
|
||||
146
tools/wdye/wdye.adoc
Normal file
146
tools/wdye/wdye.adoc
Normal file
@@ -0,0 +1,146 @@
|
||||
wdye(1)
|
||||
=======
|
||||
:doctype: manpage
|
||||
:manmanual: wdye Manual
|
||||
:mansource: wdye {release-version}
|
||||
|
||||
Name
|
||||
----
|
||||
wdye - what did you expect: Lua-based Expect tool
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
*wdye* _program.lua_
|
||||
|
||||
Description
|
||||
-----------
|
||||
*wdye* executes a Lua script, providing an *expect*(1)-like API targeted
|
||||
at application testing.
|
||||
|
||||
API
|
||||
---
|
||||
This list is logically ordered. Uppercase names represent object types.
|
||||
|
||||
wdye.spawn {file [, arg1, ...] [, environ=env]}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Creates a new pseudoterminal, spawns the given program in it,
|
||||
and returns a _process_ object. When *file* doesn't contain slashes,
|
||||
the program will be searched for in _PATH_.
|
||||
|
||||
The *env* map may be used to override environment variables, notably _TERM_.
|
||||
Variables evaluating to _false_ will be removed from the environment.
|
||||
|
||||
The program's whole process group receives SIGKILL when the _process_
|
||||
is garbage-collected, unless *wait* has collected the process group leader.
|
||||
|
||||
wdye.expect ([pattern1, ...])
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Waits until any pattern is ready, in order.
|
||||
When no *timeout* (or *default*) patterns are included, one is added implicitly.
|
||||
|
||||
The function returns the matching _pattern_'s values, while replacing
|
||||
any included functions with the results of their immediate evaluation,
|
||||
passing the matching _pattern_ as their sole argument.
|
||||
|
||||
wdye.timeout {[timeout, ] [value1, ...]}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns a new timeout _pattern_. When no *timeout* is given, which is specified
|
||||
in seconds, a default timeout value is assumed. Any further values
|
||||
are remembered to be later processed by *expect*.
|
||||
|
||||
wdye.continue ()
|
||||
~~~~~~~~~~~~~~~~
|
||||
Raises a _nil_ error, which is interpreted by *expect* as a signal to restart
|
||||
all processing.
|
||||
|
||||
PROCESS.buffer
|
||||
~~~~~~~~~~~~~~
|
||||
A string with the _process_' current read buffer contents.
|
||||
|
||||
PROCESS.pid
|
||||
~~~~~~~~~~~
|
||||
An integer with the _process_' process ID, or -1 if *wait* has collected it.
|
||||
|
||||
PROCESS.term
|
||||
~~~~~~~~~~~~
|
||||
A table with the _process_' *terminfo*(5) capabilities,
|
||||
notably containing all **key_...** codes.
|
||||
This functionality may not be enabled, then this table will always be empty.
|
||||
|
||||
PROCESS:send ([string, ...])
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Writes the given strings to the _process_' terminal slave,
|
||||
and returns the _process_ for method chaining.
|
||||
|
||||
Beware of echoing and deadlocks, as only *expect* can read from the _process_,
|
||||
and thus consume the terminal slave's output queue.
|
||||
|
||||
PROCESS:regex {pattern [, nocase=true] [, notransfer=true] [, value1, ...]}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns a new regular expression _pattern_. The *pattern* is a POSIX
|
||||
Extended Regular Expression. Whether it can match NUL bytes depends on your
|
||||
system C library.
|
||||
|
||||
When the *nocase* option is _true_, the expression will be matched
|
||||
case-insensitively.
|
||||
|
||||
Unless the *notransfer* option is _true_, all data up until the end of the match
|
||||
will be erased from the _process_' read buffer upon a successful match.
|
||||
|
||||
PROCESS:exact {literal [, nocase=true] [, notransfer=true] [, value1, ...]}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns a new literal string _pattern_. This behaves as if the *literal*
|
||||
had its ERE special characters quoted, and was then passed to *regex*.
|
||||
This _pattern_ can always match NUL bytes.
|
||||
|
||||
PROCESS:eof {[notransfer=true, ] [value1, ...]}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns a new end-of-file _pattern_, which matches the entire read buffer
|
||||
contents once the child process closes the terminal.
|
||||
|
||||
PROCESS:wait ([nowait])
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Waits for the program to terminate, and returns three values:
|
||||
a combined status as used by `$?` in shells,
|
||||
an exit status, and a termination signal number.
|
||||
One of the latter two values will be _nil_, as appropriate.
|
||||
|
||||
When the *nowait* option is _true_, the function returns immediately.
|
||||
If the process hasn't terminated yet, the function then returns no values.
|
||||
|
||||
PROCESS:default {[timeout, ] [notransfer=true, ] [value1, ...]}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns a new _pattern_ combining *wdye.timeout* with *eof*.
|
||||
|
||||
PATTERN.process
|
||||
~~~~~~~~~~~~~~~
|
||||
A reference to the _pattern_'s respective process, or _nil_.
|
||||
|
||||
PATTERN[group]
|
||||
~~~~~~~~~~~~~~
|
||||
For patterns that can match data, the zeroth group will be the whole matched
|
||||
input sequence.
|
||||
For *regex* patterns, positive groups relate to regular expression subgroups.
|
||||
Missing groups evaluate to _nil_.
|
||||
|
||||
Example
|
||||
-------
|
||||
for k, v in pairs(wdye) do _G[k] = v end
|
||||
local rot13 = spawn {"tr", "A-Za-z", "N-ZA-Mn-za-m", environ={TERM="dumb"}}
|
||||
rot13:send "Hello\r"
|
||||
expect(rot13:exact {"Uryyb\r"})
|
||||
|
||||
Environment
|
||||
-----------
|
||||
*WDYE_LOGGING*::
|
||||
When this environment variable is present, *wdye* produces asciicast v2
|
||||
files for every spawned program, in the current working directory.
|
||||
|
||||
Reporting bugs
|
||||
--------------
|
||||
Use https://git.janouch.name/p/liberty to report bugs, request features,
|
||||
or submit pull requests.
|
||||
|
||||
See also
|
||||
--------
|
||||
*expect*(1), *terminfo*(5), *regex*(7)
|
||||
1420
tools/wdye/wdye.c
Normal file
1420
tools/wdye/wdye.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user