Compare commits
	
		
			1 Commits
		
	
	
		
			0f3ed14575
			...
			c++
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						ddb3a60dcc
	
				 | 
					
					
						
@@ -1,32 +0,0 @@
 | 
			
		||||
# clang-format is fairly limited, and these rules are approximate:
 | 
			
		||||
#  - array initializers can get terribly mangled with clang-format 12.0,
 | 
			
		||||
#  - sometimes it still aligns with space characters,
 | 
			
		||||
#  - struct name NL { NL ... NL } NL name; is unachievable.
 | 
			
		||||
BasedOnStyle: GNU
 | 
			
		||||
ColumnLimit: 80
 | 
			
		||||
IndentWidth: 4
 | 
			
		||||
TabWidth: 4
 | 
			
		||||
UseTab: ForContinuationAndIndentation
 | 
			
		||||
BreakBeforeBraces: Allman
 | 
			
		||||
SpaceAfterCStyleCast: true
 | 
			
		||||
AlignAfterOpenBracket: DontAlign
 | 
			
		||||
AlignOperands: DontAlign
 | 
			
		||||
AlignConsecutiveMacros: Consecutive
 | 
			
		||||
AllowAllArgumentsOnNextLine: false
 | 
			
		||||
AllowAllParametersOfDeclarationOnNextLine: false
 | 
			
		||||
IndentGotoLabels: false
 | 
			
		||||
 | 
			
		||||
# IncludeCategories has some potential, but it may also break the build.
 | 
			
		||||
# Note that the documentation says the value should be "Never".
 | 
			
		||||
SortIncludes: false
 | 
			
		||||
 | 
			
		||||
# This is a compromise, it generally works out aesthetically better.
 | 
			
		||||
BinPackArguments: false
 | 
			
		||||
 | 
			
		||||
# Unfortunately, this can't be told to align to column 40 or so.
 | 
			
		||||
SpacesBeforeTrailingComments: 2
 | 
			
		||||
 | 
			
		||||
# liberty-specific macro body wrappers.
 | 
			
		||||
MacroBlockBegin: "BLOCK_START"
 | 
			
		||||
MacroBlockEnd: "BLOCK_END"
 | 
			
		||||
ForEachMacros: ["LIST_FOR_EACH"]
 | 
			
		||||
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -7,5 +7,3 @@
 | 
			
		||||
/liberty.files
 | 
			
		||||
/liberty.creator*
 | 
			
		||||
/liberty.includes
 | 
			
		||||
/liberty.cflags
 | 
			
		||||
/liberty.cxxflags
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
project (liberty C)
 | 
			
		||||
cmake_minimum_required (VERSION 2.8.12)
 | 
			
		||||
cmake_minimum_required (VERSION 2.8.5)
 | 
			
		||||
 | 
			
		||||
# Moar warnings
 | 
			
		||||
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")
 | 
			
		||||
	set (wdisabled "-Wno-unused-function -Wno-implicit-fallthrough")
 | 
			
		||||
	set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra ${wdisabled}")
 | 
			
		||||
endif ()
 | 
			
		||||
endif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
 | 
			
		||||
 | 
			
		||||
# Dependencies
 | 
			
		||||
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
 | 
			
		||||
@@ -16,9 +16,11 @@ find_package (PkgConfig REQUIRED)
 | 
			
		||||
pkg_check_modules (libssl REQUIRED libssl libcrypto)
 | 
			
		||||
 | 
			
		||||
if ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
 | 
			
		||||
	include_directories (/usr/local/include)
 | 
			
		||||
	link_directories (/usr/local/lib)
 | 
			
		||||
	# Our POSIX version macros make these undefined
 | 
			
		||||
	add_definitions (-D__BSD_VISIBLE=1 -D_BSD_SOURCE=1)
 | 
			
		||||
endif ()
 | 
			
		||||
endif ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
 | 
			
		||||
 | 
			
		||||
set (common_libraries ${libssl_LIBRARIES})
 | 
			
		||||
include_directories (${libssl_INCLUDE_DIRS})
 | 
			
		||||
@@ -30,81 +32,15 @@ foreach (extra iconv rt)
 | 
			
		||||
	find_library (extra_lib_${extra} ${extra})
 | 
			
		||||
	if (extra_lib_${extra})
 | 
			
		||||
		list (APPEND common_libraries ${extra})
 | 
			
		||||
	endif ()
 | 
			
		||||
endforeach ()
 | 
			
		||||
	endif (extra_lib_${extra})
 | 
			
		||||
endforeach (extra)
 | 
			
		||||
 | 
			
		||||
# Build some unit tests
 | 
			
		||||
include_directories (${PROJECT_SOURCE_DIR})
 | 
			
		||||
enable_testing ()
 | 
			
		||||
set (tests liberty proto)
 | 
			
		||||
 | 
			
		||||
pkg_check_modules (libpulse libpulse)
 | 
			
		||||
if (libpulse_FOUND)
 | 
			
		||||
	list (APPEND tests pulse)
 | 
			
		||||
	list (APPEND common_libraries ${libpulse_LIBRARIES})
 | 
			
		||||
	include_directories (${libpulse_INCLUDE_DIRS})
 | 
			
		||||
	link_directories (${libpulse_LIBRARY_DIRS})
 | 
			
		||||
endif ()
 | 
			
		||||
 | 
			
		||||
foreach (name ${tests})
 | 
			
		||||
foreach (name liberty proto)
 | 
			
		||||
	add_executable (test-${name} tests/${name}.c ${common_sources})
 | 
			
		||||
	add_threads (test-${name})
 | 
			
		||||
	target_link_libraries (test-${name} ${common_libraries})
 | 
			
		||||
	add_test (NAME test-${name} COMMAND test-${name})
 | 
			
		||||
endforeach ()
 | 
			
		||||
 | 
			
		||||
# --- Tools --------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
# Test the AsciiDoc manual page generator for a successful parse
 | 
			
		||||
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
 | 
			
		||||
	DEPENDS libertyxdr.adoc ${ASCIIMAN}
 | 
			
		||||
	COMMENT "Generating man page for libertyxdr" VERBATIM)
 | 
			
		||||
add_custom_target (docs ALL DEPENDS libertyxdr.7)
 | 
			
		||||
 | 
			
		||||
# 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})
 | 
			
		||||
 | 
			
		||||
# Test protocol code generation
 | 
			
		||||
set (lxdrgen_outputs)
 | 
			
		||||
set (lxdrgen_base "${PROJECT_BINARY_DIR}/lxdrgen.lxdr")
 | 
			
		||||
foreach (backend c go mjs)
 | 
			
		||||
	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
 | 
			
		||||
			-v PrefixCamel=ProtoGen
 | 
			
		||||
			${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
 | 
			
		||||
		COMMENT "Generating test protocol code (${backend})" VERBATIM)
 | 
			
		||||
endforeach ()
 | 
			
		||||
add_custom_target (test-lxdrgen-outputs ALL DEPENDS ${lxdrgen_outputs})
 | 
			
		||||
 | 
			
		||||
set_source_files_properties (${lxdrgen_base}.c
 | 
			
		||||
	PROPERTIES HEADER_FILE_ONLY TRUE)
 | 
			
		||||
add_executable (test-lxdrgen tests/lxdrgen.c ${lxdrgen_base}.c)
 | 
			
		||||
target_include_directories (test-lxdrgen PUBLIC ${PROJECT_BINARY_DIR})
 | 
			
		||||
add_test (NAME test-lxdrgen-c COMMAND test-lxdrgen)
 | 
			
		||||
 | 
			
		||||
find_program (GO_EXECUTABLE go)
 | 
			
		||||
if (GO_EXECUTABLE)
 | 
			
		||||
	add_test (test-lxdrgen-go ${GO_EXECUTABLE} vet ${lxdrgen_base}.go)
 | 
			
		||||
else ()
 | 
			
		||||
	message (WARNING "Cannot test generated protocol code for Go")
 | 
			
		||||
endif ()
 | 
			
		||||
 | 
			
		||||
find_program (NODE_EXECUTABLE node)
 | 
			
		||||
if (NODE_EXECUTABLE)
 | 
			
		||||
	add_test (test-lxdrgen-mjs ${NODE_EXECUTABLE} -c ${lxdrgen_base}.mjs)
 | 
			
		||||
else ()
 | 
			
		||||
	message (WARNING "Cannot test generated protocol code for Javascript")
 | 
			
		||||
endif ()
 | 
			
		||||
endforeach (name)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							@@ -1,4 +1,4 @@
 | 
			
		||||
Copyright (c) 2014 - 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
Copyright (c) 2014 - 2020, 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.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										36
									
								
								README.adoc
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								README.adoc
									
									
									
									
									
								
							@@ -17,42 +17,6 @@ All development is done on Linux, but other POSIX-compatible operating systems
 | 
			
		||||
should be supported as well.  They have an extremely low priority, however, and
 | 
			
		||||
I'm not testing them at all, with the exception of OpenBSD.
 | 
			
		||||
 | 
			
		||||
Tools
 | 
			
		||||
-----
 | 
			
		||||
This project also hosts a number of supporting scripts written in portable AWK:
 | 
			
		||||
 | 
			
		||||
asciiman.awk::
 | 
			
		||||
	A fallback manual page generator for AsciiDoc documents,
 | 
			
		||||
	motivated by the hugeness of AsciiDoc's and Asciidoctor's dependency trees.
 | 
			
		||||
	It uses the _man_ macro package.
 | 
			
		||||
 | 
			
		||||
cmake-parser.awk::
 | 
			
		||||
	Parses the CMake language to the extent that is necessary to reliably
 | 
			
		||||
	extract project versions.  Its greatest limitation is its inability
 | 
			
		||||
	to expand variables, which would require a full interpreter.
 | 
			
		||||
 | 
			
		||||
cmake-dump.awk::
 | 
			
		||||
	This can be used in conjunction with the previous script to dump CMake
 | 
			
		||||
	scripts in a normalized format for further processing.
 | 
			
		||||
 | 
			
		||||
lxdrgen.awk::
 | 
			
		||||
	Protocol code generator for a variant of XDR,
 | 
			
		||||
	which is link:libertyxdr.adoc[documented separately].
 | 
			
		||||
	Successfully employed in https://git.janouch.name/p/xK[xK].
 | 
			
		||||
 | 
			
		||||
lxdrgen-c.awk::
 | 
			
		||||
	LibertyXDR backend that builds on top of the C pseudolibrary.
 | 
			
		||||
 | 
			
		||||
lxdrgen-go.awk::
 | 
			
		||||
	LibertyXDR backend for Go, supporting _encoding/json_ interfaces.  It also
 | 
			
		||||
	produces optimized JSON marshallers (however, note that the _json.Marshaler_
 | 
			
		||||
	interface is bound to be underperforming, due to the amount of otherwise
 | 
			
		||||
	avoidable memory allocations it necessitates).
 | 
			
		||||
 | 
			
		||||
lxdrgen-mjs.awk::
 | 
			
		||||
	LibertyXDR backend for Javascript, currently for decoding only.
 | 
			
		||||
	It cuts a corner by not using BigInts, on par with `JSON.parse()`.
 | 
			
		||||
 | 
			
		||||
Contributing and Support
 | 
			
		||||
------------------------
 | 
			
		||||
Use https://git.janouch.name/p/liberty to report any bugs, request features,
 | 
			
		||||
 
 | 
			
		||||
@@ -148,7 +148,7 @@ irc_free_message (struct irc_message *msg)
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
irc_process_buffer (struct str *buf,
 | 
			
		||||
	void (*callback) (const struct irc_message *, const char *, void *),
 | 
			
		||||
	void (*callback)(const struct irc_message *, const char *, void *),
 | 
			
		||||
	void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	char *start = buf->str, *end = start + buf->len;
 | 
			
		||||
@@ -507,7 +507,7 @@ http_parse_upgrade (const char *upgrade, struct http_protocol **out)
 | 
			
		||||
		case HTTP_T_WHITESPACE:
 | 
			
		||||
			break;
 | 
			
		||||
		case HTTP_T_TOKEN:
 | 
			
		||||
			proto = xcalloc (1, sizeof *proto);
 | 
			
		||||
			proto = (struct http_protocol *) xcalloc (1, sizeof *proto);
 | 
			
		||||
			proto->name = xstrdup (t.string.str);
 | 
			
		||||
			LIST_APPEND_WITH_TAIL (list, tail, proto);
 | 
			
		||||
			state = STATE_SLASH;
 | 
			
		||||
@@ -1002,7 +1002,7 @@ fcgi_nv_parser_push (struct fcgi_nv_parser *self, const void *data, size_t len)
 | 
			
		||||
		if (self->input.len < self->name_len)
 | 
			
		||||
			return;
 | 
			
		||||
 | 
			
		||||
		self->name = xmalloc (self->name_len + 1);
 | 
			
		||||
		self->name = (char *) xmalloc (self->name_len + 1);
 | 
			
		||||
		self->name[self->name_len] = '\0';
 | 
			
		||||
		memcpy (self->name, self->input.str, self->name_len);
 | 
			
		||||
		str_remove_slice (&self->input, 0, self->name_len);
 | 
			
		||||
@@ -1012,7 +1012,7 @@ fcgi_nv_parser_push (struct fcgi_nv_parser *self, const void *data, size_t len)
 | 
			
		||||
		if (self->input.len < self->value_len)
 | 
			
		||||
			return;
 | 
			
		||||
 | 
			
		||||
		self->value = xmalloc (self->value_len + 1);
 | 
			
		||||
		self->value = (char *) xmalloc (self->value_len + 1);
 | 
			
		||||
		self->value[self->value_len] = '\0';
 | 
			
		||||
		memcpy (self->value, self->input.str, self->value_len);
 | 
			
		||||
		str_remove_slice (&self->input, 0, self->value_len);
 | 
			
		||||
@@ -1050,7 +1050,7 @@ fcgi_nv_convert (struct str_map *map, struct str *output)
 | 
			
		||||
	while (str_map_iter_next (&iter))
 | 
			
		||||
	{
 | 
			
		||||
		const char *name  = iter.link->key;
 | 
			
		||||
		const char *value = iter.link->data;
 | 
			
		||||
		const char *value = (const char *) iter.link->data;
 | 
			
		||||
		size_t name_len   = iter.link->key_length;
 | 
			
		||||
		size_t value_len  = strlen (value);
 | 
			
		||||
 | 
			
		||||
@@ -1227,7 +1227,7 @@ ws_parser_push (struct ws_parser *self, const void *data, size_t len)
 | 
			
		||||
		self->reserved_1  = (u8 >> 6) &   1;
 | 
			
		||||
		self->reserved_2  = (u8 >> 5) &   1;
 | 
			
		||||
		self->reserved_3  = (u8 >> 4) &   1;
 | 
			
		||||
		self->opcode      =  u8       &  15;
 | 
			
		||||
		self->opcode      = (enum ws_opcode) (u8 & 15);
 | 
			
		||||
 | 
			
		||||
		(void) msg_unpacker_u8 (&unpacker, &u8);
 | 
			
		||||
		self->is_masked   = (u8 >> 7) &   1;
 | 
			
		||||
@@ -1321,6 +1321,7 @@ enum mpd_subsystem
 | 
			
		||||
#define XX(a, b, c) MPD_SUBSYSTEM_ ## a = (1 << b),
 | 
			
		||||
	MPD_SUBSYSTEM_TABLE (XX)
 | 
			
		||||
#undef XX
 | 
			
		||||
	MPD_SUBSYSTEM_MAX
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const char *mpd_subsystem_names[] =
 | 
			
		||||
@@ -1422,9 +1423,9 @@ mpd_client_make (struct poller *poller)
 | 
			
		||||
		.socket = -1,
 | 
			
		||||
		.read_buffer = str_make (),
 | 
			
		||||
		.write_buffer = str_make (),
 | 
			
		||||
		.data = strv_make (),
 | 
			
		||||
		.socket_event = poller_fd_make (poller, -1),
 | 
			
		||||
		.timeout_timer = poller_timer_make (poller),
 | 
			
		||||
		.data = strv_make (),
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1463,7 +1464,7 @@ mpd_client_reset (struct mpd_client *self)
 | 
			
		||||
{
 | 
			
		||||
	// Get rid of all pending tasks to release resources etc.
 | 
			
		||||
	strv_reset (&self->data);
 | 
			
		||||
	struct mpd_response aborted = { .message_text = "Disconnected" };
 | 
			
		||||
	struct mpd_response aborted = { .message_text = (char *) "Disconnected" };
 | 
			
		||||
	while (self->tasks)
 | 
			
		||||
		mpd_client_dispatch (self, &aborted);
 | 
			
		||||
 | 
			
		||||
@@ -1523,7 +1524,8 @@ mpd_client_parse_response (const char *p, struct mpd_response *response)
 | 
			
		||||
	if (errno != 0 || end == p)
 | 
			
		||||
		return false;
 | 
			
		||||
	p = end;
 | 
			
		||||
	if (*p++ != ']' || *p++ != ' ' || *p++ != '{' || !(end = strchr (p, '}')))
 | 
			
		||||
	if (*p++ != ']' || *p++ != ' ' || *p++ != '{'
 | 
			
		||||
	 || !(end = (char *) strchr (p, '}')))
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	response->current_command = xstrndup (p, end - p);
 | 
			
		||||
@@ -1623,7 +1625,7 @@ mpd_client_on_ready (const struct pollfd *pfd, void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	(void) pfd;
 | 
			
		||||
 | 
			
		||||
	struct mpd_client *self = user_data;
 | 
			
		||||
	struct mpd_client *self = (struct mpd_client *) user_data;
 | 
			
		||||
	if (socket_io_try_read  (self->socket, &self->read_buffer)  != SOCKET_IO_OK
 | 
			
		||||
	 || !mpd_client_process_input (self)
 | 
			
		||||
	 || socket_io_try_write (self->socket, &self->write_buffer) != SOCKET_IO_OK)
 | 
			
		||||
@@ -1675,7 +1677,8 @@ mpd_client_add_task
 | 
			
		||||
	// later flushed if an early ACK or OK arrives).
 | 
			
		||||
	hard_assert (!self->in_list);
 | 
			
		||||
 | 
			
		||||
	struct mpd_client_task *task = xcalloc (1, sizeof *self);
 | 
			
		||||
	struct mpd_client_task *task =
 | 
			
		||||
		(struct mpd_client_task *) xcalloc (1, sizeof *self);
 | 
			
		||||
	task->callback = cb;
 | 
			
		||||
	task->user_data = user_data;
 | 
			
		||||
	LIST_APPEND_WITH_TAIL (self->tasks, self->tasks_tail, task);
 | 
			
		||||
@@ -1793,7 +1796,7 @@ mpd_client_on_idle_return (const struct mpd_response *response,
 | 
			
		||||
{
 | 
			
		||||
	(void) response;
 | 
			
		||||
 | 
			
		||||
	struct mpd_client *self = user_data;
 | 
			
		||||
	struct mpd_client *self = (struct mpd_client *) user_data;
 | 
			
		||||
	unsigned subsystems = 0;
 | 
			
		||||
	for (size_t i = 0; i < data->len; i++)
 | 
			
		||||
	{
 | 
			
		||||
@@ -1816,7 +1819,7 @@ static void mpd_client_idle (struct mpd_client *self, unsigned subsystems);
 | 
			
		||||
static void
 | 
			
		||||
mpd_client_on_timeout (void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	struct mpd_client *self = user_data;
 | 
			
		||||
	struct mpd_client *self = (struct mpd_client *) user_data;
 | 
			
		||||
 | 
			
		||||
	// Abort and immediately restore the current idle so that MPD doesn't
 | 
			
		||||
	// disconnect us, even though the documentation says this won't happen.
 | 
			
		||||
@@ -1884,7 +1887,7 @@ mpd_client_destroy_connector (struct mpd_client *self)
 | 
			
		||||
static void
 | 
			
		||||
mpd_client_on_connector_failure (void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	struct mpd_client *self = user_data;
 | 
			
		||||
	struct mpd_client *self = (struct mpd_client *) user_data;
 | 
			
		||||
	mpd_client_destroy_connector (self);
 | 
			
		||||
	mpd_client_fail (self);
 | 
			
		||||
}
 | 
			
		||||
@@ -1895,7 +1898,7 @@ mpd_client_on_connector_connected
 | 
			
		||||
{
 | 
			
		||||
	(void) host;
 | 
			
		||||
 | 
			
		||||
	struct mpd_client *self = user_data;
 | 
			
		||||
	struct mpd_client *self = (struct mpd_client *) user_data;
 | 
			
		||||
	mpd_client_destroy_connector (self);
 | 
			
		||||
	mpd_client_finish_connection (self, socket);
 | 
			
		||||
}
 | 
			
		||||
@@ -1939,7 +1942,8 @@ mpd_client_connect (struct mpd_client *self, const char *address,
 | 
			
		||||
	if (strchr (address, '/'))
 | 
			
		||||
		return mpd_client_connect_unix (self, address, e);
 | 
			
		||||
 | 
			
		||||
	struct connector *connector = xmalloc (sizeof *connector);
 | 
			
		||||
	struct connector *connector =
 | 
			
		||||
		(struct connector *) xmalloc (sizeof *connector);
 | 
			
		||||
	connector_init (connector, self->poller);
 | 
			
		||||
	self->connector = connector;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										348
									
								
								liberty-pulse.c
									
									
									
									
									
								
							
							
						
						
									
										348
									
								
								liberty-pulse.c
									
									
									
									
									
								
							@@ -1,348 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * liberty-pulse.c: PulseAudio mainloop abstraction
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2016 - 2021, 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.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 | 
			
		||||
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 | 
			
		||||
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 | 
			
		||||
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 | 
			
		||||
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 | 
			
		||||
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 | 
			
		||||
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <pulse/mainloop.h>
 | 
			
		||||
 | 
			
		||||
// --- PulseAudio mainloop abstraction -----------------------------------------
 | 
			
		||||
 | 
			
		||||
struct pa_io_event
 | 
			
		||||
{
 | 
			
		||||
	LIST_HEADER (pa_io_event)
 | 
			
		||||
 | 
			
		||||
	pa_mainloop_api *api;               ///< Parent structure
 | 
			
		||||
	struct poller_fd fd;                ///< Underlying FD event
 | 
			
		||||
 | 
			
		||||
	pa_io_event_cb_t dispatch;          ///< Dispatcher
 | 
			
		||||
	pa_io_event_destroy_cb_t free;      ///< Destroyer
 | 
			
		||||
	void *user_data;                    ///< User data
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct pa_time_event
 | 
			
		||||
{
 | 
			
		||||
	LIST_HEADER (pa_time_event)
 | 
			
		||||
 | 
			
		||||
	pa_mainloop_api *api;               ///< Parent structure
 | 
			
		||||
	struct poller_timer timer;          ///< Underlying timer event
 | 
			
		||||
 | 
			
		||||
	pa_time_event_cb_t dispatch;        ///< Dispatcher
 | 
			
		||||
	pa_time_event_destroy_cb_t free;    ///< Destroyer
 | 
			
		||||
	void *user_data;                    ///< User data
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct pa_defer_event
 | 
			
		||||
{
 | 
			
		||||
	LIST_HEADER (pa_defer_event)
 | 
			
		||||
 | 
			
		||||
	pa_mainloop_api *api;               ///< Parent structure
 | 
			
		||||
	struct poller_idle idle;            ///< Underlying idle event
 | 
			
		||||
 | 
			
		||||
	pa_defer_event_cb_t dispatch;       ///< Dispatcher
 | 
			
		||||
	pa_defer_event_destroy_cb_t free;   ///< Destroyer
 | 
			
		||||
	void *user_data;                    ///< User data
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct poller_pa
 | 
			
		||||
{
 | 
			
		||||
	struct poller *poller;              ///< The underlying event loop
 | 
			
		||||
	pa_io_event *io_list;               ///< I/O events
 | 
			
		||||
	pa_time_event *time_list;           ///< Timer events
 | 
			
		||||
	pa_defer_event *defer_list;         ///< Deferred events
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static short
 | 
			
		||||
poller_pa_flags_to_events (pa_io_event_flags_t flags)
 | 
			
		||||
{
 | 
			
		||||
	short result = 0;
 | 
			
		||||
	if (flags & PA_IO_EVENT_ERROR)   result |= POLLERR;
 | 
			
		||||
	if (flags & PA_IO_EVENT_HANGUP)  result |= POLLHUP;
 | 
			
		||||
	if (flags & PA_IO_EVENT_INPUT)   result |= POLLIN;
 | 
			
		||||
	if (flags & PA_IO_EVENT_OUTPUT)  result |= POLLOUT;
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static pa_io_event_flags_t
 | 
			
		||||
poller_pa_events_to_flags (short events)
 | 
			
		||||
{
 | 
			
		||||
	pa_io_event_flags_t result = 0;
 | 
			
		||||
	if (events & POLLERR)  result |= PA_IO_EVENT_ERROR;
 | 
			
		||||
	if (events & POLLHUP)  result |= PA_IO_EVENT_HANGUP;
 | 
			
		||||
	if (events & POLLIN)   result |= PA_IO_EVENT_INPUT;
 | 
			
		||||
	if (events & POLLOUT)  result |= PA_IO_EVENT_OUTPUT;
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct timeval
 | 
			
		||||
poller_pa_get_current_time (void)
 | 
			
		||||
{
 | 
			
		||||
	struct timeval tv;
 | 
			
		||||
#ifdef _POSIX_TIMERS
 | 
			
		||||
	struct timespec tp;
 | 
			
		||||
	hard_assert (clock_gettime (CLOCK_REALTIME, &tp) != -1);
 | 
			
		||||
	tv.tv_sec = tp.tv_sec;
 | 
			
		||||
	tv.tv_usec = tp.tv_nsec / 1000;
 | 
			
		||||
#else
 | 
			
		||||
	gettimeofday (&tv, NULL);
 | 
			
		||||
#endif
 | 
			
		||||
	return tv;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_io_dispatcher (const struct pollfd *pfd, void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	pa_io_event *self = user_data;
 | 
			
		||||
	self->dispatch (self->api, self,
 | 
			
		||||
		pfd->fd, poller_pa_events_to_flags (pfd->revents), self->user_data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_io_enable (pa_io_event *self, pa_io_event_flags_t events)
 | 
			
		||||
{
 | 
			
		||||
	struct poller_fd *fd = &self->fd;
 | 
			
		||||
	if (events)
 | 
			
		||||
		poller_fd_set (fd, poller_pa_flags_to_events (events));
 | 
			
		||||
	else
 | 
			
		||||
		poller_fd_reset (fd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static pa_io_event *
 | 
			
		||||
poller_pa_io_new (pa_mainloop_api *api, int fd_, pa_io_event_flags_t events,
 | 
			
		||||
	pa_io_event_cb_t cb, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	pa_io_event *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	self->api = api;
 | 
			
		||||
	self->dispatch = cb;
 | 
			
		||||
	self->user_data = userdata;
 | 
			
		||||
 | 
			
		||||
	struct poller_pa *data = api->userdata;
 | 
			
		||||
	self->fd = poller_fd_make (data->poller, fd_);
 | 
			
		||||
	self->fd.user_data = self;
 | 
			
		||||
	self->fd.dispatcher = poller_pa_io_dispatcher;
 | 
			
		||||
 | 
			
		||||
	// FIXME: under x2go PA tries to register twice for the same FD,
 | 
			
		||||
	//   which fails with our curent poller implementation;
 | 
			
		||||
	//   we could maintain a list of { poller_fd, listeners } structures;
 | 
			
		||||
	//   or maybe we're doing something wrong, which is yet to be determined
 | 
			
		||||
	poller_pa_io_enable (self, events);
 | 
			
		||||
	LIST_PREPEND (data->io_list, self);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_io_free (pa_io_event *self)
 | 
			
		||||
{
 | 
			
		||||
	if (self->free)
 | 
			
		||||
		self->free (self->api, self, self->user_data);
 | 
			
		||||
 | 
			
		||||
	struct poller_pa *data = self->api->userdata;
 | 
			
		||||
	poller_fd_reset (&self->fd);
 | 
			
		||||
	LIST_UNLINK (data->io_list, self);
 | 
			
		||||
	free (self);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_io_set_destroy (pa_io_event *self, pa_io_event_destroy_cb_t cb)
 | 
			
		||||
{
 | 
			
		||||
	self->free = cb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_time_dispatcher (void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	pa_time_event *self = user_data;
 | 
			
		||||
	// XXX: the meaning of the time argument is undocumented,
 | 
			
		||||
	//   so let's just put current Unix time in there
 | 
			
		||||
	struct timeval now = poller_pa_get_current_time ();
 | 
			
		||||
	self->dispatch (self->api, self, &now, self->user_data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_time_restart (pa_time_event *self, const struct timeval *tv)
 | 
			
		||||
{
 | 
			
		||||
	struct poller_timer *timer = &self->timer;
 | 
			
		||||
	if (tv)
 | 
			
		||||
	{
 | 
			
		||||
		struct timeval now = poller_pa_get_current_time ();
 | 
			
		||||
		poller_timer_set (timer,
 | 
			
		||||
			(tv->tv_sec  - now.tv_sec)  * 1000 +
 | 
			
		||||
			(tv->tv_usec - now.tv_usec) / 1000);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		poller_timer_reset (timer);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static pa_time_event *
 | 
			
		||||
poller_pa_time_new (pa_mainloop_api *api, const struct timeval *tv,
 | 
			
		||||
	pa_time_event_cb_t cb, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	pa_time_event *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	self->api = api;
 | 
			
		||||
	self->dispatch = cb;
 | 
			
		||||
	self->user_data = userdata;
 | 
			
		||||
 | 
			
		||||
	struct poller_pa *data = api->userdata;
 | 
			
		||||
	self->timer = poller_timer_make (data->poller);
 | 
			
		||||
	self->timer.user_data = self;
 | 
			
		||||
	self->timer.dispatcher = poller_pa_time_dispatcher;
 | 
			
		||||
 | 
			
		||||
	poller_pa_time_restart (self, tv);
 | 
			
		||||
	LIST_PREPEND (data->time_list, self);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_time_free (pa_time_event *self)
 | 
			
		||||
{
 | 
			
		||||
	if (self->free)
 | 
			
		||||
		self->free (self->api, self, self->user_data);
 | 
			
		||||
 | 
			
		||||
	struct poller_pa *data = self->api->userdata;
 | 
			
		||||
	poller_timer_reset (&self->timer);
 | 
			
		||||
	LIST_UNLINK (data->time_list, self);
 | 
			
		||||
	free (self);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_time_set_destroy (pa_time_event *self, pa_time_event_destroy_cb_t cb)
 | 
			
		||||
{
 | 
			
		||||
	self->free = cb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_defer_dispatcher (void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	pa_defer_event *self = user_data;
 | 
			
		||||
	self->dispatch (self->api, self, self->user_data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static pa_defer_event *
 | 
			
		||||
poller_pa_defer_new (pa_mainloop_api *api,
 | 
			
		||||
	pa_defer_event_cb_t cb, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	pa_defer_event *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	self->api = api;
 | 
			
		||||
	self->dispatch = cb;
 | 
			
		||||
	self->user_data = userdata;
 | 
			
		||||
 | 
			
		||||
	struct poller_pa *data = api->userdata;
 | 
			
		||||
	self->idle = poller_idle_make (data->poller);
 | 
			
		||||
	self->idle.user_data = self;
 | 
			
		||||
	self->idle.dispatcher = poller_pa_defer_dispatcher;
 | 
			
		||||
 | 
			
		||||
	poller_idle_set (&self->idle);
 | 
			
		||||
	LIST_PREPEND (data->defer_list, self);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_defer_enable (pa_defer_event *self, int enable)
 | 
			
		||||
{
 | 
			
		||||
	struct poller_idle *idle = &self->idle;
 | 
			
		||||
	if (enable)
 | 
			
		||||
		poller_idle_set (idle);
 | 
			
		||||
	else
 | 
			
		||||
		poller_idle_reset (idle);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_defer_free (pa_defer_event *self)
 | 
			
		||||
{
 | 
			
		||||
	if (self->free)
 | 
			
		||||
		self->free (self->api, self, self->user_data);
 | 
			
		||||
 | 
			
		||||
	struct poller_pa *data = self->api->userdata;
 | 
			
		||||
	poller_idle_reset (&self->idle);
 | 
			
		||||
	LIST_UNLINK (data->defer_list, self);
 | 
			
		||||
	free (self);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_defer_set_destroy (pa_defer_event *self,
 | 
			
		||||
	pa_defer_event_destroy_cb_t cb)
 | 
			
		||||
{
 | 
			
		||||
	self->free = cb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_quit (pa_mainloop_api *api, int retval)
 | 
			
		||||
{
 | 
			
		||||
	(void) api;
 | 
			
		||||
	(void) retval;
 | 
			
		||||
 | 
			
		||||
	// This is not called from within libpulse
 | 
			
		||||
	hard_assert (!"quitting the libpulse event loop is unimplemented");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static struct pa_mainloop_api g_poller_pa_template =
 | 
			
		||||
{
 | 
			
		||||
	.io_new            = poller_pa_io_new,
 | 
			
		||||
	.io_enable         = poller_pa_io_enable,
 | 
			
		||||
	.io_free           = poller_pa_io_free,
 | 
			
		||||
	.io_set_destroy    = poller_pa_io_set_destroy,
 | 
			
		||||
 | 
			
		||||
	.time_new          = poller_pa_time_new,
 | 
			
		||||
	.time_restart      = poller_pa_time_restart,
 | 
			
		||||
	.time_free         = poller_pa_time_free,
 | 
			
		||||
	.time_set_destroy  = poller_pa_time_set_destroy,
 | 
			
		||||
 | 
			
		||||
	.defer_new         = poller_pa_defer_new,
 | 
			
		||||
	.defer_enable      = poller_pa_defer_enable,
 | 
			
		||||
	.defer_free        = poller_pa_defer_free,
 | 
			
		||||
	.defer_set_destroy = poller_pa_defer_set_destroy,
 | 
			
		||||
 | 
			
		||||
	.quit              = poller_pa_quit,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct pa_mainloop_api *
 | 
			
		||||
poller_pa_new (struct poller *self)
 | 
			
		||||
{
 | 
			
		||||
	struct poller_pa *data = xcalloc (1, sizeof *data);
 | 
			
		||||
	data->poller = self;
 | 
			
		||||
 | 
			
		||||
	struct pa_mainloop_api *api = xmalloc (sizeof *api);
 | 
			
		||||
	*api = g_poller_pa_template;
 | 
			
		||||
	api->userdata = data;
 | 
			
		||||
	return api;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_pa_destroy (struct pa_mainloop_api *api)
 | 
			
		||||
{
 | 
			
		||||
	struct poller_pa *data = api->userdata;
 | 
			
		||||
 | 
			
		||||
	LIST_FOR_EACH (pa_io_event, iter, data->io_list)
 | 
			
		||||
		poller_pa_io_free (iter);
 | 
			
		||||
	LIST_FOR_EACH (pa_time_event, iter, data->time_list)
 | 
			
		||||
		poller_pa_time_free (iter);
 | 
			
		||||
	LIST_FOR_EACH (pa_defer_event, iter, data->defer_list)
 | 
			
		||||
		poller_pa_defer_free (iter);
 | 
			
		||||
 | 
			
		||||
	free (data);
 | 
			
		||||
	free (api);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										307
									
								
								liberty.c
									
									
									
									
									
								
							
							
						
						
									
										307
									
								
								liberty.c
									
									
									
									
									
								
							@@ -1,7 +1,7 @@
 | 
			
		||||
/*
 | 
			
		||||
 * liberty.c: the ultimate C unlibrary
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2014 - 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
 * Copyright (c) 2014 - 2020, 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.
 | 
			
		||||
@@ -16,13 +16,8 @@
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef _POSIX_C_SOURCE
 | 
			
		||||
#define _POSIX_C_SOURCE 200112L
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef _XOPEN_SOURCE
 | 
			
		||||
#define _POSIX_C_SOURCE 199309L
 | 
			
		||||
#define _XOPEN_SOURCE 600
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
@@ -122,7 +117,8 @@ extern char **environ;
 | 
			
		||||
#define CONTAINER_OF(pointer, type, member) \
 | 
			
		||||
	((type *) ((char *) pointer - offsetof (type, member)))
 | 
			
		||||
 | 
			
		||||
char *liberty = "They who can give up essential liberty to obtain a little "
 | 
			
		||||
const char *liberty =
 | 
			
		||||
	"They who can give up essential liberty to obtain a little "
 | 
			
		||||
	"temporary safety deserve neither liberty nor safety.";
 | 
			
		||||
 | 
			
		||||
// --- Logging -----------------------------------------------------------------
 | 
			
		||||
@@ -292,7 +288,7 @@ xreallocarray (void *o, size_t n, size_t m)
 | 
			
		||||
static char *
 | 
			
		||||
xstrdup (const char *s)
 | 
			
		||||
{
 | 
			
		||||
	return strcpy (xmalloc (strlen (s) + 1), s);
 | 
			
		||||
	return strcpy ((char *) xmalloc (strlen (s) + 1), s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *
 | 
			
		||||
@@ -302,7 +298,7 @@ xstrndup (const char *s, size_t n)
 | 
			
		||||
	if (n > size)
 | 
			
		||||
		n = size;
 | 
			
		||||
 | 
			
		||||
	char *copy = xmalloc (n + 1);
 | 
			
		||||
	char *copy = (char *) xmalloc (n + 1);
 | 
			
		||||
	memcpy (copy, s, n);
 | 
			
		||||
	copy[n] = '\0';
 | 
			
		||||
	return copy;
 | 
			
		||||
@@ -315,14 +311,15 @@ 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) = (type *) xcalloc (sizeof *(a), (a ## _alloc) = (n));             \
 | 
			
		||||
		(a ## _len) = 0;                                                       \
 | 
			
		||||
	BLOCK_END
 | 
			
		||||
#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
 | 
			
		||||
#define ARRAY_RESERVE(a, n)                                                    \
 | 
			
		||||
	BLOCK_START                                                                \
 | 
			
		||||
		while ((a ## _alloc) - (a ## _len) < n)                                \
 | 
			
		||||
			(a) = xreallocarray ((a), sizeof *(a), (a ## _alloc) <<= 1);       \
 | 
			
		||||
			(a) = (type *) xreallocarray ((a),                                 \
 | 
			
		||||
				sizeof *(a), (a ## _alloc) <<= 1);                             \
 | 
			
		||||
	BLOCK_END
 | 
			
		||||
 | 
			
		||||
// --- Double-linked list helpers ----------------------------------------------
 | 
			
		||||
@@ -397,7 +394,7 @@ strv_make (void)
 | 
			
		||||
	struct strv self;
 | 
			
		||||
	self.alloc = 4;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.vector = xcalloc (sizeof *self.vector, self.alloc);
 | 
			
		||||
	self.vector = (char **) xcalloc (sizeof *self.vector, self.alloc);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -424,7 +421,7 @@ strv_append_owned (struct strv *self, char *s)
 | 
			
		||||
{
 | 
			
		||||
	self->vector[self->len] = s;
 | 
			
		||||
	if (++self->len >= self->alloc)
 | 
			
		||||
		self->vector = xreallocarray (self->vector,
 | 
			
		||||
		self->vector = (char **) xreallocarray (self->vector,
 | 
			
		||||
			sizeof *self->vector, (self->alloc <<= 1));
 | 
			
		||||
	self->vector[self->len] = NULL;
 | 
			
		||||
}
 | 
			
		||||
@@ -497,7 +494,7 @@ str_make (void)
 | 
			
		||||
	struct str self;
 | 
			
		||||
	self.alloc = 16;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.str = strcpy (xmalloc (self.alloc), "");
 | 
			
		||||
	self.str = strcpy ((char *) xmalloc (self.alloc), "");
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -534,7 +531,7 @@ str_reserve (struct str *self, size_t n)
 | 
			
		||||
	while (new_alloc <= self->len + n)
 | 
			
		||||
		new_alloc <<= 1;
 | 
			
		||||
	if (new_alloc != self->alloc)
 | 
			
		||||
		self->str = xrealloc (self->str, (self->alloc = new_alloc));
 | 
			
		||||
		self->str = (char *) xrealloc (self->str, (self->alloc = new_alloc));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -613,7 +610,7 @@ str_remove_slice (struct str *self, size_t start, size_t length)
 | 
			
		||||
 | 
			
		||||
	// Shrink the string if the allocation becomes way too large
 | 
			
		||||
	if (self->alloc >= STR_SHRINK_THRESHOLD && self->len < (self->alloc >> 2))
 | 
			
		||||
		self->str = xrealloc (self->str, self->alloc >>= 2);
 | 
			
		||||
		self->str = (char *) xrealloc (self->str, self->alloc >>= 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
@@ -627,7 +624,11 @@ str_pack_u8 (struct str *self, uint8_t x)
 | 
			
		||||
static void
 | 
			
		||||
str_pack_u16 (struct str *self, uint16_t x)
 | 
			
		||||
{
 | 
			
		||||
	uint8_t tmp[2] = { x >> 8, x };
 | 
			
		||||
	uint8_t tmp[2] =
 | 
			
		||||
	{
 | 
			
		||||
		(uint8_t) (x >> 8),
 | 
			
		||||
		(uint8_t)  x
 | 
			
		||||
	};
 | 
			
		||||
	str_append_data (self, tmp, sizeof tmp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -635,7 +636,13 @@ static void
 | 
			
		||||
str_pack_u32 (struct str *self, uint32_t x)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t u = x;
 | 
			
		||||
	uint8_t tmp[4] = { u >> 24, u >> 16, u >> 8, u };
 | 
			
		||||
	uint8_t tmp[4] =
 | 
			
		||||
	{
 | 
			
		||||
		(uint8_t) (u >> 24),
 | 
			
		||||
		(uint8_t) (u >> 16),
 | 
			
		||||
		(uint8_t) (u >>  8),
 | 
			
		||||
		(uint8_t)  u
 | 
			
		||||
	};
 | 
			
		||||
	str_append_data (self, tmp, sizeof tmp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -643,7 +650,16 @@ static void
 | 
			
		||||
str_pack_u64 (struct str *self, uint64_t x)
 | 
			
		||||
{
 | 
			
		||||
	uint8_t tmp[8] =
 | 
			
		||||
		{ x >> 56, x >> 48, x >> 40, x >> 32, x >> 24, x >> 16, x >> 8, x };
 | 
			
		||||
	{
 | 
			
		||||
		(uint8_t) (x >> 56),
 | 
			
		||||
		(uint8_t) (x >> 48),
 | 
			
		||||
		(uint8_t) (x >> 40),
 | 
			
		||||
		(uint8_t) (x >> 32),
 | 
			
		||||
		(uint8_t) (x >> 24),
 | 
			
		||||
		(uint8_t) (x >> 16),
 | 
			
		||||
		(uint8_t) (x >>  8),
 | 
			
		||||
		(uint8_t)  x
 | 
			
		||||
	};
 | 
			
		||||
	str_append_data (self, tmp, sizeof tmp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -677,8 +693,8 @@ error_set (struct error **e, const char *message, ...)
 | 
			
		||||
 | 
			
		||||
	hard_assert (size >= 0);
 | 
			
		||||
 | 
			
		||||
	struct error *tmp = xmalloc (sizeof *tmp);
 | 
			
		||||
	tmp->message = xmalloc (size + 1);
 | 
			
		||||
	struct error *tmp = (struct error *) xmalloc (sizeof *tmp);
 | 
			
		||||
	tmp->message = (char *) xmalloc (size + 1);
 | 
			
		||||
 | 
			
		||||
	va_start (ap, message);
 | 
			
		||||
	size = vsnprintf (tmp->message, size + 1, message, ap);
 | 
			
		||||
@@ -783,7 +799,7 @@ random_bytes (void *output, size_t len, struct error **e)
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static unsigned char g_siphash_key[16] = "SipHash 2-4 key!";
 | 
			
		||||
static unsigned char g_siphash_key[16] = "SipHash 2-4 key" /* \0 */;
 | 
			
		||||
 | 
			
		||||
static inline void
 | 
			
		||||
siphash_wrapper_randomize (void)
 | 
			
		||||
@@ -795,7 +811,7 @@ siphash_wrapper_randomize (void)
 | 
			
		||||
static inline uint64_t
 | 
			
		||||
siphash_wrapper (const void *m, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	return siphash (g_siphash_key, m, len);
 | 
			
		||||
	return siphash (g_siphash_key, (const unsigned char *) m, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// --- String hash map ---------------------------------------------------------
 | 
			
		||||
@@ -837,7 +853,7 @@ str_map_make (str_map_free_fn free)
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.free = free;
 | 
			
		||||
	self.key_xfrm = NULL;
 | 
			
		||||
	self.map = xcalloc (self.alloc, sizeof *self.map);
 | 
			
		||||
	self.map = (struct str_map_link **) xcalloc (self.alloc, sizeof *self.map);
 | 
			
		||||
	self.shrink_lock = false;
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
@@ -893,7 +909,8 @@ str_map_resize (struct str_map *self, size_t new_size)
 | 
			
		||||
	size_t mask = new_size - 1;
 | 
			
		||||
 | 
			
		||||
	self->alloc = new_size;
 | 
			
		||||
	self->map = xcalloc (self->alloc, sizeof *self->map);
 | 
			
		||||
	self->map =
 | 
			
		||||
		(struct str_map_link **) xcalloc (self->alloc, sizeof *self->map);
 | 
			
		||||
	for (i = 0; i < old_size; i++)
 | 
			
		||||
	{
 | 
			
		||||
		struct str_map_link *iter = old_map[i], *next_iter;
 | 
			
		||||
@@ -963,7 +980,8 @@ str_map_set_real (struct str_map *self, const char *key, void *value)
 | 
			
		||||
 | 
			
		||||
	// Link in a new element for the given <key, value> pair
 | 
			
		||||
	size_t key_length = strlen (key);
 | 
			
		||||
	struct str_map_link *link = xmalloc (sizeof *link + key_length + 1);
 | 
			
		||||
	struct str_map_link *link =
 | 
			
		||||
		(struct str_map_link *) xmalloc (sizeof *link + key_length + 1);
 | 
			
		||||
	link->data = value;
 | 
			
		||||
	link->key_length = key_length;
 | 
			
		||||
	memcpy (link->key, key, key_length + 1);
 | 
			
		||||
@@ -1166,7 +1184,7 @@ async_cancel (struct async *self)
 | 
			
		||||
static void
 | 
			
		||||
async_cleanup (void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	struct async *self = user_data;
 | 
			
		||||
	struct async *self = (struct async *) user_data;
 | 
			
		||||
 | 
			
		||||
	hard_assert (!pthread_mutex_lock (&self->manager->lock));
 | 
			
		||||
	LIST_UNLINK (self->manager->running, self);
 | 
			
		||||
@@ -1182,7 +1200,7 @@ async_routine (void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	// Beware that we mustn't trigger any cancellation point before we set up
 | 
			
		||||
	// the cleanup handler, otherwise we'd need to disable it first
 | 
			
		||||
	struct async *self = user_data;
 | 
			
		||||
	struct async *self = (struct async *) user_data;
 | 
			
		||||
	pthread_cleanup_push (async_cleanup, self);
 | 
			
		||||
 | 
			
		||||
	self->execute (self);
 | 
			
		||||
@@ -1391,7 +1409,8 @@ poller_timers_make (void)
 | 
			
		||||
	struct poller_timers self;
 | 
			
		||||
	self.alloc = POLLER_MIN_ALLOC;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.heap = xmalloc (self.alloc * sizeof *self.heap);
 | 
			
		||||
	self.heap =
 | 
			
		||||
		(struct poller_timer **) xmalloc (self.alloc * sizeof *self.heap);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1501,7 +1520,7 @@ poller_timers_set (struct poller_timers *self, struct poller_timer *timer)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (self->len == self->alloc)
 | 
			
		||||
		self->heap = xreallocarray (self->heap,
 | 
			
		||||
		self->heap = (struct poller_timer **) xreallocarray (self->heap,
 | 
			
		||||
			self->alloc <<= 1, sizeof *self->heap);
 | 
			
		||||
	self->heap[self->len] = timer;
 | 
			
		||||
	timer->index = self->len;
 | 
			
		||||
@@ -1578,9 +1597,10 @@ poller_init (struct poller *self)
 | 
			
		||||
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->alloc = POLLER_MIN_ALLOC;
 | 
			
		||||
	self->fds = xcalloc (self->alloc, sizeof *self->fds);
 | 
			
		||||
	self->dummy = xcalloc (self->alloc, sizeof *self->dummy);
 | 
			
		||||
	self->revents = xcalloc (self->alloc, sizeof *self->revents);
 | 
			
		||||
	self->fds = (struct poller_fd **) xcalloc (self->alloc, sizeof *self->fds);
 | 
			
		||||
	self->dummy = (int *) xcalloc (self->alloc, sizeof *self->dummy);
 | 
			
		||||
	self->revents = (struct epoll_event *)
 | 
			
		||||
		xcalloc (self->alloc, sizeof *self->revents);
 | 
			
		||||
	self->revents_len = 0;
 | 
			
		||||
 | 
			
		||||
	poller_common_init (&self->common, self);
 | 
			
		||||
@@ -1593,7 +1613,7 @@ poller_free (struct poller *self)
 | 
			
		||||
	{
 | 
			
		||||
		struct poller_fd *fd = self->fds[i];
 | 
			
		||||
		hard_assert (epoll_ctl (self->epoll_fd,
 | 
			
		||||
			EPOLL_CTL_DEL, fd->fd, (void *) "") != -1);
 | 
			
		||||
			EPOLL_CTL_DEL, fd->fd, (struct epoll_event *) "") != -1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	poller_common_free (&self->common);
 | 
			
		||||
@@ -1613,11 +1633,11 @@ poller_ensure_space (struct poller *self)
 | 
			
		||||
	self->alloc <<= 1;
 | 
			
		||||
	hard_assert (self->alloc != 0);
 | 
			
		||||
 | 
			
		||||
	self->revents = xreallocarray
 | 
			
		||||
	self->revents = (struct epoll_event *) xreallocarray
 | 
			
		||||
		(self->revents, sizeof *self->revents, self->alloc);
 | 
			
		||||
	self->fds = xreallocarray
 | 
			
		||||
	self->fds = (struct poller_fd **) xreallocarray
 | 
			
		||||
		(self->fds, sizeof *self->fds, self->alloc);
 | 
			
		||||
	self->dummy = xreallocarray
 | 
			
		||||
	self->dummy = (int *) xreallocarray
 | 
			
		||||
		(self->dummy, sizeof *self->dummy, self->alloc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1669,8 +1689,12 @@ poller_set (struct poller *self, struct poller_fd *fd)
 | 
			
		||||
static int
 | 
			
		||||
poller_compare_fds (const void *ax, const void *bx)
 | 
			
		||||
{
 | 
			
		||||
	const struct epoll_event *ay = ax, *by = bx;
 | 
			
		||||
	struct poller_fd *a = ay->data.ptr, *b = by->data.ptr;
 | 
			
		||||
	const struct epoll_event
 | 
			
		||||
		*ay = (const struct epoll_event *) ax,
 | 
			
		||||
		*by = (const struct epoll_event *) bx;
 | 
			
		||||
	struct poller_fd
 | 
			
		||||
		*a = (struct poller_fd *) ay->data.ptr,
 | 
			
		||||
		*b = (struct poller_fd *) by->data.ptr;
 | 
			
		||||
	return a->fd - b->fd;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1681,7 +1705,7 @@ poller_remove_from_dispatch (struct poller *self, const struct poller_fd *fd)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	struct epoll_event key = { .data.ptr = (void *) fd }, *fd_event;
 | 
			
		||||
	if ((fd_event = bsearch (&key, self->revents,
 | 
			
		||||
	if ((fd_event = (struct epoll_event *) bsearch (&key, self->revents,
 | 
			
		||||
		self->revents_len, sizeof *self->revents, poller_compare_fds)))
 | 
			
		||||
	{
 | 
			
		||||
		fd_event->events = -1;
 | 
			
		||||
@@ -1704,7 +1728,7 @@ poller_remove_at_index (struct poller *self, size_t index)
 | 
			
		||||
	poller_remove_from_dispatch (self, fd);
 | 
			
		||||
	if (!fd->closed)
 | 
			
		||||
		hard_assert (epoll_ctl (self->epoll_fd,
 | 
			
		||||
			EPOLL_CTL_DEL, fd->fd, (void *) "") != -1);
 | 
			
		||||
			EPOLL_CTL_DEL, fd->fd, (struct epoll_event *) "") != -1);
 | 
			
		||||
 | 
			
		||||
	if (index != --self->len)
 | 
			
		||||
	{
 | 
			
		||||
@@ -1740,7 +1764,7 @@ poller_run (struct poller *self)
 | 
			
		||||
		if (revents->events == (uint32_t) -1)
 | 
			
		||||
			continue;
 | 
			
		||||
 | 
			
		||||
		struct poller_fd *fd = revents->data.ptr;
 | 
			
		||||
		struct poller_fd *fd = (struct poller_fd *) revents->data.ptr;
 | 
			
		||||
		hard_assert (fd->index != -1);
 | 
			
		||||
 | 
			
		||||
		struct pollfd pfd;
 | 
			
		||||
@@ -1754,9 +1778,10 @@ poller_run (struct poller *self)
 | 
			
		||||
	self->revents_len = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Sort of similar to the epoll version.  Let's hope Darwin isn't broken,
 | 
			
		||||
// that'd mean reimplementing this in terms of select() just because of Crapple.
 | 
			
		||||
#elif defined (BSD) || defined (__APPLE__)
 | 
			
		||||
#elif defined (BSD)
 | 
			
		||||
 | 
			
		||||
// Mac OS X's kqueue is fatally broken, or so I've been told; leaving it out.
 | 
			
		||||
// Otherwise this is sort of similar to the epoll version.
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/event.h>
 | 
			
		||||
@@ -1782,8 +1807,9 @@ poller_init (struct poller *self)
 | 
			
		||||
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->alloc = POLLER_MIN_ALLOC;
 | 
			
		||||
	self->fds = xcalloc (self->alloc, sizeof *self->fds);
 | 
			
		||||
	self->revents = xcalloc (self->alloc, sizeof *self->revents);
 | 
			
		||||
	self->fds = (struct poller_fd **) xcalloc (self->alloc, sizeof *self->fds);
 | 
			
		||||
	self->revents = (struct kevent *)
 | 
			
		||||
		xcalloc (self->alloc, sizeof *self->revents);
 | 
			
		||||
	self->revents_len = 0;
 | 
			
		||||
	poller_common_init (&self->common, self);
 | 
			
		||||
}
 | 
			
		||||
@@ -1806,9 +1832,9 @@ poller_ensure_space (struct poller *self)
 | 
			
		||||
	self->alloc <<= 1;
 | 
			
		||||
	hard_assert (self->alloc != 0);
 | 
			
		||||
 | 
			
		||||
	self->revents = xreallocarray
 | 
			
		||||
	self->revents = (struct kevent *) xreallocarray
 | 
			
		||||
		(self->revents, sizeof *self->revents, self->alloc);
 | 
			
		||||
	self->fds = xreallocarray
 | 
			
		||||
	self->fds = (struct poller_fd **) xreallocarray
 | 
			
		||||
		(self->fds, sizeof *self->fds, self->alloc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -2008,8 +2034,9 @@ poller_init (struct poller *self)
 | 
			
		||||
{
 | 
			
		||||
	self->alloc = POLLER_MIN_ALLOC;
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->fds = xcalloc (self->alloc, sizeof *self->fds);
 | 
			
		||||
	self->fds_data = xcalloc (self->alloc, sizeof *self->fds_data);
 | 
			
		||||
	self->fds = (struct pollfd **) xcalloc (self->alloc, sizeof *self->fds);
 | 
			
		||||
	self->fds_data = (struct poller_fd **)
 | 
			
		||||
		xcalloc (self->alloc, sizeof *self->fds_data);
 | 
			
		||||
	poller_common_init (&self->common, self);
 | 
			
		||||
	self->dispatch_next = -1;
 | 
			
		||||
}
 | 
			
		||||
@@ -2029,8 +2056,9 @@ poller_ensure_space (struct poller *self)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	self->alloc <<= 1;
 | 
			
		||||
	self->fds = xreallocarray (self->fds, sizeof *self->fds, self->alloc);
 | 
			
		||||
	self->fds_data = xreallocarray
 | 
			
		||||
	self->fds = (struct pollfd *)
 | 
			
		||||
		xreallocarray (self->fds, sizeof *self->fds, self->alloc);
 | 
			
		||||
	self->fds_data = (struct poller_fd **) xreallocarray
 | 
			
		||||
		(self->fds_data, sizeof *self->fds_data, self->alloc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -2314,7 +2342,8 @@ static struct async_getaddrinfo *
 | 
			
		||||
async_getaddrinfo (struct async_manager *manager,
 | 
			
		||||
	const char *host, const char *service, const struct addrinfo *hints)
 | 
			
		||||
{
 | 
			
		||||
	struct async_getaddrinfo *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	struct async_getaddrinfo *self =
 | 
			
		||||
		(struct async_getaddrinfo *) xcalloc (1, sizeof *self);
 | 
			
		||||
	self->async = async_make (manager);
 | 
			
		||||
 | 
			
		||||
	if (host)     self->host = xstrdup (host);
 | 
			
		||||
@@ -2378,10 +2407,11 @@ static struct async_getnameinfo *
 | 
			
		||||
async_getnameinfo (struct async_manager *manager,
 | 
			
		||||
	const struct sockaddr *sa, socklen_t sa_len, int flags)
 | 
			
		||||
{
 | 
			
		||||
	struct async_getnameinfo *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	struct async_getnameinfo *self =
 | 
			
		||||
		(struct async_getnameinfo *) xcalloc (1, sizeof *self);
 | 
			
		||||
	self->async = async_make (manager);
 | 
			
		||||
 | 
			
		||||
	self->address = memcpy (xmalloc (sa_len), sa, sa_len);
 | 
			
		||||
	self->address = (struct sockaddr *) memcpy (xmalloc (sa_len), sa, sa_len);
 | 
			
		||||
	self->address_len = sa_len;
 | 
			
		||||
	self->flags = flags;
 | 
			
		||||
 | 
			
		||||
@@ -2455,7 +2485,7 @@ write_queue_processed (struct write_queue *self, size_t len)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
write_queue_is_empty (const struct write_queue *self)
 | 
			
		||||
write_queue_is_empty (struct write_queue *self)
 | 
			
		||||
{
 | 
			
		||||
	return self->head == NULL;
 | 
			
		||||
}
 | 
			
		||||
@@ -2546,11 +2576,16 @@ struct msg_unpacker
 | 
			
		||||
static struct msg_unpacker
 | 
			
		||||
msg_unpacker_make (const void *data, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	return (struct msg_unpacker) { .data = data, .len = len, .offset = 0 };
 | 
			
		||||
	return (struct msg_unpacker)
 | 
			
		||||
	{
 | 
			
		||||
		.data = (const char *) data,
 | 
			
		||||
		.offset = 0,
 | 
			
		||||
		.len = len
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static size_t
 | 
			
		||||
msg_unpacker_get_available (const struct msg_unpacker *self)
 | 
			
		||||
msg_unpacker_get_available (struct msg_unpacker *self)
 | 
			
		||||
{
 | 
			
		||||
	return self->len - self->offset;
 | 
			
		||||
}
 | 
			
		||||
@@ -2635,8 +2670,16 @@ msg_writer_flush (struct msg_writer *self, size_t *len)
 | 
			
		||||
{
 | 
			
		||||
	// Update the message length
 | 
			
		||||
	uint64_t x = self->buf.len;
 | 
			
		||||
	uint8_t tmp[8] =
 | 
			
		||||
		{ x >> 56, x >> 48, x >> 40, x >> 32, x >> 24, x >> 16, x >> 8, x };
 | 
			
		||||
	uint8_t tmp[8] = {
 | 
			
		||||
		(uint8_t) (x >> 56),
 | 
			
		||||
		(uint8_t) (x >> 48),
 | 
			
		||||
		(uint8_t) (x >> 40),
 | 
			
		||||
		(uint8_t) (x >> 32),
 | 
			
		||||
		(uint8_t) (x >> 24),
 | 
			
		||||
		(uint8_t) (x >> 16),
 | 
			
		||||
		(uint8_t) (x >>  8),
 | 
			
		||||
		(uint8_t)  x
 | 
			
		||||
	};
 | 
			
		||||
	memcpy (self->buf.str, tmp, sizeof tmp);
 | 
			
		||||
 | 
			
		||||
	*len = x;
 | 
			
		||||
@@ -2752,12 +2795,16 @@ utf8_decode (const char **s, size_t len)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// In the middle of a character
 | 
			
		||||
	// or an overlong sequence (subset, possibly MUTF-8, not supported)
 | 
			
		||||
	if (sequence_len == 1 || *p == 0xC0 || *p == 0xC1)
 | 
			
		||||
	if (sequence_len == 1)
 | 
			
		||||
		return -1;
 | 
			
		||||
 | 
			
		||||
	// Check the rest of the sequence
 | 
			
		||||
	uint32_t cp = *p++ & ~mask;
 | 
			
		||||
 | 
			
		||||
	// Overlong sequence (possibly MUTF-8, not supported)
 | 
			
		||||
	if (!cp && sequence_len)
 | 
			
		||||
		return -1;
 | 
			
		||||
 | 
			
		||||
	while (sequence_len && --sequence_len)
 | 
			
		||||
	{
 | 
			
		||||
		if (p == end)
 | 
			
		||||
@@ -2911,7 +2958,7 @@ base64_encode (const void *data, size_t len, struct str *output)
 | 
			
		||||
	const char *alphabet =
 | 
			
		||||
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 | 
			
		||||
 | 
			
		||||
	const uint8_t *p = data;
 | 
			
		||||
	const uint8_t *p = (const uint8_t *) data;
 | 
			
		||||
	size_t n_groups = len / 3;
 | 
			
		||||
	size_t tail = len - n_groups * 3;
 | 
			
		||||
	uint32_t group;
 | 
			
		||||
@@ -2948,10 +2995,10 @@ base64_encode (const void *data, size_t len, struct str *output)
 | 
			
		||||
// --- Utilities ---------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cstr_set (char **s, char *new)
 | 
			
		||||
cstr_set (char **s, char *new_)
 | 
			
		||||
{
 | 
			
		||||
	free (*s);
 | 
			
		||||
	*s = new;
 | 
			
		||||
	*s = new_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -3029,7 +3076,7 @@ iconv_xstrdup (iconv_t conv, char *in, size_t in_len, size_t *out_len)
 | 
			
		||||
	char *buf, *buf_ptr;
 | 
			
		||||
	size_t out_left, buf_alloc;
 | 
			
		||||
 | 
			
		||||
	buf = buf_ptr = xmalloc (out_left = buf_alloc = 64);
 | 
			
		||||
	buf = buf_ptr = (char *) xmalloc (out_left = buf_alloc = 64);
 | 
			
		||||
 | 
			
		||||
	char *in_ptr = in;
 | 
			
		||||
	if (in_len == (size_t) -1)
 | 
			
		||||
@@ -3045,7 +3092,7 @@ iconv_xstrdup (iconv_t conv, char *in, size_t in_len, size_t *out_len)
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
		out_left += buf_alloc;
 | 
			
		||||
		char *new_buf = xrealloc (buf, buf_alloc <<= 1);
 | 
			
		||||
		char *new_buf = (char *) xrealloc (buf, buf_alloc <<= 1);
 | 
			
		||||
		buf_ptr += new_buf - buf;
 | 
			
		||||
		buf = new_buf;
 | 
			
		||||
	}
 | 
			
		||||
@@ -3126,8 +3173,8 @@ lock_pid_file (const char *path, struct error **e)
 | 
			
		||||
	struct flock lock =
 | 
			
		||||
	{
 | 
			
		||||
		.l_type = F_WRLCK,
 | 
			
		||||
		.l_start = 0,
 | 
			
		||||
		.l_whence = SEEK_SET,
 | 
			
		||||
		.l_start = 0,
 | 
			
		||||
		.l_len = 0,
 | 
			
		||||
	};
 | 
			
		||||
	if (fcntl (fd, F_SETLK, &lock))
 | 
			
		||||
@@ -3251,7 +3298,7 @@ get_xdg_config_dirs (struct strv *out)
 | 
			
		||||
	str_free (&config_home);
 | 
			
		||||
 | 
			
		||||
	const char *xdg_config_dirs;
 | 
			
		||||
	if (!(xdg_config_dirs = getenv ("XDG_CONFIG_DIRS")) || !*xdg_config_dirs)
 | 
			
		||||
	if (!(xdg_config_dirs = getenv ("XDG_CONFIG_DIRS")))
 | 
			
		||||
		xdg_config_dirs = "/etc/xdg";
 | 
			
		||||
	cstr_split (xdg_config_dirs, ":", true, out);
 | 
			
		||||
}
 | 
			
		||||
@@ -3276,7 +3323,7 @@ get_xdg_data_dirs (struct strv *out)
 | 
			
		||||
	str_free (&data_home);
 | 
			
		||||
 | 
			
		||||
	const char *xdg_data_dirs;
 | 
			
		||||
	if (!(xdg_data_dirs = getenv ("XDG_DATA_DIRS")) || !*xdg_data_dirs)
 | 
			
		||||
	if (!(xdg_data_dirs = getenv ("XDG_DATA_DIRS")))
 | 
			
		||||
		xdg_data_dirs = "/usr/local/share/:/usr/share/";
 | 
			
		||||
	cstr_split (xdg_data_dirs, ":", true, out);
 | 
			
		||||
}
 | 
			
		||||
@@ -3326,7 +3373,7 @@ resolve_relative_runtime_filename (const char *filename)
 | 
			
		||||
/// path can reside in a system-wide directory with no risk of a conflict.
 | 
			
		||||
/// However, we have to take care about permissions.  Do we even need this?
 | 
			
		||||
static char *
 | 
			
		||||
resolve_relative_runtime_template (const char *template)
 | 
			
		||||
resolve_relative_runtime_template (const char *template_)
 | 
			
		||||
{
 | 
			
		||||
	struct str path = str_make ();
 | 
			
		||||
	const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
 | 
			
		||||
@@ -3338,7 +3385,7 @@ resolve_relative_runtime_template (const char *template)
 | 
			
		||||
	else
 | 
			
		||||
		str_append_printf (&path, "/tmp/%s.%d", PROGRAM_NAME, geteuid ());
 | 
			
		||||
 | 
			
		||||
	str_append_printf (&path, "/%s", template);
 | 
			
		||||
	str_append_printf (&path, "/%s", template_);
 | 
			
		||||
	return resolve_relative_runtime_filename_finish (path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -3360,9 +3407,9 @@ try_expand_tilde (const char *filename)
 | 
			
		||||
	struct passwd pwd, *success = NULL;
 | 
			
		||||
 | 
			
		||||
	char *user = xstrndup (filename, until_slash);
 | 
			
		||||
	char *buf = xmalloc (buf_len);
 | 
			
		||||
	char *buf = (char *) xmalloc (buf_len);
 | 
			
		||||
	while (getpwnam_r (user, &pwd, buf, buf_len, &success) == ERANGE)
 | 
			
		||||
		buf = xrealloc (buf, buf_len <<= 1);
 | 
			
		||||
		buf = (char *) xrealloc (buf, buf_len <<= 1);
 | 
			
		||||
	free (user);
 | 
			
		||||
 | 
			
		||||
	char *result = NULL;
 | 
			
		||||
@@ -3438,7 +3485,7 @@ xssl_get_error (SSL *ssl, int result, const char **error_info)
 | 
			
		||||
static regex_t *
 | 
			
		||||
regex_compile (const char *regex, int flags, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	regex_t *re = xmalloc (sizeof *re);
 | 
			
		||||
	regex_t *re = (regex_t *) xmalloc (sizeof *re);
 | 
			
		||||
	int err = regcomp (re, regex, flags);
 | 
			
		||||
	if (!err)
 | 
			
		||||
		return re;
 | 
			
		||||
@@ -3454,7 +3501,7 @@ regex_compile (const char *regex, int flags, struct error **e)
 | 
			
		||||
static void
 | 
			
		||||
regex_free (void *regex)
 | 
			
		||||
{
 | 
			
		||||
	regfree (regex);
 | 
			
		||||
	regfree ((regex_t *) regex);
 | 
			
		||||
	free (regex);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -3473,7 +3520,7 @@ static bool
 | 
			
		||||
regex_cache_match (struct str_map *cache, const char *regex, int flags,
 | 
			
		||||
	const char *s, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	regex_t *re = str_map_find (cache, regex);
 | 
			
		||||
	regex_t *re = (regex_t *) str_map_find (cache, regex);
 | 
			
		||||
	if (!re)
 | 
			
		||||
	{
 | 
			
		||||
		re = regex_compile (regex, flags, e);
 | 
			
		||||
@@ -3743,7 +3790,7 @@ opt_handler_make (int argc, char **argv,
 | 
			
		||||
 | 
			
		||||
	self.opts = opts;
 | 
			
		||||
	self.opts_len = len;
 | 
			
		||||
	self.options = xcalloc (len + 1, sizeof *self.options);
 | 
			
		||||
	self.options = (struct option *) xcalloc (len + 1, sizeof *self.options);
 | 
			
		||||
 | 
			
		||||
	struct str opt_string = str_make ();
 | 
			
		||||
	for (size_t i = 0; i < len; i++)
 | 
			
		||||
@@ -3921,7 +3968,7 @@ test_add_internal (struct test *self, const char *name, size_t fixture_size,
 | 
			
		||||
	hard_assert (test != NULL);
 | 
			
		||||
	hard_assert (name != NULL);
 | 
			
		||||
 | 
			
		||||
	struct test_unit *unit = xcalloc (1, sizeof *unit);
 | 
			
		||||
	struct test_unit *unit = (struct test_unit *) xcalloc (1, sizeof *unit);
 | 
			
		||||
	unit->name = xstrdup (name);
 | 
			
		||||
	unit->fixture_size = fixture_size;
 | 
			
		||||
	unit->user_data = user_data;
 | 
			
		||||
@@ -4074,7 +4121,8 @@ struct connector_target
 | 
			
		||||
static struct connector_target *
 | 
			
		||||
connector_target_new (void)
 | 
			
		||||
{
 | 
			
		||||
	struct connector_target *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	struct connector_target *self =
 | 
			
		||||
		(struct connector_target *) xcalloc (1, sizeof *self);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -4287,7 +4335,7 @@ connector_free (struct connector *self)
 | 
			
		||||
static void
 | 
			
		||||
connector_on_getaddrinfo (int err, struct addrinfo *results, void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	struct connector_target *self = user_data;
 | 
			
		||||
	struct connector_target *self = (struct connector_target *) user_data;
 | 
			
		||||
 | 
			
		||||
	if (err)
 | 
			
		||||
	{
 | 
			
		||||
@@ -4417,13 +4465,11 @@ socket_io_try_write (int socket_fd, struct str *wb)
 | 
			
		||||
//   object  = lws '{' entries endobj
 | 
			
		||||
//   endobj  = lws '}'
 | 
			
		||||
//
 | 
			
		||||
//   quoted  = lws '"' (!["\\] char / '\\' escape)* '"'
 | 
			
		||||
//           / lws '`' (![`] char)* '`'
 | 
			
		||||
//   string  = (quoted)+
 | 
			
		||||
//   string  = lws '"' ('\\' escape / ![\\"] char)* '"'
 | 
			
		||||
//   char    = [\0-\177]  # or any Unicode codepoint in the UTF-8 encoding
 | 
			
		||||
//   escape  = [\\"abfnrtv] / [xX][0-9A-Fa-f][0-9A-Fa-f]? / [0-7][0-7]?[0-7]?
 | 
			
		||||
//
 | 
			
		||||
//   integer = lws [-+]? [0-9]+  # whatever strtoll() accepts on your system
 | 
			
		||||
//   integer = lws '-'? [0-9]+  # whatever strtoll() accepts on your system
 | 
			
		||||
//   null    = lws 'null'
 | 
			
		||||
//   boolean = lws 'yes'  / lws 'YES'  / lws 'no'    / lws 'NO'
 | 
			
		||||
//           / lws 'on'   / lws 'ON'   / lws 'off'   / lws 'OFF'
 | 
			
		||||
@@ -4542,7 +4588,8 @@ config_item_move (struct config_item *self, struct config_item *source)
 | 
			
		||||
static struct config_item *
 | 
			
		||||
config_item_new (enum config_item_type type)
 | 
			
		||||
{
 | 
			
		||||
	struct config_item *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	struct config_item *self =
 | 
			
		||||
		(struct config_item *) xcalloc (1, sizeof *self);
 | 
			
		||||
	self->type = type;
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
@@ -4682,7 +4729,8 @@ config_item_get (struct config_item *self, const char *path, struct error **e)
 | 
			
		||||
		const char *key = v.vector[i];
 | 
			
		||||
		if (!*key)
 | 
			
		||||
			error_set (e, "empty path element");
 | 
			
		||||
		else if (!(self = str_map_find (&self->value.object, key)))
 | 
			
		||||
		else if (!(self = (struct config_item *)
 | 
			
		||||
			str_map_find (&self->value.object, key)))
 | 
			
		||||
			error_set (e, "`%s' not found in object", key);
 | 
			
		||||
		else if (++i == v.len)
 | 
			
		||||
			result = self;
 | 
			
		||||
@@ -4714,15 +4762,13 @@ config_item_write_string (struct str *output, const struct str *s)
 | 
			
		||||
	for (size_t i = 0; i < s->len; i++)
 | 
			
		||||
	{
 | 
			
		||||
		unsigned char c = s->str[i];
 | 
			
		||||
		if      (c == '\n')  str_append (output, "\\n");
 | 
			
		||||
		else if (c == '\r')  str_append (output, "\\r");
 | 
			
		||||
		else if (c == '\t')  str_append (output, "\\t");
 | 
			
		||||
		else if (c == '\\')  str_append (output, "\\\\");
 | 
			
		||||
		else if (c == '"')   str_append (output, "\\\"");
 | 
			
		||||
		else if (iscntrl_ascii (c))
 | 
			
		||||
			str_append_printf (output, "\\x%02x", c);
 | 
			
		||||
		else
 | 
			
		||||
			str_append_c (output, c);
 | 
			
		||||
		if      (c == '\n')  str_append        (output, "\\n");
 | 
			
		||||
		else if (c == '\r')  str_append        (output, "\\r");
 | 
			
		||||
		else if (c == '\t')  str_append        (output, "\\t");
 | 
			
		||||
		else if (c == '\\')  str_append        (output, "\\\\");
 | 
			
		||||
		else if (c == '"')   str_append        (output, "\\\"");
 | 
			
		||||
		else if (c < 32)     str_append_printf (output, "\\x%02x", c);
 | 
			
		||||
		else                 str_append_c      (output, c);
 | 
			
		||||
	}
 | 
			
		||||
	str_append_c (output, '"');
 | 
			
		||||
}
 | 
			
		||||
@@ -4815,7 +4861,7 @@ config_item_write_object_innards
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (&object->value.object);
 | 
			
		||||
	struct config_item *value;
 | 
			
		||||
	while ((value = str_map_iter_next (&iter)))
 | 
			
		||||
	while ((value = (struct config_item *) str_map_iter_next (&iter)))
 | 
			
		||||
		config_item_write_kv_pair (self, iter.link->key, value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -5046,10 +5092,10 @@ config_tokenizer_escape_sequence
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
config_tokenizer_dq_string (struct config_tokenizer *self, struct str *output,
 | 
			
		||||
	struct error **e)
 | 
			
		||||
config_tokenizer_string
 | 
			
		||||
	(struct config_tokenizer *self, struct str *output, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char c = config_tokenizer_advance (self);
 | 
			
		||||
	unsigned char c;
 | 
			
		||||
	while (self->len)
 | 
			
		||||
	{
 | 
			
		||||
		if ((c = config_tokenizer_advance (self)) == '"')
 | 
			
		||||
@@ -5063,44 +5109,6 @@ config_tokenizer_dq_string (struct config_tokenizer *self, struct str *output,
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
config_tokenizer_bt_string (struct config_tokenizer *self, struct str *output,
 | 
			
		||||
	struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char c = config_tokenizer_advance (self);
 | 
			
		||||
	while (self->len)
 | 
			
		||||
	{
 | 
			
		||||
		if ((c = config_tokenizer_advance (self)) == '`')
 | 
			
		||||
			return true;
 | 
			
		||||
		str_append_c (output, c);
 | 
			
		||||
	}
 | 
			
		||||
	config_tokenizer_error (self, e, "premature end of string");
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
config_tokenizer_string (struct config_tokenizer *self, struct str *output,
 | 
			
		||||
	struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	// Go-like strings, with C/AWK-like automatic concatenation
 | 
			
		||||
	while (self->len)
 | 
			
		||||
	{
 | 
			
		||||
		bool ok = true;
 | 
			
		||||
		if (isspace_ascii (*self->p) && *self->p != '\n')
 | 
			
		||||
			config_tokenizer_advance (self);
 | 
			
		||||
		else if (*self->p == '"')
 | 
			
		||||
			ok = config_tokenizer_dq_string (self, output, e);
 | 
			
		||||
		else if (*self->p == '`')
 | 
			
		||||
			ok = config_tokenizer_bt_string (self, output, e);
 | 
			
		||||
		else
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		if (!ok)
 | 
			
		||||
			return false;
 | 
			
		||||
	}
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static enum config_token
 | 
			
		||||
config_tokenizer_next (struct config_tokenizer *self, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
@@ -5125,7 +5133,7 @@ config_tokenizer_next (struct config_tokenizer *self, struct error **e)
 | 
			
		||||
		return CONFIG_T_ABORT;
 | 
			
		||||
 | 
			
		||||
	case '"':
 | 
			
		||||
	case '`':
 | 
			
		||||
		config_tokenizer_advance (self);
 | 
			
		||||
		str_reset (&self->string);
 | 
			
		||||
		if (!config_tokenizer_string (self, &self->string, e))
 | 
			
		||||
			return CONFIG_T_ABORT;
 | 
			
		||||
@@ -5419,11 +5427,11 @@ config_read_from_file (const char *filename, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	struct config_item *root = NULL;
 | 
			
		||||
 | 
			
		||||
	struct error *error = NULL;
 | 
			
		||||
	struct str data = str_make ();
 | 
			
		||||
	if (!read_file (filename, &data, e))
 | 
			
		||||
		goto end;
 | 
			
		||||
 | 
			
		||||
	struct error *error = NULL;
 | 
			
		||||
	if (!(root = config_item_parse (data.str, data.len, false, &error)))
 | 
			
		||||
	{
 | 
			
		||||
		error_set (e, "parse error in `%s': %s", filename, error->message);
 | 
			
		||||
@@ -5443,12 +5451,12 @@ config_schema_initialize_item (struct config_schema *schema,
 | 
			
		||||
	struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	hard_assert (parent->type == CONFIG_ITEM_OBJECT);
 | 
			
		||||
	struct config_item *item =
 | 
			
		||||
	struct config_item *item = (struct config_item *)
 | 
			
		||||
		str_map_find (&parent->value.object, schema->name);
 | 
			
		||||
 | 
			
		||||
	struct error *error = NULL;
 | 
			
		||||
	if (item)
 | 
			
		||||
	{
 | 
			
		||||
		struct error *error = NULL;
 | 
			
		||||
		item->user_data = user_data;
 | 
			
		||||
		if (config_item_validate_by_schema (item, schema, &error))
 | 
			
		||||
			goto keep_current;
 | 
			
		||||
@@ -5456,9 +5464,9 @@ config_schema_initialize_item (struct config_schema *schema,
 | 
			
		||||
		error_set (warning, "resetting configuration item "
 | 
			
		||||
			"`%s' to default: %s", schema->name, error->message);
 | 
			
		||||
		error_free (error);
 | 
			
		||||
		error = NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct error *error = NULL;
 | 
			
		||||
	if (schema->default_)
 | 
			
		||||
		item = config_item_parse
 | 
			
		||||
			(schema->default_, strlen (schema->default_), true, &error);
 | 
			
		||||
@@ -5521,7 +5529,7 @@ config_schema_call_changed (struct config_item *item)
 | 
			
		||||
	{
 | 
			
		||||
		struct str_map_iter iter = str_map_iter_make (&item->value.object);
 | 
			
		||||
		struct config_item *child;
 | 
			
		||||
		while ((child = str_map_iter_next (&iter)))
 | 
			
		||||
		while ((child = (struct config_item *) str_map_iter_next (&iter)))
 | 
			
		||||
			config_schema_call_changed (child);
 | 
			
		||||
	}
 | 
			
		||||
	else if (item->schema && item->schema->on_change)
 | 
			
		||||
@@ -5574,7 +5582,8 @@ static void
 | 
			
		||||
config_register_module (struct config *self,
 | 
			
		||||
	const char *name, config_module_load_fn loader, void *user_data)
 | 
			
		||||
{
 | 
			
		||||
	struct config_module *module = xcalloc (1, sizeof *module);
 | 
			
		||||
	struct config_module *module =
 | 
			
		||||
		(struct config_module *) xcalloc (1, sizeof *module);
 | 
			
		||||
	module->name = xstrdup (name);
 | 
			
		||||
	module->loader = loader;
 | 
			
		||||
	module->user_data = user_data;
 | 
			
		||||
@@ -5592,9 +5601,9 @@ config_load (struct config *self, struct config_item *root)
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (&self->modules);
 | 
			
		||||
	struct config_module *module;
 | 
			
		||||
	while ((module = str_map_iter_next (&iter)))
 | 
			
		||||
	while ((module = (struct config_module *) str_map_iter_next (&iter)))
 | 
			
		||||
	{
 | 
			
		||||
		struct config_item *subtree = str_map_find
 | 
			
		||||
		struct config_item *subtree = (struct config_item *) str_map_find
 | 
			
		||||
			(&root->value.object, module->name);
 | 
			
		||||
		// Silently fix inputs that only a lunatic user could create
 | 
			
		||||
		if (!subtree || subtree->type != CONFIG_ITEM_OBJECT)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										108
									
								
								libertyxdr.adoc
									
									
									
									
									
								
							
							
						
						
									
										108
									
								
								libertyxdr.adoc
									
									
									
									
									
								
							@@ -1,108 +0,0 @@
 | 
			
		||||
libertyxdr(7)
 | 
			
		||||
=============
 | 
			
		||||
:doctype: manpage
 | 
			
		||||
 | 
			
		||||
Name
 | 
			
		||||
----
 | 
			
		||||
LibertyXDR - an XDR-derived IDL and data serialization format
 | 
			
		||||
 | 
			
		||||
Description
 | 
			
		||||
-----------
 | 
			
		||||
*LibertyXDR* is an interface description language, as well as a data
 | 
			
		||||
serialization format, that has been largely derived from XDR, though notably
 | 
			
		||||
simplified.
 | 
			
		||||
 | 
			
		||||
Conventions
 | 
			
		||||
~~~~~~~~~~~
 | 
			
		||||
User-defined types should be named in *CamelCase*, field names in *snake_case*,
 | 
			
		||||
and constants in *SCREAMING_SNAKE_CASE*.  Code generators will convert these to
 | 
			
		||||
whatever is appropriate in their target language.
 | 
			
		||||
 | 
			
		||||
Primitive data types
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
Like in XDR, all data is serialized in the network byte order, i.e., big-endian.
 | 
			
		||||
 | 
			
		||||
 * *void*: 0 bytes
 | 
			
		||||
+
 | 
			
		||||
This is a dummy type that cannot be assigned a field name.
 | 
			
		||||
 | 
			
		||||
 * *bool*: 1 byte 
 | 
			
		||||
+
 | 
			
		||||
This is a boolean value: 0 means _false_, any other value means _true_.
 | 
			
		||||
 | 
			
		||||
 * *u8*, *u16*, *u32*, *u64*: 1, 2, 4, and 8 bytes respectively
 | 
			
		||||
+
 | 
			
		||||
These are unsigned integers.
 | 
			
		||||
 | 
			
		||||
 * *i8*, *i16*, *i32*, *i64*: 1, 2, 4, and 8 bytes respectively
 | 
			
		||||
+
 | 
			
		||||
These are signed integers in two's complement.
 | 
			
		||||
 | 
			
		||||
 * *string*: implicitly prefixed by its length as a *u32*,
 | 
			
		||||
   then immediately followed by its contents, with no trailing NUL byte
 | 
			
		||||
+
 | 
			
		||||
This is a valid UTF-8 string without a byte order mark.  Note that strings are
 | 
			
		||||
always unbounded, unlike in XDR.
 | 
			
		||||
 | 
			
		||||
Constants
 | 
			
		||||
~~~~~~~~~
 | 
			
		||||
At the top level of a document, outside other definitions, you can define
 | 
			
		||||
typeless integer constants:
 | 
			
		||||
 | 
			
		||||
 const VERSION = 1;
 | 
			
		||||
 | 
			
		||||
The value can be either a name of another previously defined constant,
 | 
			
		||||
or an immediate decimal value, which may not contain leading zeros.
 | 
			
		||||
 | 
			
		||||
Enumerations
 | 
			
		||||
~~~~~~~~~~~~
 | 
			
		||||
An *enum* is an *i8* with uniquely named values, in their own namespace.
 | 
			
		||||
 | 
			
		||||
Values can be either specified explicitly, in the same way as with a constant,
 | 
			
		||||
or they can be left implicit, in which case names assume a value that is one
 | 
			
		||||
larger than their predecessor.  Zero is reserved for internal use, thus
 | 
			
		||||
enumerations implicitly begin with a value of one.  For example, these form
 | 
			
		||||
a sequence from one to three:
 | 
			
		||||
 | 
			
		||||
 enum Vehicle { CAR, LORRY = 2, PLANE, };
 | 
			
		||||
 | 
			
		||||
Structures
 | 
			
		||||
~~~~~~~~~~
 | 
			
		||||
A *struct* is a sequence of fields, specified by their type, and their chosen
 | 
			
		||||
name.  You can add a *<>* suffix to change a field to an array, in which case
 | 
			
		||||
it is implicitly preceded by a *u32* specifying its length in terms of its
 | 
			
		||||
elements.
 | 
			
		||||
 | 
			
		||||
Unlike in XDR, there is no padding between subsequent fields, and type
 | 
			
		||||
definitions can be arbitrarily syntactically nested, as in C.
 | 
			
		||||
 | 
			
		||||
 struct StockReport {
 | 
			
		||||
   u8 version;       // Version of this report.
 | 
			
		||||
   struct Item {
 | 
			
		||||
     Vehicle kind;   // The vehicle in question.
 | 
			
		||||
     i32 count;      // How many vehicle of that kind there are.
 | 
			
		||||
   } items<>;        // Reported items.
 | 
			
		||||
 };
 | 
			
		||||
 | 
			
		||||
Unions
 | 
			
		||||
~~~~~~
 | 
			
		||||
A *union* is a kind of structure whose fields depend on the value of its first
 | 
			
		||||
and always-present field, which must be a tag *enum*:
 | 
			
		||||
 | 
			
		||||
 union VehicleDetails switch (Vehicle kind) {
 | 
			
		||||
 case CAR:   void;
 | 
			
		||||
 case LORRY: i8 axles;
 | 
			
		||||
 case PLANE: i8 engines;
 | 
			
		||||
 };
 | 
			
		||||
 | 
			
		||||
All possible enumeration values must be named, and there is no *case*
 | 
			
		||||
fall-through.
 | 
			
		||||
 | 
			
		||||
Framing
 | 
			
		||||
-------
 | 
			
		||||
Unless this role is already filled by, e.g., WebSocket, _LibertyXDR_ structures
 | 
			
		||||
should be prefixed by their byte length in the *u32* format, once serialized.
 | 
			
		||||
 | 
			
		||||
See also
 | 
			
		||||
--------
 | 
			
		||||
_XDR: External Data Representation Standard_, RFC 4506
 | 
			
		||||
@@ -1,21 +0,0 @@
 | 
			
		||||
" filetype.vim: au! BufNewFile,BufRead *.lxdr setf libertyxdr
 | 
			
		||||
if exists("b:current_syntax")
 | 
			
		||||
	finish
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
syn match libertyxdrError "[^[:space:]:;,(){}<>=]\+"
 | 
			
		||||
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 libertyxdrType bool u8 u16 u32 u64 i8 i16 i32 i64 string void
 | 
			
		||||
 | 
			
		||||
let b:current_syntax = "libertyxdr"
 | 
			
		||||
hi def link libertyxdrError Error
 | 
			
		||||
hi def link libertyxdrBlockComment Comment
 | 
			
		||||
hi def link libertyxdrComment Comment
 | 
			
		||||
hi def link libertyxdrIdentifier Identifier
 | 
			
		||||
hi def link libertyxdrNumber Number
 | 
			
		||||
hi def link libertyxdrKeyword Statement
 | 
			
		||||
hi def link libertyxdrType Type
 | 
			
		||||
@@ -82,3 +82,4 @@ siphash (const unsigned char key[16], const unsigned char *m, size_t len)
 | 
			
		||||
 | 
			
		||||
	return v0 ^ v1 ^ v2 ^ v3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
/*
 | 
			
		||||
 * tests/liberty.c
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2015 - 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
 * Copyright (c) 2015 - 2016, 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.
 | 
			
		||||
@@ -331,12 +331,10 @@ test_utf8 (void)
 | 
			
		||||
	soft_assert (utf8_decode (&partial, 1) == -2);
 | 
			
		||||
	soft_assert (utf8_decode (&empty,   0) == -1);
 | 
			
		||||
 | 
			
		||||
	const char valid_1[] = "2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm";
 | 
			
		||||
	const char valid_2[] = "\xf0\x93\x82\xb9";
 | 
			
		||||
	const char valid[] = "2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm";
 | 
			
		||||
	const char invalid_1[] = "\xf0\x90\x28\xbc";
 | 
			
		||||
	const char invalid_2[] = "\xc0\x80";
 | 
			
		||||
	soft_assert ( utf8_validate (valid_1,   sizeof valid_1));
 | 
			
		||||
	soft_assert ( utf8_validate (valid_2,   sizeof valid_2));
 | 
			
		||||
	soft_assert ( utf8_validate (valid,     sizeof valid));
 | 
			
		||||
	soft_assert (!utf8_validate (invalid_1, sizeof invalid_1));
 | 
			
		||||
	soft_assert (!utf8_validate (invalid_2, sizeof invalid_2));
 | 
			
		||||
 | 
			
		||||
@@ -649,7 +647,7 @@ static struct config_schema g_config_test[] =
 | 
			
		||||
	  .default_  = "1" },
 | 
			
		||||
	{ .name      = "foobar",
 | 
			
		||||
	  .type      = CONFIG_ITEM_STRING,
 | 
			
		||||
	  .default_  = "\"qux\\x01`\" \"\"`a`" },
 | 
			
		||||
	  .default_  = "\"qux\\x01\"" },
 | 
			
		||||
	{}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -675,9 +673,6 @@ test_config (void)
 | 
			
		||||
		"top.bar", NULL), invalid, NULL));
 | 
			
		||||
	config_item_destroy (invalid);
 | 
			
		||||
 | 
			
		||||
	hard_assert (!strcmp ("qux\001`a",
 | 
			
		||||
		config_item_get (config.root, "top.foobar", NULL)->value.string.str));
 | 
			
		||||
 | 
			
		||||
	struct str s = str_make ();
 | 
			
		||||
	config_item_write (config.root, true, &s);
 | 
			
		||||
	struct config_item *parsed = config_item_parse (s.str, s.len, false, NULL);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										123
									
								
								tests/lxdrgen.c
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								tests/lxdrgen.c
									
									
									
									
									
								
							@@ -1,123 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * tests/lxdrgen.c
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2022, 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.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 | 
			
		||||
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 | 
			
		||||
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 | 
			
		||||
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 | 
			
		||||
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 | 
			
		||||
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 | 
			
		||||
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#define PROGRAM_NAME "test"
 | 
			
		||||
#define PROGRAM_VERSION "0"
 | 
			
		||||
 | 
			
		||||
#include "../liberty.c"
 | 
			
		||||
#include "lxdrgen.lxdr.c"
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
test_ser_deser_free (void)
 | 
			
		||||
{
 | 
			
		||||
	hard_assert (PROTO_GEN_VERSION == 1);
 | 
			
		||||
 | 
			
		||||
	enum { CASES = 3 };
 | 
			
		||||
 | 
			
		||||
	struct proto_gen_struct a = {}, b = {};
 | 
			
		||||
	a.u = xcalloc ((a.u_len = CASES + rand () % 100), sizeof *a.u);
 | 
			
		||||
	for (size_t i = 0; i < a.u_len; i++)
 | 
			
		||||
	{
 | 
			
		||||
		union proto_gen_union *u = a.u + i;
 | 
			
		||||
		switch (i % CASES)
 | 
			
		||||
		{
 | 
			
		||||
		case 0:
 | 
			
		||||
			u->tag = PROTO_GEN_ENUM_NUMBERS;
 | 
			
		||||
			u->numbers.a = rand () % UINT8_MAX;
 | 
			
		||||
			u->numbers.b = rand () % UINT16_MAX;
 | 
			
		||||
			u->numbers.c = rand () % UINT32_MAX;
 | 
			
		||||
			u->numbers.d = rand () % UINT64_MAX;
 | 
			
		||||
			u->numbers.e = rand () % UINT8_MAX;
 | 
			
		||||
			u->numbers.f = rand () % UINT16_MAX;
 | 
			
		||||
			u->numbers.g = rand () % UINT32_MAX;
 | 
			
		||||
			u->numbers.h = rand () % UINT64_MAX;
 | 
			
		||||
			break;
 | 
			
		||||
		case 1:
 | 
			
		||||
			u->tag = PROTO_GEN_ENUM_OTHERS;
 | 
			
		||||
			u->others.foo = rand () % 2;
 | 
			
		||||
			u->others.bar = str_make ();
 | 
			
		||||
			for (int i = rand () % 0x30; i > 0; i--)
 | 
			
		||||
				str_append_c (&u->others.bar, 0x30 + i);
 | 
			
		||||
			break;
 | 
			
		||||
		case 2:
 | 
			
		||||
			u->tag = PROTO_GEN_ENUM_NOTHING;
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			hard_assert (!"unhandled case");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct str buf = str_make ();
 | 
			
		||||
	hard_assert (proto_gen_struct_serialize (&a, &buf));
 | 
			
		||||
	struct msg_unpacker r = msg_unpacker_make (buf.str, buf.len);
 | 
			
		||||
	hard_assert (proto_gen_struct_deserialize (&b, &r));
 | 
			
		||||
	hard_assert (!msg_unpacker_get_available (&r));
 | 
			
		||||
	str_free (&buf);
 | 
			
		||||
 | 
			
		||||
	hard_assert (a.u_len == b.u_len);
 | 
			
		||||
	for (size_t i = 0; i < a.u_len; i++)
 | 
			
		||||
	{
 | 
			
		||||
		union proto_gen_union *ua = a.u + i;
 | 
			
		||||
		union proto_gen_union *ub = b.u + i;
 | 
			
		||||
		hard_assert (ua->tag == ub->tag);
 | 
			
		||||
		switch (ua->tag)
 | 
			
		||||
		{
 | 
			
		||||
		case PROTO_GEN_ENUM_NUMBERS:
 | 
			
		||||
			hard_assert (ua->numbers.a == ub->numbers.a);
 | 
			
		||||
			hard_assert (ua->numbers.b == ub->numbers.b);
 | 
			
		||||
			hard_assert (ua->numbers.c == ub->numbers.c);
 | 
			
		||||
			hard_assert (ua->numbers.d == ub->numbers.d);
 | 
			
		||||
			hard_assert (ua->numbers.e == ub->numbers.e);
 | 
			
		||||
			hard_assert (ua->numbers.f == ub->numbers.f);
 | 
			
		||||
			hard_assert (ua->numbers.g == ub->numbers.g);
 | 
			
		||||
			hard_assert (ua->numbers.h == ub->numbers.h);
 | 
			
		||||
			break;
 | 
			
		||||
		case PROTO_GEN_ENUM_OTHERS:
 | 
			
		||||
			hard_assert (ua->others.foo == ub->others.foo);
 | 
			
		||||
			hard_assert (ua->others.bar.len == ub->others.bar.len);
 | 
			
		||||
			hard_assert (!memcmp (ua->others.bar.str, ub->others.bar.str,
 | 
			
		||||
				ua->others.bar.len));
 | 
			
		||||
			break;
 | 
			
		||||
		case PROTO_GEN_ENUM_NOTHING:
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			hard_assert (!"unexpected case");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Emulate partially deserialized data to test disposal of that.
 | 
			
		||||
	for (size_t i = b.u_len - CASES; i < b.u_len; i++)
 | 
			
		||||
	{
 | 
			
		||||
		proto_gen_union_free (&b.u[i]);
 | 
			
		||||
		memset (&b.u[i], 0, sizeof b.u[i]);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	proto_gen_struct_free (&a);
 | 
			
		||||
	proto_gen_struct_free (&b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main (int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	struct test test;
 | 
			
		||||
	test_init (&test, argc, argv);
 | 
			
		||||
 | 
			
		||||
	test_add_simple (&test, "/ser-deser-free", NULL, test_ser_deser_free);
 | 
			
		||||
 | 
			
		||||
	return test_run (&test);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,23 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * tests/lxdrgen.lxdr: a test protocol for the generator
 | 
			
		||||
 */
 | 
			
		||||
const VERSION = 1;
 | 
			
		||||
const NOISREV = -1;
 | 
			
		||||
 | 
			
		||||
// TODO: Test failure paths, and in general go for full coverage.
 | 
			
		||||
struct Struct {
 | 
			
		||||
	union Union switch (enum Enum {
 | 
			
		||||
		NUMBERS = VERSION,
 | 
			
		||||
		OTHERS = 2,
 | 
			
		||||
		NOTHING,
 | 
			
		||||
	} tag) {
 | 
			
		||||
	case NUMBERS:
 | 
			
		||||
		i8 a; i16 b; i32 c; i64 d;
 | 
			
		||||
		u8 e; u16 f; u32 g; u64 h;
 | 
			
		||||
	case OTHERS:
 | 
			
		||||
		bool foo;
 | 
			
		||||
		string bar;
 | 
			
		||||
	case NOTHING:
 | 
			
		||||
		void;
 | 
			
		||||
	} u<>;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										127
									
								
								tests/pulse.c
									
									
									
									
									
								
							
							
						
						
									
										127
									
								
								tests/pulse.c
									
									
									
									
									
								
							@@ -1,127 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * tests/pulse.c
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2021, 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.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 | 
			
		||||
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 | 
			
		||||
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 | 
			
		||||
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 | 
			
		||||
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 | 
			
		||||
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 | 
			
		||||
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#define PROGRAM_NAME "test"
 | 
			
		||||
#define PROGRAM_VERSION "0"
 | 
			
		||||
 | 
			
		||||
#define LIBERTY_WANT_POLLER
 | 
			
		||||
 | 
			
		||||
#include "../liberty.c"
 | 
			
		||||
#include "../liberty-pulse.c"
 | 
			
		||||
 | 
			
		||||
// --- Tests -------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
enum
 | 
			
		||||
{
 | 
			
		||||
	EVENT_IO    =  1 << 0,
 | 
			
		||||
	EVENT_TIME  =  1 << 1,
 | 
			
		||||
	EVENT_DEFER =  1 << 2,
 | 
			
		||||
	EVENT_ALL   = (1 << 3) - 1
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static intptr_t g_events = 0;
 | 
			
		||||
static intptr_t g_destroys = 0;
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
io_event_cb (pa_mainloop_api *a,
 | 
			
		||||
	pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	(void) a; (void) e; (void) fd; (void) events;
 | 
			
		||||
	g_events |= (intptr_t) userdata;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
io_event_destroy_cb (pa_mainloop_api *a, pa_io_event *e, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	(void) a; (void) e;
 | 
			
		||||
	g_destroys += (intptr_t) userdata;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
time_event_cb (pa_mainloop_api *a,
 | 
			
		||||
	pa_time_event *e, const struct timeval *tv, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	(void) a; (void) e; (void) tv;
 | 
			
		||||
	g_events |= (intptr_t) userdata;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
time_event_destroy_cb (pa_mainloop_api *a, pa_time_event *e, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	(void) a; (void) e;
 | 
			
		||||
	g_destroys += (intptr_t) userdata;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
defer_event_cb (pa_mainloop_api *a, pa_defer_event *e, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	(void) a; (void) e;
 | 
			
		||||
	g_events |= (intptr_t) userdata;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
defer_event_destroy_cb (pa_mainloop_api *a, pa_defer_event *e, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
	(void) a; (void) e;
 | 
			
		||||
	g_destroys += (intptr_t) userdata;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
test_pulse (void)
 | 
			
		||||
{
 | 
			
		||||
	struct poller poller;
 | 
			
		||||
	poller_init (&poller);
 | 
			
		||||
 | 
			
		||||
	// Let's just get this over with, not aiming for high test coverage here
 | 
			
		||||
	pa_mainloop_api *api = poller_pa_new (&poller);
 | 
			
		||||
 | 
			
		||||
	pa_io_event *ie = api->io_new (api, STDOUT_FILENO, PA_IO_EVENT_OUTPUT,
 | 
			
		||||
		io_event_cb, (void *) EVENT_IO);
 | 
			
		||||
	api->io_set_destroy (ie, io_event_destroy_cb);
 | 
			
		||||
 | 
			
		||||
	const struct timeval tv = poller_pa_get_current_time ();
 | 
			
		||||
	pa_time_event *te = api->time_new (api, &tv,
 | 
			
		||||
		time_event_cb, (void *) EVENT_TIME);
 | 
			
		||||
	api->time_set_destroy (te, time_event_destroy_cb);
 | 
			
		||||
	api->time_restart (te, &tv);
 | 
			
		||||
 | 
			
		||||
	pa_defer_event *de = api->defer_new (api,
 | 
			
		||||
		defer_event_cb, (void *) EVENT_DEFER);
 | 
			
		||||
	api->defer_set_destroy (de, defer_event_destroy_cb);
 | 
			
		||||
	api->defer_enable (api->defer_new (api,
 | 
			
		||||
		defer_event_cb, (void *) EVENT_DEFER), false);
 | 
			
		||||
 | 
			
		||||
	alarm (1);
 | 
			
		||||
	while (g_events != EVENT_ALL)
 | 
			
		||||
		poller_run (&poller);
 | 
			
		||||
 | 
			
		||||
	poller_pa_destroy (api);
 | 
			
		||||
	soft_assert (g_destroys == EVENT_ALL);
 | 
			
		||||
	poller_free (&poller);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// --- Main --------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main (int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	struct test test;
 | 
			
		||||
	test_init (&test, argc, argv);
 | 
			
		||||
	test_add_simple (&test, "/pulse", NULL, test_pulse);
 | 
			
		||||
	return test_run (&test);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,245 +0,0 @@
 | 
			
		||||
# asciiman.awk: stupid AsciiDoc to manual page converter
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
# SPDX-License-Identifier: 0BSD
 | 
			
		||||
#
 | 
			
		||||
# This is not intended to produce great output, merely useful output.
 | 
			
		||||
# As such, input documents should restrict themselves as follows:
 | 
			
		||||
#
 | 
			
		||||
#  - In-line formatting sequences must not overlap,
 | 
			
		||||
#    cannot be escaped, and cannot span lines.
 | 
			
		||||
#  - Heading underlines must match in byte length exactly.
 | 
			
		||||
#  - Only a small subset of syntax is supported overall.
 | 
			
		||||
#
 | 
			
		||||
# Also beware that the output has only been tested with GNU troff.
 | 
			
		||||
# Attributes can be passed via environment variables starting with "asciidoc-".
 | 
			
		||||
 | 
			
		||||
function fatal(message) {
 | 
			
		||||
	print ".\\\" " FILENAME ":" FNR ": fatal error: " message
 | 
			
		||||
	print FILENAME ":" FNR ": fatal error: " message > "/dev/stderr"
 | 
			
		||||
	exit 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getattribute(name) {
 | 
			
		||||
	if (!(name in Attrs) && ("asciidoc-" name) in ENVIRON)
 | 
			
		||||
		Attrs[name] = ENVIRON["asciidoc-" name]
 | 
			
		||||
	return Attrs[name]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function expand(s,   attr, v) {
 | 
			
		||||
	# TODO: This should not expand unknown attribute names.
 | 
			
		||||
	while (match(s, /[{][^{}]*[}]/)) {
 | 
			
		||||
		s = substr(s, 1, RSTART - 1) \
 | 
			
		||||
			getattribute(substr(s, RSTART + 1, RLENGTH - 2)) \
 | 
			
		||||
			substr(s, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function escape(s) {
 | 
			
		||||
	gsub(/\\/, "\\\\", s)
 | 
			
		||||
	gsub(/-/, "\\-", s)
 | 
			
		||||
	sub(/^[.']/, "\\\\\\&&", s)
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function readattribute(line,    attrname, attrvalue) {
 | 
			
		||||
	if (match(line, /^:[^:]*: /)) {
 | 
			
		||||
		attrname = substr(line, RSTART + 1, RLENGTH - 3)
 | 
			
		||||
		attrvalue = substr(line, RSTART + RLENGTH)
 | 
			
		||||
		Attrs[attrname] = expand(attrvalue)
 | 
			
		||||
		return 1
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
NR == 1 {
 | 
			
		||||
	nameline = $0
 | 
			
		||||
	if (match(nameline, /[(][[:digit:]][)]$/)) {
 | 
			
		||||
		name = substr(nameline, 1, RSTART - 1)
 | 
			
		||||
		section = substr(nameline, RSTART + 1, RLENGTH - 2)
 | 
			
		||||
	} else {
 | 
			
		||||
		fatal("invalid header line")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	getline
 | 
			
		||||
	if (length(nameline) != length($0) || /[^=]/)
 | 
			
		||||
		fatal("invalid header underline")
 | 
			
		||||
 | 
			
		||||
	getline
 | 
			
		||||
	while (readattribute($0))
 | 
			
		||||
		getline
 | 
			
		||||
	if ($0)
 | 
			
		||||
		fatal("expected an empty line after the header")
 | 
			
		||||
 | 
			
		||||
	# Requesting tbl(1), even though we currently do not support tables.
 | 
			
		||||
	print "'\\\" t"
 | 
			
		||||
	printf ".TH \"%s\" \"%s\" \"\" \"%s\"",
 | 
			
		||||
		toupper(name), section, getattribute("mansource")
 | 
			
		||||
	if (getattribute("manmanual"))
 | 
			
		||||
		printf " \"%s\"", getattribute("manmanual")
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	# Hyphenation is indeed rather annoying, in particular with long links.
 | 
			
		||||
	print ".nh"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function format(line,    v) {
 | 
			
		||||
	# Pass-through, otherwise useful for hacks, is a bit of a lie here,
 | 
			
		||||
	# and formatting doesn't fully respect word boundaries.
 | 
			
		||||
	while (line) {
 | 
			
		||||
		if (match(line, /^[+][+][+][^+]+[+][+][+]/)) {
 | 
			
		||||
			v = v substr(line, RSTART + 3, RLENGTH - 6)
 | 
			
		||||
		} else if (match(line, /^__[^_]+__/)) {
 | 
			
		||||
			v = v "\\fI" substr(line, RSTART + 2, RLENGTH - 4) "\\fP"
 | 
			
		||||
		} else if (match(line, /^[*][*][^*]+[*][*]/)) {
 | 
			
		||||
			v = v "\\fB" substr(line, RSTART + 2, RLENGTH - 4) "\\fP"
 | 
			
		||||
		} else if (match(line, /^_[^_]+_/) &&
 | 
			
		||||
			substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
 | 
			
		||||
			v = v "\\fI" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
 | 
			
		||||
		} else if (match(line, /^[*][^*]+[*]/) &&
 | 
			
		||||
			substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
 | 
			
		||||
			v = v "\\fB" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
 | 
			
		||||
		} else {
 | 
			
		||||
			v = v substr(line, 1, 1)
 | 
			
		||||
			line = substr(line, 2)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		line = substr(line, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function inline(line) {
 | 
			
		||||
	if (!line) {
 | 
			
		||||
		print ".sp"
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	line = format(escape(expand(line)))
 | 
			
		||||
 | 
			
		||||
	# Strip empty URL descriptions, otherwise useful for demarking the end.
 | 
			
		||||
	while (match(line, /[^[:space:]]+\[\]/)) {
 | 
			
		||||
		line = substr(line, 1, RSTART + RLENGTH - 3) \
 | 
			
		||||
			 substr(line, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	# Enable double-spacing after the end of a sentence.
 | 
			
		||||
	gsub(/[.][[:space:]]+/, ".\n", line)
 | 
			
		||||
	gsub(/[!][[:space:]]+/, "!\n", line)
 | 
			
		||||
	gsub(/[?][[:space:]]+/, "?\n", line)
 | 
			
		||||
 | 
			
		||||
	# Quote commands resulting from that, as well as from expand().
 | 
			
		||||
	gsub(/\n[.]/, "\n\\\\\\&.", line)
 | 
			
		||||
	gsub(/\n[']/, "\n\\\\\\&'", line)
 | 
			
		||||
 | 
			
		||||
	sub(/[[:space:]]+[+]$/, "\n.br", line)
 | 
			
		||||
	print line
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Returns 1 iff the left-over $0 should be processed further.
 | 
			
		||||
function process(firstline) {
 | 
			
		||||
	if (readattribute(firstline))
 | 
			
		||||
		return 0
 | 
			
		||||
	if (getline <= 0) {
 | 
			
		||||
		inline(firstline)
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (length(firstline) == length($0) && /^-+$/) {
 | 
			
		||||
		print ".SH \"" escape(toupper(expand(firstline))) "\""
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	if (length(firstline) == length($0) && /^~+$/) {
 | 
			
		||||
		print ".SS \"" escape(expand(firstline)) "\""
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	if (firstline ~ /^(-{4,}|[.]{4,})$/) {
 | 
			
		||||
		print ".if n .RS 4"
 | 
			
		||||
		print ".nf"
 | 
			
		||||
		print ".fam C"
 | 
			
		||||
		do {
 | 
			
		||||
			print escape($0)
 | 
			
		||||
		} while (getline > 0 && $0 != firstline)
 | 
			
		||||
		print ".fam"
 | 
			
		||||
		print ".fi"
 | 
			
		||||
		print ".if n .RE"
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	if (firstline ~ /^\/{4,}$/) {
 | 
			
		||||
		do {
 | 
			
		||||
			print ".\\\" " $0
 | 
			
		||||
		} while (getline > 0 && $0 != firstline)
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	if (match(firstline, /^\/\//)) {
 | 
			
		||||
		print ".\\\" " firstline
 | 
			
		||||
		return 1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	# We generally assume these block end with a blank line.
 | 
			
		||||
	if (match(firstline, /^[[:space:]]*[*][[:space:]]+/)) {
 | 
			
		||||
		# Bullet magic copied over from AsciiDoc/Asciidoctor generators.
 | 
			
		||||
		print ".RS 4"
 | 
			
		||||
		print ".ie n \\{\\"
 | 
			
		||||
		print "\\h'-04'\\(bu\\h'+03'\\c"
 | 
			
		||||
		print ".\\}"
 | 
			
		||||
		print ".el \\{\\"
 | 
			
		||||
		print ".sp -1"
 | 
			
		||||
		print ".IP \\(bu 2.3"
 | 
			
		||||
		print ".\\}"
 | 
			
		||||
 | 
			
		||||
		inline(substr(firstline, RSTART + RLENGTH))
 | 
			
		||||
		while ($0) {
 | 
			
		||||
			sub(/^[[:space:]]+/, "")
 | 
			
		||||
			sub(/^[+]$/, "")
 | 
			
		||||
			if (!process($0) && getline <= 0)
 | 
			
		||||
				fatal("unexpected EOF")
 | 
			
		||||
			if (match($0, /^[[:space:]]*[*][[:space:]]+/))
 | 
			
		||||
				break
 | 
			
		||||
		}
 | 
			
		||||
		print ".RE"
 | 
			
		||||
		print ".sp"
 | 
			
		||||
		return !!$0
 | 
			
		||||
	}
 | 
			
		||||
	if (match(firstline, /^[[:space:]]+/)) {
 | 
			
		||||
		print ".if n .RS 4"
 | 
			
		||||
		print ".nf"
 | 
			
		||||
		print ".fam C"
 | 
			
		||||
		do {
 | 
			
		||||
			print escape(substr(firstline, RLENGTH + 1))
 | 
			
		||||
			firstline = $0
 | 
			
		||||
		} while ($0 && getline > 0)
 | 
			
		||||
		print ".fam"
 | 
			
		||||
		print ".fi"
 | 
			
		||||
		print ".if n .RE"
 | 
			
		||||
		return 1
 | 
			
		||||
	}
 | 
			
		||||
	if (match(firstline, /::$/)) {
 | 
			
		||||
		inline(substr(firstline, 1, RSTART - 1))
 | 
			
		||||
		while (match($0, /::$/)) {
 | 
			
		||||
			print ".br"
 | 
			
		||||
			inline(substr($0, 1, RSTART - 1))
 | 
			
		||||
			if (getline <= 0)
 | 
			
		||||
				fatal("unexpected EOF")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		print ".RS 4"
 | 
			
		||||
		while ($0) {
 | 
			
		||||
			sub(/^[[:space:]]+/, "")
 | 
			
		||||
			sub(/^[+]$/, "")
 | 
			
		||||
			if (!process($0) && getline <= 0)
 | 
			
		||||
				fatal("unexpected EOF")
 | 
			
		||||
			if (match($0, /::$/))
 | 
			
		||||
				break
 | 
			
		||||
		}
 | 
			
		||||
		print ".RE"
 | 
			
		||||
		print ".sp"
 | 
			
		||||
		return !!$0
 | 
			
		||||
	}
 | 
			
		||||
	inline(firstline)
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
	while (process($0)) {}
 | 
			
		||||
}
 | 
			
		||||
@@ -1,24 +0,0 @@
 | 
			
		||||
# cmake-dump.awk: dump parsed CMake scripts as tables
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
# SPDX-License-Identifier: 0BSD
 | 
			
		||||
#
 | 
			
		||||
# Parsed scripts are output in a table, with commands separated using ASCII
 | 
			
		||||
# Record Separators, and arguments using Unit Separators.
 | 
			
		||||
#
 | 
			
		||||
# Example usage: awk -f cmake-parser.awk -f cmake-dump.awk CMakeLists.txt \
 | 
			
		||||
#  | sed 'y/\x1F\x1E\t\n/\t\n  /' \
 | 
			
		||||
#  | sed -n '/^project\t\([^\t]*\).*\tVERSION\t\([^\t]*\).*/{s//\1 \2/p;q;}'
 | 
			
		||||
 | 
			
		||||
function sanitize(s) {
 | 
			
		||||
	if (s ~ /[\x1E\x1F]/)
 | 
			
		||||
		fatal("conflicting ASCII control characters found in source")
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Command {
 | 
			
		||||
	out = sanitize(Command)
 | 
			
		||||
	for (i in Args)
 | 
			
		||||
		out = out "\x1F" sanitize(Args[i])
 | 
			
		||||
	printf "%s\x1E", out
 | 
			
		||||
}
 | 
			
		||||
@@ -1,252 +0,0 @@
 | 
			
		||||
# cmake-parser.awk: rudimentary CMake script parser
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
# SPDX-License-Identifier: 0BSD
 | 
			
		||||
#
 | 
			
		||||
# Implemented roughly according to the grammar described in cmake-language(7),
 | 
			
		||||
# which is self-conflicting, and not an accurate description.
 | 
			
		||||
#
 | 
			
		||||
# The result of parsing is stored in the case-normalized Command variable,
 | 
			
		||||
# and the Args array.  These can be used by subsequent scripts.
 | 
			
		||||
 | 
			
		||||
function warning(message) {
 | 
			
		||||
	print FILENAME ":" FNR ": warning: " message > "/dev/stderr"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function fatal(message) {
 | 
			
		||||
	print FILENAME ":" FNR ": fatal error: " message > "/dev/stderr"
 | 
			
		||||
	exit 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function expect(v) {
 | 
			
		||||
	if (!v && v == 0)
 | 
			
		||||
		fatal("broken expectations at `" $0 "'")
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function literal(v) {
 | 
			
		||||
	if (substr($0, 1, length(v)) != v)
 | 
			
		||||
		return 0
 | 
			
		||||
	$0 = substr($0, length(v) + 1)
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function regexp(re) {
 | 
			
		||||
	if (!match($0, "^" re))
 | 
			
		||||
		return 0
 | 
			
		||||
	$0 = substr($0, RLENGTH + 1)
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function space() {
 | 
			
		||||
	return regexp("[ \t]+")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function unbracket(len,    v) {
 | 
			
		||||
	do {
 | 
			
		||||
		if (match($0, "]={" len "}]")) {
 | 
			
		||||
			v = v substr($0, 1, RSTART - 1)
 | 
			
		||||
			$0 = substr($0, RSTART + RLENGTH)
 | 
			
		||||
			return v
 | 
			
		||||
		}
 | 
			
		||||
		v = v $0 RS
 | 
			
		||||
	} while (getline > 0)
 | 
			
		||||
	fatal("unterminated bracket")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function bracket_comment() {
 | 
			
		||||
	if (!match($0, /^#\[=*\[/))
 | 
			
		||||
		return 0
 | 
			
		||||
	$0 = substr($0, RSTART + RLENGTH)
 | 
			
		||||
	unbracket(RLENGTH - 3)
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function line_ending() {
 | 
			
		||||
	while (space() || bracket_comment()) {}
 | 
			
		||||
	if (/^#/)
 | 
			
		||||
		$0 = ""
 | 
			
		||||
	return !$0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
# While elementary expansion of previously set variables is implementable,
 | 
			
		||||
# it doesn't seem to be worth the effort.
 | 
			
		||||
function expand(s,    v) {
 | 
			
		||||
	v = s
 | 
			
		||||
	while (match(v, /\\*[$](|ENV|CACHE)[{]/)) {
 | 
			
		||||
		if (index(substr(v, RSTART), "$") % 2 != 0) {
 | 
			
		||||
			warning("variable expansion is not supported: " s)
 | 
			
		||||
			return s
 | 
			
		||||
		}
 | 
			
		||||
		v = substr(v, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function escape_sequence(    v) {
 | 
			
		||||
	if (!literal("\\"))
 | 
			
		||||
		return 0
 | 
			
		||||
 | 
			
		||||
	if (literal("t")) return "\t"
 | 
			
		||||
	if (literal("r")) return "\r"
 | 
			
		||||
	if (literal("n")) return "\n"
 | 
			
		||||
 | 
			
		||||
	# escape_semicolon isn't treated any specially here.
 | 
			
		||||
	if (regexp("[A-Za-z0-9]"))
 | 
			
		||||
		fatal("unsupported escape sequence")
 | 
			
		||||
 | 
			
		||||
	if ($0) {
 | 
			
		||||
		v = substr($0, 1, 1)
 | 
			
		||||
		$0 = substr($0, 2)
 | 
			
		||||
		return v
 | 
			
		||||
	}
 | 
			
		||||
	if (getline > 0)
 | 
			
		||||
		return ""
 | 
			
		||||
	fatal("premature end of file")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function quoted_argument(    v, unescaped) {
 | 
			
		||||
	if (!literal("\""))
 | 
			
		||||
		return 0
 | 
			
		||||
 | 
			
		||||
	v = ""
 | 
			
		||||
	while (!literal("\"")) {
 | 
			
		||||
		if (!$0) {
 | 
			
		||||
			if (getline <= 0)
 | 
			
		||||
				fatal("premature end of file")
 | 
			
		||||
			v = v RS
 | 
			
		||||
		} else if ((unescaped = escape_sequence())) {
 | 
			
		||||
			if (unescaped == "\\" || unescaped == "$")
 | 
			
		||||
				v = v "\\"
 | 
			
		||||
			else if (unescaped == ";")
 | 
			
		||||
				v = v "\\\\"
 | 
			
		||||
			v = v unescaped
 | 
			
		||||
		} else if (unescaped == "") {
 | 
			
		||||
			# quoted_continuation
 | 
			
		||||
		} else {
 | 
			
		||||
			v = v substr($0, 1, 1)
 | 
			
		||||
			$0 = substr($0, 2)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function finalize_quoted(expanded,    v) {
 | 
			
		||||
	while (match(expanded, /\\./)) {
 | 
			
		||||
		v = v substr(expanded, 1, RSTART - 1) \
 | 
			
		||||
			substr(expanded, RSTART + 1, 1)
 | 
			
		||||
		expanded = substr(expanded, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
	Args[++N] = v expanded
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function unquoted_argument(    v, unescaped) {
 | 
			
		||||
	while (1) {
 | 
			
		||||
		if (match($0, /^[^[:space:]()#"\\]+/)) {
 | 
			
		||||
			v = v substr($0, RSTART, RLENGTH)
 | 
			
		||||
			$0 = substr($0, RSTART + RLENGTH)
 | 
			
		||||
		} else if ((unescaped = escape_sequence())) {
 | 
			
		||||
			if (unescaped == "\\" || unescaped == "$" || unescaped == ";")
 | 
			
		||||
				v = v "\\"
 | 
			
		||||
			v = v unescaped
 | 
			
		||||
		} else if (unescaped == "") {
 | 
			
		||||
			fatal("unexpected backslash in an unquoted argument")
 | 
			
		||||
		} else {
 | 
			
		||||
			# unquoted_legacy is not supported.
 | 
			
		||||
			return v
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function finalize_unquoted(expanded,    v) {
 | 
			
		||||
	while (expanded) {
 | 
			
		||||
		if (expanded ~ /^;/) {
 | 
			
		||||
			if (v)
 | 
			
		||||
				Args[++N] = v
 | 
			
		||||
			v = ""
 | 
			
		||||
			expanded = substr(expanded, 2)
 | 
			
		||||
		} else if (expanded ~ /^\\./) {
 | 
			
		||||
			v = v substr(expanded, 2, 1)
 | 
			
		||||
			expanded = substr(expanded, 3)
 | 
			
		||||
		} else {
 | 
			
		||||
			v = v substr(expanded, 1, 1)
 | 
			
		||||
			expanded = substr(expanded, 2)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (v)
 | 
			
		||||
		Args[++N] = v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# We keep and reprocess some escape sequences in here.
 | 
			
		||||
function argument(    arg, expanded, v) {
 | 
			
		||||
	if (regexp("\\[=*\\["))
 | 
			
		||||
		Args[++N] = unbracket(RLENGTH - 2)
 | 
			
		||||
	else if ((arg = quoted_argument()) || arg == "")
 | 
			
		||||
		finalize_quoted(expand(arg))
 | 
			
		||||
	else if ((arg = unquoted_argument()))
 | 
			
		||||
		finalize_unquoted(expand(arg))
 | 
			
		||||
	else
 | 
			
		||||
		return 0
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
function identifier(    v) {
 | 
			
		||||
	if (!match($0, /^[A-Za-z_][A-Za-z0-9_]*/))
 | 
			
		||||
		return 0
 | 
			
		||||
	v = substr($0, 1, RLENGTH)
 | 
			
		||||
	$0 = substr($0, RLENGTH + 1)
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function separation() {
 | 
			
		||||
	if (space() || bracket_comment())
 | 
			
		||||
		return 1
 | 
			
		||||
 | 
			
		||||
	if (!line_ending())
 | 
			
		||||
		return 0
 | 
			
		||||
	if (getline > 0)
 | 
			
		||||
		return 1
 | 
			
		||||
	fatal("premature end of file")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function command_invocation(    level) {
 | 
			
		||||
	while (space()) {}
 | 
			
		||||
	Command = identifier()
 | 
			
		||||
	if (!Command)
 | 
			
		||||
		return 0
 | 
			
		||||
	while (space()) {}
 | 
			
		||||
 | 
			
		||||
	Command = tolower(Command)
 | 
			
		||||
	for (N in Args)
 | 
			
		||||
		delete Args[N]
 | 
			
		||||
 | 
			
		||||
	N = 0
 | 
			
		||||
	expect(literal("("))
 | 
			
		||||
	while (1) {
 | 
			
		||||
		while (separation()) {}
 | 
			
		||||
		if (literal(")")) {
 | 
			
		||||
			if (!level--)
 | 
			
		||||
				break
 | 
			
		||||
			Args[++N] = ")"
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		if (literal("(")) {
 | 
			
		||||
			level++
 | 
			
		||||
			Args[++N] = "("
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		expect(argument())
 | 
			
		||||
		if (!/^[()]/)
 | 
			
		||||
			expect(separation())
 | 
			
		||||
	}
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
	command_invocation()
 | 
			
		||||
	expect(line_ending())
 | 
			
		||||
}
 | 
			
		||||
@@ -1,324 +0,0 @@
 | 
			
		||||
# lxdrgen-c.awk: C backend for lxdrgen.awk.
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
# SPDX-License-Identifier: 0BSD
 | 
			
		||||
#
 | 
			
		||||
# Neither *_new() nor *_destroy() functions are provided, because they'd only
 | 
			
		||||
# be useful for top-levels, and are merely extra malloc()/free() calls.
 | 
			
		||||
# Users are expected to reuse buffers.
 | 
			
		||||
#
 | 
			
		||||
# Similarly, no constructors are produced--those are easy to write manually.
 | 
			
		||||
#
 | 
			
		||||
# All arrays are deserialized zero-terminated, so u8<> and i8<> can be directly
 | 
			
		||||
# used as C strings.
 | 
			
		||||
#
 | 
			
		||||
# All types must be able to dispose partially zero values going from the back,
 | 
			
		||||
# i.e., in the reverse order of deserialization.
 | 
			
		||||
 | 
			
		||||
function define_internal(name, ctype) {
 | 
			
		||||
	Types[name] = "internal"
 | 
			
		||||
	CodegenCType[name] = ctype
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function define_int(shortname, ctype) {
 | 
			
		||||
	define_internal(shortname, ctype)
 | 
			
		||||
	CodegenSerialize[shortname] = \
 | 
			
		||||
		"\tstr_pack_" shortname "(w, %s);\n"
 | 
			
		||||
	CodegenDeserialize[shortname] = \
 | 
			
		||||
		"\tif (!msg_unpacker_" shortname "(r, &%s))\n" \
 | 
			
		||||
		"\t\treturn false;\n"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function define_sint(size) { define_int("i" size, "int" size "_t") }
 | 
			
		||||
function define_uint(size) { define_int("u" size, "uint" size "_t") }
 | 
			
		||||
 | 
			
		||||
function codegen_begin() {
 | 
			
		||||
	define_sint("8")
 | 
			
		||||
	define_sint("16")
 | 
			
		||||
	define_sint("32")
 | 
			
		||||
	define_sint("64")
 | 
			
		||||
	define_uint("8")
 | 
			
		||||
	define_uint("16")
 | 
			
		||||
	define_uint("32")
 | 
			
		||||
	define_uint("64")
 | 
			
		||||
 | 
			
		||||
	define_internal("string", "struct str")
 | 
			
		||||
	CodegenDispose["string"] = "\tstr_free(&%s);\n"
 | 
			
		||||
	CodegenSerialize["string"] = \
 | 
			
		||||
		"\tif (!proto_string_serialize(&%s, w))\n" \
 | 
			
		||||
		"\t\treturn false;\n"
 | 
			
		||||
	CodegenDeserialize["string"] = \
 | 
			
		||||
		"\tif (!proto_string_deserialize(&%s, r))\n" \
 | 
			
		||||
		"\t\treturn false;\n"
 | 
			
		||||
 | 
			
		||||
	define_internal("bool", "bool")
 | 
			
		||||
	CodegenSerialize["bool"] = \
 | 
			
		||||
		"\tstr_pack_u8(w, !!%s);\n"
 | 
			
		||||
	CodegenDeserialize["bool"] = \
 | 
			
		||||
		"\t{\n" \
 | 
			
		||||
		"\t\tuint8_t v = 0;\n" \
 | 
			
		||||
		"\t\tif (!msg_unpacker_u8(r, &v))\n" \
 | 
			
		||||
		"\t\t\treturn false;\n" \
 | 
			
		||||
		"\t\t%s = !!v;\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	print "// Code generated from " FILENAME ". DO NOT EDIT."
 | 
			
		||||
	print "// This file directly depends on liberty.c, but doesn't include it."
 | 
			
		||||
	print ""
 | 
			
		||||
	print "static bool"
 | 
			
		||||
	print "proto_string_serialize(const struct str *s, struct str *w) {"
 | 
			
		||||
	print "\tif (s->len > UINT32_MAX)"
 | 
			
		||||
	print "\t\treturn false;"
 | 
			
		||||
	print "\tstr_pack_u32(w, s->len);"
 | 
			
		||||
	print "\tstr_append_str(w, s);"
 | 
			
		||||
	print "\treturn true;"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
	print "static bool"
 | 
			
		||||
	print "proto_string_deserialize(struct str *s, struct msg_unpacker *r) {"
 | 
			
		||||
	print "\tuint32_t len = 0;"
 | 
			
		||||
	print "\tif (!msg_unpacker_u32(r, &len))"
 | 
			
		||||
	print "\t\treturn false;"
 | 
			
		||||
	print "\tif (msg_unpacker_get_available(r) < len)"
 | 
			
		||||
	print "\t\treturn false;"
 | 
			
		||||
	print "\t*s = str_make();"
 | 
			
		||||
	print "\tstr_append_data(s, r->data + r->offset, len);"
 | 
			
		||||
	print "\tr->offset += len;"
 | 
			
		||||
	print "\tif (!utf8_validate (s->str, s->len))"
 | 
			
		||||
	print "\t\treturn false;"
 | 
			
		||||
	print "\treturn true;"
 | 
			
		||||
	print "}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_constant(name, value) {
 | 
			
		||||
	print ""
 | 
			
		||||
	print "enum { " PrefixUpper name " = " value " };"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_enum_value(name, subname, value, cg) {
 | 
			
		||||
	append(cg, "fields",
 | 
			
		||||
		"\t" PrefixUpper toupper(cameltosnake(name)) "_" subname \
 | 
			
		||||
		" = " value ",\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_enum(name, cg,    ctype) {
 | 
			
		||||
	ctype = "enum " PrefixLower cameltosnake(name)
 | 
			
		||||
	print ""
 | 
			
		||||
	print ctype " {"
 | 
			
		||||
	print cg["fields"] "};"
 | 
			
		||||
 | 
			
		||||
	# XXX: This should also check if it isn't out-of-range for any reason,
 | 
			
		||||
	# but our usage of sprintf() stands in the way a bit.
 | 
			
		||||
	CodegenSerialize[name] = "\tstr_pack_i8(w, %s);\n"
 | 
			
		||||
	CodegenDeserialize[name] = \
 | 
			
		||||
		"\t{\n" \
 | 
			
		||||
		"\t\tint8_t v = 0;\n" \
 | 
			
		||||
		"\t\tif (!msg_unpacker_i8(r, &v) || !v)\n" \
 | 
			
		||||
		"\t\t\treturn false;\n" \
 | 
			
		||||
		"\t\t%s = v;\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	CodegenCType[name] = ctype
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_tag(d, cg,    f) {
 | 
			
		||||
	f = "self->" d["name"]
 | 
			
		||||
	append(cg, "fields", "\t" CodegenCType[d["type"]] " " d["name"] ";\n")
 | 
			
		||||
	append(cg, "dispose", sprintf(CodegenDispose[d["type"]], f))
 | 
			
		||||
	append(cg, "serialize", sprintf(CodegenSerialize[d["type"]], f))
 | 
			
		||||
	# Do not deserialize here, that would be out of order.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_field(d, cg,    f, dispose, serialize, deserialize) {
 | 
			
		||||
	f = "self->" d["name"]
 | 
			
		||||
	dispose = CodegenDispose[d["type"]]
 | 
			
		||||
	serialize = CodegenSerialize[d["type"]]
 | 
			
		||||
	deserialize = CodegenDeserialize[d["type"]]
 | 
			
		||||
	if (!d["isarray"]) {
 | 
			
		||||
		append(cg, "fields", "\t" CodegenCType[d["type"]] " " d["name"] ";\n")
 | 
			
		||||
		append(cg, "dispose", sprintf(dispose, f))
 | 
			
		||||
		append(cg, "serialize", sprintf(serialize, f))
 | 
			
		||||
		append(cg, "deserialize", sprintf(deserialize, f))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "fields",
 | 
			
		||||
		"\t" CodegenCType["u32"] " " d["name"] "_len;\n" \
 | 
			
		||||
		"\t" CodegenCType[d["type"]] " *" d["name"] ";\n")
 | 
			
		||||
 | 
			
		||||
	if (dispose)
 | 
			
		||||
		append(cg, "dispose", "\tif (" f ")\n" \
 | 
			
		||||
			"\t\tfor (size_t i = 0; i < " f "_len; i++)\n" \
 | 
			
		||||
			indent(indent(sprintf(dispose, f "[i]"))))
 | 
			
		||||
	append(cg, "dispose", "\tfree(" f ");\n")
 | 
			
		||||
 | 
			
		||||
	append(cg, "serialize", sprintf(CodegenSerialize["u32"], f "_len"))
 | 
			
		||||
	if (d["type"] == "u8" || d["type"] == "i8") {
 | 
			
		||||
		append(cg, "serialize",
 | 
			
		||||
			"\tstr_append_data(w, " f ", " f "_len);\n")
 | 
			
		||||
	} else if (serialize) {
 | 
			
		||||
		append(cg, "serialize",
 | 
			
		||||
			"\tfor (size_t i = 0; i < " f "_len; i++)\n" \
 | 
			
		||||
			indent(sprintf(serialize, f "[i]")))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "deserialize", sprintf(CodegenDeserialize["u32"], f "_len") \
 | 
			
		||||
		"\tif (!(" f " = calloc(" f "_len + 1, sizeof *" f ")))\n" \
 | 
			
		||||
		"\t\treturn false;\n")
 | 
			
		||||
	if (d["type"] == "u8" || d["type"] == "i8") {
 | 
			
		||||
		append(cg, "deserialize",
 | 
			
		||||
			"\tif (msg_unpacker_get_available(r) < " f "_len)\n" \
 | 
			
		||||
			"\t\treturn false;\n" \
 | 
			
		||||
			"\tmemcpy(" f ", r->data + r->offset, " f "_len);\n" \
 | 
			
		||||
			"\tr->offset += " f "_len;\n")
 | 
			
		||||
	} else if (deserialize) {
 | 
			
		||||
		append(cg, "deserialize",
 | 
			
		||||
			"\tfor (size_t i = 0; i < " f "_len; i++)\n" \
 | 
			
		||||
			indent(sprintf(deserialize, f "[i]")))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct(name, cg,    ctype, funcname) {
 | 
			
		||||
	ctype = "struct " PrefixLower cameltosnake(name)
 | 
			
		||||
	print ""
 | 
			
		||||
	print ctype " {"
 | 
			
		||||
	print cg["fields"] "};"
 | 
			
		||||
 | 
			
		||||
	if (cg["dispose"]) {
 | 
			
		||||
		funcname = PrefixLower cameltosnake(name) "_free"
 | 
			
		||||
		print ""
 | 
			
		||||
		print "static void\n" funcname "(" ctype " *self) {"
 | 
			
		||||
		print cg["dispose"] "}"
 | 
			
		||||
 | 
			
		||||
		CodegenDispose[name] = "\t" funcname "(&%s);\n"
 | 
			
		||||
	}
 | 
			
		||||
	if (cg["serialize"]) {
 | 
			
		||||
		funcname = PrefixLower cameltosnake(name) "_serialize"
 | 
			
		||||
		print ""
 | 
			
		||||
		print "static bool\n" \
 | 
			
		||||
			  funcname "(\n\t\tconst " ctype " *self, struct str *w) {"
 | 
			
		||||
		print cg["serialize"] "\treturn true;"
 | 
			
		||||
		print "}"
 | 
			
		||||
 | 
			
		||||
		CodegenSerialize[name] = "\tif (!" funcname "(&%s, w))\n" \
 | 
			
		||||
			"\t\treturn false;\n"
 | 
			
		||||
	}
 | 
			
		||||
	if (cg["deserialize"]) {
 | 
			
		||||
		funcname = PrefixLower cameltosnake(name) "_deserialize"
 | 
			
		||||
		print ""
 | 
			
		||||
		print "static bool\n" \
 | 
			
		||||
			  funcname "(\n\t\t" ctype " *self, struct msg_unpacker *r) {"
 | 
			
		||||
		print cg["deserialize"] "\treturn true;"
 | 
			
		||||
		print "}"
 | 
			
		||||
 | 
			
		||||
		CodegenDeserialize[name] = "\tif (!" funcname "(&%s, r))\n" \
 | 
			
		||||
			"\t\treturn false;\n"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CodegenCType[name] = ctype
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union_tag(d, cg) {
 | 
			
		||||
	cg["tagtype"] = d["type"]
 | 
			
		||||
	cg["tagname"] = d["name"]
 | 
			
		||||
	append(cg, "fields", "\t" CodegenCType[d["type"]] " " d["name"] ";\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union_struct( \
 | 
			
		||||
		name, casename, cg, scg,     structname, fieldname, fullcasename) {
 | 
			
		||||
	# Don't generate obviously useless structs.
 | 
			
		||||
	fullcasename = toupper(cameltosnake(cg["tagtype"])) "_" casename
 | 
			
		||||
	if (!scg["dispose"] && !scg["deserialize"]) {
 | 
			
		||||
		append(cg, "structless", "\tcase " PrefixUpper fullcasename ":\n")
 | 
			
		||||
		for (i in scg)
 | 
			
		||||
			delete scg[i]
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	# And thus not all generated structs are present in Types.
 | 
			
		||||
	structname = name "_" casename
 | 
			
		||||
	fieldname = tolower(casename)
 | 
			
		||||
	codegen_struct(structname, scg)
 | 
			
		||||
 | 
			
		||||
	append(cg, "fields", "\t" CodegenCType[structname] " " fieldname ";\n")
 | 
			
		||||
	if (CodegenDispose[structname])
 | 
			
		||||
		append(cg, "dispose", "\tcase " PrefixUpper fullcasename ":\n" \
 | 
			
		||||
			indent(sprintf(CodegenDispose[structname], "self->" fieldname)) \
 | 
			
		||||
			"\t\tbreak;\n")
 | 
			
		||||
 | 
			
		||||
	# With no de/serialization code, this will simply recognize the tag.
 | 
			
		||||
	append(cg, "serialize", "\tcase " PrefixUpper fullcasename ":\n" \
 | 
			
		||||
		indent(sprintf(CodegenSerialize[structname], "self->" fieldname)) \
 | 
			
		||||
		"\t\tbreak;\n")
 | 
			
		||||
	append(cg, "deserialize", "\tcase " PrefixUpper fullcasename ":\n" \
 | 
			
		||||
		indent(sprintf(CodegenDeserialize[structname], "self->" fieldname)) \
 | 
			
		||||
		"\t\tbreak;\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union(name, cg,    f, ctype, funcname) {
 | 
			
		||||
	ctype = "union " PrefixLower cameltosnake(name)
 | 
			
		||||
	print ""
 | 
			
		||||
	print ctype " {"
 | 
			
		||||
	print cg["fields"] "};"
 | 
			
		||||
 | 
			
		||||
	f = "self->" cg["tagname"]
 | 
			
		||||
	if (cg["dispose"]) {
 | 
			
		||||
		funcname = PrefixLower cameltosnake(name) "_free"
 | 
			
		||||
		print ""
 | 
			
		||||
		print "static void\n" funcname "(" ctype " *self) {"
 | 
			
		||||
		print "\tswitch (" f ") {"
 | 
			
		||||
		if (cg["structless"])
 | 
			
		||||
			print cg["structless"] \
 | 
			
		||||
				indent(sprintf(CodegenDispose[cg["tagtype"]], f)) "\t\tbreak;"
 | 
			
		||||
		print cg["dispose"] "\tdefault:"
 | 
			
		||||
		print "\t\tbreak;"
 | 
			
		||||
		print "\t}"
 | 
			
		||||
		print "}"
 | 
			
		||||
 | 
			
		||||
		CodegenDispose[name] = "\t" funcname "(&%s);\n"
 | 
			
		||||
	}
 | 
			
		||||
	if (cg["serialize"]) {
 | 
			
		||||
		funcname = PrefixLower cameltosnake(name) "_serialize"
 | 
			
		||||
		print ""
 | 
			
		||||
		print "static bool\n" \
 | 
			
		||||
			  funcname "(\n\t\tconst " ctype " *self, struct str *w) {"
 | 
			
		||||
		print "\tswitch (" f ") {"
 | 
			
		||||
		if (cg["structless"])
 | 
			
		||||
			print cg["structless"] \
 | 
			
		||||
				indent(sprintf(CodegenSerialize[cg["tagtype"]], f)) "\t\tbreak;"
 | 
			
		||||
		print cg["serialize"] "\tdefault:"
 | 
			
		||||
		print "\t\treturn false;"
 | 
			
		||||
		print "\t}"
 | 
			
		||||
		print "\treturn true;"
 | 
			
		||||
		print "}"
 | 
			
		||||
 | 
			
		||||
		CodegenSerialize[name] = "\tif (!" funcname "(&%s, w))\n" \
 | 
			
		||||
			"\t\treturn false;\n"
 | 
			
		||||
	}
 | 
			
		||||
	if (cg["deserialize"]) {
 | 
			
		||||
		funcname = PrefixLower cameltosnake(name) "_deserialize"
 | 
			
		||||
		print ""
 | 
			
		||||
		print "static bool\n" \
 | 
			
		||||
			  funcname "(\n\t\t" ctype " *self, struct msg_unpacker *r) {"
 | 
			
		||||
		print sprintf(CodegenDeserialize[cg["tagtype"]], f)
 | 
			
		||||
		print "\tswitch (" f ") {"
 | 
			
		||||
		if (cg["structless"])
 | 
			
		||||
			print cg["structless"] "\t\tbreak;"
 | 
			
		||||
		print cg["deserialize"] "\tdefault:"
 | 
			
		||||
		print "\t\treturn false;"
 | 
			
		||||
		print "\t}"
 | 
			
		||||
		print "\treturn true;"
 | 
			
		||||
		print "}"
 | 
			
		||||
 | 
			
		||||
		CodegenDeserialize[name] = "\tif (!" funcname "(&%s, r))\n" \
 | 
			
		||||
			"\t\treturn false;\n"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CodegenCType[name] = ctype
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
@@ -1,541 +0,0 @@
 | 
			
		||||
# lxdrgen-go.awk: Go backend for lxdrgen.awk.
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
# SPDX-License-Identifier: 0BSD
 | 
			
		||||
#
 | 
			
		||||
# This backend also enables proxying to other endpoints using JSON.
 | 
			
		||||
 | 
			
		||||
function define_internal(name, gotype) {
 | 
			
		||||
	Types[name] = "internal"
 | 
			
		||||
	CodegenGoType[name] = gotype
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function define_sint(size,    shortname, gotype) {
 | 
			
		||||
	shortname = "i" size
 | 
			
		||||
	gotype = "int" size
 | 
			
		||||
	define_internal(shortname, gotype)
 | 
			
		||||
 | 
			
		||||
	CodegenAppendJSON[shortname] = \
 | 
			
		||||
		"\tb = strconv.AppendInt(b, int64(%s), 10)\n"
 | 
			
		||||
	if (size == 8) {
 | 
			
		||||
		CodegenSerialize[shortname] = "\tdata = append(data, uint8(%s))\n"
 | 
			
		||||
		CodegenDeserialize[shortname] = \
 | 
			
		||||
			"\tif len(data) >= 1 {\n" \
 | 
			
		||||
			"\t\t%s, data = int8(data[0]), data[1:]\n" \
 | 
			
		||||
			"\t} else {\n" \
 | 
			
		||||
			"\t\treturn nil, false\n" \
 | 
			
		||||
			"\t}\n"
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CodegenSerialize[shortname] = \
 | 
			
		||||
		"\tdata = binary.BigEndian.AppendUint" size "(data, uint" size "(%s))\n"
 | 
			
		||||
	CodegenDeserialize[shortname] = \
 | 
			
		||||
		"\tif len(data) >= " (size / 8) " {\n" \
 | 
			
		||||
		"\t\t%s = " gotype "(binary.BigEndian.Uint" size "(data))\n" \
 | 
			
		||||
		"\t\tdata = data[" (size / 8) ":]\n" \
 | 
			
		||||
		"\t} else {\n" \
 | 
			
		||||
		"\t\treturn nil, false\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function define_uint(size,    shortname, gotype) {
 | 
			
		||||
	# Both []byte and []uint8 luckily marshal as base64-encoded JSON strings,
 | 
			
		||||
	# so there's no need to rename the type as an exception.
 | 
			
		||||
	shortname = "u" size
 | 
			
		||||
	gotype = "uint" size
 | 
			
		||||
	define_internal(shortname, gotype)
 | 
			
		||||
 | 
			
		||||
	CodegenAppendJSON[shortname] = \
 | 
			
		||||
		"\tb = strconv.AppendUint(b, uint64(%s), 10)\n"
 | 
			
		||||
	if (size == 8) {
 | 
			
		||||
		CodegenSerialize[shortname] = "\tdata = append(data, %s)\n"
 | 
			
		||||
		CodegenDeserialize[shortname] = \
 | 
			
		||||
			"\tif len(data) >= 1 {\n" \
 | 
			
		||||
			"\t\t%s, data = data[0], data[1:]\n" \
 | 
			
		||||
			"\t} else {\n" \
 | 
			
		||||
			"\t\treturn nil, false\n" \
 | 
			
		||||
			"\t}\n"
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CodegenSerialize[shortname] = \
 | 
			
		||||
		"\tdata = binary.BigEndian.AppendUint" size "(data, %s)\n"
 | 
			
		||||
	CodegenDeserialize[shortname] = \
 | 
			
		||||
		"\tif len(data) >= " (size / 8) " {\n" \
 | 
			
		||||
		"\t\t%s = binary.BigEndian.Uint" size "(data)\n" \
 | 
			
		||||
		"\t\tdata = data[" (size / 8) ":]\n" \
 | 
			
		||||
		"\t} else {\n" \
 | 
			
		||||
		"\t\treturn nil, false\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Currently two outputs cannot coexist within the same package.
 | 
			
		||||
function codegen_private(name) {
 | 
			
		||||
	return "proto" name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_begin(    funcname) {
 | 
			
		||||
	define_sint("8")
 | 
			
		||||
	define_sint("16")
 | 
			
		||||
	define_sint("32")
 | 
			
		||||
	define_sint("64")
 | 
			
		||||
	define_uint("8")
 | 
			
		||||
	define_uint("16")
 | 
			
		||||
	define_uint("32")
 | 
			
		||||
	define_uint("64")
 | 
			
		||||
	define_internal("bool", "bool")
 | 
			
		||||
	define_internal("string", "string")
 | 
			
		||||
 | 
			
		||||
	# Cater to "go generate", for what it's worth.
 | 
			
		||||
	CodegenPackage = ENV["GOPACKAGE"]
 | 
			
		||||
	if (!CodegenPackage)
 | 
			
		||||
		CodegenPackage = "main"
 | 
			
		||||
 | 
			
		||||
	print "// Code generated from " FILENAME ". DO NOT EDIT."
 | 
			
		||||
	print ""
 | 
			
		||||
	print "package " CodegenPackage
 | 
			
		||||
	print ""
 | 
			
		||||
	print "import ("
 | 
			
		||||
	print "\t`encoding/base64`"
 | 
			
		||||
	print "\t`encoding/binary`"
 | 
			
		||||
	print "\t`encoding/json`"
 | 
			
		||||
	print "\t`errors`"
 | 
			
		||||
	print "\t`math`"
 | 
			
		||||
	print "\t`strconv`"
 | 
			
		||||
	print "\t`unicode/utf8`"
 | 
			
		||||
	print ")"
 | 
			
		||||
	print ""
 | 
			
		||||
	print "// This is a hack to always use the base64 import."
 | 
			
		||||
	print "var _ = base64.StdEncoding"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenAppendJSON["bool"] = \
 | 
			
		||||
		"\tb = strconv.AppendBool(b, %s)\n"
 | 
			
		||||
	CodegenSerialize["bool"] = \
 | 
			
		||||
		"\tif %s {\n" \
 | 
			
		||||
		"\t\tdata = append(data, 1)\n" \
 | 
			
		||||
		"\t} else {\n" \
 | 
			
		||||
		"\t\tdata = append(data, 0)\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	funcname = codegen_private("ConsumeBoolFrom")
 | 
			
		||||
	print "// " funcname " tries to deserialize a boolean value"
 | 
			
		||||
	print "// from the beginning of a byte stream. When successful,"
 | 
			
		||||
	print "// it returns a subslice with any data that might follow."
 | 
			
		||||
	print "func " funcname "(data []byte, b *bool) ([]byte, bool) {"
 | 
			
		||||
	print "\tif len(data) < 1 {"
 | 
			
		||||
	print "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\tif data[0] != 0 {"
 | 
			
		||||
	print "\t\t*b = true"
 | 
			
		||||
	print "\t} else {"
 | 
			
		||||
	print "\t\t*b = false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn data[1:], true"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenDeserialize["bool"] = \
 | 
			
		||||
		"\tif data, ok = " funcname "(data, &%s); !ok {\n" \
 | 
			
		||||
		"\t\treturn nil, ok\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	funcname = codegen_private("AppendStringTo")
 | 
			
		||||
	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 "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\tdata = binary.BigEndian.AppendUint32(data, uint32(len(s)))"
 | 
			
		||||
	print "\treturn append(data, s...), true"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenSerialize["string"] = \
 | 
			
		||||
		"\tif data, ok = " funcname "(data, %s); !ok {\n" \
 | 
			
		||||
		"\t\treturn nil, ok\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	funcname = codegen_private("ConsumeStringFrom")
 | 
			
		||||
	print "// " funcname " tries to deserialize a string value"
 | 
			
		||||
	print "// from the beginning of a byte stream. When successful,"
 | 
			
		||||
	print "// it returns a subslice with any data that might follow."
 | 
			
		||||
	print "func " funcname "(data []byte, s *string) ([]byte, bool) {"
 | 
			
		||||
	print "\tif len(data) < 4 {"
 | 
			
		||||
	print "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\tlength := binary.BigEndian.Uint32(data)"
 | 
			
		||||
	print "\tif data = data[4:]; uint64(len(data)) < uint64(length) {"
 | 
			
		||||
	print "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\t*s = string(data[:length])"
 | 
			
		||||
	print "\tif !utf8.ValidString(*s) {"
 | 
			
		||||
	print "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn data[length:], true"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenDeserialize["string"] = \
 | 
			
		||||
		"\tif data, ok = " funcname "(data, &%s); !ok {\n" \
 | 
			
		||||
		"\t\treturn nil, ok\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	funcname = codegen_private("UnmarshalEnumJSON")
 | 
			
		||||
	print "// " funcname " converts a JSON fragment to an integer,"
 | 
			
		||||
	print "// ensuring that it's within the expected range of enum values."
 | 
			
		||||
	print "func " funcname "(data []byte) (int64, error) {"
 | 
			
		||||
	print "\tvar n int64"
 | 
			
		||||
	print "\tif err := json.Unmarshal(data, &n); err != nil {"
 | 
			
		||||
	print "\t\treturn 0, err"
 | 
			
		||||
	print "\t} else if n > math.MaxInt8 || n < math.MinInt8 {"
 | 
			
		||||
	print "\t\treturn 0, errors.New(`integer out of range`)"
 | 
			
		||||
	print "\t} else {"
 | 
			
		||||
	print "\t\treturn n, nil"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_constant(name, value) {
 | 
			
		||||
	print "const " PrefixCamel snaketocamel(name) " = " value
 | 
			
		||||
	print ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_enum_value(name, subname, value, cg,    goname) {
 | 
			
		||||
	goname = PrefixCamel name snaketocamel(subname)
 | 
			
		||||
	append(cg, "fields",
 | 
			
		||||
		"\t" goname " = " value "\n")
 | 
			
		||||
	append(cg, "stringer",
 | 
			
		||||
		"\tcase " goname ":\n" \
 | 
			
		||||
		"\t\treturn `" snaketocamel(subname) "`\n")
 | 
			
		||||
	append(cg, "marshal",
 | 
			
		||||
		goname ",\n")
 | 
			
		||||
	append(cg, "unmarshal",
 | 
			
		||||
		"\tcase `" snaketocamel(subname) "`:\n" \
 | 
			
		||||
		"\t\t*v = " goname "\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_enum(name, cg,    gotype, fields, funcname) {
 | 
			
		||||
	gotype = PrefixCamel name
 | 
			
		||||
	print "type " gotype " int8"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	print "const ("
 | 
			
		||||
	print cg["fields"] ")"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	print "func (v " gotype ") String() string {"
 | 
			
		||||
	print "\tswitch v {"
 | 
			
		||||
	print cg["stringer"] "\tdefault:"
 | 
			
		||||
	print "\t\treturn strconv.Itoa(int(v))"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenIsMarshaler[name] = 1
 | 
			
		||||
	fields = cg["marshal"]
 | 
			
		||||
	sub(/,\n$/, ":", fields)
 | 
			
		||||
	gsub(/\n/, "\n\t", fields)
 | 
			
		||||
	print "func (v " gotype ") MarshalJSON() ([]byte, error) {"
 | 
			
		||||
	print "\tswitch v {"
 | 
			
		||||
	print indent("case " fields)
 | 
			
		||||
	print "\t\treturn []byte(`\"` + v.String() + `\"`), nil"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn json.Marshal(int(v))"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	funcname = codegen_private("UnmarshalEnumJSON")
 | 
			
		||||
	print "func (v *" gotype ") UnmarshalJSON(data []byte) error {"
 | 
			
		||||
	print "\tvar s string"
 | 
			
		||||
	print "\tif json.Unmarshal(data, &s) == nil {"
 | 
			
		||||
	print "\t\t// Handled below."
 | 
			
		||||
	print "\t} else if n, err := " funcname "(data); err != nil {"
 | 
			
		||||
	print "\t\treturn err"
 | 
			
		||||
	print "\t} else {"
 | 
			
		||||
	print "\t\t*v = " gotype "(n)"
 | 
			
		||||
	print "\t\treturn nil"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\tswitch s {"
 | 
			
		||||
	print cg["unmarshal"] "\tdefault:"
 | 
			
		||||
	print "\t\treturn errors.New(`unrecognized value: ` + s)"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn nil"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	# XXX: This should also check if it isn't out-of-range for any reason,
 | 
			
		||||
	# but our usage of sprintf() stands in the way a bit.
 | 
			
		||||
	CodegenSerialize[name] = "\tdata = append(data, uint8(%s))\n"
 | 
			
		||||
	CodegenDeserialize[name] = \
 | 
			
		||||
		"\tif len(data) >= 1 {\n" \
 | 
			
		||||
		"\t\t%s, data = " gotype "(data[0]), data[1:]\n" \
 | 
			
		||||
		"\t} else {\n" \
 | 
			
		||||
		"\t\treturn nil, false\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	CodegenGoType[name] = gotype
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_marshal(type, f,    marshal) {
 | 
			
		||||
	if (CodegenAppendJSON[type])
 | 
			
		||||
		return sprintf(CodegenAppendJSON[type], f)
 | 
			
		||||
 | 
			
		||||
	# Complex types are json.Marshalers, there's no need to json.Marshal(&f).
 | 
			
		||||
	if (CodegenIsMarshaler[type])
 | 
			
		||||
		marshal = f ".MarshalJSON()"
 | 
			
		||||
	else
 | 
			
		||||
		marshal = "json.Marshal(" f ")"
 | 
			
		||||
 | 
			
		||||
	return \
 | 
			
		||||
		"\tif j, err := " marshal "; err != nil {\n" \
 | 
			
		||||
		"\t\treturn nil, err\n" \
 | 
			
		||||
		"\t} else {\n" \
 | 
			
		||||
		"\t\tb = append(b, j...)\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_field_marshal(d, cg,    camel, f, marshal) {
 | 
			
		||||
	camel = snaketocamel(d["name"])
 | 
			
		||||
	f = "s." camel
 | 
			
		||||
	if (!d["isarray"]) {
 | 
			
		||||
		append(cg, "marshal",
 | 
			
		||||
			"\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \
 | 
			
		||||
			codegen_marshal(d["type"], f))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	# Note that we do not produce `null` for nil slices, unlike encoding/json.
 | 
			
		||||
	# And arrays never get deserialized as such.
 | 
			
		||||
	if (d["type"] == "u8") {
 | 
			
		||||
		append(cg, "marshal",
 | 
			
		||||
			"\tb = append(b, `,\"" decapitalize(camel) "\":\"`...)\n" \
 | 
			
		||||
			"\tb = append(b, base64.StdEncoding.EncodeToString(" f ")...)\n" \
 | 
			
		||||
			"\tb = append(b, '\"')\n")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "marshal",
 | 
			
		||||
		"\tb = append(b, `,\"" decapitalize(camel) "\":[`...)\n" \
 | 
			
		||||
		"\tfor i := 0; i < len(" f "); i++ {\n" \
 | 
			
		||||
		"\t\tif i > 0 {\n" \
 | 
			
		||||
		"\t\t\tb = append(b, ',')\n" \
 | 
			
		||||
		"\t\t}\n" \
 | 
			
		||||
		indent(codegen_marshal(d["type"], f "[i]")) \
 | 
			
		||||
		"\t}\n" \
 | 
			
		||||
		"\tb = append(b, ']')\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_field(d, cg,    camel, f, serialize, deserialize) {
 | 
			
		||||
	codegen_struct_field_marshal(d, cg)
 | 
			
		||||
 | 
			
		||||
	camel = snaketocamel(d["name"])
 | 
			
		||||
	f = "s." camel
 | 
			
		||||
	serialize = CodegenSerialize[d["type"]]
 | 
			
		||||
	deserialize = CodegenDeserialize[d["type"]]
 | 
			
		||||
	if (!d["isarray"]) {
 | 
			
		||||
		append(cg, "fields", "\t" camel " " CodegenGoType[d["type"]] \
 | 
			
		||||
			" `json:\"" decapitalize(camel) "\"`\n")
 | 
			
		||||
		append(cg, "serialize", sprintf(serialize, f))
 | 
			
		||||
		append(cg, "deserialize", sprintf(deserialize, f))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "fields", "\t" camel " []" CodegenGoType[d["type"]] \
 | 
			
		||||
		" `json:\"" decapitalize(camel) "\"`\n")
 | 
			
		||||
 | 
			
		||||
	# XXX: This should also check if it isn't out-of-range for any reason.
 | 
			
		||||
	append(cg, "serialize",
 | 
			
		||||
		sprintf(CodegenSerialize["u32"], "uint32(len(" f "))"))
 | 
			
		||||
	if (d["type"] == "u8") {
 | 
			
		||||
		append(cg, "serialize",
 | 
			
		||||
			"\tdata = append(data, " f "...)\n")
 | 
			
		||||
	} else {
 | 
			
		||||
		append(cg, "serialize",
 | 
			
		||||
			"\tfor i := 0; i < len(" f "); i++ {\n" \
 | 
			
		||||
			indent(sprintf(serialize, f "[i]")) \
 | 
			
		||||
			"\t}\n")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "deserialize",
 | 
			
		||||
		"\t{\n" \
 | 
			
		||||
		"\t\tvar length uint32\n" \
 | 
			
		||||
		indent(sprintf(CodegenDeserialize["u32"], "length")))
 | 
			
		||||
	if (d["type"] == "u8") {
 | 
			
		||||
		append(cg, "deserialize",
 | 
			
		||||
			"\t\tif uint64(len(data)) < uint64(length) {\n" \
 | 
			
		||||
			"\t\t\treturn nil, false\n" \
 | 
			
		||||
			"\t\t}\n" \
 | 
			
		||||
			"\t\t" f ", data = data[:length], data[length:]\n" \
 | 
			
		||||
			"\t}\n")
 | 
			
		||||
	} else {
 | 
			
		||||
		append(cg, "deserialize",
 | 
			
		||||
			"\t\t" f " = make([]" CodegenGoType[d["type"]] ", length)\n" \
 | 
			
		||||
			"\t}\n" \
 | 
			
		||||
			"\tfor i := 0; i < len(" f "); i++ {\n" \
 | 
			
		||||
			indent(sprintf(deserialize, f "[i]")) \
 | 
			
		||||
			"\t}\n")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_tag(d, cg,    camel, f) {
 | 
			
		||||
	codegen_struct_field_marshal(d, cg)
 | 
			
		||||
 | 
			
		||||
	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.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct(name, cg,    gotype) {
 | 
			
		||||
	gotype = PrefixCamel name
 | 
			
		||||
	print "type " gotype " struct {\n" cg["fields"] "}\n"
 | 
			
		||||
 | 
			
		||||
	if (cg["marshal"]) {
 | 
			
		||||
		CodegenIsMarshaler[name] = 1
 | 
			
		||||
		print "func (s *" gotype ") MarshalJSON() ([]byte, error) {"
 | 
			
		||||
		print "\tb := []byte{}"
 | 
			
		||||
		print cg["marshal"] "\tb[0] = '{'"
 | 
			
		||||
		print "\treturn append(b, '}'), nil"
 | 
			
		||||
		print "}"
 | 
			
		||||
		print ""
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (cg["serialize"]) {
 | 
			
		||||
		print "func (s *" gotype ") AppendTo(data []byte) ([]byte, bool) {"
 | 
			
		||||
		print "\tok := true"
 | 
			
		||||
		print cg["serialize"] "\treturn data, ok"
 | 
			
		||||
		print "}"
 | 
			
		||||
		print ""
 | 
			
		||||
 | 
			
		||||
		CodegenSerialize[name] = \
 | 
			
		||||
			"\tif data, ok = %s.AppendTo(data); !ok {\n" \
 | 
			
		||||
			"\t\treturn nil, ok\n" \
 | 
			
		||||
			"\t}\n"
 | 
			
		||||
	}
 | 
			
		||||
	if (cg["deserialize"]) {
 | 
			
		||||
		print "func (s *" gotype ") ConsumeFrom(data []byte) ([]byte, bool) {"
 | 
			
		||||
		print "\tok := true"
 | 
			
		||||
		print cg["deserialize"] "\treturn data, ok"
 | 
			
		||||
		print "}"
 | 
			
		||||
		print ""
 | 
			
		||||
 | 
			
		||||
		CodegenDeserialize[name] = \
 | 
			
		||||
			"\tif data, ok = %s.ConsumeFrom(data); !ok {\n" \
 | 
			
		||||
			"\t\treturn nil, ok\n" \
 | 
			
		||||
			"\t}\n"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CodegenGoType[name] = gotype
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union_tag(d, cg) {
 | 
			
		||||
	cg["tagtype"] = d["type"]
 | 
			
		||||
	cg["tagname"] = d["name"]
 | 
			
		||||
	# The tag is implied from the type of struct stored in the interface.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union_struct(name, casename, cg, scg,     structname, init) {
 | 
			
		||||
	# And thus not all generated structs are present in Types.
 | 
			
		||||
	structname = name snaketocamel(casename)
 | 
			
		||||
	codegen_struct(structname, scg)
 | 
			
		||||
 | 
			
		||||
	init = CodegenGoType[structname] "{" snaketocamel(cg["tagname"]) \
 | 
			
		||||
		": " decapitalize(snaketocamel(cg["tagname"])) "}"
 | 
			
		||||
	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")
 | 
			
		||||
	append(cg, "serialize",
 | 
			
		||||
		"\tcase *" CodegenGoType[structname] ":\n" \
 | 
			
		||||
		indent(sprintf(CodegenSerialize[structname], "union")))
 | 
			
		||||
	append(cg, "deserialize",
 | 
			
		||||
		"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
 | 
			
		||||
		"\t\ts := " init "\n" \
 | 
			
		||||
		indent(sprintf(CodegenDeserialize[structname], "s")) \
 | 
			
		||||
		"\t\tu.Interface = &s\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union(name, cg,    gotype, tagfield, tagvar) {
 | 
			
		||||
	gotype = PrefixCamel name
 | 
			
		||||
	print "type " gotype " struct {"
 | 
			
		||||
	print "\tInterface any"
 | 
			
		||||
	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 "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	tagfield = snaketocamel(cg["tagname"])
 | 
			
		||||
	tagvar = decapitalize(tagfield)
 | 
			
		||||
	print "func (u *" gotype ") UnmarshalJSON(data []byte) (err error) {"
 | 
			
		||||
	print "\tvar t struct {"
 | 
			
		||||
	print "\t\t" tagfield " " CodegenGoType[cg["tagtype"]] \
 | 
			
		||||
		" `json:\"" tagvar "\"`"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\tif err := json.Unmarshal(data, &t); err != nil {"
 | 
			
		||||
	print "\t\treturn err"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\tswitch " tagvar " := t." tagfield "; " tagvar " {"
 | 
			
		||||
	print cg["unmarshal"] "\tdefault:"
 | 
			
		||||
	print "\t\terr = errors.New(`unsupported value: ` + " tagvar ".String())"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn err"
 | 
			
		||||
	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.
 | 
			
		||||
	print "func (u *" gotype ") AppendTo(data []byte) ([]byte, bool) {"
 | 
			
		||||
	print "\tok := true"
 | 
			
		||||
	print "\tswitch union := u.Interface.(type) {"
 | 
			
		||||
	print cg["serialize"] "\tdefault:"
 | 
			
		||||
	print "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn data, ok"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenSerialize[name] = \
 | 
			
		||||
		"\tif data, ok = %s.AppendTo(data); !ok {\n" \
 | 
			
		||||
		"\t\treturn nil, ok\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	print "func (u *" gotype ") ConsumeFrom(data []byte) ([]byte, bool) {"
 | 
			
		||||
	print "\tok := true"
 | 
			
		||||
	print "\tvar " tagvar " " CodegenGoType[cg["tagtype"]]
 | 
			
		||||
	print sprintf(CodegenDeserialize[cg["tagtype"]], tagvar)
 | 
			
		||||
	print "\tswitch " tagvar " {"
 | 
			
		||||
	print cg["deserialize"] "\tdefault:"
 | 
			
		||||
	print "\t\treturn nil, false"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "\treturn data, ok"
 | 
			
		||||
	print "}"
 | 
			
		||||
	print ""
 | 
			
		||||
 | 
			
		||||
	CodegenDeserialize[name] = \
 | 
			
		||||
		"\tif data, ok = %s.ConsumeFrom(data); !ok {\n" \
 | 
			
		||||
		"\t\treturn nil, ok\n" \
 | 
			
		||||
		"\t}\n"
 | 
			
		||||
 | 
			
		||||
	CodegenGoType[name] = gotype
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
@@ -1,226 +0,0 @@
 | 
			
		||||
# lxdrgen-mjs.awk: Javascript backend for lxdrgen.awk.
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
			
		||||
# SPDX-License-Identifier: 0BSD
 | 
			
		||||
#
 | 
			
		||||
# This backend is currently for decoding the binary format only.
 | 
			
		||||
# (JSON is way too expensive to process and transfer.)
 | 
			
		||||
#
 | 
			
		||||
# Import the resulting script as a Javascript module.
 | 
			
		||||
# Identifiers intentionally aren't prefixed.
 | 
			
		||||
 | 
			
		||||
function define_internal(name) {
 | 
			
		||||
	Types[name] = "internal"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function define_sint(size,    shortname) {
 | 
			
		||||
	shortname = "i" size
 | 
			
		||||
	define_internal(shortname)
 | 
			
		||||
	CodegenDeserialize[shortname] = "\t%s = r." shortname "()\n"
 | 
			
		||||
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\t" shortname "() {"
 | 
			
		||||
	if (size == "64") {
 | 
			
		||||
		# XXX: 2^53 - 1 must be enough for anyone.  BigInts are a PITA.
 | 
			
		||||
		print "\t\tconst " shortname \
 | 
			
		||||
			" = Number(this.getBigInt" size "(this.offset))"
 | 
			
		||||
	} else {
 | 
			
		||||
		print "\t\tconst " shortname " = this.getInt" size "(this.offset)"
 | 
			
		||||
	}
 | 
			
		||||
	print "\t\tthis.offset += " (size / 8)
 | 
			
		||||
	print "\t\treturn " shortname
 | 
			
		||||
	print "\t}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function define_uint(size,    shortname) {
 | 
			
		||||
	shortname = "u" size
 | 
			
		||||
	define_internal(shortname)
 | 
			
		||||
	CodegenDeserialize[shortname] = "\t%s = r." shortname "()\n"
 | 
			
		||||
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\t" shortname "() {"
 | 
			
		||||
	if (size == "64") {
 | 
			
		||||
		# XXX: 2^53 - 1 must be enough for anyone.  BigInts are a PITA.
 | 
			
		||||
		print "\t\tconst " shortname \
 | 
			
		||||
			" = Number(this.getBigUint" size "(this.offset))"
 | 
			
		||||
	} else {
 | 
			
		||||
		print "\t\tconst " shortname " = this.getUint" size "(this.offset)"
 | 
			
		||||
	}
 | 
			
		||||
	print "\t\tthis.offset += " (size / 8)
 | 
			
		||||
	print "\t\treturn " shortname
 | 
			
		||||
	print "\t}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_begin() {
 | 
			
		||||
	print "// Code generated from " FILENAME ". DO NOT EDIT."
 | 
			
		||||
	print ""
 | 
			
		||||
	print "export class Reader extends DataView {"
 | 
			
		||||
	print "\tconstructor() {"
 | 
			
		||||
	print "\t\tsuper(...arguments)"
 | 
			
		||||
	print "\t\tthis.offset = 0"
 | 
			
		||||
	print "\t\tthis.decoder = new TextDecoder('utf-8', {fatal: true})"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\tget empty() {"
 | 
			
		||||
	print "\t\treturn this.byteLength <= this.offset"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\trequire(len) {"
 | 
			
		||||
	print "\t\tif (this.byteLength - this.offset < len)"
 | 
			
		||||
	print "\t\t\tthrow `Premature end of data`"
 | 
			
		||||
	print "\t\treturn this.byteOffset + this.offset"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
 | 
			
		||||
	define_internal("string")
 | 
			
		||||
	CodegenDeserialize["string"] = "\t%s = r.string()\n"
 | 
			
		||||
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\tstring() {"
 | 
			
		||||
	print "\t\tconst len = this.getUint32(this.offset)"
 | 
			
		||||
	print "\t\tthis.offset += 4"
 | 
			
		||||
	print "\t\tconst array = new Uint8Array("
 | 
			
		||||
	print "\t\t\tthis.buffer, this.require(len), len)"
 | 
			
		||||
	print "\t\tthis.offset += len"
 | 
			
		||||
	print "\t\treturn this.decoder.decode(array)"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
 | 
			
		||||
	define_internal("bool")
 | 
			
		||||
	CodegenDeserialize["bool"] = "\t%s = r.bool()\n"
 | 
			
		||||
 | 
			
		||||
	print ""
 | 
			
		||||
	print "\tbool() {"
 | 
			
		||||
	print "\t\tconst u8 = this.getUint8(this.offset)"
 | 
			
		||||
	print "\t\tthis.offset += 1"
 | 
			
		||||
	print "\t\treturn u8 != 0"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
 | 
			
		||||
	define_sint("8")
 | 
			
		||||
	define_sint("16")
 | 
			
		||||
	define_sint("32")
 | 
			
		||||
	define_sint("64")
 | 
			
		||||
	define_uint("8")
 | 
			
		||||
	define_uint("16")
 | 
			
		||||
	define_uint("32")
 | 
			
		||||
	define_uint("64")
 | 
			
		||||
 | 
			
		||||
	print "}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_constant(name, value) {
 | 
			
		||||
	print ""
 | 
			
		||||
	print "export const " decapitalize(snaketocamel(name)) " = " value
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_enum_value(name, subname, value, cg) {
 | 
			
		||||
	append(cg, "fields", "\t" snaketocamel(subname) ": " value ",\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_enum(name, cg) {
 | 
			
		||||
	print ""
 | 
			
		||||
	print "export const " name " = Object.freeze({"
 | 
			
		||||
	print cg["fields"] "})"
 | 
			
		||||
 | 
			
		||||
	CodegenDeserialize[name] = "\t%s = r.i8()\n"
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_field(d, cg,    camel, f, deserialize) {
 | 
			
		||||
	camel = decapitalize(snaketocamel(d["name"]))
 | 
			
		||||
	f = "s." camel
 | 
			
		||||
	append(cg, "fields", "\t" camel "\n")
 | 
			
		||||
 | 
			
		||||
	deserialize = CodegenDeserialize[d["type"]]
 | 
			
		||||
	if (!d["isarray"]) {
 | 
			
		||||
		append(cg, "deserialize", sprintf(deserialize, f))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "deserialize",
 | 
			
		||||
		"\t{\n" \
 | 
			
		||||
		indent(sprintf(CodegenDeserialize["u32"], "const len")))
 | 
			
		||||
	if (d["type"] == "u8") {
 | 
			
		||||
		append(cg, "deserialize",
 | 
			
		||||
			"\t\t" f " = new Uint8Array(\n" \
 | 
			
		||||
			"\t\t\tr.buffer, r.require(len), len)\n" \
 | 
			
		||||
			"\t\tr.offset += len\n" \
 | 
			
		||||
			"\t}\n")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if (d["type"] == "i8") {
 | 
			
		||||
		append(cg, "deserialize",
 | 
			
		||||
			"\t\t" f " = new Int8Array(\n" \
 | 
			
		||||
			"\t\t\tr.buffer, r.require(len), len)\n" \
 | 
			
		||||
			"\t\tr.offset += len\n" \
 | 
			
		||||
			"\t}\n")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	append(cg, "deserialize",
 | 
			
		||||
		"\t\t" f " = new Array(len)\n" \
 | 
			
		||||
		"\t}\n" \
 | 
			
		||||
		"\tfor (let i = 0; i < " f ".length; i++)\n" \
 | 
			
		||||
		indent(sprintf(deserialize, f "[i]")))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct_tag(d, cg) {
 | 
			
		||||
	append(cg, "fields", "\t" decapitalize(snaketocamel(d["name"])) "\n")
 | 
			
		||||
	# Do not deserialize here, that is already done by the containing union.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_struct(name, cg) {
 | 
			
		||||
	print ""
 | 
			
		||||
	print "export class " name " {"
 | 
			
		||||
	print cg["fields"] cg["methods"]
 | 
			
		||||
	print "\tstatic deserialize(r) {"
 | 
			
		||||
	print "\t\tconst s = new " name "()"
 | 
			
		||||
	print indent(cg["deserialize"]) "\t\treturn s"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "}"
 | 
			
		||||
 | 
			
		||||
	CodegenDeserialize[name] = "\t%s = " name ".deserialize(r)\n"
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union_tag(d, cg) {
 | 
			
		||||
	cg["tagtype"] = d["type"]
 | 
			
		||||
	cg["tagname"] = d["name"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union_struct(name, casename, cg, scg,     structname) {
 | 
			
		||||
	append(scg, "methods",
 | 
			
		||||
		"\n" \
 | 
			
		||||
		"\tconstructor() {\n" \
 | 
			
		||||
		"\t\tthis." decapitalize(snaketocamel(cg["tagname"])) \
 | 
			
		||||
			" = " cg["tagtype"] "." snaketocamel(casename) "\n" \
 | 
			
		||||
		"\t}\n")
 | 
			
		||||
 | 
			
		||||
	# And thus not all generated structs are present in Types.
 | 
			
		||||
	structname = name snaketocamel(casename)
 | 
			
		||||
	codegen_struct(structname, scg)
 | 
			
		||||
 | 
			
		||||
	append(cg, "deserialize",
 | 
			
		||||
		"\tcase " cg["tagtype"] "." snaketocamel(casename) ":\n" \
 | 
			
		||||
		"\t{\n" \
 | 
			
		||||
		indent(sprintf(CodegenDeserialize[structname], "const s")) \
 | 
			
		||||
		"\t\treturn s\n" \
 | 
			
		||||
		"\t}\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function codegen_union(name, cg,    tagvar) {
 | 
			
		||||
	tagvar = decapitalize(snaketocamel(cg["tagname"]))
 | 
			
		||||
 | 
			
		||||
	print ""
 | 
			
		||||
	print "export function deserialize" name "(r) {"
 | 
			
		||||
	print sprintf(CodegenDeserialize[cg["tagtype"]], "const " tagvar) \
 | 
			
		||||
		"\tswitch (" tagvar ") {"
 | 
			
		||||
	print cg["deserialize"] "\tdefault:"
 | 
			
		||||
	print "\t\tthrow `Unknown " cg["tagtype"] " (${tagvar})`"
 | 
			
		||||
	print "\t}"
 | 
			
		||||
	print "}"
 | 
			
		||||
 | 
			
		||||
	CodegenDeserialize[name] = "\t%s = deserialize" name "(r)\n"
 | 
			
		||||
	for (i in cg)
 | 
			
		||||
		delete cg[i]
 | 
			
		||||
}
 | 
			
		||||
@@ -1,291 +0,0 @@
 | 
			
		||||
# lxdrgen.awk: an XDR-derived code generator for network protocols.
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2022, 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 \
 | 
			
		||||
#  -v PrefixCamel=Foo foo.lxdr > foo.{c,go,mjs} | {clang-format,gofmt,...}
 | 
			
		||||
 | 
			
		||||
# --- Utilities ----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
function cameltosnake(s) {
 | 
			
		||||
	while (match(s, /[[:lower:]][[:upper:]]/)) {
 | 
			
		||||
		s = substr(s, 1, RSTART) "_" \
 | 
			
		||||
			tolower(substr(s, RSTART + 1, RLENGTH - 1)) \
 | 
			
		||||
			substr(s, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
	return tolower(s)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function snaketocamel(s) {
 | 
			
		||||
	s = toupper(substr(s, 1, 1)) tolower(substr(s, 2))
 | 
			
		||||
	while (match(s, /_[[:alnum:]]/)) {
 | 
			
		||||
		s = substr(s, 1, RSTART - 1) \
 | 
			
		||||
			toupper(substr(s, RSTART + 1, RLENGTH - 1)) \
 | 
			
		||||
			substr(s, RSTART + RLENGTH)
 | 
			
		||||
	}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function decapitalize(s) {
 | 
			
		||||
	if (match(s, /[[:upper:]][[:lower:]]/)) {
 | 
			
		||||
		return tolower(substr(s, 1, 1)) substr(s, 2)
 | 
			
		||||
	}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function indent(s) {
 | 
			
		||||
	if (!s)
 | 
			
		||||
		return s
 | 
			
		||||
 | 
			
		||||
	gsub(/\n/, "\n\t", s)
 | 
			
		||||
	sub(/\t*$/, "", s)
 | 
			
		||||
	return "\t" s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function append(a, key, value) {
 | 
			
		||||
	a[key] = a[key] value
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# --- Parsing ------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
function fatal(message) {
 | 
			
		||||
	print "// " FILENAME ":" FNR ": fatal error: " message
 | 
			
		||||
	print FILENAME ":" FNR ": fatal error: " message > "/dev/stderr"
 | 
			
		||||
	exit 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function skipcomment() {
 | 
			
		||||
	do {
 | 
			
		||||
		if (match($0, /[*]\//)) {
 | 
			
		||||
			$0 = substr($0, RSTART + RLENGTH)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	} while (getline > 0)
 | 
			
		||||
	fatal("unterminated block comment")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function nexttoken() {
 | 
			
		||||
	do {
 | 
			
		||||
		if (match($0, /^[[:space:]]+/)) {
 | 
			
		||||
			$0 = substr($0, RLENGTH + 1)
 | 
			
		||||
		} else if (match($0, /^\/\/.*/)) {
 | 
			
		||||
			$0 = ""
 | 
			
		||||
		} else if (match($0, /^\/[*]/)) {
 | 
			
		||||
			$0 = substr($0, RLENGTH + 1)
 | 
			
		||||
			skipcomment()
 | 
			
		||||
		} else if (match($0, /^[[:alpha:]][[:alnum:]_]*/)) {
 | 
			
		||||
			Token = substr($0, 1, RLENGTH)
 | 
			
		||||
			$0 = substr($0, RLENGTH + 1)
 | 
			
		||||
			return Token
 | 
			
		||||
		# AWK implementations rarely support non-decimal notations
 | 
			
		||||
		# in their implicit string-to-number conversions.
 | 
			
		||||
		} else if (match($0, /^(0|-?[1-9][0-9]*)/)) {
 | 
			
		||||
			Token = substr($0, 1, RLENGTH)
 | 
			
		||||
			$0 = substr($0, RLENGTH + 1)
 | 
			
		||||
			return Token
 | 
			
		||||
		} else if ($0) {
 | 
			
		||||
			Token = substr($0, 1, 1)
 | 
			
		||||
			$0 = substr($0, 2)
 | 
			
		||||
			return Token
 | 
			
		||||
		}
 | 
			
		||||
	} while ($0 || getline > 0)
 | 
			
		||||
	Token = ""
 | 
			
		||||
	return Token
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function expect(v) {
 | 
			
		||||
	if (!v)
 | 
			
		||||
		fatal("broken expectations at `" Token "' before `" $0 "'")
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function accept(what) {
 | 
			
		||||
	if (Token != what)
 | 
			
		||||
		return 0
 | 
			
		||||
	nexttoken()
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function identifier(    v) {
 | 
			
		||||
	if (Token !~ /^[[:alpha:]]/)
 | 
			
		||||
		return 0
 | 
			
		||||
	v = Token
 | 
			
		||||
	nexttoken()
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function number(    v) {
 | 
			
		||||
	if (Token !~ /^(0|-?[1-9])/)
 | 
			
		||||
		return 0
 | 
			
		||||
	v = Token
 | 
			
		||||
	nexttoken()
 | 
			
		||||
	return v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function readnumber(    ident) {
 | 
			
		||||
	ident = identifier()
 | 
			
		||||
	if (!ident)
 | 
			
		||||
		return expect(number())
 | 
			
		||||
	if (!(ident in Consts))
 | 
			
		||||
		fatal("unknown constant: " ident)
 | 
			
		||||
	return Consts[ident]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function defconst(    ident, num) {
 | 
			
		||||
	if (!accept("const"))
 | 
			
		||||
		return 0
 | 
			
		||||
 | 
			
		||||
	ident = expect(identifier())
 | 
			
		||||
	expect(accept("="))
 | 
			
		||||
	num = readnumber()
 | 
			
		||||
	if (ident in Consts)
 | 
			
		||||
		fatal("constant redefined: " ident)
 | 
			
		||||
 | 
			
		||||
	Consts[ident] = num
 | 
			
		||||
	codegen_constant(ident, num)
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function readtype(    ident) {
 | 
			
		||||
	ident = deftype()
 | 
			
		||||
	if (ident)
 | 
			
		||||
		return ident
 | 
			
		||||
 | 
			
		||||
	ident = identifier()
 | 
			
		||||
	if (!ident)
 | 
			
		||||
		return 0
 | 
			
		||||
 | 
			
		||||
	if (!(ident in Types))
 | 
			
		||||
		fatal("unknown type: " ident)
 | 
			
		||||
	return ident
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function defenum(    name, ident, value, cg) {
 | 
			
		||||
	delete cg[0]
 | 
			
		||||
 | 
			
		||||
	name = expect(identifier())
 | 
			
		||||
	expect(accept("{"))
 | 
			
		||||
	while (!accept("}")) {
 | 
			
		||||
		ident = expect(identifier())
 | 
			
		||||
		value = value + 1
 | 
			
		||||
		if (accept("="))
 | 
			
		||||
			value = readnumber() + 0
 | 
			
		||||
		if (!value)
 | 
			
		||||
			fatal("enumeration values cannot be zero")
 | 
			
		||||
		if (value < -128 || value > 127)
 | 
			
		||||
			fatal("enumeration value out of range")
 | 
			
		||||
		expect(accept(","))
 | 
			
		||||
		append(EnumValues, name, SUBSEP ident)
 | 
			
		||||
		if (EnumValues[name, ident]++)
 | 
			
		||||
			fatal("duplicate enum value: " ident)
 | 
			
		||||
		codegen_enum_value(name, ident, value, cg)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Types[name] = "enum"
 | 
			
		||||
	codegen_enum(name, cg)
 | 
			
		||||
	return name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function readfield(out,    nonvoid) {
 | 
			
		||||
	nonvoid = !accept("void")
 | 
			
		||||
	if (nonvoid) {
 | 
			
		||||
		out["type"] = expect(readtype())
 | 
			
		||||
		out["name"] = expect(identifier())
 | 
			
		||||
		# TODO: Consider supporting XDR's VLA length limits here.
 | 
			
		||||
		# TODO: Consider supporting XDR's fixed-length syntax for string limits.
 | 
			
		||||
		out["isarray"] = accept("<") && expect(accept(">"))
 | 
			
		||||
	}
 | 
			
		||||
	expect(accept(";"))
 | 
			
		||||
	return nonvoid
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function defstruct(    name, d, cg) {
 | 
			
		||||
	delete d[0]
 | 
			
		||||
	delete cg[0]
 | 
			
		||||
 | 
			
		||||
	name = expect(identifier())
 | 
			
		||||
	expect(accept("{"))
 | 
			
		||||
	while (!accept("}")) {
 | 
			
		||||
		if (readfield(d))
 | 
			
		||||
			codegen_struct_field(d, cg)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Types[name] = "struct"
 | 
			
		||||
	codegen_struct(name, cg)
 | 
			
		||||
	return name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
			
		||||
	delete cg[0]
 | 
			
		||||
	delete scg[0]
 | 
			
		||||
	delete d[0]
 | 
			
		||||
 | 
			
		||||
	name = expect(identifier())
 | 
			
		||||
	expect(accept("switch"))
 | 
			
		||||
	expect(accept("("))
 | 
			
		||||
	tag["type"] = tagtype = expect(readtype())
 | 
			
		||||
	tag["name"] = expect(identifier())
 | 
			
		||||
	expect(accept(")"))
 | 
			
		||||
 | 
			
		||||
	if (Types[tagtype] != "enum")
 | 
			
		||||
		fatal("not an enum type: " tagtype)
 | 
			
		||||
	codegen_union_tag(tag, cg)
 | 
			
		||||
 | 
			
		||||
	split(EnumValues[tagtype], a, SUBSEP)
 | 
			
		||||
	for (i in a)
 | 
			
		||||
		unseen[a[i]]++
 | 
			
		||||
 | 
			
		||||
	expect(accept("{"))
 | 
			
		||||
	while (!accept("}")) {
 | 
			
		||||
		if (accept("case")) {
 | 
			
		||||
			if (tagvalue)
 | 
			
		||||
				codegen_union_struct(name, tagvalue, cg, scg)
 | 
			
		||||
 | 
			
		||||
			tagvalue = expect(identifier())
 | 
			
		||||
			expect(accept(":"))
 | 
			
		||||
			if (!unseen[tagvalue]--)
 | 
			
		||||
				fatal("no such value or duplicate case: " tagtype "." tagvalue)
 | 
			
		||||
			codegen_struct_tag(tag, scg)
 | 
			
		||||
		} else if (tagvalue) {
 | 
			
		||||
			if (readfield(d))
 | 
			
		||||
				codegen_struct_field(d, scg)
 | 
			
		||||
		} else {
 | 
			
		||||
			fatal("union fields must fall under a case")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (tagvalue)
 | 
			
		||||
		codegen_union_struct(name, tagvalue, cg, scg)
 | 
			
		||||
 | 
			
		||||
	# What remains non-zero in unseen[2..] is simply not recognized/allowed.
 | 
			
		||||
	Types[name] = "union"
 | 
			
		||||
	codegen_union(name, cg)
 | 
			
		||||
	return name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function deftype() {
 | 
			
		||||
	if (accept("enum"))
 | 
			
		||||
		return defenum()
 | 
			
		||||
	if (accept("struct"))
 | 
			
		||||
		return defstruct()
 | 
			
		||||
	if (accept("union"))
 | 
			
		||||
		return defunion()
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
	if (PrefixCamel) {
 | 
			
		||||
		PrefixLower = tolower(cameltosnake(PrefixCamel)) "_"
 | 
			
		||||
		PrefixUpper = toupper(cameltosnake(PrefixCamel)) "_"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	# This is not in a BEGIN clause (even though it consumes all input),
 | 
			
		||||
	# so that the code generator can insert the first FILENAME.
 | 
			
		||||
	codegen_begin()
 | 
			
		||||
 | 
			
		||||
	nexttoken()
 | 
			
		||||
	while (Token != "") {
 | 
			
		||||
		expect(defconst() || deftype())
 | 
			
		||||
		expect(accept(";"))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user