Compare commits
	
		
			59 Commits
		
	
	
		
			d01a1ff034
			...
			master
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						7566f9af82
	
				 | 
					
					
						|||
| 
						
						
							
						
						7425355d01
	
				 | 
					
					
						|||
| 
						
						
							
						
						d8f785eae5
	
				 | 
					
					
						|||
| 
						
						
							
						
						31ae400852
	
				 | 
					
					
						|||
| 
						
						
							
						
						b69d3f8692
	
				 | 
					
					
						|||
| 
						
						
							
						
						9a26284a64
	
				 | 
					
					
						|||
| 
						
						
							
						
						0f20cce9c8
	
				 | 
					
					
						|||
| 
						
						
							
						
						017cb1d570
	
				 | 
					
					
						|||
| 
						
						
							
						
						1642d387f3
	
				 | 
					
					
						|||
| 
						
						
							
						
						af889b733e
	
				 | 
					
					
						|||
| 
						
						
							
						
						51231d84ba
	
				 | 
					
					
						|||
| 
						
						
							
						
						6c47e384f5
	
				 | 
					
					
						|||
| 
						
						
							
						
						914e743dc4
	
				 | 
					
					
						|||
| 
						
						
							
						
						37a8f16235
	
				 | 
					
					
						|||
| 
						
						
							
						
						9fe576ae9e
	
				 | 
					
					
						|||
| 
						
						
							
						
						5c02778ff8
	
				 | 
					
					
						|||
| 
						
						
							
						
						e40d56152d
	
				 | 
					
					
						|||
| 
						
						
							
						
						21379d4c02
	
				 | 
					
					
						|||
| 
						
						
							
						
						9268fb8eba
	
				 | 
					
					
						|||
| 
						
						
							
						
						b01df19b80
	
				 | 
					
					
						|||
| 
						
						
							
						
						09e635cf97
	
				 | 
					
					
						|||
| 
						
						
							
						
						7560e8700e
	
				 | 
					
					
						|||
| 
						
						
							
						
						1930f138d4
	
				 | 
					
					
						|||
| 
						
						
							
						
						32cbb15266
	
				 | 
					
					
						|||
| 
						
						
							
						
						149938cc44
	
				 | 
					
					
						|||
| 
						
						
							
						
						62f8a7d05f
	
				 | 
					
					
						|||
| 
						
						
							
						
						492815c8fc
	
				 | 
					
					
						|||
| 
						
						
							
						
						aacf1b1d47
	
				 | 
					
					
						|||
| 
						
						
							
						
						49d7cb12bb
	
				 | 
					
					
						|||
| 
						
						
							
						
						fdf845d0bd
	
				 | 
					
					
						|||
| 
						
						
							
						
						75fc6f1c37
	
				 | 
					
					
						|||
| 
						
						
							
						
						8a8437634a
	
				 | 
					
					
						|||
| 
						
						
							
						
						e78b410a6a
	
				 | 
					
					
						|||
| 
						
						
							
						
						bf44e827e8
	
				 | 
					
					
						|||
| 
						
						
							
						
						8386af0420
	
				 | 
					
					
						|||
| 
						
						
							
						
						f04cc2c61e
	
				 | 
					
					
						|||
| 
						
						
							
						
						969a4cfc3e
	
				 | 
					
					
						|||
| 
						
						
							
						
						ad5b2fb8cd
	
				 | 
					
					
						|||
| 
						
						
							
						
						2a1f17a8f7
	
				 | 
					
					
						|||
| 
						
						
							
						
						8d56fae41b
	
				 | 
					
					
						|||
| 
						
						
							
						
						0239a4242a
	
				 | 
					
					
						|||
| 
						
						
							
						
						1966b81b4d
	
				 | 
					
					
						|||
| 
						
						
							
						
						f8c6ac2ed1
	
				 | 
					
					
						|||
| 
						
						
							
						
						f32bcbd7f4
	
				 | 
					
					
						|||
| 
						
						
							
						
						c5424e6992
	
				 | 
					
					
						|||
| 
						
						
							
						
						cb9d162a26
	
				 | 
					
					
						|||
| 
						
						
							
						
						db6357db9a
	
				 | 
					
					
						|||
| 
						
						
							
						
						7a0cb13a1a
	
				 | 
					
					
						|||
| 
						
						
							
						
						b6c54073cd
	
				 | 
					
					
						|||
| 
						
						
							
						
						62166f9679
	
				 | 
					
					
						|||
| 
						
						
							
						
						2edc9c6fd1
	
				 | 
					
					
						|||
| 
						
						
							
						
						f78f8a70f1
	
				 | 
					
					
						|||
| 
						
						
							
						
						be9a3e693e
	
				 | 
					
					
						|||
| 
						
						
							
						
						53197b51e5
	
				 | 
					
					
						|||
| 
						
						
							
						
						8466d0d850
	
				 | 
					
					
						|||
| 
						
						
							
						
						4c2874649d
	
				 | 
					
					
						|||
| 
						
						
							
						
						717c301207
	
				 | 
					
					
						|||
| 
						
						
							
						
						091f92bab3
	
				 | 
					
					
						|||
| 
						
						
							
						
						556c25855e
	
				 | 
					
					
						
@@ -1,7 +1,8 @@
 | 
				
			|||||||
project (liberty C)
 | 
					cmake_minimum_required (VERSION 3.0...3.27)
 | 
				
			||||||
cmake_minimum_required (VERSION 2.8.12)
 | 
					project (liberty C CXX)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Moar warnings
 | 
					# Moar warnings
 | 
				
			||||||
 | 
					set (CMAKE_CXX_STANDARD 11)
 | 
				
			||||||
if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
 | 
					if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
 | 
				
			||||||
	# -Wunused-function is pretty annoying here, as everything is static
 | 
						# -Wunused-function is pretty annoying here, as everything is static
 | 
				
			||||||
	set (wdisabled "-Wno-unused-function")
 | 
						set (wdisabled "-Wno-unused-function")
 | 
				
			||||||
@@ -9,7 +10,7 @@ if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
 | 
				
			|||||||
endif ()
 | 
					endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Dependencies
 | 
					# Dependencies
 | 
				
			||||||
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
 | 
					set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
 | 
				
			||||||
include (AddThreads)
 | 
					include (AddThreads)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
find_package (PkgConfig REQUIRED)
 | 
					find_package (PkgConfig REQUIRED)
 | 
				
			||||||
@@ -29,14 +30,14 @@ link_directories (${libssl_LIBRARY_DIRS})
 | 
				
			|||||||
foreach (extra iconv rt)
 | 
					foreach (extra iconv rt)
 | 
				
			||||||
	find_library (extra_lib_${extra} ${extra})
 | 
						find_library (extra_lib_${extra} ${extra})
 | 
				
			||||||
	if (extra_lib_${extra})
 | 
						if (extra_lib_${extra})
 | 
				
			||||||
		list (APPEND common_libraries ${extra})
 | 
							list (APPEND common_libraries ${extra_lib_${extra}})
 | 
				
			||||||
	endif ()
 | 
						endif ()
 | 
				
			||||||
endforeach ()
 | 
					endforeach ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Build some unit tests
 | 
					# Build some unit tests
 | 
				
			||||||
include_directories (${PROJECT_SOURCE_DIR})
 | 
					include_directories ("${PROJECT_SOURCE_DIR}")
 | 
				
			||||||
enable_testing ()
 | 
					enable_testing ()
 | 
				
			||||||
set (tests liberty proto)
 | 
					set (tests liberty proto xdg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pkg_check_modules (libpulse libpulse)
 | 
					pkg_check_modules (libpulse libpulse)
 | 
				
			||||||
if (libpulse_FOUND)
 | 
					if (libpulse_FOUND)
 | 
				
			||||||
@@ -56,7 +57,7 @@ endforeach ()
 | 
				
			|||||||
# --- Tools --------------------------------------------------------------------
 | 
					# --- Tools --------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Test the AsciiDoc manual page generator for a successful parse
 | 
					# Test the AsciiDoc manual page generator for a successful parse
 | 
				
			||||||
set (ASCIIMAN ${PROJECT_SOURCE_DIR}/tools/asciiman.awk)
 | 
					set (ASCIIMAN "${PROJECT_SOURCE_DIR}/tools/asciiman.awk")
 | 
				
			||||||
add_custom_command (OUTPUT libertyxdr.7
 | 
					add_custom_command (OUTPUT libertyxdr.7
 | 
				
			||||||
	COMMAND env LC_ALL=C awk -f ${ASCIIMAN}
 | 
						COMMAND env LC_ALL=C awk -f ${ASCIIMAN}
 | 
				
			||||||
		"${PROJECT_SOURCE_DIR}/libertyxdr.adoc" > libertyxdr.7
 | 
							"${PROJECT_SOURCE_DIR}/libertyxdr.adoc" > libertyxdr.7
 | 
				
			||||||
@@ -64,36 +65,53 @@ add_custom_command (OUTPUT libertyxdr.7
 | 
				
			|||||||
	COMMENT "Generating man page for libertyxdr" VERBATIM)
 | 
						COMMENT "Generating man page for libertyxdr" VERBATIM)
 | 
				
			||||||
add_custom_target (docs ALL DEPENDS libertyxdr.7)
 | 
					add_custom_target (docs ALL DEPENDS libertyxdr.7)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Test the --help/--version to AsciiDoc convertor
 | 
				
			||||||
 | 
					add_test (test-help2adoc
 | 
				
			||||||
 | 
						env LC_ALL=C "${PROJECT_SOURCE_DIR}/tests/help2adoc.sh")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Test CMake script parsing
 | 
					# Test CMake script parsing
 | 
				
			||||||
add_test (test-cmake-parser
 | 
					add_test (test-cmake-parser
 | 
				
			||||||
	env LC_ALL=C awk -f ${PROJECT_SOURCE_DIR}/tools/cmake-parser.awk
 | 
						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})
 | 
						-f "${PROJECT_SOURCE_DIR}/tools/cmake-dump.awk" ${CMAKE_CURRENT_LIST_FILE})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Test protocol code generation
 | 
					# Test protocol code generation
 | 
				
			||||||
set (lxdrgen_outputs)
 | 
					set (lxdrgen_outputs)
 | 
				
			||||||
set (lxdrgen_base "${PROJECT_BINARY_DIR}/lxdrgen.lxdr")
 | 
					set (lxdrgen_base "${PROJECT_BINARY_DIR}/lxdrgen.lxdr")
 | 
				
			||||||
foreach (backend c go mjs)
 | 
					foreach (backend c cpp go mjs swift)
 | 
				
			||||||
	list (APPEND lxdrgen_outputs ${lxdrgen_base}.${backend})
 | 
						list (APPEND lxdrgen_outputs ${lxdrgen_base}.${backend})
 | 
				
			||||||
	add_custom_command (OUTPUT ${lxdrgen_base}.${backend}
 | 
						add_custom_command (OUTPUT ${lxdrgen_base}.${backend}
 | 
				
			||||||
		COMMAND env LC_ALL=C awk
 | 
							COMMAND env LC_ALL=C awk
 | 
				
			||||||
			-f ${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk
 | 
								-f "${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk"
 | 
				
			||||||
			-f ${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk
 | 
								-f "${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk"
 | 
				
			||||||
			-v PrefixCamel=ProtoGen
 | 
								-v PrefixCamel=ProtoGen
 | 
				
			||||||
			${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr
 | 
								"${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr"
 | 
				
			||||||
			> ${lxdrgen_base}.${backend}
 | 
								> ${lxdrgen_base}.${backend}
 | 
				
			||||||
		DEPENDS
 | 
							DEPENDS
 | 
				
			||||||
			${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk
 | 
								"${PROJECT_SOURCE_DIR}/tools/lxdrgen.awk"
 | 
				
			||||||
			${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk
 | 
								"${PROJECT_SOURCE_DIR}/tools/lxdrgen-${backend}.awk"
 | 
				
			||||||
			${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr
 | 
								"${PROJECT_SOURCE_DIR}/tests/lxdrgen.lxdr"
 | 
				
			||||||
		COMMENT "Generating test protocol code (${backend})" VERBATIM)
 | 
							COMMENT "Generating test protocol code (${backend})" VERBATIM)
 | 
				
			||||||
endforeach ()
 | 
					endforeach ()
 | 
				
			||||||
add_custom_target (test-lxdrgen-outputs ALL DEPENDS ${lxdrgen_outputs})
 | 
					add_custom_target (test-lxdrgen-outputs ALL DEPENDS ${lxdrgen_outputs})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set_source_files_properties (${lxdrgen_base}.c
 | 
					set_source_files_properties (${lxdrgen_base}.c
 | 
				
			||||||
	PROPERTIES HEADER_FILE_ONLY TRUE)
 | 
						PROPERTIES HEADER_FILE_ONLY TRUE)
 | 
				
			||||||
add_executable (test-lxdrgen tests/lxdrgen.c ${lxdrgen_base}.c)
 | 
					add_executable (test-lxdrgen-c tests/lxdrgen.c ${lxdrgen_base}.c)
 | 
				
			||||||
target_include_directories (test-lxdrgen PUBLIC ${PROJECT_BINARY_DIR})
 | 
					target_include_directories (test-lxdrgen-c PUBLIC ${PROJECT_BINARY_DIR})
 | 
				
			||||||
add_test (NAME test-lxdrgen-c COMMAND test-lxdrgen)
 | 
					add_test (NAME test-lxdrgen-c COMMAND test-lxdrgen-c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set_source_files_properties (${lxdrgen_base}.cpp
 | 
				
			||||||
 | 
						PROPERTIES HEADER_FILE_ONLY TRUE)
 | 
				
			||||||
 | 
					if (WIN32)
 | 
				
			||||||
 | 
						add_executable (test-lxdrgen-cpp tests/lxdrgen.cpp
 | 
				
			||||||
 | 
							${lxdrgen_base}.cpp tools/lxdrgen-cpp-win32.cpp)
 | 
				
			||||||
 | 
					else ()
 | 
				
			||||||
 | 
						add_executable (test-lxdrgen-cpp tests/lxdrgen.cpp
 | 
				
			||||||
 | 
							${lxdrgen_base}.cpp tools/lxdrgen-cpp-posix.cpp)
 | 
				
			||||||
 | 
					endif ()
 | 
				
			||||||
 | 
					target_link_libraries (test-lxdrgen-cpp ${common_libraries})
 | 
				
			||||||
 | 
					target_include_directories (test-lxdrgen-cpp PUBLIC ${PROJECT_BINARY_DIR})
 | 
				
			||||||
 | 
					add_test (NAME test-lxdrgen-cpp COMMAND test-lxdrgen-cpp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
find_program (GO_EXECUTABLE go)
 | 
					find_program (GO_EXECUTABLE go)
 | 
				
			||||||
if (GO_EXECUTABLE)
 | 
					if (GO_EXECUTABLE)
 | 
				
			||||||
@@ -108,3 +126,11 @@ if (NODE_EXECUTABLE)
 | 
				
			|||||||
else ()
 | 
					else ()
 | 
				
			||||||
	message (WARNING "Cannot test generated protocol code for Javascript")
 | 
						message (WARNING "Cannot test generated protocol code for Javascript")
 | 
				
			||||||
endif ()
 | 
					endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					find_program (SWIFTC_EXECUTABLE swiftc)
 | 
				
			||||||
 | 
					if (SWIFTC_EXECUTABLE)
 | 
				
			||||||
 | 
						add_test (test-lxdrgen-swift
 | 
				
			||||||
 | 
							${SWIFTC_EXECUTABLE} -typecheck ${lxdrgen_base}.swift)
 | 
				
			||||||
 | 
					else ()
 | 
				
			||||||
 | 
						message (WARNING "Cannot test generated protocol code for Swift")
 | 
				
			||||||
 | 
					endif ()
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							@@ -1,4 +1,4 @@
 | 
				
			|||||||
Copyright (c) 2014 - 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
					Copyright (c) 2014 - 2025, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Permission to use, copy, modify, and/or distribute this software for any
 | 
					Permission to use, copy, modify, and/or distribute this software for any
 | 
				
			||||||
purpose with or without fee is hereby granted.
 | 
					purpose with or without fee is hereby granted.
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								README.adoc
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								README.adoc
									
									
									
									
									
								
							@@ -15,8 +15,8 @@ mess that are header files.
 | 
				
			|||||||
The API is intentionally unstable, which allows for easy refactoring.
 | 
					The API is intentionally unstable, which allows for easy refactoring.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
All development is done on Linux, but other POSIX-compatible operating systems
 | 
					All development is done on Linux, but other POSIX-compatible operating systems
 | 
				
			||||||
should be generally supported as well.  They have an extremely low priority,
 | 
					should be generally supported as well.  They have a lower priority, however,
 | 
				
			||||||
however, and I'm not testing them at all, perhaps with the exception of macOS.
 | 
					and don't receive as much testing.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Tools
 | 
					Tools
 | 
				
			||||||
-----
 | 
					-----
 | 
				
			||||||
@@ -36,6 +36,10 @@ cmake-dump.awk::
 | 
				
			|||||||
	This can be used in conjunction with the previous script to dump CMake
 | 
						This can be used in conjunction with the previous script to dump CMake
 | 
				
			||||||
	scripts in a normalized format for further processing.
 | 
						scripts in a normalized format for further processing.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					help2adoc.awk::
 | 
				
			||||||
 | 
						Produces AsciiDoc manual pages from --version/--help output.
 | 
				
			||||||
 | 
						These can then be processed by _asciiman.awk_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
lxdrgen.awk::
 | 
					lxdrgen.awk::
 | 
				
			||||||
	Protocol code generator for a variant of XDR,
 | 
						Protocol code generator for a variant of XDR,
 | 
				
			||||||
	which is link:libertyxdr.adoc[documented separately].
 | 
						which is link:libertyxdr.adoc[documented separately].
 | 
				
			||||||
@@ -44,6 +48,13 @@ lxdrgen.awk::
 | 
				
			|||||||
lxdrgen-c.awk::
 | 
					lxdrgen-c.awk::
 | 
				
			||||||
	LibertyXDR backend that builds on top of the C pseudolibrary.
 | 
						LibertyXDR backend that builds on top of the C pseudolibrary.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					lxdrgen-cpp.awk::
 | 
				
			||||||
 | 
					lxdrgen-cpp-posix.cpp::
 | 
				
			||||||
 | 
					lxdrgen-cpp-qt.cpp::
 | 
				
			||||||
 | 
					lxdrgen-cpp-win32.cpp::
 | 
				
			||||||
 | 
						LibertyXDR backend for C++, primarily targeting Win32 and its wide strings.
 | 
				
			||||||
 | 
						Link the result together with one of the accompanied source files.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
lxdrgen-go.awk::
 | 
					lxdrgen-go.awk::
 | 
				
			||||||
	LibertyXDR backend for Go, supporting _encoding/json_ interfaces.  It also
 | 
						LibertyXDR backend for Go, supporting _encoding/json_ interfaces.  It also
 | 
				
			||||||
	produces optimized JSON marshallers (however, note that the _json.Marshaler_
 | 
						produces optimized JSON marshallers (however, note that the _json.Marshaler_
 | 
				
			||||||
@@ -54,6 +65,12 @@ lxdrgen-mjs.awk::
 | 
				
			|||||||
	LibertyXDR backend for Javascript, currently for decoding only.
 | 
						LibertyXDR backend for Javascript, currently for decoding only.
 | 
				
			||||||
	It cuts a corner by not using BigInts, on par with `JSON.parse()`.
 | 
						It cuts a corner by not using BigInts, on par with `JSON.parse()`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					lxdrgen-swift.awk::
 | 
				
			||||||
 | 
						LibertyXDR backend for the Swift programming language.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wdye::
 | 
				
			||||||
 | 
						Compiled Lua-based Expect-like utility, intended purely for build checks.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Contributing and Support
 | 
					Contributing and Support
 | 
				
			||||||
------------------------
 | 
					------------------------
 | 
				
			||||||
Use https://git.janouch.name/p/liberty to report any bugs, request features,
 | 
					Use https://git.janouch.name/p/liberty to report any bugs, request features,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,15 +9,15 @@ find_package (Threads)
 | 
				
			|||||||
function (add_threads target)
 | 
					function (add_threads target)
 | 
				
			||||||
	if (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
 | 
						if (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
 | 
				
			||||||
		message (FATAL_ERROR "pthreads not found")
 | 
							message (FATAL_ERROR "pthreads not found")
 | 
				
			||||||
	endif (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
 | 
						endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (THREADS_HAVE_PTHREAD_ARG)
 | 
						if (THREADS_HAVE_PTHREAD_ARG)
 | 
				
			||||||
		set_property (TARGET ${target} PROPERTY
 | 
							set_property (TARGET ${target} PROPERTY
 | 
				
			||||||
			COMPILE_OPTIONS "-pthread")
 | 
								COMPILE_OPTIONS "-pthread")
 | 
				
			||||||
		set_property (TARGET ${target} PROPERTY
 | 
							set_property (TARGET ${target} PROPERTY
 | 
				
			||||||
			INTERFACE_COMPILE_OPTIONS "-pthread")
 | 
								INTERFACE_COMPILE_OPTIONS "-pthread")
 | 
				
			||||||
	endif (THREADS_HAVE_PTHREAD_ARG)
 | 
						endif ()
 | 
				
			||||||
	if (CMAKE_THREAD_LIBS_INIT)
 | 
						if (CMAKE_THREAD_LIBS_INIT)
 | 
				
			||||||
		target_link_libraries (${target} "${CMAKE_THREAD_LIBS_INIT}")
 | 
							target_link_libraries (${target} "${CMAKE_THREAD_LIBS_INIT}")
 | 
				
			||||||
	endif (CMAKE_THREAD_LIBS_INIT)
 | 
						endif ()
 | 
				
			||||||
endfunction (add_threads)
 | 
					endfunction ()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,7 @@ pkg_check_modules (Ncursesw QUIET ncursesw)
 | 
				
			|||||||
set (required_vars Ncursesw_LIBRARIES)
 | 
					set (required_vars Ncursesw_LIBRARIES)
 | 
				
			||||||
if (NOT Ncursesw_FOUND)
 | 
					if (NOT Ncursesw_FOUND)
 | 
				
			||||||
	find_library (Ncursesw_LIBRARIES NAMES ncursesw)
 | 
						find_library (Ncursesw_LIBRARIES NAMES ncursesw)
 | 
				
			||||||
	find_path (Ncursesw_INCLUDE_DIRS ncurses.h)
 | 
						find_path (Ncursesw_INCLUDE_DIRS ncurses.h PATH_SUFFIXES ncurses)
 | 
				
			||||||
	list (APPEND required_vars Ncursesw_INCLUDE_DIRS)
 | 
						list (APPEND required_vars Ncursesw_INCLUDE_DIRS)
 | 
				
			||||||
endif (NOT Ncursesw_FOUND)
 | 
					endif (NOT Ncursesw_FOUND)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										84
									
								
								cmake/IconUtils.cmake
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								cmake/IconUtils.cmake
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
				
			|||||||
 | 
					# Public Domain
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function (icon_to_png name svg size output_dir output)
 | 
				
			||||||
 | 
						set (_dimensions "${size}x${size}")
 | 
				
			||||||
 | 
						set (_png_path "${output_dir}/hicolor/${_dimensions}/apps")
 | 
				
			||||||
 | 
						set (_png "${_png_path}/${name}.png")
 | 
				
			||||||
 | 
						set (${output} "${_png}" PARENT_SCOPE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						set (_find_program_REQUIRE)
 | 
				
			||||||
 | 
						if (NOT ${CMAKE_VERSION} VERSION_LESS 3.18.0)
 | 
				
			||||||
 | 
							set (_find_program_REQUIRE REQUIRED)
 | 
				
			||||||
 | 
						endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						find_program (rsvg_convert_EXECUTABLE rsvg-convert ${_find_program_REQUIRE})
 | 
				
			||||||
 | 
						add_custom_command (OUTPUT "${_png}"
 | 
				
			||||||
 | 
							COMMAND ${CMAKE_COMMAND} -E make_directory "${_png_path}"
 | 
				
			||||||
 | 
							COMMAND ${rsvg_convert_EXECUTABLE} "--output=${_png}"
 | 
				
			||||||
 | 
								"--width=${size}" "--height=${size}" -- "${svg}"
 | 
				
			||||||
 | 
							DEPENDS "${svg}"
 | 
				
			||||||
 | 
							COMMENT "Generating ${name} ${_dimensions} application icon" VERBATIM)
 | 
				
			||||||
 | 
					endfunction ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# You should include a 256x256 icon--which takes less space as raw PNG.
 | 
				
			||||||
 | 
					function (icon_for_win32 ico pngs pngs_raw)
 | 
				
			||||||
 | 
						set (_raws)
 | 
				
			||||||
 | 
						foreach (png ${pngs_raw})
 | 
				
			||||||
 | 
							list (APPEND _raws "--raw=${png}")
 | 
				
			||||||
 | 
						endforeach ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						set (_find_program_REQUIRE)
 | 
				
			||||||
 | 
						if (NOT ${CMAKE_VERSION} VERSION_LESS 3.18.0)
 | 
				
			||||||
 | 
							set (_find_program_REQUIRE REQUIRED)
 | 
				
			||||||
 | 
						endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						find_program (icotool_EXECUTABLE icotool ${_find_program_REQUIRE})
 | 
				
			||||||
 | 
						add_custom_command (OUTPUT "${ico}"
 | 
				
			||||||
 | 
							COMMAND ${icotool_EXECUTABLE} -c -o "${ico}" ${_raws} -- ${pngs}
 | 
				
			||||||
 | 
							DEPENDS ${pngs} ${pngs_raw}
 | 
				
			||||||
 | 
							COMMENT "Generating Windows program icon" VERBATIM)
 | 
				
			||||||
 | 
					endfunction ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function (icon_to_iconset_size name svg size iconset outputs)
 | 
				
			||||||
 | 
						math (EXPR _size2x "${size} * 2")
 | 
				
			||||||
 | 
						set (_dimensions "${size}x${size}")
 | 
				
			||||||
 | 
						set (_png1x "${iconset}/icon_${_dimensions}.png")
 | 
				
			||||||
 | 
						set (_png2x "${iconset}/icon_${_dimensions}@2x.png")
 | 
				
			||||||
 | 
						set (${outputs} "${_png1x};${_png2x}" PARENT_SCOPE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						set (_find_program_REQUIRE)
 | 
				
			||||||
 | 
						if (NOT ${CMAKE_VERSION} VERSION_LESS 3.18.0)
 | 
				
			||||||
 | 
							set (_find_program_REQUIRE REQUIRED)
 | 
				
			||||||
 | 
						endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						find_program (rsvg_convert_EXECUTABLE rsvg-convert ${_find_program_REQUIRE})
 | 
				
			||||||
 | 
						add_custom_command (OUTPUT "${_png1x}" "${_png2x}"
 | 
				
			||||||
 | 
							COMMAND ${CMAKE_COMMAND} -E make_directory "${iconset}"
 | 
				
			||||||
 | 
							COMMAND ${rsvg_convert_EXECUTABLE} "--output=${_png1x}"
 | 
				
			||||||
 | 
								"--width=${size}" "--height=${size}" -- "${svg}"
 | 
				
			||||||
 | 
							COMMAND ${rsvg_convert_EXECUTABLE} "--output=${_png2x}"
 | 
				
			||||||
 | 
								"--width=${_size2x}" "--height=${_size2x}" -- "${svg}"
 | 
				
			||||||
 | 
							DEPENDS "${svg}"
 | 
				
			||||||
 | 
							COMMENT "Generating ${name} ${_dimensions} icons" VERBATIM)
 | 
				
			||||||
 | 
					endfunction ()
 | 
				
			||||||
 | 
					function (icon_to_icns svg output_basename output)
 | 
				
			||||||
 | 
						get_filename_component (_name "${output_basename}" NAME_WE)
 | 
				
			||||||
 | 
						set (_iconset "${PROJECT_BINARY_DIR}/${_name}.iconset")
 | 
				
			||||||
 | 
						set (_icon "${PROJECT_BINARY_DIR}/${output_basename}")
 | 
				
			||||||
 | 
						set (${output} "${_icon}" PARENT_SCOPE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						set (_icon_png_list)
 | 
				
			||||||
 | 
						foreach (_icon_size 16 32 128 256 512)
 | 
				
			||||||
 | 
							icon_to_iconset_size ("${_name}" "${svg}"
 | 
				
			||||||
 | 
								"${_icon_size}" "${_iconset}" _icon_pngs)
 | 
				
			||||||
 | 
							list (APPEND _icon_png_list ${_icon_pngs})
 | 
				
			||||||
 | 
						endforeach ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# XXX: This will not normally work from within Nix.
 | 
				
			||||||
 | 
						add_custom_command (OUTPUT "${_icon}"
 | 
				
			||||||
 | 
							COMMAND iconutil -c icns -o "${_icon}" "${_iconset}"
 | 
				
			||||||
 | 
							DEPENDS ${_icon_png_list}
 | 
				
			||||||
 | 
							COMMENT "Generating ${_name} icon" VERBATIM)
 | 
				
			||||||
 | 
						set_source_files_properties ("${_icon}" PROPERTIES
 | 
				
			||||||
 | 
							MACOSX_PACKAGE_LOCATION Resources)
 | 
				
			||||||
 | 
					endfunction ()
 | 
				
			||||||
							
								
								
									
										15
									
								
								cmake/toolchains/MinGW-w64-x64.cmake
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								cmake/toolchains/MinGW-w64-x64.cmake
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					set (CMAKE_SYSTEM_NAME "Windows")
 | 
				
			||||||
 | 
					set (CMAKE_SYSTEM_PROCESSOR "x86_64")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_C_COMPILER "x86_64-w64-mingw32-gcc")
 | 
				
			||||||
 | 
					set (CMAKE_CXX_COMPILER "x86_64-w64-mingw32-g++")
 | 
				
			||||||
 | 
					set (CMAKE_RC_COMPILER "x86_64-w64-mingw32-windres")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Remember to set WINEPATH for library dependencies
 | 
				
			||||||
 | 
					set (CMAKE_CROSSCOMPILING_EMULATOR "wine64")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH "/usr/x86_64-w64-mingw32")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
 | 
				
			||||||
							
								
								
									
										15
									
								
								cmake/toolchains/MinGW-w64-x86.cmake
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								cmake/toolchains/MinGW-w64-x86.cmake
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					set (CMAKE_SYSTEM_NAME "Windows")
 | 
				
			||||||
 | 
					set (CMAKE_SYSTEM_PROCESSOR "x86")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_C_COMPILER "i686-w64-mingw32-gcc")
 | 
				
			||||||
 | 
					set (CMAKE_CXX_COMPILER "i686-w64-mingw32-g++")
 | 
				
			||||||
 | 
					set (CMAKE_RC_COMPILER "i686-w64-mingw32-windres")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Remember to set WINEPATH for library dependencies
 | 
				
			||||||
 | 
					set (CMAKE_CROSSCOMPILING_EMULATOR "wine")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
 | 
				
			||||||
 | 
					set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
 | 
				
			||||||
@@ -1381,7 +1381,7 @@ struct mpd_client
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Protocol:
 | 
						// Protocol:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bool got_hello;                     ///< Got the OK MPD hello message
 | 
						char *got_hello;                    ///< Version from OK MPD hello message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bool idling;                        ///< Sent idle as the last command
 | 
						bool idling;                        ///< Sent idle as the last command
 | 
				
			||||||
	unsigned idling_subsystems;         ///< Subsystems we're idling for
 | 
						unsigned idling_subsystems;         ///< Subsystems we're idling for
 | 
				
			||||||
@@ -1482,7 +1482,7 @@ mpd_client_reset (struct mpd_client *self)
 | 
				
			|||||||
	str_reset (&self->read_buffer);
 | 
						str_reset (&self->read_buffer);
 | 
				
			||||||
	str_reset (&self->write_buffer);
 | 
						str_reset (&self->write_buffer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	self->got_hello = false;
 | 
						cstr_set (&self->got_hello, NULL);
 | 
				
			||||||
	self->idling = false;
 | 
						self->idling = false;
 | 
				
			||||||
	self->idling_subsystems = 0;
 | 
						self->idling_subsystems = 0;
 | 
				
			||||||
	self->in_list = false;
 | 
						self->in_list = false;
 | 
				
			||||||
@@ -1549,7 +1549,8 @@ mpd_client_parse_hello (struct mpd_client *self, const char *line)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// TODO: call "on_connected" now.  We should however also set up a timer
 | 
						// TODO: call "on_connected" now.  We should however also set up a timer
 | 
				
			||||||
	//   so that we don't wait on this message forever.
 | 
						//   so that we don't wait on this message forever.
 | 
				
			||||||
	return self->got_hello = true;
 | 
						cstr_set (&self->got_hello, xstrdup (line + sizeof hello - 1));
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static bool
 | 
					static bool
 | 
				
			||||||
@@ -1593,6 +1594,8 @@ mpd_client_parse_kv (char *line, char **value)
 | 
				
			|||||||
static void
 | 
					static void
 | 
				
			||||||
mpd_client_update_poller (struct mpd_client *self)
 | 
					mpd_client_update_poller (struct mpd_client *self)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						if (self->state != MPD_CONNECTED)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
	poller_fd_set (&self->socket_event,
 | 
						poller_fd_set (&self->socket_event,
 | 
				
			||||||
		self->write_buffer.len ? (POLLIN | POLLOUT) : POLLIN);
 | 
							self->write_buffer.len ? (POLLIN | POLLOUT) : POLLIN);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1634,30 +1637,30 @@ mpd_client_on_ready (const struct pollfd *pfd, void *user_data)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
					// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static bool
 | 
					 | 
				
			||||||
mpd_client_must_quote_char (char c)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	return (unsigned char) c <= ' ' || c == '"' || c == '\'';
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static bool
 | 
					static bool
 | 
				
			||||||
mpd_client_must_quote (const char *s)
 | 
					mpd_client_must_quote (const char *s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (!*s)
 | 
						if (!*s)
 | 
				
			||||||
		return true;
 | 
							return true;
 | 
				
			||||||
	for (; *s; s++)
 | 
						for (; *s; s++)
 | 
				
			||||||
		if (mpd_client_must_quote_char (*s))
 | 
							if ((unsigned char) *s <= ' ' || *s == '"' || *s == '\'')
 | 
				
			||||||
			return true;
 | 
								return true;
 | 
				
			||||||
	return false;
 | 
						return false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static bool
 | 
				
			||||||
 | 
					mpd_client_must_escape_in_quote (char c)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return c == '"' || c == '\'' || c == '\\';
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
mpd_client_quote (const char *s, struct str *output)
 | 
					mpd_client_quote (const char *s, struct str *output)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	str_append_c (output, '"');
 | 
						str_append_c (output, '"');
 | 
				
			||||||
	for (; *s; s++)
 | 
						for (; *s; s++)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if (mpd_client_must_quote_char (*s))
 | 
							if (mpd_client_must_escape_in_quote (*s))
 | 
				
			||||||
			str_append_c (output, '\\');
 | 
								str_append_c (output, '\\');
 | 
				
			||||||
		str_append_c (output, *s);
 | 
							str_append_c (output, *s);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -1911,14 +1914,14 @@ mpd_client_connect_unix (struct mpd_client *self, const char *address,
 | 
				
			|||||||
	// Expand tilde if needed
 | 
						// Expand tilde if needed
 | 
				
			||||||
	char *expanded = resolve_filename (address, xstrdup);
 | 
						char *expanded = resolve_filename (address, xstrdup);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct sockaddr_un sun;
 | 
						struct sockaddr_un sau;
 | 
				
			||||||
	sun.sun_family = AF_UNIX;
 | 
						sau.sun_family = AF_UNIX;
 | 
				
			||||||
	strncpy (sun.sun_path, expanded, sizeof sun.sun_path);
 | 
						strncpy (sau.sun_path, expanded, sizeof sau.sun_path);
 | 
				
			||||||
	sun.sun_path[sizeof sun.sun_path - 1] = 0;
 | 
						sau.sun_path[sizeof sau.sun_path - 1] = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	free (expanded);
 | 
						free (expanded);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (connect (fd, (struct sockaddr *) &sun, sizeof sun))
 | 
						if (connect (fd, (struct sockaddr *) &sau, sizeof sau))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		error_set (e, "%s: %s", "connect", strerror (errno));
 | 
							error_set (e, "%s: %s", "connect", strerror (errno));
 | 
				
			||||||
		xclose (fd);
 | 
							xclose (fd);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										778
									
								
								liberty-xdg.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										778
									
								
								liberty-xdg.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,778 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * liberty-xdg.c: the ultimate C unlibrary: freedesktop.org specifications
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (c) 2023 - 2024, 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.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// This files assumes you've already included liberty.c.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef LIBERTY_XDG_WANT_X11
 | 
				
			||||||
 | 
					#include <X11/Xatom.h>
 | 
				
			||||||
 | 
					#include <X11/Xlib.h>
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef LIBERTY_XDG_WANT_ICONS
 | 
				
			||||||
 | 
					#include <png.h>
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// --- XSettings ---------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef LIBERTY_XDG_WANT_X11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct xdg_xsettings_setting
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						enum xdg_xsettings_type
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							XDG_XSETTINGS_INTEGER,
 | 
				
			||||||
 | 
							XDG_XSETTINGS_STRING,
 | 
				
			||||||
 | 
							XDG_XSETTINGS_COLOR,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						type;                               ///< What's stored in the union
 | 
				
			||||||
 | 
						uint32_t serial;                    ///< Serial of the last change
 | 
				
			||||||
 | 
						union
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							int32_t integer;
 | 
				
			||||||
 | 
							struct str string;
 | 
				
			||||||
 | 
							struct { uint16_t red, green, blue, alpha; } color;
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					xdg_xsettings_setting_destroy (struct xdg_xsettings_setting *self)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (self->type == XDG_XSETTINGS_STRING)
 | 
				
			||||||
 | 
							str_free (&self->string);
 | 
				
			||||||
 | 
						free (self);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct xdg_xsettings
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct str_map settings;            ///< Name -> xdg_xsettings_setting
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					xdg_xsettings_free (struct xdg_xsettings *self)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						str_map_free (&self->settings);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct xdg_xsettings
 | 
				
			||||||
 | 
					xdg_xsettings_make (void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (struct xdg_xsettings)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							.settings =
 | 
				
			||||||
 | 
								str_map_make ((str_map_free_fn) xdg_xsettings_setting_destroy),
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					xdg_xsettings_update (struct xdg_xsettings *self, Display *dpy)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						// TODO: We're supposed to lock the server.
 | 
				
			||||||
 | 
						// TODO: We're supposed to trap X errors.
 | 
				
			||||||
 | 
						char *selection = xstrdup_printf ("_XSETTINGS_S%d", DefaultScreen (dpy));
 | 
				
			||||||
 | 
						Atom selection_atom = XInternAtom (dpy, selection, True);
 | 
				
			||||||
 | 
						free (selection);
 | 
				
			||||||
 | 
						if (!selection_atom)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Window owner = XGetSelectionOwner (dpy, selection_atom);
 | 
				
			||||||
 | 
						if (!owner)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Atom xsettings_atom = XInternAtom (dpy, "_XSETTINGS_SETTINGS", True);
 | 
				
			||||||
 | 
						if (!xsettings_atom)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Atom actual_type = None;
 | 
				
			||||||
 | 
						int actual_format = 0;
 | 
				
			||||||
 | 
						unsigned long nitems = 0, bytes_after = 0;
 | 
				
			||||||
 | 
						unsigned char *buffer = NULL;
 | 
				
			||||||
 | 
						int status = XGetWindowProperty (dpy,
 | 
				
			||||||
 | 
							owner,
 | 
				
			||||||
 | 
							xsettings_atom,
 | 
				
			||||||
 | 
							0L,
 | 
				
			||||||
 | 
							LONG_MAX,
 | 
				
			||||||
 | 
							False,
 | 
				
			||||||
 | 
							xsettings_atom,
 | 
				
			||||||
 | 
							&actual_type,
 | 
				
			||||||
 | 
							&actual_format,
 | 
				
			||||||
 | 
							&nitems,
 | 
				
			||||||
 | 
							&bytes_after,
 | 
				
			||||||
 | 
							&buffer);
 | 
				
			||||||
 | 
						if (status != Success || !buffer)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (actual_type != xsettings_atom
 | 
				
			||||||
 | 
						 || actual_format != 8
 | 
				
			||||||
 | 
						 || nitems < 12)
 | 
				
			||||||
 | 
							goto fail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const struct peeker *peeker = NULL;
 | 
				
			||||||
 | 
						if (buffer[0] == LSBFirst)
 | 
				
			||||||
 | 
							peeker = &peeker_le;
 | 
				
			||||||
 | 
						else if (buffer[0] == MSBFirst)
 | 
				
			||||||
 | 
							peeker = &peeker_be;
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							goto fail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// We're ignoring the serial for now.
 | 
				
			||||||
 | 
						uint32_t n_settings = peeker->u32 (buffer + 8);
 | 
				
			||||||
 | 
						size_t offset = 12;
 | 
				
			||||||
 | 
						struct str name = str_make ();
 | 
				
			||||||
 | 
						struct xdg_xsettings_setting *setting = xcalloc (1, sizeof *setting);
 | 
				
			||||||
 | 
						while (n_settings--)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if (nitems < offset + 4)
 | 
				
			||||||
 | 
								goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							setting->type = buffer[offset];
 | 
				
			||||||
 | 
							uint16_t name_len = peeker->u16 (buffer + offset + 2);
 | 
				
			||||||
 | 
							offset += 4;
 | 
				
			||||||
 | 
							if (nitems < offset + name_len)
 | 
				
			||||||
 | 
								goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							str_append_data (&name, buffer + offset, name_len);
 | 
				
			||||||
 | 
							offset += ((name_len + 3) & ~3);
 | 
				
			||||||
 | 
							if (nitems < offset + 4)
 | 
				
			||||||
 | 
								goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							setting->serial = peeker->u32 (buffer + offset);
 | 
				
			||||||
 | 
							offset += 4;
 | 
				
			||||||
 | 
							switch (setting->type)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
							case XDG_XSETTINGS_INTEGER:
 | 
				
			||||||
 | 
								if (nitems < offset + 4)
 | 
				
			||||||
 | 
									goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								setting->integer = (int32_t) peeker->u32 (buffer + offset);
 | 
				
			||||||
 | 
								offset += 4;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							case XDG_XSETTINGS_STRING:
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								if (nitems < offset + 4)
 | 
				
			||||||
 | 
									goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								uint32_t value_len = peeker->u32 (buffer + offset);
 | 
				
			||||||
 | 
								offset += 4;
 | 
				
			||||||
 | 
								if (nitems < offset + value_len)
 | 
				
			||||||
 | 
									goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								setting->string = str_make ();
 | 
				
			||||||
 | 
								str_append_data (&setting->string, buffer + offset, value_len);
 | 
				
			||||||
 | 
								offset += ((value_len + 3) & ~3);
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							case XDG_XSETTINGS_COLOR:
 | 
				
			||||||
 | 
								if (nitems < offset + 8)
 | 
				
			||||||
 | 
									goto fail_item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								setting->color.red   = peeker->u16 (buffer + offset);
 | 
				
			||||||
 | 
								setting->color.green = peeker->u16 (buffer + offset + 2);
 | 
				
			||||||
 | 
								setting->color.blue  = peeker->u16 (buffer + offset + 4);
 | 
				
			||||||
 | 
								setting->color.alpha = peeker->u16 (buffer + offset + 6);
 | 
				
			||||||
 | 
								offset += 8;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								goto fail_item;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// TODO(p): Change detection, by comparing existence and serials.
 | 
				
			||||||
 | 
							str_map_set (&self->settings, name.str, setting);
 | 
				
			||||||
 | 
							setting = xcalloc (1, sizeof *setting);
 | 
				
			||||||
 | 
							str_reset (&name);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					fail_item:
 | 
				
			||||||
 | 
						xdg_xsettings_setting_destroy (setting);
 | 
				
			||||||
 | 
						str_free (&name);
 | 
				
			||||||
 | 
					fail:
 | 
				
			||||||
 | 
						XFree (buffer);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // LIBERTY_XDG_WANT_X11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// --- Desktop file parser -----------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Useful for parsing desktop-entry-spec, icon-theme-spec, trash-spec,
 | 
				
			||||||
 | 
					// mime-apps-spec.  This code is not designed for making changes to the files.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct desktop_file
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct str_map groups;              ///< Group name → Key → Value
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					desktop_file_free_group (void *value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						str_map_free (value);
 | 
				
			||||||
 | 
						free (value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					desktop_file_free (struct desktop_file *self)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						str_map_free (&self->groups);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					desktop_file_parse_line (struct desktop_file *self,
 | 
				
			||||||
 | 
						char **group_name, const char *line, const char *end)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct str_map *group = NULL;
 | 
				
			||||||
 | 
						if (*group_name)
 | 
				
			||||||
 | 
							group = str_map_find (&self->groups, *group_name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (*line == '[')
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							bool ok = *--end == ']';
 | 
				
			||||||
 | 
							for (const char *p = ++line; ok && p != end; p++)
 | 
				
			||||||
 | 
								ok = (unsigned char) *p >= 32 && (unsigned char) *p <= 127
 | 
				
			||||||
 | 
									&& *p != '[' && *p != ']';
 | 
				
			||||||
 | 
							if (!ok)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								cstr_set (group_name, NULL);
 | 
				
			||||||
 | 
								print_debug ("invalid desktop file group header");
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							cstr_set (group_name, xstrndup (line, end - line));
 | 
				
			||||||
 | 
							if (str_map_find (&self->groups, *group_name))
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								print_debug ("duplicate desktop file group: %s", *group_name);
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							group = xcalloc (1, sizeof *group);
 | 
				
			||||||
 | 
							*group = str_map_make (free);
 | 
				
			||||||
 | 
							str_map_set (&self->groups, *group_name, group);
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (!group)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							print_debug ("unexpected desktop file entry outside of a group");
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const char *key_end = line;
 | 
				
			||||||
 | 
						while (key_end != end && (isalnum_ascii (*key_end) || *key_end == '-'))
 | 
				
			||||||
 | 
							key_end++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// We could validate these further, but we just search in them anyway.
 | 
				
			||||||
 | 
						if (key_end != end && *key_end == '[')
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							while (++key_end != end && *key_end != ']')
 | 
				
			||||||
 | 
								;
 | 
				
			||||||
 | 
							if (key_end != end && *key_end == ']')
 | 
				
			||||||
 | 
								key_end++;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const char *value = key_end;
 | 
				
			||||||
 | 
						while (value != end && *value == ' ')
 | 
				
			||||||
 | 
							value++;
 | 
				
			||||||
 | 
						if (value == end || *value++ != '=')
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							print_debug ("invalid desktop file entry");
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						while (value != end && *value == ' ')
 | 
				
			||||||
 | 
							value++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *key = xstrndup (line, key_end - line);
 | 
				
			||||||
 | 
						if (str_map_find (group, key))
 | 
				
			||||||
 | 
							print_debug ("duplicate desktop file entry for: %s", key);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							str_map_set (group, key, xstrndup (value, end - value));
 | 
				
			||||||
 | 
						free (key);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct desktop_file
 | 
				
			||||||
 | 
					desktop_file_make (const char *data, size_t len)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct desktop_file self = (struct desktop_file)
 | 
				
			||||||
 | 
							{ .groups = str_map_make (desktop_file_free_group) };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *group_name = NULL;
 | 
				
			||||||
 | 
						const char *p = data, *data_end = p + len;
 | 
				
			||||||
 | 
						while (p != data_end)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							const char *line = p, *line_end = line;
 | 
				
			||||||
 | 
							while (line_end != data_end && *line_end != '\n')
 | 
				
			||||||
 | 
								line_end++;
 | 
				
			||||||
 | 
							if ((p = line_end) != data_end && *p == '\n')
 | 
				
			||||||
 | 
								p++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (line != line_end && *line != '#')
 | 
				
			||||||
 | 
								desktop_file_parse_line (&self, &group_name, line, line_end);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						free (group_name);
 | 
				
			||||||
 | 
						return self;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const char *
 | 
				
			||||||
 | 
					desktop_file_get (struct desktop_file *self, const char *group, const char *key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						// TODO(p): Ideally, also implement localised keys.
 | 
				
			||||||
 | 
						struct str_map *group_map = str_map_find (&self->groups, group);
 | 
				
			||||||
 | 
						if (!group_map)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return str_map_find (group_map, key);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct strv
 | 
				
			||||||
 | 
					desktop_file_unescape (const char *value, bool is_list)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct strv result = strv_make ();
 | 
				
			||||||
 | 
						struct str s = str_make ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// XXX: The unescaping behaviour is underspecified.
 | 
				
			||||||
 | 
						//   It might make sense to warn about unrecognised escape sequences.
 | 
				
			||||||
 | 
						bool escape = false;
 | 
				
			||||||
 | 
						for (const char *p = value; *p; p++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if (escape)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								switch (*p)
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
								break; case 's': str_append_c (&s, ' ');
 | 
				
			||||||
 | 
								break; case 'n': str_append_c (&s, '\n');
 | 
				
			||||||
 | 
								break; case 't': str_append_c (&s, '\t');
 | 
				
			||||||
 | 
								break; case 'r': str_append_c (&s, '\r');
 | 
				
			||||||
 | 
								break; default:  str_append_c (&s, *p);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								escape = false;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else if (*p == '\\' && p[1])
 | 
				
			||||||
 | 
								escape = true;
 | 
				
			||||||
 | 
							else if (*p == ';' && is_list)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								strv_append_owned (&result, str_steal (&s));
 | 
				
			||||||
 | 
								s = str_make ();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								str_append_c (&s, *p);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!is_list || s.len != 0)
 | 
				
			||||||
 | 
							strv_append_owned (&result, str_steal (&s));
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							str_free (&s);
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static char *
 | 
				
			||||||
 | 
					desktop_file_get_string (struct desktop_file *self,
 | 
				
			||||||
 | 
						const char *group, const char *key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *value = desktop_file_get (self, group, key);
 | 
				
			||||||
 | 
						if (!value)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						struct strv values = desktop_file_unescape (value, false /* is_list */);
 | 
				
			||||||
 | 
						char *unescaped = strv_steal (&values, 0);
 | 
				
			||||||
 | 
						strv_free (&values);
 | 
				
			||||||
 | 
						return unescaped;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct strv
 | 
				
			||||||
 | 
					desktop_file_get_stringv (struct desktop_file *self,
 | 
				
			||||||
 | 
						const char *group, const char *key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *value = desktop_file_get (self, group, key);
 | 
				
			||||||
 | 
						if (!value)
 | 
				
			||||||
 | 
							return strv_make ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return desktop_file_unescape (value, true /* is_list */);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static bool
 | 
				
			||||||
 | 
					desktop_file_get_bool (struct desktop_file *self,
 | 
				
			||||||
 | 
						const char *group, const char *key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *value = desktop_file_get (self, group, key);
 | 
				
			||||||
 | 
						if (!value)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Let's be compatible with pre-1.0 files when it costs us so little.
 | 
				
			||||||
 | 
						if (!strcmp (value, "true")
 | 
				
			||||||
 | 
						 || !strcmp (value, "1"))
 | 
				
			||||||
 | 
							return true;
 | 
				
			||||||
 | 
						if (!strcmp (value, "false")
 | 
				
			||||||
 | 
						 || !strcmp (value, "0"))
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						print_debug ("invalid desktop file boolean for '%s': %s", key, value);
 | 
				
			||||||
 | 
						return false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Nothing uses the "numeric" type.
 | 
				
			||||||
 | 
					// "icon-theme-spec" uses "integer" and doesn't say what it is.
 | 
				
			||||||
 | 
					static long
 | 
				
			||||||
 | 
					desktop_file_get_integer (struct desktop_file *self,
 | 
				
			||||||
 | 
						const char *group, const char *key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *value = desktop_file_get (self, group, key);
 | 
				
			||||||
 | 
						if (!value)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *end = NULL;
 | 
				
			||||||
 | 
						long parsed = (errno = 0, strtol (value, &end, 10));
 | 
				
			||||||
 | 
						if (errno != 0 || *end)
 | 
				
			||||||
 | 
							print_debug ("invalid desktop file integer for '%s': %s", key, value);
 | 
				
			||||||
 | 
						return parsed;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// --- Icon themes -------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// This implements part of the Icon Theme Specification.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef LIBERTY_XDG_WANT_ICONS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct icon_theme_icon
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						uint32_t width;                     ///< Width of argb in pixels
 | 
				
			||||||
 | 
						uint32_t height;                    ///< Height of argb in pixels
 | 
				
			||||||
 | 
						uint32_t argb[];                    ///< ARGB32 data, unassociated alpha
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					icon_theme_open_on_error (png_structp pngp, const char *error)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						print_debug ("%s: %s", (const char *) png_get_error_ptr (pngp), error);
 | 
				
			||||||
 | 
						png_longjmp (pngp, 1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					icon_theme_open_on_warning (png_structp pngp, const char *warning)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						(void) pngp;
 | 
				
			||||||
 | 
						(void) warning;
 | 
				
			||||||
 | 
						// Fuck your "gamma value does not match libpng estimate".
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// For simplicity, only support PNG icons, using the most popular library.
 | 
				
			||||||
 | 
					static struct icon_theme_icon *
 | 
				
			||||||
 | 
					icon_theme_open (const char *path)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						volatile png_bytep buffer = NULL;
 | 
				
			||||||
 | 
						volatile png_bytepp row_pointers = NULL;
 | 
				
			||||||
 | 
						struct icon_theme_icon *volatile result = NULL;
 | 
				
			||||||
 | 
						FILE *fp = fopen (path, "rb");
 | 
				
			||||||
 | 
						if (!fp)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if (errno != ENOENT)
 | 
				
			||||||
 | 
								print_debug ("%s: %s", path, strerror (errno));
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// The simplified and high-level APIs aren't powerful enough.
 | 
				
			||||||
 | 
						png_structp pngp = png_create_read_struct (PNG_LIBPNG_VER_STRING,
 | 
				
			||||||
 | 
							(png_voidp) path, icon_theme_open_on_error, icon_theme_open_on_warning);
 | 
				
			||||||
 | 
						png_infop infop = png_create_info_struct (pngp);
 | 
				
			||||||
 | 
						if (!infop)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							print_debug ("%s: %s", path, strerror (errno));
 | 
				
			||||||
 | 
							goto fail;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (setjmp (png_jmpbuf (pngp)))
 | 
				
			||||||
 | 
							goto fail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						png_init_io (pngp, fp);
 | 
				
			||||||
 | 
						png_read_info (pngp, infop);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Asking for at least 8-bit channels.  This call is a superset of:
 | 
				
			||||||
 | 
						//  - png_set_palette_to_rgb(),
 | 
				
			||||||
 | 
						//  - png_set_tRNS_to_alpha(),
 | 
				
			||||||
 | 
						//  - png_set_expand_gray_1_2_4_to_8().
 | 
				
			||||||
 | 
						png_set_expand (pngp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Reduce the possibilities further to RGB or RGBA...
 | 
				
			||||||
 | 
						png_set_gray_to_rgb (pngp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// ...and /exactly/ 8-bit channels.
 | 
				
			||||||
 | 
						// Alternatively, use png_set_expand_16() above to obtain 16-bit channels.
 | 
				
			||||||
 | 
						png_set_scale_16 (pngp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// PNG uses RGBA order, let's change that to ARGB (both in memory order).
 | 
				
			||||||
 | 
						// This doesn't change a row's `color_type` in png_do_read_filler(),
 | 
				
			||||||
 | 
						// and the following transformation thus ignores it.
 | 
				
			||||||
 | 
						png_set_add_alpha (pngp, 0xFFFF, PNG_FILLER_BEFORE);
 | 
				
			||||||
 | 
						png_set_swap_alpha (pngp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						(void) png_set_interlace_handling (pngp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						png_read_update_info (pngp, infop);
 | 
				
			||||||
 | 
						if (png_get_bit_depth (pngp, infop) != 8
 | 
				
			||||||
 | 
						 || png_get_channels (pngp, infop) != 4
 | 
				
			||||||
 | 
						 || png_get_color_type (pngp, infop) != PNG_COLOR_TYPE_RGB_ALPHA)
 | 
				
			||||||
 | 
							png_error (pngp, "result not A8R8G8B8");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						size_t row_bytes = png_get_rowbytes (pngp, infop);
 | 
				
			||||||
 | 
						size_t height = png_get_image_height (pngp, infop);
 | 
				
			||||||
 | 
						buffer = xcalloc (row_bytes, height);
 | 
				
			||||||
 | 
						row_pointers = xcalloc (height, sizeof buffer);
 | 
				
			||||||
 | 
						for (size_t y = 0; y < height; y++)
 | 
				
			||||||
 | 
							row_pointers[y] = buffer + y * row_bytes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						png_read_image (pngp, row_pointers);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result = xcalloc (1, sizeof *result + row_bytes * height);
 | 
				
			||||||
 | 
						result->width = png_get_image_width (pngp, infop);
 | 
				
			||||||
 | 
						result->height = height;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						uint32_t *dst = (uint32_t *) result->argb, *src = (uint32_t *) buffer;
 | 
				
			||||||
 | 
						for (size_t pixels = result->width * result->height; pixels--; )
 | 
				
			||||||
 | 
							*dst++ = ntohl (*src++);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fail:
 | 
				
			||||||
 | 
						free (buffer);
 | 
				
			||||||
 | 
						free (row_pointers);
 | 
				
			||||||
 | 
						png_destroy_read_struct (&pngp, &infop, NULL);
 | 
				
			||||||
 | 
						fclose (fp);
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct icon_theme_find_context
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct strv base;                   ///< Base directories
 | 
				
			||||||
 | 
						struct str_map visited;             ///< Cycle prevention
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ARRAY (struct icon_theme_icon *, icons)
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					icon_theme_find__fallback (struct icon_theme_find_context *ctx,
 | 
				
			||||||
 | 
						const char *name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						for (size_t i = 0; i < ctx->base.len; i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							char *path = xstrdup_printf ("%s/%s.png", ctx->base.vector[i], name);
 | 
				
			||||||
 | 
							struct icon_theme_icon *icon = icon_theme_open (path);
 | 
				
			||||||
 | 
							free (path);
 | 
				
			||||||
 | 
							if (icon)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								ARRAY_RESERVE (ctx->icons, 1);
 | 
				
			||||||
 | 
								ctx->icons[ctx->icons_len++] = icon;
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct desktop_file
 | 
				
			||||||
 | 
					icon_theme_find__index (struct icon_theme_find_context *ctx, const char *theme)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct str data = str_make ();
 | 
				
			||||||
 | 
						for (size_t i = 0; i < ctx->base.len; i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							struct error *e = NULL;
 | 
				
			||||||
 | 
							char *path = xstrdup_printf ("%s/%s/index.theme",
 | 
				
			||||||
 | 
								ctx->base.vector[i], theme);
 | 
				
			||||||
 | 
							read_file (path, &data, &e);
 | 
				
			||||||
 | 
							free (path);
 | 
				
			||||||
 | 
							if (!e)
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (errno != ENOENT)
 | 
				
			||||||
 | 
								print_debug ("%s", e->message);
 | 
				
			||||||
 | 
							error_free (e);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						struct desktop_file index = desktop_file_make (data.str, data.len);
 | 
				
			||||||
 | 
						str_free (&data);
 | 
				
			||||||
 | 
						return index;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					icon_theme_find__named (struct icon_theme_find_context *ctx,
 | 
				
			||||||
 | 
						const char *theme, const char *name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						// Either a cycle, or a common ancestor of inherited themes, which is valid.
 | 
				
			||||||
 | 
						if (str_map_find (&ctx->visited, theme))
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						str_map_set (&ctx->visited, theme, (void *) (intptr_t) 1);
 | 
				
			||||||
 | 
						struct desktop_file index = icon_theme_find__index (ctx, theme);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *directories =
 | 
				
			||||||
 | 
							desktop_file_get_string (&index, "Icon Theme", "Directories");
 | 
				
			||||||
 | 
						if (!directories)
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// NOTE: The sizes are not deduplicated, and priorities are uncertain.
 | 
				
			||||||
 | 
						struct strv dirs = strv_make ();
 | 
				
			||||||
 | 
						cstr_split (directories, ",", true, &dirs);
 | 
				
			||||||
 | 
						free (directories);
 | 
				
			||||||
 | 
						for (size_t d = 0; d < dirs.len; d++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							// The hicolor icon theme stuffs everything in Directories.
 | 
				
			||||||
 | 
							if (desktop_file_get (&index, dirs.vector[d], "Scale")
 | 
				
			||||||
 | 
							 && desktop_file_get_integer (&index, dirs.vector[d], "Scale") != 1)
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for (size_t i = 0; i < ctx->base.len; i++)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								char *path = xstrdup_printf ("%s/%s/%s/%s.png",
 | 
				
			||||||
 | 
									ctx->base.vector[i], theme, dirs.vector[d], name);
 | 
				
			||||||
 | 
								struct icon_theme_icon *icon = icon_theme_open (path);
 | 
				
			||||||
 | 
								free (path);
 | 
				
			||||||
 | 
								if (icon)
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									ARRAY_RESERVE (ctx->icons, 1);
 | 
				
			||||||
 | 
									ctx->icons[ctx->icons_len++] = icon;
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						strv_free (&dirs);
 | 
				
			||||||
 | 
						if (ctx->icons_len)
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *inherits =
 | 
				
			||||||
 | 
							desktop_file_get_string (&index, "Icon Theme", "Inherits");
 | 
				
			||||||
 | 
						if (inherits)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							struct strv parents = strv_make ();
 | 
				
			||||||
 | 
							cstr_split (inherits, ",", true, &parents);
 | 
				
			||||||
 | 
							free (inherits);
 | 
				
			||||||
 | 
							for (size_t i = 0; i < parents.len; i++)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								icon_theme_find__named (ctx, parents.vector[i], name);
 | 
				
			||||||
 | 
								if (ctx->icons_len)
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							strv_free (&parents);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
						desktop_file_free (&index);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Return all base directories appropriate for icon search.
 | 
				
			||||||
 | 
					static struct strv
 | 
				
			||||||
 | 
					icon_theme_get_base_directories (void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct strv dirs = strv_make ();
 | 
				
			||||||
 | 
						struct str icons = str_make ();
 | 
				
			||||||
 | 
						(void) str_append_env_path (&icons, "HOME", false);
 | 
				
			||||||
 | 
						str_append (&icons, "/.icons");
 | 
				
			||||||
 | 
						strv_append_owned (&dirs, str_steal (&icons));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Note that we use XDG_CONFIG_HOME as well, which might be intended.
 | 
				
			||||||
 | 
						struct strv xdg = strv_make ();
 | 
				
			||||||
 | 
						get_xdg_data_dirs (&xdg);
 | 
				
			||||||
 | 
						for (size_t i = 0; i < xdg.len; i++)
 | 
				
			||||||
 | 
							strv_append_owned (&dirs, xstrdup_printf ("%s/icons", xdg.vector[i]));
 | 
				
			||||||
 | 
						strv_free (&xdg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						strv_append (&dirs, "/usr/share/pixmaps");
 | 
				
			||||||
 | 
						return dirs;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int
 | 
				
			||||||
 | 
					icon_theme_find__compare (const void *a, const void *b)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const struct icon_theme_icon **ia = (const struct icon_theme_icon **) a;
 | 
				
			||||||
 | 
						const struct icon_theme_icon **ib = (const struct icon_theme_icon **) b;
 | 
				
			||||||
 | 
						double pa = (double) (*ia)->width * (*ia)->height;
 | 
				
			||||||
 | 
						double pb = (double) (*ib)->width * (*ib)->height;
 | 
				
			||||||
 | 
						return (pa > pb) - (pa < pb);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Return all sizes of the named icon.  When the theme name is not NULL,
 | 
				
			||||||
 | 
					/// use it as the preferred theme.  Always consult fallbacks locations.
 | 
				
			||||||
 | 
					/// Ignore icon scales other than 1.
 | 
				
			||||||
 | 
					static struct icon_theme_icon **
 | 
				
			||||||
 | 
					icon_theme_find (const char *theme, const char *name, size_t *len)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct icon_theme_find_context ctx = {};
 | 
				
			||||||
 | 
						ctx.base = icon_theme_get_base_directories ();
 | 
				
			||||||
 | 
						ctx.visited = str_map_make (NULL);
 | 
				
			||||||
 | 
						ARRAY_INIT (ctx.icons);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (theme)
 | 
				
			||||||
 | 
							icon_theme_find__named (&ctx, theme, name);
 | 
				
			||||||
 | 
						if (!ctx.icons_len)
 | 
				
			||||||
 | 
							icon_theme_find__named (&ctx, "hicolor", name);
 | 
				
			||||||
 | 
						if (!ctx.icons_len)
 | 
				
			||||||
 | 
							icon_theme_find__fallback (&ctx, name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						strv_free (&ctx.base);
 | 
				
			||||||
 | 
						str_map_free (&ctx.visited);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ARRAY_RESERVE (ctx.icons, 1);
 | 
				
			||||||
 | 
						ctx.icons[ctx.icons_len] = NULL;
 | 
				
			||||||
 | 
						if (!ctx.icons_len)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							free (ctx.icons);
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						qsort (ctx.icons,
 | 
				
			||||||
 | 
							ctx.icons_len, sizeof *ctx.icons, icon_theme_find__compare);
 | 
				
			||||||
 | 
						*len = ctx.icons_len;
 | 
				
			||||||
 | 
						return ctx.icons;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					icon_theme_free (struct icon_theme_icon **icons)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						for (struct icon_theme_icon **p = icons; *p; p++)
 | 
				
			||||||
 | 
							free (*p);
 | 
				
			||||||
 | 
						free (icons);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef LIBERTY_XDG_WANT_X11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					icon_theme_set_window_icon (Display *dpy,
 | 
				
			||||||
 | 
						Window window, const char *theme, const char *name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						size_t icons_len = 0;
 | 
				
			||||||
 | 
						struct icon_theme_icon **icons = icon_theme_find (theme, name, &icons_len);
 | 
				
			||||||
 | 
						if (!icons)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						size_t n = 0;
 | 
				
			||||||
 | 
						for (size_t i = 0; i < icons_len; i++)
 | 
				
			||||||
 | 
							n += 2 + icons[i]->width * icons[i]->height;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						unsigned long *data = xcalloc (n, sizeof *data), *p = data;
 | 
				
			||||||
 | 
						for (size_t i = 0; i < icons_len; i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							*p++ = icons[i]->width;
 | 
				
			||||||
 | 
							*p++ = icons[i]->height;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							uint32_t *q = icons[i]->argb;
 | 
				
			||||||
 | 
							for (size_t k = icons[i]->width * icons[i]->height; k--; )
 | 
				
			||||||
 | 
								*p++ = *q++;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						XChangeProperty (dpy, window, XInternAtom (dpy, "_NET_WM_ICON", False),
 | 
				
			||||||
 | 
							XA_CARDINAL, 32, PropModeReplace, (const unsigned char *) data, n);
 | 
				
			||||||
 | 
						free (data);
 | 
				
			||||||
 | 
						icon_theme_free (icons);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // LIBERTY_XDG_WANT_X11
 | 
				
			||||||
 | 
					#endif // LIBERTY_XDG_WANT_ICONS
 | 
				
			||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * liberty-xui.c: the ultimate C unlibrary: hybrid terminal/X11 UI
 | 
					 * liberty-xui.c: the ultimate C unlibrary: hybrid terminal/X11 UI
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Copyright (c) 2016 - 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
					 * Copyright (c) 2016 - 2024, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
					 * Permission to use, copy, modify, and/or distribute this software for any
 | 
				
			||||||
 * purpose with or without fee is hereby granted.
 | 
					 * purpose with or without fee is hereby granted.
 | 
				
			||||||
@@ -17,18 +17,19 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This file includes some common stuff to build terminal/X11 applications with.
 | 
					// This file includes some common stuff to build terminal/X11 applications with.
 | 
				
			||||||
 | 
					// It assumes you've already included liberty.c, and may include liberty-xdg.c.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <ncurses.h>
 | 
					#include <ncurses.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// It is surprisingly hard to find a good library to handle Unicode shenanigans,
 | 
					// It is surprisingly hard to find a good library to handle Unicode shenanigans,
 | 
				
			||||||
// and there's enough of those for it to be impractical to reimplement them.
 | 
					// and there's enough of those for it to be impractical to reimplement them.
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
//                         GLib          ICU     libunistring    utf8proc
 | 
					//                        GLib        ICU   libunistring  utf8proc libgrapheme
 | 
				
			||||||
// Decently sized            .            .            x            x
 | 
					// Decently sized           .          .          x          x          x
 | 
				
			||||||
// Grapheme breaks           .            x            .            x
 | 
					// Grapheme breaks          .          x          .          x          x
 | 
				
			||||||
// Character width           x            .            x            x
 | 
					// Character width          x          .          x          x          .
 | 
				
			||||||
// Locale handling           .            .            x            .
 | 
					// Locale handling          .          .          x          .          .
 | 
				
			||||||
// Liberal license           .            x            .            x
 | 
					// Liberal license          .          x          .          x          x
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// Also note that the ICU API is icky and uses UTF-16 for its primary encoding.
 | 
					// Also note that the ICU API is icky and uses UTF-16 for its primary encoding.
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
@@ -47,9 +48,9 @@
 | 
				
			|||||||
#include <unicase.h>
 | 
					#include <unicase.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <termios.h>
 | 
					#include <termios.h>
 | 
				
			||||||
#ifndef TIOCGWINSZ
 | 
					#ifdef HAVE_RESIZETERM
 | 
				
			||||||
#include <sys/ioctl.h>
 | 
					#include <sys/ioctl.h>
 | 
				
			||||||
#endif  // ! TIOCGWINSZ
 | 
					#endif  // HAVE_RESIZETERM
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ncurses is notoriously retarded for input handling, and in past versions
 | 
					// ncurses is notoriously retarded for input handling, and in past versions
 | 
				
			||||||
// used to process mouse events unreliably.  Moreover, rxvt-unicode only
 | 
					// used to process mouse events unreliably.  Moreover, rxvt-unicode only
 | 
				
			||||||
@@ -66,6 +67,10 @@ enum { XUI_KEYMOD_DOUBLE_CLICK = 1 << 15 };
 | 
				
			|||||||
#include <X11/keysym.h>
 | 
					#include <X11/keysym.h>
 | 
				
			||||||
#include <X11/XKBlib.h>
 | 
					#include <X11/XKBlib.h>
 | 
				
			||||||
#include <X11/Xft/Xft.h>
 | 
					#include <X11/Xft/Xft.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define LIBERTY_XDG_WANT_X11
 | 
				
			||||||
 | 
					#define LIBERTY_XDG_WANT_ICONS
 | 
				
			||||||
 | 
					#include "liberty-xdg.c"
 | 
				
			||||||
#endif  // LIBERTY_XUI_WANT_X11
 | 
					#endif  // LIBERTY_XUI_WANT_X11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The application needs to implement these.
 | 
					// The application needs to implement these.
 | 
				
			||||||
@@ -200,8 +205,8 @@ static void
 | 
				
			|||||||
line_editor_start (struct line_editor *self, char prompt)
 | 
					line_editor_start (struct line_editor *self, char prompt)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	self->alloc = 16;
 | 
						self->alloc = 16;
 | 
				
			||||||
	self->line = xcalloc (sizeof *self->line, self->alloc);
 | 
						self->line = xcalloc (self->alloc, sizeof *self->line);
 | 
				
			||||||
	self->w = xcalloc (sizeof *self->w, self->alloc);
 | 
						self->w = xcalloc (self->alloc, sizeof *self->w);
 | 
				
			||||||
	self->len = 0;
 | 
						self->len = 0;
 | 
				
			||||||
	self->point = 0;
 | 
						self->point = 0;
 | 
				
			||||||
	self->prompt = prompt;
 | 
						self->prompt = prompt;
 | 
				
			||||||
@@ -690,7 +695,10 @@ struct xui
 | 
				
			|||||||
	XftDraw *xft_draw;                  ///< Xft rendering context
 | 
						XftDraw *xft_draw;                  ///< Xft rendering context
 | 
				
			||||||
	struct x11_font *xft_fonts;         ///< Font collection
 | 
						struct x11_font *xft_fonts;         ///< Font collection
 | 
				
			||||||
	char *x11_selection;                ///< CLIPBOARD selection
 | 
						char *x11_selection;                ///< CLIPBOARD selection
 | 
				
			||||||
 | 
						struct xdg_xsettings x11_xsettings; ///< XSETTINGS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						int32_t x11_double_click_time;      ///< Maximum delay for double clicks
 | 
				
			||||||
 | 
						int32_t x11_double_click_distance;  ///< Maximum distance for double clicks
 | 
				
			||||||
	const char *x11_fontname;           ///< Fontconfig font name
 | 
						const char *x11_fontname;           ///< Fontconfig font name
 | 
				
			||||||
	const char *x11_fontname_monospace; ///< Fontconfig monospace font name
 | 
						const char *x11_fontname_monospace; ///< Fontconfig monospace font name
 | 
				
			||||||
	XRenderColor *x_fg;                 ///< Foreground per attribute
 | 
						XRenderColor *x_fg;                 ///< Foreground per attribute
 | 
				
			||||||
@@ -720,9 +728,12 @@ tui_flush_buffer (struct widget *self, struct row_buffer *buf)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	move (self->y, self->x);
 | 
						move (self->y, self->x);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	int space = MIN (self->width, g_xui.width - self->x);
 | 
						if (self->y >= 0 && self->y < g_xui.height)
 | 
				
			||||||
	row_buffer_align (buf, space, self->attrs);
 | 
						{
 | 
				
			||||||
	row_buffer_flush (buf);
 | 
							int space = MIN (self->width, g_xui.width - self->x);
 | 
				
			||||||
 | 
							row_buffer_align (buf, space, self->attrs);
 | 
				
			||||||
 | 
							row_buffer_flush (buf);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	row_buffer_free (buf);
 | 
						row_buffer_free (buf);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1030,7 +1041,6 @@ x11_font_destroy (struct x11_font *self)
 | 
				
			|||||||
	LIST_FOR_EACH (struct x11_font_link, iter, self->list)
 | 
						LIST_FOR_EACH (struct x11_font_link, iter, self->list)
 | 
				
			||||||
		x11_font_link_destroy (iter);
 | 
							x11_font_link_destroy (iter);
 | 
				
			||||||
	free (self);
 | 
						free (self);
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Find or instantiate a font that can render the character given by cp.
 | 
					/// Find or instantiate a font that can render the character given by cp.
 | 
				
			||||||
@@ -1161,8 +1171,10 @@ x11_widget_font (struct widget *self)
 | 
				
			|||||||
	unsigned style = 0;
 | 
						unsigned style = 0;
 | 
				
			||||||
	if (self->attrs & A_BOLD)
 | 
						if (self->attrs & A_BOLD)
 | 
				
			||||||
		style |= X11_FONT_BOLD;
 | 
							style |= X11_FONT_BOLD;
 | 
				
			||||||
 | 
					#ifdef A_ITALIC
 | 
				
			||||||
	if (self->attrs & A_ITALIC)
 | 
						if (self->attrs & A_ITALIC)
 | 
				
			||||||
		style |= X11_FONT_ITALIC;
 | 
							style |= X11_FONT_ITALIC;
 | 
				
			||||||
 | 
					#endif  // A_ITALIC
 | 
				
			||||||
	if (self->extended_attrs & XUI_ATTR_MONOSPACE)
 | 
						if (self->extended_attrs & XUI_ATTR_MONOSPACE)
 | 
				
			||||||
		style |= X11_FONT_MONOSPACE;
 | 
							style |= X11_FONT_MONOSPACE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1374,6 +1386,7 @@ x11_destroy (void)
 | 
				
			|||||||
	LIST_FOR_EACH (struct x11_font, font, g_xui.xft_fonts)
 | 
						LIST_FOR_EACH (struct x11_font, font, g_xui.xft_fonts)
 | 
				
			||||||
		x11_font_destroy (font);
 | 
							x11_font_destroy (font);
 | 
				
			||||||
	cstr_set (&g_xui.x11_selection, NULL);
 | 
						cstr_set (&g_xui.x11_selection, NULL);
 | 
				
			||||||
 | 
						xdg_xsettings_free (&g_xui.x11_xsettings);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	free (g_xui.x_fg);
 | 
						free (g_xui.x_fg);
 | 
				
			||||||
	free (g_xui.x_bg);
 | 
						free (g_xui.x_bg);
 | 
				
			||||||
@@ -1542,7 +1555,8 @@ x11_init_pixmap (void)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	int screen = DefaultScreen (g_xui.dpy);
 | 
						int screen = DefaultScreen (g_xui.dpy);
 | 
				
			||||||
	g_xui.x11_pixmap = XCreatePixmap (g_xui.dpy, g_xui.x11_window,
 | 
						g_xui.x11_pixmap = XCreatePixmap (g_xui.dpy, g_xui.x11_window,
 | 
				
			||||||
		g_xui.width, g_xui.height, DefaultDepth (g_xui.dpy, screen));
 | 
							MAX (g_xui.width, 1), MAX (g_xui.height, 1),
 | 
				
			||||||
 | 
							DefaultDepth (g_xui.dpy, screen));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Visual *visual = DefaultVisual (g_xui.dpy, screen);
 | 
						Visual *visual = DefaultVisual (g_xui.dpy, screen);
 | 
				
			||||||
	XRenderPictFormat *format = XRenderFindVisualFormat (g_xui.dpy, visual);
 | 
						XRenderPictFormat *format = XRenderFindVisualFormat (g_xui.dpy, visual);
 | 
				
			||||||
@@ -1622,9 +1636,10 @@ on_x11_input_event (XEvent *ev)
 | 
				
			|||||||
	unsigned int button = ev->xbutton.button;
 | 
						unsigned int button = ev->xbutton.button;
 | 
				
			||||||
	int modifiers = x11_state_to_modifiers (ev->xbutton.state);
 | 
						int modifiers = x11_state_to_modifiers (ev->xbutton.state);
 | 
				
			||||||
	if (ev->type == ButtonPress
 | 
						if (ev->type == ButtonPress
 | 
				
			||||||
	 && ev->xbutton.time - last_press_event.xbutton.time < 500
 | 
						 && ev->xbutton.time - last_press_event.xbutton.time
 | 
				
			||||||
	 && abs (last_press_event.xbutton.x - x) < 5
 | 
							< (Time) g_xui.x11_double_click_time
 | 
				
			||||||
	 && abs (last_press_event.xbutton.y - y) < 5
 | 
						 && abs (last_press_event.xbutton.x - x) < g_xui.x11_double_click_distance
 | 
				
			||||||
 | 
						 && abs (last_press_event.xbutton.y - y) < g_xui.x11_double_click_distance
 | 
				
			||||||
	 && last_press_event.xbutton.button == button)
 | 
						 && last_press_event.xbutton.button == button)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		modifiers |= XUI_KEYMOD_DOUBLE_CLICK;
 | 
							modifiers |= XUI_KEYMOD_DOUBLE_CLICK;
 | 
				
			||||||
@@ -1869,6 +1884,8 @@ x11_init (struct poller *poller, struct attrs *app_attrs, size_t app_attrs_len)
 | 
				
			|||||||
	if (!(g_xui.dpy = XkbOpenDisplay
 | 
						if (!(g_xui.dpy = XkbOpenDisplay
 | 
				
			||||||
		(NULL, &g_xui.xkb_base_event_code, NULL, NULL, NULL, NULL)))
 | 
							(NULL, &g_xui.xkb_base_event_code, NULL, NULL, NULL, NULL)))
 | 
				
			||||||
		exit_fatal ("cannot open display");
 | 
							exit_fatal ("cannot open display");
 | 
				
			||||||
 | 
						if (!XftInit (NULL))
 | 
				
			||||||
 | 
							print_warning ("Fontconfig initialization failed");
 | 
				
			||||||
	if (!XftDefaultHasRender (g_xui.dpy))
 | 
						if (!XftDefaultHasRender (g_xui.dpy))
 | 
				
			||||||
		exit_fatal ("XRender is not supported");
 | 
							exit_fatal ("XRender is not supported");
 | 
				
			||||||
	if (!(g_xui.x11_im = XOpenIM (g_xui.dpy, NULL, NULL, NULL)))
 | 
						if (!(g_xui.x11_im = XOpenIM (g_xui.dpy, NULL, NULL, NULL)))
 | 
				
			||||||
@@ -1890,8 +1907,13 @@ x11_init (struct poller *poller, struct attrs *app_attrs, size_t app_attrs_len)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	x11_init_attributes (app_attrs, app_attrs_len);
 | 
						x11_init_attributes (app_attrs, app_attrs_len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!FcInit ())
 | 
						// https://www.freedesktop.org/wiki/Specifications/XSettingsRegistry/
 | 
				
			||||||
		print_warning ("Fontconfig initialization failed");
 | 
						// TODO: Try to use Xft/{Antialias,DPI,HintStyle,Hinting,RGBA}
 | 
				
			||||||
 | 
						//   from XSETTINGS.  Sadly, Gtk/FontName is in the Pango format,
 | 
				
			||||||
 | 
						//   which is rather difficult to parse.
 | 
				
			||||||
 | 
						g_xui.x11_xsettings = xdg_xsettings_make ();
 | 
				
			||||||
 | 
						xdg_xsettings_update (&g_xui.x11_xsettings, g_xui.dpy);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!(g_xui.xft_fonts = x11_font_open (0)))
 | 
						if (!(g_xui.xft_fonts = x11_font_open (0)))
 | 
				
			||||||
		exit_fatal ("cannot open a font");
 | 
							exit_fatal ("cannot open a font");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1941,6 +1963,25 @@ x11_init (struct poller *poller, struct attrs *app_attrs, size_t app_attrs_len)
 | 
				
			|||||||
		XSetWMName (g_xui.dpy, g_xui.x11_window, &prop);
 | 
							XSetWMName (g_xui.dpy, g_xui.x11_window, &prop);
 | 
				
			||||||
	XFree (prop.value);
 | 
						XFree (prop.value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// It should not be outlandish to expect to find a program icon,
 | 
				
			||||||
 | 
						// although it should be possible to use a "DBus well-known name".
 | 
				
			||||||
 | 
						const char *icon_theme_name = NULL;
 | 
				
			||||||
 | 
						const struct xdg_xsettings_setting *setting =
 | 
				
			||||||
 | 
							str_map_find (&g_xui.x11_xsettings.settings, "Net/IconThemeName");
 | 
				
			||||||
 | 
						if (setting != NULL && setting->type == XDG_XSETTINGS_STRING)
 | 
				
			||||||
 | 
							icon_theme_name = setting->string.str;
 | 
				
			||||||
 | 
						icon_theme_set_window_icon (g_xui.dpy,
 | 
				
			||||||
 | 
							g_xui.x11_window, icon_theme_name, name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ((setting = str_map_find (&g_xui.x11_xsettings.settings,
 | 
				
			||||||
 | 
								"Net/DoubleClickTime"))
 | 
				
			||||||
 | 
						 && setting->type == XDG_XSETTINGS_INTEGER && setting->integer >= 0)
 | 
				
			||||||
 | 
							g_xui.x11_double_click_time = setting->integer;
 | 
				
			||||||
 | 
						if ((setting = str_map_find (&g_xui.x11_xsettings.settings,
 | 
				
			||||||
 | 
								"Net/DoubleClickDistance"))
 | 
				
			||||||
 | 
						 && setting->type == XDG_XSETTINGS_INTEGER && setting->integer >= 0)
 | 
				
			||||||
 | 
							g_xui.x11_double_click_distance = setting->integer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO: It is possible to do, e.g., on-the-spot.
 | 
						// TODO: It is possible to do, e.g., on-the-spot.
 | 
				
			||||||
	XIMStyle im_style = XIMPreeditNothing | XIMStatusNothing;
 | 
						XIMStyle im_style = XIMPreeditNothing | XIMStatusNothing;
 | 
				
			||||||
	XIMStyles *im_styles = NULL;
 | 
						XIMStyles *im_styles = NULL;
 | 
				
			||||||
@@ -2146,11 +2187,10 @@ xui_preinit (void)
 | 
				
			|||||||
	// Presumably, although not necessarily; unsure if queryable at all.
 | 
						// Presumably, although not necessarily; unsure if queryable at all.
 | 
				
			||||||
	g_xui.focused = true;
 | 
						g_xui.focused = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO: Try to use Gtk/FontName from the _XSETTINGS_S%d selection,
 | 
					 | 
				
			||||||
	//   as well as Net/DoubleClick*.  See the XSETTINGS proposal for details.
 | 
					 | 
				
			||||||
	//   https://www.freedesktop.org/wiki/Specifications/XSettingsRegistry/
 | 
					 | 
				
			||||||
	//   Note that this needs an X11 connection already.
 | 
					 | 
				
			||||||
#ifdef LIBERTY_XUI_WANT_X11
 | 
					#ifdef LIBERTY_XUI_WANT_X11
 | 
				
			||||||
 | 
						// Note that XSETTINGS overrides some values in the init.
 | 
				
			||||||
 | 
						g_xui.x11_double_click_time = 500;
 | 
				
			||||||
 | 
						g_xui.x11_double_click_distance = 5;
 | 
				
			||||||
	g_xui.x11_fontname = "sans\\-serif-11";
 | 
						g_xui.x11_fontname = "sans\\-serif-11";
 | 
				
			||||||
	g_xui.x11_fontname_monospace = "monospace-11";
 | 
						g_xui.x11_fontname_monospace = "monospace-11";
 | 
				
			||||||
#endif  // LIBERTY_XUI_WANT_X11
 | 
					#endif  // LIBERTY_XUI_WANT_X11
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										105
									
								
								liberty.c
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								liberty.c
									
									
									
									
									
								
							@@ -1,7 +1,7 @@
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * liberty.c: the ultimate C unlibrary
 | 
					 * liberty.c: the ultimate C unlibrary
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Copyright (c) 2014 - 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
					 * Copyright (c) 2014 - 2024, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
					 * Permission to use, copy, modify, and/or distribute this software for any
 | 
				
			||||||
 * purpose with or without fee is hereby granted.
 | 
					 * purpose with or without fee is hereby granted.
 | 
				
			||||||
@@ -292,7 +292,8 @@ xreallocarray (void *o, size_t n, size_t m)
 | 
				
			|||||||
static char *
 | 
					static char *
 | 
				
			||||||
xstrdup (const char *s)
 | 
					xstrdup (const char *s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return strcpy (xmalloc (strlen (s) + 1), s);
 | 
						size_t len = strlen (s) + 1;
 | 
				
			||||||
 | 
						return memcpy (xmalloc (len), s, len);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static char *
 | 
					static char *
 | 
				
			||||||
@@ -315,7 +316,7 @@ xstrndup (const char *s, size_t n)
 | 
				
			|||||||
#define ARRAY(type, name) type *name; size_t name ## _len, name ## _alloc;
 | 
					#define ARRAY(type, name) type *name; size_t name ## _len, name ## _alloc;
 | 
				
			||||||
#define ARRAY_INIT_SIZED(a, n)                                                 \
 | 
					#define ARRAY_INIT_SIZED(a, n)                                                 \
 | 
				
			||||||
	BLOCK_START                                                                \
 | 
						BLOCK_START                                                                \
 | 
				
			||||||
		(a) = xcalloc (sizeof *(a), (a ## _alloc) = (n));                      \
 | 
							(a) = xcalloc ((a ## _alloc) = (n), sizeof *(a));                      \
 | 
				
			||||||
		(a ## _len) = 0;                                                       \
 | 
							(a ## _len) = 0;                                                       \
 | 
				
			||||||
	BLOCK_END
 | 
						BLOCK_END
 | 
				
			||||||
#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
 | 
					#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
 | 
				
			||||||
@@ -397,7 +398,7 @@ strv_make (void)
 | 
				
			|||||||
	struct strv self;
 | 
						struct strv self;
 | 
				
			||||||
	self.alloc = 4;
 | 
						self.alloc = 4;
 | 
				
			||||||
	self.len = 0;
 | 
						self.len = 0;
 | 
				
			||||||
	self.vector = xcalloc (sizeof *self.vector, self.alloc);
 | 
						self.vector = xcalloc (self.alloc, sizeof *self.vector);
 | 
				
			||||||
	return self;
 | 
						return self;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -652,6 +653,60 @@ str_pack_u64 (struct str *self, uint64_t x)
 | 
				
			|||||||
#define str_pack_i32(self, x)  str_pack_u32 ((self), (uint32_t) (x))
 | 
					#define str_pack_i32(self, x)  str_pack_u32 ((self), (uint32_t) (x))
 | 
				
			||||||
#define str_pack_i64(self, x)  str_pack_u64 ((self), (uint64_t) (x))
 | 
					#define str_pack_i64(self, x)  str_pack_u64 ((self), (uint64_t) (x))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// --- Reading binary numbers --------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Doing this byte by byte prevents unaligned memory access issues.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static uint64_t
 | 
				
			||||||
 | 
					peek_u64be (const uint8_t *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (uint64_t) p[0] << 56 | (uint64_t) p[1] << 48
 | 
				
			||||||
 | 
							| (uint64_t) p[2] << 40 | (uint64_t) p[3] << 32
 | 
				
			||||||
 | 
							| (uint64_t) p[4] << 24 | (uint64_t) p[5] << 16 | p[6] << 8 | p[7];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static uint32_t
 | 
				
			||||||
 | 
					peek_u32be (const uint8_t *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 | p[2] << 8 | p[3];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static uint16_t
 | 
				
			||||||
 | 
					peek_u16be (const uint8_t *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (uint16_t) p[0] << 8 | p[1];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static uint64_t
 | 
				
			||||||
 | 
					peek_u64le (const uint8_t *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (uint64_t) p[7] << 56 | (uint64_t) p[6] << 48
 | 
				
			||||||
 | 
							| (uint64_t) p[5] << 40 | (uint64_t) p[4] << 32
 | 
				
			||||||
 | 
							| (uint64_t) p[3] << 24 | (uint64_t) p[2] << 16 | p[1] << 8 | p[0];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static uint32_t
 | 
				
			||||||
 | 
					peek_u32le (const uint8_t *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (uint32_t) p[3] << 24 | (uint32_t) p[2] << 16 | p[1] << 8 | p[0];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static uint16_t
 | 
				
			||||||
 | 
					peek_u16le (const uint8_t *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (uint16_t) p[1] << 8 | p[0];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct peeker
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						uint64_t (*u64) (const uint8_t *);
 | 
				
			||||||
 | 
						uint32_t (*u32) (const uint8_t *);
 | 
				
			||||||
 | 
						uint16_t (*u16) (const uint8_t *);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const struct peeker peeker_be = {peek_u64be, peek_u32be, peek_u16be};
 | 
				
			||||||
 | 
					static const struct peeker peeker_le = {peek_u64le, peek_u32le, peek_u16le};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// --- Errors ------------------------------------------------------------------
 | 
					// --- Errors ------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Error reporting utilities.  Inspired by GError, only much simpler.
 | 
					// Error reporting utilities.  Inspired by GError, only much simpler.
 | 
				
			||||||
@@ -1154,7 +1209,10 @@ async_make (struct async_manager *manager)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Only allowed from the main thread once the job has been started but before
 | 
					/// Only allowed from the main thread once the job has been started but before
 | 
				
			||||||
/// the results have been dispatched
 | 
					/// the results have been dispatched.
 | 
				
			||||||
 | 
					///
 | 
				
			||||||
 | 
					/// Note that it may in practice lead to memory leakage, although that's
 | 
				
			||||||
 | 
					/// an implementation issue: https://eissing.org/icing/posts/rip_pthread_cancel/
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
async_cancel (struct async *self)
 | 
					async_cancel (struct async *self)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -2503,12 +2561,7 @@ msg_reader_get (struct msg_reader *self, size_t *len)
 | 
				
			|||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	uint8_t *x = (uint8_t *) self->buf.str + self->offset;
 | 
						uint8_t *x = (uint8_t *) self->buf.str + self->offset;
 | 
				
			||||||
	uint64_t msg_len
 | 
						uint64_t msg_len = peek_u64be (x);
 | 
				
			||||||
		= (uint64_t) x[0] << 56 | (uint64_t) x[1] << 48
 | 
					 | 
				
			||||||
		| (uint64_t) x[2] << 40 | (uint64_t) x[3] << 32
 | 
					 | 
				
			||||||
		| (uint64_t) x[4] << 24 | (uint64_t) x[5] << 16
 | 
					 | 
				
			||||||
		| (uint64_t) x[6] << 8  | (uint64_t) x[7];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (msg_len < sizeof msg_len)
 | 
						if (msg_len < sizeof msg_len)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		// The message is shorter than its header
 | 
							// The message is shorter than its header
 | 
				
			||||||
@@ -2573,8 +2626,7 @@ static bool
 | 
				
			|||||||
msg_unpacker_u16 (struct msg_unpacker *self, uint16_t *value)
 | 
					msg_unpacker_u16 (struct msg_unpacker *self, uint16_t *value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	UNPACKER_INT_BEGIN
 | 
						UNPACKER_INT_BEGIN
 | 
				
			||||||
	*value
 | 
						*value = peek_u16be (x);
 | 
				
			||||||
		= (uint16_t) x[0] << 8  | (uint16_t) x[1];
 | 
					 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -2582,9 +2634,7 @@ static bool
 | 
				
			|||||||
msg_unpacker_u32 (struct msg_unpacker *self, uint32_t *value)
 | 
					msg_unpacker_u32 (struct msg_unpacker *self, uint32_t *value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	UNPACKER_INT_BEGIN
 | 
						UNPACKER_INT_BEGIN
 | 
				
			||||||
	*value
 | 
						*value = peek_u32be (x);
 | 
				
			||||||
		= (uint32_t) x[0] << 24 | (uint32_t) x[1] << 16
 | 
					 | 
				
			||||||
		| (uint32_t) x[2] << 8  | (uint32_t) x[3];
 | 
					 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -2592,11 +2642,7 @@ static bool
 | 
				
			|||||||
msg_unpacker_u64 (struct msg_unpacker *self, uint64_t *value)
 | 
					msg_unpacker_u64 (struct msg_unpacker *self, uint64_t *value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	UNPACKER_INT_BEGIN
 | 
						UNPACKER_INT_BEGIN
 | 
				
			||||||
	*value
 | 
						*value = peek_u64be (x);
 | 
				
			||||||
		= (uint64_t) x[0] << 56 | (uint64_t) x[1] << 48
 | 
					 | 
				
			||||||
		| (uint64_t) x[2] << 40 | (uint64_t) x[3] << 32
 | 
					 | 
				
			||||||
		| (uint64_t) x[4] << 24 | (uint64_t) x[5] << 16
 | 
					 | 
				
			||||||
		| (uint64_t) x[6] << 8  | (uint64_t) x[7];
 | 
					 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -3552,6 +3598,8 @@ write_file_safe (const char *filename, const void *data, size_t data_len,
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	// XXX: ideally we would also open the directory, use *at() versions
 | 
						// XXX: ideally we would also open the directory, use *at() versions
 | 
				
			||||||
	//   of functions and call fsync() on the directory as appropriate
 | 
						//   of functions and call fsync() on the directory as appropriate
 | 
				
			||||||
 | 
						// FIXME: this should behave similarly to mkstemp(), just with 0666;
 | 
				
			||||||
 | 
						//   as it is, this function is not particularly safe
 | 
				
			||||||
	char *temp = xstrdup_printf ("%s.new", filename);
 | 
						char *temp = xstrdup_printf ("%s.new", filename);
 | 
				
			||||||
	bool success = write_file (temp, data, data_len, e);
 | 
						bool success = write_file (temp, data, data_len, e);
 | 
				
			||||||
	if (success && !(success = !rename (temp, filename)))
 | 
						if (success && !(success = !rename (temp, filename)))
 | 
				
			||||||
@@ -4456,7 +4504,7 @@ struct config_item
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	value;                              ///< The value of this item
 | 
						value;                              ///< The value of this item
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct config_schema *schema;       ///< Schema describing this value
 | 
						const struct config_schema *schema; ///< Schema describing this value
 | 
				
			||||||
	void *user_data;                    ///< User value attached by schema owner
 | 
						void *user_data;                    ///< User value attached by schema owner
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -4608,7 +4656,7 @@ config_item_object (void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static bool
 | 
					static bool
 | 
				
			||||||
config_schema_accepts_type
 | 
					config_schema_accepts_type
 | 
				
			||||||
	(struct config_schema *self, enum config_item_type type)
 | 
						(const struct config_schema *self, enum config_item_type type)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (self->type == type)
 | 
						if (self->type == type)
 | 
				
			||||||
		return true;
 | 
							return true;
 | 
				
			||||||
@@ -4621,7 +4669,7 @@ config_schema_accepts_type
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static bool
 | 
					static bool
 | 
				
			||||||
config_item_validate_by_schema (struct config_item *self,
 | 
					config_item_validate_by_schema (struct config_item *self,
 | 
				
			||||||
	struct config_schema *schema, struct error **e)
 | 
						const struct config_schema *schema, struct error **e)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct error *error = NULL;
 | 
						struct error *error = NULL;
 | 
				
			||||||
	if (!config_schema_accepts_type (schema, self->type))
 | 
						if (!config_schema_accepts_type (schema, self->type))
 | 
				
			||||||
@@ -4642,7 +4690,7 @@ static bool
 | 
				
			|||||||
config_item_set_from (struct config_item *self, struct config_item *source,
 | 
					config_item_set_from (struct config_item *self, struct config_item *source,
 | 
				
			||||||
	struct error **e)
 | 
						struct error **e)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct config_schema *schema = self->schema;
 | 
						const struct config_schema *schema = self->schema;
 | 
				
			||||||
	if (!schema)
 | 
						if (!schema)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		// Easy, we don't know what this item is
 | 
							// Easy, we don't know what this item is
 | 
				
			||||||
@@ -4788,7 +4836,8 @@ config_item_write_kv_pair (struct config_writer *self,
 | 
				
			|||||||
		str_append_printf (self->output,
 | 
							str_append_printf (self->output,
 | 
				
			||||||
			"%s# %s\n", indent, value->schema->comment);
 | 
								"%s# %s\n", indent, value->schema->comment);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bool can_use_word = true;
 | 
						char *end = NULL;
 | 
				
			||||||
 | 
						bool can_use_word = ((void) strtoll (key, &end, 10), end == key);
 | 
				
			||||||
	for (const char *p = key; *p; p++)
 | 
						for (const char *p = key; *p; p++)
 | 
				
			||||||
		if (!config_tokenizer_is_word_char (*p))
 | 
							if (!config_tokenizer_is_word_char (*p))
 | 
				
			||||||
			can_use_word = false;
 | 
								can_use_word = false;
 | 
				
			||||||
@@ -5438,7 +5487,7 @@ end:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/// "user_data" is passed to allow its immediate use in validation callbacks
 | 
					/// "user_data" is passed to allow its immediate use in validation callbacks
 | 
				
			||||||
static struct config_item *
 | 
					static struct config_item *
 | 
				
			||||||
config_schema_initialize_item (struct config_schema *schema,
 | 
					config_schema_initialize_item (const struct config_schema *schema,
 | 
				
			||||||
	struct config_item *parent, void *user_data, struct error **warning,
 | 
						struct config_item *parent, void *user_data, struct error **warning,
 | 
				
			||||||
	struct error **e)
 | 
						struct error **e)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -5495,7 +5544,7 @@ keep_current:
 | 
				
			|||||||
/// Assign schemas and user_data to multiple items at once;
 | 
					/// Assign schemas and user_data to multiple items at once;
 | 
				
			||||||
/// feel free to copy over and modify to suit your particular needs
 | 
					/// feel free to copy over and modify to suit your particular needs
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
config_schema_apply_to_object (struct config_schema *schema_array,
 | 
					config_schema_apply_to_object (const struct config_schema *schema_array,
 | 
				
			||||||
	struct config_item *object, void *user_data)
 | 
						struct config_item *object, void *user_data)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	while (schema_array->name)
 | 
						while (schema_array->name)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -93,10 +93,12 @@ and always-present field, which must be a tag *enum*:
 | 
				
			|||||||
 case CAR:   void;
 | 
					 case CAR:   void;
 | 
				
			||||||
 case LORRY: i8 axles;
 | 
					 case LORRY: i8 axles;
 | 
				
			||||||
 case PLANE: i8 engines;
 | 
					 case PLANE: i8 engines;
 | 
				
			||||||
 | 
					 default:    void;
 | 
				
			||||||
 };
 | 
					 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
All possible enumeration values must be named, and there is no *case*
 | 
					There is no *case* fall-through.
 | 
				
			||||||
fall-through.
 | 
					Unless *default* is present, only the listed enumeration values are valid.
 | 
				
			||||||
 | 
					Any *default* must currently be empty.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Framing
 | 
					Framing
 | 
				
			||||||
-------
 | 
					-------
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,7 @@ syn region libertyxdrBlockComment start=+/[*]+ end=+[*]/+
 | 
				
			|||||||
syn match libertyxdrComment "//.*"
 | 
					syn match libertyxdrComment "//.*"
 | 
				
			||||||
syn match libertyxdrIdentifier "\<[[:alpha:]][[:alnum:]_]*\>"
 | 
					syn match libertyxdrIdentifier "\<[[:alpha:]][[:alnum:]_]*\>"
 | 
				
			||||||
syn match libertyxdrNumber "\<0\>\|\(-\|\<\)[1-9][[:digit:]]*\>"
 | 
					syn match libertyxdrNumber "\<0\>\|\(-\|\<\)[1-9][[:digit:]]*\>"
 | 
				
			||||||
syn keyword libertyxdrKeyword const enum struct union switch case
 | 
					syn keyword libertyxdrKeyword const enum struct union switch case default
 | 
				
			||||||
syn keyword libertyxdrType bool u8 u16 u32 u64 i8 i16 i32 i64 string void
 | 
					syn keyword libertyxdrType bool u8 u16 u32 u64 i8 i16 i32 i64 string void
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let b:current_syntax = "libertyxdr"
 | 
					let b:current_syntax = "libertyxdr"
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										214
									
								
								tests/help2adoc.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										214
									
								
								tests/help2adoc.sh
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,214 @@
 | 
				
			|||||||
 | 
					#!/bin/sh -e
 | 
				
			||||||
 | 
					# This test very exactly matches the output,
 | 
				
			||||||
 | 
					# but help2adoc is more or less feature-complete already.
 | 
				
			||||||
 | 
					self=$(realpath "$0")
 | 
				
			||||||
 | 
					help2adoc=$(realpath "$(dirname "$0")/../tools/help2adoc.awk")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_oneline_help() {
 | 
				
			||||||
 | 
					cat <<END
 | 
				
			||||||
 | 
					Usage: $self [--brightness [+-]BRIGHTNESS] [--input NAME] [--restart]
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_oneline_version() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					eizoctl 1.0
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_oneline_out() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					eizoctl(1)
 | 
				
			||||||
 | 
					==========
 | 
				
			||||||
 | 
					:doctype: manpage
 | 
				
			||||||
 | 
					:manmanual: eizoctl Manual
 | 
				
			||||||
 | 
					:mansource: eizoctl 1.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Name
 | 
				
			||||||
 | 
					----
 | 
				
			||||||
 | 
					eizoctl - manual page for eizoctl 1.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Synopsis
 | 
				
			||||||
 | 
					--------
 | 
				
			||||||
 | 
					*eizoctl* [**--brightness** [+-]__BRIGHTNESS__] [**--input** __NAME__] [**--restart**]
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_simple_help() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					Usage: elksmart-comm [OPTION]... [COMMAND...]
 | 
				
			||||||
 | 
					Usage: elksmart-comm
 | 
				
			||||||
 | 
					Transmit or receive infrared commands.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  -d, --debug                 elksmart-comm will run in debug mode
 | 
				
			||||||
 | 
					 -f, --frequency HZ          frequency (38000 Hz by default)
 | 
				
			||||||
 | 
					   -n, --nec                   use the NEC transmission format
 | 
				
			||||||
 | 
					  -h, --help                  display this help and exit
 | 
				
			||||||
 | 
					 -V, --version               output version information and exit
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_simple_version() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					elksmart-comm (usb-drivers) dev
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_simple_out() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					elksmart-comm(1)
 | 
				
			||||||
 | 
					================
 | 
				
			||||||
 | 
					:doctype: manpage
 | 
				
			||||||
 | 
					:manmanual: elksmart-comm Manual
 | 
				
			||||||
 | 
					:mansource: usb-drivers dev
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Name
 | 
				
			||||||
 | 
					----
 | 
				
			||||||
 | 
					elksmart-comm - manual page for elksmart-comm dev
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Synopsis
 | 
				
			||||||
 | 
					--------
 | 
				
			||||||
 | 
					*elksmart-comm* [__OPTION__]... [__COMMAND__...]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*elksmart-comm*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Description
 | 
				
			||||||
 | 
					-----------
 | 
				
			||||||
 | 
					Transmit or receive infrared commands.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*-d*, **--debug**::
 | 
				
			||||||
 | 
						**elksmart-comm** will run in debug mode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*-f*, **--frequency** __HZ__::
 | 
				
			||||||
 | 
						frequency (38000 Hz by default)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*-n*, **--nec**::
 | 
				
			||||||
 | 
						use the NEC transmission format
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*-h*, **--help**::
 | 
				
			||||||
 | 
						display this help and exit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*-V*, **--version**::
 | 
				
			||||||
 | 
						output version information and exit
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_wild_help() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					Usage:
 | 
				
			||||||
 | 
					 wild [option]… <command>… — Be wild
 | 
				
			||||||
 | 
					What's happening?
 | 
				
			||||||
 | 
					  -f, --frequency hz-2-foo  frequency to --foo at
 | 
				
			||||||
 | 
					      --foo=bar
 | 
				
			||||||
 | 
					  Foobar.
 | 
				
			||||||
 | 
					  Boo far.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 Subsection:
 | 
				
			||||||
 | 
					  --help
 | 
				
			||||||
 | 
					  --version
 | 
				
			||||||
 | 
					  Oh my.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Major section:
 | 
				
			||||||
 | 
					And now for something completely different.
 | 
				
			||||||
 | 
					  Very wild
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_wild_version() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					wild 1
 | 
				
			||||||
 | 
					Copies left and right.
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_wild_out() {
 | 
				
			||||||
 | 
					cat <<'END'
 | 
				
			||||||
 | 
					wild(1)
 | 
				
			||||||
 | 
					=======
 | 
				
			||||||
 | 
					:doctype: manpage
 | 
				
			||||||
 | 
					:manmanual: wild Manual
 | 
				
			||||||
 | 
					:mansource: wild 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Name
 | 
				
			||||||
 | 
					----
 | 
				
			||||||
 | 
					wild - manual page for wild 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Synopsis
 | 
				
			||||||
 | 
					--------
 | 
				
			||||||
 | 
					*wild* [__option__]... <__command__>...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Description
 | 
				
			||||||
 | 
					-----------
 | 
				
			||||||
 | 
					Be **wild**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					What's happening?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*-f*, **--frequency** __hz-2-foo__::
 | 
				
			||||||
 | 
						frequency to **--foo** at
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*--foo*=__bar__::
 | 
				
			||||||
 | 
						Foobar.
 | 
				
			||||||
 | 
						Boo far.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Subsection
 | 
				
			||||||
 | 
					~~~~~~~~~~
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*--help*::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*--version*::
 | 
				
			||||||
 | 
						Oh my.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Major section
 | 
				
			||||||
 | 
					-------------
 | 
				
			||||||
 | 
					And now for something completely different.
 | 
				
			||||||
 | 
					  Very wild
 | 
				
			||||||
 | 
					END
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					run() {
 | 
				
			||||||
 | 
						echo "-- help2adoc/$1"
 | 
				
			||||||
 | 
						local selfquoted=$(echo "$self" | sed 's/\\/&&/g')
 | 
				
			||||||
 | 
						local output=$(TEST=$1 awk -f "$help2adoc" -v Target="$selfquoted")
 | 
				
			||||||
 | 
						local expect="$($1_out)"
 | 
				
			||||||
 | 
						if [ "$output" = "$expect" ]
 | 
				
			||||||
 | 
						then return
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						echo "== Expected"
 | 
				
			||||||
 | 
						sed 's/^/   /' <<-END
 | 
				
			||||||
 | 
						$expect
 | 
				
			||||||
 | 
						END
 | 
				
			||||||
 | 
						echo "== Received"
 | 
				
			||||||
 | 
						sed 's/^/   /' <<-END
 | 
				
			||||||
 | 
						$output
 | 
				
			||||||
 | 
						END
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [ -z "$TEST" ]
 | 
				
			||||||
 | 
					then
 | 
				
			||||||
 | 
						run test_oneline
 | 
				
			||||||
 | 
						run test_simple
 | 
				
			||||||
 | 
						run test_wild
 | 
				
			||||||
 | 
						echo "-- OK"
 | 
				
			||||||
 | 
					elif [ "$1" = "--help" ]
 | 
				
			||||||
 | 
					then ${TEST}_help
 | 
				
			||||||
 | 
					elif [ "$1" = "--version" ]
 | 
				
			||||||
 | 
					then ${TEST}_version
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
						echo "Wrong usage"
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
@@ -636,7 +636,7 @@ test_config_validate_nonnegative
 | 
				
			|||||||
	return false;
 | 
						return false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct config_schema g_config_test[] =
 | 
					static const struct config_schema g_config_test[] =
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	{ .name      = "foo",
 | 
						{ .name      = "foo",
 | 
				
			||||||
	  .comment   = "baz",
 | 
						  .comment   = "baz",
 | 
				
			||||||
@@ -647,7 +647,7 @@ static struct config_schema g_config_test[] =
 | 
				
			|||||||
	  .type      = CONFIG_ITEM_INTEGER,
 | 
						  .type      = CONFIG_ITEM_INTEGER,
 | 
				
			||||||
	  .validate  = test_config_validate_nonnegative,
 | 
						  .validate  = test_config_validate_nonnegative,
 | 
				
			||||||
	  .default_  = "1" },
 | 
						  .default_  = "1" },
 | 
				
			||||||
	{ .name      = "foobar",
 | 
						{ .name      = "123",
 | 
				
			||||||
	  .type      = CONFIG_ITEM_STRING,
 | 
						  .type      = CONFIG_ITEM_STRING,
 | 
				
			||||||
	  .default_  = "\"qux\\x01`\" \"\"`a`" },
 | 
						  .default_  = "\"qux\\x01`\" \"\"`a`" },
 | 
				
			||||||
	{}
 | 
						{}
 | 
				
			||||||
@@ -676,10 +676,11 @@ test_config (void)
 | 
				
			|||||||
	config_item_destroy (invalid);
 | 
						config_item_destroy (invalid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hard_assert (!strcmp ("qux\001`a",
 | 
						hard_assert (!strcmp ("qux\001`a",
 | 
				
			||||||
		config_item_get (config.root, "top.foobar", NULL)->value.string.str));
 | 
							config_item_get (config.root, "top.123", NULL)->value.string.str));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct str s = str_make ();
 | 
						struct str s = str_make ();
 | 
				
			||||||
	config_item_write (config.root, true, &s);
 | 
						config_item_write (config.root, true, &s);
 | 
				
			||||||
 | 
						print_debug ("%s", s.str);
 | 
				
			||||||
	struct config_item *parsed = config_item_parse (s.str, s.len, false, NULL);
 | 
						struct config_item *parsed = config_item_parse (s.str, s.len, false, NULL);
 | 
				
			||||||
	hard_assert (parsed);
 | 
						hard_assert (parsed);
 | 
				
			||||||
	config_item_destroy (parsed);
 | 
						config_item_destroy (parsed);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -53,6 +53,10 @@ test_ser_deser_free (void)
 | 
				
			|||||||
			u->others.bar = str_make ();
 | 
								u->others.bar = str_make ();
 | 
				
			||||||
			for (int i = rand () % 0x30; i > 0; i--)
 | 
								for (int i = rand () % 0x30; i > 0; i--)
 | 
				
			||||||
				str_append_c (&u->others.bar, 0x30 + i);
 | 
									str_append_c (&u->others.bar, 0x30 + i);
 | 
				
			||||||
 | 
								u->others.baz_len = rand () % 0x30;
 | 
				
			||||||
 | 
								u->others.baz = xcalloc (1, u->others.baz_len);
 | 
				
			||||||
 | 
								for (uint32_t i = 0; i < u->others.baz_len; i++)
 | 
				
			||||||
 | 
									u->others.baz[i] = 0x30 + i;
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case 2:
 | 
							case 2:
 | 
				
			||||||
			u->tag = PROTO_GEN_ENUM_NOTHING;
 | 
								u->tag = PROTO_GEN_ENUM_NOTHING;
 | 
				
			||||||
@@ -62,6 +66,8 @@ test_ser_deser_free (void)
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						a.o.tag = PROTO_GEN_ENUM_NOTHING;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct str buf = str_make ();
 | 
						struct str buf = str_make ();
 | 
				
			||||||
	hard_assert (proto_gen_struct_serialize (&a, &buf));
 | 
						hard_assert (proto_gen_struct_serialize (&a, &buf));
 | 
				
			||||||
	struct msg_unpacker r = msg_unpacker_make (buf.str, buf.len);
 | 
						struct msg_unpacker r = msg_unpacker_make (buf.str, buf.len);
 | 
				
			||||||
@@ -92,6 +98,9 @@ test_ser_deser_free (void)
 | 
				
			|||||||
			hard_assert (ua->others.bar.len == ub->others.bar.len);
 | 
								hard_assert (ua->others.bar.len == ub->others.bar.len);
 | 
				
			||||||
			hard_assert (!memcmp (ua->others.bar.str, ub->others.bar.str,
 | 
								hard_assert (!memcmp (ua->others.bar.str, ub->others.bar.str,
 | 
				
			||||||
				ua->others.bar.len));
 | 
									ua->others.bar.len));
 | 
				
			||||||
 | 
								hard_assert (ua->others.baz_len == ub->others.baz_len);
 | 
				
			||||||
 | 
								hard_assert (!memcmp (ua->others.baz, ub->others.baz,
 | 
				
			||||||
 | 
									ua->others.baz_len));
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case PROTO_GEN_ENUM_NOTHING:
 | 
							case PROTO_GEN_ENUM_NOTHING:
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
@@ -100,6 +109,8 @@ test_ser_deser_free (void)
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hard_assert (a.o.tag == b.o.tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Emulate partially deserialized data to test disposal of that.
 | 
						// Emulate partially deserialized data to test disposal of that.
 | 
				
			||||||
	for (size_t i = b.u_len - CASES; i < b.u_len; i++)
 | 
						for (size_t i = b.u_len - CASES; i < b.u_len; i++)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										132
									
								
								tests/lxdrgen.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								tests/lxdrgen.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,132 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * tests/lxdrgen.cpp
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (c) 2023, 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 "lxdrgen.lxdr.cpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <cstdlib>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					hard_assert (bool condition, const char *description)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (!condition)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							fprintf (stderr, "assertion failed: %s\n", description);
 | 
				
			||||||
 | 
							abort ();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define hard_assert(condition) hard_assert (condition, #condition)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int
 | 
				
			||||||
 | 
					main (int argc, char *argv[])
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						hard_assert (ProtoGen::VERSION == 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						enum { CASES = 3 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ProtoGen::Struct a = {}, b = {};
 | 
				
			||||||
 | 
						a.u.resize (CASES + rand () % 100);
 | 
				
			||||||
 | 
						for (size_t i = 0; i < a.u.size (); i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							std::unique_ptr<ProtoGen::Union> &u = a.u[i];
 | 
				
			||||||
 | 
							switch (i % CASES)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
							case 0:
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								auto numbers = new ProtoGen::Union_Numbers ();
 | 
				
			||||||
 | 
								numbers->a = rand () % UINT8_MAX;
 | 
				
			||||||
 | 
								numbers->b = rand () % UINT16_MAX;
 | 
				
			||||||
 | 
								numbers->c = rand () % UINT32_MAX;
 | 
				
			||||||
 | 
								numbers->d = rand () % UINT64_MAX;
 | 
				
			||||||
 | 
								numbers->e = rand () % UINT8_MAX;
 | 
				
			||||||
 | 
								numbers->f = rand () % UINT16_MAX;
 | 
				
			||||||
 | 
								numbers->g = rand () % UINT32_MAX;
 | 
				
			||||||
 | 
								numbers->h = rand () % UINT64_MAX;
 | 
				
			||||||
 | 
								u.reset (numbers);
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							case 1:
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								auto others = new ProtoGen::Union_Others ();
 | 
				
			||||||
 | 
								others->foo = rand () % 2;
 | 
				
			||||||
 | 
								for (int i = rand () % 0x30; i > 0; i--)
 | 
				
			||||||
 | 
									others->bar += 0x30 + i;
 | 
				
			||||||
 | 
								for (int i = rand () % 0x30; i > 0; i--)
 | 
				
			||||||
 | 
									others->baz.push_back (0x30 + i);
 | 
				
			||||||
 | 
								u.reset (others);
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							case 2:
 | 
				
			||||||
 | 
								u.reset (new ProtoGen::Union_Nothing ());
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								hard_assert (!"unhandled case");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						a.o.reset (new ProtoGen::Onion_Nothing ());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						LibertyXDR::Writer buf;
 | 
				
			||||||
 | 
						hard_assert (a.serialize (buf));
 | 
				
			||||||
 | 
						LibertyXDR::Reader r;
 | 
				
			||||||
 | 
						r.data = buf.data.data ();
 | 
				
			||||||
 | 
						r.length = buf.data.size ();
 | 
				
			||||||
 | 
						hard_assert (b.deserialize (r));
 | 
				
			||||||
 | 
						hard_assert (!r.length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hard_assert (a.u.size () == b.u.size ());
 | 
				
			||||||
 | 
						for (size_t i = 0; i < a.u.size (); i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ProtoGen::Union *ua = a.u[i].get ();
 | 
				
			||||||
 | 
							ProtoGen::Union *ub = b.u[i].get ();
 | 
				
			||||||
 | 
							hard_assert (ua->tag == ub->tag);
 | 
				
			||||||
 | 
							switch (ua->tag)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
							case ProtoGen::Enum::NUMBERS:
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								auto a = dynamic_cast<ProtoGen::Union_Numbers *> (ua);
 | 
				
			||||||
 | 
								auto b = dynamic_cast<ProtoGen::Union_Numbers *> (ub);
 | 
				
			||||||
 | 
								hard_assert (a->a == b->a);
 | 
				
			||||||
 | 
								hard_assert (a->b == b->b);
 | 
				
			||||||
 | 
								hard_assert (a->c == b->c);
 | 
				
			||||||
 | 
								hard_assert (a->d == b->d);
 | 
				
			||||||
 | 
								hard_assert (a->e == b->e);
 | 
				
			||||||
 | 
								hard_assert (a->f == b->f);
 | 
				
			||||||
 | 
								hard_assert (a->g == b->g);
 | 
				
			||||||
 | 
								hard_assert (a->h == b->h);
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							case ProtoGen::Enum::OTHERS:
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								auto a = dynamic_cast<ProtoGen::Union_Others *> (ua);
 | 
				
			||||||
 | 
								auto b = dynamic_cast<ProtoGen::Union_Others *> (ub);
 | 
				
			||||||
 | 
								hard_assert (a->foo == b->foo);
 | 
				
			||||||
 | 
								hard_assert (a->bar == b->bar);
 | 
				
			||||||
 | 
								hard_assert (a->baz == b->baz);
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							case ProtoGen::Enum::NOTHING:
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								hard_assert (!"unexpected case");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hard_assert (a.o->tag == b.o->tag);
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -17,7 +17,15 @@ struct Struct {
 | 
				
			|||||||
	case OTHERS:
 | 
						case OTHERS:
 | 
				
			||||||
		bool foo;
 | 
							bool foo;
 | 
				
			||||||
		string bar;
 | 
							string bar;
 | 
				
			||||||
 | 
							u8 baz<>;
 | 
				
			||||||
	case NOTHING:
 | 
						case NOTHING:
 | 
				
			||||||
		void;
 | 
							void;
 | 
				
			||||||
	} u<>;
 | 
						} u<>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						union Onion switch (Enum tag) {
 | 
				
			||||||
 | 
						case NOTHING:
 | 
				
			||||||
 | 
							void;
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							void;
 | 
				
			||||||
 | 
						} o;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										67
									
								
								tests/xdg.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								tests/xdg.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * tests/xdg.c
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (c) 2024, 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 "../liberty-xdg.c"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const char file[] =
 | 
				
			||||||
 | 
						"# This only tests the happy paths\n"
 | 
				
			||||||
 | 
						"[Desktop Entry]\n"
 | 
				
			||||||
 | 
						"Version = 1.0\n"
 | 
				
			||||||
 | 
						"Name=\\s\\n\\t\\r\\\\\n"
 | 
				
			||||||
 | 
						"Name[fr]=Nom\n"
 | 
				
			||||||
 | 
						"Hidden=true\n"
 | 
				
			||||||
 | 
						"Categories=Utility;TextEditor;\n"
 | 
				
			||||||
 | 
						"Number=42";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					test_desktop_file (void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct desktop_file entry = desktop_file_make (file, sizeof file - 1);
 | 
				
			||||||
 | 
						const char *group = "Desktop Entry";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *value = desktop_file_get_string (&entry, group, "Version");
 | 
				
			||||||
 | 
						hard_assert (!strcmp (value, "1.0"));
 | 
				
			||||||
 | 
						cstr_set (&value, desktop_file_get_string (&entry, group, "Name"));
 | 
				
			||||||
 | 
						hard_assert (!strcmp (value, " \n\t\r\\"));
 | 
				
			||||||
 | 
						free (value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hard_assert (desktop_file_get_bool (&entry, group, "Hidden"));
 | 
				
			||||||
 | 
						struct strv values = desktop_file_get_stringv (&entry, group, "Categories");
 | 
				
			||||||
 | 
						hard_assert (values.len == 2);
 | 
				
			||||||
 | 
						hard_assert (!strcmp (values.vector[0], "Utility"));
 | 
				
			||||||
 | 
						hard_assert (!strcmp (values.vector[1], "TextEditor"));
 | 
				
			||||||
 | 
						strv_free (&values);
 | 
				
			||||||
 | 
						hard_assert (desktop_file_get_integer (&entry, group, "Number") == 42);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						desktop_file_free (&entry);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int
 | 
				
			||||||
 | 
					main (int argc, char *argv[])
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct test test;
 | 
				
			||||||
 | 
						test_init (&test, argc, argv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						test_add_simple (&test, "/desktop-file", NULL, test_desktop_file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return test_run (&test);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
# asciiman.awk: simplified AsciiDoc to manual page converter
 | 
					# asciiman.awk: simplified AsciiDoc to manual page converter
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# Copyright (c) 2022 - 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
					# Copyright (c) 2022 - 2024, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
# SPDX-License-Identifier: 0BSD
 | 
					# SPDX-License-Identifier: 0BSD
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# This is not intended to produce great output, merely useful output.
 | 
					# This is not intended to produce great output, merely useful output.
 | 
				
			||||||
@@ -149,6 +149,11 @@ function format(line,    v) {
 | 
				
			|||||||
		} else if (match(line, /^[*][^*]+[*]/) &&
 | 
							} else if (match(line, /^[*][^*]+[*]/) &&
 | 
				
			||||||
			substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
 | 
								substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
 | 
				
			||||||
			v = v "\\fB" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
 | 
								v = v "\\fB" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
 | 
				
			||||||
 | 
							} else if (match(line, /^`[^`]+`/) &&
 | 
				
			||||||
 | 
								substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
 | 
				
			||||||
 | 
								# Manual pages are usually already rendered in monospace;
 | 
				
			||||||
 | 
								# follow others, and render this in boldface.
 | 
				
			||||||
 | 
								v = v "\\fB" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			v = v substr(line, 1, 1)
 | 
								v = v substr(line, 1, 1)
 | 
				
			||||||
			line = substr(line, 2)
 | 
								line = substr(line, 2)
 | 
				
			||||||
@@ -195,7 +200,7 @@ function inline(line) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Returns 1 iff the left-over $0 should be processed further.
 | 
					# Returns 1 iff the left-over $0 should be processed further.
 | 
				
			||||||
function process(firstline,     posattrs, namedattrs) {
 | 
					function process(firstline,     posattrs, namedattrs, ok) {
 | 
				
			||||||
	if (readattribute(firstline))
 | 
						if (readattribute(firstline))
 | 
				
			||||||
		return 0
 | 
							return 0
 | 
				
			||||||
	if (getline <= 0) {
 | 
						if (getline <= 0) {
 | 
				
			||||||
@@ -226,6 +231,13 @@ function process(firstline,     posattrs, namedattrs) {
 | 
				
			|||||||
		return 0
 | 
							return 0
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (firstline ~ /^--$/) {
 | 
				
			||||||
 | 
							flushspace()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							# For now, recognize, but do not process open block delimiters.
 | 
				
			||||||
 | 
							InOpenBlock = !InOpenBlock
 | 
				
			||||||
 | 
							return 1
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if (firstline ~ /^(-{4,}|[.]{4,})$/) {
 | 
						if (firstline ~ /^(-{4,}|[.]{4,})$/) {
 | 
				
			||||||
		flushspace()
 | 
							flushspace()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -269,9 +281,11 @@ function process(firstline,     posattrs, namedattrs) {
 | 
				
			|||||||
		while ($0) {
 | 
							while ($0) {
 | 
				
			||||||
			sub(/^[[:space:]]+/, "")
 | 
								sub(/^[[:space:]]+/, "")
 | 
				
			||||||
			sub(/^[+]$/, "")
 | 
								sub(/^[+]$/, "")
 | 
				
			||||||
			if (!process($0) && getline <= 0)
 | 
								if (!process($0) && (ok = getline) <= 0) {
 | 
				
			||||||
				fatal("unexpected EOF")
 | 
									if (ok < 0)
 | 
				
			||||||
			if (match($0, /^[[:space:]]*[*][[:space:]]+/))
 | 
										fatal("getline failed")
 | 
				
			||||||
 | 
									$0 = ""
 | 
				
			||||||
 | 
								} else if (match($0, /^[[:space:]]*[*][[:space:]]+/))
 | 
				
			||||||
				break
 | 
									break
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		print ".RE"
 | 
							print ".RE"
 | 
				
			||||||
@@ -306,9 +320,11 @@ function process(firstline,     posattrs, namedattrs) {
 | 
				
			|||||||
		while ($0) {
 | 
							while ($0) {
 | 
				
			||||||
			sub(/^[[:space:]]+/, "")
 | 
								sub(/^[[:space:]]+/, "")
 | 
				
			||||||
			sub(/^[+]$/, "")
 | 
								sub(/^[+]$/, "")
 | 
				
			||||||
			if (!process($0) && getline <= 0)
 | 
								if (!process($0) && (ok = getline) <= 0) {
 | 
				
			||||||
				fatal("unexpected EOF")
 | 
									if (ok < 0)
 | 
				
			||||||
			if (match($0, /::$/))
 | 
										fatal("getline failed")
 | 
				
			||||||
 | 
									$0 = ""
 | 
				
			||||||
 | 
								} else if (match($0, /::$/))
 | 
				
			||||||
				break
 | 
									break
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		print ".RE"
 | 
							print ".RE"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -75,7 +75,7 @@ function line_ending() {
 | 
				
			|||||||
# it doesn't seem to be worth the effort.
 | 
					# it doesn't seem to be worth the effort.
 | 
				
			||||||
function expand(s,    v) {
 | 
					function expand(s,    v) {
 | 
				
			||||||
	v = s
 | 
						v = s
 | 
				
			||||||
	while (match(v, /\\*[$](|ENV|CACHE)[{]/)) {
 | 
						while (match(v, /\\*[$](ENV|CACHE)?[{]/)) {
 | 
				
			||||||
		if (index(substr(v, RSTART), "$") % 2 != 0) {
 | 
							if (index(substr(v, RSTART), "$") % 2 != 0) {
 | 
				
			||||||
			warning("variable expansion is not supported: " s)
 | 
								warning("variable expansion is not supported: " s)
 | 
				
			||||||
			return s
 | 
								return s
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										234
									
								
								tools/help2adoc.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										234
									
								
								tools/help2adoc.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,234 @@
 | 
				
			|||||||
 | 
					# help2adoc.awk: convert --version/--help to AsciiDoc manual pages
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Copyright (c) 2024, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					# SPDX-License-Identifier: 0BSD
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Usage: awk -f help2adoc.awk -v Target=cat
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# This is not intended to produce great output, merely useful output,
 | 
				
			||||||
 | 
					# if only because there is no real standard of what the input should look like.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# The only target that needs to work is liberty's own opt_handler.
 | 
				
			||||||
 | 
					# The expected input format is roughly that of GNU utilites.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function fatal(message) {
 | 
				
			||||||
 | 
						print "// " message
 | 
				
			||||||
 | 
						print "fatal error: " message > "/dev/stderr"
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# The input model of this script is that function take the next line on $0,
 | 
				
			||||||
 | 
					# read further lines as necessary, and leave the next line in $0 again.
 | 
				
			||||||
 | 
					function readline(    ok) {
 | 
				
			||||||
 | 
						if ((ok = (Command | getline)) < 0)
 | 
				
			||||||
 | 
							fatal("read error")
 | 
				
			||||||
 | 
						if (!ok)
 | 
				
			||||||
 | 
							exit
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function emboldenoptions(line) {
 | 
				
			||||||
 | 
						# -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
 | 
				
			||||||
 | 
						sub(/^-[^-=,[:space:]{[<]/, "*&*", line)
 | 
				
			||||||
 | 
						while (match(line, /[^-_[:alnum:]*'+]-[^-=,[:space:]{[<]/)) {
 | 
				
			||||||
 | 
							line = substr(line, 1, RSTART) \
 | 
				
			||||||
 | 
								"**" substr(line, RSTART + 1, RLENGTH - 1) "**" \
 | 
				
			||||||
 | 
								substr(line, RSTART + RLENGTH)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sub(/^--[-_[:alnum:]]+/, "*&*", line)
 | 
				
			||||||
 | 
						while (match(line, /[^-_[:alnum:]*'+]--[-_[:alnum:]]+/)) {
 | 
				
			||||||
 | 
							line = substr(line, 1, RSTART) \
 | 
				
			||||||
 | 
								"**" substr(line, RSTART + 1, RLENGTH - 1) "**" \
 | 
				
			||||||
 | 
								substr(line, RSTART + RLENGTH)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return line
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function formatinline(line,    programname, last, i) {
 | 
				
			||||||
 | 
						# Go the extra step of emboldening the program name at word boundaries.
 | 
				
			||||||
 | 
						programname = ProgramName
 | 
				
			||||||
 | 
						gsub(/[][\\.^$(){}|*+?]/, "\\\\&", programname)
 | 
				
			||||||
 | 
						if (match(line, "^" programname "[^-_[:alnum:]*'+/]")) {
 | 
				
			||||||
 | 
							line = "**" substr(line, RSTART, RLENGTH - 1) "**" \
 | 
				
			||||||
 | 
								substr(line, RSTART + RLENGTH - 1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						while (match(line, "[^-_[:alnum:]*'+/]" programname "[^-_[:alnum:]*'+/]")) {
 | 
				
			||||||
 | 
							line = substr(line, 1, RSTART) \
 | 
				
			||||||
 | 
								"**" substr(line, RSTART + 1, RLENGTH - 2) "**" \
 | 
				
			||||||
 | 
								substr(line, RSTART + RLENGTH - 1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (match(line, "[^-_[:alnum:]*'+/]" programname "$")) {
 | 
				
			||||||
 | 
							line = substr(line, 1, RSTART) \
 | 
				
			||||||
 | 
								"**" substr(line, RSTART + 1, RLENGTH - 1) "**"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return emboldenoptions(line)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function printusage(usage,    description) {
 | 
				
			||||||
 | 
						gsub(/…/, "...", usage)
 | 
				
			||||||
 | 
						gsub(/—|–/, "-", usage)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# --help output will more likely than not simply include argv[0],
 | 
				
			||||||
 | 
						# or perhaps program_invocation_short_name (not addressed here).
 | 
				
			||||||
 | 
						if (substr(usage, 1, length(Target) + 1) == Target " ")
 | 
				
			||||||
 | 
							usage = ProgramName substr(usage, length(Target) + 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# A lot of GNOME software includes the description here.
 | 
				
			||||||
 | 
						if (match(usage, / +- +/) && usage !~ / - [^[:alnum:]]/) {
 | 
				
			||||||
 | 
							description = substr(usage, RSTART + RLENGTH)
 | 
				
			||||||
 | 
							usage = substr(usage, 1, RSTART - 1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						while (match(usage, /[^-_[:alnum:]*'+.][[:alnum:]][-_[:alnum:]]+/)) {
 | 
				
			||||||
 | 
							usage = substr(usage, 1, RSTART) \
 | 
				
			||||||
 | 
								"__" substr(usage, RSTART + 1, RLENGTH - 1) "__" \
 | 
				
			||||||
 | 
								substr(usage, RSTART + RLENGTH)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sub(/^[^[:space:]]+/, "*&*", usage)
 | 
				
			||||||
 | 
						print emboldenoptions(usage)
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (description) {
 | 
				
			||||||
 | 
							flushsections()
 | 
				
			||||||
 | 
							print formatinline(description)
 | 
				
			||||||
 | 
							print ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# We're going with Setext headers, because that's what asciiman.awk supports.
 | 
				
			||||||
 | 
					function printheader(text, underline) {
 | 
				
			||||||
 | 
						print text
 | 
				
			||||||
 | 
						gsub(/./, underline, text)
 | 
				
			||||||
 | 
						print text
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BEGIN {
 | 
				
			||||||
 | 
						if (!Target)
 | 
				
			||||||
 | 
							fatal("missing Target")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						TargetQuoted = Target
 | 
				
			||||||
 | 
						gsub(/'/, "'\\''", TargetQuoted)
 | 
				
			||||||
 | 
						TargetQuoted = "'" TargetQuoted "'"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# Remaining --version lines could be about copyright (GNU),
 | 
				
			||||||
 | 
						# or something else entirely.
 | 
				
			||||||
 | 
						Command = TargetQuoted " --version"
 | 
				
			||||||
 | 
						if ((Command | getline) > 0) {
 | 
				
			||||||
 | 
							# GNU --version output can place the package name in parentheses.
 | 
				
			||||||
 | 
							Package = $0
 | 
				
			||||||
 | 
							if (match($0, /[[:space:]][(][^)]*[)]/)) {
 | 
				
			||||||
 | 
								Package = substr($0, RSTART + 2, RLENGTH - 3) \
 | 
				
			||||||
 | 
									substr($0, RSTART + RLENGTH)
 | 
				
			||||||
 | 
								sub(/[[:space:]]+[(][^)]*[)]/, "")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							Version = $0
 | 
				
			||||||
 | 
							sub(/[[:space:]]+[^[:space:]]+$/, "")
 | 
				
			||||||
 | 
							Name = $0
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fatal("failed to get --version output")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (Name !~ /[[:space:]]/)
 | 
				
			||||||
 | 
							ProgramName = Name
 | 
				
			||||||
 | 
						else if (match(Target, /[^\/]+$/))
 | 
				
			||||||
 | 
							ProgramName = substr(Target, RSTART, RLENGTH)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						printheader(ProgramName "(1)", "=")
 | 
				
			||||||
 | 
						print ":doctype: manpage"
 | 
				
			||||||
 | 
						print ":manmanual: " Name " Manual"
 | 
				
			||||||
 | 
						print ":mansource: " Package
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						printheader("Name", "-")
 | 
				
			||||||
 | 
						print ProgramName " - manual page for " Version
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						close(Command)
 | 
				
			||||||
 | 
						Command = TargetQuoted " --help"
 | 
				
			||||||
 | 
						if ((Command | getline) <= 0)
 | 
				
			||||||
 | 
							fatal("failed to get --help output")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						NextSection = "Description"
 | 
				
			||||||
 | 
						NextSubsection = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# The SYNOPSIS section is mandatory, so just put it there.
 | 
				
			||||||
 | 
						printheader("Synopsis", "-")
 | 
				
			||||||
 | 
						while (1) {
 | 
				
			||||||
 | 
							if (match($0, /^[Uu]sage:[[:space:]]*/)) {
 | 
				
			||||||
 | 
								if (($0 = substr($0, RSTART + RLENGTH)))
 | 
				
			||||||
 | 
									printusage($0)
 | 
				
			||||||
 | 
							} else if (match($0, /^[[:space:]]+/) && !/^[[:space:]]*-/) {
 | 
				
			||||||
 | 
								if (($0 = substr($0, RSTART + RLENGTH)))
 | 
				
			||||||
 | 
									printusage($0)
 | 
				
			||||||
 | 
							} else if ($0) {
 | 
				
			||||||
 | 
								break
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							readline()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						while (1) {
 | 
				
			||||||
 | 
							if (match($0, /^[[:alpha:]][-[:alnum:][:space:]]+:$/)) {
 | 
				
			||||||
 | 
								# We don't flush sections here,
 | 
				
			||||||
 | 
								# so that we don't unnecessarily enforce DESCRIPTION first.
 | 
				
			||||||
 | 
								NextSection = substr($0, RSTART, RLENGTH - 1)
 | 
				
			||||||
 | 
							} else if (match($0, /^ [[:alpha:]][-[:alnum:][:space:]]+:$/)) {
 | 
				
			||||||
 | 
								flushsections()
 | 
				
			||||||
 | 
								NextSubsection = substr($0, RSTART + 1, RLENGTH - 2)
 | 
				
			||||||
 | 
							} else if (match($0, /^ +-/)) {
 | 
				
			||||||
 | 
								flushsections()
 | 
				
			||||||
 | 
								parseoption(substr($0, RSTART + RLENGTH - 1))
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							} else if ($0) {
 | 
				
			||||||
 | 
								flushsections()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								# That will be probably interpreted as a literal block.
 | 
				
			||||||
 | 
								if (!/^[[:space:]]/)
 | 
				
			||||||
 | 
									$0 = formatinline($0)
 | 
				
			||||||
 | 
								print
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								print
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							readline()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function flushsections() {
 | 
				
			||||||
 | 
						if (NextSection) {
 | 
				
			||||||
 | 
							print ""
 | 
				
			||||||
 | 
							printheader(NextSection, "-")
 | 
				
			||||||
 | 
							NextSection = ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (NextSubsection) {
 | 
				
			||||||
 | 
							print ""
 | 
				
			||||||
 | 
							printheader(NextSubsection, "~")
 | 
				
			||||||
 | 
							NextSubsection = ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function parseoption(line,    usage) {
 | 
				
			||||||
 | 
						# Often enough you will see it separated with only one space,
 | 
				
			||||||
 | 
						# which will simply not work for us.
 | 
				
			||||||
 | 
						if (match(line, /[[:space:]]{2,}/)) {
 | 
				
			||||||
 | 
							usage = substr(line, 1, RSTART - 1)
 | 
				
			||||||
 | 
							line = substr(line, RSTART + RLENGTH)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							usage = line
 | 
				
			||||||
 | 
							line = ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						usage = emboldenoptions(usage)
 | 
				
			||||||
 | 
						while (match(usage, /[=<, ][[:alnum:]][-_[:alnum:]]*/)) {
 | 
				
			||||||
 | 
							usage = substr(usage, 1, RSTART) \
 | 
				
			||||||
 | 
								"__" substr(usage, RSTART + 1, RLENGTH - 1) "__" \
 | 
				
			||||||
 | 
								substr(usage, RSTART + RLENGTH)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print usage "::"
 | 
				
			||||||
 | 
						if (line)
 | 
				
			||||||
 | 
							print "\t" formatinline(line)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						readline()
 | 
				
			||||||
 | 
						while (match($0, /^ +[^-[:space:]]|^ {7,}./)) {
 | 
				
			||||||
 | 
							print "\t" formatinline(substr($0, RSTART + RLENGTH - 1))
 | 
				
			||||||
 | 
							readline()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -222,7 +222,7 @@ function codegen_struct(name, cg,    ctype, funcname) {
 | 
				
			|||||||
		delete cg[i]
 | 
							delete cg[i]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union_tag(d, cg) {
 | 
					function codegen_union_tag(name, d, cg) {
 | 
				
			||||||
	cg["tagtype"] = d["type"]
 | 
						cg["tagtype"] = d["type"]
 | 
				
			||||||
	cg["tagname"] = d["name"]
 | 
						cg["tagname"] = d["name"]
 | 
				
			||||||
	append(cg, "fields", "\t" CodegenCType[d["type"]] " " d["name"] ";\n")
 | 
						append(cg, "fields", "\t" CodegenCType[d["type"]] " " d["name"] ";\n")
 | 
				
			||||||
@@ -259,7 +259,7 @@ function codegen_union_struct( \
 | 
				
			|||||||
		"\t\tbreak;\n")
 | 
							"\t\tbreak;\n")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    f, ctype, funcname) {
 | 
					function codegen_union(name, cg, exhaustive,    f, ctype, funcname) {
 | 
				
			||||||
	ctype = "union " PrefixLower cameltosnake(name)
 | 
						ctype = "union " PrefixLower cameltosnake(name)
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
	print ctype " {"
 | 
						print ctype " {"
 | 
				
			||||||
@@ -281,7 +281,7 @@ function codegen_union(name, cg,    f, ctype, funcname) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		CodegenDispose[name] = "\t" funcname "(&%s);\n"
 | 
							CodegenDispose[name] = "\t" funcname "(&%s);\n"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (cg["serialize"]) {
 | 
						{
 | 
				
			||||||
		funcname = PrefixLower cameltosnake(name) "_serialize"
 | 
							funcname = PrefixLower cameltosnake(name) "_serialize"
 | 
				
			||||||
		print ""
 | 
							print ""
 | 
				
			||||||
		print "static bool\n" \
 | 
							print "static bool\n" \
 | 
				
			||||||
@@ -299,7 +299,7 @@ function codegen_union(name, cg,    f, ctype, funcname) {
 | 
				
			|||||||
		CodegenSerialize[name] = "\tif (!" funcname "(&%s, w))\n" \
 | 
							CodegenSerialize[name] = "\tif (!" funcname "(&%s, w))\n" \
 | 
				
			||||||
			"\t\treturn false;\n"
 | 
								"\t\treturn false;\n"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (cg["deserialize"]) {
 | 
						{
 | 
				
			||||||
		funcname = PrefixLower cameltosnake(name) "_deserialize"
 | 
							funcname = PrefixLower cameltosnake(name) "_deserialize"
 | 
				
			||||||
		print ""
 | 
							print ""
 | 
				
			||||||
		print "static bool\n" \
 | 
							print "static bool\n" \
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										67
									
								
								tools/lxdrgen-cpp-posix.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								tools/lxdrgen-cpp-posix.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
				
			|||||||
 | 
					// lxdrgen-cpp-posix.cpp: POSIX support code for lxdrgen-cpp.awk.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: 0BSD
 | 
				
			||||||
 | 
					#include <iconv.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <cstdint>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Various BSD derivatives may have a problem here.
 | 
				
			||||||
 | 
					// Linux defines __STDC_ISO_10646__, but also supports "WCHAR_T".
 | 
				
			||||||
 | 
					#ifdef APPLE
 | 
				
			||||||
 | 
					#define ICONV_WCHAR "UTF-32"
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					#define ICONV_WCHAR "WCHAR_T"
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace LibertyXDR {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool utf8_to_wstring(const uint8_t *utf8, size_t length, std::wstring &wide) {
 | 
				
			||||||
 | 
						iconv_t conv = iconv_open(ICONV_WCHAR, "UTF-8");
 | 
				
			||||||
 | 
						if (conv == (iconv_t) -1)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						wchar_t buffer[1024] = {};
 | 
				
			||||||
 | 
						char *start = (char *) buffer, *out = start, *in = (char *) utf8;
 | 
				
			||||||
 | 
						size_t available = sizeof buffer;
 | 
				
			||||||
 | 
						wide.clear();
 | 
				
			||||||
 | 
						while (iconv(conv, &in, &length, &out, &available) == (size_t) -1) {
 | 
				
			||||||
 | 
							if (errno != E2BIG) {
 | 
				
			||||||
 | 
								iconv_close(conv);
 | 
				
			||||||
 | 
								return false;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							wide.append(buffer, (out - start) / sizeof *buffer);
 | 
				
			||||||
 | 
							out = start;
 | 
				
			||||||
 | 
							available = sizeof buffer;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						wide.append(buffer, (out - start) / sizeof *buffer);
 | 
				
			||||||
 | 
						iconv_close(conv);
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool wstring_to_utf8(const std::wstring &wide, std::string &utf8) {
 | 
				
			||||||
 | 
						iconv_t conv = iconv_open("UTF-8", ICONV_WCHAR);
 | 
				
			||||||
 | 
						if (conv == (iconv_t) -1)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char buffer[1024] = {}, *out = buffer, *in = (char *) wide.data();
 | 
				
			||||||
 | 
						size_t available = sizeof buffer, length = wide.size() * sizeof wide[0];
 | 
				
			||||||
 | 
						utf8.clear();
 | 
				
			||||||
 | 
						while (iconv(conv, &in, &length, &out, &available) == (size_t) -1) {
 | 
				
			||||||
 | 
							if (errno != E2BIG) {
 | 
				
			||||||
 | 
								iconv_close(conv);
 | 
				
			||||||
 | 
								return false;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							utf8.append(buffer, out - buffer);
 | 
				
			||||||
 | 
							out = buffer;
 | 
				
			||||||
 | 
							available = sizeof buffer;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						utf8.append(buffer, out - buffer);
 | 
				
			||||||
 | 
						iconv_close(conv);
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace LibertyXDR
 | 
				
			||||||
							
								
								
									
										23
									
								
								tools/lxdrgen-cpp-qt.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tools/lxdrgen-cpp-qt.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					// lxdrgen-cpp-qt.cpp: Qt support code for lxdrgen-cpp.awk.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Copyright (c) 2024, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: 0BSD
 | 
				
			||||||
 | 
					#include <QString>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace LibertyXDR {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool utf8_to_wstring(const uint8_t *utf8, size_t length, std::wstring &wide) {
 | 
				
			||||||
 | 
						QByteArrayView view(reinterpret_cast<const char *>(utf8), length);
 | 
				
			||||||
 | 
						if (!view.isValidUtf8())
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						wide = QString::fromUtf8(view).toStdWString();
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool wstring_to_utf8(const std::wstring &wide, std::string &utf8) {
 | 
				
			||||||
 | 
						utf8 = QString::fromStdWString(wide).toUtf8().toStdString();
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace LibertyXDR
 | 
				
			||||||
							
								
								
									
										47
									
								
								tools/lxdrgen-cpp-win32.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								tools/lxdrgen-cpp-win32.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					// lxdrgen-cpp-win32.cpp: Win32 support code for lxdrgen-cpp.awk.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: 0BSD
 | 
				
			||||||
 | 
					#include <windows.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <climits>
 | 
				
			||||||
 | 
					#include <cstdint>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace LibertyXDR {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool utf8_to_wstring(const uint8_t *utf8, size_t length, std::wstring &wide) {
 | 
				
			||||||
 | 
						wide.clear();
 | 
				
			||||||
 | 
						if (!length)
 | 
				
			||||||
 | 
							return true;
 | 
				
			||||||
 | 
						if (length > INT_MAX)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
 | 
				
			||||||
 | 
							(LPCCH) utf8, length, nullptr, 0);
 | 
				
			||||||
 | 
						if (size <= 0)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						wide.resize(size);
 | 
				
			||||||
 | 
						return !!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
 | 
				
			||||||
 | 
							(LPCCH) utf8, length, wide.data(), size);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool wstring_to_utf8(const std::wstring &wide, std::string &utf8) {
 | 
				
			||||||
 | 
						utf8.clear();
 | 
				
			||||||
 | 
						if (wide.empty())
 | 
				
			||||||
 | 
							return true;
 | 
				
			||||||
 | 
						if (wide.size() > INT_MAX)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
 | 
				
			||||||
 | 
							(LPCWCH) wide.data(), wide.size(), nullptr, 0, NULL, NULL);
 | 
				
			||||||
 | 
						if (size <= 0)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						utf8.resize(size);
 | 
				
			||||||
 | 
						return !!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
 | 
				
			||||||
 | 
							(LPCWCH) wide.data(), wide.size(), utf8.data(), size, NULL, NULL);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace LibertyXDR
 | 
				
			||||||
							
								
								
									
										350
									
								
								tools/lxdrgen-cpp.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										350
									
								
								tools/lxdrgen-cpp.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,350 @@
 | 
				
			|||||||
 | 
					# lxdrgen-cpp.awk: C++ backend for lxdrgen.awk.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# This backend is intended for Windows, it just happens to have a fallback
 | 
				
			||||||
 | 
					# that will probably work on Unices, of which we make use in tests.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					# SPDX-License-Identifier: 0BSD
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function define_internal(name, ctype) {
 | 
				
			||||||
 | 
						Types[name] = "internal"
 | 
				
			||||||
 | 
						CodegenCType[name] = ctype
 | 
				
			||||||
 | 
						CodegenSerialize[name] = \
 | 
				
			||||||
 | 
							"\tw.append(%s);\n"
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = \
 | 
				
			||||||
 | 
							"\tif (!r.read(%s))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function define_int(shortname, ctype) {
 | 
				
			||||||
 | 
						define_internal(shortname, ctype)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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", "std::wstring")
 | 
				
			||||||
 | 
						define_internal("bool", "bool")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSerialize["string"] = \
 | 
				
			||||||
 | 
							"\tif (!w.append(%s))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						print "// Code generated from " FILENAME ". DO NOT EDIT."
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "#include <cstdint>"
 | 
				
			||||||
 | 
						print "#include <memory>"
 | 
				
			||||||
 | 
						print "#include <string>"
 | 
				
			||||||
 | 
						print "#include <vector>"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "namespace LibertyXDR {"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "bool utf8_to_wstring("
 | 
				
			||||||
 | 
						print "\tconst uint8_t *utf8, size_t length, std::wstring &wide);"
 | 
				
			||||||
 | 
						print "bool wstring_to_utf8("
 | 
				
			||||||
 | 
						print "\tconst std::wstring &wide, std::string &utf8);"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "struct Reader {"
 | 
				
			||||||
 | 
						print "\tconst uint8_t *data = {};"
 | 
				
			||||||
 | 
						print "\tsize_t length = {};"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\ttemplate<typename T> bool read(T &number) {"
 | 
				
			||||||
 | 
						print "\t\tif (length < sizeof number)"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\t\tnumber = 0;"
 | 
				
			||||||
 | 
						print "\t\tfor (size_t i = 0; i < sizeof number; i++) {"
 | 
				
			||||||
 | 
						print "\t\t\tnumber = number << 8 | *data++;"
 | 
				
			||||||
 | 
						print "\t\t\tlength--;"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool read(bool &boolean) {"
 | 
				
			||||||
 | 
						print "\t\tuint8_t number = 0;"
 | 
				
			||||||
 | 
						print "\t\tif (!read(number))"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\t\tboolean = number != 0;"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool read(std::wstring &string) {"
 | 
				
			||||||
 | 
						print "\t\tuint32_t size = 0;"
 | 
				
			||||||
 | 
						print "\t\tif (!read(size) || size > length)"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print "\t\tif (!utf8_to_wstring(data, size, string))"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\t\tdata += size;"
 | 
				
			||||||
 | 
						print "\t\tlength -= size;"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool read(std::vector<uint8_t> &vector) {"
 | 
				
			||||||
 | 
						print "\t\tuint32_t size = 0;"
 | 
				
			||||||
 | 
						print "\t\tif (!read(size) || size > length)"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print "\t\tvector.assign(data, data + size);"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\t\tdata += size;"
 | 
				
			||||||
 | 
						print "\t\tlength -= size;"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print "};"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "struct Writer {"
 | 
				
			||||||
 | 
						print "\tstd::vector<uint8_t> data;"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\ttemplate<typename T> bool append(T number) {"
 | 
				
			||||||
 | 
						print "\t\tuint8_t buffer[sizeof number], *p = buffer + sizeof buffer;"
 | 
				
			||||||
 | 
						print "\t\twhile (p != buffer) {"
 | 
				
			||||||
 | 
						print "\t\t\t*--p = number;"
 | 
				
			||||||
 | 
						print "\t\t\tnumber >>= 8;"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\tdata.insert(data.end(), buffer, buffer + sizeof buffer);"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool append(int8_t number) {"
 | 
				
			||||||
 | 
						print "\t\tdata.push_back(number);"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool append(uint8_t number) {"
 | 
				
			||||||
 | 
						print "\t\tdata.push_back(number);"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool append(bool boolean) {"
 | 
				
			||||||
 | 
						print "\t\treturn append(uint8_t(boolean));"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool append(const std::wstring &string) {"
 | 
				
			||||||
 | 
						print "\t\tif (string.size() > UINT32_MAX)"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\t\tstd::string utf8;"
 | 
				
			||||||
 | 
						print "\t\tif (!wstring_to_utf8(string, utf8))"
 | 
				
			||||||
 | 
						print "\t\t\treturn false;"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\t\tappend<uint32_t>(utf8.size());"
 | 
				
			||||||
 | 
						print "\t\tdata.insert(data.end(), utf8.begin(), utf8.end());"
 | 
				
			||||||
 | 
						print "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print "};"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "} // namespace LibertyXDR"
 | 
				
			||||||
 | 
						print "namespace " PrefixCamel " {"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					END {
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "} // namespace " PrefixCamel
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_constant(name, value) {
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "enum { " name " = " value " };"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_enum_value(name, subname, value, cg) {
 | 
				
			||||||
 | 
						append(cg, "fields", "\t" subname " = " value ",\n")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_enum(name, cg) {
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "enum struct " name " : int8_t {"
 | 
				
			||||||
 | 
						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] = \
 | 
				
			||||||
 | 
							"\tw.append(static_cast<int8_t>(%s));\n"
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = \
 | 
				
			||||||
 | 
							"\t{\n" \
 | 
				
			||||||
 | 
							"\t\tint8_t v = 0;\n" \
 | 
				
			||||||
 | 
							"\t\tif (!r.read(v) || !v)\n" \
 | 
				
			||||||
 | 
							"\t\t\treturn false;\n" \
 | 
				
			||||||
 | 
							"\t\t%s = static_cast<" name ">(v);\n" \
 | 
				
			||||||
 | 
							"\t}\n"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenCType[name] = name
 | 
				
			||||||
 | 
						for (i in cg)
 | 
				
			||||||
 | 
							delete cg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Some identifiers do not pose a problem in C, but do in our C++.
 | 
				
			||||||
 | 
					function codegen_struct_sanitize(name) {
 | 
				
			||||||
 | 
						if (name ~ /^(serialize|deserialize)_*$/ ||
 | 
				
			||||||
 | 
							name ~ /^(catch|class|delete|except|finally|friend|new|operator)_*$/ ||
 | 
				
			||||||
 | 
							name ~ /^(private|protected|public|template|this|throw|try|virtual)_*$/)
 | 
				
			||||||
 | 
							return name "_"
 | 
				
			||||||
 | 
						return name
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_struct_tag(d, cg,    name, f) {
 | 
				
			||||||
 | 
						name = codegen_struct_sanitize(d["name"])
 | 
				
			||||||
 | 
						f = "this->" name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						append(cg, "serialize", sprintf(CodegenSerialize[d["type"]], f))
 | 
				
			||||||
 | 
						# Do not deserialize here, that would be out of order.
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_struct_field(d, cg,    name, f, serialize, deserialize) {
 | 
				
			||||||
 | 
						name = codegen_struct_sanitize(d["name"])
 | 
				
			||||||
 | 
						f = "this->" name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						serialize = CodegenSerialize[d["type"]]
 | 
				
			||||||
 | 
						deserialize = CodegenDeserialize[d["type"]]
 | 
				
			||||||
 | 
						if (!d["isarray"]) {
 | 
				
			||||||
 | 
							append(cg, "fields",
 | 
				
			||||||
 | 
								"\t" CodegenCType[d["type"]] " " name " = {};\n")
 | 
				
			||||||
 | 
							append(cg, "serialize", sprintf(serialize, f))
 | 
				
			||||||
 | 
							append(cg, "deserialize", sprintf(deserialize, f))
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						append(cg, "fields",
 | 
				
			||||||
 | 
							"\tstd::vector<" CodegenCType[d["type"]] "> " name ";\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# XXX: We should probably pedantically check for overflows.
 | 
				
			||||||
 | 
						append(cg, "serialize",
 | 
				
			||||||
 | 
							sprintf(CodegenSerialize["u32"], "uint32_t(" f ".size())") \
 | 
				
			||||||
 | 
							"\tfor (const auto &it : " f ")\n" \
 | 
				
			||||||
 | 
							indent(sprintf(serialize, "it")))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (d["type"] == "u8") {
 | 
				
			||||||
 | 
							append(cg, "deserialize",
 | 
				
			||||||
 | 
								"\tif (!r.read(" f "))\n" \
 | 
				
			||||||
 | 
								"\t\treturn false;\n")
 | 
				
			||||||
 | 
						} else if (deserialize) {
 | 
				
			||||||
 | 
							append(cg, "deserialize",
 | 
				
			||||||
 | 
								"\t{\n" \
 | 
				
			||||||
 | 
								"\t\tuint32_t size = 0;\n" \
 | 
				
			||||||
 | 
								indent(sprintf(CodegenDeserialize["u32"], "size")) \
 | 
				
			||||||
 | 
								"\t\t" f ".resize(size);\n" \
 | 
				
			||||||
 | 
								"\t}\n" \
 | 
				
			||||||
 | 
								"\tfor (auto &it : " f ")\n" \
 | 
				
			||||||
 | 
								indent(sprintf(deserialize, "it")))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_struct(name, cg) {
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "struct " name " {"
 | 
				
			||||||
 | 
						print cg["fields"]
 | 
				
			||||||
 | 
						print "\tbool serialize(LibertyXDR::Writer &w) const {"
 | 
				
			||||||
 | 
						print indent(cg["serialize"]) "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tbool deserialize([[maybe_unused]] LibertyXDR::Reader &r) {"
 | 
				
			||||||
 | 
						print indent(cg["deserialize"]) "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print "};"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSerialize[name] = "\tif (!%s->serialize(w))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = "\tif (!%s->deserialize(r))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenCType[name] = name
 | 
				
			||||||
 | 
						for (i in cg)
 | 
				
			||||||
 | 
							delete cg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_union_tag(name, d, cg,    tagname) {
 | 
				
			||||||
 | 
						cg["tagtype"] = d["type"]
 | 
				
			||||||
 | 
						cg["tagname"] = tagname = codegen_struct_sanitize(d["name"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "struct " name " {"
 | 
				
			||||||
 | 
						print "\t" CodegenCType[d["type"]] " " tagname " = {};"
 | 
				
			||||||
 | 
						print "\tvirtual ~" name "() = 0;"
 | 
				
			||||||
 | 
						print "\tvirtual bool serialize(LibertyXDR::Writer &w) const = 0;"
 | 
				
			||||||
 | 
						print "\tvirtual bool deserialize(LibertyXDR::Reader &r) = 0;"
 | 
				
			||||||
 | 
						print "};"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print name "::~" name "() {}"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_union_struct(name, casename, cg, scg,     structname) {
 | 
				
			||||||
 | 
						# And thus not all generated structs are present in Types.
 | 
				
			||||||
 | 
						structname = name "_" snaketocamel(casename)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "struct " structname " : virtual public " name " {"
 | 
				
			||||||
 | 
						print scg["fields"]
 | 
				
			||||||
 | 
						print "\t" structname "() {"
 | 
				
			||||||
 | 
						print "\t\tthis->" cg["tagname"] " = " \
 | 
				
			||||||
 | 
							CodegenCType[cg["tagtype"]] "::" casename ";"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tvirtual bool serialize(LibertyXDR::Writer &w) const {"
 | 
				
			||||||
 | 
						print indent(scg["serialize"]) "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tvirtual bool deserialize([[maybe_unused]] LibertyXDR::Reader &r) {"
 | 
				
			||||||
 | 
						print indent(scg["deserialize"]) "\t\treturn true;"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print "};"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						append(cg, "deserialize",
 | 
				
			||||||
 | 
							"\tcase " CodegenCType[cg["tagtype"]] "::" casename ":\n" \
 | 
				
			||||||
 | 
							"\t\treturn new " structname "();\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSerialize[structname] = "\tif (!%s->serialize(w))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
						CodegenDeserialize[structname] = "\tif (!%s->deserialize(r))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenCType[structname] = structname
 | 
				
			||||||
 | 
						for (i in scg)
 | 
				
			||||||
 | 
							delete scg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_union(name, cg, exhaustive,    ctype) {
 | 
				
			||||||
 | 
						CodegenSerialize[name] = "\tif (!%s->serialize(w))\n" \
 | 
				
			||||||
 | 
							"\t\treturn false;\n"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctype = "std::unique_ptr<" name ">"
 | 
				
			||||||
 | 
						if (cg["deserialize"]) {
 | 
				
			||||||
 | 
							print ""
 | 
				
			||||||
 | 
							print "static " name " *read" name "(" \
 | 
				
			||||||
 | 
								CodegenCType[cg["tagtype"]] " " cg["tagname"] ") {"
 | 
				
			||||||
 | 
							print "\tswitch (" cg["tagname"] ") {"
 | 
				
			||||||
 | 
							print cg["deserialize"] "\tdefault:"
 | 
				
			||||||
 | 
							print "\t\treturn nullptr;"
 | 
				
			||||||
 | 
							print "\t}"
 | 
				
			||||||
 | 
							print "}"
 | 
				
			||||||
 | 
							print ""
 | 
				
			||||||
 | 
							print "static " ctype " read" name "(LibertyXDR::Reader &r) {"
 | 
				
			||||||
 | 
							print "\tint8_t v = 0;"
 | 
				
			||||||
 | 
							print "\tif (!r.read(v) || !v)"
 | 
				
			||||||
 | 
							print "\t\treturn nullptr;"
 | 
				
			||||||
 | 
							print ""
 | 
				
			||||||
 | 
							print "\t" ctype " result(read" name "(static_cast<" \
 | 
				
			||||||
 | 
								CodegenCType[cg["tagtype"]] ">(v)));"
 | 
				
			||||||
 | 
							print "\tif (!result || !result->deserialize(r))"
 | 
				
			||||||
 | 
							print "\t\treturn nullptr;"
 | 
				
			||||||
 | 
							print "\treturn result;"
 | 
				
			||||||
 | 
							print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							CodegenDeserialize[name] = "\tif (!(%s = read" name "(r)))\n" \
 | 
				
			||||||
 | 
								"\t\treturn false;\n"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenCType[name] = ctype
 | 
				
			||||||
 | 
						for (i in cg)
 | 
				
			||||||
 | 
							delete cg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
# lxdrgen-go.awk: Go backend for lxdrgen.awk.
 | 
					# lxdrgen-go.awk: Go backend for lxdrgen.awk.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
					# Copyright (c) 2022 - 2024, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
# SPDX-License-Identifier: 0BSD
 | 
					# SPDX-License-Identifier: 0BSD
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# This backend also enables proxying to other endpoints using JSON.
 | 
					# This backend also enables proxying to other endpoints using JSON.
 | 
				
			||||||
@@ -145,7 +145,7 @@ function codegen_begin(    funcname) {
 | 
				
			|||||||
	print "// " funcname " tries to serialize a string value,"
 | 
						print "// " funcname " tries to serialize a string value,"
 | 
				
			||||||
	print "// appending it to the end of a byte stream."
 | 
						print "// appending it to the end of a byte stream."
 | 
				
			||||||
	print "func " funcname "(data []byte, s string) ([]byte, bool) {"
 | 
						print "func " funcname "(data []byte, s string) ([]byte, bool) {"
 | 
				
			||||||
	print "\tif len(s) > math.MaxUint32 {"
 | 
						print "\tif int64(len(s)) > math.MaxUint32 {"
 | 
				
			||||||
	print "\t\treturn nil, false"
 | 
						print "\t\treturn nil, false"
 | 
				
			||||||
	print "\t}"
 | 
						print "\t}"
 | 
				
			||||||
	print "\tdata = binary.BigEndian.AppendUint32(data, uint32(len(s)))"
 | 
						print "\tdata = binary.BigEndian.AppendUint32(data, uint32(len(s)))"
 | 
				
			||||||
@@ -301,9 +301,11 @@ function codegen_marshal(type, f,    marshal) {
 | 
				
			|||||||
		"\t}\n"
 | 
							"\t}\n"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_struct_field_marshal(d, cg,    camel, f, marshal) {
 | 
					function codegen_struct_field_marshal(d, cg, isaccessor,    camel, f, marshal) {
 | 
				
			||||||
	camel = snaketocamel(d["name"])
 | 
						camel = snaketocamel(d["name"])
 | 
				
			||||||
	f = "s." camel
 | 
						f = "s." camel
 | 
				
			||||||
 | 
						if (isaccessor)
 | 
				
			||||||
 | 
							f = f "()"
 | 
				
			||||||
	if (!d["isarray"]) {
 | 
						if (!d["isarray"]) {
 | 
				
			||||||
		append(cg, "marshal",
 | 
							append(cg, "marshal",
 | 
				
			||||||
			"\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \
 | 
								"\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \
 | 
				
			||||||
@@ -333,7 +335,7 @@ function codegen_struct_field_marshal(d, cg,    camel, f, marshal) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_struct_field(d, cg,    camel, f, serialize, deserialize) {
 | 
					function codegen_struct_field(d, cg,    camel, f, serialize, deserialize) {
 | 
				
			||||||
	codegen_struct_field_marshal(d, cg)
 | 
						codegen_struct_field_marshal(d, cg, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	camel = snaketocamel(d["name"])
 | 
						camel = snaketocamel(d["name"])
 | 
				
			||||||
	f = "s." camel
 | 
						f = "s." camel
 | 
				
			||||||
@@ -384,15 +386,11 @@ function codegen_struct_field(d, cg,    camel, f, serialize, deserialize) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_struct_tag(d, cg,    camel, f) {
 | 
					function codegen_struct_tag(d, cg) {
 | 
				
			||||||
	codegen_struct_field_marshal(d, cg)
 | 
						codegen_struct_field_marshal(d, cg, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	camel = snaketocamel(d["name"])
 | 
						# Do not serialize or deserialize here,
 | 
				
			||||||
	f = "s." camel
 | 
						# that is already done by the containing union.
 | 
				
			||||||
	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) {
 | 
					function codegen_struct(name, cg,    gotype) {
 | 
				
			||||||
@@ -439,9 +437,9 @@ function codegen_struct(name, cg,    gotype) {
 | 
				
			|||||||
		delete cg[i]
 | 
							delete cg[i]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union_tag(d, cg) {
 | 
					function codegen_union_tag(name, d, cg) {
 | 
				
			||||||
	cg["tagtype"] = d["type"]
 | 
						cg["tagtype"] = d["type"]
 | 
				
			||||||
	cg["tagname"] = d["name"]
 | 
						cg["tagname"] = snaketocamel(d["name"])
 | 
				
			||||||
	# The tag is implied from the type of struct stored in the interface.
 | 
						# The tag is implied from the type of struct stored in the interface.
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -450,13 +448,18 @@ function codegen_union_struct(name, casename, cg, scg,     structname, init) {
 | 
				
			|||||||
	structname = name snaketocamel(casename)
 | 
						structname = name snaketocamel(casename)
 | 
				
			||||||
	codegen_struct(structname, scg)
 | 
						codegen_struct(structname, scg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	init = CodegenGoType[structname] "{" snaketocamel(cg["tagname"]) \
 | 
						print "func (u *" CodegenGoType[structname] ") " cg["tagname"] "() " \
 | 
				
			||||||
		": " decapitalize(snaketocamel(cg["tagname"])) "}"
 | 
							CodegenGoType[cg["tagtype"]] " {"
 | 
				
			||||||
 | 
						print "\treturn " CodegenGoType[cg["tagtype"]] snaketocamel(casename)
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						init = CodegenGoType[structname] "{}"
 | 
				
			||||||
	append(cg, "unmarshal",
 | 
						append(cg, "unmarshal",
 | 
				
			||||||
		"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
 | 
							"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
 | 
				
			||||||
		"\t\ts := " init "\n" \
 | 
							"\t\ts := " init "\n" \
 | 
				
			||||||
		"\t\terr = json.Unmarshal(data, &s)\n" \
 | 
							"\t\terr = json.Unmarshal(data, &s)\n" \
 | 
				
			||||||
		"\t\tu.Interface = &s\n")
 | 
							"\t\tu.Variant = &s\n")
 | 
				
			||||||
	append(cg, "serialize",
 | 
						append(cg, "serialize",
 | 
				
			||||||
		"\tcase *" CodegenGoType[structname] ":\n" \
 | 
							"\tcase *" CodegenGoType[structname] ":\n" \
 | 
				
			||||||
		indent(sprintf(CodegenSerialize[structname], "union")))
 | 
							indent(sprintf(CodegenSerialize[structname], "union")))
 | 
				
			||||||
@@ -464,35 +467,37 @@ function codegen_union_struct(name, casename, cg, scg,     structname, init) {
 | 
				
			|||||||
		"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
 | 
							"\tcase " CodegenGoType[cg["tagtype"]] snaketocamel(casename) ":\n" \
 | 
				
			||||||
		"\t\ts := " init "\n" \
 | 
							"\t\ts := " init "\n" \
 | 
				
			||||||
		indent(sprintf(CodegenDeserialize[structname], "s")) \
 | 
							indent(sprintf(CodegenDeserialize[structname], "s")) \
 | 
				
			||||||
		"\t\tu.Interface = &s\n")
 | 
							"\t\tu.Variant = &s\n")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    gotype, tagfield, tagvar) {
 | 
					function codegen_union(name, cg, exhaustive,    gotype, tagvar) {
 | 
				
			||||||
	gotype = PrefixCamel name
 | 
						gotype = PrefixCamel name
 | 
				
			||||||
 | 
						# This must be a struct, so that UnmarshalJSON can create concrete types.
 | 
				
			||||||
	print "type " gotype " struct {"
 | 
						print "type " gotype " struct {"
 | 
				
			||||||
	print "\tInterface any"
 | 
						print "\tVariant interface {"
 | 
				
			||||||
 | 
						print "\t\t" cg["tagname"] "() " CodegenGoType[cg["tagtype"]]
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
	print "}"
 | 
						print "}"
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# This cannot be a pointer method, it wouldn't work recursively.
 | 
						# This cannot be a pointer method, it wouldn't work recursively.
 | 
				
			||||||
	CodegenIsMarshaler[name] = 1
 | 
						CodegenIsMarshaler[name] = 1
 | 
				
			||||||
	print "func (u " gotype ") MarshalJSON() ([]byte, error) {"
 | 
						print "func (u " gotype ") MarshalJSON() ([]byte, error) {"
 | 
				
			||||||
	print "\treturn u.Interface.(json.Marshaler).MarshalJSON()"
 | 
						print "\treturn u.Variant.(json.Marshaler).MarshalJSON()"
 | 
				
			||||||
	print "}"
 | 
						print "}"
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tagfield = snaketocamel(cg["tagname"])
 | 
						tagvar = decapitalize(cg["tagname"])
 | 
				
			||||||
	tagvar = decapitalize(tagfield)
 | 
					 | 
				
			||||||
	print "func (u *" gotype ") UnmarshalJSON(data []byte) (err error) {"
 | 
						print "func (u *" gotype ") UnmarshalJSON(data []byte) (err error) {"
 | 
				
			||||||
	print "\tvar t struct {"
 | 
						print "\tvar t struct {"
 | 
				
			||||||
	print "\t\t" tagfield " " CodegenGoType[cg["tagtype"]] \
 | 
						print "\t\t" cg["tagname"] " " CodegenGoType[cg["tagtype"]] \
 | 
				
			||||||
		" `json:\"" tagvar "\"`"
 | 
							" `json:\"" tagvar "\"`"
 | 
				
			||||||
	print "\t}"
 | 
						print "\t}"
 | 
				
			||||||
	print "\tif err := json.Unmarshal(data, &t); err != nil {"
 | 
						print "\tif err := json.Unmarshal(data, &t); err != nil {"
 | 
				
			||||||
	print "\t\treturn err"
 | 
						print "\t\treturn err"
 | 
				
			||||||
	print "\t}"
 | 
						print "\t}"
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
	print "\tswitch " tagvar " := t." tagfield "; " tagvar " {"
 | 
						print "\tswitch " tagvar " := t." cg["tagname"] "; " tagvar " {"
 | 
				
			||||||
	print cg["unmarshal"] "\tdefault:"
 | 
						print cg["unmarshal"] "\tdefault:"
 | 
				
			||||||
	print "\t\terr = errors.New(`unsupported value: ` + " tagvar ".String())"
 | 
						print "\t\terr = errors.New(`unsupported value: ` + " tagvar ".String())"
 | 
				
			||||||
	print "\t}"
 | 
						print "\t}"
 | 
				
			||||||
@@ -500,13 +505,15 @@ function codegen_union(name, cg,    gotype, tagfield, tagvar) {
 | 
				
			|||||||
	print "}"
 | 
						print "}"
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# XXX: Consider changing the interface into an AppendTo/ConsumeFrom one,
 | 
						# XXX: Consider rather testing the type for having an AppendTo method,
 | 
				
			||||||
	# that would eliminate these type case switches entirely.
 | 
						# which would eliminate this type case switch entirely.
 | 
				
			||||||
	# On the other hand, it would make it possible to send unsuitable structs.
 | 
					 | 
				
			||||||
	print "func (u *" gotype ") AppendTo(data []byte) ([]byte, bool) {"
 | 
						print "func (u *" gotype ") AppendTo(data []byte) ([]byte, bool) {"
 | 
				
			||||||
	print "\tok := true"
 | 
						print "\tok := true"
 | 
				
			||||||
	print "\tswitch union := u.Interface.(type) {"
 | 
						print sprintf(CodegenSerialize[cg["tagtype"]],
 | 
				
			||||||
 | 
							"u.Variant." cg["tagname"] "()") \
 | 
				
			||||||
 | 
							"\tswitch union := u.Variant.(type) {"
 | 
				
			||||||
	print cg["serialize"] "\tdefault:"
 | 
						print cg["serialize"] "\tdefault:"
 | 
				
			||||||
 | 
						print "\t\t_ = union"
 | 
				
			||||||
	print "\t\treturn nil, false"
 | 
						print "\t\treturn nil, false"
 | 
				
			||||||
	print "\t}"
 | 
						print "\t}"
 | 
				
			||||||
	print "\treturn data, ok"
 | 
						print "\treturn data, ok"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -183,16 +183,16 @@ function codegen_struct(name, cg) {
 | 
				
			|||||||
		delete cg[i]
 | 
							delete cg[i]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union_tag(d, cg) {
 | 
					function codegen_union_tag(name, d, cg) {
 | 
				
			||||||
	cg["tagtype"] = d["type"]
 | 
						cg["tagtype"] = d["type"]
 | 
				
			||||||
	cg["tagname"] = d["name"]
 | 
						cg["tagname"] = decapitalize(snaketocamel(d["name"]))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union_struct(name, casename, cg, scg,     structname) {
 | 
					function codegen_union_struct(name, casename, cg, scg,     structname) {
 | 
				
			||||||
	append(scg, "methods",
 | 
						append(scg, "methods",
 | 
				
			||||||
		"\n" \
 | 
							"\n" \
 | 
				
			||||||
		"\tconstructor() {\n" \
 | 
							"\tconstructor() {\n" \
 | 
				
			||||||
		"\t\tthis." decapitalize(snaketocamel(cg["tagname"])) \
 | 
							"\t\tthis." cg["tagname"] \
 | 
				
			||||||
			" = " cg["tagtype"] "." snaketocamel(casename) "\n" \
 | 
								" = " cg["tagtype"] "." snaketocamel(casename) "\n" \
 | 
				
			||||||
		"\t}\n")
 | 
							"\t}\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -208,8 +208,8 @@ function codegen_union_struct(name, casename, cg, scg,     structname) {
 | 
				
			|||||||
		"\t}\n")
 | 
							"\t}\n")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    tagvar) {
 | 
					function codegen_union(name, cg, exhaustive,    tagvar) {
 | 
				
			||||||
	tagvar = decapitalize(snaketocamel(cg["tagname"]))
 | 
						tagvar = cg["tagname"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
	print "export function deserialize" name "(r) {"
 | 
						print "export function deserialize" name "(r) {"
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										277
									
								
								tools/lxdrgen-swift.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										277
									
								
								tools/lxdrgen-swift.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,277 @@
 | 
				
			|||||||
 | 
					# lxdrgen-swift.awk: Swift backend for lxdrgen.awk.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
 | 
					# SPDX-License-Identifier: 0BSD
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function define_internal(name, swifttype) {
 | 
				
			||||||
 | 
						Types[name] = "internal"
 | 
				
			||||||
 | 
						CodegenSwiftType[name] = swifttype
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = "%s.read()"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function define_sint(size,    shortname, swifttype) {
 | 
				
			||||||
 | 
						shortname = "i" size
 | 
				
			||||||
 | 
						swifttype = "Int" size
 | 
				
			||||||
 | 
						define_internal(shortname, swifttype)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function define_uint(size,    shortname, swifttype) {
 | 
				
			||||||
 | 
						shortname = "u" size
 | 
				
			||||||
 | 
						swifttype = "UInt" size
 | 
				
			||||||
 | 
						define_internal(shortname, swifttype)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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("bool", "Bool")
 | 
				
			||||||
 | 
						define_internal("string", "String")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						print "// Code generated from " FILENAME ". DO NOT EDIT."
 | 
				
			||||||
 | 
						print "import Foundation"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public struct " PrefixCamel "Reader {"
 | 
				
			||||||
 | 
						print "\tpublic var data: Data"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic enum ReadError: Error {"
 | 
				
			||||||
 | 
						print "\t\tcase unexpectedEOF"
 | 
				
			||||||
 | 
						print "\t\tcase invalidEncoding"
 | 
				
			||||||
 | 
						print "\t\tcase overflow"
 | 
				
			||||||
 | 
						print "\t\tcase unexpectedValue"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func read<T: FixedWidthInteger>() throws -> T {"
 | 
				
			||||||
 | 
						print "\t\tlet size = MemoryLayout<T>.size"
 | 
				
			||||||
 | 
						print "\t\tguard data.count >= size else {"
 | 
				
			||||||
 | 
						print "\t\t\tthrow ReadError.unexpectedEOF"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\tvar acc: T = 0"
 | 
				
			||||||
 | 
						print "\t\tdata.prefix(size).forEach { acc = acc << 8 | T($0) }"
 | 
				
			||||||
 | 
						print "\t\tdata = data.dropFirst(size)"
 | 
				
			||||||
 | 
						print "\t\treturn acc"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func read() throws -> Bool {"
 | 
				
			||||||
 | 
						print "\t\ttry read() != UInt8(0)"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func read() throws -> String {"
 | 
				
			||||||
 | 
						print "\t\tlet size: UInt32 = try self.read()"
 | 
				
			||||||
 | 
						print "\t\tguard let count = Int(exactly: size) else {"
 | 
				
			||||||
 | 
						print "\t\t\tthrow ReadError.overflow"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\tguard data.count >= count else {"
 | 
				
			||||||
 | 
						print "\t\t\tthrow ReadError.unexpectedEOF"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\tdefer {"
 | 
				
			||||||
 | 
						print "\t\t\tdata = data.dropFirst(count)"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\tif let s = String(data: data.prefix(count), encoding: .utf8) {"
 | 
				
			||||||
 | 
						print "\t\t\treturn s"
 | 
				
			||||||
 | 
						print "\t\t} else {"
 | 
				
			||||||
 | 
						print "\t\t\tthrow ReadError.invalidEncoding"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func read<" \
 | 
				
			||||||
 | 
							"T: RawRepresentable<Int8>>() throws -> T {"
 | 
				
			||||||
 | 
						print "\t\tguard let value = T(rawValue: try read()) else {"
 | 
				
			||||||
 | 
						print "\t\t\tthrow ReadError.unexpectedValue"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\treturn value"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func read<T>("
 | 
				
			||||||
 | 
						print "\t\t\t_ read: (inout Self) throws -> T) throws -> [T] {"
 | 
				
			||||||
 | 
						print "\t\tlet size: UInt32 = try self.read()"
 | 
				
			||||||
 | 
						print "\t\tguard let count = Int(exactly: size) else {"
 | 
				
			||||||
 | 
						print "\t\t\tthrow ReadError.overflow"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\tvar array = [T]()"
 | 
				
			||||||
 | 
						print "\t\tarray.reserveCapacity(count)"
 | 
				
			||||||
 | 
						print "\t\tfor _ in 0..<count {"
 | 
				
			||||||
 | 
						print "\t\t\tarray.append(try read(&self))"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t\treturn array"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public struct " PrefixCamel "Writer {"
 | 
				
			||||||
 | 
						print "\tpublic var data = Data()"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func append<T: FixedWidthInteger>(_ number: T) {"
 | 
				
			||||||
 | 
						print "\t\tvar n = number.byteSwapped"
 | 
				
			||||||
 | 
						print "\t\tfor _ in 0..<MemoryLayout<T>.size {"
 | 
				
			||||||
 | 
						print "\t\t\tdata.append(UInt8(truncatingIfNeeded: n))"
 | 
				
			||||||
 | 
						print "\t\t\tn >>= 8"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func append(_ bool: Bool) {"
 | 
				
			||||||
 | 
						print "\t\tappend(UInt8(bool ? 1 : 0))"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func append(_ string: String) {"
 | 
				
			||||||
 | 
						print "\t\tlet bytes = string.data(using: .utf8)!"
 | 
				
			||||||
 | 
						print "\t\tappend(UInt32(bytes.count))"
 | 
				
			||||||
 | 
						print "\t\tdata.append(bytes)"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func append<T: " \
 | 
				
			||||||
 | 
							"RawRepresentable<Int8>>(_ value: T) {"
 | 
				
			||||||
 | 
						print "\t\tappend(value.rawValue)"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func append<T>("
 | 
				
			||||||
 | 
						print "\t\t\t_ array: Array<T>, _ write: (inout Self, T) -> ()) {"
 | 
				
			||||||
 | 
						print "\t\tappend(UInt32(array.count))"
 | 
				
			||||||
 | 
						print "\t\tfor i in 0..<array.count {"
 | 
				
			||||||
 | 
						print "\t\t\twrite(&self, array[i])"
 | 
				
			||||||
 | 
						print "\t\t}"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic mutating func append<T: " \
 | 
				
			||||||
 | 
							PrefixCamel "Encodable>(_ value: T) {"
 | 
				
			||||||
 | 
						print "\t\tvalue.encode(to: &self)"
 | 
				
			||||||
 | 
						print "\t}"
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public protocol " PrefixCamel "Encodable { " \
 | 
				
			||||||
 | 
							"func encode(to: inout " PrefixCamel "Writer) }"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_constant(name, value) {
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public let " decapitalize(PrefixCamel snaketocamel(name)) " = " value
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_enum_value(name, subname, value, cg) {
 | 
				
			||||||
 | 
						append(cg, "fields",
 | 
				
			||||||
 | 
							"\tcase " decapitalize(snaketocamel(subname)) " = " value "\n")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_enum(name, cg,    swifttype) {
 | 
				
			||||||
 | 
						swifttype = PrefixCamel name
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public enum " swifttype ": Int8 {"
 | 
				
			||||||
 | 
						print cg["fields"] "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSwiftType[name] = swifttype
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = "%s.read()"
 | 
				
			||||||
 | 
						for (i in cg)
 | 
				
			||||||
 | 
							delete cg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_struct_field(d, cg,    camel) {
 | 
				
			||||||
 | 
						camel = decapitalize(snaketocamel(d["name"]))
 | 
				
			||||||
 | 
						if (!d["isarray"]) {
 | 
				
			||||||
 | 
							append(cg, "fields",
 | 
				
			||||||
 | 
								"\tpublic var " camel ": " CodegenSwiftType[d["type"]] "\n")
 | 
				
			||||||
 | 
							append(cg, "deserialize",
 | 
				
			||||||
 | 
								"\t\tself." camel " = try " \
 | 
				
			||||||
 | 
									sprintf(CodegenDeserialize[d["type"]], "from") "\n")
 | 
				
			||||||
 | 
							append(cg, "serialize",
 | 
				
			||||||
 | 
								"\t\tto.append(self." camel ")\n")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						append(cg, "fields",
 | 
				
			||||||
 | 
							"\tpublic var " camel ": [" CodegenSwiftType[d["type"]] "]\n")
 | 
				
			||||||
 | 
						append(cg, "deserialize",
 | 
				
			||||||
 | 
							"\t\tself." camel " = try from.read() { r in try " \
 | 
				
			||||||
 | 
								sprintf(CodegenDeserialize[d["type"]], "r") " }\n")
 | 
				
			||||||
 | 
						append(cg, "serialize",
 | 
				
			||||||
 | 
							"\t\tto.append(self." camel ") { (w, value) in w.append(value) }\n")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_struct_tag(d, cg,    camel) {
 | 
				
			||||||
 | 
						camel = decapitalize(snaketocamel(d["name"]))
 | 
				
			||||||
 | 
						append(cg, "serialize",
 | 
				
			||||||
 | 
							"\t\tto.append(self." camel ")\n")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_struct(name, cg,    swifttype) {
 | 
				
			||||||
 | 
						swifttype = PrefixCamel name
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public struct " swifttype " {\n" cg["fields"] "}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "extension " swifttype ": " PrefixCamel "Encodable {"
 | 
				
			||||||
 | 
						print "\tpublic init(from: inout " PrefixCamel "Reader) throws {"
 | 
				
			||||||
 | 
						print cg["deserialize"] "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic func encode(to: inout " PrefixCamel "Writer) {"
 | 
				
			||||||
 | 
						print cg["serialize"] "\t}"
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSwiftType[name] = swifttype
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = "%s.read()"
 | 
				
			||||||
 | 
						for (i in cg)
 | 
				
			||||||
 | 
							delete cg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_union_tag(name, d, cg) {
 | 
				
			||||||
 | 
						cg["tagtype"] = d["type"]
 | 
				
			||||||
 | 
						cg["tagname"] = decapitalize(snaketocamel(d["name"]))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_union_struct(name, casename, cg, scg,     swifttype) {
 | 
				
			||||||
 | 
						# And thus not all generated structs are present in Types.
 | 
				
			||||||
 | 
						swifttype = PrefixCamel name snaketocamel(casename)
 | 
				
			||||||
 | 
						casename = decapitalize(snaketocamel(casename))
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public struct " swifttype ": " PrefixCamel name " {"
 | 
				
			||||||
 | 
						print "\tpublic var " cg["tagname"] \
 | 
				
			||||||
 | 
							": " CodegenSwiftType[cg["tagtype"]] " { ." casename " }"
 | 
				
			||||||
 | 
						print scg["fields"] "}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "extension " swifttype ": " PrefixCamel "Encodable {"
 | 
				
			||||||
 | 
						print "\tfileprivate init(from: inout " PrefixCamel "Reader) throws {"
 | 
				
			||||||
 | 
						print scg["deserialize"] "\t}"
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "\tpublic func encode(to: inout " PrefixCamel "Writer) {"
 | 
				
			||||||
 | 
						print scg["serialize"] "\t}"
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						append(cg, "cases", "\tcase ." casename ":\n" \
 | 
				
			||||||
 | 
							"\t\treturn try " swifttype "(from: &from)\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSwiftType[name] = swifttype
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = "%s.read()"
 | 
				
			||||||
 | 
						for (i in scg)
 | 
				
			||||||
 | 
							delete scg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function codegen_union(name, cg, exhaustive,    swifttype, init) {
 | 
				
			||||||
 | 
						# Classes don't have automatic member-wise initializers,
 | 
				
			||||||
 | 
						# thus using structs and protocols.
 | 
				
			||||||
 | 
						swifttype = PrefixCamel name
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public protocol " swifttype ": " PrefixCamel "Encodable {"
 | 
				
			||||||
 | 
						print "\tvar " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] " { get }"
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!exhaustive)
 | 
				
			||||||
 | 
							append(cg, "cases", "\tdefault:\n" \
 | 
				
			||||||
 | 
								"\t\tthrow " PrefixCamel "Reader.ReadError.unexpectedValue\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						init = decapitalize(swifttype)
 | 
				
			||||||
 | 
						print ""
 | 
				
			||||||
 | 
						print "public func " init \
 | 
				
			||||||
 | 
							"(from: inout " PrefixCamel "Reader) throws -> " swifttype " {"
 | 
				
			||||||
 | 
						print "\tlet " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] \
 | 
				
			||||||
 | 
							" = try from.read()"
 | 
				
			||||||
 | 
						print "\tswitch " cg["tagname"] " {"
 | 
				
			||||||
 | 
						print cg["cases"] "\t}"
 | 
				
			||||||
 | 
						print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CodegenSwiftType[name] = swifttype
 | 
				
			||||||
 | 
						CodegenDeserialize[name] = init "(from: &%s)"
 | 
				
			||||||
 | 
						for (i in cg)
 | 
				
			||||||
 | 
							delete cg[i]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
# lxdrgen.awk: an XDR-derived code generator for network protocols.
 | 
					# lxdrgen.awk: an XDR-derived code generator for network protocols.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 | 
					# Copyright (c) 2022 - 2025, Přemysl Eric Janouch <p@janouch.name>
 | 
				
			||||||
# SPDX-License-Identifier: 0BSD
 | 
					# SPDX-License-Identifier: 0BSD
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# Usage: env LC_ALL=C awk -f lxdrgen.awk -f lxdrgen-{c,go,mjs}.awk \
 | 
					# Usage: env LC_ALL=C awk -f lxdrgen.awk -f lxdrgen-{c,go,mjs}.awk \
 | 
				
			||||||
@@ -28,9 +28,10 @@ function snaketocamel(s) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function decapitalize(s) {
 | 
					function decapitalize(s) {
 | 
				
			||||||
	if (match(s, /[[:upper:]][[:lower:]]/)) {
 | 
						if (match(s, /^[[:upper:]][[:lower:]]/))
 | 
				
			||||||
		return tolower(substr(s, 1, 1)) substr(s, 2)
 | 
							return tolower(substr(s, 1, 1)) substr(s, 2)
 | 
				
			||||||
	}
 | 
						if (match(s, /^[[:upper:]]$/))
 | 
				
			||||||
 | 
							return tolower(s)
 | 
				
			||||||
	return s
 | 
						return s
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -216,7 +217,8 @@ function defstruct(    name, d, cg) {
 | 
				
			|||||||
	return name
 | 
						return name
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
					function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i,
 | 
				
			||||||
 | 
							unseen, defaulted, exhaustive) {
 | 
				
			||||||
	delete cg[0]
 | 
						delete cg[0]
 | 
				
			||||||
	delete scg[0]
 | 
						delete scg[0]
 | 
				
			||||||
	delete d[0]
 | 
						delete d[0]
 | 
				
			||||||
@@ -230,7 +232,7 @@ function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if (Types[tagtype] != "enum")
 | 
						if (Types[tagtype] != "enum")
 | 
				
			||||||
		fatal("not an enum type: " tagtype)
 | 
							fatal("not an enum type: " tagtype)
 | 
				
			||||||
	codegen_union_tag(tag, cg)
 | 
						codegen_union_tag(name, tag, cg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	split(EnumValues[tagtype], a, SUBSEP)
 | 
						split(EnumValues[tagtype], a, SUBSEP)
 | 
				
			||||||
	for (i in a)
 | 
						for (i in a)
 | 
				
			||||||
@@ -247,9 +249,22 @@ function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
				
			|||||||
			if (!unseen[tagvalue]--)
 | 
								if (!unseen[tagvalue]--)
 | 
				
			||||||
				fatal("no such value or duplicate case: " tagtype "." tagvalue)
 | 
									fatal("no such value or duplicate case: " tagtype "." tagvalue)
 | 
				
			||||||
			codegen_struct_tag(tag, scg)
 | 
								codegen_struct_tag(tag, scg)
 | 
				
			||||||
 | 
							} else if (accept("default")) {
 | 
				
			||||||
 | 
								if (tagvalue)
 | 
				
			||||||
 | 
									codegen_union_struct(name, tagvalue, cg, scg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								expect(accept(":"))
 | 
				
			||||||
 | 
								if (defaulted)
 | 
				
			||||||
 | 
									fatal("duplicate default")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								tagvalue = ""
 | 
				
			||||||
 | 
								defaulted = 1
 | 
				
			||||||
		} else if (tagvalue) {
 | 
							} else if (tagvalue) {
 | 
				
			||||||
			if (readfield(d))
 | 
								if (readfield(d))
 | 
				
			||||||
				codegen_struct_field(d, scg)
 | 
									codegen_struct_field(d, scg)
 | 
				
			||||||
 | 
							} else if (defaulted) {
 | 
				
			||||||
 | 
								if (readfield(d))
 | 
				
			||||||
 | 
									fatal("default must not contain fields")
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			fatal("union fields must fall under a case")
 | 
								fatal("union fields must fall under a case")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -257,9 +272,20 @@ function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
				
			|||||||
	if (tagvalue)
 | 
						if (tagvalue)
 | 
				
			||||||
		codegen_union_struct(name, tagvalue, cg, scg)
 | 
							codegen_union_struct(name, tagvalue, cg, scg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# What remains non-zero in unseen[2..] is simply not recognized/allowed.
 | 
						# Unseen cases are only recognized/allowed when default is present.
 | 
				
			||||||
 | 
						exhaustive = 1
 | 
				
			||||||
 | 
						for (i in unseen)
 | 
				
			||||||
 | 
							if (i && unseen[i]) {
 | 
				
			||||||
 | 
								if (defaulted) {
 | 
				
			||||||
 | 
									codegen_struct_tag(tag, scg)
 | 
				
			||||||
 | 
									codegen_union_struct(name, i, cg, scg)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									exhaustive = 0
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Types[name] = "union"
 | 
						Types[name] = "union"
 | 
				
			||||||
	codegen_union(name, cg)
 | 
						codegen_union(name, cg, exhaustive)
 | 
				
			||||||
	return name
 | 
						return name
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										42
									
								
								tools/wdye/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								tools/wdye/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
				
			|||||||
 | 
					cmake_minimum_required (VERSION 3.18)
 | 
				
			||||||
 | 
					project (wdye VERSION 1 DESCRIPTION "What did you expect?" LANGUAGES C)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_C_STANDARD 99)
 | 
				
			||||||
 | 
					set (CMAKE_C_STANDARD_REQUIRED ON)
 | 
				
			||||||
 | 
					set (CMAKE_C_EXTENSIONS OFF)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# -Wunused-function is pretty annoying here, as everything is static
 | 
				
			||||||
 | 
					set (options -Wall -Wextra -Wno-unused-function)
 | 
				
			||||||
 | 
					add_compile_options ("$<$<CXX_COMPILER_ID:GNU>:${options}>")
 | 
				
			||||||
 | 
					add_compile_options ("$<$<CXX_COMPILER_ID:Clang>:${options}>")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../../cmake")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					find_package (Curses)
 | 
				
			||||||
 | 
					find_package (PkgConfig REQUIRED)
 | 
				
			||||||
 | 
					pkg_search_module (lua REQUIRED
 | 
				
			||||||
 | 
						lua53 lua5.3 lua-5.3 lua54 lua5.4 lua-5.4 lua>=5.3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					option (WITH_CURSES "Offer terminal sequences using Curses" "${CURSES_FOUND}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# -liconv may or may not be a part of libc
 | 
				
			||||||
 | 
					find_path (iconv_INCLUDE_DIRS iconv.h)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					include_directories (BEFORE "${PROJECT_BINARY_DIR}" ${iconv_INCLUDE_DIRS})
 | 
				
			||||||
 | 
					file (CONFIGURE OUTPUT "${PROJECT_BINARY_DIR}/config.h" CONTENT [[
 | 
				
			||||||
 | 
					#define PROGRAM_NAME "${PROJECT_NAME}"
 | 
				
			||||||
 | 
					#define PROGRAM_VERSION "${PROJECT_VERSION}"
 | 
				
			||||||
 | 
					#cmakedefine WITH_CURSES
 | 
				
			||||||
 | 
					]])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_executable (wdye wdye.c)
 | 
				
			||||||
 | 
					target_include_directories (wdye PUBLIC ${lua_INCLUDE_DIRS})
 | 
				
			||||||
 | 
					target_link_directories (wdye PUBLIC ${lua_LIBRARY_DIRS})
 | 
				
			||||||
 | 
					target_link_libraries (wdye PUBLIC ${lua_LIBRARIES})
 | 
				
			||||||
 | 
					if (WITH_CURSES)
 | 
				
			||||||
 | 
						target_include_directories (wdye PUBLIC ${CURSES_INCLUDE_DIRS})
 | 
				
			||||||
 | 
						target_link_libraries (wdye PUBLIC ${CURSES_LIBRARIES})
 | 
				
			||||||
 | 
					endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_test (NAME wdye COMMAND wdye "${PROJECT_SOURCE_DIR}/test.lua")
 | 
				
			||||||
 | 
					include (CTest)
 | 
				
			||||||
							
								
								
									
										33
									
								
								tools/wdye/test.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								tools/wdye/test.lua
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					for k, v in pairs(wdye) do _G[k] = v end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- The terminal echoes back, we don't want to read the same stuff twice.
 | 
				
			||||||
 | 
					local cat = spawn {"sh", "-c", "cat > /dev/null", environ={TERM="xterm"}}
 | 
				
			||||||
 | 
					assert(cat, "failed to spawn process")
 | 
				
			||||||
 | 
					assert(cat.term.key_left, "bad terminfo")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					cat:send("Hello\r")
 | 
				
			||||||
 | 
					local m = expect(cat:exact {"Hello\r", function (p) return p[0] end})
 | 
				
			||||||
 | 
					assert(m == "Hello\r", "exact match failed, or value expansion mismatch")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local t = table.pack(expect(timeout {.5, 42}))
 | 
				
			||||||
 | 
					assert(#t == 1 and t[1] == 42, "timeout match failed, or value mismatch")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					cat:send("abc123\r")
 | 
				
			||||||
 | 
					expect(cat:regex {"A(.*)3", nocase=true, function (p)
 | 
				
			||||||
 | 
						assert(p[0] == "abc123", "wrong regex group #0")
 | 
				
			||||||
 | 
						assert(p[1] == "bc12", "wrong regex group #1")
 | 
				
			||||||
 | 
					end})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					assert(not cat:wait (true), "process reports exiting early")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- Send EOF (^D), test method chaining.
 | 
				
			||||||
 | 
					cat:send("Closing...\r"):send("\004")
 | 
				
			||||||
 | 
					local v = expect(cat:eof {true},
 | 
				
			||||||
 | 
						cat:default {.5, function (p) error "expected EOF, got a timeout" end})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					assert(cat.pid > 0, "process has no ID")
 | 
				
			||||||
 | 
					local s1, exit, signal = cat:wait ()
 | 
				
			||||||
 | 
					assert(s1 == 0 and exit == 0 and not signal, "unexpected exit status")
 | 
				
			||||||
 | 
					assert(cat.pid < 0, "process still has an ID")
 | 
				
			||||||
 | 
					local s2 = cat:wait (true)
 | 
				
			||||||
 | 
					assert(s1 == s2, "exit status not remembered")
 | 
				
			||||||
							
								
								
									
										146
									
								
								tools/wdye/wdye.adoc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								tools/wdye/wdye.adoc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,146 @@
 | 
				
			|||||||
 | 
					wdye(1)
 | 
				
			||||||
 | 
					=======
 | 
				
			||||||
 | 
					:doctype: manpage
 | 
				
			||||||
 | 
					:manmanual: wdye Manual
 | 
				
			||||||
 | 
					:mansource: wdye {release-version}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Name
 | 
				
			||||||
 | 
					----
 | 
				
			||||||
 | 
					wdye - what did you expect: Lua-based Expect tool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Synopsis
 | 
				
			||||||
 | 
					--------
 | 
				
			||||||
 | 
					*wdye* _program.lua_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Description
 | 
				
			||||||
 | 
					-----------
 | 
				
			||||||
 | 
					*wdye* executes a Lua script, providing an *expect*(1)-like API targeted
 | 
				
			||||||
 | 
					at application testing.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					API
 | 
				
			||||||
 | 
					---
 | 
				
			||||||
 | 
					This list is logically ordered.  Uppercase names represent object types.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wdye.spawn {file [, arg1, ...] [, environ=env]}
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Creates a new pseudoterminal, spawns the given program in it,
 | 
				
			||||||
 | 
					and returns a _process_ object.  When *file* doesn't contain slashes,
 | 
				
			||||||
 | 
					the program will be searched for in _PATH_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The *env* map may be used to override environment variables, notably _TERM_.
 | 
				
			||||||
 | 
					Variables evaluating to _false_ will be removed from the environment.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The program's whole process group receives SIGKILL when the _process_
 | 
				
			||||||
 | 
					is garbage-collected, unless *wait* has collected the process group leader.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wdye.expect ([pattern1, ...])
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Waits until any pattern is ready, in order.
 | 
				
			||||||
 | 
					When no *timeout* (or *default*) patterns are included, one is added implicitly.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The function returns the matching _pattern_'s values, while replacing
 | 
				
			||||||
 | 
					any included functions with the results of their immediate evaluation,
 | 
				
			||||||
 | 
					passing the matching _pattern_ as their sole argument.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wdye.timeout {[timeout, ] [value1, ...]}
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Returns a new timeout _pattern_.  When no *timeout* is given, which is specified
 | 
				
			||||||
 | 
					in seconds, a default timeout value is assumed.  Any further values
 | 
				
			||||||
 | 
					are remembered to be later processed by *expect*.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wdye.continue ()
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Raises a _nil_ error, which is interpreted by *expect* as a signal to restart
 | 
				
			||||||
 | 
					all processing.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS.buffer
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					A string with the _process_' current read buffer contents.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS.pid
 | 
				
			||||||
 | 
					~~~~~~~~~~~
 | 
				
			||||||
 | 
					An integer with the _process_' process ID, or -1 if *wait* has collected it.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS.term
 | 
				
			||||||
 | 
					~~~~~~~~~~~~
 | 
				
			||||||
 | 
					A table with the _process_' *terminfo*(5) capabilities,
 | 
				
			||||||
 | 
					notably containing all **key_...** codes.
 | 
				
			||||||
 | 
					This functionality may not be enabled, then this table will always be empty.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS:send ([string, ...])
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Writes the given strings to the _process_' terminal slave,
 | 
				
			||||||
 | 
					and returns the _process_ for method chaining.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Beware of echoing and deadlocks, as only *expect* can read from the _process_,
 | 
				
			||||||
 | 
					and thus consume the terminal slave's output queue.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS:regex {pattern [, nocase=true] [, notransfer=true] [, value1, ...]}
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Returns a new regular expression _pattern_.  The *pattern* is a POSIX
 | 
				
			||||||
 | 
					Extended Regular Expression.  Whether it can match NUL bytes depends on your
 | 
				
			||||||
 | 
					system C library.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					When the *nocase* option is _true_, the expression will be matched
 | 
				
			||||||
 | 
					case-insensitively.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Unless the *notransfer* option is _true_, all data up until the end of the match
 | 
				
			||||||
 | 
					will be erased from the _process_' read buffer upon a successful match.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS:exact {literal [, nocase=true] [, notransfer=true] [, value1, ...]}
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Returns a new literal string _pattern_.  This behaves as if the *literal*
 | 
				
			||||||
 | 
					had its ERE special characters quoted, and was then passed to *regex*.
 | 
				
			||||||
 | 
					This _pattern_ can always match NUL bytes.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS:eof {[notransfer=true, ] [value1, ...]}
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Returns a new end-of-file _pattern_, which matches the entire read buffer
 | 
				
			||||||
 | 
					contents once the child process closes the terminal.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS:wait ([nowait])
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Waits for the program to terminate, and returns three values:
 | 
				
			||||||
 | 
					a combined status as used by `$?` in shells,
 | 
				
			||||||
 | 
					an exit status, and a termination signal number.
 | 
				
			||||||
 | 
					One of the latter two values will be _nil_, as appropriate.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					When the *nowait* option is _true_, the function returns immediately.
 | 
				
			||||||
 | 
					If the process hasn't terminated yet, the function then returns no values.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROCESS:default {[timeout, ] [notransfer=true, ] [value1, ...]}
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					Returns a new _pattern_ combining *wdye.timeout* with *eof*.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PATTERN.process
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					A reference to the _pattern_'s respective process, or _nil_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PATTERN[group]
 | 
				
			||||||
 | 
					~~~~~~~~~~~~~~
 | 
				
			||||||
 | 
					For patterns that can match data, the zeroth group will be the whole matched
 | 
				
			||||||
 | 
					input sequence.
 | 
				
			||||||
 | 
					For *regex* patterns, positive groups relate to regular expression subgroups.
 | 
				
			||||||
 | 
					Missing groups evaluate to _nil_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Example
 | 
				
			||||||
 | 
					-------
 | 
				
			||||||
 | 
					 for k, v in pairs(wdye) do _G[k] = v end
 | 
				
			||||||
 | 
					 local rot13 = spawn {"tr", "A-Za-z", "N-ZA-Mn-za-m", environ={TERM="dumb"}}
 | 
				
			||||||
 | 
					 rot13:send "Hello\r"
 | 
				
			||||||
 | 
					 expect(rot13:exact {"Uryyb\r"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Environment
 | 
				
			||||||
 | 
					-----------
 | 
				
			||||||
 | 
					*WDYE_LOGGING*::
 | 
				
			||||||
 | 
						When this environment variable is present, *wdye* produces asciicast v2
 | 
				
			||||||
 | 
						files for every spawned program, in the current working directory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Reporting bugs
 | 
				
			||||||
 | 
					--------------
 | 
				
			||||||
 | 
					Use https://git.janouch.name/p/liberty to report bugs, request features,
 | 
				
			||||||
 | 
					or submit pull requests.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					See also
 | 
				
			||||||
 | 
					--------
 | 
				
			||||||
 | 
					*expect*(1), *terminfo*(5), *regex*(7)
 | 
				
			||||||
							
								
								
									
										1420
									
								
								tools/wdye/wdye.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1420
									
								
								tools/wdye/wdye.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user