Compare commits
90 Commits
02708608a9
...
bb30c7d86e
| Author | SHA1 | Date | |
|---|---|---|---|
|
bb30c7d86e
|
|||
|
47ef2ae5bd
|
|||
|
69800a6afb
|
|||
|
fe1035633a
|
|||
|
da75b6f735
|
|||
|
199c56e141
|
|||
|
6e9217e5d0
|
|||
|
3835b6e499
|
|||
|
bf534010cb
|
|||
|
7b0d7a19e5
|
|||
|
1dcd259d05
|
|||
|
03894cae45
|
|||
|
412100289e
|
|||
|
ec128558a4
|
|||
|
7f7606008d
|
|||
|
17322a3686
|
|||
|
22edb6d489
|
|||
|
9866675bb7
|
|||
|
e25a880883
|
|||
|
9afcb337ad
|
|||
|
daa900e12f
|
|||
|
4a5929b4ef
|
|||
|
084e964286
|
|||
|
0e08055d6d
|
|||
|
6642bdf9cd
|
|||
|
349a0fc3b1
|
|||
|
5552ce1dbe
|
|||
|
680980632d
|
|||
|
973a4b7656
|
|||
|
74b00a921a
|
|||
|
f53b717f3b
|
|||
|
dfc7ff57ef
|
|||
|
2a15b1de70
|
|||
|
dc54db9069
|
|||
|
0b77bdeaf9
|
|||
|
a34ce0b6b8
|
|||
|
ad143fd8c0
|
|||
|
296cc704a1
|
|||
|
c1c191717f
|
|||
|
952cf985dc
|
|||
|
6234f686e0
|
|||
|
b07d9df5fc
|
|||
| 3cc975bb2a | |||
| 365aed456e | |||
| 1051ad555a | |||
| 9bff16f5ec | |||
| 052d2ffc9a | |||
| ce8703cea0 | |||
| f213a76ad4 | |||
| 61ca0c988f | |||
| 8a9a28231b | |||
| ff046ea596 | |||
| 38d105dede | |||
| a90aeaf0d9 | |||
| 60dd23ab8f | |||
| 9e3cb2b6aa | |||
| f90cc1e5a2 | |||
| bc7e83137e | |||
| 8c06ec3276 | |||
| 6a19b51516 | |||
| 3f20b39b71 | |||
| 385b3bdb47 | |||
| 020a11a5b5 | |||
| 9957adc458 | |||
| 4dfd88c2c8 | |||
| b5724a654a | |||
| 66340e08d7 | |||
| 733de7bae2 | |||
| 13d04e7a35 | |||
| 455f2cec82 | |||
| ee40af0031 | |||
| 80815519b3 | |||
| f6d74544f8 | |||
| 2d8a8e0b1b | |||
| a4313ee4b9 | |||
| 8b2e41ed8f | |||
| 91fca5cb05 | |||
| 51663d5ee3 | |||
| 5d3e911f01 | |||
| 75d063e363 | |||
| 122ab355a6 | |||
| 0adcaf67c2 | |||
| 835f0a36db | |||
| 6b1273f43c | |||
| bb3d669c3b | |||
| ee2457df7c | |||
| 649c351560 | |||
| 792b074e3d | |||
| bb2de1fd48 | |||
| 1a305a1c6b |
18
.travis.yml
18
.travis.yml
@@ -1,18 +0,0 @@
|
||||
language: c
|
||||
notifications:
|
||||
irc:
|
||||
channels: "anathema.us.nu#anathema"
|
||||
use_notice: true
|
||||
skip_join: true
|
||||
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,33 +2,45 @@ 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)
|
||||
include (AddThreads)
|
||||
|
||||
find_package (PkgConfig REQUIRED)
|
||||
pkg_check_modules (libssl REQUIRED libssl libcrypto)
|
||||
|
||||
# -lpthread is only there for debugging (gdb & errno)
|
||||
# -lrt is only for glibc < 2.17
|
||||
set (common_libraries ${libssl_LIBRARIES} rt pthread)
|
||||
if ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
|
||||
include_directories (/usr/local/include)
|
||||
link_directories (/usr/local/lib)
|
||||
# Our POSIX version macros make these undefined
|
||||
add_definitions (-D__BSD_VISIBLE=1 -D_BSD_SOURCE=1)
|
||||
endif ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
|
||||
|
||||
set (common_libraries ${libssl_LIBRARIES})
|
||||
include_directories (${libssl_INCLUDE_DIRS})
|
||||
link_directories (${libssl_LIBRARY_DIRS})
|
||||
|
||||
# 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)
|
||||
# -lrt is only for glibc < 2.17
|
||||
# -liconv may or may not be a part of libc
|
||||
foreach (extra iconv rt)
|
||||
find_library (extra_lib_${extra} ${extra})
|
||||
if (extra_lib_${extra})
|
||||
list (APPEND common_libraries ${extra})
|
||||
endif (extra_lib_${extra})
|
||||
endforeach (extra)
|
||||
|
||||
# Build some unit tests
|
||||
include_directories (${PROJECT_SOURCE_DIR})
|
||||
enable_testing ()
|
||||
foreach (name liberty proto)
|
||||
add_executable (test-${name} tests/${name}.c ${common_sources})
|
||||
add_threads (test-${name})
|
||||
target_link_libraries (test-${name} ${common_libraries})
|
||||
add_test (test-${name} test-${name})
|
||||
add_test (NAME test-${name} COMMAND test-${name})
|
||||
endforeach (name)
|
||||
|
||||
22
LICENSE
22
LICENSE
@@ -1,14 +1,12 @@
|
||||
Copyright (c) 2014 - 2015, Přemysl Janouch <p.janouch@gmail.com>
|
||||
All rights reserved.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
liberty
|
||||
=======
|
||||
|
||||
`liberty' is a pseudolibrary of all the common C code I have written for various
|
||||
'liberty' is a pseudolibrary of all the common C code I have written for various
|
||||
projects. I used to copy-paste large swaths of code with minimal changes to it
|
||||
and it slowly became awfully painful to synchronize. The project can be thought
|
||||
of as a successor to my other C library, libxtnd.
|
||||
@@ -15,14 +15,17 @@ The API is intentionally unstable, which allows for easy refactoring.
|
||||
|
||||
All development is done on Linux, but other POSIX-compatible operating systems
|
||||
should be supported as well. They have an extremely low priority, however, and
|
||||
I'm not testing them at all.
|
||||
I'm not testing them at all, with the exception of OpenBSD.
|
||||
|
||||
Contributing and Support
|
||||
------------------------
|
||||
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 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.
|
||||
23
cmake/AddThreads.cmake
Normal file
23
cmake/AddThreads.cmake
Normal file
@@ -0,0 +1,23 @@
|
||||
# Public Domain
|
||||
|
||||
# We're looking for pthreads only, while preferring the -pthread flag
|
||||
set (CMAKE_THREAD_PREFER_PTHREAD ON)
|
||||
set (THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package (Threads)
|
||||
|
||||
# Prepares the given target for threads
|
||||
function (add_threads target)
|
||||
if (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
|
||||
message (FATAL_ERROR "pthreads not found")
|
||||
endif (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
|
||||
|
||||
if (THREADS_HAVE_PTHREAD_ARG)
|
||||
set_property (TARGET ${target} PROPERTY
|
||||
COMPILE_OPTIONS "-pthread")
|
||||
set_property (TARGET ${target} PROPERTY
|
||||
INTERFACE_COMPILE_OPTIONS "-pthread")
|
||||
endif (THREADS_HAVE_PTHREAD_ARG)
|
||||
if (CMAKE_THREAD_LIBS_INIT)
|
||||
target_link_libraries (${target} "${CMAKE_THREAD_LIBS_INIT}")
|
||||
endif (CMAKE_THREAD_LIBS_INIT)
|
||||
endfunction (add_threads)
|
||||
17
cmake/FindNcursesw.cmake
Normal file
17
cmake/FindNcursesw.cmake
Normal file
@@ -0,0 +1,17 @@
|
||||
# Public Domain
|
||||
|
||||
find_package (PkgConfig REQUIRED)
|
||||
pkg_check_modules (NCURSESW QUIET ncursesw)
|
||||
|
||||
# OpenBSD doesn't provide a pkg-config file
|
||||
set (required_vars NCURSESW_LIBRARIES)
|
||||
if (NOT NCURSESW_FOUND)
|
||||
find_library (NCURSESW_LIBRARIES NAMES ncursesw)
|
||||
find_path (NCURSESW_INCLUDE_DIRS ncurses.h)
|
||||
list (APPEND required_vars NCURSESW_INCLUDE_DIRS)
|
||||
endif (NOT NCURSESW_FOUND)
|
||||
|
||||
include (FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS (NCURSESW DEFAULT_MSG ${required_vars})
|
||||
|
||||
mark_as_advanced (NCURSESW_LIBRARIES NCURSESW_INCLUDE_DIRS)
|
||||
10
cmake/FindUnistring.cmake
Normal file
10
cmake/FindUnistring.cmake
Normal file
@@ -0,0 +1,10 @@
|
||||
# Public Domain
|
||||
|
||||
find_path (UNISTRING_INCLUDE_DIRS unistr.h)
|
||||
find_library (UNISTRING_LIBRARIES NAMES unistring libunistring)
|
||||
|
||||
include (FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS (UNISTRING DEFAULT_MSG
|
||||
UNISTRING_INCLUDE_DIRS UNISTRING_LIBRARIES)
|
||||
|
||||
mark_as_advanced (UNISTRING_LIBRARIES UNISTRING_INCLUDE_DIRS)
|
||||
814
liberty-proto.c
814
liberty-proto.c
File diff suppressed because it is too large
Load Diff
270
liberty-tui.c
Normal file
270
liberty-tui.c
Normal file
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* liberty-tui.c: the ultimate C unlibrary: TUI
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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 file includes some common stuff to build TUI applications with
|
||||
|
||||
#include <ncurses.h>
|
||||
|
||||
// It is surprisingly hard to find a good library to handle Unicode shenanigans,
|
||||
// and there's enough of those for it to be impractical to reimplement them.
|
||||
//
|
||||
// GLib ICU libunistring utf8proc
|
||||
// Decently sized . . x x
|
||||
// Grapheme breaks . x . x
|
||||
// Character width x . x x
|
||||
// Locale handling . . x .
|
||||
// Liberal license . x . x
|
||||
//
|
||||
// Also note that the ICU API is icky and uses UTF-16 for its primary encoding.
|
||||
//
|
||||
// Currently we're chugging along with libunistring but utf8proc seems viable.
|
||||
// Non-Unicode locales can mostly be handled with simple iconv like in sdtui.
|
||||
// Similarly grapheme breaks can be guessed at using character width (a basic
|
||||
// test here is Zalgo text).
|
||||
//
|
||||
// None of this is ever going to work too reliably anyway because terminals
|
||||
// and Unicode don't go awfully well together. In particular, character cell
|
||||
// devices have some problems with double-wide characters.
|
||||
|
||||
#include <unistr.h>
|
||||
#include <uniwidth.h>
|
||||
#include <uniconv.h>
|
||||
#include <unicase.h>
|
||||
|
||||
// --- Configurable display attributes -----------------------------------------
|
||||
|
||||
struct attrs
|
||||
{
|
||||
short fg; ///< Foreground colour index
|
||||
short bg; ///< Background colour index
|
||||
chtype attrs; ///< Other attributes
|
||||
};
|
||||
|
||||
/// Decode attributes in the value using a subset of the git config format,
|
||||
/// ignoring all errors since it doesn't affect functionality
|
||||
static struct attrs
|
||||
attrs_decode (const char *value)
|
||||
{
|
||||
struct strv v = strv_make ();
|
||||
cstr_split (value, " ", true, &v);
|
||||
|
||||
int colors = 0;
|
||||
struct attrs attrs = { -1, -1, 0 };
|
||||
for (char **it = v.vector; *it; it++)
|
||||
{
|
||||
char *end = NULL;
|
||||
long n = strtol (*it, &end, 10);
|
||||
if (*it != end && !*end && n >= SHRT_MIN && n <= SHRT_MAX)
|
||||
{
|
||||
if (colors == 0) attrs.fg = n;
|
||||
if (colors == 1) attrs.bg = n;
|
||||
colors++;
|
||||
}
|
||||
else if (!strcmp (*it, "bold")) attrs.attrs |= A_BOLD;
|
||||
else if (!strcmp (*it, "dim")) attrs.attrs |= A_DIM;
|
||||
else if (!strcmp (*it, "ul")) attrs.attrs |= A_UNDERLINE;
|
||||
else if (!strcmp (*it, "blink")) attrs.attrs |= A_BLINK;
|
||||
else if (!strcmp (*it, "reverse")) attrs.attrs |= A_REVERSE;
|
||||
#ifdef A_ITALIC
|
||||
else if (!strcmp (*it, "italic")) attrs.attrs |= A_ITALIC;
|
||||
#endif // A_ITALIC
|
||||
}
|
||||
strv_free (&v);
|
||||
return attrs;
|
||||
}
|
||||
|
||||
// --- Terminal output ---------------------------------------------------------
|
||||
|
||||
// Necessary abstraction to simplify aligned, formatted character output
|
||||
|
||||
// This callback you need to implement in the application
|
||||
static bool app_is_character_in_locale (ucs4_t ch);
|
||||
|
||||
struct row_char
|
||||
{
|
||||
ucs4_t c; ///< Unicode codepoint
|
||||
chtype attrs; ///< Special attributes
|
||||
int width; ///< How many cells this takes
|
||||
};
|
||||
|
||||
struct row_buffer
|
||||
{
|
||||
ARRAY (struct row_char, chars) ///< Characters
|
||||
int total_width; ///< Total width of all characters
|
||||
};
|
||||
|
||||
static struct row_buffer
|
||||
row_buffer_make (void)
|
||||
{
|
||||
struct row_buffer self = {};
|
||||
ARRAY_INIT_SIZED (self.chars, 256);
|
||||
return self;
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_free (struct row_buffer *self)
|
||||
{
|
||||
free (self->chars);
|
||||
}
|
||||
|
||||
/// Replace invalid chars and push all codepoints to the array w/ attributes.
|
||||
static void
|
||||
row_buffer_append (struct row_buffer *self, const char *str, chtype attrs)
|
||||
{
|
||||
// The encoding is only really used internally for some corner cases
|
||||
const char *encoding = locale_charset ();
|
||||
|
||||
// Note that this function is a hotspot, try to keep it decently fast
|
||||
struct row_char current = { .attrs = attrs };
|
||||
struct row_char invalid = { .attrs = attrs, .c = '?', .width = 1 };
|
||||
const uint8_t *next = (const uint8_t *) str;
|
||||
while ((next = u8_next (¤t.c, next)))
|
||||
{
|
||||
current.width = uc_width (current.c, encoding);
|
||||
if (current.width < 0 || !app_is_character_in_locale (current.c))
|
||||
current = invalid;
|
||||
|
||||
ARRAY_RESERVE (self->chars, 1);
|
||||
self->chars[self->chars_len++] = current;
|
||||
self->total_width += current.width;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_append_args (struct row_buffer *self, const char *s, ...)
|
||||
ATTRIBUTE_SENTINEL;
|
||||
|
||||
static void
|
||||
row_buffer_append_args (struct row_buffer *self, const char *s, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start (ap, s);
|
||||
|
||||
while (s)
|
||||
{
|
||||
row_buffer_append (self, s, va_arg (ap, chtype));
|
||||
s = va_arg (ap, const char *);
|
||||
}
|
||||
va_end (ap);
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_append_buffer (struct row_buffer *self, const struct row_buffer *rb)
|
||||
{
|
||||
ARRAY_RESERVE (self->chars, rb->chars_len);
|
||||
memcpy (self->chars + self->chars_len, rb->chars,
|
||||
rb->chars_len * sizeof *rb->chars);
|
||||
|
||||
self->chars_len += rb->chars_len;
|
||||
self->total_width += rb->total_width;
|
||||
}
|
||||
|
||||
/// Pop as many codepoints as needed to free up "space" character cells.
|
||||
/// Given the suffix nature of combining marks, this should work pretty fine.
|
||||
static int
|
||||
row_buffer_pop_cells (struct row_buffer *self, int space)
|
||||
{
|
||||
int made = 0;
|
||||
while (self->chars_len && made < space)
|
||||
made += self->chars[--self->chars_len].width;
|
||||
self->total_width -= made;
|
||||
return made;
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_space (struct row_buffer *self, int width, chtype attrs)
|
||||
{
|
||||
if (width < 0)
|
||||
return;
|
||||
|
||||
ARRAY_RESERVE (self->chars, (size_t) width);
|
||||
|
||||
struct row_char space = { .attrs = attrs, .c = ' ', .width = 1 };
|
||||
self->total_width += width;
|
||||
while (width-- > 0)
|
||||
self->chars[self->chars_len++] = space;
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_ellipsis (struct row_buffer *self, int target)
|
||||
{
|
||||
if (self->total_width <= target
|
||||
|| !row_buffer_pop_cells (self, self->total_width - target))
|
||||
return;
|
||||
|
||||
// We use attributes from the last character we've removed,
|
||||
// assuming that we don't shrink the array (and there's no real need)
|
||||
ucs4_t ellipsis = 0x2026; // …
|
||||
if (app_is_character_in_locale (ellipsis))
|
||||
{
|
||||
if (self->total_width >= target)
|
||||
row_buffer_pop_cells (self, 1);
|
||||
if (self->total_width + 1 <= target)
|
||||
row_buffer_append (self, "…", self->chars[self->chars_len].attrs);
|
||||
}
|
||||
else if (target >= 3)
|
||||
{
|
||||
if (self->total_width >= target)
|
||||
row_buffer_pop_cells (self, 3);
|
||||
if (self->total_width + 3 <= target)
|
||||
row_buffer_append (self, "...", self->chars[self->chars_len].attrs);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_align (struct row_buffer *self, int target, chtype attrs)
|
||||
{
|
||||
row_buffer_ellipsis (self, target);
|
||||
row_buffer_space (self, target - self->total_width, attrs);
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_print (uint32_t *ucs4, chtype attrs)
|
||||
{
|
||||
// This assumes that we can reset the attribute set without consequences
|
||||
char *str = u32_strconv_to_locale (ucs4);
|
||||
if (str)
|
||||
{
|
||||
attrset (attrs);
|
||||
addstr (str);
|
||||
attrset (0);
|
||||
free (str);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
row_buffer_flush (struct row_buffer *self)
|
||||
{
|
||||
if (!self->chars_len)
|
||||
return;
|
||||
|
||||
// We only NUL-terminate the chunks because of the libunistring API
|
||||
uint32_t chunk[self->chars_len + 1], *insertion_point = chunk;
|
||||
for (size_t i = 0; i < self->chars_len; i++)
|
||||
{
|
||||
struct row_char *iter = self->chars + i;
|
||||
if (i && iter[0].attrs != iter[-1].attrs)
|
||||
{
|
||||
row_buffer_print (chunk, iter[-1].attrs);
|
||||
insertion_point = chunk;
|
||||
}
|
||||
*insertion_point++ = iter->c;
|
||||
*insertion_point = 0;
|
||||
}
|
||||
row_buffer_print (chunk, self->chars[self->chars_len - 1].attrs);
|
||||
}
|
||||
26
libertyconf.vim
Normal file
26
libertyconf.vim
Normal file
@@ -0,0 +1,26 @@
|
||||
" Since the liberty configuration format is nearly indistinguishable,
|
||||
" this syntax highlight definition needs to be loaded with `set ft=libertyconf`
|
||||
if exists("b:current_syntax")
|
||||
finish
|
||||
endif
|
||||
|
||||
syn match libertyconfError "[^_[:alnum:][:space:]]\+"
|
||||
syn match libertyconfComment "#.*"
|
||||
syn match libertyconfSpecial "{\|}\|="
|
||||
syn match libertyconfNumber "[+-]\=\<\d\+\>"
|
||||
syn match libertyconfBoolean "\c\<\(true\|yes\|on\|false\|no\|off\)\>"
|
||||
syn match libertyconfNull "null"
|
||||
syn match libertyconfEscape display "\\\([xX]\x\{1,2}\|\o\{1,3}\|.\|$\)"
|
||||
\ contained
|
||||
syn region libertyconfString start=+"+ skip=+\\\\\|\\"+ end=+"+
|
||||
\ contains=libertyconfEscape
|
||||
|
||||
let b:current_syntax = "libertyconf"
|
||||
hi def link libertyconfError Error
|
||||
hi def link libertyconfComment Comment
|
||||
hi def link libertyconfSpecial Special
|
||||
hi def link libertyconfNumber Number
|
||||
hi def link libertyconfBoolean Boolean
|
||||
hi def link libertyconfNull Constant
|
||||
hi def link libertyconfEscape SpecialChar
|
||||
hi def link libertyconfString String
|
||||
28
meson/packaging/make-deb.sh
Executable file
28
meson/packaging/make-deb.sh
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh -e
|
||||
cd "$MESON_BUILD_ROOT"
|
||||
. "$MESON_SUBDIR/meta"
|
||||
wd="`pwd`/`mktemp -d deb.XXXXXX`"
|
||||
trap "rm -rf '$wd'" INT QUIT TERM EXIT
|
||||
|
||||
[ "$arch" = x86 ] && arch=i386
|
||||
[ "$arch" = x86_64 ] && arch=amd64
|
||||
target="$name-$version-$system-$arch.deb"
|
||||
|
||||
echo 2.0 > "$wd/debian-binary"
|
||||
cat > "$wd/control" <<-EOF
|
||||
Package: $name
|
||||
Version: $version
|
||||
Section: misc
|
||||
Priority: optional
|
||||
Architecture: $arch
|
||||
Maintainer: $author
|
||||
Description: $summary
|
||||
EOF
|
||||
fakeroot sh -e <<-EOF
|
||||
DESTDIR="$wd/pkg" ninja install
|
||||
cd "$wd/pkg" && tar cJf ../data.tar.xz .
|
||||
EOF
|
||||
|
||||
(cd "$wd" && tar czf control.tar.gz ./control)
|
||||
ar rc "$target" "$wd/debian-binary" "$wd/control.tar.gz" "$wd/data.tar.xz"
|
||||
echo Written $target
|
||||
22
meson/packaging/make-pacman.sh
Executable file
22
meson/packaging/make-pacman.sh
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh -e
|
||||
cd "$MESON_BUILD_ROOT"
|
||||
. "$MESON_SUBDIR/meta"
|
||||
wd="`pwd`/`mktemp -d pacman.XXXXXX`"
|
||||
trap "rm -rf '$wd'" INT QUIT TERM EXIT
|
||||
|
||||
target="$name-$version-$arch.tar.xz"
|
||||
fakeroot sh -e <<-EOF
|
||||
DESTDIR="$wd" ninja install
|
||||
cat > "$wd/.PKGINFO" <<END
|
||||
pkgname = $name
|
||||
pkgver = $version-1
|
||||
pkgdesc = $summary
|
||||
url = $url
|
||||
builddate = \`date -u +%s\`
|
||||
packager = $author
|
||||
size = \`du -sb | cut -f1\`
|
||||
arch = $arch
|
||||
END
|
||||
cd "$wd" && tar cJf "../$target" .PKGINFO *
|
||||
echo Written $target
|
||||
EOF
|
||||
11
meson/packaging/meson.build
Normal file
11
meson/packaging/meson.build
Normal file
@@ -0,0 +1,11 @@
|
||||
# You need to prepare a configuration object with the required metadata
|
||||
packaging.set ('arch', target_machine.cpu_family ())
|
||||
packaging.set ('system', target_machine.system ())
|
||||
configure_file (input: 'meta.in', output: 'meta', configuration: packaging)
|
||||
|
||||
# RPM is awful and I've given up on both manual generation (we'd have to either
|
||||
# include rpmrc data or generate fake noarch packages) and rpmbuild (just no)
|
||||
run_target ('deb',
|
||||
command: [join_paths (meson.current_source_dir (), 'make-deb.sh')])
|
||||
run_target ('pacman',
|
||||
command: [join_paths (meson.current_source_dir (), 'make-pacman.sh')])
|
||||
8
meson/packaging/meta.in
Normal file
8
meson/packaging/meta.in
Normal file
@@ -0,0 +1,8 @@
|
||||
define() { [ -z "$2" ] && { echo $1 is undefined; exit 1; } || eval "$1='$2'"; }
|
||||
|
||||
define name "@name@"
|
||||
define version "@version@"
|
||||
define summary "@summary@"
|
||||
define author "@author@"
|
||||
define arch "@arch@"
|
||||
define system "@system@"
|
||||
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;
|
||||
|
||||
340
tests/liberty.c
340
tests/liberty.c
@@ -1,12 +1,10 @@
|
||||
/*
|
||||
* tests/liberty.c
|
||||
*
|
||||
* Copyright (c) 2015, Přemysl Janouch <p.janouch@gmail.com>
|
||||
* All rights reserved.
|
||||
* 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
|
||||
@@ -21,6 +19,9 @@
|
||||
#define PROGRAM_NAME "test"
|
||||
#define PROGRAM_VERSION "0"
|
||||
|
||||
#define LIBERTY_WANT_POLLER
|
||||
#define LIBERTY_WANT_ASYNC
|
||||
|
||||
#include "../liberty.c"
|
||||
|
||||
// --- Memory ------------------------------------------------------------------
|
||||
@@ -101,10 +102,10 @@ test_list (void)
|
||||
}
|
||||
|
||||
// Remove a few entries
|
||||
LIST_UNLINK (list, a[0]); a[0] = NULL;
|
||||
LIST_UNLINK (list, a[3]); a[3] = NULL;
|
||||
LIST_UNLINK (list, a[4]); a[4] = NULL;
|
||||
LIST_UNLINK (list, a[6]); a[6] = NULL;
|
||||
LIST_UNLINK (list, a[0]); free (a[0]); a[0] = NULL;
|
||||
LIST_UNLINK (list, a[3]); free (a[3]); a[3] = NULL;
|
||||
LIST_UNLINK (list, a[4]); free (a[4]); a[4] = NULL;
|
||||
LIST_UNLINK (list, a[6]); free (a[6]); a[6] = NULL;
|
||||
|
||||
// Prepend one more item
|
||||
a[0] = make_link (0);
|
||||
@@ -133,11 +134,11 @@ test_list_with_tail (void)
|
||||
}
|
||||
|
||||
// Remove a few entries
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[0]); a[0] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[3]); a[3] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[4]); a[4] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[6]); a[6] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[9]); a[9] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[0]); free (a[0]); a[0] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[3]); free (a[3]); a[3] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[4]); free (a[4]); a[4] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[6]); free (a[6]); a[6] = NULL;
|
||||
LIST_UNLINK_WITH_TAIL (list, tail, a[9]); free (a[9]); a[9] = NULL;
|
||||
|
||||
// Append one more item
|
||||
a[9] = make_link (9);
|
||||
@@ -154,31 +155,28 @@ test_list_with_tail (void)
|
||||
// --- Strings -----------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_str_vector (void)
|
||||
test_strv (void)
|
||||
{
|
||||
struct str_vector v;
|
||||
str_vector_init (&v);
|
||||
|
||||
str_vector_add_owned (&v, xstrdup ("xkcd"));
|
||||
str_vector_reset (&v);
|
||||
struct strv v = strv_make ();
|
||||
strv_append_owned (&v, xstrdup ("xkcd"));
|
||||
strv_reset (&v);
|
||||
|
||||
const char *a[] =
|
||||
{ "123", "456", "a", "bc", "def", "ghij", "klmno", "pqrstu" };
|
||||
|
||||
// Add the first two items via another vector
|
||||
struct str_vector w;
|
||||
str_vector_init (&w);
|
||||
str_vector_add_args (&w, a[0], a[1], NULL);
|
||||
str_vector_add_vector (&v, w.vector);
|
||||
str_vector_free (&w);
|
||||
struct strv w = strv_make ();
|
||||
strv_append_args (&w, a[0], a[1], NULL);
|
||||
strv_append_vector (&v, w.vector);
|
||||
strv_free (&w);
|
||||
|
||||
// Add an item and delete it right after
|
||||
str_vector_add (&v, "test");
|
||||
str_vector_remove (&v, v.len - 1);
|
||||
strv_append (&v, "test");
|
||||
strv_remove (&v, v.len - 1);
|
||||
|
||||
// Add the rest of the list properly
|
||||
for (int i = 2; i < (int) N_ELEMENTS (a); i++)
|
||||
str_vector_add (&v, a[i]);
|
||||
strv_append (&v, a[i]);
|
||||
|
||||
// Check the contents
|
||||
soft_assert (v.len == N_ELEMENTS (a));
|
||||
@@ -186,7 +184,7 @@ test_str_vector (void)
|
||||
soft_assert (!strcmp (v.vector[i], a[i]));
|
||||
soft_assert (v.vector[v.len] == NULL);
|
||||
|
||||
str_vector_free (&v);
|
||||
strv_free (&v);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -194,15 +192,13 @@ test_str (void)
|
||||
{
|
||||
uint8_t x[] = { 0x12, 0x34, 0x56, 0x78, 0x11, 0x22, 0x33, 0x44 };
|
||||
|
||||
struct str s;
|
||||
str_init (&s);
|
||||
str_ensure_space (&s, MEGA);
|
||||
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');
|
||||
@@ -263,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 ();
|
||||
@@ -280,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;
|
||||
@@ -308,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++)
|
||||
{
|
||||
@@ -317,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;
|
||||
@@ -339,6 +330,12 @@ test_utf8 (void)
|
||||
const char invalid[] = "\xf0\x90\x28\xbc";
|
||||
soft_assert ( utf8_validate (valid, sizeof valid));
|
||||
soft_assert (!utf8_validate (invalid, sizeof invalid));
|
||||
|
||||
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);
|
||||
hard_assert (utf8_iter_next (&iter, &ch_len) == 0x1ECD && ch_len == 3);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -348,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));
|
||||
@@ -360,6 +357,253 @@ test_base64 (void)
|
||||
str_free (&decoded);
|
||||
}
|
||||
|
||||
// --- Asynchronous jobs -------------------------------------------------------
|
||||
|
||||
struct test_async_data
|
||||
{
|
||||
struct async_manager manager; ///< Async manager
|
||||
struct async_getaddrinfo *gai; ///< Address resolution job
|
||||
struct async_getnameinfo *gni; ///< Name resolution job
|
||||
|
||||
struct async busyloop; ///< Busy job for cancellation
|
||||
bool finished; ///< End of test indicator
|
||||
};
|
||||
|
||||
static void
|
||||
on_getnameinfo (int err, char *host, char *service, void *user_data)
|
||||
{
|
||||
(void) host;
|
||||
(void) service;
|
||||
|
||||
hard_assert (!err);
|
||||
struct test_async_data *data = user_data;
|
||||
data->gni = NULL;
|
||||
|
||||
async_cancel (&data->busyloop);
|
||||
}
|
||||
|
||||
static void
|
||||
on_getaddrinfo (int err, struct addrinfo *results, void *user_data)
|
||||
{
|
||||
hard_assert (!err);
|
||||
struct test_async_data *data = user_data;
|
||||
data->gai = NULL;
|
||||
|
||||
data->gni = async_getnameinfo
|
||||
(&data->manager, results->ai_addr, results->ai_addrlen, 0);
|
||||
data->gni->dispatcher = on_getnameinfo;
|
||||
data->gni->user_data = data;
|
||||
|
||||
freeaddrinfo (results);
|
||||
}
|
||||
|
||||
static void
|
||||
on_busyloop_execute (struct async *async)
|
||||
{
|
||||
(void) async;
|
||||
|
||||
while (true)
|
||||
sleep (1);
|
||||
}
|
||||
|
||||
static void
|
||||
on_busyloop_destroy (struct async *async)
|
||||
{
|
||||
CONTAINER_OF (async, struct test_async_data, busyloop)->finished = true;
|
||||
}
|
||||
|
||||
static void
|
||||
test_async (void)
|
||||
{
|
||||
struct test_async_data data;
|
||||
memset (&data, 0, sizeof data);
|
||||
data.manager = async_manager_make ();
|
||||
|
||||
data.busyloop = async_make (&data.manager);
|
||||
data.busyloop.execute = on_busyloop_execute;
|
||||
data.busyloop.destroy = on_busyloop_destroy;
|
||||
async_run (&data.busyloop);
|
||||
|
||||
struct addrinfo hints;
|
||||
memset (&hints, 0, sizeof hints);
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
// Localhost should be network-independent and instantaneous
|
||||
data.gai = async_getaddrinfo (&data.manager, "127.0.0.1", "22", &hints);
|
||||
data.gai->dispatcher = on_getaddrinfo;
|
||||
data.gai->user_data = &data;
|
||||
|
||||
struct pollfd pfd =
|
||||
{ .events = POLLIN, .fd = data.manager.finished_pipe[0] };
|
||||
|
||||
// Eventually the busyloop should get cancelled and stop the loop
|
||||
while (!data.finished)
|
||||
{
|
||||
hard_assert (poll (&pfd, 1, 1000) == 1);
|
||||
async_manager_dispatch (&data.manager);
|
||||
}
|
||||
|
||||
soft_assert (!data.gai);
|
||||
soft_assert (!data.gni);
|
||||
async_manager_free (&data.manager);
|
||||
}
|
||||
|
||||
// --- Connector ---------------------------------------------------------------
|
||||
|
||||
// This also happens to test a large part of the poller implementation
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
struct test_connector_fixture
|
||||
{
|
||||
const char *host; ///< The host we're listening on
|
||||
int port; ///< The port we're listening on
|
||||
|
||||
int listening_fd; ///< Listening FD
|
||||
|
||||
struct poller poller; ///< Poller
|
||||
struct poller_fd listening_event; ///< Listening event
|
||||
bool quitting; ///< Quit signal for the event loop
|
||||
};
|
||||
|
||||
static void
|
||||
test_connector_on_client (const struct pollfd *pfd, void *user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
int fd = accept (pfd->fd, NULL, NULL);
|
||||
if (fd == -1)
|
||||
{
|
||||
if (errno == EAGAIN
|
||||
|| errno == EINTR
|
||||
|| errno == ECONNABORTED)
|
||||
return;
|
||||
|
||||
exit_fatal ("%s: %s", "accept", strerror (errno));
|
||||
}
|
||||
|
||||
const char message[] = "Hello!\n";
|
||||
(void) write (fd, message, strlen (message));
|
||||
xclose (fd);
|
||||
}
|
||||
|
||||
static bool
|
||||
test_connector_try_bind
|
||||
(struct test_connector_fixture *self, const char *host, int port)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons ((self->port = port));
|
||||
sin.sin_addr.s_addr = inet_addr ((self->host = host));
|
||||
|
||||
int fd = socket (AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
return true;
|
||||
|
||||
int yes = 1;
|
||||
(void) setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
|
||||
|
||||
if (bind (fd, (struct sockaddr *) &sin, sizeof sin)
|
||||
|| listen (fd, 10))
|
||||
{
|
||||
xclose (fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
self->listening_fd = fd;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
test_connector_fixture_init
|
||||
(const void *user_data, struct test_connector_fixture *self)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
// Find a free port on localhost in the user range and bind to it
|
||||
for (int i = 0; i < 1024; i++)
|
||||
if (test_connector_try_bind (self, "127.0.0.1", 1024 + i))
|
||||
break;
|
||||
if (!self->listening_fd)
|
||||
exit_fatal ("cannot bind to localhost");
|
||||
|
||||
// Make it so that we immediately accept all connections
|
||||
poller_init (&self->poller);
|
||||
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);
|
||||
}
|
||||
|
||||
static void
|
||||
test_connector_fixture_free
|
||||
(const void *user_data, struct test_connector_fixture *self)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
poller_free (&self->poller);
|
||||
xclose (self->listening_fd);
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static void
|
||||
test_connector_on_connected (void *user_data, int socket, const char *hostname)
|
||||
{
|
||||
struct test_connector_fixture *self = user_data;
|
||||
hard_assert (!strcmp (hostname, self->host));
|
||||
xclose (socket);
|
||||
|
||||
self->quitting = true;
|
||||
}
|
||||
|
||||
static void
|
||||
test_connector_on_failure (void *user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
exit_fatal ("failed to connect to the prepared port");
|
||||
}
|
||||
|
||||
static void
|
||||
test_connector_on_connecting (void *user_data, const char *address)
|
||||
{
|
||||
(void) user_data;
|
||||
print_debug ("connecting to %s", address);
|
||||
}
|
||||
|
||||
static void
|
||||
test_connector_on_error (void *user_data, const char *error)
|
||||
{
|
||||
(void) user_data;
|
||||
print_debug ("%s: %s", "connecting failed", error);
|
||||
}
|
||||
|
||||
static void
|
||||
test_connector (const void *user_data, struct test_connector_fixture *self)
|
||||
{
|
||||
(void) user_data;
|
||||
print_debug ("final target is %s:%d", self->host, self->port);
|
||||
|
||||
struct connector connector;
|
||||
connector_init (&connector, &self->poller);
|
||||
connector.on_connecting = test_connector_on_connecting;
|
||||
connector.on_error = test_connector_on_error;
|
||||
connector.on_connected = test_connector_on_connected;
|
||||
connector.on_failure = test_connector_on_failure;
|
||||
connector.user_data = self;
|
||||
|
||||
connector_add_target (&connector, ":D", "nonsense");
|
||||
|
||||
char *port = xstrdup_printf ("%d", self->port);
|
||||
connector_add_target (&connector, self->host, port);
|
||||
free (port);
|
||||
|
||||
while (!self->quitting)
|
||||
poller_run (&self->poller);
|
||||
|
||||
connector_free (&connector);
|
||||
}
|
||||
|
||||
// --- Main --------------------------------------------------------------------
|
||||
|
||||
int
|
||||
@@ -371,12 +615,18 @@ main (int argc, char *argv[])
|
||||
test_add_simple (&test, "/memory", NULL, test_memory);
|
||||
test_add_simple (&test, "/list", NULL, test_list);
|
||||
test_add_simple (&test, "/list-with-tail", NULL, test_list_with_tail);
|
||||
test_add_simple (&test, "/str-vector", NULL, test_str_vector);
|
||||
test_add_simple (&test, "/strv", NULL, test_strv);
|
||||
test_add_simple (&test, "/str", NULL, test_str);
|
||||
test_add_simple (&test, "/error", NULL, test_error);
|
||||
test_add_simple (&test, "/str-map", NULL, test_str_map);
|
||||
test_add_simple (&test, "/utf-8", NULL, test_utf8);
|
||||
test_add_simple (&test, "/base64", NULL, test_base64);
|
||||
test_add_simple (&test, "/async", NULL, test_async);
|
||||
|
||||
test_add (&test, "/connector", struct test_connector_fixture, NULL,
|
||||
test_connector_fixture_init,
|
||||
test_connector,
|
||||
test_connector_fixture_free);
|
||||
|
||||
// TODO: write tests for the rest of the library
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
/*
|
||||
* tests/proto.c
|
||||
*
|
||||
* Copyright (c) 2015, Přemysl Janouch <p.janouch@gmail.com>
|
||||
* All rights reserved.
|
||||
* 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
|
||||
@@ -22,12 +20,16 @@
|
||||
#define PROGRAM_VERSION "0"
|
||||
|
||||
#define LIBERTY_WANT_SSL
|
||||
// The MPD client is a full wrapper and needs the network
|
||||
#define LIBERTY_WANT_POLLER
|
||||
#define LIBERTY_WANT_ASYNC
|
||||
|
||||
#define LIBERTY_WANT_PROTO_IRC
|
||||
#define LIBERTY_WANT_PROTO_HTTP
|
||||
#define LIBERTY_WANT_PROTO_SCGI
|
||||
#define LIBERTY_WANT_PROTO_FASTCGI
|
||||
#define LIBERTY_WANT_PROTO_WS
|
||||
#define LIBERTY_WANT_PROTO_MPD
|
||||
|
||||
#include "../liberty.c"
|
||||
|
||||
@@ -40,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;
|
||||
@@ -76,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;
|
||||
@@ -132,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;
|
||||
@@ -178,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;
|
||||
@@ -201,7 +199,7 @@ main (int argc, char *argv[])
|
||||
test_add_simple (&test, "/http-parser", NULL, test_http_parser);
|
||||
test_add_simple (&test, "/scgi-parser", NULL, test_scgi_parser);
|
||||
test_add_simple (&test, "/websockets", NULL, test_websockets);
|
||||
// TODO: test FastCGI
|
||||
// TODO: test FastCGI and MPD
|
||||
|
||||
return test_run (&test);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user