Compare commits
	
		
			11 Commits
		
	
	
		
			1dcd259d05
			...
			8ffe20c0e8
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						8ffe20c0e8
	
				 | 
					
					
						|||
| 
						
						
							
						
						bb30c7d86e
	
				 | 
					
					
						|||
| 
						
						
							
						
						47ef2ae5bd
	
				 | 
					
					
						|||
| 
						
						
							
						
						69800a6afb
	
				 | 
					
					
						|||
| 
						
						
							
						
						fe1035633a
	
				 | 
					
					
						|||
| 
						
						
							
						
						da75b6f735
	
				 | 
					
					
						|||
| 
						
						
							
						
						199c56e141
	
				 | 
					
					
						|||
| 
						
						
							
						
						6e9217e5d0
	
				 | 
					
					
						|||
| 
						
						
							
						
						3835b6e499
	
				 | 
					
					
						|||
| 
						
						
							
						
						bf534010cb
	
				 | 
					
					
						|||
| 
						
						
							
						
						7b0d7a19e5
	
				 | 
					
					
						
							
								
								
									
										24
									
								
								.travis.yml
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								.travis.yml
									
									
									
									
									
								
							@@ -1,24 +0,0 @@
 | 
			
		||||
language: c
 | 
			
		||||
notifications:
 | 
			
		||||
 irc:
 | 
			
		||||
  channels: "irc.janouch.name#dev"
 | 
			
		||||
  use_notice: true
 | 
			
		||||
  skip_join: true
 | 
			
		||||
  template:
 | 
			
		||||
   - "%{repository_name}#%{build_number} on %{branch}: %{message}"
 | 
			
		||||
   - "  %{compare_url}"
 | 
			
		||||
   - "  %{build_url}"
 | 
			
		||||
  on_success: change
 | 
			
		||||
  on_failure: always
 | 
			
		||||
compiler:
 | 
			
		||||
 - clang
 | 
			
		||||
 - gcc
 | 
			
		||||
before_install:
 | 
			
		||||
 - sudo apt-get update -qq
 | 
			
		||||
before_script:
 | 
			
		||||
 - mkdir build
 | 
			
		||||
 - cd build
 | 
			
		||||
script:
 | 
			
		||||
 - cmake .. -DCMAKE_INSTALL_PREFIX=/usr
 | 
			
		||||
 - make
 | 
			
		||||
 - ctest -V
 | 
			
		||||
@@ -2,10 +2,11 @@ project (liberty C)
 | 
			
		||||
cmake_minimum_required (VERSION 2.8.5)
 | 
			
		||||
 | 
			
		||||
# Moar warnings
 | 
			
		||||
if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUC)
 | 
			
		||||
if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
 | 
			
		||||
	# -Wunused-function is pretty annoying here, as everything is static
 | 
			
		||||
	set (CMAKE_C_FLAGS "-std=c99 -Wall -Wextra -Wno-unused-function")
 | 
			
		||||
endif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUC)
 | 
			
		||||
	set (wdisabled "-Wno-unused-function -Wno-implicit-fallthrough")
 | 
			
		||||
	set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra ${wdisabled}")
 | 
			
		||||
endif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
 | 
			
		||||
 | 
			
		||||
# Dependencies
 | 
			
		||||
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
 | 
			
		||||
@@ -34,15 +35,8 @@ foreach (extra iconv rt)
 | 
			
		||||
	endif (extra_lib_${extra})
 | 
			
		||||
endforeach (extra)
 | 
			
		||||
 | 
			
		||||
# Generate a configuration file
 | 
			
		||||
# TODO: actualy use the configuration file for something; so far we allow
 | 
			
		||||
#   for direct inclusion without running this CMakeLists.txt
 | 
			
		||||
configure_file (${PROJECT_SOURCE_DIR}/liberty-config.h.in
 | 
			
		||||
	${PROJECT_BINARY_DIR}/liberty-config.h)
 | 
			
		||||
include_directories (${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR})
 | 
			
		||||
set (common_sources ${PROJECT_BINARY_DIR}/liberty-config.h)
 | 
			
		||||
 | 
			
		||||
# Build some unit tests
 | 
			
		||||
include_directories (${PROJECT_SOURCE_DIR})
 | 
			
		||||
enable_testing ()
 | 
			
		||||
foreach (name liberty proto)
 | 
			
		||||
	add_executable (test-${name} tests/${name}.c ${common_sources})
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										5
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								LICENSE
									
									
									
									
									
								
							@@ -1,8 +1,7 @@
 | 
			
		||||
Copyright (c) 2014 - 2017, Přemysl Janouch <p.janouch@gmail.com>
 | 
			
		||||
Copyright (c) 2014 - 2018, Přemysl Janouch <p@janouch.name>
 | 
			
		||||
 | 
			
		||||
Permission to use, copy, modify, and/or distribute this software for any
 | 
			
		||||
purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
copyright notice and this permission notice appear in all copies.
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										17
									
								
								README.adoc
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								README.adoc
									
									
									
									
									
								
							@@ -19,18 +19,13 @@ I'm not testing them at all, with the exception of OpenBSD.
 | 
			
		||||
 | 
			
		||||
Contributing and Support
 | 
			
		||||
------------------------
 | 
			
		||||
Use this project's GitHub to report any bugs, request features, or submit pull
 | 
			
		||||
requests.  If you want to discuss this project, or maybe just hang out with
 | 
			
		||||
the developer, feel free to join me at irc://irc.janouch.name, channel #dev.
 | 
			
		||||
Use https://git.janouch.name/p/liberty to report any bugs, request features,
 | 
			
		||||
or submit pull requests.  `git send-email` is tolerated.  If you want to discuss
 | 
			
		||||
the project, feel free to join me at ircs://irc.janouch.name, channel #dev.
 | 
			
		||||
 | 
			
		||||
Bitcoin donations: 12r5uEWEgcHC46xd64tt3hHt9EUvYYDHe9
 | 
			
		||||
Bitcoin donations are accepted at: 12r5uEWEgcHC46xd64tt3hHt9EUvYYDHe9
 | 
			
		||||
 | 
			
		||||
License
 | 
			
		||||
-------
 | 
			
		||||
'liberty' is written by Přemysl Janouch <p.janouch@gmail.com>.
 | 
			
		||||
 | 
			
		||||
You may use the software under the terms of the ISC license, the text of which
 | 
			
		||||
is included within the package, or, at your option, you may relicense the work
 | 
			
		||||
under the MIT or the Modified BSD License, as listed at the following site:
 | 
			
		||||
 | 
			
		||||
http://www.gnu.org/licenses/license-list.html
 | 
			
		||||
This software is released under the terms of the 0BSD license, the text of which
 | 
			
		||||
is included within the package along with the list of authors.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										181
									
								
								liberty-proto.c
									
									
									
									
									
								
							
							
						
						
									
										181
									
								
								liberty-proto.c
									
									
									
									
									
								
							@@ -1,11 +1,10 @@
 | 
			
		||||
/*
 | 
			
		||||
 * liberty-proto.c: the ultimate C unlibrary: protocols
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2014 - 2016, Přemysl Janouch <p.janouch@gmail.com>
 | 
			
		||||
 * Copyright (c) 2014 - 2016, Přemysl Janouch <p@janouch.name>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -34,9 +33,7 @@ struct irc_message
 | 
			
		||||
static char *
 | 
			
		||||
irc_unescape_message_tag (const char *value)
 | 
			
		||||
{
 | 
			
		||||
	struct str s;
 | 
			
		||||
	str_init (&s);
 | 
			
		||||
 | 
			
		||||
	struct str s = str_make ();
 | 
			
		||||
	bool escape = false;
 | 
			
		||||
	for (const char *p = value; *p; p++)
 | 
			
		||||
	{
 | 
			
		||||
@@ -63,8 +60,7 @@ irc_unescape_message_tag (const char *value)
 | 
			
		||||
static void
 | 
			
		||||
irc_parse_message_tags (const char *tags, struct str_map *out)
 | 
			
		||||
{
 | 
			
		||||
	struct strv v;
 | 
			
		||||
	strv_init (&v);
 | 
			
		||||
	struct strv v = strv_make ();
 | 
			
		||||
	cstr_split (tags, ";", true, &v);
 | 
			
		||||
 | 
			
		||||
	for (size_t i = 0; i < v.len; i++)
 | 
			
		||||
@@ -78,19 +74,16 @@ irc_parse_message_tags (const char *tags, struct str_map *out)
 | 
			
		||||
		else
 | 
			
		||||
			str_map_set (out, key, xstrdup (""));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	strv_free (&v);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
irc_parse_message (struct irc_message *msg, const char *line)
 | 
			
		||||
{
 | 
			
		||||
	str_map_init (&msg->tags);
 | 
			
		||||
	msg->tags.free = free;
 | 
			
		||||
 | 
			
		||||
	msg->tags = str_map_make (free);
 | 
			
		||||
	msg->prefix = NULL;
 | 
			
		||||
	msg->command = NULL;
 | 
			
		||||
	strv_init (&msg->params);
 | 
			
		||||
	msg->params = strv_make ();
 | 
			
		||||
 | 
			
		||||
	// IRC 3.2 message tags
 | 
			
		||||
	if (*line == '@')
 | 
			
		||||
@@ -288,14 +281,15 @@ struct http_tokenizer
 | 
			
		||||
	struct str string;                  ///< "token" / "quoted-string" content
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
http_tokenizer_init (struct http_tokenizer *self, const char *input, size_t len)
 | 
			
		||||
static struct http_tokenizer
 | 
			
		||||
http_tokenizer_make (const char *input, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->input = (const unsigned char *) input;
 | 
			
		||||
	self->input_len = len;
 | 
			
		||||
 | 
			
		||||
	str_init (&self->string);
 | 
			
		||||
	return (struct http_tokenizer)
 | 
			
		||||
	{
 | 
			
		||||
		.input = (const unsigned char *) input,
 | 
			
		||||
		.input_len = len,
 | 
			
		||||
		.string = str_make (),
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -428,8 +422,8 @@ http_parse_media_type (const char *media_type,
 | 
			
		||||
	char **type, char **subtype, struct str_map *parameters)
 | 
			
		||||
{
 | 
			
		||||
	bool result = false;
 | 
			
		||||
	struct http_tokenizer t;
 | 
			
		||||
	http_tokenizer_init (&t, media_type, strlen (media_type));
 | 
			
		||||
	struct http_tokenizer t =
 | 
			
		||||
		http_tokenizer_make (media_type, strlen (media_type));
 | 
			
		||||
 | 
			
		||||
	if (http_tokenizer_next (&t, true) != HTTP_T_TOKEN)
 | 
			
		||||
		goto end;
 | 
			
		||||
@@ -490,8 +484,7 @@ http_parse_upgrade (const char *upgrade, struct http_protocol **out)
 | 
			
		||||
	struct http_protocol *list = NULL;
 | 
			
		||||
	struct http_protocol *tail = NULL;
 | 
			
		||||
 | 
			
		||||
	struct http_tokenizer t;
 | 
			
		||||
	http_tokenizer_init (&t, upgrade, strlen (upgrade));
 | 
			
		||||
	struct http_tokenizer t = http_tokenizer_make (upgrade, strlen (upgrade));
 | 
			
		||||
 | 
			
		||||
	enum {
 | 
			
		||||
		STATE_PROTOCOL_NAME,
 | 
			
		||||
@@ -618,16 +611,16 @@ struct scgi_parser
 | 
			
		||||
	void *user_data;                    ///< User data passed to callbacks
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
scgi_parser_init (struct scgi_parser *self)
 | 
			
		||||
static struct scgi_parser
 | 
			
		||||
scgi_parser_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
 | 
			
		||||
	str_init (&self->input);
 | 
			
		||||
	str_map_init (&self->headers);
 | 
			
		||||
	self->headers.free = free;
 | 
			
		||||
	str_init (&self->name);
 | 
			
		||||
	str_init (&self->value);
 | 
			
		||||
	return (struct scgi_parser)
 | 
			
		||||
	{
 | 
			
		||||
		.input = str_make (),
 | 
			
		||||
		.headers = str_map_make (free),
 | 
			
		||||
		.name = str_make (),
 | 
			
		||||
		.value = str_make (),
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -728,7 +721,7 @@ scgi_parser_push (struct scgi_parser *self,
 | 
			
		||||
				self->name.str, str_steal (&self->value));
 | 
			
		||||
 | 
			
		||||
			str_reset (&self->name);
 | 
			
		||||
			str_init (&self->value);
 | 
			
		||||
			self->value = str_make ();
 | 
			
		||||
 | 
			
		||||
			self->state = SCGI_READING_NAME;
 | 
			
		||||
		}
 | 
			
		||||
@@ -827,12 +820,11 @@ struct fcgi_parser
 | 
			
		||||
	void *user_data;                    ///< User data
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
fcgi_parser_init (struct fcgi_parser *self)
 | 
			
		||||
static struct fcgi_parser
 | 
			
		||||
fcgi_parser_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	str_init (&self->input);
 | 
			
		||||
	str_init (&self->content);
 | 
			
		||||
	return (struct fcgi_parser)
 | 
			
		||||
		{ .input = str_make (), .content = str_make () };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -845,8 +837,8 @@ fcgi_parser_free (struct fcgi_parser *self)
 | 
			
		||||
static void
 | 
			
		||||
fcgi_parser_unpack_header (struct fcgi_parser *self)
 | 
			
		||||
{
 | 
			
		||||
	struct msg_unpacker unpacker;
 | 
			
		||||
	msg_unpacker_init (&unpacker, self->input.str, self->input.len);
 | 
			
		||||
	struct msg_unpacker unpacker =
 | 
			
		||||
		msg_unpacker_make (self->input.str, self->input.len);
 | 
			
		||||
 | 
			
		||||
	bool success = true;
 | 
			
		||||
	uint8_t reserved;
 | 
			
		||||
@@ -928,11 +920,10 @@ struct fcgi_nv_parser
 | 
			
		||||
	char *value;                        ///< The current value, 0-terminated
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
fcgi_nv_parser_init (struct fcgi_nv_parser *self)
 | 
			
		||||
static struct fcgi_nv_parser
 | 
			
		||||
fcgi_nv_parser_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	str_init (&self->input);
 | 
			
		||||
	return (struct fcgi_nv_parser) { .input = str_make () };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -951,8 +942,8 @@ fcgi_nv_parser_push (struct fcgi_nv_parser *self, const void *data, size_t len)
 | 
			
		||||
 | 
			
		||||
	while (true)
 | 
			
		||||
	{
 | 
			
		||||
		struct msg_unpacker unpacker;
 | 
			
		||||
		msg_unpacker_init (&unpacker, self->input.str, self->input.len);
 | 
			
		||||
		struct msg_unpacker unpacker =
 | 
			
		||||
			msg_unpacker_make (self->input.str, self->input.len);
 | 
			
		||||
 | 
			
		||||
	switch (self->state)
 | 
			
		||||
	{
 | 
			
		||||
@@ -1049,8 +1040,7 @@ fcgi_nv_convert_len (size_t len, struct str *output)
 | 
			
		||||
static void
 | 
			
		||||
fcgi_nv_convert (struct str_map *map, struct str *output)
 | 
			
		||||
{
 | 
			
		||||
	struct str_map_iter iter;
 | 
			
		||||
	str_map_iter_init (&iter, map);
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (map);
 | 
			
		||||
	while (str_map_iter_next (&iter))
 | 
			
		||||
	{
 | 
			
		||||
		const char *name  = iter.link->key;
 | 
			
		||||
@@ -1089,8 +1079,7 @@ ws_encode_response_key (const char *key)
 | 
			
		||||
	SHA1 ((unsigned char *) response_key, strlen (response_key), hash);
 | 
			
		||||
	free (response_key);
 | 
			
		||||
 | 
			
		||||
	struct str base64;
 | 
			
		||||
	str_init (&base64);
 | 
			
		||||
	struct str base64 = str_make ();
 | 
			
		||||
	base64_encode (hash, sizeof hash, &base64);
 | 
			
		||||
	return str_steal (&base64);
 | 
			
		||||
}
 | 
			
		||||
@@ -1168,11 +1157,10 @@ struct ws_parser
 | 
			
		||||
	void *user_data;                    ///< User data for callbacks
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
ws_parser_init (struct ws_parser *self)
 | 
			
		||||
static struct ws_parser
 | 
			
		||||
ws_parser_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	str_init (&self->input);
 | 
			
		||||
	return (struct ws_parser) { .input = str_make () };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -1213,8 +1201,8 @@ ws_parser_push (struct ws_parser *self, const void *data, size_t len)
 | 
			
		||||
	bool success = false;
 | 
			
		||||
	str_append_data (&self->input, data, len);
 | 
			
		||||
 | 
			
		||||
	struct msg_unpacker unpacker;
 | 
			
		||||
	msg_unpacker_init (&unpacker, self->input.str, self->input.len);
 | 
			
		||||
	struct msg_unpacker unpacker =
 | 
			
		||||
		msg_unpacker_make (self->input.str, self->input.len);
 | 
			
		||||
 | 
			
		||||
	while (true)
 | 
			
		||||
	switch (self->state)
 | 
			
		||||
@@ -1275,7 +1263,7 @@ ws_parser_push (struct ws_parser *self, const void *data, size_t len)
 | 
			
		||||
	case WS_PARSER_PAYLOAD:
 | 
			
		||||
		// Move the buffer so that payload data is at the front
 | 
			
		||||
		str_remove_slice (&self->input, 0, unpacker.offset);
 | 
			
		||||
		msg_unpacker_init (&unpacker, self->input.str, self->input.len);
 | 
			
		||||
		unpacker = msg_unpacker_make (self->input.str, self->input.len);
 | 
			
		||||
 | 
			
		||||
		if (self->input.len < self->payload_len)
 | 
			
		||||
			goto need_data;
 | 
			
		||||
@@ -1418,21 +1406,19 @@ struct mpd_client
 | 
			
		||||
static void mpd_client_reset (struct mpd_client *self);
 | 
			
		||||
static void mpd_client_destroy_connector (struct mpd_client *self);
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
mpd_client_init (struct mpd_client *self, struct poller *poller)
 | 
			
		||||
static struct mpd_client
 | 
			
		||||
mpd_client_make (struct poller *poller)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
 | 
			
		||||
	self->poller = poller;
 | 
			
		||||
	self->socket = -1;
 | 
			
		||||
 | 
			
		||||
	str_init (&self->read_buffer);
 | 
			
		||||
	str_init (&self->write_buffer);
 | 
			
		||||
 | 
			
		||||
	strv_init (&self->data);
 | 
			
		||||
 | 
			
		||||
	poller_fd_init (&self->socket_event, poller, -1);
 | 
			
		||||
	poller_timer_init (&self->timeout_timer, poller);
 | 
			
		||||
	return (struct mpd_client)
 | 
			
		||||
	{
 | 
			
		||||
		.poller = poller,
 | 
			
		||||
		.socket = -1,
 | 
			
		||||
		.read_buffer = str_make (),
 | 
			
		||||
		.write_buffer = str_make (),
 | 
			
		||||
		.data = strv_make (),
 | 
			
		||||
		.socket_event = poller_fd_make (poller, -1),
 | 
			
		||||
		.timeout_timer = poller_timer_make (poller),
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -1694,8 +1680,9 @@ mpd_client_add_task
 | 
			
		||||
static void mpd_client_send_command
 | 
			
		||||
	(struct mpd_client *self, const char *command, ...) ATTRIBUTE_SENTINEL;
 | 
			
		||||
 | 
			
		||||
/// Avoid calling this method directly if you don't want things to explode
 | 
			
		||||
static void
 | 
			
		||||
mpd_client_send_commandv (struct mpd_client *self, char **commands)
 | 
			
		||||
mpd_client_send_command_raw (struct mpd_client *self, const char *raw)
 | 
			
		||||
{
 | 
			
		||||
	// Automatically interrupt idle mode
 | 
			
		||||
	if (self->idling)
 | 
			
		||||
@@ -1707,35 +1694,37 @@ mpd_client_send_commandv (struct mpd_client *self, char **commands)
 | 
			
		||||
		mpd_client_send_command (self, "noidle", NULL);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct str line;
 | 
			
		||||
	str_init (&line);
 | 
			
		||||
 | 
			
		||||
	for (; *commands; commands++)
 | 
			
		||||
	{
 | 
			
		||||
		if (line.len)
 | 
			
		||||
			str_append_c (&line, ' ');
 | 
			
		||||
 | 
			
		||||
		if (mpd_client_must_quote (*commands))
 | 
			
		||||
			mpd_client_quote (*commands, &line);
 | 
			
		||||
		else
 | 
			
		||||
			str_append (&line, *commands);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (self->on_io_hook)
 | 
			
		||||
		self->on_io_hook (self->user_data, true, line.str);
 | 
			
		||||
		self->on_io_hook (self->user_data, true, raw);
 | 
			
		||||
 | 
			
		||||
	str_append_c (&line, '\n');
 | 
			
		||||
	str_append_str (&self->write_buffer, &line);
 | 
			
		||||
	str_free (&line);
 | 
			
		||||
	str_append (&self->write_buffer, raw);
 | 
			
		||||
	str_append_c (&self->write_buffer, '\n');
 | 
			
		||||
 | 
			
		||||
	mpd_client_update_poller (self);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
mpd_client_send_commandv (struct mpd_client *self, char **fields)
 | 
			
		||||
{
 | 
			
		||||
	struct str line = str_make ();
 | 
			
		||||
	for (; *fields; fields++)
 | 
			
		||||
	{
 | 
			
		||||
		if (line.len)
 | 
			
		||||
			str_append_c (&line, ' ');
 | 
			
		||||
 | 
			
		||||
		if (mpd_client_must_quote (*fields))
 | 
			
		||||
			mpd_client_quote (*fields, &line);
 | 
			
		||||
		else
 | 
			
		||||
			str_append (&line, *fields);
 | 
			
		||||
	}
 | 
			
		||||
	mpd_client_send_command_raw (self, line.str);
 | 
			
		||||
	str_free (&line);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
mpd_client_send_command (struct mpd_client *self, const char *command, ...)
 | 
			
		||||
{
 | 
			
		||||
	struct strv v;
 | 
			
		||||
	strv_init (&v);
 | 
			
		||||
	struct strv v = strv_make ();
 | 
			
		||||
 | 
			
		||||
	va_list ap;
 | 
			
		||||
	va_start (ap, command);
 | 
			
		||||
@@ -1835,9 +1824,7 @@ mpd_client_idle (struct mpd_client *self, unsigned subsystems)
 | 
			
		||||
{
 | 
			
		||||
	hard_assert (!self->in_list);
 | 
			
		||||
 | 
			
		||||
	struct strv v;
 | 
			
		||||
	strv_init (&v);
 | 
			
		||||
 | 
			
		||||
	struct strv v = strv_make ();
 | 
			
		||||
	strv_append (&v, "idle");
 | 
			
		||||
	for (size_t i = 0; i < N_ELEMENTS (mpd_subsystem_names); i++)
 | 
			
		||||
		if (subsystems & (1 << i))
 | 
			
		||||
@@ -1864,7 +1851,7 @@ mpd_client_finish_connection (struct mpd_client *self, int socket)
 | 
			
		||||
	self->socket = socket;
 | 
			
		||||
	self->state = MPD_CONNECTED;
 | 
			
		||||
 | 
			
		||||
	poller_fd_init (&self->socket_event, self->poller, self->socket);
 | 
			
		||||
	self->socket_event = poller_fd_make (self->poller, self->socket);
 | 
			
		||||
	self->socket_event.dispatcher = mpd_client_on_ready;
 | 
			
		||||
	self->socket_event.user_data = self;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,10 @@
 | 
			
		||||
/*
 | 
			
		||||
 * liberty-tui.c: the ultimate C unlibrary: TUI
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2016 - 2017, Přemysl Janouch <p.janouch@gmail.com>
 | 
			
		||||
 * Copyright (c) 2016 - 2017, Přemysl Janouch <p@janouch.name>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -61,8 +60,7 @@ struct attrs
 | 
			
		||||
static struct attrs
 | 
			
		||||
attrs_decode (const char *value)
 | 
			
		||||
{
 | 
			
		||||
	struct strv v;
 | 
			
		||||
	strv_init (&v);
 | 
			
		||||
	struct strv v = strv_make ();
 | 
			
		||||
	cstr_split (value, " ", true, &v);
 | 
			
		||||
 | 
			
		||||
	int colors = 0;
 | 
			
		||||
@@ -110,11 +108,12 @@ struct row_buffer
 | 
			
		||||
	int total_width;                    ///< Total width of all characters
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
row_buffer_init (struct row_buffer *self)
 | 
			
		||||
static struct row_buffer
 | 
			
		||||
row_buffer_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	ARRAY_INIT_SIZED (self->chars, 256);
 | 
			
		||||
	struct row_buffer self = {};
 | 
			
		||||
	ARRAY_INIT_SIZED (self.chars, 256);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										439
									
								
								liberty.c
									
									
									
									
									
								
							
							
						
						
									
										439
									
								
								liberty.c
									
									
									
									
									
								
							@@ -1,11 +1,10 @@
 | 
			
		||||
/*
 | 
			
		||||
 * liberty.c: the ultimate C unlibrary
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2014 - 2016, Přemysl Janouch <p.janouch@gmail.com>
 | 
			
		||||
 * Copyright (c) 2014 - 2018, Přemysl Janouch <p@janouch.name>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -38,6 +37,7 @@
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <sys/time.h>
 | 
			
		||||
#include <sys/uio.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <poll.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
@@ -383,12 +383,14 @@ struct strv
 | 
			
		||||
	size_t alloc;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
strv_init (struct strv *self)
 | 
			
		||||
static struct strv
 | 
			
		||||
strv_make (void)
 | 
			
		||||
{
 | 
			
		||||
	self->alloc = 4;
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->vector = xcalloc (sizeof *self->vector, self->alloc);
 | 
			
		||||
	struct strv self;
 | 
			
		||||
	self.alloc = 4;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.vector = xcalloc (sizeof *self.vector, self.alloc);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -406,7 +408,7 @@ static void
 | 
			
		||||
strv_reset (struct strv *self)
 | 
			
		||||
{
 | 
			
		||||
	strv_free (self);
 | 
			
		||||
	strv_init (self);
 | 
			
		||||
	*self = strv_make ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -481,12 +483,14 @@ struct str
 | 
			
		||||
/// long as the allocation is below the given threshold.  (Trivial heuristics.)
 | 
			
		||||
#define STR_SHRINK_THRESHOLD (1 << 20)
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
str_init (struct str *self)
 | 
			
		||||
static struct str
 | 
			
		||||
str_make (void)
 | 
			
		||||
{
 | 
			
		||||
	self->alloc = 16;
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->str = strcpy (xmalloc (self->alloc), "");
 | 
			
		||||
	struct str self;
 | 
			
		||||
	self.alloc = 16;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.str = strcpy (xmalloc (self.alloc), "");
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -502,7 +506,7 @@ static void
 | 
			
		||||
str_reset (struct str *self)
 | 
			
		||||
{
 | 
			
		||||
	str_free (self);
 | 
			
		||||
	str_init (self);
 | 
			
		||||
	*self = str_make ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *
 | 
			
		||||
@@ -802,15 +806,17 @@ struct str_map
 | 
			
		||||
 | 
			
		||||
typedef void (*str_map_free_fn) (void *);
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
str_map_init (struct str_map *self)
 | 
			
		||||
static struct str_map
 | 
			
		||||
str_map_make (str_map_free_fn free)
 | 
			
		||||
{
 | 
			
		||||
	self->alloc = STR_MAP_MIN_ALLOC;
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->free = NULL;
 | 
			
		||||
	self->key_xfrm = NULL;
 | 
			
		||||
	self->map = xcalloc (self->alloc, sizeof *self->map);
 | 
			
		||||
	self->shrink_lock = false;
 | 
			
		||||
	struct str_map self;
 | 
			
		||||
	self.alloc = STR_MAP_MIN_ALLOC;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.free = free;
 | 
			
		||||
	self.key_xfrm = NULL;
 | 
			
		||||
	self.map = xcalloc (self.alloc, sizeof *self.map);
 | 
			
		||||
	self.shrink_lock = false;
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -1003,12 +1009,10 @@ struct str_map_iter
 | 
			
		||||
	struct str_map_link *link;          ///< Current link
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
str_map_iter_init (struct str_map_iter *self, const struct str_map *map)
 | 
			
		||||
static struct str_map_iter
 | 
			
		||||
str_map_iter_make (const struct str_map *map)
 | 
			
		||||
{
 | 
			
		||||
	self->map = map;
 | 
			
		||||
	self->next_index = 0;
 | 
			
		||||
	self->link = NULL;
 | 
			
		||||
	return (struct str_map_iter) { .map = map, .next_index = 0, .link = NULL };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *
 | 
			
		||||
@@ -1038,13 +1042,15 @@ struct str_map_unset_iter
 | 
			
		||||
	struct str_map_link *next;          ///< Next link
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
str_map_unset_iter_init (struct str_map_unset_iter *self, struct str_map *map)
 | 
			
		||||
static struct str_map_unset_iter
 | 
			
		||||
str_map_unset_iter_make (struct str_map *map)
 | 
			
		||||
{
 | 
			
		||||
	str_map_iter_init (&self->iter, map);
 | 
			
		||||
	struct str_map_unset_iter self;
 | 
			
		||||
	self.iter = str_map_iter_make (map);
 | 
			
		||||
	map->shrink_lock = true;
 | 
			
		||||
	(void) str_map_iter_next (&self->iter);
 | 
			
		||||
	self->next = self->iter.link;
 | 
			
		||||
	(void) str_map_iter_next (&self.iter);
 | 
			
		||||
	self.next = self.iter.link;
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *
 | 
			
		||||
@@ -1118,11 +1124,10 @@ struct async_manager
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
async_init (struct async *self, struct async_manager *manager)
 | 
			
		||||
static struct async
 | 
			
		||||
async_make (struct async_manager *manager)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->manager = manager;
 | 
			
		||||
	return (struct async) { .manager = manager };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Only allowed from the main thread once the job has been started but before
 | 
			
		||||
@@ -1266,17 +1271,18 @@ async_manager_cancel_all (struct async_manager *self)
 | 
			
		||||
	async_manager_dispatch (self);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
async_manager_init (struct async_manager *self)
 | 
			
		||||
static struct async_manager
 | 
			
		||||
async_manager_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	hard_assert (!pthread_mutex_init (&self->lock, NULL));
 | 
			
		||||
	hard_assert (!pthread_cond_init (&self->finished_cond, NULL));
 | 
			
		||||
	struct async_manager self = {};
 | 
			
		||||
	hard_assert (!pthread_mutex_init (&self.lock, NULL));
 | 
			
		||||
	hard_assert (!pthread_cond_init (&self.finished_cond, NULL));
 | 
			
		||||
 | 
			
		||||
	hard_assert (!pipe (self->finished_pipe));
 | 
			
		||||
	hard_assert (set_blocking (self->finished_pipe[0], false));
 | 
			
		||||
	set_cloexec (self->finished_pipe[0]);
 | 
			
		||||
	set_cloexec (self->finished_pipe[1]);
 | 
			
		||||
	hard_assert (!pipe (self.finished_pipe));
 | 
			
		||||
	hard_assert (set_blocking (self.finished_pipe[0], false));
 | 
			
		||||
	set_cloexec (self.finished_pipe[0]);
 | 
			
		||||
	set_cloexec (self.finished_pipe[1]);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -1355,12 +1361,14 @@ struct poller_timers
 | 
			
		||||
	size_t alloc;                       ///< Number of timers allocated
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_timers_init (struct poller_timers *self)
 | 
			
		||||
static struct poller_timers
 | 
			
		||||
poller_timers_make (void)
 | 
			
		||||
{
 | 
			
		||||
	self->alloc = POLLER_MIN_ALLOC;
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	self->heap = xmalloc (self->alloc * sizeof *self->heap);
 | 
			
		||||
	struct poller_timers self;
 | 
			
		||||
	self.alloc = POLLER_MIN_ALLOC;
 | 
			
		||||
	self.len = 0;
 | 
			
		||||
	self.heap = xmalloc (self.alloc * sizeof *self.heap);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -2085,12 +2093,11 @@ poller_run (struct poller *self)
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_timer_init (struct poller_timer *self, struct poller *poller)
 | 
			
		||||
static struct poller_timer
 | 
			
		||||
poller_timer_make (struct poller *poller)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->timers = &poller->common.timers;
 | 
			
		||||
	self->index = -1;
 | 
			
		||||
	return (struct poller_timer)
 | 
			
		||||
		{ .timers = &poller->common.timers, .index = -1, };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -2115,11 +2122,10 @@ poller_timer_reset (struct poller_timer *self)
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_idle_init (struct poller_idle *self, struct poller *poller)
 | 
			
		||||
static struct poller_idle
 | 
			
		||||
poller_idle_make (struct poller *poller)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->poller = poller;
 | 
			
		||||
	return (struct poller_idle) { .poller = poller };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -2146,13 +2152,10 @@ poller_idle_reset (struct poller_idle *self)
 | 
			
		||||
 | 
			
		||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
poller_fd_init (struct poller_fd *self, struct poller *poller, int fd)
 | 
			
		||||
static struct poller_fd
 | 
			
		||||
poller_fd_make (struct poller *poller, int fd)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->poller = poller;
 | 
			
		||||
	self->index = -1;
 | 
			
		||||
	self->fd = fd;
 | 
			
		||||
	return (struct poller_fd) { .poller = poller, .index = -1, .fd = fd };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -2183,12 +2186,12 @@ poller_common_dummy_dispatcher (const struct pollfd *pfd, void *user_data)
 | 
			
		||||
static void
 | 
			
		||||
poller_common_init (struct poller_common *self, struct poller *poller)
 | 
			
		||||
{
 | 
			
		||||
	poller_timers_init (&self->timers);
 | 
			
		||||
	self->timers = poller_timers_make ();
 | 
			
		||||
	self->idle = NULL;
 | 
			
		||||
#ifdef LIBERTY_WANT_ASYNC
 | 
			
		||||
	async_manager_init (&self->async);
 | 
			
		||||
	self->async = async_manager_make ();
 | 
			
		||||
 | 
			
		||||
	poller_fd_init (&self->async_event, poller, self->async.finished_pipe[0]);
 | 
			
		||||
	self->async_event = poller_fd_make (poller, self->async.finished_pipe[0]);
 | 
			
		||||
	poller_fd_set (&self->async_event, POLLIN);
 | 
			
		||||
	self->async_event.dispatcher = poller_common_dummy_dispatcher;
 | 
			
		||||
	self->async_event.user_data = self;
 | 
			
		||||
@@ -2289,7 +2292,7 @@ async_getaddrinfo (struct async_manager *manager,
 | 
			
		||||
	const char *host, const char *service, const struct addrinfo *hints)
 | 
			
		||||
{
 | 
			
		||||
	struct async_getaddrinfo *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	async_init (&self->async, manager);
 | 
			
		||||
	self->async = async_make (manager);
 | 
			
		||||
 | 
			
		||||
	if (host)     self->host = xstrdup (host);
 | 
			
		||||
	if (service)  self->service = xstrdup (service);
 | 
			
		||||
@@ -2353,7 +2356,7 @@ async_getnameinfo (struct async_manager *manager,
 | 
			
		||||
	const struct sockaddr *sa, socklen_t sa_len, int flags)
 | 
			
		||||
{
 | 
			
		||||
	struct async_getnameinfo *self = xcalloc (1, sizeof *self);
 | 
			
		||||
	async_init (&self->async, manager);
 | 
			
		||||
	self->async = async_make (manager);
 | 
			
		||||
 | 
			
		||||
	self->address = memcpy (xmalloc (sa_len), sa, sa_len);
 | 
			
		||||
	self->address_len = sa_len;
 | 
			
		||||
@@ -2387,12 +2390,10 @@ struct write_queue
 | 
			
		||||
	size_t len;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
write_queue_init (struct write_queue *self)
 | 
			
		||||
static struct write_queue
 | 
			
		||||
write_queue_make (void)
 | 
			
		||||
{
 | 
			
		||||
	self->head = self->tail = NULL;
 | 
			
		||||
	self->head_offset = 0;
 | 
			
		||||
	self->len = 0;
 | 
			
		||||
	return (struct write_queue) {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -2444,11 +2445,10 @@ struct msg_reader
 | 
			
		||||
	uint64_t offset;                    ///< Current offset in the buffer
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
msg_reader_init (struct msg_reader *self)
 | 
			
		||||
static struct msg_reader
 | 
			
		||||
msg_reader_make (void)
 | 
			
		||||
{
 | 
			
		||||
	str_init (&self->buf);
 | 
			
		||||
	self->offset = 0;
 | 
			
		||||
	return (struct msg_reader) { .buf = str_make (), .offset = 0 };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -2520,12 +2520,10 @@ struct msg_unpacker
 | 
			
		||||
	size_t len;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
msg_unpacker_init (struct msg_unpacker *self, const void *data, size_t len)
 | 
			
		||||
static struct msg_unpacker
 | 
			
		||||
msg_unpacker_make (const void *data, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	self->data = data;
 | 
			
		||||
	self->len = len;
 | 
			
		||||
	self->offset = 0;
 | 
			
		||||
	return (struct msg_unpacker) { .data = data, .len = len, .offset = 0 };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static size_t
 | 
			
		||||
@@ -2600,12 +2598,13 @@ struct msg_writer
 | 
			
		||||
	struct str buf;                     ///< Holds the message data
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
msg_writer_init (struct msg_writer *self)
 | 
			
		||||
static struct msg_writer
 | 
			
		||||
msg_writer_make (void)
 | 
			
		||||
{
 | 
			
		||||
	str_init (&self->buf);
 | 
			
		||||
	struct msg_writer self = { .buf = str_make () };
 | 
			
		||||
	// Placeholder for message length
 | 
			
		||||
	str_append_data (&self->buf, "\x00\x00\x00\x00" "\x00\x00\x00\x00", 8);
 | 
			
		||||
	str_append_data (&self.buf, "\x00\x00\x00\x00" "\x00\x00\x00\x00", 8);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *
 | 
			
		||||
@@ -2765,10 +2764,10 @@ struct utf8_iter
 | 
			
		||||
	size_t len;                         ///< How many bytes remain
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
utf8_iter_init (struct utf8_iter *self, const char *s)
 | 
			
		||||
static struct utf8_iter
 | 
			
		||||
utf8_iter_make (const char *s)
 | 
			
		||||
{
 | 
			
		||||
	self->len = strlen ((self->s = s));
 | 
			
		||||
	return (struct utf8_iter) { .s = s, .len = strlen (s) };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int32_t
 | 
			
		||||
@@ -2964,8 +2963,7 @@ strv_join (const struct strv *v, const char *delimiter)
 | 
			
		||||
	if (!v->len)
 | 
			
		||||
		return xstrdup ("");
 | 
			
		||||
 | 
			
		||||
	struct str result;
 | 
			
		||||
	str_init (&result);
 | 
			
		||||
	struct str result = str_make ();
 | 
			
		||||
	str_append (&result, v->vector[0]);
 | 
			
		||||
	for (size_t i = 1; i < v->len; i++)
 | 
			
		||||
		str_append_printf (&result, "%s%s", delimiter, v->vector[i]);
 | 
			
		||||
@@ -2978,8 +2976,7 @@ static char *
 | 
			
		||||
xstrdup_printf (const char *format, ...)
 | 
			
		||||
{
 | 
			
		||||
	va_list ap;
 | 
			
		||||
	struct str tmp;
 | 
			
		||||
	str_init (&tmp);
 | 
			
		||||
	struct str tmp = str_make ();
 | 
			
		||||
	va_start (ap, format);
 | 
			
		||||
	str_append_vprintf (&tmp, format, ap);
 | 
			
		||||
	va_end (ap);
 | 
			
		||||
@@ -3099,8 +3096,7 @@ lock_pid_file (const char *path, struct error **e)
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct str pid;
 | 
			
		||||
	str_init (&pid);
 | 
			
		||||
	struct str pid = str_make ();
 | 
			
		||||
	str_append_printf (&pid, "%ld", (long) getpid ());
 | 
			
		||||
 | 
			
		||||
	if (ftruncate (fd, 0)
 | 
			
		||||
@@ -3208,8 +3204,7 @@ resolve_relative_filename_generic
 | 
			
		||||
static void
 | 
			
		||||
get_xdg_config_dirs (struct strv *out)
 | 
			
		||||
{
 | 
			
		||||
	struct str config_home;
 | 
			
		||||
	str_init (&config_home);
 | 
			
		||||
	struct str config_home = str_make ();
 | 
			
		||||
	get_xdg_home_dir (&config_home, "XDG_CONFIG_HOME", ".config");
 | 
			
		||||
	strv_append (out, config_home.str);
 | 
			
		||||
	str_free (&config_home);
 | 
			
		||||
@@ -3223,8 +3218,7 @@ get_xdg_config_dirs (struct strv *out)
 | 
			
		||||
static char *
 | 
			
		||||
resolve_relative_config_filename (const char *filename)
 | 
			
		||||
{
 | 
			
		||||
	struct strv paths;
 | 
			
		||||
	strv_init (&paths);
 | 
			
		||||
	struct strv paths = strv_make ();
 | 
			
		||||
	get_xdg_config_dirs (&paths);
 | 
			
		||||
	char *result = resolve_relative_filename_generic
 | 
			
		||||
		(&paths, PROGRAM_NAME "/", filename);
 | 
			
		||||
@@ -3235,8 +3229,7 @@ resolve_relative_config_filename (const char *filename)
 | 
			
		||||
static void
 | 
			
		||||
get_xdg_data_dirs (struct strv *out)
 | 
			
		||||
{
 | 
			
		||||
	struct str data_home;
 | 
			
		||||
	str_init (&data_home);
 | 
			
		||||
	struct str data_home = str_make ();
 | 
			
		||||
	get_xdg_home_dir (&data_home, "XDG_DATA_HOME", ".local/share");
 | 
			
		||||
	strv_append (out, data_home.str);
 | 
			
		||||
	str_free (&data_home);
 | 
			
		||||
@@ -3250,8 +3243,7 @@ get_xdg_data_dirs (struct strv *out)
 | 
			
		||||
static char *
 | 
			
		||||
resolve_relative_data_filename (const char *filename)
 | 
			
		||||
{
 | 
			
		||||
	struct strv paths;
 | 
			
		||||
	strv_init (&paths);
 | 
			
		||||
	struct strv paths = strv_make ();
 | 
			
		||||
	get_xdg_data_dirs (&paths);
 | 
			
		||||
	char *result = resolve_relative_filename_generic
 | 
			
		||||
		(&paths, PROGRAM_NAME "/", filename);
 | 
			
		||||
@@ -3262,9 +3254,7 @@ resolve_relative_data_filename (const char *filename)
 | 
			
		||||
static char *
 | 
			
		||||
resolve_relative_runtime_filename (const char *filename)
 | 
			
		||||
{
 | 
			
		||||
	struct str path;
 | 
			
		||||
	str_init (&path);
 | 
			
		||||
 | 
			
		||||
	struct str path = str_make ();
 | 
			
		||||
	const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
 | 
			
		||||
	if (runtime_dir && *runtime_dir == '/')
 | 
			
		||||
		str_append (&path, runtime_dir);
 | 
			
		||||
@@ -3290,8 +3280,7 @@ try_expand_tilde (const char *filename)
 | 
			
		||||
	size_t until_slash = strcspn (filename, "/");
 | 
			
		||||
	if (!until_slash)
 | 
			
		||||
	{
 | 
			
		||||
		struct str expanded;
 | 
			
		||||
		str_init (&expanded);
 | 
			
		||||
		struct str expanded = str_make ();
 | 
			
		||||
		str_append_env_path (&expanded, "HOME", false);
 | 
			
		||||
		str_append (&expanded, filename);
 | 
			
		||||
		return str_steal (&expanded);
 | 
			
		||||
@@ -3406,11 +3395,10 @@ regex_free (void *regex)
 | 
			
		||||
// Adding basic support for subgroups is easy: check `re_nsub' and output into
 | 
			
		||||
// a `struct strv' (if all we want is the substrings).
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
regex_cache_init (struct str_map *cache)
 | 
			
		||||
static struct str_map
 | 
			
		||||
regex_cache_make (void)
 | 
			
		||||
{
 | 
			
		||||
	str_map_init (cache);
 | 
			
		||||
	cache->free = regex_free;
 | 
			
		||||
	return str_map_make (regex_free);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
@@ -3507,14 +3495,10 @@ write_file_safe (const char *filename, const void *data, size_t data_len,
 | 
			
		||||
 | 
			
		||||
// --- Simple configuration ----------------------------------------------------
 | 
			
		||||
 | 
			
		||||
// The keys are stripped of surrounding whitespace, the values are not.
 | 
			
		||||
// This is the bare minimum to make an application configurable.
 | 
			
		||||
// Keys are stripped of surrounding whitespace, values are not.
 | 
			
		||||
 | 
			
		||||
struct simple_config_item
 | 
			
		||||
{
 | 
			
		||||
	const char *key;
 | 
			
		||||
	const char *default_value;
 | 
			
		||||
	const char *description;
 | 
			
		||||
};
 | 
			
		||||
struct simple_config_item { const char *key, *default_value, *description; };
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
simple_config_load_defaults
 | 
			
		||||
@@ -3532,62 +3516,35 @@ simple_config_update_from_file (struct str_map *config, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	char *filename = resolve_filename
 | 
			
		||||
		(PROGRAM_NAME ".conf", resolve_relative_config_filename);
 | 
			
		||||
	if (!filename)
 | 
			
		||||
		return true;
 | 
			
		||||
 | 
			
		||||
	FILE *fp = fopen (filename, "r");
 | 
			
		||||
	if (!fp)
 | 
			
		||||
	struct str s = str_make ();
 | 
			
		||||
	bool ok = !filename || read_file (filename, &s, e);
 | 
			
		||||
	size_t line_no = 0;
 | 
			
		||||
	for (char *x = strtok (s.str, "\r\n"); ok && x; x = strtok (NULL, "\r\n"))
 | 
			
		||||
	{
 | 
			
		||||
		error_set (e, "could not open `%s' for reading: %s",
 | 
			
		||||
			filename, strerror (errno));
 | 
			
		||||
		free (filename);
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct str line;
 | 
			
		||||
	str_init (&line);
 | 
			
		||||
 | 
			
		||||
	bool errors = false;
 | 
			
		||||
	for (unsigned line_no = 1; read_line (fp, &line); line_no++)
 | 
			
		||||
	{
 | 
			
		||||
		char *start = line.str;
 | 
			
		||||
		if (*start == '#')
 | 
			
		||||
		line_no++;
 | 
			
		||||
		if (strchr ("#", *(x += strspn (x, " \t"))))
 | 
			
		||||
			continue;
 | 
			
		||||
 | 
			
		||||
		while (isspace (*start))
 | 
			
		||||
			start++;
 | 
			
		||||
 | 
			
		||||
		char *end = strchr (start, '=');
 | 
			
		||||
		if (end)
 | 
			
		||||
		char *equals = strchr (x, '=');
 | 
			
		||||
		if (!equals || equals == x)
 | 
			
		||||
			ok = error_set (e, "%s: malformed line %zu", filename, line_no);
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			char *value = end + 1;
 | 
			
		||||
			do
 | 
			
		||||
				*end = '\0';
 | 
			
		||||
			while (isspace (*--end));
 | 
			
		||||
 | 
			
		||||
			str_map_set (config, start, xstrdup (value));
 | 
			
		||||
		}
 | 
			
		||||
		else if (*start)
 | 
			
		||||
		{
 | 
			
		||||
			error_set (e, "line %u in config: %s", line_no, "malformed input");
 | 
			
		||||
			errors = true;
 | 
			
		||||
			break;
 | 
			
		||||
			char *end = equals++;
 | 
			
		||||
			do *end = '\0'; while (strchr (" \t", *--end));
 | 
			
		||||
			str_map_set (config, x, xstrdup (equals));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	str_free (&line);
 | 
			
		||||
	fclose (fp);
 | 
			
		||||
	str_free (&s);
 | 
			
		||||
	free (filename);
 | 
			
		||||
	return !errors;
 | 
			
		||||
	return ok;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *
 | 
			
		||||
write_configuration_file (const char *path_hint, const struct str *data,
 | 
			
		||||
	struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	struct str path;
 | 
			
		||||
	str_init (&path);
 | 
			
		||||
 | 
			
		||||
	struct str path = str_make ();
 | 
			
		||||
	if (path_hint)
 | 
			
		||||
		str_append (&path, path_hint);
 | 
			
		||||
	else
 | 
			
		||||
@@ -3608,9 +3565,7 @@ static char *
 | 
			
		||||
simple_config_write_default (const char *path_hint, const char *prolog,
 | 
			
		||||
	const struct simple_config_item *table, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	struct str data;
 | 
			
		||||
	str_init (&data);
 | 
			
		||||
 | 
			
		||||
	struct str data = str_make ();
 | 
			
		||||
	if (prolog)
 | 
			
		||||
		str_append (&data, prolog);
 | 
			
		||||
 | 
			
		||||
@@ -3702,31 +3657,31 @@ opt_handler_free (struct opt_handler *self)
 | 
			
		||||
	free (self->opt_string);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
opt_handler_init (struct opt_handler *self, int argc, char **argv,
 | 
			
		||||
static struct opt_handler
 | 
			
		||||
opt_handler_make (int argc, char **argv,
 | 
			
		||||
	const struct opt *opts, const char *arg_hint, const char *description)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->argc = argc;
 | 
			
		||||
	self->argv = argv;
 | 
			
		||||
	self->arg_hint = arg_hint;
 | 
			
		||||
	self->description = description;
 | 
			
		||||
	struct opt_handler self =
 | 
			
		||||
	{
 | 
			
		||||
		.argc = argc,
 | 
			
		||||
		.argv = argv,
 | 
			
		||||
		.arg_hint = arg_hint,
 | 
			
		||||
		.description = description,
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	size_t len = 0;
 | 
			
		||||
	for (const struct opt *iter = opts; iter->long_name; iter++)
 | 
			
		||||
		len++;
 | 
			
		||||
 | 
			
		||||
	self->opts = opts;
 | 
			
		||||
	self->opts_len = len;
 | 
			
		||||
	self->options = xcalloc (len + 1, sizeof *self->options);
 | 
			
		||||
 | 
			
		||||
	struct str opt_string;
 | 
			
		||||
	str_init (&opt_string);
 | 
			
		||||
	self.opts = opts;
 | 
			
		||||
	self.opts_len = len;
 | 
			
		||||
	self.options = xcalloc (len + 1, sizeof *self.options);
 | 
			
		||||
 | 
			
		||||
	struct str opt_string = str_make ();
 | 
			
		||||
	for (size_t i = 0; i < len; i++)
 | 
			
		||||
	{
 | 
			
		||||
		const struct opt *opt = opts + i;
 | 
			
		||||
		struct option *mapped = self->options + i;
 | 
			
		||||
		struct option *mapped = self.options + i;
 | 
			
		||||
 | 
			
		||||
		mapped->name = opt->long_name;
 | 
			
		||||
		if (!opt->arg_hint)
 | 
			
		||||
@@ -3748,25 +3703,21 @@ opt_handler_init (struct opt_handler *self, int argc, char **argv,
 | 
			
		||||
				str_append_c (&opt_string, ':');
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	self->opt_string = str_steal (&opt_string);
 | 
			
		||||
	self.opt_string = str_steal (&opt_string);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
opt_handler_usage (const struct opt_handler *self, FILE *stream)
 | 
			
		||||
{
 | 
			
		||||
	struct str usage;
 | 
			
		||||
	str_init (&usage);
 | 
			
		||||
 | 
			
		||||
	struct str usage = str_make ();
 | 
			
		||||
	str_append_printf (&usage, "Usage: %s [OPTION]... %s\n",
 | 
			
		||||
		self->argv[0], self->arg_hint ? self->arg_hint : "");
 | 
			
		||||
	str_append_printf (&usage, "%s\n\n", self->description);
 | 
			
		||||
 | 
			
		||||
	for (size_t i = 0; i < self->opts_len; i++)
 | 
			
		||||
	{
 | 
			
		||||
		struct str row;
 | 
			
		||||
		str_init (&row);
 | 
			
		||||
 | 
			
		||||
		struct str row = str_make ();
 | 
			
		||||
		const struct opt *opt = self->opts + i;
 | 
			
		||||
		if (!(opt->flags & OPT_LONG_ONLY))
 | 
			
		||||
			str_append_printf (&row, "  -%c, ", opt->short_name);
 | 
			
		||||
@@ -3837,8 +3788,8 @@ static void
 | 
			
		||||
test_init (struct test *self, int argc, char **argv)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	str_map_init (&self->whitelist);
 | 
			
		||||
	str_map_init (&self->blacklist);
 | 
			
		||||
	self->whitelist = str_map_make (NULL);
 | 
			
		||||
	self->blacklist = str_map_make (NULL);
 | 
			
		||||
 | 
			
		||||
	// Usually this shouldn't pose a problem but let's make it optional
 | 
			
		||||
	self->can_fork = true;
 | 
			
		||||
@@ -3854,8 +3805,8 @@ test_init (struct test *self, int argc, char **argv)
 | 
			
		||||
		{ 0, NULL, NULL, 0, NULL }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	struct opt_handler oh;
 | 
			
		||||
	opt_handler_init (&oh, argc, argv, opts, NULL, "Unit test runner");
 | 
			
		||||
	struct opt_handler oh =
 | 
			
		||||
		opt_handler_make (argc, argv, opts, NULL, "Unit test runner");
 | 
			
		||||
 | 
			
		||||
	int c;
 | 
			
		||||
	while ((c = opt_handler_get (&oh)) != -1)
 | 
			
		||||
@@ -3925,8 +3876,7 @@ test_add_internal (struct test *self, const char *name, size_t fixture_size,
 | 
			
		||||
static bool
 | 
			
		||||
str_map_glob_match (struct str_map *self, const char *entry)
 | 
			
		||||
{
 | 
			
		||||
	struct str_map_iter iter;
 | 
			
		||||
	str_map_iter_init (&iter, self);
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (self);
 | 
			
		||||
	while (str_map_iter_next (&iter))
 | 
			
		||||
		if (!fnmatch (iter.link->key, entry, 0))
 | 
			
		||||
			return true;
 | 
			
		||||
@@ -4250,7 +4200,7 @@ connector_init (struct connector *self, struct poller *poller)
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->poller = poller;
 | 
			
		||||
	self->socket = -1;
 | 
			
		||||
	poller_fd_init (&self->connected_event, poller, self->socket);
 | 
			
		||||
	self->connected_event = poller_fd_make (poller, self->socket);
 | 
			
		||||
	self->connected_event.user_data = self;
 | 
			
		||||
	self->connected_event.dispatcher = (poller_fd_fn) connector_on_ready;
 | 
			
		||||
}
 | 
			
		||||
@@ -4387,6 +4337,32 @@ socket_io_try_write (int socket_fd, struct str *wb)
 | 
			
		||||
// This is a more powerful configuration format, adding key-value maps and
 | 
			
		||||
// simplifying item validation and dynamic handling of changes.  All strings
 | 
			
		||||
// must be encoded in UTF-8.
 | 
			
		||||
//
 | 
			
		||||
// The syntax is roughly described by the following parsing expression grammar:
 | 
			
		||||
//
 | 
			
		||||
//   config  = entries eof  # as if there were implicit curly braces around
 | 
			
		||||
//   entries = (newline* pair)* newline*
 | 
			
		||||
//   pair    = key newline* lws '=' newline* value (&endobj / newline / eof)
 | 
			
		||||
//   key     = string / !null !boolean lws [A-Za-z_][0-9A-Za-z_]*
 | 
			
		||||
//   value   = object / string / integer / null / boolean
 | 
			
		||||
//
 | 
			
		||||
//   object  = lws '{' entries endobj
 | 
			
		||||
//   endobj  = lws '}'
 | 
			
		||||
//
 | 
			
		||||
//   string  = lws '"' ('\\' escape / ![\\"] char)* '"'
 | 
			
		||||
//   char    = [\0-\177]  # or any Unicode codepoint in the UTF-8 encoding
 | 
			
		||||
//   escape  = [\\"abfnrtv] / [xX][0-9A-Fa-f][0-9A-Fa-f]? / [0-7][0-7]?[0-7]?
 | 
			
		||||
//
 | 
			
		||||
//   integer = lws '-'? [0-9]+  # whatever strtoll() accepts on your system
 | 
			
		||||
//   null    = lws 'null'
 | 
			
		||||
//   boolean = lws 'yes'  / lws 'YES'  / lws 'no'    / lws 'NO'
 | 
			
		||||
//           / lws 'on'   / lws 'ON'   / lws 'off'   / lws 'OFF'
 | 
			
		||||
//           / lws 'true' / lws 'TRUE' / lws 'false' / lws 'FALSE'
 | 
			
		||||
//
 | 
			
		||||
//   newline = lws comment? '\n'
 | 
			
		||||
//   eof     = lws comment? !.
 | 
			
		||||
//   lws     = [ \t\r]*  # linear whitespace (plus CR as it is insignificant)
 | 
			
		||||
//   comment = '#' (!'\n' .)*
 | 
			
		||||
 | 
			
		||||
enum config_item_type
 | 
			
		||||
{
 | 
			
		||||
@@ -4527,7 +4503,7 @@ static struct config_item *
 | 
			
		||||
config_item_string (const struct str *s)
 | 
			
		||||
{
 | 
			
		||||
	struct config_item *self = config_item_new (CONFIG_ITEM_STRING);
 | 
			
		||||
	str_init (&self->value.string);
 | 
			
		||||
	self->value.string = str_make ();
 | 
			
		||||
	hard_assert (utf8_validate
 | 
			
		||||
		(self->value.string.str, self->value.string.len));
 | 
			
		||||
	if (s) str_append_str (&self->value.string, s);
 | 
			
		||||
@@ -4537,8 +4513,7 @@ config_item_string (const struct str *s)
 | 
			
		||||
static struct config_item *
 | 
			
		||||
config_item_string_from_cstr (const char *s)
 | 
			
		||||
{
 | 
			
		||||
	struct str tmp;
 | 
			
		||||
	str_init (&tmp);
 | 
			
		||||
	struct str tmp = str_make ();
 | 
			
		||||
	str_append (&tmp, s);
 | 
			
		||||
	struct config_item *self = config_item_string (&tmp);
 | 
			
		||||
	str_free (&tmp);
 | 
			
		||||
@@ -4557,8 +4532,7 @@ static struct config_item *
 | 
			
		||||
config_item_object (void)
 | 
			
		||||
{
 | 
			
		||||
	struct config_item *self = config_item_new (CONFIG_ITEM_OBJECT);
 | 
			
		||||
	str_map_init (&self->value.object);
 | 
			
		||||
	self->value.object.free = (void (*)(void *)) config_item_destroy;
 | 
			
		||||
	self->value.object = str_map_make ((str_map_free_fn) config_item_destroy);
 | 
			
		||||
	return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -4628,8 +4602,7 @@ config_item_get (struct config_item *self, const char *path, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	hard_assert (self->type == CONFIG_ITEM_OBJECT);
 | 
			
		||||
 | 
			
		||||
	struct strv v;
 | 
			
		||||
	strv_init (&v);
 | 
			
		||||
	struct strv v = strv_make ();
 | 
			
		||||
	cstr_split (path, ".", false, &v);
 | 
			
		||||
 | 
			
		||||
	struct config_item *result = NULL;
 | 
			
		||||
@@ -4768,9 +4741,7 @@ config_item_write_object_innards
 | 
			
		||||
{
 | 
			
		||||
	hard_assert (object->type == CONFIG_ITEM_OBJECT);
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter;
 | 
			
		||||
	str_map_iter_init (&iter, &object->value.object);
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (&object->value.object);
 | 
			
		||||
	struct config_item *value;
 | 
			
		||||
	while ((value = str_map_iter_next (&iter)))
 | 
			
		||||
		config_item_write_kv_pair (self, iter.link->key, value);
 | 
			
		||||
@@ -4845,14 +4816,11 @@ struct config_tokenizer
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// Input has to be null-terminated anyway
 | 
			
		||||
static void
 | 
			
		||||
config_tokenizer_init (struct config_tokenizer *self, const char *p, size_t len)
 | 
			
		||||
static struct config_tokenizer
 | 
			
		||||
config_tokenizer_make (const char *p, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	self->p = p;
 | 
			
		||||
	self->len = len;
 | 
			
		||||
	self->report_line = true;
 | 
			
		||||
	str_init (&self->string);
 | 
			
		||||
	return (struct config_tokenizer)
 | 
			
		||||
		{ .p = p, .len = len, .report_line = true, .string = str_make () };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -4890,8 +4858,7 @@ static void
 | 
			
		||||
config_tokenizer_error (struct config_tokenizer *self,
 | 
			
		||||
	struct error **e, const char *format, ...)
 | 
			
		||||
{
 | 
			
		||||
	struct str description;
 | 
			
		||||
	str_init (&description);
 | 
			
		||||
	struct str description = str_make ();
 | 
			
		||||
 | 
			
		||||
	va_list ap;
 | 
			
		||||
	va_start (ap, format);
 | 
			
		||||
@@ -5108,15 +5075,16 @@ struct config_parser
 | 
			
		||||
	bool replace_token;                 ///< Replace the token
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
config_parser_init (struct config_parser *self, const char *script, size_t len)
 | 
			
		||||
static struct config_parser
 | 
			
		||||
config_parser_make (const char *script, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	config_tokenizer_init (&self->tokenizer, script, len);
 | 
			
		||||
 | 
			
		||||
	// As reading in tokens may cause exceptions, we wait for the first peek()
 | 
			
		||||
	// to replace the initial CONFIG_T_ABORT.
 | 
			
		||||
	self->replace_token = true;
 | 
			
		||||
	return (struct config_parser)
 | 
			
		||||
	{
 | 
			
		||||
		.tokenizer = config_tokenizer_make (script, len),
 | 
			
		||||
		.replace_token = true,
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -5286,9 +5254,7 @@ static struct config_item *
 | 
			
		||||
config_item_parse (const char *script, size_t len,
 | 
			
		||||
	bool single_value_only, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	struct config_parser parser;
 | 
			
		||||
	config_parser_init (&parser, script, len);
 | 
			
		||||
 | 
			
		||||
	struct config_parser parser = config_parser_make (script, len);
 | 
			
		||||
	struct config_item *volatile object = NULL;
 | 
			
		||||
	jmp_buf err;
 | 
			
		||||
 | 
			
		||||
@@ -5325,8 +5291,7 @@ static struct config_item *
 | 
			
		||||
config_item_clone (struct config_item *self)
 | 
			
		||||
{
 | 
			
		||||
	// Oh well, it saves code
 | 
			
		||||
	struct str tmp;
 | 
			
		||||
	str_init (&tmp);
 | 
			
		||||
	struct str tmp = str_make ();
 | 
			
		||||
	config_item_write (self, false, &tmp);
 | 
			
		||||
	struct config_item *result =
 | 
			
		||||
		config_item_parse (tmp.str, tmp.len, true, NULL);
 | 
			
		||||
@@ -5339,8 +5304,7 @@ config_read_from_file (const char *filename, struct error **e)
 | 
			
		||||
{
 | 
			
		||||
	struct config_item *root = NULL;
 | 
			
		||||
 | 
			
		||||
	struct str data;
 | 
			
		||||
	str_init (&data);
 | 
			
		||||
	struct str data = str_make ();
 | 
			
		||||
	if (!read_file (filename, &data, e))
 | 
			
		||||
		goto end;
 | 
			
		||||
 | 
			
		||||
@@ -5440,9 +5404,7 @@ config_schema_call_changed (struct config_item *item)
 | 
			
		||||
{
 | 
			
		||||
	if (item->type == CONFIG_ITEM_OBJECT)
 | 
			
		||||
	{
 | 
			
		||||
		struct str_map_iter iter;
 | 
			
		||||
		str_map_iter_init (&iter, &item->value.object);
 | 
			
		||||
 | 
			
		||||
		struct str_map_iter iter = str_map_iter_make (&item->value.object);
 | 
			
		||||
		struct config_item *child;
 | 
			
		||||
		while ((child = str_map_iter_next (&iter)))
 | 
			
		||||
			config_schema_call_changed (child);
 | 
			
		||||
@@ -5478,12 +5440,11 @@ struct config
 | 
			
		||||
	struct config_item *root;           ///< CONFIG_ITEM_OBJECT
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
config_init (struct config *self)
 | 
			
		||||
static struct config
 | 
			
		||||
config_make (void)
 | 
			
		||||
{
 | 
			
		||||
	memset (self, 0, sizeof *self);
 | 
			
		||||
	str_map_init (&self->modules);
 | 
			
		||||
	self->modules.free = (str_map_free_fn) config_module_destroy;
 | 
			
		||||
	return (struct config)
 | 
			
		||||
		{ .modules = str_map_make ((str_map_free_fn) config_module_destroy) };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -5514,9 +5475,7 @@ config_load (struct config *self, struct config_item *root)
 | 
			
		||||
		config_item_destroy (self->root);
 | 
			
		||||
	self->root = root;
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter;
 | 
			
		||||
	str_map_iter_init (&iter, &self->modules);
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (&self->modules);
 | 
			
		||||
	struct config_module *module;
 | 
			
		||||
	while ((module = str_map_iter_next (&iter)))
 | 
			
		||||
	{
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ fakeroot sh -e <<-EOF
 | 
			
		||||
	url = $url
 | 
			
		||||
	builddate = \`date -u +%s\`
 | 
			
		||||
	packager = $author
 | 
			
		||||
	size = \`du -sb --apparent-size | cut -f1\`
 | 
			
		||||
	size = \`du -sb | cut -f1\`
 | 
			
		||||
	arch = $arch
 | 
			
		||||
	END
 | 
			
		||||
	cd "$wd" && tar cJf "../$target" .PKGINFO *
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								siphash.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								siphash.c
									
									
									
									
									
								
							@@ -61,13 +61,13 @@ siphash (const unsigned char key[16], const unsigned char *m, size_t len)
 | 
			
		||||
 | 
			
		||||
	switch (len - blocks)
 | 
			
		||||
	{
 | 
			
		||||
	case 7: last7 |= (uint64_t) m[i + 6] << 48;
 | 
			
		||||
	case 6: last7 |= (uint64_t) m[i + 5] << 40;
 | 
			
		||||
	case 5: last7 |= (uint64_t) m[i + 4] << 32;
 | 
			
		||||
	case 4: last7 |= (uint64_t) m[i + 3] << 24;
 | 
			
		||||
	case 3: last7 |= (uint64_t) m[i + 2] << 16;
 | 
			
		||||
	case 2: last7 |= (uint64_t) m[i + 1] <<  8;
 | 
			
		||||
	case 1: last7 |= (uint64_t) m[i + 0]      ;
 | 
			
		||||
	case 7: last7 |= (uint64_t) m[i + 6] << 48; // Fall-through
 | 
			
		||||
	case 6: last7 |= (uint64_t) m[i + 5] << 40; // Fall-through
 | 
			
		||||
	case 5: last7 |= (uint64_t) m[i + 4] << 32; // Fall-through
 | 
			
		||||
	case 4: last7 |= (uint64_t) m[i + 3] << 24; // Fall-through
 | 
			
		||||
	case 3: last7 |= (uint64_t) m[i + 2] << 16; // Fall-through
 | 
			
		||||
	case 2: last7 |= (uint64_t) m[i + 1] <<  8; // Fall-through
 | 
			
		||||
	case 1: last7 |= (uint64_t) m[i + 0]      ; // Fall-through
 | 
			
		||||
	default:;
 | 
			
		||||
	};
 | 
			
		||||
	v3 ^= last7;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,10 @@
 | 
			
		||||
/*
 | 
			
		||||
 * tests/liberty.c
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2015 - 2016, Přemysl Janouch <p.janouch@gmail.com>
 | 
			
		||||
 * Copyright (c) 2015 - 2016, Přemysl Janouch <p@janouch.name>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -158,9 +157,7 @@ test_list_with_tail (void)
 | 
			
		||||
static void
 | 
			
		||||
test_strv (void)
 | 
			
		||||
{
 | 
			
		||||
	struct strv v;
 | 
			
		||||
	strv_init (&v);
 | 
			
		||||
 | 
			
		||||
	struct strv v = strv_make ();
 | 
			
		||||
	strv_append_owned (&v, xstrdup ("xkcd"));
 | 
			
		||||
	strv_reset (&v);
 | 
			
		||||
 | 
			
		||||
@@ -168,8 +165,7 @@ test_strv (void)
 | 
			
		||||
		{ "123", "456", "a", "bc", "def", "ghij", "klmno", "pqrstu" };
 | 
			
		||||
 | 
			
		||||
	// Add the first two items via another vector
 | 
			
		||||
	struct strv w;
 | 
			
		||||
	strv_init (&w);
 | 
			
		||||
	struct strv w = strv_make ();
 | 
			
		||||
	strv_append_args (&w, a[0], a[1], NULL);
 | 
			
		||||
	strv_append_vector (&v, w.vector);
 | 
			
		||||
	strv_free (&w);
 | 
			
		||||
@@ -196,15 +192,13 @@ test_str (void)
 | 
			
		||||
{
 | 
			
		||||
	uint8_t x[] = { 0x12, 0x34, 0x56, 0x78, 0x11, 0x22, 0x33, 0x44 };
 | 
			
		||||
 | 
			
		||||
	struct str s;
 | 
			
		||||
	str_init (&s);
 | 
			
		||||
	struct str s = str_make ();
 | 
			
		||||
	str_reserve (&s, MEGA);
 | 
			
		||||
	str_append_data (&s, x, sizeof x);
 | 
			
		||||
	str_remove_slice (&s, 4, 4);
 | 
			
		||||
	soft_assert (s.len == 4);
 | 
			
		||||
 | 
			
		||||
	struct str t;
 | 
			
		||||
	str_init (&t);
 | 
			
		||||
	struct str t = str_make ();
 | 
			
		||||
	str_append_str (&t, &s);
 | 
			
		||||
	str_append (&t, "abc");
 | 
			
		||||
	str_append_c (&t, 'd');
 | 
			
		||||
@@ -265,10 +259,8 @@ static void
 | 
			
		||||
test_str_map (void)
 | 
			
		||||
{
 | 
			
		||||
	// Put two reference counted objects in the map under case-insensitive keys
 | 
			
		||||
	struct str_map m;
 | 
			
		||||
	str_map_init (&m);
 | 
			
		||||
	struct str_map m = str_map_make (free_counter);
 | 
			
		||||
	m.key_xfrm = tolower_ascii_strxfrm;
 | 
			
		||||
	m.free = free_counter;
 | 
			
		||||
 | 
			
		||||
	int *a = make_counter ();
 | 
			
		||||
	int *b = make_counter ();
 | 
			
		||||
@@ -282,8 +274,7 @@ test_str_map (void)
 | 
			
		||||
	soft_assert (str_map_find (&m, "DEFghi") == b);
 | 
			
		||||
 | 
			
		||||
	// Check that we can iterate over both of them
 | 
			
		||||
	struct str_map_iter iter;
 | 
			
		||||
	str_map_iter_init (&iter, &m);
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (&m);
 | 
			
		||||
 | 
			
		||||
	bool met_a = false;
 | 
			
		||||
	bool met_b = false;
 | 
			
		||||
@@ -310,8 +301,7 @@ test_str_map (void)
 | 
			
		||||
	free_counter (b);
 | 
			
		||||
 | 
			
		||||
	// Iterator test with a high number of items
 | 
			
		||||
	str_map_init (&m);
 | 
			
		||||
	m.free = free;
 | 
			
		||||
	m = str_map_make (free);
 | 
			
		||||
 | 
			
		||||
	for (size_t i = 0; i < 100 * 100; i++)
 | 
			
		||||
	{
 | 
			
		||||
@@ -319,8 +309,7 @@ test_str_map (void)
 | 
			
		||||
		str_map_set (&m, x, x);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct str_map_unset_iter unset_iter;
 | 
			
		||||
	str_map_unset_iter_init (&unset_iter, &m);
 | 
			
		||||
	struct str_map_unset_iter unset_iter = str_map_unset_iter_make (&m);
 | 
			
		||||
	while ((str_map_unset_iter_next (&unset_iter)))
 | 
			
		||||
	{
 | 
			
		||||
		unsigned long x;
 | 
			
		||||
@@ -342,9 +331,7 @@ test_utf8 (void)
 | 
			
		||||
	soft_assert ( utf8_validate (valid,   sizeof valid));
 | 
			
		||||
	soft_assert (!utf8_validate (invalid, sizeof invalid));
 | 
			
		||||
 | 
			
		||||
	struct utf8_iter iter;
 | 
			
		||||
	utf8_iter_init (&iter, "fóọ");
 | 
			
		||||
 | 
			
		||||
	struct utf8_iter iter = utf8_iter_make ("fóọ");
 | 
			
		||||
	size_t ch_len;
 | 
			
		||||
	hard_assert (utf8_iter_next (&iter, &ch_len) == 'f'    && ch_len == 1);
 | 
			
		||||
	hard_assert (utf8_iter_next (&iter, &ch_len) == 0x00F3 && ch_len == 2);
 | 
			
		||||
@@ -358,8 +345,8 @@ test_base64 (void)
 | 
			
		||||
	for (size_t i = 0; i < N_ELEMENTS (data); i++)
 | 
			
		||||
		data[i] = i;
 | 
			
		||||
 | 
			
		||||
	struct str encoded;  str_init (&encoded);
 | 
			
		||||
	struct str decoded;  str_init (&decoded);
 | 
			
		||||
	struct str encoded = str_make ();
 | 
			
		||||
	struct str decoded = str_make ();
 | 
			
		||||
 | 
			
		||||
	base64_encode (data, sizeof data, &encoded);
 | 
			
		||||
	soft_assert (base64_decode (encoded.str, false, &decoded));
 | 
			
		||||
@@ -430,9 +417,9 @@ test_async (void)
 | 
			
		||||
{
 | 
			
		||||
	struct test_async_data data;
 | 
			
		||||
	memset (&data, 0, sizeof data);
 | 
			
		||||
	async_manager_init (&data.manager);
 | 
			
		||||
	data.manager = async_manager_make ();
 | 
			
		||||
 | 
			
		||||
	async_init (&data.busyloop, &data.manager);
 | 
			
		||||
	data.busyloop = async_make (&data.manager);
 | 
			
		||||
	data.busyloop.execute = on_busyloop_execute;
 | 
			
		||||
	data.busyloop.destroy = on_busyloop_destroy;
 | 
			
		||||
	async_run (&data.busyloop);
 | 
			
		||||
@@ -542,7 +529,7 @@ test_connector_fixture_init
 | 
			
		||||
 | 
			
		||||
	// Make it so that we immediately accept all connections
 | 
			
		||||
	poller_init (&self->poller);
 | 
			
		||||
	poller_fd_init (&self->listening_event, &self->poller, self->listening_fd);
 | 
			
		||||
	self->listening_event = poller_fd_make (&self->poller, self->listening_fd);
 | 
			
		||||
	self->listening_event.dispatcher = test_connector_on_client;
 | 
			
		||||
	self->listening_event.user_data = (poller_fd_fn) self;
 | 
			
		||||
	poller_fd_set (&self->listening_event, POLLIN);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,10 @@
 | 
			
		||||
/*
 | 
			
		||||
 * tests/proto.c
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2015, Přemysl Janouch <p.janouch@gmail.com>
 | 
			
		||||
 * Copyright (c) 2015, Přemysl Janouch <p@janouch.name>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and/or distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -43,8 +42,7 @@ test_irc (void)
 | 
			
		||||
	irc_parse_message (&msg, "@first=a\\:\\s\\r\\n\\\\;2nd "
 | 
			
		||||
		":srv hi there :good m8 :how are you?");
 | 
			
		||||
 | 
			
		||||
	struct str_map_iter iter;
 | 
			
		||||
	str_map_iter_init (&iter, &msg.tags);
 | 
			
		||||
	struct str_map_iter iter = str_map_iter_make (&msg.tags);
 | 
			
		||||
	soft_assert (msg.tags.len == 2);
 | 
			
		||||
 | 
			
		||||
	char *value;
 | 
			
		||||
@@ -79,8 +77,7 @@ test_irc (void)
 | 
			
		||||
static void
 | 
			
		||||
test_http_parser (void)
 | 
			
		||||
{
 | 
			
		||||
	struct str_map parameters;
 | 
			
		||||
	str_map_init (¶meters);
 | 
			
		||||
	struct str_map parameters = str_map_make (NULL);
 | 
			
		||||
	parameters.key_xfrm = tolower_ascii_strxfrm;
 | 
			
		||||
 | 
			
		||||
	char *type = NULL;
 | 
			
		||||
@@ -135,8 +132,7 @@ test_scgi_parser_on_content (void *user_data, const void *data, size_t len)
 | 
			
		||||
static void
 | 
			
		||||
test_scgi_parser (void)
 | 
			
		||||
{
 | 
			
		||||
	struct scgi_parser parser;
 | 
			
		||||
	scgi_parser_init (&parser);
 | 
			
		||||
	struct scgi_parser parser = scgi_parser_make ();
 | 
			
		||||
	parser.on_headers_read = test_scgi_parser_on_headers_read;
 | 
			
		||||
	parser.on_content      = test_scgi_parser_on_content;
 | 
			
		||||
	parser.user_data       = &parser;
 | 
			
		||||
@@ -181,8 +177,7 @@ test_websockets (void)
 | 
			
		||||
	soft_assert (!strcmp (accept, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="));
 | 
			
		||||
	free (accept);
 | 
			
		||||
 | 
			
		||||
	struct ws_parser parser;
 | 
			
		||||
	ws_parser_init (&parser);
 | 
			
		||||
	struct ws_parser parser = ws_parser_make ();
 | 
			
		||||
	parser.on_frame_header = test_websockets_on_frame_header;
 | 
			
		||||
	parser.on_frame        = test_websockets_on_frame;
 | 
			
		||||
	parser.user_data       = &parser;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user