Index: vendor/libucl/dist/m4/ax_lua.m4 =================================================================== --- vendor/libucl/dist/m4/ax_lua.m4 (revision 279545) +++ vendor/libucl/dist/m4/ax_lua.m4 (nonexistent) @@ -1,606 +0,0 @@ -# =========================================================================== -# http://www.gnu.org/software/autoconf-archive/ax_lua.html -# =========================================================================== -# -# SYNOPSIS -# -# AX_PROG_LUA[([MINIMUM-VERSION], [TOO-BIG-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])] -# AX_LUA_HEADERS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])] -# AX_LUA_LIBS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])] -# AX_LUA_READLINE[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])] -# -# DESCRIPTION -# -# Detect a Lua interpreter, optionally specifying a minimum and maximum -# version number. Set up important Lua paths, such as the directories in -# which to install scripts and modules (shared libraries). -# -# Also detect Lua headers and libraries. The Lua version contained in the -# header is checked to match the Lua interpreter version exactly. When -# searching for Lua libraries, the version number is used as a suffix. -# This is done with the goal of supporting multiple Lua installs (5.1 and -# 5.2 side-by-side). -# -# A note on compatibility with previous versions: This file has been -# mostly rewritten for serial 18. Most developers should be able to use -# these macros without needing to modify configure.ac. Care has been taken -# to preserve each macro's behavior, but there are some differences: -# -# 1) AX_WITH_LUA is deprecated; it now expands to the exact same thing as -# AX_PROG_LUA with no arguments. -# -# 2) AX_LUA_HEADERS now checks that the version number defined in lua.h -# matches the interpreter version. AX_LUA_HEADERS_VERSION is therefore -# unnecessary, so it is deprecated and does not expand to anything. -# -# 3) The configure flag --with-lua-suffix no longer exists; the user -# should instead specify the LUA precious variable on the command line. -# See the AX_PROG_LUA description for details. -# -# Please read the macro descriptions below for more information. -# -# This file was inspired by Andrew Dalke's and James Henstridge's -# python.m4 and Tom Payne's, Matthieu Moy's, and Reuben Thomas's ax_lua.m4 -# (serial 17). Basically, this file is a mash-up of those two files. I -# like to think it combines the best of the two! -# -# AX_PROG_LUA: Search for the Lua interpreter, and set up important Lua -# paths. Adds precious variable LUA, which may contain the path of the Lua -# interpreter. If LUA is blank, the user's path is searched for an -# suitable interpreter. -# -# If MINIMUM-VERSION is supplied, then only Lua interpreters with a -# version number greater or equal to MINIMUM-VERSION will be accepted. If -# TOO-BIG- VERSION is also supplied, then only Lua interpreters with a -# version number greater or equal to MINIMUM-VERSION and less than -# TOO-BIG-VERSION will be accepted. -# -# The Lua version number, LUA_VERSION, is found from the interpreter, and -# substituted. LUA_PLATFORM is also found, but not currently supported (no -# standard representation). -# -# Finally, the macro finds four paths: -# -# luadir Directory to install Lua scripts. -# pkgluadir $luadir/$PACKAGE -# luaexecdir Directory to install Lua modules. -# pkgluaexecdir $luaexecdir/$PACKAGE -# -# These paths a found based on $prefix, $exec_prefix, Lua's package.path, -# and package.cpath. The first path of package.path beginning with $prefix -# is selected as luadir. The first path of package.cpath beginning with -# $exec_prefix is used as luaexecdir. This should work on all reasonable -# Lua installations. If a path cannot be determined, a default path is -# used. Of course, the user can override these later when invoking make. -# -# luadir Default: $prefix/share/lua/$LUA_VERSION -# luaexecdir Default: $exec_prefix/lib/lua/$LUA_VERSION -# -# These directories can be used by Automake as install destinations. The -# variable name minus 'dir' needs to be used as a prefix to the -# appropriate Automake primary, e.g. lua_SCRIPS or luaexec_LIBRARIES. -# -# If an acceptable Lua interpreter is found, then ACTION-IF-FOUND is -# performed, otherwise ACTION-IF-NOT-FOUND is preformed. If ACTION-IF-NOT- -# FOUND is blank, then it will default to printing an error. To prevent -# the default behavior, give ':' as an action. -# -# AX_LUA_HEADERS: Search for Lua headers. Requires that AX_PROG_LUA be -# expanded before this macro. Adds precious variable LUA_INCLUDE, which -# may contain Lua specific include flags, e.g. -I/usr/include/lua5.1. If -# LUA_INCLUDE is blank, then this macro will attempt to find suitable -# flags. -# -# LUA_INCLUDE can be used by Automake to compile Lua modules or -# executables with embedded interpreters. The *_CPPFLAGS variables should -# be used for this purpose, e.g. myprog_CPPFLAGS = $(LUA_INCLUDE). -# -# This macro searches for the header lua.h (and others). The search is -# performed with a combination of CPPFLAGS, CPATH, etc, and LUA_INCLUDE. -# If the search is unsuccessful, then some common directories are tried. -# If the headers are then found, then LUA_INCLUDE is set accordingly. -# -# The paths automatically searched are: -# -# * /usr/include/luaX.Y -# * /usr/include/lua/X.Y -# * /usr/include/luaXY -# * /usr/local/include/luaX.Y -# * /usr/local/include/lua-X.Y -# * /usr/local/include/lua/X.Y -# * /usr/local/include/luaXY -# -# (Where X.Y is the Lua version number, e.g. 5.1.) -# -# The Lua version number found in the headers is always checked to match -# the Lua interpreter's version number. Lua headers with mismatched -# version numbers are not accepted. -# -# If headers are found, then ACTION-IF-FOUND is performed, otherwise -# ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then -# it will default to printing an error. To prevent the default behavior, -# set the action to ':'. -# -# AX_LUA_LIBS: Search for Lua libraries. Requires that AX_PROG_LUA be -# expanded before this macro. Adds precious variable LUA_LIB, which may -# contain Lua specific linker flags, e.g. -llua5.1. If LUA_LIB is blank, -# then this macro will attempt to find suitable flags. -# -# LUA_LIB can be used by Automake to link Lua modules or executables with -# embedded interpreters. The *_LIBADD and *_LDADD variables should be used -# for this purpose, e.g. mymod_LIBADD = $(LUA_LIB). -# -# This macro searches for the Lua library. More technically, it searches -# for a library containing the function lua_load. The search is performed -# with a combination of LIBS, LIBRARY_PATH, and LUA_LIB. -# -# If the search determines that some linker flags are missing, then those -# flags will be added to LUA_LIB. -# -# If libraries are found, then ACTION-IF-FOUND is performed, otherwise -# ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then -# it will default to printing an error. To prevent the default behavior, -# set the action to ':'. -# -# AX_LUA_READLINE: Search for readline headers and libraries. Requires the -# AX_LIB_READLINE macro, which is provided by ax_lib_readline.m4 from the -# Autoconf Archive. -# -# If a readline compatible library is found, then ACTION-IF-FOUND is -# performed, otherwise ACTION-IF-NOT-FOUND is performed. -# -# LICENSE -# -# Copyright (c) 2014 Reuben Thomas -# Copyright (c) 2013 Tim Perkins -# -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by the -# Free Software Foundation, either version 3 of the License, or (at your -# option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -# Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program. If not, see . -# -# As a special exception, the respective Autoconf Macro's copyright owner -# gives unlimited permission to copy, distribute and modify the configure -# scripts that are the output of Autoconf when processing the Macro. You -# need not follow the terms of the GNU General Public License when using -# or distributing such scripts, even though portions of the text of the -# Macro appear in them. The GNU General Public License (GPL) does govern -# all other use of the material that constitutes the Autoconf Macro. -# -# This special exception to the GPL applies to versions of the Autoconf -# Macro released by the Autoconf Archive. When you make and distribute a -# modified version of the Autoconf Macro, you may extend this special -# exception to the GPL to apply to your modified version as well. - -#serial 23 - -dnl ========================================================================= -dnl AX_PROG_LUA([MINIMUM-VERSION], [TOO-BIG-VERSION], -dnl [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) -dnl ========================================================================= -AC_DEFUN([AX_PROG_LUA], -[ - dnl Make LUA a precious variable. - AC_ARG_VAR([LUA], [The Lua interpreter, e.g. /usr/bin/lua5.1]) - - dnl Find a Lua interpreter. - m4_define_default([_AX_LUA_INTERPRETER_LIST], - [lua lua5.2 lua52 lua5.1 lua51 lua50]) - - m4_if([$1], [], - [ dnl No version check is needed. Find any Lua interpreter. - AS_IF([test "x$LUA" = 'x'], - [AC_PATH_PROGS([LUA], [_AX_LUA_INTERPRETER_LIST], [:])]) - ax_display_LUA='lua' - - dnl At least check if this is a Lua interpreter. - AC_MSG_CHECKING([if $LUA is a Lua interpreter]) - _AX_LUA_CHK_IS_INTRP([$LUA], - [AC_MSG_RESULT([yes])], - [ AC_MSG_RESULT([no]) - AC_MSG_ERROR([not a Lua interpreter]) - ]) - ], - [ dnl A version check is needed. - AS_IF([test "x$LUA" != 'x'], - [ dnl Check if this is a Lua interpreter. - AC_MSG_CHECKING([if $LUA is a Lua interpreter]) - _AX_LUA_CHK_IS_INTRP([$LUA], - [AC_MSG_RESULT([yes])], - [ AC_MSG_RESULT([no]) - AC_MSG_ERROR([not a Lua interpreter]) - ]) - dnl Check the version. - m4_if([$2], [], - [_ax_check_text="whether $LUA version >= $1"], - [_ax_check_text="whether $LUA version >= $1, < $2"]) - AC_MSG_CHECKING([$_ax_check_text]) - _AX_LUA_CHK_VER([$LUA], [$1], [$2], - [AC_MSG_RESULT([yes])], - [ AC_MSG_RESULT([no]) - AC_MSG_ERROR([version is out of range for specified LUA])]) - ax_display_LUA=$LUA - ], - [ dnl Try each interpreter until we find one that satisfies VERSION. - m4_if([$2], [], - [_ax_check_text="for a Lua interpreter with version >= $1"], - [_ax_check_text="for a Lua interpreter with version >= $1, < $2"]) - AC_CACHE_CHECK([$_ax_check_text], - [ax_cv_pathless_LUA], - [ for ax_cv_pathless_LUA in _AX_LUA_INTERPRETER_LIST none; do - test "x$ax_cv_pathless_LUA" = 'xnone' && break - _AX_LUA_CHK_IS_INTRP([$ax_cv_pathless_LUA], [], [continue]) - _AX_LUA_CHK_VER([$ax_cv_pathless_LUA], [$1], [$2], [break]) - done - ]) - dnl Set $LUA to the absolute path of $ax_cv_pathless_LUA. - AS_IF([test "x$ax_cv_pathless_LUA" = 'xnone'], - [LUA=':'], - [AC_PATH_PROG([LUA], [$ax_cv_pathless_LUA])]) - ax_display_LUA=$ax_cv_pathless_LUA - ]) - ]) - - AS_IF([test "x$LUA" = 'x:'], - [ dnl Run any user-specified action, or abort. - m4_default([$4], [AC_MSG_ERROR([cannot find suitable Lua interpreter])]) - ], - [ dnl Query Lua for its version number. - AC_CACHE_CHECK([for $ax_display_LUA version], [ax_cv_lua_version], - [ ax_cv_lua_version=`$LUA -e 'print(_VERSION:match "(%d+%.%d+)")'` ]) - AS_IF([test "x$ax_cv_lua_version" = 'x'], - [AC_MSG_ERROR([invalid Lua version number])]) - AC_SUBST([LUA_VERSION], [$ax_cv_lua_version]) - AC_SUBST([LUA_SHORT_VERSION], [`echo "$LUA_VERSION" | sed 's|\.||'`]) - - dnl The following check is not supported: - dnl At times (like when building shared libraries) you may want to know - dnl which OS platform Lua thinks this is. - AC_CACHE_CHECK([for $ax_display_LUA platform], [ax_cv_lua_platform], - [ax_cv_lua_platform=`$LUA -e "print('unknown')"`]) - AC_SUBST([LUA_PLATFORM], [$ax_cv_lua_platform]) - - dnl Use the values of $prefix and $exec_prefix for the corresponding - dnl values of LUA_PREFIX and LUA_EXEC_PREFIX. These are made distinct - dnl variables so they can be overridden if need be. However, the general - dnl consensus is that you shouldn't need this ability. - AC_SUBST([LUA_PREFIX], ['${prefix}']) - AC_SUBST([LUA_EXEC_PREFIX], ['${exec_prefix}']) - - dnl Lua provides no way to query the script directory, and instead - dnl provides LUA_PATH. However, we should be able to make a safe educated - dnl guess. If the built-in search path contains a directory which is - dnl prefixed by $prefix, then we can store scripts there. The first - dnl matching path will be used. - AC_CACHE_CHECK([for $ax_display_LUA script directory], - [ax_cv_lua_luadir], - [ AS_IF([test "x$prefix" = 'xNONE'], - [ax_lua_prefix=$ac_default_prefix], - [ax_lua_prefix=$prefix]) - - dnl Initialize to the default path. - ax_cv_lua_luadir="$LUA_PREFIX/share/lua/$LUA_VERSION" - - dnl Try to find a path with the prefix. - _AX_LUA_FND_PRFX_PTH([$LUA], [$ax_lua_prefix], [package.path]) - AS_IF([test "x$ax_lua_prefixed_path" != 'x'], - [ dnl Fix the prefix. - _ax_strip_prefix=`echo "$ax_lua_prefix" | sed 's|.|.|g'` - ax_cv_lua_luadir=`echo "$ax_lua_prefixed_path" | \ - sed "s,^$_ax_strip_prefix,$LUA_PREFIX,"` - ]) - ]) - AC_SUBST([luadir], [$ax_cv_lua_luadir]) - AC_SUBST([pkgluadir], [\${luadir}/$PACKAGE]) - - dnl Lua provides no way to query the module directory, and instead - dnl provides LUA_PATH. However, we should be able to make a safe educated - dnl guess. If the built-in search path contains a directory which is - dnl prefixed by $exec_prefix, then we can store modules there. The first - dnl matching path will be used. - AC_CACHE_CHECK([for $ax_display_LUA module directory], - [ax_cv_lua_luaexecdir], - [ AS_IF([test "x$exec_prefix" = 'xNONE'], - [ax_lua_exec_prefix=$ax_lua_prefix], - [ax_lua_exec_prefix=$exec_prefix]) - - dnl Initialize to the default path. - ax_cv_lua_luaexecdir="$LUA_EXEC_PREFIX/lib/lua/$LUA_VERSION" - - dnl Try to find a path with the prefix. - _AX_LUA_FND_PRFX_PTH([$LUA], - [$ax_lua_exec_prefix], [package.cpathd]) - AS_IF([test "x$ax_lua_prefixed_path" != 'x'], - [ dnl Fix the prefix. - _ax_strip_prefix=`echo "$ax_lua_exec_prefix" | sed 's|.|.|g'` - ax_cv_lua_luaexecdir=`echo "$ax_lua_prefixed_path" | \ - sed "s,^$_ax_strip_prefix,$LUA_EXEC_PREFIX,"` - ]) - ]) - AC_SUBST([luaexecdir], [$ax_cv_lua_luaexecdir]) - AC_SUBST([pkgluaexecdir], [\${luaexecdir}/$PACKAGE]) - - dnl Run any user specified action. - $3 - ]) -]) - -dnl AX_WITH_LUA is now the same thing as AX_PROG_LUA. -AC_DEFUN([AX_WITH_LUA], -[ - AC_MSG_WARN([[$0 is deprecated, please use AX_PROG_LUA]]) - AX_PROG_LUA -]) - - -dnl ========================================================================= -dnl _AX_LUA_CHK_IS_INTRP(PROG, [ACTION-IF-TRUE], [ACTION-IF-FALSE]) -dnl ========================================================================= -AC_DEFUN([_AX_LUA_CHK_IS_INTRP], -[ - dnl Just print _VERSION because all Lua interpreters have this global. - AS_IF([$1 -e "print('Hello ' .. _VERSION .. '!')" &>/dev/null], - [$2], [$3]) -]) - - -dnl ========================================================================= -dnl _AX_LUA_CHK_VER(PROG, MINIMUM-VERSION, [TOO-BIG-VERSION], -dnl [ACTION-IF-TRUE], [ACTION-IF-FALSE]) -dnl ========================================================================= -AC_DEFUN([_AX_LUA_CHK_VER], -[ - AS_IF([$1 2>/dev/null -e ' - function norm (v) i,j=v:match "(%d+)%.(%d+)" return 100 * i + j end - v=norm (_VERSION) - os.exit ((v >= norm ("$2") and ("$3" == "" or v < norm ("$3"))) and 0 or 1)'], - [$4], [$5]) -]) - - -dnl ========================================================================= -dnl _AX_LUA_FND_PRFX_PTH(PROG, PREFIX, LUA-PATH-VARIABLE) -dnl ========================================================================= -AC_DEFUN([_AX_LUA_FND_PRFX_PTH], -[ - dnl Invokes the Lua interpreter PROG to print the path variable - dnl LUA-PATH-VARIABLE, usually package.path or package.cpath. Paths are - dnl then matched against PREFIX. The first path to begin with PREFIX is set - dnl to ax_lua_prefixed_path. - - ax_lua_prefixed_path='' - _ax_package_paths=`$1 -e 'print($3)' 2>/dev/null | sed 's|;|\n|g'` - dnl Try the paths in order, looking for the prefix. - for _ax_package_path in $_ax_package_paths; do - dnl Copy the path, up to the use of a Lua wildcard. - _ax_path_parts=`echo "$_ax_package_path" | sed 's|/|\n|g'` - _ax_reassembled='' - for _ax_path_part in $_ax_path_parts; do - echo "$_ax_path_part" | grep '\?' >/dev/null && break - _ax_reassembled="$_ax_reassembled/$_ax_path_part" - done - dnl Check the path against the prefix. - _ax_package_path=$_ax_reassembled - if echo "$_ax_package_path" | grep "^$2" >/dev/null; then - dnl Found it. - ax_lua_prefixed_path=$_ax_package_path - break - fi - done -]) - - -dnl ========================================================================= -dnl AX_LUA_HEADERS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) -dnl ========================================================================= -AC_DEFUN([AX_LUA_HEADERS], -[ - dnl Check for LUA_VERSION. - AC_MSG_CHECKING([if LUA_VERSION is defined]) - AS_IF([test "x$LUA_VERSION" != 'x'], - [AC_MSG_RESULT([yes])], - [ AC_MSG_RESULT([no]) - AC_MSG_ERROR([cannot check Lua headers without knowing LUA_VERSION]) - ]) - - dnl Make LUA_INCLUDE a precious variable. - AC_ARG_VAR([LUA_INCLUDE], [The Lua includes, e.g. -I/usr/include/lua5.1]) - - dnl Some default directories to search. - LUA_SHORT_VERSION=`echo "$LUA_VERSION" | sed 's|\.||'` - m4_define_default([_AX_LUA_INCLUDE_LIST], - [ /usr/include/lua$LUA_VERSION \ - /usr/include/lua/$LUA_VERSION \ - /usr/include/lua$LUA_SHORT_VERSION \ - /usr/local/include/lua$LUA_VERSION \ - /usr/local/include/lua-$LUA_VERSION \ - /usr/local/include/lua/$LUA_VERSION \ - /usr/local/include/lua$LUA_SHORT_VERSION \ - ]) - - dnl Try to find the headers. - _ax_lua_saved_cppflags=$CPPFLAGS - CPPFLAGS="$CPPFLAGS $LUA_INCLUDE" - AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h]) - CPPFLAGS=$_ax_lua_saved_cppflags - - dnl Try some other directories if LUA_INCLUDE was not set. - AS_IF([test "x$LUA_INCLUDE" = 'x' && - test "x$ac_cv_header_lua_h" != 'xyes'], - [ dnl Try some common include paths. - for _ax_include_path in _AX_LUA_INCLUDE_LIST; do - test ! -d "$_ax_include_path" && continue - - AC_MSG_CHECKING([for Lua headers in]) - AC_MSG_RESULT([$_ax_include_path]) - - AS_UNSET([ac_cv_header_lua_h]) - AS_UNSET([ac_cv_header_lualib_h]) - AS_UNSET([ac_cv_header_lauxlib_h]) - AS_UNSET([ac_cv_header_luaconf_h]) - - _ax_lua_saved_cppflags=$CPPFLAGS - CPPFLAGS="$CPPFLAGS -I$_ax_include_path" - AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h]) - CPPFLAGS=$_ax_lua_saved_cppflags - - AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'], - [ LUA_INCLUDE="-I$_ax_include_path" - break - ]) - done - ]) - - AS_IF([test "x$ac_cv_header_lua_h" = 'xyes' && test "x$cross_compiling" != 'xyes'], - [ dnl Make a program to print LUA_VERSION defined in the header. - dnl TODO This probably shouldn't be a runtime test. - - AC_CACHE_CHECK([for Lua header version], - [ax_cv_lua_header_version], - [ _ax_lua_saved_cppflags=$CPPFLAGS - CPPFLAGS="$CPPFLAGS $LUA_INCLUDE" - AC_RUN_IFELSE( - [ AC_LANG_SOURCE([[ -#include -#include -#include -int main(int argc, char ** argv) -{ - if(argc > 1) printf("%s", LUA_VERSION); - exit(EXIT_SUCCESS); -} -]]) - ], - [ ax_cv_lua_header_version=`./conftest$EXEEXT p | \ - sed "s|^Lua \(.*\)|\1|" | \ - grep -o "^@<:@0-9@:>@\+\\.@<:@0-9@:>@\+"` - ], - [ax_cv_lua_header_version='unknown']) - CPPFLAGS=$_ax_lua_saved_cppflags - ]) - - dnl Compare this to the previously found LUA_VERSION. - AC_MSG_CHECKING([if Lua header version matches $LUA_VERSION]) - AS_IF([test "x$ax_cv_lua_header_version" = "x$LUA_VERSION"], - [ AC_MSG_RESULT([yes]) - ax_header_version_match='yes' - ], - [ AC_MSG_RESULT([no]) - ax_header_version_match='no' - ]) - ], - [ - ax_header_version_match='yes' - ]) - - dnl Was LUA_INCLUDE specified? - AS_IF([test "x$ax_header_version_match" != 'xyes' && - test "x$LUA_INCLUDE" != 'x'], - [AC_MSG_ERROR([cannot find headers for specified LUA_INCLUDE])]) - - dnl Test the final result and run user code. - AS_IF([test "x$ax_header_version_match" = 'xyes'], [$1], - [m4_default([$2], [AC_MSG_ERROR([cannot find Lua includes])])]) -]) - -dnl AX_LUA_HEADERS_VERSION no longer exists, use AX_LUA_HEADERS. -AC_DEFUN([AX_LUA_HEADERS_VERSION], -[ - AC_MSG_WARN([[$0 is deprecated, please use AX_LUA_HEADERS]]) -]) - - -dnl ========================================================================= -dnl AX_LUA_LIBS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) -dnl ========================================================================= -AC_DEFUN([AX_LUA_LIBS], -[ - dnl TODO Should this macro also check various -L flags? - - dnl Check for LUA_VERSION. - AC_MSG_CHECKING([if LUA_VERSION is defined]) - AS_IF([test "x$LUA_VERSION" != 'x'], - [AC_MSG_RESULT([yes])], - [ AC_MSG_RESULT([no]) - AC_MSG_ERROR([cannot check Lua libs without knowing LUA_VERSION]) - ]) - - dnl Make LUA_LIB a precious variable. - AC_ARG_VAR([LUA_LIB], [The Lua library, e.g. -llua5.1]) - - AS_IF([test "x$LUA_LIB" != 'x'], - [ dnl Check that LUA_LIBS works. - _ax_lua_saved_libs=$LIBS - LIBS="$LIBS $LUA_LIB" - AC_SEARCH_LIBS([lua_load], [], - [_ax_found_lua_libs='yes'], - [_ax_found_lua_libs='no']) - LIBS=$_ax_lua_saved_libs - - dnl Check the result. - AS_IF([test "x$_ax_found_lua_libs" != 'xyes'], - [AC_MSG_ERROR([cannot find libs for specified LUA_LIB])]) - ], - [ dnl First search for extra libs. - _ax_lua_extra_libs='' - - _ax_lua_saved_libs=$LIBS - LIBS="$LIBS $LUA_LIB" - AC_SEARCH_LIBS([exp], [m]) - AC_SEARCH_LIBS([dlopen], [dl]) - LIBS=$_ax_lua_saved_libs - - AS_IF([test "x$ac_cv_search_exp" != 'xno' && - test "x$ac_cv_search_exp" != 'xnone required'], - [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_exp"]) - - AS_IF([test "x$ac_cv_search_dlopen" != 'xno' && - test "x$ac_cv_search_dlopen" != 'xnone required'], - [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_dlopen"]) - - dnl Try to find the Lua libs. - _ax_lua_saved_libs=$LIBS - LIBS="$LIBS $LUA_LIB" - AC_SEARCH_LIBS([lua_load], - [ lua$LUA_VERSION \ - lua$LUA_SHORT_VERSION \ - lua-$LUA_VERSION \ - lua-$LUA_SHORT_VERSION \ - lua], - [_ax_found_lua_libs='yes'], - [_ax_found_lua_libs='no'], - [$_ax_lua_extra_libs]) - LIBS=$_ax_lua_saved_libs - - AS_IF([test "x$ac_cv_search_lua_load" != 'xno' && - test "x$ac_cv_search_lua_load" != 'xnone required'], - [LUA_LIB="$ac_cv_search_lua_load $_ax_lua_extra_libs"]) - ]) - - dnl Test the result and run user code. - AS_IF([test "x$_ax_found_lua_libs" = 'xyes'], [$1], - [m4_default([$2], [AC_MSG_ERROR([cannot find Lua libs])])]) -]) - - -dnl ========================================================================= -dnl AX_LUA_READLINE([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) -dnl ========================================================================= -AC_DEFUN([AX_LUA_READLINE], -[ - AX_LIB_READLINE - AS_IF([test "x$ac_cv_header_readline_readline_h" != 'x' && - test "x$ac_cv_header_readline_history_h" != 'x'], - [ LUA_LIBS_CFLAGS="-DLUA_USE_READLINE $LUA_LIBS_CFLAGS" - $1 - ], - [$2]) -]) Index: vendor/libucl/dist/m4/.gitignore =================================================================== --- vendor/libucl/dist/m4/.gitignore (revision 279545) +++ vendor/libucl/dist/m4/.gitignore (nonexistent) @@ -1,4 +0,0 @@ -# Ignore everything in this directory -* -# Except this file -!.gitignore Index: vendor/libucl/dist/ChangeLog.md =================================================================== --- vendor/libucl/dist/ChangeLog.md (revision 279545) +++ vendor/libucl/dist/ChangeLog.md (revision 279546) @@ -1,22 +1,34 @@ # Version history ## Libucl 0.5 - Streamline emitter has been added, so it is now possible to output partial `ucl` objects - Emitter now is more flexible due to emitter_context structure ### 0.5.1 - Fixed number of bugs and memory leaks ### 0.5.2 - Allow userdata objects to be emitted and destructed - Use userdata objects to store lua function references ### Libucl 0.6 - Reworked macro interface ### Libucl 0.6.1 - Various utilities fixes + +### Libucl 0.7.0 + +- Move to klib library from uthash to reduce memory overhead and increase performance + +### Libucl 0.7.1 + +- Added safe iterators API + +### Libucl 0.7.2 + +- Fixed serious bugs in schema and arrays iteration Index: vendor/libucl/dist/Makefile.am =================================================================== --- vendor/libucl/dist/Makefile.am (revision 279545) +++ vendor/libucl/dist/Makefile.am (revision 279546) @@ -1,11 +1,11 @@ ACLOCAL_AMFLAGS = -I m4 -EXTRA_DIST = uthash README.md +EXTRA_DIST = uthash klib README.md pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libucl.pc if LUA_SUB LUA_SUBDIR = lua endif SUBDIRS = src tests utils doc $(LUA_SUBDIR) \ No newline at end of file Index: vendor/libucl/dist/README.md =================================================================== --- vendor/libucl/dist/README.md (revision 279545) +++ vendor/libucl/dist/README.md (revision 279546) @@ -1,369 +1,369 @@ # LIBUCL -[![Build Status](https://travis-ci.org/vstakhov/libucl.svg?branch=master)](https://travis-ci.org/vstakhov/libucl) +[![Build Status](https://travis-ci.org/vstakhov/libucl.svg?branch=master)](https://travis-ci.org/vstakhov/libucl)[![Coverity](https://scan.coverity.com/projects/4138/badge.svg)](https://scan.coverity.com/projects/4138) **Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)* - [Introduction](#introduction) - [Basic structure](#basic-structure) - [Improvements to the json notation](#improvements-to-the-json-notation) - [General syntax sugar](#general-syntax-sugar) - [Automatic arrays creation](#automatic-arrays-creation) - [Named keys hierarchy](#named-keys-hierarchy) - [Convenient numbers and booleans](#convenient-numbers-and-booleans) - [General improvements](#general-improvements) - [Commments](#commments) - [Macros support](#macros-support) - [Variables support](#variables-support) - [Multiline strings](#multiline-strings) - [Emitter](#emitter) - [Validation](#validation) - [Performance](#performance) - [Conclusion](#conclusion) ## Introduction This document describes the main features and principles of the configuration language called `UCL` - universal configuration language. If you are looking for the libucl API documentation you can find it at [this page](doc/api.md). ## Basic structure UCL is heavily infused by `nginx` configuration as the example of a convenient configuration system. However, UCL is fully compatible with `JSON` format and is able to parse json files. For example, you can write the same configuration in the following ways: * in nginx like: ```nginx param = value; section { param = value; param1 = value1; flag = true; number = 10k; time = 0.2s; string = "something"; subsection { host = { host = "hostname"; port = 900; } host = { host = "hostname"; port = 901; } } } ``` * or in JSON: ```json { "param": "value", "param1": "value1", "flag": true, "subsection": { "host": [ { "host": "hostname", "port": 900 }, { "host": "hostname", "port": 901 } ] } } ``` ## Improvements to the json notation. There are various things that make ucl configuration more convenient for editing than strict json: ### General syntax sugar * Braces are not necessary to enclose a top object: it is automatically treated as an object: ```json "key": "value" ``` is equal to: ```json {"key": "value"} ``` * There is no requirement of quotes for strings and keys, moreover, `:` may be replaced `=` or even be skipped for objects: ```nginx key = value; section { key = value; } ``` is equal to: ```json { "key": "value", "section": { "key": "value" } } ``` * No commas mess: you can safely place a comma or semicolon for the last element in an array or an object: ```json { "key1": "value", "key2": "value", } ``` ### Automatic arrays creation * Non-unique keys in an object are allowed and are automatically converted to the arrays internally: ```json { "key": "value1", "key": "value2" } ``` is converted to: ```json { "key": ["value1", "value2"] } ``` ### Named keys hierarchy UCL accepts named keys and organize them into objects hierarchy internally. Here is an example of this process: ```nginx section "blah" { key = value; } section foo { key = value; } ``` is converted to the following object: ```nginx section { blah { - key = value; + key = value; } foo { - key = value; + key = value; } } ``` Plain definitions may be more complex and contain more than a single level of nested objects: ```nginx section "blah" "foo" { key = value; } ``` is presented as: ```nginx section { blah { - foo { - key = value; - } + foo { + key = value; + } } } ``` ### Convenient numbers and booleans * Numbers can have suffixes to specify standard multipliers: + `[kKmMgG]` - standard 10 base multipliers (so `1k` is translated to 1000) + `[kKmMgG]b` - 2 power multipliers (so `1kb` is translated to 1024) + `[s|min|d|w|y]` - time multipliers, all time values are translated to float number of seconds, for example `10min` is translated to 600.0 and `10ms` is translated to 0.01 * Hexadecimal integers can be used by `0x` prefix, for example `key = 0xff`. However, floating point values can use decimal base only. * Booleans can be specified as `true` or `yes` or `on` and `false` or `no` or `off`. * It is still possible to treat numbers and booleans as strings by enclosing them in double quotes. ## General improvements ### Commments UCL supports different style of comments: * single line: `#` * multiline: `/* ... */` Multiline comments may be nested: ```c # Sample single line comment /* some comment /* nested comment */ end of comment */ ``` ### Macros support UCL supports external macros both multiline and single line ones: ```nginx .macro "sometext"; .macro { - Some long text - .... + Some long text + .... }; ``` Moreover, each macro can accept an optional list of arguments in braces. These arguments themselves are the UCL object that is parsed and passed to a macro as options: ```nginx .macro(param=value) "something"; .macro(param={key=value}) "something"; .macro(.include "params.conf") "something"; .macro(#this is multiline macro param = [value1, value2]) "something"; .macro(key="()") "something"; ``` UCL also provide a convenient `include` macro to load content from another files to the current UCL object. This macro accepts either path to file: ```nginx .include "/full/path.conf" .include "./relative/path.conf" .include "${CURDIR}/path.conf" ``` or URL (if ucl is built with url support provided by either `libcurl` or `libfetch`): .include "http://example.com/file.conf" `.include` macro supports a set of options: * `try` (default: **false**) - if this option is `true` than UCL treats errors on loading of this file as non-fatal. For example, such a file can be absent but it won't stop the parsing of the top-level document. * `sign` (default: **false**) - if this option is `true` UCL loads and checks the signature for a file from path named `.sig`. Trusted public keys should be provided for UCL API after parser is created but before any configurations are parsed. * `glob` (default: **false**) - if this option is `true` UCL treats the filename as GLOB pattern and load all files that matches the specified pattern (normally the format of patterns is defined in `glob` manual page for your operating system). This option is meaningless for URL includes. * `url` (default: **true**) - allow URL includes. * `priority` (default: 0) - specify priority for the include (see below). Priorities are used by UCL parser to manage the policy of objects rewriting during including other files as following: * If we have two objects with the same priority then we form an implicit array * If a new object has bigger priority then we overwrite an old one * If a new object has lower priority then we ignore it By default, the priority of top-level object is set to zero (lowest priority). Currently, you can define up to 16 priorities (from 0 to 15). Includes with bigger priorities will rewrite keys from the objects with lower priorities as specified by the policy. ### Variables support UCL supports variables in input. Variables are registered by a user of the UCL parser and can be presented in the following forms: * `${VARIABLE}` * `$VARIABLE` UCL currently does not support nested variables. To escape variables one could use double dollar signs: * `$${VARIABLE}` is converted to `${VARIABLE}` * `$$VARIABLE` is converted to `$VARIABLE` However, if no valid variables are found in a string, no expansion will be performed (and `$$` thus remains unchanged). This may be a subject to change in future libucl releases. ### Multiline strings UCL can handle multiline strings as well as single line ones. It uses shell/perl like notation for such objects: ``` key = <@]), [], [enable_urls=no]) AC_ARG_ENABLE([regex], AS_HELP_STRING([--enable-regex], [Enable regex checking for schema @<:@default=yes@:>@]), [], [enable_regex=yes]) AC_ARG_ENABLE([signatures], AS_HELP_STRING([--enable-signatures], [Enable signatures check (requires openssl) @<:@default=no@:>@]), [], [enable_signatures=no]) AC_ARG_ENABLE([lua], AS_HELP_STRING([--enable-lua], [Enable lua API build (requires lua libraries and headers) @<:@default=no@:>@]), [], [enable_lua=no]) AC_ARG_ENABLE([utils], AS_HELP_STRING([--enable-utils], [Build and install utils @<:@default=no@:>@]), [case "${enableval}" in yes) utils=true ;; no) utils=false ;; *) AC_MSG_ERROR([bad value ${enableval} for --enable-utils]) ;; esac],[utils=false]) AM_CONDITIONAL([UTILS], [test x$utils = xtrue]) AS_IF([test "x$enable_signatures" = "xyes"], [ AC_SEARCH_LIBS([EVP_MD_CTX_create], [crypto], [ AC_DEFINE(HAVE_OPENSSL, 1, [Define to 1 if you have the 'crypto' library (-lcrypto).]) LIBCRYPTO_LIB="-lcrypto" LIBS_EXTRA="${LIBS_EXTRA} -lcrypto" ], [AC_MSG_ERROR([unable to find the EVP_MD_CTX_create() function])]) ]) AC_SUBST(LIBCRYPTO_LIB) AC_PATH_PROG(PANDOC, pandoc, [/non/existent]) AC_SEARCH_LIBS([clock_gettime], [rt], [], [ AC_CHECK_HEADER([mach/mach_time.h], [ AC_DEFINE(HAVE_MACH_MACH_TIME_H, 1, [Define to 1 on Darwin]) ], [AC_MSG_ERROR([unable to find clock_gettime or mach_absolute_time])]) ]) AC_SEARCH_LIBS([remainder], [m], [], [AC_MSG_ERROR([unable to find remainder() function])]) AS_IF([test "x$enable_regex" = "xyes"], [ AC_CHECK_HEADER([regex.h], [ AC_DEFINE(HAVE_REGEX_H, 1, [Define to 1 if you have the header file.]) AC_SEARCH_LIBS([regexec], [regex], [ AS_IF([test "x$ac_cv_search_regexec" = "x-lregex"], [ LIBREGEX_LIB="-lregex" LIBS_EXTRA="${LIBS_EXTRA} -lregex" ] )], [AC_MSG_ERROR([unable to find the regexec() function])])], [AC_MSG_ERROR([unable to find the regex.h header]) ], [#include ]) ]) AC_SUBST(LIBREGEX_LIB) AS_IF([test "x$enable_lua" = "xyes"], [ AX_PROG_LUA([5.1], [], [ AX_LUA_HEADERS([ AX_LUA_LIBS([ AC_DEFINE(HAVE_LUA, 1, [Define to 1 for lua support.]) with_lua="yes" ], [AC_MSG_ERROR([unable to find the lua libraries]) ]) ], [AC_MSG_ERROR([unable to find the lua header files]) ]) ], [AC_MSG_ERROR([unable to find the lua interpreter])]) ], [with_lua="no"]) AM_CONDITIONAL([LUA_SUB], [test "$with_lua" = "yes"]) AS_IF([test "x$enable_urls" = "xyes"], [ AC_CHECK_HEADER([fetch.h], [ AC_DEFINE(HAVE_FETCH_H, 1, [Define to 1 if you have the header file.]) AC_CHECK_LIB(fetch, fetchXGet, [ AC_DEFINE(HAVE_LIBFETCH, 1, [Define to 1 if you have the 'fetch' library (-lfetch).]) LIBFETCH_LIBS="-lfetch" have_libfetch="yes" LIBS_EXTRA="${LIBS_EXTRA} -lfetch" ]) ], [],[ #include #ifdef HAVE_SYS_PARAM_H #include #endif ]) AC_SUBST(LIBFETCH_LIBS) AS_IF([ test "x$have_libfetch" != "xyes"], [ dnl Fallback to libcurl PKG_CHECK_MODULES([CURL], [libcurl], [ AC_DEFINE(CURL_FOUND, 1, [Use libcurl]) LIBS_EXTRA="${LIBS_EXTRA} -lcurl"], [AC_MSG_ERROR([unable to find neither libfetch nor libcurl])]) ]) AC_SUBST(CURL_FOUND) AC_SUBST(CURL_LIBS) AC_SUBST(CURL_CFLAGS) ]) AC_SUBST(LIBS_EXTRA) AC_MSG_CHECKING(for GCC atomic builtins) AC_LINK_IFELSE([ AC_LANG_SOURCE([[ int main() { volatile unsigned long val = 1; __sync_synchronize(); __sync_val_compare_and_swap(&val, 1, 0); __sync_add_and_fetch(&val, 1); __sync_sub_and_fetch(&val, 1); return 0; } ]]) ], [ AC_MSG_RESULT([yes]) AC_DEFINE([HAVE_ATOMIC_BUILTINS], [1], [Has gcc/MSVC atomic intrinsics]) ], [ AC_MSG_RESULT([no]) AC_DEFINE([HAVE_ATOMIC_BUILTINS], [0], [Has gcc/MSVC atomic intrinsics]) AC_MSG_WARN([Libucl references could be thread-unsafe because atomic builtins are missing]) ]) AC_CONFIG_FILES(Makefile \ src/Makefile \ lua/Makefile tests/Makefile \ utils/Makefile \ doc/Makefile \ lua/libucl.rockspec \ libucl.pc) AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h]) AC_OUTPUT Index: vendor/libucl/dist/doc/Makefile.am =================================================================== --- vendor/libucl/dist/doc/Makefile.am (revision 279545) +++ vendor/libucl/dist/doc/Makefile.am (revision 279546) @@ -1,8 +1,9 @@ EXTRA_DIST = api.md dist_man_MANS = libucl.3 gen-man: @PANDOC@ tail -n +$$(grep -n '# Synopsis' api.md | cut -d':' -f1) api.md | \ - cat pandoc.template - | sed -e 's/^# \(.*\)/# \U\1/' | \ + cat pandoc.template - | sed -e 's/^# \(.*\)/# \U\1/' \ + -e "s/%%date%%/$$(LANG=C date +'%d %B, %Y')/" | \ @PANDOC@ -s -f markdown -t man -o libucl.3 \ No newline at end of file Index: vendor/libucl/dist/doc/api.md =================================================================== --- vendor/libucl/dist/doc/api.md (revision 279545) +++ vendor/libucl/dist/doc/api.md (revision 279546) @@ -1,439 +1,495 @@ # API documentation **Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)* - [Synopsis](#synopsis) - [Description](#description) - [Parser functions](#parser-functions) - [Emitting functions](#emitting-functions) - [Conversion functions](#conversion-functions) - [Generation functions](#generation-functions) - [Iteration functions](#iteration-functions) - [Validation functions](#validation-functions) - [Utility functions](#utility-functions) - [Parser functions](#parser-functions-1) - [ucl_parser_new](#ucl_parser_new) - [ucl_parser_register_macro](#ucl_parser_register_macro) - [ucl_parser_register_variable](#ucl_parser_register_variable) - [ucl_parser_add_chunk](#ucl_parser_add_chunk) - [ucl_parser_add_string](#ucl_parser_add_string) - [ucl_parser_add_file](#ucl_parser_add_file) - [ucl_parser_get_object](#ucl_parser_get_object) - [ucl_parser_get_error](#ucl_parser_get_error) - [ucl_parser_free](#ucl_parser_free) - [ucl_pubkey_add](#ucl_pubkey_add) - [ucl_parser_set_filevars](#ucl_parser_set_filevars) - [Parser usage example](#parser-usage-example) - [Emitting functions](#emitting-functions-1) - [ucl_object_emit](#ucl_object_emit) - [ucl_object_emit_full](#ucl_object_emit_full) - [Conversion functions](#conversion-functions-1) - [Generation functions](#generation-functions-1) - [ucl_object_new](#ucl_object_new) - [ucl_object_typed_new](#ucl_object_typed_new) - [Primitive objects generation](#primitive-objects-generation) - [ucl_object_fromstring_common](#ucl_object_fromstring_common) - [Iteration functions](#iteration-functions-1) - [ucl_iterate_object](#ucl_iterate_object) - [Validation functions](#validation-functions-1) - [ucl_object_validate](#ucl_object_validate) # Synopsis `#include ` # Description Libucl is a parser and `C` API to parse and generate `ucl` objects. Libucl consist of several groups of functions: ### Parser functions Used to parse `ucl` files and provide interface to extract `ucl` object. Currently, `libucl` can parse only full `ucl` documents, for instance, it is impossible to parse a part of document and therefore it is impossible to use `libucl` as a streaming parser. In future, this limitation can be removed. ### Emitting functions Convert `ucl` objects to some textual or binary representation. Currently, libucl supports the following exports: - `JSON` - valid json format (can possibly lose some original data, such as implicit arrays) - `Config` - human-readable configuration format (lossless) - `YAML` - embedded yaml format (has the same limitations as `json` output) ### Conversion functions Help to convert `ucl` objects to C types. These functions are used to convert `ucl_object_t` to C primitive types, such as numbers, strings or boolean values. ### Generation functions Allow creation of `ucl` objects from C types and creating of complex `ucl` objects, such as hashes or arrays from primitive `ucl` objects, such as numbers or strings. ### Iteration functions Iterate over `ucl` complex objects or over a chain of values, for example when a key in an object has multiple values (that can be treated as implicit array or implicit consolidation). ### Validation functions Validation functions are used to validate some object `obj` using json-schema compatible object `schema`. Both input and schema must be UCL objects to perform validation. ### Utility functions Provide basic utilities to manage `ucl` objects: creating, removing, retaining and releasing reference count and so on. # Parser functions Parser functions operates with `struct ucl_parser`. ### ucl_parser_new ~~~C struct ucl_parser* ucl_parser_new (int flags); ~~~ Creates new parser with the specified flags: - `UCL_PARSER_KEY_LOWERCASE` - lowercase keys parsed - `UCL_PARSER_ZEROCOPY` - try to use zero-copy mode when reading files (in zero-copy mode text chunk being parsed without copying strings so it should exist till any object parsed is used) - `UCL_PARSER_NO_TIME` - treat time values as strings without parsing them as floats ### ucl_parser_register_macro ~~~C void ucl_parser_register_macro (struct ucl_parser *parser, const char *macro, ucl_macro_handler handler, void* ud); ~~~ Register new macro with name .`macro` parsed by handler `handler` that accepts opaque data pointer `ud`. Macro handler should be of the following type: ~~~C bool (*ucl_macro_handler) (const unsigned char *data, size_t len, void* ud);` ~~~ Handler function accepts macro text `data` of length `len` and the opaque pointer `ud`. If macro is parsed successfully the handler should return `true`. `false` indicates parsing failure and the parser can be terminated. ### ucl_parser_register_variable ~~~C void ucl_parser_register_variable (struct ucl_parser *parser, const char *var, const char *value); ~~~ Register new variable $`var` that should be replaced by the parser to the `value` string. ### ucl_parser_add_chunk ~~~C bool ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data, size_t len); ~~~ Add new text chunk with `data` of length `len` to the parser. At the moment, `libucl` parser is not a streamlined parser and chunk *must* contain the *valid* ucl object. For example, this object should be valid: ~~~json { "var": "value" } ~~~ while this one won't be parsed correctly: ~~~json { "var": ~~~ This limitation may possible be removed in future. ### ucl_parser_add_string ~~~C bool ucl_parser_add_string (struct ucl_parser *parser, const char *data, size_t len); ~~~ This function acts exactly like `ucl_parser_add_chunk` does but if `len` argument is zero, then the string `data` must be zero-terminated and the actual length is calculated up to `\0` character. ### ucl_parser_add_file ~~~C bool ucl_parser_add_file (struct ucl_parser *parser, const char *filename); ~~~ Load file `filename` and parse it with the specified `parser`. This function uses `mmap` call to load file, therefore, it should not be `shrunk` during parsing. Otherwise, `libucl` can cause memory corruption and terminate the calling application. This function is also used by the internal handler of `include` macro, hence, this macro has the same limitation. ### ucl_parser_get_object ~~~C ucl_object_t* ucl_parser_get_object (struct ucl_parser *parser); ~~~ If the `ucl` data has been parsed correctly this function returns the top object for the parser. Otherwise, this function returns the `NULL` pointer. The reference count for `ucl` object returned is increased by one, therefore, a caller should decrease reference by using `ucl_object_unref` to free object after usage. ### ucl_parser_get_error ~~~C const char *ucl_parser_get_error(struct ucl_parser *parser); ~~~ Returns the constant error string for the parser object. If no error occurred during parsing a `NULL` object is returned. A caller should not try to free or modify this string. ### ucl_parser_free ~~~C void ucl_parser_free (struct ucl_parser *parser); ~~~ Frees memory occupied by the parser object. The reference count for top object is decreased as well, however if the function `ucl_parser_get_object` was called previously then the top object won't be freed. ### ucl_pubkey_add ~~~C bool ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len); ~~~ This function adds a public key from text blob `key` of length `len` to the `parser` object. This public key should be in the `PEM` format and can be used by `.includes` macro for checking signatures of files included. `Openssl` support should be enabled to make this function working. If a key cannot be added (e.g. due to format error) or `openssl` was not linked to `libucl` then this function returns `false`. ### ucl_parser_set_filevars ~~~C bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand); ~~~ Add the standard file variables to the `parser` based on the `filename` specified: - `$FILENAME` - a filename of `ucl` input - `$CURDIR` - a current directory of the input For example, if a `filename` param is `../something.conf` then the variables will have the following values: - `$FILENAME` - "../something.conf" - `$CURDIR` - ".." if `need_expand` parameter is `true` then all relative paths are expanded using `realpath` call. In this example if `..` is `/etc/dir` then variables will have these values: - `$FILENAME` - "/etc/something.conf" - `$CURDIR` - "/etc" ## Parser usage example The following example loads, parses and extracts `ucl` object from stdin using `libucl` parser functions (the length of input is limited to 8K): ~~~C char inbuf[8192]; struct ucl_parser *parser = NULL; int ret = 0, r = 0; ucl_object_t *obj = NULL; FILE *in; in = stdin; parser = ucl_parser_new (0); while (!feof (in) && r < (int)sizeof (inbuf)) { r += fread (inbuf + r, 1, sizeof (inbuf) - r, in); } ucl_parser_add_chunk (parser, inbuf, r); fclose (in); if (ucl_parser_get_error (parser)) { printf ("Error occurred: %s\n", ucl_parser_get_error (parser)); ret = 1; } else { obj = ucl_parser_get_object (parser); } if (parser != NULL) { ucl_parser_free (parser); } if (obj != NULL) { ucl_object_unref (obj); } return ret; ~~~ # Emitting functions Libucl can transform UCL objects to a number of tectual formats: - configuration (`UCL_EMIT_CONFIG`) - nginx like human readable configuration file where implicit arrays are transformed to the duplicate keys - compact json: `UCL_EMIT_JSON_COMPACT` - single line valid json without spaces - formatted json: `UCL_EMIT_JSON` - pretty formatted JSON with newlines and spaces - compact yaml: `UCL_EMIT_YAML` - compact YAML output Moreover, libucl API allows to select a custom set of emitting functions allowing efficient and zero-copy output of libucl objects. Libucl uses the following structure to support this feature: ~~~C struct ucl_emitter_functions { /** Append a single character */ int (*ucl_emitter_append_character) (unsigned char c, size_t nchars, void *ud); /** Append a string of a specified length */ int (*ucl_emitter_append_len) (unsigned const char *str, size_t len, void *ud); /** Append a 64 bit integer */ int (*ucl_emitter_append_int) (int64_t elt, void *ud); /** Append floating point element */ int (*ucl_emitter_append_double) (double elt, void *ud); /** Opaque userdata pointer */ void *ud; }; ~~~ This structure defines the following callbacks: - `ucl_emitter_append_character` - a function that is called to append `nchars` characters equal to `c` - `ucl_emitter_append_len` - used to append a string of length `len` starting from pointer `str` - `ucl_emitter_append_int` - this function applies to integer numbers - `ucl_emitter_append_double` - this function is intended to output floating point variable The set of these functions could be used to output text formats of `UCL` objects to different structures or streams. Libucl provides the following functions for emitting UCL objects: ### ucl_object_emit ~~~C unsigned char *ucl_object_emit (const ucl_object_t *obj, enum ucl_emitter emit_type); ~~~ Allocate a string that is suitable to fit the underlying UCL object `obj` and fill it with the textual representation of the object `obj` according to style `emit_type`. The caller should free the returned string after using. ### ucl_object_emit_full ~~~C bool ucl_object_emit_full (const ucl_object_t *obj, enum ucl_emitter emit_type, struct ucl_emitter_functions *emitter); ~~~ This function is similar to the previous with the exception that it accepts the additional argument `emitter` that defines the concrete set of output functions. This emit function could be useful for custom structures or streams emitters (including C++ ones, for example). # Conversion functions Conversion functions are used to convert UCL objects to primitive types, such as strings, numbers, or boolean values. There are two types of conversion functions: - safe: try to convert an ucl object to a primitive type and fail if such a conversion is not possible - unsafe: return primitive type without additional checks, if the object cannot be converted then some reasonable default is returned (NULL for strings and 0 for numbers) Also there is a single `ucl_object_tostring_forced` function that converts any UCL object (including compound types - arrays and objects) to a string representation. For objects, arrays, booleans and numeric types this function performs emitting to a compact json format actually. Here is a list of all conversion functions: - `ucl_object_toint` - returns `int64_t` of UCL object - `ucl_object_todouble` - returns `double` of UCL object - `ucl_object_toboolean` - returns `bool` of UCL object - `ucl_object_tostring` - returns `const char *` of UCL object (this string is NULL terminated) - `ucl_object_tolstring` - returns `const char *` and `size_t` len of UCL object (string does not need to be NULL terminated) - `ucl_object_tostring_forced` - returns string representation of any UCL object Strings returned by these pointers are associated with the UCL object and exist over its lifetime. A caller should not free this memory. # Generation functions It is possible to generate UCL objects from C primitive types. Moreover, libucl allows creation and modifying complex UCL objects, such as arrays or associative objects. ## ucl_object_new ~~~C ucl_object_t * ucl_object_new (void) ~~~ Creates new object of type `UCL_NULL`. This object should be released by caller. ## ucl_object_typed_new ~~~C ucl_object_t * ucl_object_typed_new (unsigned int type) ~~~ Create an object of a specified type: - `UCL_OBJECT` - UCL object - key/value pairs - `UCL_ARRAY` - UCL array - `UCL_INT` - integer number - `UCL_FLOAT` - floating point number - `UCL_STRING` - NULL terminated string - `UCL_BOOLEAN` - boolean value - `UCL_TIME` - time value (floating point number of seconds) - `UCL_USERDATA` - opaque userdata pointer (may be used in macros) - `UCL_NULL` - null value This object should be released by caller. ## Primitive objects generation Libucl provides the functions similar to inverse conversion functions called with the specific C type: - `ucl_object_fromint` - converts `int64_t` to UCL object - `ucl_object_fromdouble` - converts `double` to UCL object - `ucl_object_fromboolean` - converts `bool` to UCL object - `ucl_object_fromstring` - converts `const char *` to UCL object (this string should be NULL terminated) - `ucl_object_fromlstring` - converts `const char *` and `size_t` len to UCL object (string does not need to be NULL terminated) Also there is a function to generate UCL object from a string performing various parsing or conversion operations called `ucl_object_fromstring_common`. ## ucl_object_fromstring_common ~~~C ucl_object_t * ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags flags) ~~~ This function is used to convert a string `str` of size `len` to a UCL object applying `flags` conversions. If `len` is equal to zero then a `str` is assumed as NULL-terminated. This function supports the following flags (a set of flags can be specified using logical `OR` operation): - `UCL_STRING_ESCAPE` - perform JSON escape - `UCL_STRING_TRIM` - trim leading and trailing whitespaces - `UCL_STRING_PARSE_BOOLEAN` - parse passed string and detect boolean - `UCL_STRING_PARSE_INT` - parse passed string and detect integer number - `UCL_STRING_PARSE_DOUBLE` - parse passed string and detect integer or float number - `UCL_STRING_PARSE_TIME` - parse time values as floating point numbers - `UCL_STRING_PARSE_NUMBER` - parse passed string and detect number (both float, integer and time types) - `UCL_STRING_PARSE` - parse passed string (and detect booleans, numbers and time values) - `UCL_STRING_PARSE_BYTES` - assume that numeric multipliers are in bytes notation, for example `10k` means `10*1024` and not `10*1000` as assumed without this flag If parsing operations fail then the resulting UCL object will be a `UCL_STRING`. A caller should always check the type of the returned object and release it after using. # Iteration functions -Iteration are used to iterate over UCL compound types: arrays and objects. Moreover, iterations could be performed over the keys with multiple values (implicit arrays). To iterate over an object, an array or a key with multiple values there is a function `ucl_iterate_object`. +Iteration are used to iterate over UCL compound types: arrays and objects. Moreover, iterations could be performed over the keys with multiple values (implicit arrays). +There are two types of iterators API: old and unsafe one via `ucl_iterate_object` and the proposed interface of safe iterators. + ## ucl_iterate_object ~~~C const ucl_object_t* ucl_iterate_object (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expand_values); ~~~ This function accepts opaque iterator pointer `iter`. In the first call this iterator *must* be initialized to `NULL`. Iterator is changed by this function call. `ucl_iterate_object` returns the next UCL object in the compound object `obj` or `NULL` if all objects have been iterated. The reference count of the object returned is not increased, so a caller should not unref the object or modify its content (e.g. by inserting to another compound object). The object `obj` should not be changed during the iteration process as well. `expand_values` flag speicifies whether `ucl_iterate_object` should expand keys with multiple values. The general rule is that if you need to iterate through the *object* or *explicit array*, then you always need to set this flag to `true`. However, if you get some key in the object and want to extract all its values then you should set `expand_values` to `false`. Mixing of iteration types is not permitted since the iterator is set according to the iteration type and cannot be reused. Here is an example of iteration over the objects using libucl API (assuming that `top` is `UCL_OBJECT` in this example): ~~~C ucl_object_iter_t it = NULL, it_obj = NULL; const ucl_object_t *cur, *tmp; /* Iterate over the object */ while ((obj = ucl_iterate_object (top, &it, true))) { printf ("key: \"%s\"\n", ucl_object_key (obj)); /* Iterate over the values of a key */ while ((cur = ucl_iterate_object (obj, &it_obj, false))) { printf ("value: \"%s\"\n", ucl_object_tostring_forced (cur)); } } +~~~ + +## Safe iterators API + +Safe iterators are defined to clarify iterating over UCL objects and simplify flattening of UCL objects in non-trivial cases. +For example, if there is an implicit array that contains another array and a boolean value it is extremely unclear how to iterate over +such an object. Safe iterators are desinged to define two sorts of iteration: + +1. Iteration over complex objects with expanding all values +2. Iteration over complex objects without expanding of values + +The following example demonstrates the difference between these two types of iteration: + +~~~ +key = 1; +key = [2, 3, 4]; + +Iteration with expansion: + +1, 2, 3, 4 + +Iteration without expansion: + +1, [2, 3, 4] +~~~ + +UCL defines the following functions to manage safe iterators: + +- `ucl_object_iterate_new` - creates new safe iterator +- `ucl_object_iterate_reset` - resets iterator to a new object +- `ucl_object_iterate_safe` - safely iterate the object inside iterator +- `ucl_object_iterate_free` - free memory associated with the safe iterator + +Please note that unlike unsafe iterators, safe iterators *must* be explicitly initialized and freed. +An assert is likely generated if you use uninitialized or `NULL` iterator in all safe iterators functions. + +~~~C +ucl_object_iter_t it; +const ucl_object_t *cur; + +it = ucl_object_iterate_new (obj); + +while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { + /* Do something */ +} + +/* Switch to another object */ +it = ucl_object_iterate_reset (it, another_obj); + +while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { + /* Do something else */ +} + +ucl_object_iterate_free (it); ~~~ # Validation functions Currently, there is only one validation function called `ucl_object_validate`. It performs validation of object using the specified schema. This function is defined as following: ## ucl_object_validate ~~~C bool ucl_object_validate (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err); ~~~ This function uses ucl object `schema`, that must be valid in terms of `json-schema` draft v4, to validate input object `obj`. If this function returns `true` then validation procedure has been succeed. Otherwise, `false` is returned and `err` is set to a specific value. If a caller sets `err` to NULL then this function does not set any error just returning `false`. Error is the structure defined as following: ~~~C struct ucl_schema_error { enum ucl_schema_error_code code; /* error code */ char msg[128]; /* error message */ ucl_object_t *obj; /* object where error occurred */ }; ~~~ Caller may use `code` field to get a numeric error code: ~~~C enum ucl_schema_error_code { UCL_SCHEMA_OK = 0, /* no error */ UCL_SCHEMA_TYPE_MISMATCH, /* type of object is incorrect */ UCL_SCHEMA_INVALID_SCHEMA, /* schema is invalid */ UCL_SCHEMA_MISSING_PROPERTY,/* missing properties */ UCL_SCHEMA_CONSTRAINT, /* constraint found */ UCL_SCHEMA_MISSING_DEPENDENCY, /* missing dependency */ UCL_SCHEMA_UNKNOWN /* generic error */ }; ~~~ `msg` is a string description of an error and `obj` is an object where error has occurred. Error object is not allocated by libucl, so there is no need to free it after validation (a static object should thus be used). Index: vendor/libucl/dist/doc/libucl.3 =================================================================== --- vendor/libucl/dist/doc/libucl.3 (revision 279545) +++ vendor/libucl/dist/doc/libucl.3 (revision 279546) @@ -1,638 +1,708 @@ -.TH "LIBUCL" "3" "July 26, 2014" "Libucl manual" "" +.TH "LIBUCL" "3" "27 December, 2014" "Libucl manual" "" .SH NAME .PP \f[B]ucl_parser_new\f[], \f[B]ucl_parser_register_macro\f[], \f[B]ucl_parser_register_variable\f[], \f[B]ucl_parser_add_chunk\f[], \f[B]ucl_parser_add_string\f[], \f[B]ucl_parser_add_file\f[], \f[B]ucl_parser_get_object\f[], \f[B]ucl_parser_get_error\f[], \f[B]ucl_parser_free\f[], \f[B]ucl_pubkey_add\f[], \f[B]ucl_parser_set_filevars\f[] \- universal configuration library parser and utility functions .SH LIBRARY .PP UCL library (libucl, \-lucl) .SH SYNOPSIS .PP \f[C]#include\ \f[] .SH DESCRIPTION .PP Libucl is a parser and \f[C]C\f[] API to parse and generate \f[C]ucl\f[] objects. Libucl consist of several groups of functions: .SS Parser functions .PP Used to parse \f[C]ucl\f[] files and provide interface to extract \f[C]ucl\f[] object. Currently, \f[C]libucl\f[] can parse only full \f[C]ucl\f[] documents, for instance, it is impossible to parse a part of document and therefore it is impossible to use \f[C]libucl\f[] as a streaming parser. In future, this limitation can be removed. .SS Emitting functions .PP Convert \f[C]ucl\f[] objects to some textual or binary representation. Currently, libucl supports the following exports: .IP \[bu] 2 \f[C]JSON\f[] \- valid json format (can possibly lose some original data, such as implicit arrays) .IP \[bu] 2 \f[C]Config\f[] \- human\-readable configuration format (lossless) .IP \[bu] 2 \f[C]YAML\f[] \- embedded yaml format (has the same limitations as \f[C]json\f[] output) .SS Conversion functions .PP Help to convert \f[C]ucl\f[] objects to C types. These functions are used to convert \f[C]ucl_object_t\f[] to C primitive types, such as numbers, strings or boolean values. .SS Generation functions .PP Allow creation of \f[C]ucl\f[] objects from C types and creating of complex \f[C]ucl\f[] objects, such as hashes or arrays from primitive \f[C]ucl\f[] objects, such as numbers or strings. .SS Iteration functions .PP Iterate over \f[C]ucl\f[] complex objects or over a chain of values, for example when a key in an object has multiple values (that can be treated as implicit array or implicit consolidation). .SS Validation functions .PP Validation functions are used to validate some object \f[C]obj\f[] using json\-schema compatible object \f[C]schema\f[]. Both input and schema must be UCL objects to perform validation. .SS Utility functions .PP Provide basic utilities to manage \f[C]ucl\f[] objects: creating, removing, retaining and releasing reference count and so on. .SH PARSER FUNCTIONS .PP Parser functions operates with \f[C]struct\ ucl_parser\f[]. .SS ucl_parser_new .IP .nf \f[C] struct\ ucl_parser*\ ucl_parser_new\ (int\ flags); \f[] .fi .PP Creates new parser with the specified flags: .IP \[bu] 2 \f[C]UCL_PARSER_KEY_LOWERCASE\f[] \- lowercase keys parsed .IP \[bu] 2 \f[C]UCL_PARSER_ZEROCOPY\f[] \- try to use zero\-copy mode when reading files (in zero\-copy mode text chunk being parsed without copying strings so it should exist till any object parsed is used) .IP \[bu] 2 \f[C]UCL_PARSER_NO_TIME\f[] \- treat time values as strings without parsing them as floats .SS ucl_parser_register_macro .IP .nf \f[C] void\ ucl_parser_register_macro\ (struct\ ucl_parser\ *parser, \ \ \ \ const\ char\ *macro,\ ucl_macro_handler\ handler,\ void*\ ud); \f[] .fi .PP Register new macro with name .\f[C]macro\f[] parsed by handler \f[C]handler\f[] that accepts opaque data pointer \f[C]ud\f[]. Macro handler should be of the following type: .IP .nf \f[C] bool\ (*ucl_macro_handler)\ (const\ unsigned\ char\ *data, \ \ \ \ size_t\ len,\ void*\ ud);` \f[] .fi .PP Handler function accepts macro text \f[C]data\f[] of length \f[C]len\f[] and the opaque pointer \f[C]ud\f[]. If macro is parsed successfully the handler should return \f[C]true\f[]. \f[C]false\f[] indicates parsing failure and the parser can be terminated. .SS ucl_parser_register_variable .IP .nf \f[C] void\ ucl_parser_register_variable\ (struct\ ucl_parser\ *parser, \ \ \ \ const\ char\ *var,\ const\ char\ *value); \f[] .fi .PP Register new variable $\f[C]var\f[] that should be replaced by the parser to the \f[C]value\f[] string. .SS ucl_parser_add_chunk .IP .nf \f[C] bool\ ucl_parser_add_chunk\ (struct\ ucl_parser\ *parser,\ \ \ \ \ const\ unsigned\ char\ *data,\ size_t\ len); \f[] .fi .PP Add new text chunk with \f[C]data\f[] of length \f[C]len\f[] to the parser. At the moment, \f[C]libucl\f[] parser is not a streamlined parser and chunk \f[I]must\f[] contain the \f[I]valid\f[] ucl object. For example, this object should be valid: .IP .nf \f[C] {\ "var":\ "value"\ } \f[] .fi .PP while this one won\[aq]t be parsed correctly: .IP .nf \f[C] {\ "var":\ \f[] .fi .PP This limitation may possible be removed in future. .SS ucl_parser_add_string .IP .nf \f[C] bool\ ucl_parser_add_string\ (struct\ ucl_parser\ *parser,\ \ \ \ \ const\ char\ *data,\ size_t\ len); \f[] .fi .PP This function acts exactly like \f[C]ucl_parser_add_chunk\f[] does but if \f[C]len\f[] argument is zero, then the string \f[C]data\f[] must be zero\-terminated and the actual length is calculated up to \f[C]\\0\f[] character. .SS ucl_parser_add_file .IP .nf \f[C] bool\ ucl_parser_add_file\ (struct\ ucl_parser\ *parser,\ \ \ \ \ const\ char\ *filename); \f[] .fi .PP Load file \f[C]filename\f[] and parse it with the specified \f[C]parser\f[]. This function uses \f[C]mmap\f[] call to load file, therefore, it should not be \f[C]shrunk\f[] during parsing. Otherwise, \f[C]libucl\f[] can cause memory corruption and terminate the calling application. This function is also used by the internal handler of \f[C]include\f[] macro, hence, this macro has the same limitation. .SS ucl_parser_get_object .IP .nf \f[C] ucl_object_t*\ ucl_parser_get_object\ (struct\ ucl_parser\ *parser); \f[] .fi .PP If the \f[C]ucl\f[] data has been parsed correctly this function returns the top object for the parser. Otherwise, this function returns the \f[C]NULL\f[] pointer. The reference count for \f[C]ucl\f[] object returned is increased by one, therefore, a caller should decrease reference by using \f[C]ucl_object_unref\f[] to free object after usage. .SS ucl_parser_get_error .IP .nf \f[C] const\ char\ *ucl_parser_get_error(struct\ ucl_parser\ *parser); \f[] .fi .PP Returns the constant error string for the parser object. If no error occurred during parsing a \f[C]NULL\f[] object is returned. A caller should not try to free or modify this string. .SS ucl_parser_free .IP .nf \f[C] void\ ucl_parser_free\ (struct\ ucl_parser\ *parser); \f[] .fi .PP Frees memory occupied by the parser object. The reference count for top object is decreased as well, however if the function \f[C]ucl_parser_get_object\f[] was called previously then the top object won\[aq]t be freed. .SS ucl_pubkey_add .IP .nf \f[C] bool\ ucl_pubkey_add\ (struct\ ucl_parser\ *parser,\ \ \ \ \ const\ unsigned\ char\ *key,\ size_t\ len); \f[] .fi .PP This function adds a public key from text blob \f[C]key\f[] of length \f[C]len\f[] to the \f[C]parser\f[] object. This public key should be in the \f[C]PEM\f[] format and can be used by \f[C]\&.includes\f[] macro for checking signatures of files included. \f[C]Openssl\f[] support should be enabled to make this function working. If a key cannot be added (e.g. due to format error) or \f[C]openssl\f[] was not linked to \f[C]libucl\f[] then this function returns \f[C]false\f[]. .SS ucl_parser_set_filevars .IP .nf \f[C] bool\ ucl_parser_set_filevars\ (struct\ ucl_parser\ *parser,\ \ \ \ \ const\ char\ *filename,\ bool\ need_expand); \f[] .fi .PP Add the standard file variables to the \f[C]parser\f[] based on the \f[C]filename\f[] specified: .IP \[bu] 2 \f[C]$FILENAME\f[] \- a filename of \f[C]ucl\f[] input .IP \[bu] 2 \f[C]$CURDIR\f[] \- a current directory of the input .PP For example, if a \f[C]filename\f[] param is \f[C]\&../something.conf\f[] then the variables will have the following values: .IP \[bu] 2 \f[C]$FILENAME\f[] \- "../something.conf" .IP \[bu] 2 \f[C]$CURDIR\f[] \- ".." .PP if \f[C]need_expand\f[] parameter is \f[C]true\f[] then all relative paths are expanded using \f[C]realpath\f[] call. In this example if \f[C]\&..\f[] is \f[C]/etc/dir\f[] then variables will have these values: .IP \[bu] 2 \f[C]$FILENAME\f[] \- "/etc/something.conf" .IP \[bu] 2 \f[C]$CURDIR\f[] \- "/etc" .SS Parser usage example .PP The following example loads, parses and extracts \f[C]ucl\f[] object from stdin using \f[C]libucl\f[] parser functions (the length of input is limited to 8K): .IP .nf \f[C] char\ inbuf[8192]; struct\ ucl_parser\ *parser\ =\ NULL; int\ ret\ =\ 0,\ r\ =\ 0; ucl_object_t\ *obj\ =\ NULL; FILE\ *in; in\ =\ stdin; parser\ =\ ucl_parser_new\ (0); while\ (!feof\ (in)\ &&\ r\ <\ (int)sizeof\ (inbuf))\ { \ \ \ \ r\ +=\ fread\ (inbuf\ +\ r,\ 1,\ sizeof\ (inbuf)\ \-\ r,\ in); } ucl_parser_add_chunk\ (parser,\ inbuf,\ r); fclose\ (in); if\ (ucl_parser_get_error\ (parser))\ { \ \ \ \ printf\ ("Error\ occurred:\ %s\\n",\ ucl_parser_get_error\ (parser)); \ \ \ \ ret\ =\ 1; } else\ { \ \ \ \ obj\ =\ ucl_parser_get_object\ (parser); } if\ (parser\ !=\ NULL)\ { \ \ \ \ ucl_parser_free\ (parser); } if\ (obj\ !=\ NULL)\ { \ \ \ \ ucl_object_unref\ (obj); } return\ ret; \f[] .fi .SH EMITTING FUNCTIONS .PP Libucl can transform UCL objects to a number of tectual formats: .IP \[bu] 2 configuration (\f[C]UCL_EMIT_CONFIG\f[]) \- nginx like human readable configuration file where implicit arrays are transformed to the duplicate keys .IP \[bu] 2 compact json: \f[C]UCL_EMIT_JSON_COMPACT\f[] \- single line valid json without spaces .IP \[bu] 2 formatted json: \f[C]UCL_EMIT_JSON\f[] \- pretty formatted JSON with newlines and spaces .IP \[bu] 2 compact yaml: \f[C]UCL_EMIT_YAML\f[] \- compact YAML output .PP Moreover, libucl API allows to select a custom set of emitting functions allowing efficient and zero\-copy output of libucl objects. Libucl uses the following structure to support this feature: .IP .nf \f[C] struct\ ucl_emitter_functions\ { \ \ \ \ /**\ Append\ a\ single\ character\ */ \ \ \ \ int\ (*ucl_emitter_append_character)\ (unsigned\ char\ c,\ size_t\ nchars,\ void\ *ud); \ \ \ \ /**\ Append\ a\ string\ of\ a\ specified\ length\ */ \ \ \ \ int\ (*ucl_emitter_append_len)\ (unsigned\ const\ char\ *str,\ size_t\ len,\ void\ *ud); \ \ \ \ /**\ Append\ a\ 64\ bit\ integer\ */ \ \ \ \ int\ (*ucl_emitter_append_int)\ (int64_t\ elt,\ void\ *ud); \ \ \ \ /**\ Append\ floating\ point\ element\ */ \ \ \ \ int\ (*ucl_emitter_append_double)\ (double\ elt,\ void\ *ud); \ \ \ \ /**\ Opaque\ userdata\ pointer\ */ \ \ \ \ void\ *ud; }; \f[] .fi .PP This structure defines the following callbacks: .IP \[bu] 2 \f[C]ucl_emitter_append_character\f[] \- a function that is called to append \f[C]nchars\f[] characters equal to \f[C]c\f[] .IP \[bu] 2 \f[C]ucl_emitter_append_len\f[] \- used to append a string of length \f[C]len\f[] starting from pointer \f[C]str\f[] .IP \[bu] 2 \f[C]ucl_emitter_append_int\f[] \- this function applies to integer numbers .IP \[bu] 2 \f[C]ucl_emitter_append_double\f[] \- this function is intended to output floating point variable .PP The set of these functions could be used to output text formats of \f[C]UCL\f[] objects to different structures or streams. .PP Libucl provides the following functions for emitting UCL objects: .SS ucl_object_emit .IP .nf \f[C] unsigned\ char\ *ucl_object_emit\ (const\ ucl_object_t\ *obj,\ enum\ ucl_emitter\ emit_type); \f[] .fi .PP Allocate a string that is suitable to fit the underlying UCL object \f[C]obj\f[] and fill it with the textual representation of the object \f[C]obj\f[] according to style \f[C]emit_type\f[]. The caller should free the returned string after using. .SS ucl_object_emit_full .IP .nf \f[C] bool\ ucl_object_emit_full\ (const\ ucl_object_t\ *obj,\ enum\ ucl_emitter\ emit_type, \ \ \ \ \ \ \ \ struct\ ucl_emitter_functions\ *emitter); \f[] .fi .PP This function is similar to the previous with the exception that it accepts the additional argument \f[C]emitter\f[] that defines the concrete set of output functions. This emit function could be useful for custom structures or streams emitters (including C++ ones, for example). .SH CONVERSION FUNCTIONS .PP Conversion functions are used to convert UCL objects to primitive types, such as strings, numbers, or boolean values. There are two types of conversion functions: .IP \[bu] 2 safe: try to convert an ucl object to a primitive type and fail if such a conversion is not possible .IP \[bu] 2 unsafe: return primitive type without additional checks, if the object cannot be converted then some reasonable default is returned (NULL for strings and 0 for numbers) .PP Also there is a single \f[C]ucl_object_tostring_forced\f[] function that converts any UCL object (including compound types \- arrays and objects) to a string representation. For objects, arrays, booleans and numeric types this function performs emitting to a compact json format actually. .PP Here is a list of all conversion functions: .IP \[bu] 2 \f[C]ucl_object_toint\f[] \- returns \f[C]int64_t\f[] of UCL object .IP \[bu] 2 \f[C]ucl_object_todouble\f[] \- returns \f[C]double\f[] of UCL object .IP \[bu] 2 \f[C]ucl_object_toboolean\f[] \- returns \f[C]bool\f[] of UCL object .IP \[bu] 2 \f[C]ucl_object_tostring\f[] \- returns \f[C]const\ char\ *\f[] of UCL object (this string is NULL terminated) .IP \[bu] 2 \f[C]ucl_object_tolstring\f[] \- returns \f[C]const\ char\ *\f[] and \f[C]size_t\f[] len of UCL object (string does not need to be NULL terminated) .IP \[bu] 2 \f[C]ucl_object_tostring_forced\f[] \- returns string representation of any UCL object .PP Strings returned by these pointers are associated with the UCL object and exist over its lifetime. A caller should not free this memory. .SH GENERATION FUNCTIONS .PP It is possible to generate UCL objects from C primitive types. Moreover, libucl allows creation and modifying complex UCL objects, such as arrays or associative objects. .SS ucl_object_new .IP .nf \f[C] ucl_object_t\ *\ ucl_object_new\ (void) \f[] .fi .PP Creates new object of type \f[C]UCL_NULL\f[]. This object should be released by caller. .SS ucl_object_typed_new .IP .nf \f[C] ucl_object_t\ *\ ucl_object_typed_new\ (unsigned\ int\ type) \f[] .fi .PP Create an object of a specified type: \- \f[C]UCL_OBJECT\f[] \- UCL object \- key/value pairs \- \f[C]UCL_ARRAY\f[] \- UCL array \- \f[C]UCL_INT\f[] \- integer number \- \f[C]UCL_FLOAT\f[] \- floating point number \- \f[C]UCL_STRING\f[] \- NULL terminated string \- \f[C]UCL_BOOLEAN\f[] \- boolean value \- \f[C]UCL_TIME\f[] \- time value (floating point number of seconds) \- \f[C]UCL_USERDATA\f[] \- opaque userdata pointer (may be used in macros) \- \f[C]UCL_NULL\f[] \- null value .PP This object should be released by caller. .SS Primitive objects generation .PP Libucl provides the functions similar to inverse conversion functions called with the specific C type: \- \f[C]ucl_object_fromint\f[] \- converts \f[C]int64_t\f[] to UCL object \- \f[C]ucl_object_fromdouble\f[] \- converts \f[C]double\f[] to UCL object \- \f[C]ucl_object_fromboolean\f[] \- converts \f[C]bool\f[] to UCL object \- \f[C]ucl_object_fromstring\f[] \- converts \f[C]const\ char\ *\f[] to UCL object (this string should be NULL terminated) \- \f[C]ucl_object_fromlstring\f[] \- converts \f[C]const\ char\ *\f[] and \f[C]size_t\f[] len to UCL object (string does not need to be NULL terminated) .PP Also there is a function to generate UCL object from a string performing various parsing or conversion operations called \f[C]ucl_object_fromstring_common\f[]. .SS ucl_object_fromstring_common .IP .nf \f[C] ucl_object_t\ *\ ucl_object_fromstring_common\ (const\ char\ *str,\ \ \ \ \ size_t\ len,\ enum\ ucl_string_flags\ flags) \f[] .fi .PP This function is used to convert a string \f[C]str\f[] of size \f[C]len\f[] to a UCL object applying \f[C]flags\f[] conversions. If \f[C]len\f[] is equal to zero then a \f[C]str\f[] is assumed as NULL\-terminated. This function supports the following flags (a set of flags can be specified using logical \f[C]OR\f[] operation): .IP \[bu] 2 \f[C]UCL_STRING_ESCAPE\f[] \- perform JSON escape .IP \[bu] 2 \f[C]UCL_STRING_TRIM\f[] \- trim leading and trailing whitespaces .IP \[bu] 2 \f[C]UCL_STRING_PARSE_BOOLEAN\f[] \- parse passed string and detect boolean .IP \[bu] 2 \f[C]UCL_STRING_PARSE_INT\f[] \- parse passed string and detect integer number .IP \[bu] 2 \f[C]UCL_STRING_PARSE_DOUBLE\f[] \- parse passed string and detect integer or float number .IP \[bu] 2 \f[C]UCL_STRING_PARSE_TIME\f[] \- parse time values as floating point numbers .IP \[bu] 2 \f[C]UCL_STRING_PARSE_NUMBER\f[] \- parse passed string and detect number (both float, integer and time types) .IP \[bu] 2 \f[C]UCL_STRING_PARSE\f[] \- parse passed string (and detect booleans, numbers and time values) .IP \[bu] 2 \f[C]UCL_STRING_PARSE_BYTES\f[] \- assume that numeric multipliers are in bytes notation, for example \f[C]10k\f[] means \f[C]10*1024\f[] and not \f[C]10*1000\f[] as assumed without this flag .PP If parsing operations fail then the resulting UCL object will be a \f[C]UCL_STRING\f[]. A caller should always check the type of the returned object and release it after using. .SH ITERATION FUNCTIONS .PP Iteration are used to iterate over UCL compound types: arrays and objects. Moreover, iterations could be performed over the keys with multiple values (implicit arrays). -To iterate over an object, an array or a key with multiple values there -is a function \f[C]ucl_iterate_object\f[]. +There are two types of iterators API: old and unsafe one via +\f[C]ucl_iterate_object\f[] and the proposed interface of safe +iterators. .SS ucl_iterate_object .IP .nf \f[C] const\ ucl_object_t*\ ucl_iterate_object\ (const\ ucl_object_t\ *obj,\ \ \ \ \ ucl_object_iter_t\ *iter,\ bool\ expand_values); \f[] .fi .PP This function accepts opaque iterator pointer \f[C]iter\f[]. In the first call this iterator \f[I]must\f[] be initialized to \f[C]NULL\f[]. Iterator is changed by this function call. \f[C]ucl_iterate_object\f[] returns the next UCL object in the compound object \f[C]obj\f[] or \f[C]NULL\f[] if all objects have been iterated. The reference count of the object returned is not increased, so a caller should not unref the object or modify its content (e.g. by inserting to another compound object). The object \f[C]obj\f[] should not be changed during the iteration process as well. \f[C]expand_values\f[] flag speicifies whether \f[C]ucl_iterate_object\f[] should expand keys with multiple values. The general rule is that if you need to iterate through the \f[I]object\f[] or \f[I]explicit array\f[], then you always need to set this flag to \f[C]true\f[]. However, if you get some key in the object and want to extract all its values then you should set \f[C]expand_values\f[] to \f[C]false\f[]. Mixing of iteration types is not permitted since the iterator is set according to the iteration type and cannot be reused. Here is an example of iteration over the objects using libucl API (assuming that \f[C]top\f[] is \f[C]UCL_OBJECT\f[] in this example): .IP .nf \f[C] ucl_object_iter_t\ it\ =\ NULL,\ it_obj\ =\ NULL; const\ ucl_object_t\ *cur,\ *tmp; /*\ Iterate\ over\ the\ object\ */ while\ ((obj\ =\ ucl_iterate_object\ (top,\ &it,\ true)))\ { \ \ \ \ printf\ ("key:\ \\"%s\\"\\n",\ ucl_object_key\ (obj)); \ \ \ \ /*\ Iterate\ over\ the\ values\ of\ a\ key\ */ \ \ \ \ while\ ((cur\ =\ ucl_iterate_object\ (obj,\ &it_obj,\ false)))\ { \ \ \ \ \ \ \ \ printf\ ("value:\ \\"%s\\"\\n",\ \ \ \ \ \ \ \ \ \ \ \ \ ucl_object_tostring_forced\ (cur)); \ \ \ \ } } +\f[] +.fi +.SS Safe iterators API +.PP +Safe iterators are defined to clarify iterating over UCL objects and +simplify flattening of UCL objects in non\-trivial cases. +For example, if there is an implicit array that contains another array +and a boolean value it is extremely unclear how to iterate over such an +object. +Safe iterators are desinged to define two sorts of iteration: +.IP "1." 3 +Iteration over complex objects with expanding all values +.IP "2." 3 +Iteration over complex objects without expanding of values +.PP +The following example demonstrates the difference between these two +types of iteration: +.IP +.nf +\f[C] +key\ =\ 1; +key\ =\ [2,\ 3,\ 4]; + +Iteration\ with\ expansion: + +1,\ 2,\ 3,\ 4 + +Iteration\ without\ expansion: + +1,\ [2,\ 3,\ 4] +\f[] +.fi +.PP +UCL defines the following functions to manage safe iterators: +.IP \[bu] 2 +\f[C]ucl_object_iterate_new\f[] \- creates new safe iterator +.IP \[bu] 2 +\f[C]ucl_object_iterate_reset\f[] \- resets iterator to a new object +.IP \[bu] 2 +\f[C]ucl_object_iterate_safe\f[] \- safely iterate the object inside +iterator +.IP \[bu] 2 +\f[C]ucl_object_iterate_free\f[] \- free memory associated with the safe +iterator +.PP +Please note that unlike unsafe iterators, safe iterators \f[I]must\f[] +be explicitly initialized and freed. +An assert is likely generated if you use uninitialized or \f[C]NULL\f[] +iterator in all safe iterators functions. +.IP +.nf +\f[C] +ucl_object_iter_t\ it; +const\ ucl_object_t\ *cur; + +it\ =\ ucl_object_iterate_new\ (obj); + +while\ ((cur\ =\ ucl_object_iterate_safe\ (it,\ true))\ !=\ NULL)\ { +\ \ \ \ /*\ Do\ something\ */ +} + +/*\ Switch\ to\ another\ object\ */ +it\ =\ ucl_object_iterate_reset\ (it,\ another_obj); + +while\ ((cur\ =\ ucl_object_iterate_safe\ (it,\ true))\ !=\ NULL)\ { +\ \ \ \ /*\ Do\ something\ else\ */ +} + +ucl_object_iterate_free\ (it); \f[] .fi .SH VALIDATION FUNCTIONS .PP Currently, there is only one validation function called \f[C]ucl_object_validate\f[]. It performs validation of object using the specified schema. This function is defined as following: .SS ucl_object_validate .IP .nf \f[C] bool\ ucl_object_validate\ (const\ ucl_object_t\ *schema, \ \ \ \ const\ ucl_object_t\ *obj,\ struct\ ucl_schema_error\ *err); \f[] .fi .PP This function uses ucl object \f[C]schema\f[], that must be valid in terms of \f[C]json\-schema\f[] draft v4, to validate input object \f[C]obj\f[]. If this function returns \f[C]true\f[] then validation procedure has been succeed. Otherwise, \f[C]false\f[] is returned and \f[C]err\f[] is set to a specific value. If a caller sets \f[C]err\f[] to NULL then this function does not set any error just returning \f[C]false\f[]. Error is the structure defined as following: .IP .nf \f[C] struct\ ucl_schema_error\ { \ \ \ \ enum\ ucl_schema_error_code\ code;\ \ \ \ /*\ error\ code\ */ \ \ \ \ char\ msg[128];\ \ \ \ \ \ \ \ \ \ \ \ \ \ /*\ error\ message\ */ \ \ \ \ ucl_object_t\ *obj;\ \ \ \ \ \ \ \ \ \ /*\ object\ where\ error\ occurred\ */ }; \f[] .fi .PP Caller may use \f[C]code\f[] field to get a numeric error code: .IP .nf \f[C] enum\ ucl_schema_error_code\ { \ \ \ \ UCL_SCHEMA_OK\ =\ 0,\ \ \ \ \ \ \ \ \ \ /*\ no\ error\ */ \ \ \ \ UCL_SCHEMA_TYPE_MISMATCH,\ \ \ /*\ type\ of\ object\ is\ incorrect\ */ \ \ \ \ UCL_SCHEMA_INVALID_SCHEMA,\ \ /*\ schema\ is\ invalid\ */ \ \ \ \ UCL_SCHEMA_MISSING_PROPERTY,/*\ missing\ properties\ */ \ \ \ \ UCL_SCHEMA_CONSTRAINT,\ \ \ \ \ \ /*\ constraint\ found\ */ \ \ \ \ UCL_SCHEMA_MISSING_DEPENDENCY,\ /*\ missing\ dependency\ */ \ \ \ \ UCL_SCHEMA_UNKNOWN\ \ \ \ \ \ \ \ \ \ /*\ generic\ error\ */ }; \f[] .fi .PP \f[C]msg\f[] is a string description of an error and \f[C]obj\f[] is an object where error has occurred. Error object is not allocated by libucl, so there is no need to free it after validation (a static object should thus be used). .SH AUTHORS Vsevolod Stakhov . Index: vendor/libucl/dist/doc/pandoc.template =================================================================== --- vendor/libucl/dist/doc/pandoc.template (revision 279545) +++ vendor/libucl/dist/doc/pandoc.template (revision 279546) @@ -1,12 +1,12 @@ % LIBUCL(3) Libucl manual % Vsevolod Stakhov -% July 26, 2014 +% %%date%% # Name **ucl_parser_new**, **ucl_parser_register_macro**, **ucl_parser_register_variable**, **ucl_parser_add_chunk**, **ucl_parser_add_string**, **ucl_parser_add_file**, **ucl_parser_get_object**, **ucl_parser_get_error**, **ucl_parser_free**, **ucl_pubkey_add**, **ucl_parser_set_filevars** - universal configuration library parser and utility functions # Library UCL library (libucl, -lucl) Index: vendor/libucl/dist/include/ucl.h =================================================================== --- vendor/libucl/dist/include/ucl.h (revision 279545) +++ vendor/libucl/dist/include/ucl.h (revision 279546) @@ -1,1106 +1,1144 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef UCL_H_ #define UCL_H_ #include #include #include #include #include #include #include #ifdef _WIN32 # define UCL_EXTERN __declspec(dllexport) #else # define UCL_EXTERN #endif /** * @mainpage * This is a reference manual for UCL API. You may find the description of UCL format by following this * [github repository](https://github.com/vstakhov/libucl). * * This manual has several main sections: * - @ref structures * - @ref utils * - @ref parser * - @ref emitter */ /** * @file ucl.h * @brief UCL parsing and emitting functions * * UCL is universal configuration language, which is a form of * JSON with less strict rules that make it more comfortable for * using as a configuration language */ #ifdef __cplusplus extern "C" { #endif /* * Memory allocation utilities * UCL_ALLOC(size) - allocate memory for UCL * UCL_FREE(size, ptr) - free memory of specified size at ptr * Default: malloc and free */ #ifndef UCL_ALLOC #define UCL_ALLOC(size) malloc(size) #endif #ifndef UCL_FREE #define UCL_FREE(size, ptr) free(ptr) #endif #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) #define UCL_WARN_UNUSED_RESULT \ __attribute__((warn_unused_result)) #else #define UCL_WARN_UNUSED_RESULT #endif #ifdef __GNUC__ #define UCL_DEPRECATED(func) func __attribute__ ((deprecated)) #elif defined(_MSC_VER) #define UCL_DEPRECATED(func) __declspec(deprecated) func #else #define UCL_DEPRECATED(func) func #endif /** * @defgroup structures Structures and types * UCL defines several enumeration types used for error reporting or specifying flags and attributes. * * @{ */ /** * The common error codes returned by ucl parser */ typedef enum ucl_error { UCL_EOK = 0, /**< No error */ UCL_ESYNTAX, /**< Syntax error occurred during parsing */ UCL_EIO, /**< IO error occurred during parsing */ UCL_ESTATE, /**< Invalid state machine state */ UCL_ENESTED, /**< Input has too many recursion levels */ UCL_EMACRO, /**< Error processing a macro */ UCL_EINTERNAL, /**< Internal unclassified error */ UCL_ESSL /**< SSL error */ } ucl_error_t; /** * #ucl_object_t may have one of specified types, some types are compatible with each other and some are not. * For example, you can always convert #UCL_TIME to #UCL_FLOAT. Also you can convert #UCL_FLOAT to #UCL_INTEGER * by loosing floating point. Every object may be converted to a string by #ucl_object_tostring_forced() function. * */ typedef enum ucl_type { UCL_OBJECT = 0, /**< UCL object - key/value pairs */ UCL_ARRAY, /**< UCL array */ UCL_INT, /**< Integer number */ UCL_FLOAT, /**< Floating point number */ UCL_STRING, /**< Null terminated string */ UCL_BOOLEAN, /**< Boolean value */ UCL_TIME, /**< Time value (floating point number of seconds) */ UCL_USERDATA, /**< Opaque userdata pointer (may be used in macros) */ UCL_NULL /**< Null value */ } ucl_type_t; /** * You can use one of these types to serialise #ucl_object_t by using ucl_object_emit(). */ typedef enum ucl_emitter { UCL_EMIT_JSON = 0, /**< Emit fine formatted JSON */ UCL_EMIT_JSON_COMPACT, /**< Emit compacted JSON */ UCL_EMIT_CONFIG, /**< Emit human readable config format */ UCL_EMIT_YAML /**< Emit embedded YAML format */ } ucl_emitter_t; /** * These flags defines parser behaviour. If you specify #UCL_PARSER_ZEROCOPY you must ensure * that the input memory is not freed if an object is in use. Moreover, if you want to use * zero-terminated keys and string values then you should not use zero-copy mode, as in this case * UCL still has to perform copying implicitly. */ typedef enum ucl_parser_flags { UCL_PARSER_KEY_LOWERCASE = 0x1, /**< Convert all keys to lower case */ UCL_PARSER_ZEROCOPY = 0x2, /**< Parse input in zero-copy mode if possible */ UCL_PARSER_NO_TIME = 0x4, /**< Do not parse time and treat time values as strings */ UCL_PARSER_NO_IMPLICIT_ARRAYS = 0x8 /** Create explicit arrays instead of implicit ones */ } ucl_parser_flags_t; /** * String conversion flags, that are used in #ucl_object_fromstring_common function. */ typedef enum ucl_string_flags { UCL_STRING_ESCAPE = 0x1, /**< Perform JSON escape */ UCL_STRING_TRIM = 0x2, /**< Trim leading and trailing whitespaces */ UCL_STRING_PARSE_BOOLEAN = 0x4, /**< Parse passed string and detect boolean */ UCL_STRING_PARSE_INT = 0x8, /**< Parse passed string and detect integer number */ UCL_STRING_PARSE_DOUBLE = 0x10, /**< Parse passed string and detect integer or float number */ UCL_STRING_PARSE_TIME = 0x20, /**< Parse time strings */ UCL_STRING_PARSE_NUMBER = UCL_STRING_PARSE_INT|UCL_STRING_PARSE_DOUBLE|UCL_STRING_PARSE_TIME, /**< Parse passed string and detect number */ UCL_STRING_PARSE = UCL_STRING_PARSE_BOOLEAN|UCL_STRING_PARSE_NUMBER, /**< Parse passed string (and detect booleans and numbers) */ UCL_STRING_PARSE_BYTES = 0x40 /**< Treat numbers as bytes */ } ucl_string_flags_t; /** * Basic flags for an object */ typedef enum ucl_object_flags { UCL_OBJECT_ALLOCATED_KEY = 0x1, /**< An object has key allocated internally */ UCL_OBJECT_ALLOCATED_VALUE = 0x2, /**< An object has a string value allocated internally */ UCL_OBJECT_NEED_KEY_ESCAPE = 0x4, /**< The key of an object need to be escaped on output */ UCL_OBJECT_EPHEMERAL = 0x8, /**< Temporary object that does not need to be freed really */ UCL_OBJECT_MULTILINE = 0x10, /**< String should be displayed as multiline string */ UCL_OBJECT_MULTIVALUE = 0x20 /**< Object is a key with multiple values */ } ucl_object_flags_t; /** * UCL object structure. Please mention that the most of fields should not be touched by * UCL users. In future, this structure may be converted to private one. */ typedef struct ucl_object_s { /** * Variant value type */ union { int64_t iv; /**< Int value of an object */ const char *sv; /**< String value of an object */ double dv; /**< Double value of an object */ - struct ucl_object_s *av; /**< Array */ + void *av; /**< Array */ void *ov; /**< Object */ void* ud; /**< Opaque user data */ } value; const char *key; /**< Key of an object */ struct ucl_object_s *next; /**< Array handle */ struct ucl_object_s *prev; /**< Array handle */ uint32_t keylen; /**< Lenght of a key */ uint32_t len; /**< Size of an object */ uint32_t ref; /**< Reference count */ uint16_t flags; /**< Object flags */ uint16_t type; /**< Real type */ unsigned char* trash_stack[2]; /**< Pointer to allocated chunks */ } ucl_object_t; /** * Destructor type for userdata objects * @param ud user specified data pointer */ typedef void (*ucl_userdata_dtor)(void *ud); typedef const char* (*ucl_userdata_emitter)(void *ud); /** @} */ /** * @defgroup utils Utility functions * A number of utility functions simplify handling of UCL objects * * @{ */ /** * Copy and return a key of an object, returned key is zero-terminated * @param obj CL object * @return zero terminated key */ UCL_EXTERN char* ucl_copy_key_trash (const ucl_object_t *obj); /** * Copy and return a string value of an object, returned key is zero-terminated * @param obj CL object * @return zero terminated string representation of object value */ UCL_EXTERN char* ucl_copy_value_trash (const ucl_object_t *obj); /** * Creates a new object * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_new (void) UCL_WARN_UNUSED_RESULT; /** * Create new object with type specified * @param type type of a new object * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_typed_new (ucl_type_t type) UCL_WARN_UNUSED_RESULT; /** * Create new object with type and priority specified * @param type type of a new object * @param priority priority of an object * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_new_full (ucl_type_t type, unsigned priority) UCL_WARN_UNUSED_RESULT; /** * Create new object with userdata dtor * @param dtor destructor function * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_new_userdata (ucl_userdata_dtor dtor, ucl_userdata_emitter emitter) UCL_WARN_UNUSED_RESULT; /** * Perform deep copy of an object copying everything * @param other object to copy * @return new object with refcount equal to 1 */ UCL_EXTERN ucl_object_t * ucl_object_copy (const ucl_object_t *other) UCL_WARN_UNUSED_RESULT; /** * Return the type of an object * @return the object type */ UCL_EXTERN ucl_type_t ucl_object_type (const ucl_object_t *obj); /** * Convert any string to an ucl object making the specified transformations * @param str fixed size or NULL terminated string * @param len length (if len is zero, than str is treated as NULL terminated) * @param flags conversion flags * @return new object */ UCL_EXTERN ucl_object_t * ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags flags) UCL_WARN_UNUSED_RESULT; /** * Create a UCL object from the specified string * @param str NULL terminated string, will be json escaped * @return new object */ UCL_EXTERN ucl_object_t *ucl_object_fromstring (const char *str) UCL_WARN_UNUSED_RESULT; /** * Create a UCL object from the specified string * @param str fixed size string, will be json escaped * @param len length of a string * @return new object */ UCL_EXTERN ucl_object_t *ucl_object_fromlstring (const char *str, size_t len) UCL_WARN_UNUSED_RESULT; /** * Create an object from an integer number * @param iv number * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_fromint (int64_t iv) UCL_WARN_UNUSED_RESULT; /** * Create an object from a float number * @param dv number * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_fromdouble (double dv) UCL_WARN_UNUSED_RESULT; /** * Create an object from a boolean * @param bv bool value * @return new object */ UCL_EXTERN ucl_object_t* ucl_object_frombool (bool bv) UCL_WARN_UNUSED_RESULT; /** * Insert a object 'elt' to the hash 'top' and associate it with key 'key' * @param top destination object (must be of type UCL_OBJECT) * @param elt element to insert (must NOT be NULL) * @param key key to associate with this object (either const or preallocated) * @param keylen length of the key (or 0 for NULL terminated keys) * @param copy_key make an internal copy of key * @return true if key has been inserted */ UCL_EXTERN bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key); /** * Replace a object 'elt' to the hash 'top' and associate it with key 'key', old object will be unrefed, * if no object has been found this function works like ucl_object_insert_key() * @param top destination object (must be of type UCL_OBJECT) * @param elt element to insert (must NOT be NULL) * @param key key to associate with this object (either const or preallocated) * @param keylen length of the key (or 0 for NULL terminated keys) * @param copy_key make an internal copy of key * @return true if key has been inserted */ UCL_EXTERN bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key); /** * Merge the keys from one object to another object. Overwrite on conflict * @param top destination object (must be of type UCL_OBJECT) * @param elt element to insert (must be of type UCL_OBJECT) * @param copy copy rather than reference the elements * @return true if all keys have been merged */ UCL_EXTERN bool ucl_object_merge (ucl_object_t *top, ucl_object_t *elt, bool copy); /** * Delete a object associated with key 'key', old object will be unrefered, * @param top object * @param key key associated to the object to remove * @param keylen length of the key (or 0 for NULL terminated keys) */ UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top, const char *key, size_t keylen); /** * Delete a object associated with key 'key', old object will be unrefered, * @param top object * @param key key associated to the object to remove */ UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top, const char *key); /** * Removes `key` from `top` object, returning the object that was removed. This * object is not released, caller must unref the returned object when it is no * longer needed. * @param top object * @param key key to remove * @param keylen length of the key (or 0 for NULL terminated keys) * @return removed object or NULL if object has not been found */ UCL_EXTERN ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key, size_t keylen) UCL_WARN_UNUSED_RESULT; /** * Removes `key` from `top` object returning the object that was removed. This * object is not released, caller must unref the returned object when it is no * longer needed. * @param top object * @param key key to remove * @return removed object or NULL if object has not been found */ UCL_EXTERN ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key) UCL_WARN_UNUSED_RESULT; /** * Insert a object 'elt' to the hash 'top' and associate it with key 'key', if * the specified key exist, try to merge its content * @param top destination object (must be of type UCL_OBJECT) * @param elt element to insert (must NOT be NULL) * @param key key to associate with this object (either const or preallocated) * @param keylen length of the key (or 0 for NULL terminated keys) * @param copy_key make an internal copy of key * @return true if key has been inserted */ UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key); /** * Append an element to the end of array object * @param top destination object (must NOT be NULL) * @param elt element to append (must NOT be NULL) * @return true if value has been inserted */ UCL_EXTERN bool ucl_array_append (ucl_object_t *top, ucl_object_t *elt); /** * Append an element to the start of array object * @param top destination object (must NOT be NULL) * @param elt element to append (must NOT be NULL) * @return true if value has been inserted */ UCL_EXTERN bool ucl_array_prepend (ucl_object_t *top, ucl_object_t *elt); /** * Merge all elements of second array into the first array * @param top destination array (must be of type UCL_ARRAY) * @param elt array to copy elements from (must be of type UCL_ARRAY) * @param copy copy elements instead of referencing them * @return true if arrays were merged */ UCL_EXTERN bool ucl_array_merge (ucl_object_t *top, ucl_object_t *elt, bool copy); /** * Removes an element `elt` from the array `top`, returning the object that was * removed. This object is not released, caller must unref the returned object * when it is no longer needed. * @param top array ucl object * @param elt element to remove * @return removed element or NULL if `top` is NULL or not an array */ UCL_EXTERN ucl_object_t* ucl_array_delete (ucl_object_t *top, ucl_object_t *elt); /** * Returns the first element of the array `top` * @param top array ucl object * @return element or NULL if `top` is NULL or not an array */ UCL_EXTERN const ucl_object_t* ucl_array_head (const ucl_object_t *top); /** * Returns the last element of the array `top` * @param top array ucl object * @return element or NULL if `top` is NULL or not an array */ UCL_EXTERN const ucl_object_t* ucl_array_tail (const ucl_object_t *top); /** * Removes the last element from the array `top`, returning the object that was * removed. This object is not released, caller must unref the returned object * when it is no longer needed. * @param top array ucl object * @return removed element or NULL if `top` is NULL or not an array */ UCL_EXTERN ucl_object_t* ucl_array_pop_last (ucl_object_t *top); /** * Removes the first element from the array `top`, returning the object that was * removed. This object is not released, caller must unref the returned object * when it is no longer needed. * @param top array ucl object * @return removed element or NULL if `top` is NULL or not an array */ UCL_EXTERN ucl_object_t* ucl_array_pop_first (ucl_object_t *top); /** * Return object identified by index of the array `top` * @param top object to get a key from (must be of type UCL_ARRAY) * @param index array index to return * @return object at the specified index or NULL if index is not found */ UCL_EXTERN const ucl_object_t* ucl_array_find_index (const ucl_object_t *top, unsigned int index); /** * Replace an element in an array with a different element, returning the object * that was replaced. This object is not released, caller must unref the * returned object when it is no longer needed. * @param top destination object (must be of type UCL_ARRAY) * @param elt element to append (must NOT be NULL) * @param index array index in destination to overwrite with elt * @return object that was replaced or NULL if index is not found */ ucl_object_t * ucl_array_replace_index (ucl_object_t *top, ucl_object_t *elt, unsigned int index); /** * Append a element to another element forming an implicit array * @param head head to append (may be NULL) * @param elt new element * @return the new implicit array */ UCL_EXTERN ucl_object_t * ucl_elt_append (ucl_object_t *head, ucl_object_t *elt); /** * Converts an object to double value * @param obj CL object * @param target target double variable * @return true if conversion was successful */ UCL_EXTERN bool ucl_object_todouble_safe (const ucl_object_t *obj, double *target); /** * Unsafe version of \ref ucl_obj_todouble_safe * @param obj CL object * @return double value */ UCL_EXTERN double ucl_object_todouble (const ucl_object_t *obj); /** * Converts an object to integer value * @param obj CL object * @param target target integer variable * @return true if conversion was successful */ UCL_EXTERN bool ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target); /** * Unsafe version of \ref ucl_obj_toint_safe * @param obj CL object * @return int value */ UCL_EXTERN int64_t ucl_object_toint (const ucl_object_t *obj); /** * Converts an object to boolean value * @param obj CL object * @param target target boolean variable * @return true if conversion was successful */ UCL_EXTERN bool ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target); /** * Unsafe version of \ref ucl_obj_toboolean_safe * @param obj CL object * @return boolean value */ UCL_EXTERN bool ucl_object_toboolean (const ucl_object_t *obj); /** * Converts an object to string value * @param obj CL object * @param target target string variable, no need to free value * @return true if conversion was successful */ UCL_EXTERN bool ucl_object_tostring_safe (const ucl_object_t *obj, const char **target); /** * Unsafe version of \ref ucl_obj_tostring_safe * @param obj CL object * @return string value */ UCL_EXTERN const char* ucl_object_tostring (const ucl_object_t *obj); /** * Convert any object to a string in JSON notation if needed * @param obj CL object * @return string value */ UCL_EXTERN const char* ucl_object_tostring_forced (const ucl_object_t *obj); /** * Return string as char * and len, string may be not zero terminated, more efficient that \ref ucl_obj_tostring as it * allows zero-copy (if #UCL_PARSER_ZEROCOPY has been used during parsing) * @param obj CL object * @param target target string variable, no need to free value * @param tlen target length * @return true if conversion was successful */ UCL_EXTERN bool ucl_object_tolstring_safe (const ucl_object_t *obj, const char **target, size_t *tlen); /** * Unsafe version of \ref ucl_obj_tolstring_safe * @param obj CL object * @return string value */ UCL_EXTERN const char* ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen); /** * Return object identified by a key in the specified object * @param obj object to get a key from (must be of type UCL_OBJECT) * @param key key to search * @return object matching the specified key or NULL if key was not found */ UCL_EXTERN const ucl_object_t* ucl_object_find_key (const ucl_object_t *obj, const char *key); /** * Return object identified by a fixed size key in the specified object * @param obj object to get a key from (must be of type UCL_OBJECT) * @param key key to search * @param klen length of a key * @return object matching the specified key or NULL if key was not found */ UCL_EXTERN const ucl_object_t* ucl_object_find_keyl (const ucl_object_t *obj, const char *key, size_t klen); /** * Return object identified by dot notation string * @param obj object to search in * @param path dot.notation.path to the path to lookup. May use numeric .index on arrays * @return object matched the specified path or NULL if path is not found */ UCL_EXTERN const ucl_object_t *ucl_lookup_path (const ucl_object_t *obj, const char *path); /** * Returns a key of an object as a NULL terminated string * @param obj CL object * @return key or NULL if there is no key */ UCL_EXTERN const char* ucl_object_key (const ucl_object_t *obj); /** * Returns a key of an object as a fixed size string (may be more efficient) * @param obj CL object * @param len target key length * @return key pointer */ UCL_EXTERN const char* ucl_object_keyl (const ucl_object_t *obj, size_t *len); /** * Increase reference count for an object * @param obj object to ref * @return the referenced object */ UCL_EXTERN ucl_object_t* ucl_object_ref (const ucl_object_t *obj); /** * Free ucl object * @param obj ucl object to free */ UCL_DEPRECATED(UCL_EXTERN void ucl_object_free (ucl_object_t *obj)); /** * Decrease reference count for an object * @param obj object to unref */ UCL_EXTERN void ucl_object_unref (ucl_object_t *obj); /** * Compare objects `o1` and `o2` * @param o1 the first object * @param o2 the second object * @return values >0, 0 and <0 if `o1` is more than, equal and less than `o2`. * The order of comparison: * 1) Type of objects * 2) Size of objects * 3) Content of objects */ UCL_EXTERN int ucl_object_compare (const ucl_object_t *o1, const ucl_object_t *o2); /** * Sort UCL array using `cmp` compare function * @param ar * @param cmp */ UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar, int (*cmp)(const ucl_object_t *o1, const ucl_object_t *o2)); /** * Get the priority for specific UCL object * @param obj any ucl object * @return priority of an object */ UCL_EXTERN unsigned int ucl_object_get_priority (const ucl_object_t *obj); /** * Set explicit priority of an object. * @param obj any ucl object * @param priority new priroity value (only 4 least significant bits are considred) */ UCL_EXTERN void ucl_object_set_priority (ucl_object_t *obj, unsigned int priority); /** * Opaque iterator object */ typedef void* ucl_object_iter_t; /** * Get next key from an object * @param obj object to iterate * @param iter opaque iterator, must be set to NULL on the first call: * ucl_object_iter_t it = NULL; * while ((cur = ucl_iterate_object (obj, &it)) != NULL) ... * @return the next object or NULL */ UCL_EXTERN const ucl_object_t* ucl_iterate_object (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expand_values); + +/** + * Create new safe iterator for the specified object + * @param obj object to iterate + * @return new iterator object that should be used with safe iterators API only + */ +UCL_EXTERN ucl_object_iter_t ucl_object_iterate_new (const ucl_object_t *obj) + UCL_WARN_UNUSED_RESULT; +/** + * Reset initialized iterator to a new object + * @param obj new object to iterate + * @return modified iterator object + */ +UCL_EXTERN ucl_object_iter_t ucl_object_iterate_reset (ucl_object_iter_t it, + const ucl_object_t *obj); + +/** + * Get the next object from the `obj`. This fucntion iterates over arrays, objects + * and implicit arrays + * @param iter safe iterator + * @return the next object in sequence + */ +UCL_EXTERN const ucl_object_t* ucl_object_iterate_safe (ucl_object_iter_t iter, + bool expand_values); + +/** + * Free memory associated with the safe iterator + * @param it safe iterator object + */ +UCL_EXTERN void ucl_object_iterate_free (ucl_object_iter_t it); + /** @} */ /** * @defgroup parser Parsing functions * These functions are used to parse UCL objects * * @{ */ /** * Macro handler for a parser * @param data the content of macro * @param len the length of content * @param arguments arguments object * @param ud opaque user data * @param err error pointer * @return true if macro has been parsed */ typedef bool (*ucl_macro_handler) (const unsigned char *data, size_t len, const ucl_object_t *arguments, void* ud); /* Opaque parser */ struct ucl_parser; /** * Creates new parser object * @param pool pool to allocate memory from * @return new parser object */ UCL_EXTERN struct ucl_parser* ucl_parser_new (int flags); /** * Register new handler for a macro * @param parser parser object * @param macro macro name (without leading dot) * @param handler handler (it is called immediately after macro is parsed) * @param ud opaque user data for a handler */ UCL_EXTERN void ucl_parser_register_macro (struct ucl_parser *parser, const char *macro, ucl_macro_handler handler, void* ud); /** * Handler to detect unregistered variables * @param data variable data * @param len length of variable * @param replace (out) replace value for variable * @param replace_len (out) replace length for variable * @param need_free (out) UCL will free `dest` after usage * @param ud opaque userdata * @return true if variable */ typedef bool (*ucl_variable_handler) (const unsigned char *data, size_t len, unsigned char **replace, size_t *replace_len, bool *need_free, void* ud); /** * Register new parser variable * @param parser parser object * @param var variable name * @param value variable value */ UCL_EXTERN void ucl_parser_register_variable (struct ucl_parser *parser, const char *var, const char *value); /** * Set handler for unknown variables * @param parser parser structure * @param handler desired handler * @param ud opaque data for the handler */ UCL_EXTERN void ucl_parser_set_variables_handler (struct ucl_parser *parser, ucl_variable_handler handler, void *ud); /** * Load new chunk to a parser * @param parser parser structure * @param data the pointer to the beginning of a chunk * @param len the length of a chunk * @return true if chunk has been added and false in case of error */ UCL_EXTERN bool ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data, size_t len); /** * Load new chunk to a parser with the specified priority * @param parser parser structure * @param data the pointer to the beginning of a chunk * @param len the length of a chunk * @param priority the desired priority of a chunk (only 4 least significant bits * are considered for this parameter) * @return true if chunk has been added and false in case of error */ UCL_EXTERN bool ucl_parser_add_chunk_priority (struct ucl_parser *parser, const unsigned char *data, size_t len, unsigned priority); /** * Load ucl object from a string * @param parser parser structure * @param data the pointer to the string * @param len the length of the string, if `len` is 0 then `data` must be zero-terminated string * @return true if string has been added and false in case of error */ UCL_EXTERN bool ucl_parser_add_string (struct ucl_parser *parser, const char *data,size_t len); /** * Load and add data from a file * @param parser parser structure * @param filename the name of file * @param err if *err is NULL it is set to parser error * @return true if chunk has been added and false in case of error */ UCL_EXTERN bool ucl_parser_add_file (struct ucl_parser *parser, const char *filename); /** * Load and add data from a file descriptor * @param parser parser structure * @param filename the name of file * @param err if *err is NULL it is set to parser error * @return true if chunk has been added and false in case of error */ UCL_EXTERN bool ucl_parser_add_fd (struct ucl_parser *parser, int fd); /** * Get a top object for a parser (refcount is increased) * @param parser parser structure * @param err if *err is NULL it is set to parser error * @return top parser object or NULL */ UCL_EXTERN ucl_object_t* ucl_parser_get_object (struct ucl_parser *parser); /** * Get the error string if failing * @param parser parser object */ UCL_EXTERN const char *ucl_parser_get_error(struct ucl_parser *parser); + +/** + * Clear the error in the parser + * @param parser parser object + */ +UCL_EXTERN void ucl_parser_clear_error(struct ucl_parser *parser); + /** * Free ucl parser object * @param parser parser object */ UCL_EXTERN void ucl_parser_free (struct ucl_parser *parser); /** * Add new public key to parser for signatures check * @param parser parser object * @param key PEM representation of a key * @param len length of the key * @param err if *err is NULL it is set to parser error * @return true if a key has been successfully added */ UCL_EXTERN bool ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len); /** * Set FILENAME and CURDIR variables in parser * @param parser parser object * @param filename filename to set or NULL to set FILENAME to "undef" and CURDIR to getcwd() * @param need_expand perform realpath() if this variable is true and filename is not NULL * @return true if variables has been set */ UCL_EXTERN bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand); /** @} */ /** * @defgroup emitter Emitting functions * These functions are used to serialise UCL objects to some string representation. * * @{ */ struct ucl_emitter_context; /** * Structure using for emitter callbacks */ struct ucl_emitter_functions { /** Append a single character */ int (*ucl_emitter_append_character) (unsigned char c, size_t nchars, void *ud); /** Append a string of a specified length */ int (*ucl_emitter_append_len) (unsigned const char *str, size_t len, void *ud); /** Append a 64 bit integer */ int (*ucl_emitter_append_int) (int64_t elt, void *ud); /** Append floating point element */ int (*ucl_emitter_append_double) (double elt, void *ud); /** Free userdata */ void (*ucl_emitter_free_func)(void *ud); /** Opaque userdata pointer */ void *ud; }; struct ucl_emitter_operations { /** Write a primitive element */ void (*ucl_emitter_write_elt) (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool first, bool print_key); /** Start ucl object */ void (*ucl_emitter_start_object) (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool print_key); /** End ucl object */ void (*ucl_emitter_end_object) (struct ucl_emitter_context *ctx, const ucl_object_t *obj); /** Start ucl array */ void (*ucl_emitter_start_array) (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool print_key); void (*ucl_emitter_end_array) (struct ucl_emitter_context *ctx, const ucl_object_t *obj); }; /** * Structure that defines emitter functions */ struct ucl_emitter_context { /** Name of emitter (e.g. json, compact_json) */ const char *name; /** Unique id (e.g. UCL_EMIT_JSON for standard emitters */ int id; /** A set of output functions */ const struct ucl_emitter_functions *func; /** A set of output operations */ const struct ucl_emitter_operations *ops; /** Current amount of indent tabs */ unsigned int indent; /** Top level object */ const ucl_object_t *top; /** The rest of context */ unsigned char data[1]; }; /** * Emit object to a string * @param obj object * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is * #UCL_EMIT_CONFIG then emit config like object * @return dump of an object (must be freed after using) or NULL in case of error */ UCL_EXTERN unsigned char *ucl_object_emit (const ucl_object_t *obj, enum ucl_emitter emit_type); /** * Emit object to a string * @param obj object * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is * #UCL_EMIT_CONFIG then emit config like object * @param emitter a set of emitter functions * @return dump of an object (must be freed after using) or NULL in case of error */ UCL_EXTERN bool ucl_object_emit_full (const ucl_object_t *obj, enum ucl_emitter emit_type, struct ucl_emitter_functions *emitter); /** * Start streamlined UCL object emitter * @param obj top UCL object * @param emit_type emit type * @param emitter a set of emitter functions * @return new streamlined context that should be freed by * `ucl_object_emit_streamline_finish` */ UCL_EXTERN struct ucl_emitter_context* ucl_object_emit_streamline_new ( const ucl_object_t *obj, enum ucl_emitter emit_type, struct ucl_emitter_functions *emitter); /** * Start object or array container for the streamlined output * @param ctx streamlined context * @param obj container object */ UCL_EXTERN void ucl_object_emit_streamline_start_container ( struct ucl_emitter_context *ctx, const ucl_object_t *obj); /** * Add a complete UCL object to streamlined output * @param ctx streamlined context * @param obj object to output */ UCL_EXTERN void ucl_object_emit_streamline_add_object ( struct ucl_emitter_context *ctx, const ucl_object_t *obj); /** * End previously added container * @param ctx streamlined context */ UCL_EXTERN void ucl_object_emit_streamline_end_container ( struct ucl_emitter_context *ctx); /** * Terminate streamlined container finishing all containers in it * @param ctx streamlined context */ UCL_EXTERN void ucl_object_emit_streamline_finish ( struct ucl_emitter_context *ctx); /** * Returns functions to emit object to memory * @param pmem target pointer (should be freed by caller) * @return emitter functions structure */ UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_memory_funcs ( void **pmem); /** * Returns functions to emit object to FILE * * @param fp FILE * object * @return emitter functions structure */ UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_file_funcs ( FILE *fp); /** * Returns functions to emit object to a file descriptor * @param fd file descriptor * @return emitter functions structure */ UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_fd_funcs ( int fd); /** * Free emitter functions * @param f pointer to functions */ UCL_EXTERN void ucl_object_emit_funcs_free (struct ucl_emitter_functions *f); /** @} */ /** * @defgroup schema Schema functions * These functions are used to validate UCL objects using json schema format * * @{ */ /** * Used to define UCL schema error */ enum ucl_schema_error_code { UCL_SCHEMA_OK = 0, /**< no error */ UCL_SCHEMA_TYPE_MISMATCH, /**< type of object is incorrect */ UCL_SCHEMA_INVALID_SCHEMA, /**< schema is invalid */ UCL_SCHEMA_MISSING_PROPERTY,/**< one or more missing properties */ UCL_SCHEMA_CONSTRAINT, /**< constraint found */ UCL_SCHEMA_MISSING_DEPENDENCY, /**< missing dependency */ UCL_SCHEMA_UNKNOWN /**< generic error */ }; /** * Generic ucl schema error */ struct ucl_schema_error { enum ucl_schema_error_code code; /**< error code */ char msg[128]; /**< error message */ const ucl_object_t *obj; /**< object where error occured */ }; /** * Validate object `obj` using schema object `schema`. * @param schema schema object * @param obj object to validate * @param err error pointer, if this parameter is not NULL and error has been * occured, then `err` is filled with the exact error definition. * @return true if `obj` is valid using `schema` */ UCL_EXTERN bool ucl_object_validate (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err); /** @} */ #ifdef __cplusplus } #endif /* * XXX: Poorly named API functions, need to replace them with the appropriate * named function. All API functions *must* use naming ucl_object_*. Usage of * ucl_obj* should be avoided. */ #define ucl_obj_todouble_safe ucl_object_todouble_safe #define ucl_obj_todouble ucl_object_todouble #define ucl_obj_tostring ucl_object_tostring #define ucl_obj_tostring_safe ucl_object_tostring_safe #define ucl_obj_tolstring ucl_object_tolstring #define ucl_obj_tolstring_safe ucl_object_tolstring_safe #define ucl_obj_toint ucl_object_toint #define ucl_obj_toint_safe ucl_object_toint_safe #define ucl_obj_toboolean ucl_object_toboolean #define ucl_obj_toboolean_safe ucl_object_toboolean_safe #define ucl_obj_get_key ucl_object_find_key #define ucl_obj_get_keyl ucl_object_find_keyl #define ucl_obj_unref ucl_object_unref #define ucl_obj_ref ucl_object_ref #define ucl_obj_free ucl_object_free #endif /* UCL_H_ */ Index: vendor/libucl/dist/klib/khash.h =================================================================== --- vendor/libucl/dist/klib/khash.h (nonexistent) +++ vendor/libucl/dist/klib/khash.h (revision 279546) @@ -0,0 +1,627 @@ +/* The MIT License + + Copyright (c) 2008, 2009, 2011 by Attractive Chaos + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +/* + An example: + +#include "khash.h" +KHASH_MAP_INIT_INT(32, char) +int main() { + int ret, is_missing; + khiter_t k; + khash_t(32) *h = kh_init(32); + k = kh_put(32, h, 5, &ret); + kh_value(h, k) = 10; + k = kh_get(32, h, 10); + is_missing = (k == kh_end(h)); + k = kh_get(32, h, 5); + kh_del(32, h, k); + for (k = kh_begin(h); k != kh_end(h); ++k) + if (kh_exist(h, k)) kh_value(h, k) = 1; + kh_destroy(32, h); + return 0; +} +*/ + +/* + 2013-05-02 (0.2.8): + + * Use quadratic probing. When the capacity is power of 2, stepping function + i*(i+1)/2 guarantees to traverse each bucket. It is better than double + hashing on cache performance and is more robust than linear probing. + + In theory, double hashing should be more robust than quadratic probing. + However, my implementation is probably not for large hash tables, because + the second hash function is closely tied to the first hash function, + which reduce the effectiveness of double hashing. + + Reference: http://research.cs.vt.edu/AVresearch/hashing/quadratic.php + + 2011-12-29 (0.2.7): + + * Minor code clean up; no actual effect. + + 2011-09-16 (0.2.6): + + * The capacity is a power of 2. This seems to dramatically improve the + speed for simple keys. Thank Zilong Tan for the suggestion. Reference: + + - http://code.google.com/p/ulib/ + - http://nothings.org/computer/judy/ + + * Allow to optionally use linear probing which usually has better + performance for random input. Double hashing is still the default as it + is more robust to certain non-random input. + + * Added Wang's integer hash function (not used by default). This hash + function is more robust to certain non-random input. + + 2011-02-14 (0.2.5): + + * Allow to declare global functions. + + 2009-09-26 (0.2.4): + + * Improve portability + + 2008-09-19 (0.2.3): + + * Corrected the example + * Improved interfaces + + 2008-09-11 (0.2.2): + + * Improved speed a little in kh_put() + + 2008-09-10 (0.2.1): + + * Added kh_clear() + * Fixed a compiling error + + 2008-09-02 (0.2.0): + + * Changed to token concatenation which increases flexibility. + + 2008-08-31 (0.1.2): + + * Fixed a bug in kh_get(), which has not been tested previously. + + 2008-08-31 (0.1.1): + + * Added destructor +*/ + + +#ifndef __AC_KHASH_H +#define __AC_KHASH_H + +/*! + @header + + Generic hash table library. + */ + +#define AC_VERSION_KHASH_H "0.2.8" + +#include +#include +#include + +/* compiler specific configuration */ + +#if UINT_MAX == 0xffffffffu +typedef unsigned int khint32_t; +#elif ULONG_MAX == 0xffffffffu +typedef unsigned long khint32_t; +#endif + +#if ULONG_MAX == ULLONG_MAX +typedef unsigned long khint64_t; +#else +typedef unsigned long long khint64_t; +#endif + +#ifndef kh_inline +#ifdef _MSC_VER +#define kh_inline __inline +#else +#define kh_inline inline +#endif +#endif /* kh_inline */ + +#ifndef kh_unused +# ifdef __GNUC__ +# define kh_unused(x) __attribute__((__unused__)) x +# else +# define kh_unused(x) x +# endif +#endif + +typedef khint32_t khint_t; +typedef khint_t khiter_t; + +#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2) +#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1) +#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3) +#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1))) +#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1))) +#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1))) +#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1)) + +#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4) + +#ifndef kroundup32 +#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) +#endif + +#ifndef kcalloc +#define kcalloc(N,Z) calloc(N,Z) +#endif +#ifndef kmalloc +#define kmalloc(Z) malloc(Z) +#endif +#ifndef krealloc +#define krealloc(P,Z) realloc(P,Z) +#endif +#ifndef kfree +#define kfree(P) free(P) +#endif + +static const double __ac_HASH_UPPER = 0.77; + +#define __KHASH_TYPE(name, khkey_t, khval_t) \ + typedef struct kh_##name##_s { \ + khint_t n_buckets, size, n_occupied, upper_bound; \ + khint32_t *flags; \ + khkey_t *keys; \ + khval_t *vals; \ + } kh_##name##_t; + +#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \ + extern kh_##name##_t * kh_init_##name(void); \ + extern void kh_destroy_##name(kh_##name##_t *h); \ + extern void kh_clear_##name(kh_##name##_t *h); \ + extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \ + extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \ + extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \ + extern void kh_del_##name(kh_##name##_t *h, khint_t x); + +#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ + SCOPE kh_##name##_t *kh_init_##name(void) { \ + return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \ + } \ + SCOPE void kh_destroy_##name(kh_##name##_t *h) \ + { \ + if (h) { \ + kfree((void *)h->keys); kfree(h->flags); \ + kfree((void *)h->vals); \ + kfree(h); \ + } \ + } \ + SCOPE void kh_unused(kh_clear_##name)(kh_##name##_t *h) \ + { \ + if (h && h->flags) { \ + memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \ + h->size = h->n_occupied = 0; \ + } \ + } \ + SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ + { \ + if (h->n_buckets) { \ + khint_t k, i, last, mask, step = 0; \ + mask = h->n_buckets - 1; \ + k = __hash_func(key); i = k & mask; \ + last = i; \ + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ + i = (i + (++step)) & mask; \ + if (i == last) return h->n_buckets; \ + } \ + return __ac_iseither(h->flags, i)? h->n_buckets : i; \ + } else return 0; \ + } \ + SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \ + { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \ + khint32_t *new_flags = 0; \ + khint_t j = 1; \ + { \ + kroundup32(new_n_buckets); \ + if (new_n_buckets < 4) new_n_buckets = 4; \ + if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \ + else { /* hash table size to be changed (shrink or expand); rehash */ \ + new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ + if (!new_flags) return -1; \ + memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ + if (h->n_buckets < new_n_buckets) { /* expand */ \ + khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ + if (!new_keys) { kfree(new_flags); return -1; } \ + h->keys = new_keys; \ + if (kh_is_map) { \ + khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ + if (!new_vals) { kfree(new_flags); return -1; } \ + h->vals = new_vals; \ + } \ + } /* otherwise shrink */ \ + } \ + } \ + if (j) { /* rehashing is needed */ \ + for (j = 0; j != h->n_buckets; ++j) { \ + if (__ac_iseither(h->flags, j) == 0) { \ + khkey_t key = h->keys[j]; \ + khval_t val; \ + khint_t new_mask; \ + new_mask = new_n_buckets - 1; \ + if (kh_is_map) val = h->vals[j]; \ + __ac_set_isdel_true(h->flags, j); \ + while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \ + khint_t k, i, step = 0; \ + k = __hash_func(key); \ + i = k & new_mask; \ + while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \ + __ac_set_isempty_false(new_flags, i); \ + if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \ + { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \ + if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \ + __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \ + } else { /* write the element and jump out of the loop */ \ + h->keys[i] = key; \ + if (kh_is_map) h->vals[i] = val; \ + break; \ + } \ + } \ + } \ + } \ + if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \ + h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ + if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ + } \ + kfree(h->flags); /* free the working space */ \ + h->flags = new_flags; \ + h->n_buckets = new_n_buckets; \ + h->n_occupied = h->size; \ + h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \ + } \ + return 0; \ + } \ + SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \ + { \ + khint_t x; \ + if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \ + if (h->n_buckets > (h->size<<1)) { \ + if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \ + *ret = -1; return h->n_buckets; \ + } \ + } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \ + *ret = -1; return h->n_buckets; \ + } \ + } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \ + { \ + khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \ + x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \ + if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \ + else { \ + last = i; \ + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ + if (__ac_isdel(h->flags, i)) site = i; \ + i = (i + (++step)) & mask; \ + if (i == last) { x = site; break; } \ + } \ + if (x == h->n_buckets) { \ + if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \ + else x = i; \ + } \ + } \ + } \ + if (__ac_isempty(h->flags, x)) { /* not present at all */ \ + h->keys[x] = key; \ + __ac_set_isboth_false(h->flags, x); \ + ++h->size; ++h->n_occupied; \ + *ret = 1; \ + } else if (__ac_isdel(h->flags, x)) { /* deleted */ \ + h->keys[x] = key; \ + __ac_set_isboth_false(h->flags, x); \ + ++h->size; \ + *ret = 2; \ + } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \ + return x; \ + } \ + SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \ + { \ + if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \ + __ac_set_isdel_true(h->flags, x); \ + --h->size; \ + } \ + } + +#define KHASH_DECLARE(name, khkey_t, khval_t) \ + __KHASH_TYPE(name, khkey_t, khval_t) \ + __KHASH_PROTOTYPES(name, khkey_t, khval_t) + +#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ + __KHASH_TYPE(name, khkey_t, khval_t) \ + __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) + +#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ + KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) + +/* --- BEGIN OF HASH FUNCTIONS --- */ + +/*! @function + @abstract Integer hash function + @param key The integer [khint32_t] + @return The hash value [khint_t] + */ +#define kh_int_hash_func(key) (khint32_t)(key) +/*! @function + @abstract Integer comparison function + */ +#define kh_int_hash_equal(a, b) ((a) == (b)) +/*! @function + @abstract 64-bit integer hash function + @param key The integer [khint64_t] + @return The hash value [khint_t] + */ +#define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11) +/*! @function + @abstract 64-bit integer comparison function + */ +#define kh_int64_hash_equal(a, b) ((a) == (b)) +/*! @function + @abstract const char* hash function + @param s Pointer to a null terminated string + @return The hash value + */ +static kh_inline khint_t __ac_X31_hash_string(const char *s) +{ + khint_t h = (khint_t)*s; + if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s; + return h; +} +/*! @function + @abstract Another interface to const char* hash function + @param key Pointer to a null terminated string [const char*] + @return The hash value [khint_t] + */ +#define kh_str_hash_func(key) __ac_X31_hash_string(key) +/*! @function + @abstract Const char* comparison function + */ +#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0) + +static kh_inline khint_t __ac_Wang_hash(khint_t key) +{ + key += ~(key << 15); + key ^= (key >> 10); + key += (key << 3); + key ^= (key >> 6); + key += ~(key << 11); + key ^= (key >> 16); + return key; +} +#define kh_int_hash_func2(k) __ac_Wang_hash((khint_t)key) + +/* --- END OF HASH FUNCTIONS --- */ + +/* Other convenient macros... */ + +/*! + @abstract Type of the hash table. + @param name Name of the hash table [symbol] + */ +#define khash_t(name) kh_##name##_t + +/*! @function + @abstract Initiate a hash table. + @param name Name of the hash table [symbol] + @return Pointer to the hash table [khash_t(name)*] + */ +#define kh_init(name) kh_init_##name() + +/*! @function + @abstract Destroy a hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + */ +#define kh_destroy(name, h) kh_destroy_##name(h) + +/*! @function + @abstract Reset a hash table without deallocating memory. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + */ +#define kh_clear(name, h) kh_clear_##name(h) + +/*! @function + @abstract Resize a hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param s New size [khint_t] + */ +#define kh_resize(name, h, s) kh_resize_##name(h, s) + +/*! @function + @abstract Insert a key to the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @param r Extra return code: -1 if the operation failed; + 0 if the key is present in the hash table; + 1 if the bucket is empty (never used); 2 if the element in + the bucket has been deleted [int*] + @return Iterator to the inserted element [khint_t] + */ +#define kh_put(name, h, k, r) kh_put_##name(h, k, r) + +/*! @function + @abstract Retrieve a key from the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @return Iterator to the found element, or kh_end(h) if the element is absent [khint_t] + */ +#define kh_get(name, h, k) kh_get_##name(h, k) + +/*! @function + @abstract Remove a key from the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Iterator to the element to be deleted [khint_t] + */ +#define kh_del(name, h, k) kh_del_##name(h, k) + +/*! @function + @abstract Test whether a bucket contains data. + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return 1 if containing data; 0 otherwise [int] + */ +#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x))) + +/*! @function + @abstract Get key given an iterator + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return Key [type of keys] + */ +#define kh_key(h, x) ((h)->keys[x]) + +/*! @function + @abstract Get value given an iterator + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return Value [type of values] + @discussion For hash sets, calling this results in segfault. + */ +#define kh_val(h, x) ((h)->vals[x]) + +/*! @function + @abstract Alias of kh_val() + */ +#define kh_value(h, x) ((h)->vals[x]) + +/*! @function + @abstract Get the start iterator + @param h Pointer to the hash table [khash_t(name)*] + @return The start iterator [khint_t] + */ +#define kh_begin(h) (khint_t)(0) + +/*! @function + @abstract Get the end iterator + @param h Pointer to the hash table [khash_t(name)*] + @return The end iterator [khint_t] + */ +#define kh_end(h) ((h)->n_buckets) + +/*! @function + @abstract Get the number of elements in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @return Number of elements in the hash table [khint_t] + */ +#define kh_size(h) ((h)->size) + +/*! @function + @abstract Get the number of buckets in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @return Number of buckets in the hash table [khint_t] + */ +#define kh_n_buckets(h) ((h)->n_buckets) + +/*! @function + @abstract Iterate over the entries in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param kvar Variable to which key will be assigned + @param vvar Variable to which value will be assigned + @param code Block of code to execute + */ +#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (kvar) = kh_key(h,__i); \ + (vvar) = kh_val(h,__i); \ + code; \ + } } + +/*! @function + @abstract Iterate over the values in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param vvar Variable to which value will be assigned + @param code Block of code to execute + */ +#define kh_foreach_value(h, vvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (vvar) = kh_val(h,__i); \ + code; \ + } } + +/* More conenient interfaces */ + +/*! @function + @abstract Instantiate a hash set containing integer keys + @param name Name of the hash table [symbol] + */ +#define KHASH_SET_INIT_INT(name) \ + KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing integer keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] + */ +#define KHASH_MAP_INIT_INT(name, khval_t) \ + KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing 64-bit integer keys + @param name Name of the hash table [symbol] + */ +#define KHASH_SET_INIT_INT64(name) \ + KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing 64-bit integer keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] + */ +#define KHASH_MAP_INIT_INT64(name, khval_t) \ + KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) + +typedef const char *kh_cstr_t; +/*! @function + @abstract Instantiate a hash map containing const char* keys + @param name Name of the hash table [symbol] + */ +#define KHASH_SET_INIT_STR(name) \ + KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing const char* keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] + */ +#define KHASH_MAP_INIT_STR(name, khval_t) \ + KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal) + +#endif /* __AC_KHASH_H */ Property changes on: vendor/libucl/dist/klib/khash.h ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: vendor/libucl/dist/klib/kvec.h =================================================================== --- vendor/libucl/dist/klib/kvec.h (nonexistent) +++ vendor/libucl/dist/klib/kvec.h (revision 279546) @@ -0,0 +1,103 @@ +/* The MIT License + + Copyright (c) 2008, by Attractive Chaos + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +/* + An example: + +#include "kvec.h" +int main() { + kvec_t(int) array; + kv_init(array); + kv_push(int, array, 10); // append + kv_a(int, array, 20) = 5; // dynamic + kv_A(array, 20) = 4; // static + kv_destroy(array); + return 0; +} +*/ + +/* + 2008-09-22 (0.1.0): + + * The initial version. + +*/ + +#ifndef AC_KVEC_H +#define AC_KVEC_H + +#include + +#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) + +#define kvec_t(type) struct { size_t n, m; type *a; } +#define kv_init(v) ((v).n = (v).m = 0, (v).a = 0) +#define kv_destroy(v) free((v).a) +#define kv_A(v, i) ((v).a[(i)]) +#define kv_pop(v) ((v).a[--(v).n]) +#define kv_size(v) ((v).n) +#define kv_max(v) ((v).m) + +#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m)) +#define kv_grow_factor 1.5 +#define kv_grow(type, v) ((v).m = ((v).m > 1 ? (v).m * kv_grow_factor : 2), \ + (v).a = (type*)realloc((v).a, sizeof(type) * (v).m)) + +#define kv_copy(type, v1, v0) do { \ + if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \ + (v1).n = (v0).n; \ + memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \ + } while (0) \ + +#define kv_push(type, v, x) do { \ + if ((v).n == (v).m) { \ + kv_grow(type, v); \ + } \ + (v).a[(v).n++] = (x); \ + } while (0) + +#define kv_prepend(type, v, x) do { \ + if ((v).n == (v).m) { \ + kv_grow(type, v); \ + } \ + memmove((v).a + 1, (v).a, sizeof(type) * (v).n); \ + (v).a[0] = (x); \ + (v).n ++; \ +} while (0) + +#define kv_concat(type, v1, v0) do { \ + if ((v1).m < (v0).n + (v1).n) kv_resize(type, v1, (v0).n + (v1).n); \ + memcpy((v1).a + (v1).n, (v0).a, sizeof(type) * ((v0).n + (v1).n)); \ + (v1).n = (v0).n + (v1).n; \ + } while (0) + +#define kv_del(type, v, i) do { \ + if ((i) < (v).n) { \ + memmove((v).a + (i), (v).a + ((i) + 1), sizeof(type) * ((v).n - (i) - 1)); \ + (v).n --; \ + } \ +} while (0) + +#endif Property changes on: vendor/libucl/dist/klib/kvec.h ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: vendor/libucl/dist/src/Makefile.am =================================================================== --- vendor/libucl/dist/src/Makefile.am (revision 279545) +++ vendor/libucl/dist/src/Makefile.am (revision 279546) @@ -1,27 +1,28 @@ libucl_common_cflags= -I$(top_srcdir)/src \ -I$(top_srcdir)/include \ -I$(top_srcdir)/uthash \ + -I$(top_srcdir)/klib \ -Wall -W -Wno-unused-parameter -Wno-pointer-sign lib_LTLIBRARIES= libucl.la libucl_la_SOURCES= ucl_emitter.c \ ucl_emitter_streamline.c \ ucl_emitter_utils.c \ ucl_hash.c \ ucl_parser.c \ ucl_schema.c \ ucl_util.c \ xxhash.c libucl_la_CFLAGS= $(libucl_common_cflags) \ @CURL_CFLAGS@ libucl_la_LDFLAGS = -version-info @SO_VERSION@ libucl_la_LIBADD= @LIBFETCH_LIBS@ \ @LIBCRYPTO_LIB@ \ @LIBREGEX_LIB@ \ @CURL_LIBS@ include_HEADERS= $(top_srcdir)/include/ucl.h noinst_HEADERS= ucl_internal.h \ xxhash.h \ ucl_hash.h \ ucl_chartable.h \ tree.h Index: vendor/libucl/dist/src/ucl_emitter.c =================================================================== --- vendor/libucl/dist/src/ucl_emitter.c (revision 279545) +++ vendor/libucl/dist/src/ucl_emitter.c (revision 279546) @@ -1,506 +1,511 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "ucl.h" #include "ucl_internal.h" #include "ucl_chartable.h" #ifdef HAVE_FLOAT_H #include #endif #ifdef HAVE_MATH_H #include #endif /** * @file ucl_emitter.c * Serialise UCL object to various of output formats */ static void ucl_emitter_common_elt (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool first, bool print_key, bool compact); #define UCL_EMIT_TYPE_OPS(type) \ static void ucl_emit_ ## type ## _elt (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj, bool first, bool print_key); \ static void ucl_emit_ ## type ## _start_obj (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj, bool print_key); \ static void ucl_emit_ ## type## _start_array (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj, bool print_key); \ static void ucl_emit_ ##type## _end_object (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj); \ static void ucl_emit_ ##type## _end_array (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj) /* * JSON format operations */ UCL_EMIT_TYPE_OPS(json); UCL_EMIT_TYPE_OPS(json_compact); UCL_EMIT_TYPE_OPS(config); UCL_EMIT_TYPE_OPS(yaml); #define UCL_EMIT_TYPE_CONTENT(type) { \ .ucl_emitter_write_elt = ucl_emit_ ## type ## _elt, \ .ucl_emitter_start_object = ucl_emit_ ## type ##_start_obj, \ .ucl_emitter_start_array = ucl_emit_ ## type ##_start_array, \ .ucl_emitter_end_object = ucl_emit_ ## type ##_end_object, \ .ucl_emitter_end_array = ucl_emit_ ## type ##_end_array \ } const struct ucl_emitter_operations ucl_standartd_emitter_ops[] = { [UCL_EMIT_JSON] = UCL_EMIT_TYPE_CONTENT(json), [UCL_EMIT_JSON_COMPACT] = UCL_EMIT_TYPE_CONTENT(json_compact), [UCL_EMIT_CONFIG] = UCL_EMIT_TYPE_CONTENT(config), [UCL_EMIT_YAML] = UCL_EMIT_TYPE_CONTENT(yaml) }; /* * Utility to check whether we need a top object */ #define UCL_EMIT_IDENT_TOP_OBJ(ctx, obj) ((ctx)->top != (obj) || \ ((ctx)->id == UCL_EMIT_JSON_COMPACT || (ctx)->id == UCL_EMIT_JSON)) /** * Add tabulation to the output buffer * @param buf target buffer * @param tabs number of tabs to add */ static inline void ucl_add_tabs (const struct ucl_emitter_functions *func, unsigned int tabs, bool compact) { if (!compact && tabs > 0) { func->ucl_emitter_append_character (' ', tabs * 4, func->ud); } } /** * Print key for the element * @param ctx * @param obj */ static void ucl_emitter_print_key (bool print_key, struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool compact) { const struct ucl_emitter_functions *func = ctx->func; if (!print_key) { return; } if (ctx->id == UCL_EMIT_CONFIG) { if (obj->flags & UCL_OBJECT_NEED_KEY_ESCAPE) { ucl_elt_string_write_json (obj->key, obj->keylen, ctx); } else { func->ucl_emitter_append_len (obj->key, obj->keylen, func->ud); } if (obj->type != UCL_OBJECT && obj->type != UCL_ARRAY) { func->ucl_emitter_append_len (" = ", 3, func->ud); } else { func->ucl_emitter_append_character (' ', 1, func->ud); } } else if (ctx->id == UCL_EMIT_YAML) { if (obj->keylen > 0 && (obj->flags & UCL_OBJECT_NEED_KEY_ESCAPE)) { ucl_elt_string_write_json (obj->key, obj->keylen, ctx); } else if (obj->keylen > 0) { func->ucl_emitter_append_len (obj->key, obj->keylen, func->ud); } else { func->ucl_emitter_append_len ("null", 4, func->ud); } func->ucl_emitter_append_len (": ", 2, func->ud); } else { if (obj->keylen > 0) { ucl_elt_string_write_json (obj->key, obj->keylen, ctx); } else { func->ucl_emitter_append_len ("null", 4, func->ud); } if (compact) { func->ucl_emitter_append_character (':', 1, func->ud); } else { func->ucl_emitter_append_len (": ", 2, func->ud); } } } static void ucl_emitter_finish_object (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool compact, bool is_array) { const struct ucl_emitter_functions *func = ctx->func; if (ctx->id == UCL_EMIT_CONFIG && obj != ctx->top) { if (obj->type != UCL_OBJECT && obj->type != UCL_ARRAY) { if (!is_array) { /* Objects are split by ';' */ func->ucl_emitter_append_len (";\n", 2, func->ud); } else { /* Use commas for arrays */ func->ucl_emitter_append_len (",\n", 2, func->ud); } } else { func->ucl_emitter_append_character ('\n', 1, func->ud); } } } /** * End standard ucl object * @param ctx emitter context * @param compact compact flag */ static void ucl_emitter_common_end_object (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool compact) { const struct ucl_emitter_functions *func = ctx->func; if (UCL_EMIT_IDENT_TOP_OBJ(ctx, obj)) { ctx->indent --; if (compact) { func->ucl_emitter_append_character ('}', 1, func->ud); } else { if (ctx->id != UCL_EMIT_CONFIG) { /* newline is already added for this format */ func->ucl_emitter_append_character ('\n', 1, func->ud); } ucl_add_tabs (func, ctx->indent, compact); func->ucl_emitter_append_character ('}', 1, func->ud); } } ucl_emitter_finish_object (ctx, obj, compact, false); } /** * End standard ucl array * @param ctx emitter context * @param compact compact flag */ static void ucl_emitter_common_end_array (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool compact) { const struct ucl_emitter_functions *func = ctx->func; ctx->indent --; if (compact) { func->ucl_emitter_append_character (']', 1, func->ud); } else { if (ctx->id != UCL_EMIT_CONFIG) { /* newline is already added for this format */ func->ucl_emitter_append_character ('\n', 1, func->ud); } ucl_add_tabs (func, ctx->indent, compact); func->ucl_emitter_append_character (']', 1, func->ud); } ucl_emitter_finish_object (ctx, obj, compact, true); } /** * Start emit standard UCL array * @param ctx emitter context * @param obj object to write * @param compact compact flag */ static void ucl_emitter_common_start_array (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool print_key, bool compact) { const ucl_object_t *cur; + ucl_object_iter_t iter = NULL; const struct ucl_emitter_functions *func = ctx->func; bool first = true; ucl_emitter_print_key (print_key, ctx, obj, compact); if (compact) { func->ucl_emitter_append_character ('[', 1, func->ud); } else { func->ucl_emitter_append_len ("[\n", 2, func->ud); } ctx->indent ++; if (obj->type == UCL_ARRAY) { /* explicit array */ - cur = obj->value.av; + while ((cur = ucl_iterate_object (obj, &iter, true)) != NULL) { + ucl_emitter_common_elt (ctx, cur, first, false, compact); + first = false; + } } else { /* implicit array */ cur = obj; + while (cur) { + ucl_emitter_common_elt (ctx, cur, first, false, compact); + first = false; + cur = cur->next; + } } - while (cur) { - ucl_emitter_common_elt (ctx, cur, first, false, compact); - first = false; - cur = cur->next; - } + } /** * Start emit standard UCL object * @param ctx emitter context * @param obj object to write * @param compact compact flag */ static void ucl_emitter_common_start_object (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool print_key, bool compact) { ucl_hash_iter_t it = NULL; const ucl_object_t *cur, *elt; const struct ucl_emitter_functions *func = ctx->func; bool first = true; ucl_emitter_print_key (print_key, ctx, obj, compact); /* * Print { * */ if (UCL_EMIT_IDENT_TOP_OBJ(ctx, obj)) { if (compact) { func->ucl_emitter_append_character ('{', 1, func->ud); } else { func->ucl_emitter_append_len ("{\n", 2, func->ud); } ctx->indent ++; } while ((cur = ucl_hash_iterate (obj->value.ov, &it))) { if (ctx->id == UCL_EMIT_CONFIG) { LL_FOREACH (cur, elt) { ucl_emitter_common_elt (ctx, elt, first, true, compact); } } else { /* Expand implicit arrays */ if (cur->next != NULL) { if (!first) { if (compact) { func->ucl_emitter_append_character (',', 1, func->ud); } else { func->ucl_emitter_append_len (",\n", 2, func->ud); } } ucl_add_tabs (func, ctx->indent, compact); ucl_emitter_common_start_array (ctx, cur, true, compact); ucl_emitter_common_end_array (ctx, cur, compact); } else { ucl_emitter_common_elt (ctx, cur, first, true, compact); } } first = false; } } /** * Common choice of object emitting * @param ctx emitter context * @param obj object to print * @param first flag to mark the first element * @param print_key print key of an object * @param compact compact output */ static void ucl_emitter_common_elt (struct ucl_emitter_context *ctx, const ucl_object_t *obj, bool first, bool print_key, bool compact) { const struct ucl_emitter_functions *func = ctx->func; bool flag; struct ucl_object_userdata *ud; const char *ud_out = ""; if (ctx->id != UCL_EMIT_CONFIG && !first) { if (compact) { func->ucl_emitter_append_character (',', 1, func->ud); } else { if (ctx->id == UCL_EMIT_YAML && ctx->indent == 0) { func->ucl_emitter_append_len ("\n", 1, func->ud); } else { func->ucl_emitter_append_len (",\n", 2, func->ud); } } } ucl_add_tabs (func, ctx->indent, compact); switch (obj->type) { case UCL_INT: ucl_emitter_print_key (print_key, ctx, obj, compact); func->ucl_emitter_append_int (ucl_object_toint (obj), func->ud); ucl_emitter_finish_object (ctx, obj, compact, !print_key); break; case UCL_FLOAT: case UCL_TIME: ucl_emitter_print_key (print_key, ctx, obj, compact); func->ucl_emitter_append_double (ucl_object_todouble (obj), func->ud); ucl_emitter_finish_object (ctx, obj, compact, !print_key); break; case UCL_BOOLEAN: ucl_emitter_print_key (print_key, ctx, obj, compact); flag = ucl_object_toboolean (obj); if (flag) { func->ucl_emitter_append_len ("true", 4, func->ud); } else { func->ucl_emitter_append_len ("false", 5, func->ud); } ucl_emitter_finish_object (ctx, obj, compact, !print_key); break; case UCL_STRING: ucl_emitter_print_key (print_key, ctx, obj, compact); if (ctx->id == UCL_EMIT_CONFIG && ucl_maybe_long_string (obj)) { ucl_elt_string_write_multiline (obj->value.sv, obj->len, ctx); } else { ucl_elt_string_write_json (obj->value.sv, obj->len, ctx); } ucl_emitter_finish_object (ctx, obj, compact, !print_key); break; case UCL_NULL: ucl_emitter_print_key (print_key, ctx, obj, compact); func->ucl_emitter_append_len ("null", 4, func->ud); ucl_emitter_finish_object (ctx, obj, compact, !print_key); break; case UCL_OBJECT: ucl_emitter_common_start_object (ctx, obj, print_key, compact); ucl_emitter_common_end_object (ctx, obj, compact); break; case UCL_ARRAY: ucl_emitter_common_start_array (ctx, obj, print_key, compact); ucl_emitter_common_end_array (ctx, obj, compact); break; case UCL_USERDATA: ud = (struct ucl_object_userdata *)obj; ucl_emitter_print_key (print_key, ctx, obj, compact); if (ud->emitter) { ud_out = ud->emitter (obj->value.ud); if (ud_out == NULL) { ud_out = "null"; } } ucl_elt_string_write_json (ud_out, strlen (ud_out), ctx); ucl_emitter_finish_object (ctx, obj, compact, !print_key); break; } } /* * Specific standard implementations of the emitter functions */ #define UCL_EMIT_TYPE_IMPL(type, compact) \ static void ucl_emit_ ## type ## _elt (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj, bool first, bool print_key) { \ ucl_emitter_common_elt (ctx, obj, first, print_key, (compact)); \ } \ static void ucl_emit_ ## type ## _start_obj (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj, bool print_key) { \ ucl_emitter_common_start_object (ctx, obj, print_key, (compact)); \ } \ static void ucl_emit_ ## type## _start_array (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj, bool print_key) { \ ucl_emitter_common_start_array (ctx, obj, print_key, (compact)); \ } \ static void ucl_emit_ ##type## _end_object (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj) { \ ucl_emitter_common_end_object (ctx, obj, (compact)); \ } \ static void ucl_emit_ ##type## _end_array (struct ucl_emitter_context *ctx, \ const ucl_object_t *obj) { \ ucl_emitter_common_end_array (ctx, obj, (compact)); \ } UCL_EMIT_TYPE_IMPL(json, false) UCL_EMIT_TYPE_IMPL(json_compact, true) UCL_EMIT_TYPE_IMPL(config, false) UCL_EMIT_TYPE_IMPL(yaml, false) unsigned char * ucl_object_emit (const ucl_object_t *obj, enum ucl_emitter emit_type) { unsigned char *res = NULL; struct ucl_emitter_functions *func; if (obj == NULL) { return NULL; } func = ucl_object_emit_memory_funcs ((void **)&res); if (func != NULL) { ucl_object_emit_full (obj, emit_type, func); ucl_object_emit_funcs_free (func); } return res; } bool ucl_object_emit_full (const ucl_object_t *obj, enum ucl_emitter emit_type, struct ucl_emitter_functions *emitter) { const struct ucl_emitter_context *ctx; struct ucl_emitter_context my_ctx; bool res = false; ctx = ucl_emit_get_standard_context (emit_type); if (ctx != NULL) { memcpy (&my_ctx, ctx, sizeof (my_ctx)); my_ctx.func = emitter; my_ctx.indent = 0; my_ctx.top = obj; my_ctx.ops->ucl_emitter_write_elt (&my_ctx, obj, true, false); res = true; } return res; } Index: vendor/libucl/dist/src/ucl_emitter_utils.c =================================================================== --- vendor/libucl/dist/src/ucl_emitter_utils.c (revision 279545) +++ vendor/libucl/dist/src/ucl_emitter_utils.c (revision 279546) @@ -1,486 +1,487 @@ /* Copyright (c) 2014, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "ucl.h" #include "ucl_internal.h" #include "ucl_chartable.h" #ifdef HAVE_FLOAT_H #include #endif #ifdef HAVE_MATH_H #include #endif extern const struct ucl_emitter_operations ucl_standartd_emitter_ops[]; static const struct ucl_emitter_context ucl_standard_emitters[] = { [UCL_EMIT_JSON] = { .name = "json", .id = UCL_EMIT_JSON, .func = NULL, .ops = &ucl_standartd_emitter_ops[UCL_EMIT_JSON] }, [UCL_EMIT_JSON_COMPACT] = { .name = "json_compact", .id = UCL_EMIT_JSON_COMPACT, .func = NULL, .ops = &ucl_standartd_emitter_ops[UCL_EMIT_JSON_COMPACT] }, [UCL_EMIT_CONFIG] = { .name = "config", .id = UCL_EMIT_CONFIG, .func = NULL, .ops = &ucl_standartd_emitter_ops[UCL_EMIT_CONFIG] }, [UCL_EMIT_YAML] = { .name = "yaml", .id = UCL_EMIT_YAML, .func = NULL, .ops = &ucl_standartd_emitter_ops[UCL_EMIT_YAML] } }; /** * Get standard emitter context for a specified emit_type * @param emit_type type of emitter * @return context or NULL if input is invalid */ const struct ucl_emitter_context * ucl_emit_get_standard_context (enum ucl_emitter emit_type) { if (emit_type >= UCL_EMIT_JSON && emit_type <= UCL_EMIT_YAML) { return &ucl_standard_emitters[emit_type]; } return NULL; } /** * Serialise string * @param str string to emit * @param buf target buffer */ void ucl_elt_string_write_json (const char *str, size_t size, struct ucl_emitter_context *ctx) { const char *p = str, *c = str; size_t len = 0; const struct ucl_emitter_functions *func = ctx->func; func->ucl_emitter_append_character ('"', 1, func->ud); while (size) { if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) { if (len > 0) { func->ucl_emitter_append_len (c, len, func->ud); } switch (*p) { case '\n': func->ucl_emitter_append_len ("\\n", 2, func->ud); break; case '\r': func->ucl_emitter_append_len ("\\r", 2, func->ud); break; case '\b': func->ucl_emitter_append_len ("\\b", 2, func->ud); break; case '\t': func->ucl_emitter_append_len ("\\t", 2, func->ud); break; case '\f': func->ucl_emitter_append_len ("\\f", 2, func->ud); break; case '\\': func->ucl_emitter_append_len ("\\\\", 2, func->ud); break; case '"': func->ucl_emitter_append_len ("\\\"", 2, func->ud); break; } len = 0; c = ++p; } else { p ++; len ++; } size --; } if (len > 0) { func->ucl_emitter_append_len (c, len, func->ud); } func->ucl_emitter_append_character ('"', 1, func->ud); } void ucl_elt_string_write_multiline (const char *str, size_t size, struct ucl_emitter_context *ctx) { const struct ucl_emitter_functions *func = ctx->func; func->ucl_emitter_append_len ("<ud); func->ucl_emitter_append_len (str, size, func->ud); func->ucl_emitter_append_len ("\nEOD", sizeof ("\nEOD") - 1, func->ud); } /* * Generic utstring output */ static int ucl_utstring_append_character (unsigned char c, size_t len, void *ud) { UT_string *buf = ud; if (len == 1) { utstring_append_c (buf, c); } else { utstring_reserve (buf, len + 1); memset (&buf->d[buf->i], c, len); buf->i += len; buf->d[buf->i] = '\0'; } return 0; } static int ucl_utstring_append_len (const unsigned char *str, size_t len, void *ud) { UT_string *buf = ud; utstring_append_len (buf, str, len); return 0; } static int ucl_utstring_append_int (int64_t val, void *ud) { UT_string *buf = ud; utstring_printf (buf, "%jd", (intmax_t)val); return 0; } static int ucl_utstring_append_double (double val, void *ud) { UT_string *buf = ud; const double delta = 0.0000001; if (val == (double)(int)val) { utstring_printf (buf, "%.1lf", val); } else if (fabs (val - (double)(int)val) < delta) { /* Write at maximum precision */ utstring_printf (buf, "%.*lg", DBL_DIG, val); } else { utstring_printf (buf, "%lf", val); } return 0; } /* * Generic file output */ static int ucl_file_append_character (unsigned char c, size_t len, void *ud) { FILE *fp = ud; while (len --) { fputc (c, fp); } return 0; } static int ucl_file_append_len (const unsigned char *str, size_t len, void *ud) { FILE *fp = ud; fwrite (str, len, 1, fp); return 0; } static int ucl_file_append_int (int64_t val, void *ud) { FILE *fp = ud; fprintf (fp, "%jd", (intmax_t)val); return 0; } static int ucl_file_append_double (double val, void *ud) { FILE *fp = ud; const double delta = 0.0000001; if (val == (double)(int)val) { fprintf (fp, "%.1lf", val); } else if (fabs (val - (double)(int)val) < delta) { /* Write at maximum precision */ fprintf (fp, "%.*lg", DBL_DIG, val); } else { fprintf (fp, "%lf", val); } return 0; } /* * Generic file descriptor writing functions */ static int ucl_fd_append_character (unsigned char c, size_t len, void *ud) { int fd = *(int *)ud; unsigned char *buf; if (len == 1) { return write (fd, &c, 1); } else { buf = malloc (len); if (buf == NULL) { /* Fallback */ while (len --) { if (write (fd, &c, 1) == -1) { return -1; } } } else { memset (buf, c, len); if (write (fd, buf, len) == -1) { + free(buf); return -1; } free (buf); } } return 0; } static int ucl_fd_append_len (const unsigned char *str, size_t len, void *ud) { int fd = *(int *)ud; return write (fd, str, len); } static int ucl_fd_append_int (int64_t val, void *ud) { int fd = *(int *)ud; char intbuf[64]; snprintf (intbuf, sizeof (intbuf), "%jd", (intmax_t)val); return write (fd, intbuf, strlen (intbuf)); } static int ucl_fd_append_double (double val, void *ud) { int fd = *(int *)ud; const double delta = 0.0000001; char nbuf[64]; if (val == (double)(int)val) { snprintf (nbuf, sizeof (nbuf), "%.1lf", val); } else if (fabs (val - (double)(int)val) < delta) { /* Write at maximum precision */ snprintf (nbuf, sizeof (nbuf), "%.*lg", DBL_DIG, val); } else { snprintf (nbuf, sizeof (nbuf), "%lf", val); } return write (fd, nbuf, strlen (nbuf)); } struct ucl_emitter_functions* ucl_object_emit_memory_funcs (void **pmem) { struct ucl_emitter_functions *f; UT_string *s; f = calloc (1, sizeof (*f)); if (f != NULL) { f->ucl_emitter_append_character = ucl_utstring_append_character; f->ucl_emitter_append_double = ucl_utstring_append_double; f->ucl_emitter_append_int = ucl_utstring_append_int; f->ucl_emitter_append_len = ucl_utstring_append_len; f->ucl_emitter_free_func = free; utstring_new (s); f->ud = s; *pmem = s->d; s->pd = pmem; } return f; } struct ucl_emitter_functions* ucl_object_emit_file_funcs (FILE *fp) { struct ucl_emitter_functions *f; f = calloc (1, sizeof (*f)); if (f != NULL) { f->ucl_emitter_append_character = ucl_file_append_character; f->ucl_emitter_append_double = ucl_file_append_double; f->ucl_emitter_append_int = ucl_file_append_int; f->ucl_emitter_append_len = ucl_file_append_len; f->ucl_emitter_free_func = NULL; f->ud = fp; } return f; } struct ucl_emitter_functions* ucl_object_emit_fd_funcs (int fd) { struct ucl_emitter_functions *f; int *ip; f = calloc (1, sizeof (*f)); if (f != NULL) { ip = malloc (sizeof (fd)); if (ip == NULL) { free (f); return NULL; } memcpy (ip, &fd, sizeof (fd)); f->ucl_emitter_append_character = ucl_fd_append_character; f->ucl_emitter_append_double = ucl_fd_append_double; f->ucl_emitter_append_int = ucl_fd_append_int; f->ucl_emitter_append_len = ucl_fd_append_len; f->ucl_emitter_free_func = free; f->ud = ip; } return f; } void ucl_object_emit_funcs_free (struct ucl_emitter_functions *f) { if (f != NULL) { if (f->ucl_emitter_free_func != NULL) { f->ucl_emitter_free_func (f->ud); } free (f); } } unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj) { UT_string *buf = NULL; unsigned char *res = NULL; if (obj == NULL) { return NULL; } utstring_new (buf); if (buf != NULL) { switch (obj->type) { case UCL_OBJECT: ucl_utstring_append_len ("object", 6, buf); break; case UCL_ARRAY: ucl_utstring_append_len ("array", 5, buf); break; case UCL_INT: ucl_utstring_append_int (obj->value.iv, buf); break; case UCL_FLOAT: case UCL_TIME: ucl_utstring_append_double (obj->value.dv, buf); break; case UCL_NULL: ucl_utstring_append_len ("null", 4, buf); break; case UCL_BOOLEAN: if (obj->value.iv) { ucl_utstring_append_len ("true", 4, buf); } else { ucl_utstring_append_len ("false", 5, buf); } break; case UCL_STRING: ucl_utstring_append_len (obj->value.sv, obj->len, buf); break; case UCL_USERDATA: ucl_utstring_append_len ("userdata", 8, buf); break; } res = utstring_body (buf); free (buf); } return res; } #define LONG_STRING_LIMIT 80 bool ucl_maybe_long_string (const ucl_object_t *obj) { if (obj->len > LONG_STRING_LIMIT || (obj->flags & UCL_OBJECT_MULTILINE)) { /* String is long enough, so search for newline characters in it */ if (memchr (obj->value.sv, '\n', obj->len) != NULL) { return true; } } return false; } Index: vendor/libucl/dist/src/ucl_hash.c =================================================================== --- vendor/libucl/dist/src/ucl_hash.c (revision 279545) +++ vendor/libucl/dist/src/ucl_hash.c (revision 279546) @@ -1,141 +1,353 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "ucl_internal.h" #include "ucl_hash.h" -#include "utlist.h" +#include "khash.h" +#include "kvec.h" +struct ucl_hash_elt { + const ucl_object_t *obj; + size_t ar_idx; +}; + +struct ucl_hash_struct { + void *hash; + kvec_t(const ucl_object_t *) ar; + bool caseless; +}; + +static inline uint32_t +ucl_hash_func (const ucl_object_t *o) +{ + return XXH32 (o->key, o->keylen, 0xdeadbeef); +} + +static inline int +ucl_hash_equal (const ucl_object_t *k1, const ucl_object_t *k2) +{ + if (k1->keylen == k2->keylen) { + return strncmp (k1->key, k2->key, k1->keylen) == 0; + } + + return 0; +} + +KHASH_INIT (ucl_hash_node, const ucl_object_t *, struct ucl_hash_elt, 1, + ucl_hash_func, ucl_hash_equal) + +static inline uint32_t +ucl_hash_caseless_func (const ucl_object_t *o) +{ + void *xxh = XXH32_init (0xdeadbeef); + char hash_buf[64], *c; + const char *p; + ssize_t remain = o->keylen; + + p = o->key; + c = &hash_buf[0]; + + while (remain > 0) { + *c++ = tolower (*p++); + + if (c - &hash_buf[0] == sizeof (hash_buf)) { + XXH32_update (xxh, hash_buf, sizeof (hash_buf)); + c = &hash_buf[0]; + } + remain --; + } + + if (c - &hash_buf[0] != 0) { + XXH32_update (xxh, hash_buf, c - &hash_buf[0]); + } + + return XXH32_digest (xxh); +} + +static inline int +ucl_hash_caseless_equal (const ucl_object_t *k1, const ucl_object_t *k2) +{ + if (k1->keylen == k2->keylen) { + return strncasecmp (k1->key, k2->key, k1->keylen) == 0; + } + + return 0; +} + +KHASH_INIT (ucl_hash_caseless_node, const ucl_object_t *, struct ucl_hash_elt, 1, + ucl_hash_caseless_func, ucl_hash_caseless_equal) + ucl_hash_t* -ucl_hash_create (void) +ucl_hash_create (bool ignore_case) { ucl_hash_t *new; new = UCL_ALLOC (sizeof (ucl_hash_t)); if (new != NULL) { - new->buckets = NULL; + kv_init (new->ar); + + new->caseless = ignore_case; + if (ignore_case) { + khash_t(ucl_hash_caseless_node) *h = kh_init (ucl_hash_caseless_node); + new->hash = (void *)h; + } + else { + khash_t(ucl_hash_node) *h = kh_init (ucl_hash_node); + new->hash = (void *)h; + } } return new; } void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func) { - ucl_hash_node_t *elt, *tmp; - const ucl_object_t *cur, *otmp; + const ucl_object_t *cur, *tmp; - HASH_ITER (hh, hashlin->buckets, elt, tmp) { - HASH_DELETE (hh, hashlin->buckets, elt); - if (func) { - DL_FOREACH_SAFE (elt->data, cur, otmp) { - /* Need to deconst here */ - func (__DECONST (ucl_object_t *, cur)); + if (hashlin == NULL) { + return; + } + + if (func != NULL) { + /* Iterate over the hash first */ + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + khiter_t k; + + for (k = kh_begin (h); k != kh_end (h); ++k) { + if (kh_exist (h, k)) { + cur = (kh_value (h, k)).obj; + while (cur != NULL) { + tmp = cur->next; + func (__DECONST (ucl_object_t *, cur)); + cur = tmp; + } } } - UCL_FREE (sizeof (ucl_hash_node_t), elt); } - UCL_FREE (sizeof (ucl_hash_t), hashlin); + + if (hashlin->caseless) { + khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) + hashlin->hash; + kh_destroy (ucl_hash_caseless_node, h); + } + else { + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + kh_destroy (ucl_hash_node, h); + } + + kv_destroy (hashlin->ar); + UCL_FREE (sizeof (*hashlin), hashlin); } void ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, const char *key, unsigned keylen) { - ucl_hash_node_t *node; + khiter_t k; + int ret; + struct ucl_hash_elt *elt; - node = UCL_ALLOC (sizeof (ucl_hash_node_t)); - node->data = obj; - HASH_ADD_KEYPTR (hh, hashlin->buckets, key, keylen, node); + if (hashlin == NULL) { + return; + } + + if (hashlin->caseless) { + khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) + hashlin->hash; + k = kh_put (ucl_hash_caseless_node, h, obj, &ret); + if (ret > 0) { + elt = &kh_value (h, k); + kv_push (const ucl_object_t *, hashlin->ar, obj); + elt->obj = obj; + elt->ar_idx = kv_size (hashlin->ar) - 1; + } + } + else { + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + k = kh_put (ucl_hash_node, h, obj, &ret); + if (ret > 0) { + elt = &kh_value (h, k); + kv_push (const ucl_object_t *, hashlin->ar, obj); + elt->obj = obj; + elt->ar_idx = kv_size (hashlin->ar) - 1; + } + } } void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old, const ucl_object_t *new) { - ucl_hash_node_t *node; + khiter_t k; + int ret; + struct ucl_hash_elt elt, *pelt; - HASH_FIND (hh, hashlin->buckets, old->key, old->keylen, node); - if (node != NULL) { - /* Direct replacement */ - node->data = new; - node->hh.key = new->key; - node->hh.keylen = new->keylen; + if (hashlin == NULL) { + return; } + + if (hashlin->caseless) { + khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) + hashlin->hash; + k = kh_put (ucl_hash_caseless_node, h, old, &ret); + if (ret == 0) { + elt = kh_value (h, k); + kh_del (ucl_hash_caseless_node, h, k); + k = kh_put (ucl_hash_caseless_node, h, new, &ret); + pelt = &kh_value (h, k); + pelt->obj = new; + pelt->ar_idx = elt.ar_idx; + kv_A (hashlin->ar, elt.ar_idx) = new; + } + } + else { + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + k = kh_put (ucl_hash_node, h, old, &ret); + if (ret == 0) { + elt = kh_value (h, k); + kh_del (ucl_hash_node, h, k); + k = kh_put (ucl_hash_node, h, new, &ret); + pelt = &kh_value (h, k); + pelt->obj = new; + pelt->ar_idx = elt.ar_idx; + kv_A (hashlin->ar, elt.ar_idx) = new; + } + } } +struct ucl_hash_real_iter { + const ucl_object_t **cur; + const ucl_object_t **end; +}; + const void* ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter) { - ucl_hash_node_t *elt = *iter; + struct ucl_hash_real_iter *it = (struct ucl_hash_real_iter *)(*iter); + const ucl_object_t *ret = NULL; - if (elt == NULL) { - if (hashlin == NULL || hashlin->buckets == NULL) { - return NULL; - } - elt = hashlin->buckets; - if (elt == NULL) { - return NULL; - } + if (hashlin == NULL) { + return NULL; } - else if (elt == hashlin->buckets) { + + if (it == NULL) { + it = UCL_ALLOC (sizeof (*it)); + it->cur = &hashlin->ar.a[0]; + it->end = it->cur + hashlin->ar.n; + } + + if (it->cur < it->end) { + ret = *it->cur++; + } + else { + UCL_FREE (sizeof (*it), it); + *iter = NULL; return NULL; } - *iter = elt->hh.next ? elt->hh.next : hashlin->buckets; - return elt->data; + *iter = it; + + return ret; } bool -ucl_hash_iter_has_next (ucl_hash_iter_t iter) +ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter) { - ucl_hash_node_t *elt = iter; + struct ucl_hash_real_iter *it = (struct ucl_hash_real_iter *)(iter); - return (elt == NULL || elt->hh.prev != NULL); + return it->cur < it->end - 1; } const ucl_object_t* ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen) { - ucl_hash_node_t *found; + khiter_t k; + const ucl_object_t *ret = NULL; + ucl_object_t search; + struct ucl_hash_elt *elt; + search.key = key; + search.keylen = keylen; + if (hashlin == NULL) { return NULL; } - HASH_FIND (hh, hashlin->buckets, key, keylen, found); - if (found) { - return found->data; + if (hashlin->caseless) { + khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) + hashlin->hash; + + k = kh_get (ucl_hash_caseless_node, h, &search); + if (k != kh_end (h)) { + elt = &kh_value (h, k); + ret = elt->obj; + } } - return NULL; + else { + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + k = kh_get (ucl_hash_node, h, &search); + if (k != kh_end (h)) { + elt = &kh_value (h, k); + ret = elt->obj; + } + } + + return ret; } void ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj) { - ucl_hash_node_t *found; + khiter_t k; + struct ucl_hash_elt *elt; - HASH_FIND (hh, hashlin->buckets, obj->key, obj->keylen, found); + if (hashlin == NULL) { + return; + } - if (found) { - HASH_DELETE (hh, hashlin->buckets, found); - UCL_FREE (sizeof (ucl_hash_node_t), found); + if (hashlin->caseless) { + khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) + hashlin->hash; + + k = kh_get (ucl_hash_caseless_node, h, obj); + if (k != kh_end (h)) { + elt = &kh_value (h, k); + kv_A (hashlin->ar, elt->ar_idx) = NULL; + kh_del (ucl_hash_caseless_node, h, k); + } + } + else { + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + k = kh_get (ucl_hash_node, h, obj); + if (k != kh_end (h)) { + elt = &kh_value (h, k); + kv_A (hashlin->ar, elt->ar_idx) = NULL; + kh_del (ucl_hash_node, h, k); + } } } Index: vendor/libucl/dist/src/ucl_hash.h =================================================================== --- vendor/libucl/dist/src/ucl_hash.h (revision 279545) +++ vendor/libucl/dist/src/ucl_hash.h (revision 279546) @@ -1,99 +1,93 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef __UCL_HASH_H #define __UCL_HASH_H #include "ucl.h" -#include "uthash.h" /******************************************************************************/ -typedef struct ucl_hash_node_s -{ - const ucl_object_t *data; - UT_hash_handle hh; -} ucl_hash_node_t; +struct ucl_hash_node_s; +typedef struct ucl_hash_node_s ucl_hash_node_t; typedef int ucl_hash_cmp_func (const void* void_a, const void* void_b); typedef void ucl_hash_free_func (void *ptr); typedef void* ucl_hash_iter_t; /** * Linear chained hashtable. */ -typedef struct ucl_hash_struct -{ - ucl_hash_node_t *buckets; /**< array of hash buckets. One list for each hash modulus. */ -} ucl_hash_t; +struct ucl_hash_struct; +typedef struct ucl_hash_struct ucl_hash_t; /** * Initializes the hashtable. */ -ucl_hash_t* ucl_hash_create (void); +ucl_hash_t* ucl_hash_create (bool ignore_case); /** * Deinitializes the hashtable. */ void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func); /** * Inserts an element in the the hashtable. */ void ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, const char *key, unsigned keylen); /** * Replace element in the hash */ void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old, const ucl_object_t *new); /** * Delete an element from the the hashtable. */ void ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj); /** * Searches an element in the hashtable. */ const ucl_object_t* ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen); /** * Iterate over hash table * @param hashlin hash * @param iter iterator (must be NULL on first iteration) * @return the next object */ const void* ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter); /** * Check whether an iterator has next element */ -bool ucl_hash_iter_has_next (ucl_hash_iter_t iter); +bool ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter); #endif Index: vendor/libucl/dist/src/ucl_internal.h =================================================================== --- vendor/libucl/dist/src/ucl_internal.h (revision 279545) +++ vendor/libucl/dist/src/ucl_internal.h (revision 279546) @@ -1,396 +1,399 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef UCL_INTERNAL_H_ #define UCL_INTERNAL_H_ #ifdef HAVE_CONFIG_H #include "config.h" #else /* Help embedded builds */ #define HAVE_SYS_TYPES_H #define HAVE_SYS_MMAN_H #define HAVE_SYS_STAT_H #define HAVE_SYS_PARAM_H #define HAVE_LIMITS_H #define HAVE_FCNTL_H #define HAVE_ERRNO_H #define HAVE_UNISTD_H #define HAVE_CTYPE_H #define HAVE_STDIO_H #define HAVE_STRING_H #define HAVE_FLOAT_H #define HAVE_LIBGEN_H #define HAVE_MATH_H #define HAVE_STDBOOL_H #define HAVE_STDINT_H #define HAVE_STDARG_H #ifndef _WIN32 # define HAVE_REGEX_H #endif #endif #ifdef HAVE_SYS_TYPES_H #include #endif #ifdef HAVE_SYS_MMAN_H # ifndef _WIN32 # include # endif #endif #ifdef HAVE_SYS_STAT_H #include #endif #ifdef HAVE_SYS_PARAM_H #include #endif #ifdef HAVE_LIMITS_H #include #endif #ifdef HAVE_FCNTL_H #include #endif #ifdef HAVE_ERRNO_H #include #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef HAVE_CTYPE_H #include #endif #ifdef HAVE_STDIO_H #include #endif #ifdef HAVE_STRING_H #include #endif #include "utlist.h" #include "utstring.h" #include "uthash.h" #include "ucl.h" #include "ucl_hash.h" #include "xxhash.h" #ifdef HAVE_OPENSSL #include #endif #ifndef __DECONST #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) #endif /** * @file rcl_internal.h * Internal structures and functions of UCL library */ #define UCL_MAX_RECURSION 16 #define UCL_TRASH_KEY 0 #define UCL_TRASH_VALUE 1 enum ucl_parser_state { UCL_STATE_INIT = 0, UCL_STATE_OBJECT, UCL_STATE_ARRAY, UCL_STATE_KEY, UCL_STATE_VALUE, UCL_STATE_AFTER_VALUE, UCL_STATE_ARRAY_VALUE, UCL_STATE_SCOMMENT, UCL_STATE_MCOMMENT, UCL_STATE_MACRO_NAME, UCL_STATE_MACRO, UCL_STATE_ERROR }; enum ucl_character_type { UCL_CHARACTER_DENIED = 0, UCL_CHARACTER_KEY = 1, UCL_CHARACTER_KEY_START = 1 << 1, UCL_CHARACTER_WHITESPACE = 1 << 2, UCL_CHARACTER_WHITESPACE_UNSAFE = 1 << 3, UCL_CHARACTER_VALUE_END = 1 << 4, UCL_CHARACTER_VALUE_STR = 1 << 5, UCL_CHARACTER_VALUE_DIGIT = 1 << 6, UCL_CHARACTER_VALUE_DIGIT_START = 1 << 7, UCL_CHARACTER_ESCAPE = 1 << 8, UCL_CHARACTER_KEY_SEP = 1 << 9, UCL_CHARACTER_JSON_UNSAFE = 1 << 10, UCL_CHARACTER_UCL_UNSAFE = 1 << 11 }; struct ucl_macro { char *name; ucl_macro_handler handler; void* ud; UT_hash_handle hh; }; struct ucl_stack { ucl_object_t *obj; struct ucl_stack *next; int level; }; struct ucl_chunk { const unsigned char *begin; const unsigned char *end; const unsigned char *pos; size_t remain; unsigned int line; unsigned int column; unsigned priority; struct ucl_chunk *next; }; #ifdef HAVE_OPENSSL struct ucl_pubkey { EVP_PKEY *key; struct ucl_pubkey *next; }; #else struct ucl_pubkey { struct ucl_pubkey *next; }; #endif struct ucl_variable { char *var; char *value; size_t var_len; size_t value_len; struct ucl_variable *prev, *next; }; struct ucl_parser { enum ucl_parser_state state; enum ucl_parser_state prev_state; unsigned int recursion; int flags; ucl_object_t *top_obj; ucl_object_t *cur_obj; char *cur_file; struct ucl_macro *macroes; struct ucl_stack *stack; struct ucl_chunk *chunks; struct ucl_pubkey *keys; struct ucl_variable *variables; ucl_variable_handler var_handler; void *var_data; UT_string *err; }; struct ucl_object_userdata { ucl_object_t obj; ucl_userdata_dtor dtor; ucl_userdata_emitter emitter; }; /** * Unescape json string inplace * @param str */ size_t ucl_unescape_json_string (char *str, size_t len); /** * Handle include macro * @param data include data * @param len length of data * @param ud user data * @param err error ptr * @return */ bool ucl_include_handler (const unsigned char *data, size_t len, const ucl_object_t *args, void* ud); bool ucl_try_include_handler (const unsigned char *data, size_t len, const ucl_object_t *args, void* ud); /** * Handle includes macro * @param data include data * @param len length of data * @param ud user data * @param err error ptr * @return */ bool ucl_includes_handler (const unsigned char *data, size_t len, const ucl_object_t *args, void* ud); size_t ucl_strlcpy (char *dst, const char *src, size_t siz); size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz); size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz); #ifdef __GNUC__ static inline void ucl_create_err (UT_string **err, const char *fmt, ...) __attribute__ (( format( printf, 2, 3) )); #endif static inline void ucl_create_err (UT_string **err, const char *fmt, ...) { if (*err == NULL) { utstring_new (*err); va_list ap; va_start (ap, fmt); utstring_printf_va (*err, fmt, ap); va_end (ap); } } /** * Check whether a given string contains a boolean value * @param obj object to set * @param start start of a string * @param len length of a string * @return true if a string is a boolean value */ static inline bool ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len) { const char *p = (const char *)start; bool ret = false, val = false; if (len == 5) { if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) { ret = true; val = false; } } else if (len == 4) { if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) { ret = true; val = true; } } else if (len == 3) { if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) { ret = true; val = true; } else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) { ret = true; val = false; } } else if (len == 2) { if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) { ret = true; val = false; } else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) { ret = true; val = true; } } if (ret) { obj->type = UCL_BOOLEAN; obj->value.iv = val; } return ret; } /** * Check numeric string * @param obj object to set if a string is numeric * @param start start of string * @param end end of string * @param pos position where parsing has stopped * @param allow_double allow parsing of floating point values * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error */ int ucl_maybe_parse_number (ucl_object_t *obj, const char *start, const char *end, const char **pos, bool allow_double, bool number_bytes, bool allow_time); static inline const ucl_object_t * ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj) { return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen); } -static inline ucl_hash_t * -ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj) UCL_WARN_UNUSED_RESULT; +static inline ucl_hash_t * ucl_hash_insert_object (ucl_hash_t *hashlin, + const ucl_object_t *obj, + bool ignore_case) UCL_WARN_UNUSED_RESULT; static inline ucl_hash_t * -ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj) +ucl_hash_insert_object (ucl_hash_t *hashlin, + const ucl_object_t *obj, + bool ignore_case) { if (hashlin == NULL) { - hashlin = ucl_hash_create (); + hashlin = ucl_hash_create (ignore_case); } ucl_hash_insert (hashlin, obj, obj->key, obj->keylen); return hashlin; } /** * Get standard emitter context for a specified emit_type * @param emit_type type of emitter * @return context or NULL if input is invalid */ const struct ucl_emitter_context * ucl_emit_get_standard_context (enum ucl_emitter emit_type); /** * Serialize string as JSON string * @param str string to emit * @param buf target buffer */ void ucl_elt_string_write_json (const char *str, size_t size, struct ucl_emitter_context *ctx); /** * Write multiline string using `EOD` as string terminator * @param str * @param size * @param ctx */ void ucl_elt_string_write_multiline (const char *str, size_t size, struct ucl_emitter_context *ctx); /** * Emit a single object to string * @param obj * @return */ unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj); /** * Check whether a specified string is long and should be likely printed in * multiline mode * @param obj * @return */ bool ucl_maybe_long_string (const ucl_object_t *obj); #endif /* UCL_INTERNAL_H_ */ Index: vendor/libucl/dist/src/ucl_parser.c =================================================================== --- vendor/libucl/dist/src/ucl_parser.c (revision 279545) +++ vendor/libucl/dist/src/ucl_parser.c (revision 279546) @@ -1,2219 +1,2223 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "ucl.h" #include "ucl_internal.h" #include "ucl_chartable.h" /** * @file ucl_parser.c * The implementation of ucl parser */ struct ucl_parser_saved_state { unsigned int line; unsigned int column; size_t remain; const unsigned char *pos; }; /** * Move up to len characters * @param parser * @param begin * @param len * @return new position in chunk */ #define ucl_chunk_skipc(chunk, p) do{ \ if (*(p) == '\n') { \ (chunk)->line ++; \ (chunk)->column = 0; \ } \ else (chunk)->column ++; \ (p++); \ (chunk)->pos ++; \ (chunk)->remain --; \ } while (0) static inline void ucl_set_err (struct ucl_parser *parser, int code, const char *str, UT_string **err) { const char *fmt_string, *filename; struct ucl_chunk *chunk = parser->chunks; if (parser->cur_file) { filename = parser->cur_file; } else { filename = ""; } if (chunk->pos < chunk->end) { if (isgraph (*chunk->pos)) { fmt_string = "error while parsing %s: " "line: %d, column: %d - '%s', character: '%c'"; } else { fmt_string = "error while parsing %s: " "line: %d, column: %d - '%s', character: '0x%02x'"; } ucl_create_err (err, fmt_string, filename, chunk->line, chunk->column, str, *chunk->pos); } else { ucl_create_err (err, "error while parsing %s: at the end of chunk: %s", filename, str); } } /** * Skip all comments from the current pos resolving nested and multiline comments * @param parser * @return */ static bool ucl_skip_comments (struct ucl_parser *parser) { struct ucl_chunk *chunk = parser->chunks; const unsigned char *p; int comments_nested = 0; bool quoted = false; p = chunk->pos; start: if (chunk->remain > 0 && *p == '#') { if (parser->state != UCL_STATE_SCOMMENT && parser->state != UCL_STATE_MCOMMENT) { while (p < chunk->end) { if (*p == '\n') { ucl_chunk_skipc (chunk, p); goto start; } ucl_chunk_skipc (chunk, p); } } } else if (chunk->remain >= 2 && *p == '/') { if (p[1] == '*') { ucl_chunk_skipc (chunk, p); comments_nested ++; ucl_chunk_skipc (chunk, p); while (p < chunk->end) { if (*p == '"' && *(p - 1) != '\\') { quoted = !quoted; } if (!quoted) { if (*p == '*') { ucl_chunk_skipc (chunk, p); if (*p == '/') { comments_nested --; if (comments_nested == 0) { ucl_chunk_skipc (chunk, p); goto start; } } ucl_chunk_skipc (chunk, p); } else if (p[0] == '/' && chunk->remain >= 2 && p[1] == '*') { comments_nested ++; ucl_chunk_skipc (chunk, p); ucl_chunk_skipc (chunk, p); continue; } } ucl_chunk_skipc (chunk, p); } if (comments_nested != 0) { ucl_set_err (parser, UCL_ENESTED, "unfinished multiline comment", &parser->err); return false; } } } return true; } /** * Return multiplier for a character * @param c multiplier character * @param is_bytes if true use 1024 multiplier * @return multiplier */ static inline unsigned long ucl_lex_num_multiplier (const unsigned char c, bool is_bytes) { const struct { char c; long mult_normal; long mult_bytes; } multipliers[] = { {'m', 1000 * 1000, 1024 * 1024}, {'k', 1000, 1024}, {'g', 1000 * 1000 * 1000, 1024 * 1024 * 1024} }; int i; for (i = 0; i < 3; i ++) { if (tolower (c) == multipliers[i].c) { if (is_bytes) { return multipliers[i].mult_bytes; } return multipliers[i].mult_normal; } } return 1; } /** * Return multiplier for time scaling * @param c * @return */ static inline double ucl_lex_time_multiplier (const unsigned char c) { const struct { char c; double mult; } multipliers[] = { {'m', 60}, {'h', 60 * 60}, {'d', 60 * 60 * 24}, {'w', 60 * 60 * 24 * 7}, {'y', 60 * 60 * 24 * 7 * 365} }; int i; for (i = 0; i < 5; i ++) { if (tolower (c) == multipliers[i].c) { return multipliers[i].mult; } } return 1; } /** * Return true if a character is a end of an atom * @param c * @return */ static inline bool ucl_lex_is_atom_end (const unsigned char c) { return ucl_test_character (c, UCL_CHARACTER_VALUE_END); } static inline bool ucl_lex_is_comment (const unsigned char c1, const unsigned char c2) { if (c1 == '/') { if (c2 == '*') { return true; } } else if (c1 == '#') { return true; } return false; } /** * Check variable found * @param parser * @param ptr * @param remain * @param out_len * @param strict * @param found * @return */ static inline const char * ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t remain, size_t *out_len, bool strict, bool *found) { struct ucl_variable *var; unsigned char *dst; size_t dstlen; bool need_free = false; LL_FOREACH (parser->variables, var) { if (strict) { if (remain == var->var_len) { if (memcmp (ptr, var->var, var->var_len) == 0) { *out_len += var->value_len; *found = true; return (ptr + var->var_len); } } } else { if (remain >= var->var_len) { if (memcmp (ptr, var->var, var->var_len) == 0) { *out_len += var->value_len; *found = true; return (ptr + var->var_len); } } } } /* XXX: can only handle ${VAR} */ if (!(*found) && parser->var_handler != NULL && strict) { /* Call generic handler */ if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free, parser->var_data)) { *found = true; if (need_free) { free (dst); } return (ptr + remain); } } return ptr; } /** * Check for a variable in a given string * @param parser * @param ptr * @param remain * @param out_len * @param vars_found * @return */ static const char * ucl_check_variable (struct ucl_parser *parser, const char *ptr, size_t remain, size_t *out_len, bool *vars_found) { const char *p, *end, *ret = ptr; bool found = false; if (*ptr == '{') { /* We need to match the variable enclosed in braces */ p = ptr + 1; end = ptr + remain; while (p < end) { if (*p == '}') { ret = ucl_check_variable_safe (parser, ptr + 1, p - ptr - 1, out_len, true, &found); if (found) { /* {} must be excluded actually */ ret ++; if (!*vars_found) { *vars_found = true; } } else { *out_len += 2; } break; } p ++; } } else if (*ptr != '$') { /* Not count escaped dollar sign */ ret = ucl_check_variable_safe (parser, ptr, remain, out_len, false, &found); if (found && !*vars_found) { *vars_found = true; } if (!found) { (*out_len) ++; } } else { ret ++; (*out_len) ++; } return ret; } /** * Expand a single variable * @param parser * @param ptr * @param remain * @param dest * @return */ static const char * ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr, size_t remain, unsigned char **dest) { unsigned char *d = *dest, *dst; const char *p = ptr + 1, *ret; struct ucl_variable *var; size_t dstlen; bool need_free = false; bool found = false; bool strict = false; ret = ptr + 1; remain --; if (*p == '$') { *d++ = *p++; *dest = d; return p; } else if (*p == '{') { p ++; strict = true; ret += 2; remain -= 2; } LL_FOREACH (parser->variables, var) { if (remain >= var->var_len) { if (memcmp (p, var->var, var->var_len) == 0) { memcpy (d, var->value, var->value_len); ret += var->var_len; d += var->value_len; found = true; break; } } } if (!found) { if (strict && parser->var_handler != NULL) { if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free, parser->var_data)) { memcpy (d, dst, dstlen); ret += dstlen; d += remain; found = true; } } /* Leave variable as is */ if (!found) { if (strict) { /* Copy '${' */ memcpy (d, ptr, 2); d += 2; ret --; } else { memcpy (d, ptr, 1); d ++; } } } *dest = d; return ret; } /** * Expand variables in string * @param parser * @param dst * @param src * @param in_len * @return */ static ssize_t ucl_expand_variable (struct ucl_parser *parser, unsigned char **dst, const char *src, size_t in_len) { const char *p, *end = src + in_len; unsigned char *d; size_t out_len = 0; bool vars_found = false; p = src; while (p != end) { if (*p == '$') { p = ucl_check_variable (parser, p + 1, end - p - 1, &out_len, &vars_found); } else { p ++; out_len ++; } } if (!vars_found) { /* Trivial case */ *dst = NULL; return in_len; } *dst = UCL_ALLOC (out_len + 1); if (*dst == NULL) { return in_len; } d = *dst; p = src; while (p != end) { if (*p == '$') { p = ucl_expand_single_variable (parser, p, end - p, &d); } else { *d++ = *p++; } } *d = '\0'; return out_len; } /** * Store or copy pointer to the trash stack * @param parser parser object * @param src src string * @param dst destination buffer (trash stack pointer) * @param dst_const const destination pointer (e.g. value of object) * @param in_len input length * @param need_unescape need to unescape source (and copy it) * @param need_lowercase need to lowercase value (and copy) * @param need_expand need to expand variables (and copy as well) * @return output length (excluding \0 symbol) */ static inline ssize_t ucl_copy_or_store_ptr (struct ucl_parser *parser, const unsigned char *src, unsigned char **dst, const char **dst_const, size_t in_len, bool need_unescape, bool need_lowercase, bool need_expand) { ssize_t ret = -1, tret; unsigned char *tmp; if (need_unescape || need_lowercase || (need_expand && parser->variables != NULL) || !(parser->flags & UCL_PARSER_ZEROCOPY)) { /* Copy string */ *dst = UCL_ALLOC (in_len + 1); if (*dst == NULL) { ucl_set_err (parser, 0, "cannot allocate memory for a string", &parser->err); return false; } if (need_lowercase) { ret = ucl_strlcpy_tolower (*dst, src, in_len + 1); } else { ret = ucl_strlcpy_unsafe (*dst, src, in_len + 1); } if (need_unescape) { ret = ucl_unescape_json_string (*dst, ret); } if (need_expand) { tmp = *dst; tret = ret; ret = ucl_expand_variable (parser, dst, tmp, ret); if (*dst == NULL) { /* Nothing to expand */ *dst = tmp; ret = tret; } else { /* Free unexpanded value */ UCL_FREE (in_len + 1, tmp); } } *dst_const = *dst; } else { *dst_const = src; ret = in_len; } return ret; } /** * Create and append an object at the specified level * @param parser * @param is_array * @param level * @return */ static inline ucl_object_t * ucl_add_parser_stack (ucl_object_t *obj, struct ucl_parser *parser, bool is_array, int level) { struct ucl_stack *st; if (!is_array) { if (obj == NULL) { obj = ucl_object_new_full (UCL_OBJECT, parser->chunks->priority); } else { obj->type = UCL_OBJECT; } - obj->value.ov = ucl_hash_create (); + obj->value.ov = ucl_hash_create (parser->flags & UCL_PARSER_KEY_LOWERCASE); parser->state = UCL_STATE_KEY; } else { if (obj == NULL) { obj = ucl_object_new_full (UCL_ARRAY, parser->chunks->priority); } else { obj->type = UCL_ARRAY; } parser->state = UCL_STATE_VALUE; } st = UCL_ALLOC (sizeof (struct ucl_stack)); if (st == NULL) { ucl_set_err (parser, 0, "cannot allocate memory for an object", &parser->err); ucl_object_unref (obj); return NULL; } st->obj = obj; st->level = level; LL_PREPEND (parser->stack, st); parser->cur_obj = obj; return obj; } int ucl_maybe_parse_number (ucl_object_t *obj, const char *start, const char *end, const char **pos, bool allow_double, bool number_bytes, bool allow_time) { const char *p = start, *c = start; char *endptr; bool got_dot = false, got_exp = false, need_double = false, is_time = false, valid_start = false, is_hex = false, is_neg = false; double dv = 0; int64_t lv = 0; if (*p == '-') { is_neg = true; c ++; p ++; } while (p < end) { if (is_hex && isxdigit (*p)) { p ++; } else if (isdigit (*p)) { valid_start = true; p ++; } else if (!is_hex && (*p == 'x' || *p == 'X')) { is_hex = true; allow_double = false; c = p + 1; } else if (allow_double) { if (p == c) { /* Empty digits sequence, not a number */ *pos = start; return EINVAL; } else if (*p == '.') { if (got_dot) { /* Double dots, not a number */ *pos = start; return EINVAL; } else { got_dot = true; need_double = true; p ++; } } else if (*p == 'e' || *p == 'E') { if (got_exp) { /* Double exp, not a number */ *pos = start; return EINVAL; } else { got_exp = true; need_double = true; p ++; if (p >= end) { *pos = start; return EINVAL; } if (!isdigit (*p) && *p != '+' && *p != '-') { /* Wrong exponent sign */ *pos = start; return EINVAL; } else { p ++; } } } else { /* Got the end of the number, need to check */ break; } } else { break; } } if (!valid_start) { *pos = start; return EINVAL; } errno = 0; if (need_double) { dv = strtod (c, &endptr); } else { if (is_hex) { lv = strtoimax (c, &endptr, 16); } else { lv = strtoimax (c, &endptr, 10); } } if (errno == ERANGE) { *pos = start; return ERANGE; } /* Now check endptr */ if (endptr == NULL || ucl_lex_is_atom_end (*endptr) || *endptr == '\0') { p = endptr; goto set_obj; } if (endptr < end && endptr != start) { p = endptr; switch (*p) { case 'm': case 'M': case 'g': case 'G': case 'k': case 'K': if (end - p >= 2) { if (p[1] == 's' || p[1] == 'S') { /* Milliseconds */ if (!need_double) { need_double = true; dv = lv; } is_time = true; if (p[0] == 'm' || p[0] == 'M') { dv /= 1000.; } else { dv *= ucl_lex_num_multiplier (*p, false); } p += 2; goto set_obj; } else if (number_bytes || (p[1] == 'b' || p[1] == 'B')) { /* Bytes */ if (need_double) { need_double = false; lv = dv; } lv *= ucl_lex_num_multiplier (*p, true); p += 2; goto set_obj; } else if (ucl_lex_is_atom_end (p[1])) { if (need_double) { dv *= ucl_lex_num_multiplier (*p, false); } else { lv *= ucl_lex_num_multiplier (*p, number_bytes); } p ++; goto set_obj; } else if (allow_time && end - p >= 3) { if (tolower (p[0]) == 'm' && tolower (p[1]) == 'i' && tolower (p[2]) == 'n') { /* Minutes */ if (!need_double) { need_double = true; dv = lv; } is_time = true; dv *= 60.; p += 3; goto set_obj; } } } else { if (need_double) { dv *= ucl_lex_num_multiplier (*p, false); } else { lv *= ucl_lex_num_multiplier (*p, number_bytes); } p ++; goto set_obj; } break; case 'S': case 's': if (allow_time && (p == end - 1 || ucl_lex_is_atom_end (p[1]))) { if (!need_double) { need_double = true; dv = lv; } p ++; is_time = true; goto set_obj; } break; case 'h': case 'H': case 'd': case 'D': case 'w': case 'W': case 'Y': case 'y': if (allow_time && (p == end - 1 || ucl_lex_is_atom_end (p[1]))) { if (!need_double) { need_double = true; dv = lv; } is_time = true; dv *= ucl_lex_time_multiplier (*p); p ++; goto set_obj; } break; case '\t': case ' ': while (p < end && ucl_test_character(*p, UCL_CHARACTER_WHITESPACE)) { p++; } if (ucl_lex_is_atom_end(*p)) goto set_obj; break; } } else if (endptr == end) { /* Just a number at the end of chunk */ p = endptr; goto set_obj; } *pos = c; return EINVAL; set_obj: if (allow_double && (need_double || is_time)) { if (!is_time) { obj->type = UCL_FLOAT; } else { obj->type = UCL_TIME; } obj->value.dv = is_neg ? (-dv) : dv; } else { obj->type = UCL_INT; obj->value.iv = is_neg ? (-lv) : lv; } *pos = p; return 0; } /** * Parse possible number * @param parser * @param chunk * @return true if a number has been parsed */ static bool ucl_lex_number (struct ucl_parser *parser, struct ucl_chunk *chunk, ucl_object_t *obj) { const unsigned char *pos; int ret; ret = ucl_maybe_parse_number (obj, chunk->pos, chunk->end, (const char **)&pos, true, false, ((parser->flags & UCL_PARSER_NO_TIME) == 0)); if (ret == 0) { chunk->remain -= pos - chunk->pos; chunk->column += pos - chunk->pos; chunk->pos = pos; return true; } else if (ret == ERANGE) { ucl_set_err (parser, ERANGE, "numeric value out of range", &parser->err); } return false; } /** * Parse quoted string with possible escapes * @param parser * @param chunk * @return true if a string has been parsed */ static bool ucl_lex_json_string (struct ucl_parser *parser, struct ucl_chunk *chunk, bool *need_unescape, bool *ucl_escape, bool *var_expand) { const unsigned char *p = chunk->pos; unsigned char c; int i; while (p < chunk->end) { c = *p; if (c < 0x1F) { /* Unmasked control character */ if (c == '\n') { ucl_set_err (parser, UCL_ESYNTAX, "unexpected newline", &parser->err); } else { ucl_set_err (parser, UCL_ESYNTAX, "unexpected control character", &parser->err); } return false; } else if (c == '\\') { ucl_chunk_skipc (chunk, p); c = *p; if (p >= chunk->end) { ucl_set_err (parser, UCL_ESYNTAX, "unfinished escape character", &parser->err); return false; } else if (ucl_test_character (c, UCL_CHARACTER_ESCAPE)) { if (c == 'u') { ucl_chunk_skipc (chunk, p); for (i = 0; i < 4 && p < chunk->end; i ++) { if (!isxdigit (*p)) { ucl_set_err (parser, UCL_ESYNTAX, "invalid utf escape", &parser->err); return false; } ucl_chunk_skipc (chunk, p); } if (p >= chunk->end) { ucl_set_err (parser, UCL_ESYNTAX, "unfinished escape character", &parser->err); return false; } } else { ucl_chunk_skipc (chunk, p); } } *need_unescape = true; *ucl_escape = true; continue; } else if (c == '"') { ucl_chunk_skipc (chunk, p); return true; } else if (ucl_test_character (c, UCL_CHARACTER_UCL_UNSAFE)) { *ucl_escape = true; } else if (c == '$') { *var_expand = true; } ucl_chunk_skipc (chunk, p); } ucl_set_err (parser, UCL_ESYNTAX, "no quote at the end of json string", &parser->err); return false; } static void ucl_parser_append_elt (struct ucl_parser *parser, ucl_hash_t *cont, ucl_object_t *top, ucl_object_t *elt) { ucl_object_t *nobj; if ((parser->flags & UCL_PARSER_NO_IMPLICIT_ARRAYS) == 0) { /* Implicit array */ top->flags |= UCL_OBJECT_MULTIVALUE; DL_APPEND (top, elt); } else { if ((top->flags & UCL_OBJECT_MULTIVALUE) != 0) { /* Just add to the explicit array */ - DL_APPEND (top->value.av, elt); + ucl_array_append (top, elt); } else { /* Convert to an array */ ucl_hash_delete (cont, top); nobj = ucl_object_typed_new (UCL_ARRAY); nobj->key = top->key; nobj->keylen = top->keylen; nobj->flags |= UCL_OBJECT_MULTIVALUE; - DL_APPEND (nobj->value.av, top); - DL_APPEND (nobj->value.av, elt); + ucl_array_append (nobj, top); + ucl_array_append (nobj, elt); ucl_hash_insert (cont, nobj, nobj->key, nobj->keylen); } } } /** * Parse a key in an object * @param parser * @param chunk * @return true if a key has been parsed */ static bool ucl_parse_key (struct ucl_parser *parser, struct ucl_chunk *chunk, bool *next_key, bool *end_of_object) { const unsigned char *p, *c = NULL, *end, *t; const char *key = NULL; bool got_quote = false, got_eq = false, got_semicolon = false, need_unescape = false, ucl_escape = false, var_expand = false, got_content = false, got_sep = false; ucl_object_t *nobj, *tobj; ucl_hash_t *container; ssize_t keylen; p = chunk->pos; if (*p == '.') { /* It is macro actually */ ucl_chunk_skipc (chunk, p); parser->prev_state = parser->state; parser->state = UCL_STATE_MACRO_NAME; + *end_of_object = false; return true; } while (p < chunk->end) { /* * A key must start with alpha, number, '/' or '_' and end with space character */ if (c == NULL) { if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) { if (!ucl_skip_comments (parser)) { return false; } p = chunk->pos; } else if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) { ucl_chunk_skipc (chunk, p); } else if (ucl_test_character (*p, UCL_CHARACTER_KEY_START)) { /* The first symbol */ c = p; ucl_chunk_skipc (chunk, p); got_content = true; } else if (*p == '"') { /* JSON style key */ c = p + 1; got_quote = true; got_content = true; ucl_chunk_skipc (chunk, p); } else if (*p == '}') { /* We have actually end of an object */ *end_of_object = true; return true; } else if (*p == '.') { ucl_chunk_skipc (chunk, p); parser->prev_state = parser->state; parser->state = UCL_STATE_MACRO_NAME; return true; } else { /* Invalid identifier */ ucl_set_err (parser, UCL_ESYNTAX, "key must begin with a letter", &parser->err); return false; } } else { /* Parse the body of a key */ if (!got_quote) { if (ucl_test_character (*p, UCL_CHARACTER_KEY)) { got_content = true; ucl_chunk_skipc (chunk, p); } else if (ucl_test_character (*p, UCL_CHARACTER_KEY_SEP)) { end = p; break; } else { ucl_set_err (parser, UCL_ESYNTAX, "invalid character in a key", &parser->err); return false; } } else { /* We need to parse json like quoted string */ if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) { return false; } /* Always escape keys obtained via json */ end = chunk->pos - 1; p = chunk->pos; break; } } } if (p >= chunk->end && got_content) { ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err); return false; } else if (!got_content) { return true; } *end_of_object = false; /* We are now at the end of the key, need to parse the rest */ while (p < chunk->end) { if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) { ucl_chunk_skipc (chunk, p); } else if (*p == '=') { if (!got_eq && !got_semicolon) { ucl_chunk_skipc (chunk, p); got_eq = true; } else { ucl_set_err (parser, UCL_ESYNTAX, "unexpected '=' character", &parser->err); return false; } } else if (*p == ':') { if (!got_eq && !got_semicolon) { ucl_chunk_skipc (chunk, p); got_semicolon = true; } else { ucl_set_err (parser, UCL_ESYNTAX, "unexpected ':' character", &parser->err); return false; } } else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) { /* Check for comment */ if (!ucl_skip_comments (parser)) { return false; } p = chunk->pos; } else { /* Start value */ break; } } if (p >= chunk->end && got_content) { ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err); return false; } got_sep = got_semicolon || got_eq; if (!got_sep) { /* * Maybe we have more keys nested, so search for termination character. * Possible choices: * 1) key1 key2 ... keyN [:=] value <- we treat that as error * 2) key1 ... keyN {} or [] <- we treat that as nested objects * 3) key1 value[;,\n] <- we treat that as linear object */ t = p; *next_key = false; while (ucl_test_character (*t, UCL_CHARACTER_WHITESPACE)) { t ++; } /* Check first non-space character after a key */ if (*t != '{' && *t != '[') { while (t < chunk->end) { if (*t == ',' || *t == ';' || *t == '\n' || *t == '\r') { break; } else if (*t == '{' || *t == '[') { *next_key = true; break; } t ++; } } } /* Create a new object */ nobj = ucl_object_new_full (UCL_NULL, parser->chunks->priority); keylen = ucl_copy_or_store_ptr (parser, c, &nobj->trash_stack[UCL_TRASH_KEY], &key, end - c, need_unescape, parser->flags & UCL_PARSER_KEY_LOWERCASE, false); if (keylen == -1) { ucl_object_unref (nobj); return false; } else if (keylen == 0) { ucl_set_err (parser, UCL_ESYNTAX, "empty keys are not allowed", &parser->err); ucl_object_unref (nobj); return false; } container = parser->stack->obj->value.ov; nobj->key = key; nobj->keylen = keylen; tobj = __DECONST (ucl_object_t *, ucl_hash_search_obj (container, nobj)); if (tobj == NULL) { - container = ucl_hash_insert_object (container, nobj); + container = ucl_hash_insert_object (container, nobj, + parser->flags & UCL_PARSER_KEY_LOWERCASE); nobj->prev = nobj; nobj->next = NULL; parser->stack->obj->len ++; } else { /* * The logic here is the following: * * - if we have two objects with the same priority, then we form an * implicit or explicit array * - if a new object has bigger priority, then we overwrite an old one * - if a new object has lower priority, then we ignore it */ unsigned priold = ucl_object_get_priority (tobj), prinew = ucl_object_get_priority (nobj); if (priold == prinew) { ucl_parser_append_elt (parser, container, tobj, nobj); } else if (priold > prinew) { ucl_object_unref (nobj); return true; } else { ucl_hash_replace (container, tobj, nobj); ucl_object_unref (tobj); } } if (ucl_escape) { nobj->flags |= UCL_OBJECT_NEED_KEY_ESCAPE; } parser->stack->obj->value.ov = container; parser->cur_obj = nobj; return true; } /** * Parse a cl string * @param parser * @param chunk * @return true if a key has been parsed */ static bool ucl_parse_string_value (struct ucl_parser *parser, struct ucl_chunk *chunk, bool *var_expand, bool *need_unescape) { const unsigned char *p; enum { UCL_BRACE_ROUND = 0, UCL_BRACE_SQUARE, UCL_BRACE_FIGURE }; int braces[3][2] = {{0, 0}, {0, 0}, {0, 0}}; p = chunk->pos; while (p < chunk->end) { /* Skip pairs of figure braces */ if (*p == '{') { braces[UCL_BRACE_FIGURE][0] ++; } else if (*p == '}') { braces[UCL_BRACE_FIGURE][1] ++; if (braces[UCL_BRACE_FIGURE][1] <= braces[UCL_BRACE_FIGURE][0]) { /* This is not a termination symbol, continue */ ucl_chunk_skipc (chunk, p); continue; } } /* Skip pairs of square braces */ else if (*p == '[') { braces[UCL_BRACE_SQUARE][0] ++; } else if (*p == ']') { braces[UCL_BRACE_SQUARE][1] ++; if (braces[UCL_BRACE_SQUARE][1] <= braces[UCL_BRACE_SQUARE][0]) { /* This is not a termination symbol, continue */ ucl_chunk_skipc (chunk, p); continue; } } else if (*p == '$') { *var_expand = true; } else if (*p == '\\') { *need_unescape = true; ucl_chunk_skipc (chunk, p); if (p < chunk->end) { ucl_chunk_skipc (chunk, p); } continue; } if (ucl_lex_is_atom_end (*p) || (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) { break; } ucl_chunk_skipc (chunk, p); } return true; } /** * Parse multiline string ending with \n{term}\n * @param parser * @param chunk * @param term * @param term_len * @return size of multiline string or 0 in case of error */ static int ucl_parse_multiline_string (struct ucl_parser *parser, struct ucl_chunk *chunk, const unsigned char *term, int term_len, unsigned char const **beg, bool *var_expand) { const unsigned char *p, *c, *tend; bool newline = false; int len = 0; p = chunk->pos; c = p; while (p < chunk->end) { if (newline) { if (chunk->end - p < term_len) { return 0; } else if (memcmp (p, term, term_len) == 0) { tend = p + term_len; if (*tend != '\n' && *tend != ';' && *tend != ',') { /* Incomplete terminator */ ucl_chunk_skipc (chunk, p); continue; } len = p - c; chunk->remain -= term_len; chunk->pos = p + term_len; chunk->column = term_len; *beg = c; break; } } if (*p == '\n') { newline = true; } else { if (*p == '$') { *var_expand = true; } newline = false; } ucl_chunk_skipc (chunk, p); } return len; } static ucl_object_t* ucl_get_value_object (struct ucl_parser *parser) { ucl_object_t *t, *obj = NULL; + if (parser == NULL || parser->stack == NULL || parser->stack->obj == NULL) { + return NULL; + } + if (parser->stack->obj->type == UCL_ARRAY) { /* Object must be allocated */ obj = ucl_object_new_full (UCL_NULL, parser->chunks->priority); - t = parser->stack->obj->value.av; - DL_APPEND (t, obj); + t = parser->stack->obj; + ucl_array_append (t, obj); parser->cur_obj = obj; - parser->stack->obj->value.av = t; - parser->stack->obj->len ++; } else { /* Object has been already allocated */ obj = parser->cur_obj; } return obj; } /** * Handle value data * @param parser * @param chunk * @return */ static bool ucl_parse_value (struct ucl_parser *parser, struct ucl_chunk *chunk) { const unsigned char *p, *c; ucl_object_t *obj = NULL; unsigned int stripped_spaces; int str_len; bool need_unescape = false, ucl_escape = false, var_expand = false; p = chunk->pos; /* Skip any spaces and comments */ if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) || (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) { while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) { ucl_chunk_skipc (chunk, p); } if (!ucl_skip_comments (parser)) { return false; } p = chunk->pos; } while (p < chunk->end) { c = p; switch (*p) { case '"': obj = ucl_get_value_object (parser); ucl_chunk_skipc (chunk, p); if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) { return false; } str_len = chunk->pos - c - 2; obj->type = UCL_STRING; if ((str_len = ucl_copy_or_store_ptr (parser, c + 1, &obj->trash_stack[UCL_TRASH_VALUE], &obj->value.sv, str_len, need_unescape, false, var_expand)) == -1) { return false; } obj->len = str_len; parser->state = UCL_STATE_AFTER_VALUE; p = chunk->pos; return true; break; case '{': obj = ucl_get_value_object (parser); /* We have a new object */ obj = ucl_add_parser_stack (obj, parser, false, parser->stack->level); if (obj == NULL) { return false; } ucl_chunk_skipc (chunk, p); return true; break; case '[': obj = ucl_get_value_object (parser); /* We have a new array */ obj = ucl_add_parser_stack (obj, parser, true, parser->stack->level); if (obj == NULL) { return false; } ucl_chunk_skipc (chunk, p); return true; break; case ']': /* We have the array ending */ if (parser->stack && parser->stack->obj->type == UCL_ARRAY) { parser->state = UCL_STATE_AFTER_VALUE; return true; } else { goto parse_string; } break; case '<': obj = ucl_get_value_object (parser); /* We have something like multiline value, which must be <<[A-Z]+\n */ if (chunk->end - p > 3) { if (memcmp (p, "<<", 2) == 0) { p += 2; /* We allow only uppercase characters in multiline definitions */ while (p < chunk->end && *p >= 'A' && *p <= 'Z') { p ++; } if (*p =='\n') { /* Set chunk positions and start multiline parsing */ c += 2; chunk->remain -= p - c; chunk->pos = p + 1; chunk->column = 0; chunk->line ++; if ((str_len = ucl_parse_multiline_string (parser, chunk, c, p - c, &c, &var_expand)) == 0) { ucl_set_err (parser, UCL_ESYNTAX, "unterminated multiline value", &parser->err); return false; } obj->type = UCL_STRING; if ((str_len = ucl_copy_or_store_ptr (parser, c, &obj->trash_stack[UCL_TRASH_VALUE], &obj->value.sv, str_len - 1, false, false, var_expand)) == -1) { return false; } obj->len = str_len; parser->state = UCL_STATE_AFTER_VALUE; return true; } } } /* Fallback to ordinary strings */ default: parse_string: if (obj == NULL) { obj = ucl_get_value_object (parser); } /* Parse atom */ if (ucl_test_character (*p, UCL_CHARACTER_VALUE_DIGIT_START)) { if (!ucl_lex_number (parser, chunk, obj)) { if (parser->state == UCL_STATE_ERROR) { return false; } } else { parser->state = UCL_STATE_AFTER_VALUE; return true; } /* Fallback to normal string */ } if (!ucl_parse_string_value (parser, chunk, &var_expand, &need_unescape)) { return false; } /* Cut trailing spaces */ stripped_spaces = 0; while (ucl_test_character (*(chunk->pos - 1 - stripped_spaces), UCL_CHARACTER_WHITESPACE)) { stripped_spaces ++; } str_len = chunk->pos - c - stripped_spaces; if (str_len <= 0) { ucl_set_err (parser, 0, "string value must not be empty", &parser->err); return false; } else if (str_len == 4 && memcmp (c, "null", 4) == 0) { obj->len = 0; obj->type = UCL_NULL; } else if (!ucl_maybe_parse_boolean (obj, c, str_len)) { obj->type = UCL_STRING; if ((str_len = ucl_copy_or_store_ptr (parser, c, &obj->trash_stack[UCL_TRASH_VALUE], &obj->value.sv, str_len, need_unescape, false, var_expand)) == -1) { return false; } obj->len = str_len; } parser->state = UCL_STATE_AFTER_VALUE; p = chunk->pos; return true; break; } } return true; } /** * Handle after value data * @param parser * @param chunk * @return */ static bool ucl_parse_after_value (struct ucl_parser *parser, struct ucl_chunk *chunk) { const unsigned char *p; bool got_sep = false; struct ucl_stack *st; p = chunk->pos; while (p < chunk->end) { if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) { /* Skip whitespaces */ ucl_chunk_skipc (chunk, p); } else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) { /* Skip comment */ if (!ucl_skip_comments (parser)) { return false; } /* Treat comment as a separator */ got_sep = true; p = chunk->pos; } else if (ucl_test_character (*p, UCL_CHARACTER_VALUE_END)) { if (*p == '}' || *p == ']') { if (parser->stack == NULL) { ucl_set_err (parser, UCL_ESYNTAX, "end of array or object detected without corresponding start", &parser->err); return false; } if ((*p == '}' && parser->stack->obj->type == UCL_OBJECT) || (*p == ']' && parser->stack->obj->type == UCL_ARRAY)) { /* Pop all nested objects from a stack */ st = parser->stack; parser->stack = st->next; UCL_FREE (sizeof (struct ucl_stack), st); while (parser->stack != NULL) { st = parser->stack; if (st->next == NULL || st->next->level == st->level) { break; } parser->stack = st->next; UCL_FREE (sizeof (struct ucl_stack), st); } } else { ucl_set_err (parser, UCL_ESYNTAX, "unexpected terminating symbol detected", &parser->err); return false; } if (parser->stack == NULL) { /* Ignore everything after a top object */ return true; } else { ucl_chunk_skipc (chunk, p); } got_sep = true; } else { /* Got a separator */ got_sep = true; ucl_chunk_skipc (chunk, p); } } else { /* Anything else */ if (!got_sep) { ucl_set_err (parser, UCL_ESYNTAX, "delimiter is missing", &parser->err); return false; } return true; } } return true; } /** * Handle macro data * @param parser * @param chunk * @return */ static bool ucl_parse_macro_value (struct ucl_parser *parser, struct ucl_chunk *chunk, struct ucl_macro *macro, unsigned char const **macro_start, size_t *macro_len) { const unsigned char *p, *c; bool need_unescape = false, ucl_escape = false, var_expand = false; p = chunk->pos; switch (*p) { case '"': /* We have macro value encoded in quotes */ c = p; ucl_chunk_skipc (chunk, p); if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) { return false; } *macro_start = c + 1; *macro_len = chunk->pos - c - 2; p = chunk->pos; break; case '{': /* We got a multiline macro body */ ucl_chunk_skipc (chunk, p); /* Skip spaces at the beginning */ while (p < chunk->end) { if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) { ucl_chunk_skipc (chunk, p); } else { break; } } c = p; while (p < chunk->end) { if (*p == '}') { break; } ucl_chunk_skipc (chunk, p); } *macro_start = c; *macro_len = p - c; ucl_chunk_skipc (chunk, p); break; default: /* Macro is not enclosed in quotes or braces */ c = p; while (p < chunk->end) { if (ucl_lex_is_atom_end (*p)) { break; } ucl_chunk_skipc (chunk, p); } *macro_start = c; *macro_len = p - c; break; } /* We are at the end of a macro */ /* Skip ';' and space characters and return to previous state */ while (p < chunk->end) { if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) && *p != ';') { break; } ucl_chunk_skipc (chunk, p); } return true; } /** * Parse macro arguments as UCL object * @param parser parser structure * @param chunk the current data chunk * @return */ static ucl_object_t * ucl_parse_macro_arguments (struct ucl_parser *parser, struct ucl_chunk *chunk) { ucl_object_t *res = NULL; struct ucl_parser *params_parser; int obraces = 1, ebraces = 0, state = 0; const unsigned char *p, *c; size_t args_len = 0; struct ucl_parser_saved_state saved; saved.column = chunk->column; saved.line = chunk->line; saved.pos = chunk->pos; saved.remain = chunk->remain; p = chunk->pos; if (*p != '(' || chunk->remain < 2) { return NULL; } /* Set begin and start */ ucl_chunk_skipc (chunk, p); c = p; while ((p) < (chunk)->end) { switch (state) { case 0: /* Parse symbols and check for '(', ')' and '"' */ if (*p == '(') { obraces ++; } else if (*p == ')') { ebraces ++; } else if (*p == '"') { state = 1; } /* Check pairing */ if (obraces == ebraces) { state = 99; } else { args_len ++; } /* Check overflow */ if (chunk->remain == 0) { goto restore_chunk; } ucl_chunk_skipc (chunk, p); break; case 1: /* We have quote character, so skip all but quotes */ if (*p == '"' && *(p - 1) != '\\') { state = 0; } if (chunk->remain == 0) { goto restore_chunk; } ucl_chunk_skipc (chunk, p); break; case 99: /* * We have read the full body of arguments, so we need to parse and set * object from that */ params_parser = ucl_parser_new (parser->flags); if (!ucl_parser_add_chunk (params_parser, c, args_len)) { ucl_set_err (parser, UCL_ESYNTAX, "macro arguments parsing error", &parser->err); } else { res = ucl_parser_get_object (params_parser); } ucl_parser_free (params_parser); return res; break; } } return res; restore_chunk: chunk->column = saved.column; chunk->line = saved.line; chunk->pos = saved.pos; chunk->remain = saved.remain; return NULL; } #define SKIP_SPACES_COMMENTS(parser, chunk, p) do { \ while ((p) < (chunk)->end) { \ if (!ucl_test_character (*(p), UCL_CHARACTER_WHITESPACE_UNSAFE)) { \ if ((chunk)->remain >= 2 && ucl_lex_is_comment ((p)[0], (p)[1])) { \ if (!ucl_skip_comments (parser)) { \ return false; \ } \ p = (chunk)->pos; \ } \ break; \ } \ ucl_chunk_skipc (chunk, p); \ } \ } while(0) /** * Handle the main states of rcl parser * @param parser parser structure * @param data the pointer to the beginning of a chunk * @param len the length of a chunk * @return true if chunk has been parsed and false in case of error */ static bool ucl_state_machine (struct ucl_parser *parser) { ucl_object_t *obj, *macro_args; struct ucl_chunk *chunk = parser->chunks; const unsigned char *p, *c = NULL, *macro_start = NULL; unsigned char *macro_escaped; size_t macro_len = 0; struct ucl_macro *macro = NULL; bool next_key = false, end_of_object = false, ret; if (parser->top_obj == NULL) { if (*chunk->pos == '[') { obj = ucl_add_parser_stack (NULL, parser, true, 0); } else { obj = ucl_add_parser_stack (NULL, parser, false, 0); } if (obj == NULL) { return false; } parser->top_obj = obj; parser->cur_obj = obj; parser->state = UCL_STATE_INIT; } p = chunk->pos; while (chunk->pos < chunk->end) { switch (parser->state) { case UCL_STATE_INIT: /* * At the init state we can either go to the parse array or object * if we got [ or { correspondingly or can just treat new data as * a key of newly created object */ if (!ucl_skip_comments (parser)) { parser->prev_state = parser->state; parser->state = UCL_STATE_ERROR; return false; } else { /* Skip any spaces */ while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) { ucl_chunk_skipc (chunk, p); } p = chunk->pos; if (*p == '[') { parser->state = UCL_STATE_VALUE; ucl_chunk_skipc (chunk, p); } else { parser->state = UCL_STATE_KEY; if (*p == '{') { ucl_chunk_skipc (chunk, p); } } } break; case UCL_STATE_KEY: /* Skip any spaces */ while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) { ucl_chunk_skipc (chunk, p); } if (*p == '}') { /* We have the end of an object */ parser->state = UCL_STATE_AFTER_VALUE; continue; } if (parser->stack == NULL) { /* No objects are on stack, but we want to parse a key */ ucl_set_err (parser, UCL_ESYNTAX, "top object is finished but the parser " "expects a key", &parser->err); parser->prev_state = parser->state; parser->state = UCL_STATE_ERROR; return false; } if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object)) { parser->prev_state = parser->state; parser->state = UCL_STATE_ERROR; return false; } if (end_of_object) { p = chunk->pos; parser->state = UCL_STATE_AFTER_VALUE; continue; } else if (parser->state != UCL_STATE_MACRO_NAME) { if (next_key && parser->stack->obj->type == UCL_OBJECT) { /* Parse more keys and nest objects accordingly */ obj = ucl_add_parser_stack (parser->cur_obj, parser, false, parser->stack->level + 1); if (obj == NULL) { return false; } } else { parser->state = UCL_STATE_VALUE; } } else { c = chunk->pos; } p = chunk->pos; break; case UCL_STATE_VALUE: /* We need to check what we do have */ if (!ucl_parse_value (parser, chunk)) { parser->prev_state = parser->state; parser->state = UCL_STATE_ERROR; return false; } /* State is set in ucl_parse_value call */ p = chunk->pos; break; case UCL_STATE_AFTER_VALUE: if (!ucl_parse_after_value (parser, chunk)) { parser->prev_state = parser->state; parser->state = UCL_STATE_ERROR; return false; } if (parser->stack != NULL) { if (parser->stack->obj->type == UCL_OBJECT) { parser->state = UCL_STATE_KEY; } else { /* Array */ parser->state = UCL_STATE_VALUE; } } else { /* Skip everything at the end */ return true; } p = chunk->pos; break; case UCL_STATE_MACRO_NAME: if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) && *p != '(') { ucl_chunk_skipc (chunk, p); } else if (p - c > 0) { /* We got macro name */ macro_len = (size_t)(p - c); HASH_FIND (hh, parser->macroes, c, macro_len, macro); if (macro == NULL) { ucl_create_err (&parser->err, "error on line %d at column %d: " "unknown macro: '%.*s', character: '%c'", chunk->line, chunk->column, (int)(p - c), c, *chunk->pos); parser->state = UCL_STATE_ERROR; return false; } /* Now we need to skip all spaces */ SKIP_SPACES_COMMENTS(parser, chunk, p); parser->state = UCL_STATE_MACRO; } break; case UCL_STATE_MACRO: if (*chunk->pos == '(') { macro_args = ucl_parse_macro_arguments (parser, chunk); p = chunk->pos; if (macro_args) { SKIP_SPACES_COMMENTS(parser, chunk, p); } } else { macro_args = NULL; } if (!ucl_parse_macro_value (parser, chunk, macro, ¯o_start, ¯o_len)) { parser->prev_state = parser->state; parser->state = UCL_STATE_ERROR; return false; } macro_len = ucl_expand_variable (parser, ¯o_escaped, macro_start, macro_len); parser->state = parser->prev_state; if (macro_escaped == NULL) { ret = macro->handler (macro_start, macro_len, macro_args, macro->ud); } else { ret = macro->handler (macro_escaped, macro_len, macro_args, macro->ud); UCL_FREE (macro_len + 1, macro_escaped); } p = chunk->pos; if (macro_args) { ucl_object_unref (macro_args); } if (!ret) { return false; } break; default: /* TODO: add all states */ ucl_set_err (parser, UCL_EINTERNAL, "internal error: parser is in an unknown state", &parser->err); parser->state = UCL_STATE_ERROR; return false; } } return true; } struct ucl_parser* ucl_parser_new (int flags) { struct ucl_parser *new; new = UCL_ALLOC (sizeof (struct ucl_parser)); if (new == NULL) { return NULL; } memset (new, 0, sizeof (struct ucl_parser)); ucl_parser_register_macro (new, "include", ucl_include_handler, new); ucl_parser_register_macro (new, "try_include", ucl_try_include_handler, new); ucl_parser_register_macro (new, "includes", ucl_includes_handler, new); new->flags = flags; /* Initial assumption about filevars */ ucl_parser_set_filevars (new, NULL, false); return new; } void ucl_parser_register_macro (struct ucl_parser *parser, const char *macro, ucl_macro_handler handler, void* ud) { struct ucl_macro *new; if (macro == NULL || handler == NULL) { return; } new = UCL_ALLOC (sizeof (struct ucl_macro)); if (new == NULL) { return; } memset (new, 0, sizeof (struct ucl_macro)); new->handler = handler; new->name = strdup (macro); new->ud = ud; HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new); } void ucl_parser_register_variable (struct ucl_parser *parser, const char *var, const char *value) { struct ucl_variable *new = NULL, *cur; if (var == NULL) { return; } /* Find whether a variable already exists */ LL_FOREACH (parser->variables, cur) { if (strcmp (cur->var, var) == 0) { new = cur; break; } } if (value == NULL) { if (new != NULL) { /* Remove variable */ DL_DELETE (parser->variables, new); free (new->var); free (new->value); UCL_FREE (sizeof (struct ucl_variable), new); } else { /* Do nothing */ return; } } else { if (new == NULL) { new = UCL_ALLOC (sizeof (struct ucl_variable)); if (new == NULL) { return; } memset (new, 0, sizeof (struct ucl_variable)); new->var = strdup (var); new->var_len = strlen (var); new->value = strdup (value); new->value_len = strlen (value); DL_APPEND (parser->variables, new); } else { free (new->value); new->value = strdup (value); new->value_len = strlen (value); } } } void ucl_parser_set_variables_handler (struct ucl_parser *parser, ucl_variable_handler handler, void *ud) { parser->var_handler = handler; parser->var_data = ud; } bool ucl_parser_add_chunk_priority (struct ucl_parser *parser, const unsigned char *data, size_t len, unsigned priority) { struct ucl_chunk *chunk; if (data == NULL) { ucl_create_err (&parser->err, "invalid chunk added"); return false; } if (len == 0) { parser->top_obj = ucl_object_new_full (UCL_OBJECT, priority); return true; } if (parser->state != UCL_STATE_ERROR) { chunk = UCL_ALLOC (sizeof (struct ucl_chunk)); if (chunk == NULL) { ucl_create_err (&parser->err, "cannot allocate chunk structure"); return false; } chunk->begin = data; chunk->remain = len; chunk->pos = chunk->begin; chunk->end = chunk->begin + len; chunk->line = 1; chunk->column = 0; chunk->priority = priority; LL_PREPEND (parser->chunks, chunk); parser->recursion ++; if (parser->recursion > UCL_MAX_RECURSION) { ucl_create_err (&parser->err, "maximum include nesting limit is reached: %d", parser->recursion); return false; } return ucl_state_machine (parser); } ucl_create_err (&parser->err, "a parser is in an invalid state"); return false; } bool ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data, size_t len) { return ucl_parser_add_chunk_priority (parser, data, len, 0); } bool ucl_parser_add_string (struct ucl_parser *parser, const char *data, size_t len) { if (data == NULL) { ucl_create_err (&parser->err, "invalid string added"); return false; } if (len == 0) { len = strlen (data); } return ucl_parser_add_chunk (parser, (const unsigned char *)data, len); } Index: vendor/libucl/dist/src/ucl_schema.c =================================================================== --- vendor/libucl/dist/src/ucl_schema.c (revision 279545) +++ vendor/libucl/dist/src/ucl_schema.c (revision 279546) @@ -1,1014 +1,1015 @@ /* * Copyright (c) 2014, Vsevolod Stakhov * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "ucl.h" #include "ucl_internal.h" #include "tree.h" #include "utlist.h" #ifdef HAVE_STDARG_H #include #endif #ifdef HAVE_STDIO_H #include #endif #ifdef HAVE_REGEX_H #include #endif #ifdef HAVE_MATH_H #include #endif static bool ucl_schema_validate (const ucl_object_t *schema, const ucl_object_t *obj, bool try_array, struct ucl_schema_error *err, const ucl_object_t *root); static bool ucl_string_to_type (const char *input, ucl_type_t *res) { if (strcasecmp (input, "object") == 0) { *res = UCL_OBJECT; } else if (strcasecmp (input, "array") == 0) { *res = UCL_ARRAY; } else if (strcasecmp (input, "integer") == 0) { *res = UCL_INT; } else if (strcasecmp (input, "number") == 0) { *res = UCL_FLOAT; } else if (strcasecmp (input, "string") == 0) { *res = UCL_STRING; } else if (strcasecmp (input, "boolean") == 0) { *res = UCL_BOOLEAN; } else if (strcasecmp (input, "null") == 0) { *res = UCL_NULL; } else { return false; } return true; } static const char * ucl_object_type_to_string (ucl_type_t type) { const char *res = "unknown"; switch (type) { case UCL_OBJECT: res = "object"; break; case UCL_ARRAY: res = "array"; break; case UCL_INT: res = "integer"; break; case UCL_FLOAT: case UCL_TIME: res = "number"; break; case UCL_STRING: res = "string"; break; case UCL_BOOLEAN: res = "boolean"; break; case UCL_NULL: case UCL_USERDATA: res = "null"; break; } return res; } /* * Create validation error */ static void ucl_schema_create_error (struct ucl_schema_error *err, enum ucl_schema_error_code code, const ucl_object_t *obj, const char *fmt, ...) { va_list va; if (err != NULL) { err->code = code; err->obj = obj; va_start (va, fmt); vsnprintf (err->msg, sizeof (err->msg), fmt, va); va_end (va); } } /* * Check whether we have a pattern specified */ static const ucl_object_t * ucl_schema_test_pattern (const ucl_object_t *obj, const char *pattern) { const ucl_object_t *res = NULL; #ifdef HAVE_REGEX_H regex_t reg; const ucl_object_t *elt; ucl_object_iter_t iter = NULL; if (regcomp (®, pattern, REG_EXTENDED | REG_NOSUB) == 0) { while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { if (regexec (®, ucl_object_key (elt), 0, NULL, 0) == 0) { res = elt; break; } } regfree (®); } #endif return res; } /* * Check dependencies for an object */ static bool ucl_schema_validate_dependencies (const ucl_object_t *deps, const ucl_object_t *obj, struct ucl_schema_error *err, const ucl_object_t *root) { const ucl_object_t *elt, *cur, *cur_dep; ucl_object_iter_t iter = NULL, piter; bool ret = true; while (ret && (cur = ucl_iterate_object (deps, &iter, true)) != NULL) { elt = ucl_object_find_key (obj, ucl_object_key (cur)); if (elt != NULL) { /* Need to check dependencies */ if (cur->type == UCL_ARRAY) { piter = NULL; while (ret && (cur_dep = ucl_iterate_object (cur, &piter, true)) != NULL) { if (ucl_object_find_key (obj, ucl_object_tostring (cur_dep)) == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_MISSING_DEPENDENCY, elt, "dependency %s is missing for key %s", ucl_object_tostring (cur_dep), ucl_object_key (cur)); ret = false; break; } } } else if (cur->type == UCL_OBJECT) { ret = ucl_schema_validate (cur, obj, true, err, root); } } } return ret; } /* * Validate object */ static bool ucl_schema_validate_object (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err, const ucl_object_t *root) { const ucl_object_t *elt, *prop, *found, *additional_schema = NULL, *required = NULL, *pat, *pelt; ucl_object_iter_t iter = NULL, piter = NULL; bool ret = true, allow_additional = true; int64_t minmax; while (ret && (elt = ucl_iterate_object (schema, &iter, true)) != NULL) { if (elt->type == UCL_OBJECT && strcmp (ucl_object_key (elt), "properties") == 0) { piter = NULL; while (ret && (prop = ucl_iterate_object (elt, &piter, true)) != NULL) { found = ucl_object_find_key (obj, ucl_object_key (prop)); if (found) { ret = ucl_schema_validate (prop, found, true, err, root); } } } else if (strcmp (ucl_object_key (elt), "additionalProperties") == 0) { if (elt->type == UCL_BOOLEAN) { if (!ucl_object_toboolean (elt)) { /* Deny additional fields completely */ allow_additional = false; } } else if (elt->type == UCL_OBJECT) { /* Define validator for additional fields */ additional_schema = elt; } else { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, elt, "additionalProperties attribute is invalid in schema"); ret = false; break; } } else if (strcmp (ucl_object_key (elt), "required") == 0) { if (elt->type == UCL_ARRAY) { required = elt; } else { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, elt, "required attribute is invalid in schema"); ret = false; break; } } else if (strcmp (ucl_object_key (elt), "minProperties") == 0 && ucl_object_toint_safe (elt, &minmax)) { if (obj->len < minmax) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "object has not enough properties: %u, minimum is: %u", obj->len, (unsigned)minmax); ret = false; break; } } else if (strcmp (ucl_object_key (elt), "maxProperties") == 0 && ucl_object_toint_safe (elt, &minmax)) { if (obj->len > minmax) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "object has too many properties: %u, maximum is: %u", obj->len, (unsigned)minmax); ret = false; break; } } else if (strcmp (ucl_object_key (elt), "patternProperties") == 0) { piter = NULL; while (ret && (prop = ucl_iterate_object (elt, &piter, true)) != NULL) { found = ucl_schema_test_pattern (obj, ucl_object_key (prop)); if (found) { ret = ucl_schema_validate (prop, found, true, err, root); } } } else if (elt->type == UCL_OBJECT && strcmp (ucl_object_key (elt), "dependencies") == 0) { ret = ucl_schema_validate_dependencies (elt, obj, err, root); } } if (ret) { /* Additional properties */ if (!allow_additional || additional_schema != NULL) { /* Check if we have exactly the same properties in schema and object */ iter = NULL; prop = ucl_object_find_key (schema, "properties"); while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { found = ucl_object_find_key (prop, ucl_object_key (elt)); if (found == NULL) { /* Try patternProperties */ piter = NULL; pat = ucl_object_find_key (schema, "patternProperties"); while ((pelt = ucl_iterate_object (pat, &piter, true)) != NULL) { found = ucl_schema_test_pattern (obj, ucl_object_key (pelt)); if (found != NULL) { break; } } } if (found == NULL) { if (!allow_additional) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "object has non-allowed property %s", ucl_object_key (elt)); ret = false; break; } else if (additional_schema != NULL) { if (!ucl_schema_validate (additional_schema, elt, true, err, root)) { ret = false; break; } } } } } /* Required properties */ if (required != NULL) { iter = NULL; while ((elt = ucl_iterate_object (required, &iter, true)) != NULL) { if (ucl_object_find_key (obj, ucl_object_tostring (elt)) == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_MISSING_PROPERTY, obj, "object has missing property %s", ucl_object_tostring (elt)); ret = false; break; } } } } return ret; } static bool ucl_schema_validate_number (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err) { const ucl_object_t *elt, *test; ucl_object_iter_t iter = NULL; bool ret = true, exclusive = false; double constraint, val; const double alpha = 1e-16; while (ret && (elt = ucl_iterate_object (schema, &iter, true)) != NULL) { if ((elt->type == UCL_FLOAT || elt->type == UCL_INT) && strcmp (ucl_object_key (elt), "multipleOf") == 0) { constraint = ucl_object_todouble (elt); if (constraint <= 0) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, elt, "multipleOf must be greater than zero"); ret = false; break; } val = ucl_object_todouble (obj); if (fabs (remainder (val, constraint)) > alpha) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "number %.4f is not multiple of %.4f, remainder is %.7f", val, constraint); ret = false; break; } } else if ((elt->type == UCL_FLOAT || elt->type == UCL_INT) && strcmp (ucl_object_key (elt), "maximum") == 0) { constraint = ucl_object_todouble (elt); test = ucl_object_find_key (schema, "exclusiveMaximum"); if (test && test->type == UCL_BOOLEAN) { exclusive = ucl_object_toboolean (test); } val = ucl_object_todouble (obj); if (val > constraint || (exclusive && val >= constraint)) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "number is too big: %.3f, maximum is: %.3f", val, constraint); ret = false; break; } } else if ((elt->type == UCL_FLOAT || elt->type == UCL_INT) && strcmp (ucl_object_key (elt), "minimum") == 0) { constraint = ucl_object_todouble (elt); test = ucl_object_find_key (schema, "exclusiveMinimum"); if (test && test->type == UCL_BOOLEAN) { exclusive = ucl_object_toboolean (test); } val = ucl_object_todouble (obj); if (val < constraint || (exclusive && val <= constraint)) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "number is too small: %.3f, minimum is: %.3f", val, constraint); ret = false; break; } } } return ret; } static bool ucl_schema_validate_string (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err) { const ucl_object_t *elt; ucl_object_iter_t iter = NULL; bool ret = true; int64_t constraint; #ifdef HAVE_REGEX_H regex_t re; #endif while (ret && (elt = ucl_iterate_object (schema, &iter, true)) != NULL) { if (elt->type == UCL_INT && strcmp (ucl_object_key (elt), "maxLength") == 0) { constraint = ucl_object_toint (elt); if (obj->len > constraint) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "string is too big: %.3f, maximum is: %.3f", obj->len, constraint); ret = false; break; } } else if (elt->type == UCL_INT && strcmp (ucl_object_key (elt), "minLength") == 0) { constraint = ucl_object_toint (elt); if (obj->len < constraint) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "string is too short: %.3f, minimum is: %.3f", obj->len, constraint); ret = false; break; } } #ifdef HAVE_REGEX_H else if (elt->type == UCL_STRING && strcmp (ucl_object_key (elt), "pattern") == 0) { if (regcomp (&re, ucl_object_tostring (elt), REG_EXTENDED | REG_NOSUB) != 0) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, elt, "cannot compile pattern %s", ucl_object_tostring (elt)); ret = false; break; } if (regexec (&re, ucl_object_tostring (obj), 0, NULL, 0) != 0) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "string doesn't match regexp %s", ucl_object_tostring (elt)); ret = false; } regfree (&re); } #endif } return ret; } struct ucl_compare_node { const ucl_object_t *obj; TREE_ENTRY(ucl_compare_node) link; struct ucl_compare_node *next; }; typedef TREE_HEAD(_tree, ucl_compare_node) ucl_compare_tree_t; TREE_DEFINE(ucl_compare_node, link) static int ucl_schema_elt_compare (struct ucl_compare_node *n1, struct ucl_compare_node *n2) { const ucl_object_t *o1 = n1->obj, *o2 = n2->obj; return ucl_object_compare (o1, o2); } static bool ucl_schema_array_is_unique (const ucl_object_t *obj, struct ucl_schema_error *err) { ucl_compare_tree_t tree = TREE_INITIALIZER (ucl_schema_elt_compare); ucl_object_iter_t iter = NULL; const ucl_object_t *elt; struct ucl_compare_node *node, test, *nodes = NULL, *tmp; bool ret = true; while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { test.obj = elt; node = TREE_FIND (&tree, ucl_compare_node, link, &test); if (node != NULL) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, elt, "duplicate values detected while uniqueItems is true"); ret = false; break; } node = calloc (1, sizeof (*node)); if (node == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_UNKNOWN, elt, "cannot allocate tree node"); ret = false; break; } node->obj = elt; TREE_INSERT (&tree, ucl_compare_node, link, node); LL_PREPEND (nodes, node); } LL_FOREACH_SAFE (nodes, node, tmp) { free (node); } return ret; } static bool ucl_schema_validate_array (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err, const ucl_object_t *root) { const ucl_object_t *elt, *it, *found, *additional_schema = NULL, *first_unvalidated = NULL; ucl_object_iter_t iter = NULL, piter = NULL; bool ret = true, allow_additional = true, need_unique = false; int64_t minmax; + unsigned int idx = 0; while (ret && (elt = ucl_iterate_object (schema, &iter, true)) != NULL) { if (strcmp (ucl_object_key (elt), "items") == 0) { if (elt->type == UCL_ARRAY) { - found = obj->value.av; + found = ucl_array_head (obj); while (ret && (it = ucl_iterate_object (elt, &piter, true)) != NULL) { if (found) { ret = ucl_schema_validate (it, found, false, err, root); - found = found->next; + found = ucl_array_find_index (obj, ++idx); } } if (found != NULL) { /* The first element that is not validated */ first_unvalidated = found; } } else if (elt->type == UCL_OBJECT) { /* Validate all items using the specified schema */ while (ret && (it = ucl_iterate_object (obj, &piter, true)) != NULL) { ret = ucl_schema_validate (elt, it, false, err, root); } } else { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, elt, "items attribute is invalid in schema"); ret = false; break; } } else if (strcmp (ucl_object_key (elt), "additionalItems") == 0) { if (elt->type == UCL_BOOLEAN) { if (!ucl_object_toboolean (elt)) { /* Deny additional fields completely */ allow_additional = false; } } else if (elt->type == UCL_OBJECT) { /* Define validator for additional fields */ additional_schema = elt; } else { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, elt, "additionalItems attribute is invalid in schema"); ret = false; break; } } else if (elt->type == UCL_BOOLEAN && strcmp (ucl_object_key (elt), "uniqueItems") == 0) { need_unique = ucl_object_toboolean (elt); } else if (strcmp (ucl_object_key (elt), "minItems") == 0 && ucl_object_toint_safe (elt, &minmax)) { if (obj->len < minmax) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "array has not enough items: %u, minimum is: %u", obj->len, (unsigned)minmax); ret = false; break; } } else if (strcmp (ucl_object_key (elt), "maxItems") == 0 && ucl_object_toint_safe (elt, &minmax)) { if (obj->len > minmax) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "array has too many items: %u, maximum is: %u", obj->len, (unsigned)minmax); ret = false; break; } } } if (ret) { /* Additional properties */ if (!allow_additional || additional_schema != NULL) { if (first_unvalidated != NULL) { if (!allow_additional) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "array has undefined item"); ret = false; } else if (additional_schema != NULL) { - elt = first_unvalidated; + elt = ucl_array_find_index (obj, idx); while (elt) { if (!ucl_schema_validate (additional_schema, elt, false, err, root)) { ret = false; break; } - elt = elt->next; + elt = ucl_array_find_index (obj, idx ++); } } } } /* Required properties */ if (ret && need_unique) { ret = ucl_schema_array_is_unique (obj, err); } } return ret; } /* * Returns whether this object is allowed for this type */ static bool ucl_schema_type_is_allowed (const ucl_object_t *type, const ucl_object_t *obj, struct ucl_schema_error *err) { ucl_object_iter_t iter = NULL; const ucl_object_t *elt; const char *type_str; ucl_type_t t; if (type == NULL) { /* Any type is allowed */ return true; } if (type->type == UCL_ARRAY) { /* One of allowed types */ while ((elt = ucl_iterate_object (type, &iter, true)) != NULL) { if (ucl_schema_type_is_allowed (elt, obj, err)) { return true; } } } else if (type->type == UCL_STRING) { type_str = ucl_object_tostring (type); if (!ucl_string_to_type (type_str, &t)) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, type, "Type attribute is invalid in schema"); return false; } if (obj->type != t) { /* Some types are actually compatible */ if (obj->type == UCL_TIME && t == UCL_FLOAT) { return true; } else if (obj->type == UCL_INT && t == UCL_FLOAT) { return true; } else { ucl_schema_create_error (err, UCL_SCHEMA_TYPE_MISMATCH, obj, "Invalid type of %s, expected %s", ucl_object_type_to_string (obj->type), ucl_object_type_to_string (t)); } } else { /* Types are equal */ return true; } } return false; } /* * Check if object is equal to one of elements of enum */ static bool ucl_schema_validate_enum (const ucl_object_t *en, const ucl_object_t *obj, struct ucl_schema_error *err) { ucl_object_iter_t iter = NULL; const ucl_object_t *elt; bool ret = false; while ((elt = ucl_iterate_object (en, &iter, true)) != NULL) { if (ucl_object_compare (elt, obj) == 0) { ret = true; break; } } if (!ret) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "object is not one of enumerated patterns"); } return ret; } /* * Check a single ref component */ static const ucl_object_t * ucl_schema_resolve_ref_component (const ucl_object_t *cur, const char *refc, int len, struct ucl_schema_error *err) { const ucl_object_t *res = NULL; char *err_str; int num, i; if (cur->type == UCL_OBJECT) { /* Find a key inside an object */ res = ucl_object_find_keyl (cur, refc, len); if (res == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, cur, "reference %s is invalid, missing path component", refc); return NULL; } } else if (cur->type == UCL_ARRAY) { /* We must figure out a number inside array */ num = strtoul (refc, &err_str, 10); if (err_str != NULL && *err_str != '/' && *err_str != '\0') { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, cur, "reference %s is invalid, invalid item number", refc); return NULL; } - res = cur->value.av; + res = ucl_array_head (cur); i = 0; while (res != NULL) { if (i == num) { break; } res = res->next; } if (res == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, cur, "reference %s is invalid, item number %d does not exist", refc, num); return NULL; } } else { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, res, "reference %s is invalid, contains primitive object in the path", refc); return NULL; } return res; } /* * Find reference schema */ static const ucl_object_t * ucl_schema_resolve_ref (const ucl_object_t *root, const char *ref, struct ucl_schema_error *err) { const char *p, *c; const ucl_object_t *res = NULL; if (ref[0] != '#') { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, root, "reference %s is invalid, not started with #", ref); return NULL; } if (ref[1] == '/') { p = &ref[2]; } else if (ref[1] == '\0') { return root; } else { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, root, "reference %s is invalid, not started with #/", ref); return NULL; } c = p; res = root; while (*p != '\0') { if (*p == '/') { if (p - c == 0) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, res, "reference %s is invalid, empty path component", ref); return NULL; } /* Now we have some url part, so we need to figure out where we are */ res = ucl_schema_resolve_ref_component (res, c, p - c, err); if (res == NULL) { return NULL; } c = p + 1; } p ++; } if (p - c != 0) { res = ucl_schema_resolve_ref_component (res, c, p - c, err); } if (res == NULL || res->type != UCL_OBJECT) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, res, "reference %s is invalid, cannot find specified object", ref); return NULL; } return res; } static bool ucl_schema_validate_values (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err) { const ucl_object_t *elt, *cur; int64_t constraint, i; elt = ucl_object_find_key (schema, "maxValues"); if (elt != NULL && elt->type == UCL_INT) { constraint = ucl_object_toint (elt); cur = obj; i = 0; while (cur) { if (i > constraint) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "object has more values than defined: %ld", (long int)constraint); return false; } i ++; cur = cur->next; } } elt = ucl_object_find_key (schema, "minValues"); if (elt != NULL && elt->type == UCL_INT) { constraint = ucl_object_toint (elt); cur = obj; i = 0; while (cur) { if (i >= constraint) { break; } i ++; cur = cur->next; } if (i < constraint) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "object has less values than defined: %ld", (long int)constraint); return false; } } return true; } static bool ucl_schema_validate (const ucl_object_t *schema, const ucl_object_t *obj, bool try_array, struct ucl_schema_error *err, const ucl_object_t *root) { const ucl_object_t *elt, *cur; ucl_object_iter_t iter = NULL; bool ret; if (schema->type != UCL_OBJECT) { ucl_schema_create_error (err, UCL_SCHEMA_INVALID_SCHEMA, schema, "schema is %s instead of object", ucl_object_type_to_string (schema->type)); return false; } if (try_array) { /* * Special case for multiple values */ if (!ucl_schema_validate_values (schema, obj, err)) { return false; } LL_FOREACH (obj, cur) { if (!ucl_schema_validate (schema, cur, false, err, root)) { return false; } } return true; } elt = ucl_object_find_key (schema, "enum"); if (elt != NULL && elt->type == UCL_ARRAY) { if (!ucl_schema_validate_enum (elt, obj, err)) { return false; } } elt = ucl_object_find_key (schema, "allOf"); if (elt != NULL && elt->type == UCL_ARRAY) { iter = NULL; while ((cur = ucl_iterate_object (elt, &iter, true)) != NULL) { ret = ucl_schema_validate (cur, obj, true, err, root); if (!ret) { return false; } } } elt = ucl_object_find_key (schema, "anyOf"); if (elt != NULL && elt->type == UCL_ARRAY) { iter = NULL; while ((cur = ucl_iterate_object (elt, &iter, true)) != NULL) { ret = ucl_schema_validate (cur, obj, true, err, root); if (ret) { break; } } if (!ret) { return false; } else { /* Reset error */ err->code = UCL_SCHEMA_OK; } } elt = ucl_object_find_key (schema, "oneOf"); if (elt != NULL && elt->type == UCL_ARRAY) { iter = NULL; ret = false; while ((cur = ucl_iterate_object (elt, &iter, true)) != NULL) { if (!ret) { ret = ucl_schema_validate (cur, obj, true, err, root); } else if (ucl_schema_validate (cur, obj, true, err, root)) { ret = false; break; } } if (!ret) { return false; } } elt = ucl_object_find_key (schema, "not"); if (elt != NULL && elt->type == UCL_OBJECT) { if (ucl_schema_validate (elt, obj, true, err, root)) { return false; } else { /* Reset error */ err->code = UCL_SCHEMA_OK; } } elt = ucl_object_find_key (schema, "$ref"); if (elt != NULL) { cur = ucl_schema_resolve_ref (root, ucl_object_tostring (elt), err); if (cur == NULL) { return false; } if (!ucl_schema_validate (cur, obj, try_array, err, root)) { return false; } } elt = ucl_object_find_key (schema, "type"); if (!ucl_schema_type_is_allowed (elt, obj, err)) { return false; } switch (obj->type) { case UCL_OBJECT: return ucl_schema_validate_object (schema, obj, err, root); break; case UCL_ARRAY: return ucl_schema_validate_array (schema, obj, err, root); break; case UCL_INT: case UCL_FLOAT: return ucl_schema_validate_number (schema, obj, err); break; case UCL_STRING: return ucl_schema_validate_string (schema, obj, err); break; default: break; } return true; } bool ucl_object_validate (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err) { return ucl_schema_validate (schema, obj, true, err, schema); } Index: vendor/libucl/dist/src/ucl_util.c =================================================================== --- vendor/libucl/dist/src/ucl_util.c (revision 279545) +++ vendor/libucl/dist/src/ucl_util.c (revision 279546) @@ -1,2407 +1,2568 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "ucl.h" #include "ucl_internal.h" #include "ucl_chartable.h" +#include "kvec.h" +#ifndef _WIN32 #include +#endif #ifdef HAVE_LIBGEN_H #include /* For dirname */ #endif +typedef kvec_t(ucl_object_t *) ucl_array_t; + +#define UCL_ARRAY_GET(ar, obj) ucl_array_t *ar = \ + (ucl_array_t *)((obj) != NULL ? (obj)->value.av : NULL) + #ifdef HAVE_OPENSSL #include #include #include #include #include #endif #ifdef CURL_FOUND #include #endif #ifdef HAVE_FETCH_H #include #endif #ifdef _WIN32 #include #ifndef PROT_READ #define PROT_READ 1 #endif #ifndef PROT_WRITE #define PROT_WRITE 2 #endif #ifndef PROT_READWRITE #define PROT_READWRITE 3 #endif #ifndef MAP_SHARED #define MAP_SHARED 1 #endif #ifndef MAP_PRIVATE #define MAP_PRIVATE 2 #endif #ifndef MAP_FAILED #define MAP_FAILED ((void *) -1) #endif +#ifdef _WIN32 +#include +#define NBBY CHAR_BIT +#endif + static void *ucl_mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset) { void *map = NULL; HANDLE handle = INVALID_HANDLE_VALUE; switch (prot) { default: case PROT_READ: { handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READONLY, 0, length, 0); if (!handle) break; map = (void *) MapViewOfFile(handle, FILE_MAP_READ, 0, 0, length); CloseHandle(handle); break; } case PROT_WRITE: { handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0); if (!handle) break; map = (void *) MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, length); CloseHandle(handle); break; } case PROT_READWRITE: { handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0); if (!handle) break; map = (void *) MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, length); CloseHandle(handle); break; } } if (map == (void *) NULL) { return (void *) MAP_FAILED; } return (void *) ((char *) map + offset); } static int ucl_munmap(void *map,size_t length) { if (!UnmapViewOfFile(map)) { return(-1); } return(0); } static char* ucl_realpath(const char *path, char *resolved_path) { char *p; char tmp[MAX_PATH + 1]; strncpy(tmp, path, sizeof(tmp)-1); p = tmp; while(*p) { if (*p == '/') *p = '\\'; p++; } return _fullpath(resolved_path, tmp, MAX_PATH); } #else #define ucl_mmap mmap #define ucl_munmap munmap #define ucl_realpath realpath #endif typedef void (*ucl_object_dtor) (ucl_object_t *obj); static void ucl_object_free_internal (ucl_object_t *obj, bool allow_rec, ucl_object_dtor dtor); static void ucl_object_dtor_unref (ucl_object_t *obj); static void ucl_object_dtor_free (ucl_object_t *obj) { if (obj->trash_stack[UCL_TRASH_KEY] != NULL) { UCL_FREE (obj->hh.keylen, obj->trash_stack[UCL_TRASH_KEY]); } if (obj->trash_stack[UCL_TRASH_VALUE] != NULL) { UCL_FREE (obj->len, obj->trash_stack[UCL_TRASH_VALUE]); } /* Do not free ephemeral objects */ if ((obj->flags & UCL_OBJECT_EPHEMERAL) == 0) { if (obj->type != UCL_USERDATA) { UCL_FREE (sizeof (ucl_object_t), obj); } else { struct ucl_object_userdata *ud = (struct ucl_object_userdata *)obj; if (ud->dtor) { ud->dtor (obj->value.ud); } UCL_FREE (sizeof (*ud), obj); } } } /* * This is a helper function that performs exactly the same as * `ucl_object_unref` but it doesn't iterate over elements allowing * to use it for individual elements of arrays and multiple values */ static void ucl_object_dtor_unref_single (ucl_object_t *obj) { if (obj != NULL) { #ifdef HAVE_ATOMIC_BUILTINS unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1); if (rc == 0) { #else if (--obj->ref == 0) { #endif ucl_object_free_internal (obj, false, ucl_object_dtor_unref); } } } static void ucl_object_dtor_unref (ucl_object_t *obj) { if (obj->ref == 0) { ucl_object_dtor_free (obj); } else { /* This may cause dtor unref being called one more time */ ucl_object_dtor_unref_single (obj); } } static void ucl_object_free_internal (ucl_object_t *obj, bool allow_rec, ucl_object_dtor dtor) { - ucl_object_t *sub, *tmp; + ucl_object_t *tmp, *sub; while (obj != NULL) { if (obj->type == UCL_ARRAY) { - sub = obj->value.av; - while (sub != NULL) { - tmp = sub->next; - dtor (sub); - sub = tmp; + UCL_ARRAY_GET (vec, obj); + unsigned int i; + + if (vec != NULL) { + for (i = 0; i < vec->n; i ++) { + sub = kv_A (*vec, i); + if (sub != NULL) { + tmp = sub; + while (sub) { + tmp = sub->next; + dtor (sub); + sub = tmp; + } + } + } + kv_destroy (*vec); + UCL_FREE (sizeof (*vec), vec); } } else if (obj->type == UCL_OBJECT) { if (obj->value.ov != NULL) { ucl_hash_destroy (obj->value.ov, (ucl_hash_free_func *)dtor); } } tmp = obj->next; dtor (obj); obj = tmp; if (!allow_rec) { break; } } } void ucl_object_free (ucl_object_t *obj) { ucl_object_free_internal (obj, true, ucl_object_dtor_free); } size_t ucl_unescape_json_string (char *str, size_t len) { char *t = str, *h = str; int i, uval; if (len <= 1) { return len; } /* t is target (tortoise), h is source (hare) */ while (len) { if (*h == '\\') { h ++; switch (*h) { case 'n': *t++ = '\n'; break; case 'r': *t++ = '\r'; break; case 'b': *t++ = '\b'; break; case 't': *t++ = '\t'; break; case 'f': *t++ = '\f'; break; case '\\': *t++ = '\\'; break; case '"': *t++ = '"'; break; case 'u': /* Unicode escape */ uval = 0; if (len > 3) { for (i = 0; i < 4; i++) { uval <<= 4; if (isdigit (h[i])) { uval += h[i] - '0'; } else if (h[i] >= 'a' && h[i] <= 'f') { uval += h[i] - 'a' + 10; } else if (h[i] >= 'A' && h[i] <= 'F') { uval += h[i] - 'A' + 10; } else { break; } } h += 3; len -= 3; /* Encode */ if(uval < 0x80) { t[0] = (char)uval; t ++; } else if(uval < 0x800) { t[0] = 0xC0 + ((uval & 0x7C0) >> 6); t[1] = 0x80 + ((uval & 0x03F)); t += 2; } else if(uval < 0x10000) { t[0] = 0xE0 + ((uval & 0xF000) >> 12); t[1] = 0x80 + ((uval & 0x0FC0) >> 6); t[2] = 0x80 + ((uval & 0x003F)); t += 3; } else if(uval <= 0x10FFFF) { t[0] = 0xF0 + ((uval & 0x1C0000) >> 18); t[1] = 0x80 + ((uval & 0x03F000) >> 12); t[2] = 0x80 + ((uval & 0x000FC0) >> 6); t[3] = 0x80 + ((uval & 0x00003F)); t += 4; } else { *t++ = '?'; } } else { *t++ = 'u'; } break; default: *t++ = *h; break; } h ++; len --; } else { *t++ = *h++; } len --; } *t = '\0'; return (t - str); } char * ucl_copy_key_trash (const ucl_object_t *obj) { ucl_object_t *deconst; if (obj == NULL) { return NULL; } if (obj->trash_stack[UCL_TRASH_KEY] == NULL && obj->key != NULL) { deconst = __DECONST (ucl_object_t *, obj); deconst->trash_stack[UCL_TRASH_KEY] = malloc (obj->keylen + 1); if (deconst->trash_stack[UCL_TRASH_KEY] != NULL) { memcpy (deconst->trash_stack[UCL_TRASH_KEY], obj->key, obj->keylen); deconst->trash_stack[UCL_TRASH_KEY][obj->keylen] = '\0'; } deconst->key = obj->trash_stack[UCL_TRASH_KEY]; deconst->flags |= UCL_OBJECT_ALLOCATED_KEY; } return obj->trash_stack[UCL_TRASH_KEY]; } char * ucl_copy_value_trash (const ucl_object_t *obj) { ucl_object_t *deconst; if (obj == NULL) { return NULL; } if (obj->trash_stack[UCL_TRASH_VALUE] == NULL) { deconst = __DECONST (ucl_object_t *, obj); if (obj->type == UCL_STRING) { /* Special case for strings */ deconst->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len + 1); if (deconst->trash_stack[UCL_TRASH_VALUE] != NULL) { memcpy (deconst->trash_stack[UCL_TRASH_VALUE], obj->value.sv, obj->len); deconst->trash_stack[UCL_TRASH_VALUE][obj->len] = '\0'; deconst->value.sv = obj->trash_stack[UCL_TRASH_VALUE]; } } else { /* Just emit value in json notation */ deconst->trash_stack[UCL_TRASH_VALUE] = ucl_object_emit_single_json (obj); deconst->len = strlen (obj->trash_stack[UCL_TRASH_VALUE]); } deconst->flags |= UCL_OBJECT_ALLOCATED_VALUE; } return obj->trash_stack[UCL_TRASH_VALUE]; } UCL_EXTERN ucl_object_t* ucl_parser_get_object (struct ucl_parser *parser) { if (parser->state != UCL_STATE_ERROR && parser->top_obj != NULL) { return ucl_object_ref (parser->top_obj); } return NULL; } UCL_EXTERN void ucl_parser_free (struct ucl_parser *parser) { struct ucl_stack *stack, *stmp; struct ucl_macro *macro, *mtmp; struct ucl_chunk *chunk, *ctmp; struct ucl_pubkey *key, *ktmp; struct ucl_variable *var, *vtmp; if (parser == NULL) { return; } if (parser->top_obj != NULL) { ucl_object_unref (parser->top_obj); } LL_FOREACH_SAFE (parser->stack, stack, stmp) { free (stack); } HASH_ITER (hh, parser->macroes, macro, mtmp) { free (macro->name); HASH_DEL (parser->macroes, macro); UCL_FREE (sizeof (struct ucl_macro), macro); } LL_FOREACH_SAFE (parser->chunks, chunk, ctmp) { UCL_FREE (sizeof (struct ucl_chunk), chunk); } LL_FOREACH_SAFE (parser->keys, key, ktmp) { UCL_FREE (sizeof (struct ucl_pubkey), key); } LL_FOREACH_SAFE (parser->variables, var, vtmp) { free (var->value); free (var->var); UCL_FREE (sizeof (struct ucl_variable), var); } if (parser->err != NULL) { utstring_free (parser->err); } if (parser->cur_file) { free (parser->cur_file); } UCL_FREE (sizeof (struct ucl_parser), parser); } UCL_EXTERN const char * ucl_parser_get_error(struct ucl_parser *parser) { if (parser == NULL) { return NULL; } if (parser->err == NULL) return NULL; return utstring_body(parser->err); } +UCL_EXTERN void +ucl_parser_clear_error(struct ucl_parser *parser) +{ + if (parser != NULL && parser->err != NULL) { + utstring_free(parser->err); + parser->err = NULL; + } +} + UCL_EXTERN bool ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len) { #ifndef HAVE_OPENSSL ucl_create_err (&parser->err, "cannot check signatures without openssl"); return false; #else # if (OPENSSL_VERSION_NUMBER < 0x10000000L) ucl_create_err (&parser->err, "cannot check signatures, openssl version is unsupported"); return EXIT_FAILURE; # else struct ucl_pubkey *nkey; BIO *mem; mem = BIO_new_mem_buf ((void *)key, len); nkey = UCL_ALLOC (sizeof (struct ucl_pubkey)); if (nkey == NULL) { ucl_create_err (&parser->err, "cannot allocate memory for key"); return false; } nkey->key = PEM_read_bio_PUBKEY (mem, &nkey->key, NULL, NULL); BIO_free (mem); if (nkey->key == NULL) { UCL_FREE (sizeof (struct ucl_pubkey), nkey); ucl_create_err (&parser->err, "%s", ERR_error_string (ERR_get_error (), NULL)); return false; } LL_PREPEND (parser->keys, nkey); # endif #endif return true; } #ifdef CURL_FOUND struct ucl_curl_cbdata { unsigned char *buf; size_t buflen; }; static size_t ucl_curl_write_callback (void* contents, size_t size, size_t nmemb, void* ud) { struct ucl_curl_cbdata *cbdata = ud; size_t realsize = size * nmemb; cbdata->buf = realloc (cbdata->buf, cbdata->buflen + realsize + 1); if (cbdata->buf == NULL) { return 0; } memcpy (&(cbdata->buf[cbdata->buflen]), contents, realsize); cbdata->buflen += realsize; cbdata->buf[cbdata->buflen] = 0; return realsize; } #endif /** * Fetch a url and save results to the memory buffer * @param url url to fetch * @param len length of url * @param buf target buffer * @param buflen target length * @return */ static bool ucl_fetch_url (const unsigned char *url, unsigned char **buf, size_t *buflen, UT_string **err, bool must_exist) { #ifdef HAVE_FETCH_H struct url *fetch_url; struct url_stat us; FILE *in; fetch_url = fetchParseURL (url); if (fetch_url == NULL) { ucl_create_err (err, "invalid URL %s: %s", url, strerror (errno)); return false; } if ((in = fetchXGet (fetch_url, &us, "")) == NULL) { if (!must_exist) { ucl_create_err (err, "cannot fetch URL %s: %s", url, strerror (errno)); } fetchFreeURL (fetch_url); return false; } *buflen = us.size; *buf = malloc (*buflen); if (*buf == NULL) { ucl_create_err (err, "cannot allocate buffer for URL %s: %s", url, strerror (errno)); fclose (in); fetchFreeURL (fetch_url); return false; } if (fread (*buf, *buflen, 1, in) != 1) { ucl_create_err (err, "cannot read URL %s: %s", url, strerror (errno)); fclose (in); fetchFreeURL (fetch_url); return false; } fetchFreeURL (fetch_url); return true; #elif defined(CURL_FOUND) CURL *curl; int r; struct ucl_curl_cbdata cbdata; curl = curl_easy_init (); if (curl == NULL) { ucl_create_err (err, "CURL interface is broken"); return false; } if ((r = curl_easy_setopt (curl, CURLOPT_URL, url)) != CURLE_OK) { ucl_create_err (err, "invalid URL %s: %s", url, curl_easy_strerror (r)); curl_easy_cleanup (curl); return false; } curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ucl_curl_write_callback); cbdata.buf = *buf; cbdata.buflen = *buflen; curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbdata); if ((r = curl_easy_perform (curl)) != CURLE_OK) { if (!must_exist) { ucl_create_err (err, "error fetching URL %s: %s", url, curl_easy_strerror (r)); } curl_easy_cleanup (curl); if (cbdata.buf) { free (cbdata.buf); } return false; } *buf = cbdata.buf; *buflen = cbdata.buflen; return true; #else ucl_create_err (err, "URL support is disabled"); return false; #endif } /** * Fetch a file and save results to the memory buffer * @param filename filename to fetch * @param len length of filename * @param buf target buffer * @param buflen target length * @return */ static bool ucl_fetch_file (const unsigned char *filename, unsigned char **buf, size_t *buflen, UT_string **err, bool must_exist) { int fd; struct stat st; if (stat (filename, &st) == -1 || !S_ISREG (st.st_mode)) { if (must_exist) { ucl_create_err (err, "cannot stat file %s: %s", filename, strerror (errno)); } return false; } if (st.st_size == 0) { /* Do not map empty files */ *buf = ""; *buflen = 0; } else { if ((fd = open (filename, O_RDONLY)) == -1) { ucl_create_err (err, "cannot open file %s: %s", filename, strerror (errno)); return false; } if ((*buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { close (fd); ucl_create_err (err, "cannot mmap file %s: %s", filename, strerror (errno)); return false; } *buflen = st.st_size; close (fd); } return true; } #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L) static inline bool ucl_sig_check (const unsigned char *data, size_t datalen, const unsigned char *sig, size_t siglen, struct ucl_parser *parser) { struct ucl_pubkey *key; char dig[EVP_MAX_MD_SIZE]; unsigned int diglen; EVP_PKEY_CTX *key_ctx; EVP_MD_CTX *sign_ctx = NULL; sign_ctx = EVP_MD_CTX_create (); LL_FOREACH (parser->keys, key) { key_ctx = EVP_PKEY_CTX_new (key->key, NULL); if (key_ctx != NULL) { if (EVP_PKEY_verify_init (key_ctx) <= 0) { EVP_PKEY_CTX_free (key_ctx); continue; } if (EVP_PKEY_CTX_set_rsa_padding (key_ctx, RSA_PKCS1_PADDING) <= 0) { EVP_PKEY_CTX_free (key_ctx); continue; } if (EVP_PKEY_CTX_set_signature_md (key_ctx, EVP_sha256 ()) <= 0) { EVP_PKEY_CTX_free (key_ctx); continue; } EVP_DigestInit (sign_ctx, EVP_sha256 ()); EVP_DigestUpdate (sign_ctx, data, datalen); EVP_DigestFinal (sign_ctx, dig, &diglen); if (EVP_PKEY_verify (key_ctx, sig, siglen, dig, diglen) == 1) { EVP_MD_CTX_destroy (sign_ctx); EVP_PKEY_CTX_free (key_ctx); return true; } EVP_PKEY_CTX_free (key_ctx); } } EVP_MD_CTX_destroy (sign_ctx); return false; } #endif /** * Include an url to configuration * @param data * @param len * @param parser * @param err * @return */ static bool ucl_include_url (const unsigned char *data, size_t len, struct ucl_parser *parser, bool check_signature, bool must_exist, unsigned priority) { bool res; unsigned char *buf = NULL; size_t buflen = 0; struct ucl_chunk *chunk; char urlbuf[PATH_MAX]; int prev_state; snprintf (urlbuf, sizeof (urlbuf), "%.*s", (int)len, data); if (!ucl_fetch_url (urlbuf, &buf, &buflen, &parser->err, must_exist)) { return (!must_exist || false); } if (check_signature) { #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L) unsigned char *sigbuf = NULL; size_t siglen = 0; /* We need to check signature first */ snprintf (urlbuf, sizeof (urlbuf), "%.*s.sig", (int)len, data); if (!ucl_fetch_url (urlbuf, &sigbuf, &siglen, &parser->err, true)) { return false; } if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) { ucl_create_err (&parser->err, "cannot verify url %s: %s", urlbuf, ERR_error_string (ERR_get_error (), NULL)); if (siglen > 0) { ucl_munmap (sigbuf, siglen); } return false; } if (siglen > 0) { ucl_munmap (sigbuf, siglen); } #endif } prev_state = parser->state; parser->state = UCL_STATE_INIT; res = ucl_parser_add_chunk_priority (parser, buf, buflen, priority); if (res == true) { /* Remove chunk from the stack */ chunk = parser->chunks; if (chunk != NULL) { parser->chunks = chunk->next; UCL_FREE (sizeof (struct ucl_chunk), chunk); } } parser->state = prev_state; free (buf); return res; } /** * Include a single file to the parser * @param data * @param len * @param parser * @param check_signature * @param must_exist * @param allow_glob * @param priority * @return */ static bool ucl_include_file_single (const unsigned char *data, size_t len, struct ucl_parser *parser, bool check_signature, bool must_exist, unsigned priority) { bool res; struct ucl_chunk *chunk; unsigned char *buf = NULL; char *old_curfile; size_t buflen; char filebuf[PATH_MAX], realbuf[PATH_MAX]; int prev_state; struct ucl_variable *cur_var, *tmp_var, *old_curdir = NULL, *old_filename = NULL; snprintf (filebuf, sizeof (filebuf), "%.*s", (int)len, data); if (ucl_realpath (filebuf, realbuf) == NULL) { if (!must_exist) { return true; } ucl_create_err (&parser->err, "cannot open file %s: %s", filebuf, strerror (errno)); return false; } if (parser->cur_file && strcmp (realbuf, parser->cur_file) == 0) { /* We are likely including the file itself */ ucl_create_err (&parser->err, "trying to include the file %s from itself", realbuf); return false; } if (!ucl_fetch_file (realbuf, &buf, &buflen, &parser->err, must_exist)) { return (!must_exist || false); } if (check_signature) { #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L) unsigned char *sigbuf = NULL; size_t siglen = 0; /* We need to check signature first */ snprintf (filebuf, sizeof (filebuf), "%s.sig", realbuf); if (!ucl_fetch_file (filebuf, &sigbuf, &siglen, &parser->err, true)) { return false; } if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) { ucl_create_err (&parser->err, "cannot verify file %s: %s", filebuf, ERR_error_string (ERR_get_error (), NULL)); if (siglen > 0) { ucl_munmap (sigbuf, siglen); } return false; } if (siglen > 0) { ucl_munmap (sigbuf, siglen); } #endif } old_curfile = parser->cur_file; parser->cur_file = strdup (realbuf); /* Store old file vars */ DL_FOREACH_SAFE (parser->variables, cur_var, tmp_var) { if (strcmp (cur_var->var, "CURDIR") == 0) { old_curdir = cur_var; DL_DELETE (parser->variables, cur_var); } else if (strcmp (cur_var->var, "FILENAME") == 0) { old_filename = cur_var; DL_DELETE (parser->variables, cur_var); } } ucl_parser_set_filevars (parser, realbuf, false); prev_state = parser->state; parser->state = UCL_STATE_INIT; res = ucl_parser_add_chunk_priority (parser, buf, buflen, priority); if (!res && !must_exist) { /* Free error */ utstring_free (parser->err); parser->err = NULL; parser->state = UCL_STATE_AFTER_VALUE; } /* Remove chunk from the stack */ chunk = parser->chunks; if (chunk != NULL) { parser->chunks = chunk->next; UCL_FREE (sizeof (struct ucl_chunk), chunk); parser->recursion --; } /* Restore old file vars */ parser->cur_file = old_curfile; DL_FOREACH_SAFE (parser->variables, cur_var, tmp_var) { if (strcmp (cur_var->var, "CURDIR") == 0 && old_curdir) { DL_DELETE (parser->variables, cur_var); free (cur_var->var); free (cur_var->value); UCL_FREE (sizeof (struct ucl_variable), cur_var); } else if (strcmp (cur_var->var, "FILENAME") == 0 && old_filename) { DL_DELETE (parser->variables, cur_var); free (cur_var->var); free (cur_var->value); UCL_FREE (sizeof (struct ucl_variable), cur_var); } } if (old_filename) { DL_APPEND (parser->variables, old_filename); } if (old_curdir) { DL_APPEND (parser->variables, old_curdir); } if (old_curfile) { free (old_curfile); } parser->state = prev_state; if (buflen > 0) { ucl_munmap (buf, buflen); } return res; } /** * Include a file to configuration * @param data * @param len * @param parser * @param err * @return */ static bool ucl_include_file (const unsigned char *data, size_t len, struct ucl_parser *parser, bool check_signature, bool must_exist, bool allow_glob, unsigned priority) { const unsigned char *p = data, *end = data + len; bool need_glob = false; int cnt = 0; - glob_t globbuf; char glob_pattern[PATH_MAX]; size_t i; +#ifndef _WIN32 if (!allow_glob) { return ucl_include_file_single (data, len, parser, check_signature, must_exist, priority); } else { /* Check for special symbols in a filename */ while (p != end) { if (*p == '*' || *p == '?') { need_glob = true; break; } p ++; } if (need_glob) { + glob_t globbuf; memset (&globbuf, 0, sizeof (globbuf)); ucl_strlcpy (glob_pattern, (const char *)data, sizeof (glob_pattern)); if (glob (glob_pattern, 0, NULL, &globbuf) != 0) { return (!must_exist || false); } for (i = 0; i < globbuf.gl_pathc; i ++) { if (!ucl_include_file_single ((unsigned char *)globbuf.gl_pathv[i], strlen (globbuf.gl_pathv[i]), parser, check_signature, must_exist, priority)) { globfree (&globbuf); return false; } cnt ++; } globfree (&globbuf); if (cnt == 0 && must_exist) { ucl_create_err (&parser->err, "cannot match any files for pattern %s", glob_pattern); return false; } } else { return ucl_include_file_single (data, len, parser, check_signature, must_exist, priority); } } - +#else + /* Win32 compilers do not support globbing. Therefore, for Win32, + treat allow_glob/need_glob as a NOOP and just return */ + return ucl_include_file_single (data, len, parser, check_signature, + must_exist, priority); +#endif + return true; } /** * Common function to handle .*include* macros * @param data * @param len * @param args * @param parser * @param default_try * @param default_sign * @return */ static bool ucl_include_common (const unsigned char *data, size_t len, const ucl_object_t *args, struct ucl_parser *parser, bool default_try, bool default_sign) { bool try_load, allow_glob, allow_url, need_sign; unsigned priority; const ucl_object_t *param; ucl_object_iter_t it = NULL; /* Default values */ try_load = default_try; allow_glob = false; allow_url = true; need_sign = default_sign; priority = 0; /* Process arguments */ if (args != NULL && args->type == UCL_OBJECT) { while ((param = ucl_iterate_object (args, &it, true)) != NULL) { if (param->type == UCL_BOOLEAN) { if (strcmp (param->key, "try") == 0) { try_load = ucl_object_toboolean (param); } else if (strcmp (param->key, "sign") == 0) { need_sign = ucl_object_toboolean (param); } else if (strcmp (param->key, "glob") == 0) { allow_glob = ucl_object_toboolean (param); } else if (strcmp (param->key, "url") == 0) { allow_url = ucl_object_toboolean (param); } } else if (param->type == UCL_INT) { if (strcmp (param->key, "priority") == 0) { priority = ucl_object_toint (param); } } } } if (*data == '/' || *data == '.') { /* Try to load a file */ return ucl_include_file (data, len, parser, need_sign, !try_load, allow_glob, priority); } else if (allow_url) { /* Globbing is not used for URL's */ return ucl_include_url (data, len, parser, need_sign, !try_load, priority); } return false; } /** * Handle include macro * @param data include data * @param len length of data * @param ud user data * @param err error ptr * @return */ UCL_EXTERN bool ucl_include_handler (const unsigned char *data, size_t len, const ucl_object_t *args, void* ud) { struct ucl_parser *parser = ud; return ucl_include_common (data, len, args, parser, false, false); } /** * Handle includes macro * @param data include data * @param len length of data * @param ud user data * @param err error ptr * @return */ UCL_EXTERN bool ucl_includes_handler (const unsigned char *data, size_t len, const ucl_object_t *args, void* ud) { struct ucl_parser *parser = ud; return ucl_include_common (data, len, args, parser, false, true); } UCL_EXTERN bool ucl_try_include_handler (const unsigned char *data, size_t len, const ucl_object_t *args, void* ud) { struct ucl_parser *parser = ud; return ucl_include_common (data, len, args, parser, true, false); } UCL_EXTERN bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand) { char realbuf[PATH_MAX], *curdir; if (filename != NULL) { if (need_expand) { if (ucl_realpath (filename, realbuf) == NULL) { return false; } } else { ucl_strlcpy (realbuf, filename, sizeof (realbuf)); } /* Define variables */ ucl_parser_register_variable (parser, "FILENAME", realbuf); curdir = dirname (realbuf); ucl_parser_register_variable (parser, "CURDIR", curdir); } else { /* Set everything from the current dir */ curdir = getcwd (realbuf, sizeof (realbuf)); ucl_parser_register_variable (parser, "FILENAME", "undef"); ucl_parser_register_variable (parser, "CURDIR", curdir); } return true; } UCL_EXTERN bool ucl_parser_add_file (struct ucl_parser *parser, const char *filename) { unsigned char *buf; size_t len; bool ret; char realbuf[PATH_MAX]; if (ucl_realpath (filename, realbuf) == NULL) { ucl_create_err (&parser->err, "cannot open file %s: %s", filename, strerror (errno)); return false; } if (!ucl_fetch_file (realbuf, &buf, &len, &parser->err, true)) { return false; } if (parser->cur_file) { free (parser->cur_file); } parser->cur_file = strdup (realbuf); ucl_parser_set_filevars (parser, realbuf, false); ret = ucl_parser_add_chunk (parser, buf, len); if (len > 0) { ucl_munmap (buf, len); } return ret; } UCL_EXTERN bool ucl_parser_add_fd (struct ucl_parser *parser, int fd) { unsigned char *buf; size_t len; bool ret; struct stat st; if (fstat (fd, &st) == -1) { ucl_create_err (&parser->err, "cannot stat fd %d: %s", fd, strerror (errno)); return false; } if ((buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { ucl_create_err (&parser->err, "cannot mmap fd %d: %s", fd, strerror (errno)); return false; } if (parser->cur_file) { free (parser->cur_file); } parser->cur_file = NULL; len = st.st_size; ret = ucl_parser_add_chunk (parser, buf, len); if (len > 0) { ucl_munmap (buf, len); } return ret; } size_t ucl_strlcpy (char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ if (n != 0) { while (--n != 0) { if ((*d++ = *s++) == '\0') { break; } } } if (n == 0 && siz != 0) { *d = '\0'; } return (s - src - 1); /* count does not include NUL */ } size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz) { memcpy (dst, src, siz - 1); dst[siz - 1] = '\0'; return siz - 1; } size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ if (n != 0) { while (--n != 0) { if ((*d++ = tolower (*s++)) == '\0') { break; } } } if (n == 0 && siz != 0) { *d = '\0'; } return (s - src); /* count does not include NUL */ } ucl_object_t * ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags flags) { ucl_object_t *obj; const char *start, *end, *p, *pos; char *dst, *d; size_t escaped_len; if (str == NULL) { return NULL; } obj = ucl_object_new (); if (obj) { if (len == 0) { len = strlen (str); } if (flags & UCL_STRING_TRIM) { /* Skip leading spaces */ for (start = str; (size_t)(start - str) < len; start ++) { if (!ucl_test_character (*start, UCL_CHARACTER_WHITESPACE_UNSAFE)) { break; } } /* Skip trailing spaces */ for (end = str + len - 1; end > start; end --) { if (!ucl_test_character (*end, UCL_CHARACTER_WHITESPACE_UNSAFE)) { break; } } end ++; } else { start = str; end = str + len; } obj->type = UCL_STRING; if (flags & UCL_STRING_ESCAPE) { for (p = start, escaped_len = 0; p < end; p ++, escaped_len ++) { if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) { escaped_len ++; } } dst = malloc (escaped_len + 1); if (dst != NULL) { for (p = start, d = dst; p < end; p ++, d ++) { if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) { switch (*p) { case '\n': *d++ = '\\'; *d = 'n'; break; case '\r': *d++ = '\\'; *d = 'r'; break; case '\b': *d++ = '\\'; *d = 'b'; break; case '\t': *d++ = '\\'; *d = 't'; break; case '\f': *d++ = '\\'; *d = 'f'; break; case '\\': *d++ = '\\'; *d = '\\'; break; case '"': *d++ = '\\'; *d = '"'; break; } } else { *d = *p; } } *d = '\0'; obj->value.sv = dst; obj->trash_stack[UCL_TRASH_VALUE] = dst; obj->len = escaped_len; } } else { dst = malloc (end - start + 1); if (dst != NULL) { ucl_strlcpy_unsafe (dst, start, end - start + 1); obj->value.sv = dst; obj->trash_stack[UCL_TRASH_VALUE] = dst; obj->len = end - start; } } if ((flags & UCL_STRING_PARSE) && dst != NULL) { /* Parse what we have */ if (flags & UCL_STRING_PARSE_BOOLEAN) { if (!ucl_maybe_parse_boolean (obj, dst, obj->len) && (flags & UCL_STRING_PARSE_NUMBER)) { ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos, flags & UCL_STRING_PARSE_DOUBLE, flags & UCL_STRING_PARSE_BYTES, flags & UCL_STRING_PARSE_TIME); } } else { ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos, flags & UCL_STRING_PARSE_DOUBLE, flags & UCL_STRING_PARSE_BYTES, flags & UCL_STRING_PARSE_TIME); } } } return obj; } static bool ucl_object_insert_key_common (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key, bool merge, bool replace) { ucl_object_t *found, *tmp; const ucl_object_t *cur; ucl_object_iter_t it = NULL; const char *p; int ret = true; if (elt == NULL || key == NULL) { return false; } if (top == NULL) { return false; } if (top->type != UCL_OBJECT) { /* It is possible to convert NULL type to an object */ if (top->type == UCL_NULL) { top->type = UCL_OBJECT; } else { /* Refuse converting of other object types */ return false; } } if (top->value.ov == NULL) { - top->value.ov = ucl_hash_create (); + top->value.ov = ucl_hash_create (false); } if (keylen == 0) { keylen = strlen (key); } for (p = key; p < key + keylen; p ++) { if (ucl_test_character (*p, UCL_CHARACTER_UCL_UNSAFE)) { elt->flags |= UCL_OBJECT_NEED_KEY_ESCAPE; break; } } /* workaround for some use cases */ if (elt->trash_stack[UCL_TRASH_KEY] != NULL && key != (const char *)elt->trash_stack[UCL_TRASH_KEY]) { /* Remove copied key */ free (elt->trash_stack[UCL_TRASH_KEY]); elt->trash_stack[UCL_TRASH_KEY] = NULL; elt->flags &= ~UCL_OBJECT_ALLOCATED_KEY; } elt->key = key; elt->keylen = keylen; if (copy_key) { ucl_copy_key_trash (elt); } found = __DECONST (ucl_object_t *, ucl_hash_search_obj (top->value.ov, elt)); if (found == NULL) { - top->value.ov = ucl_hash_insert_object (top->value.ov, elt); + top->value.ov = ucl_hash_insert_object (top->value.ov, elt, false); top->len ++; if (replace) { ret = false; } } else { if (replace) { ucl_hash_replace (top->value.ov, found, elt); ucl_object_unref (found); } else if (merge) { if (found->type != UCL_OBJECT && elt->type == UCL_OBJECT) { /* Insert old elt to new one */ ucl_object_insert_key_common (elt, found, found->key, found->keylen, copy_key, false, false); ucl_hash_delete (top->value.ov, found); - top->value.ov = ucl_hash_insert_object (top->value.ov, elt); + top->value.ov = ucl_hash_insert_object (top->value.ov, elt, false); } else if (found->type == UCL_OBJECT && elt->type != UCL_OBJECT) { /* Insert new to old */ ucl_object_insert_key_common (found, elt, elt->key, elt->keylen, copy_key, false, false); } else if (found->type == UCL_OBJECT && elt->type == UCL_OBJECT) { /* Mix two hashes */ while ((cur = ucl_iterate_object (elt, &it, true)) != NULL) { tmp = ucl_object_ref (cur); ucl_object_insert_key_common (found, tmp, cur->key, cur->keylen, copy_key, false, false); } ucl_object_unref (elt); } else { /* Just make a list of scalars */ DL_APPEND (found, elt); } } else { DL_APPEND (found, elt); } } return ret; } bool ucl_object_delete_keyl (ucl_object_t *top, const char *key, size_t keylen) { ucl_object_t *found; if (top == NULL || key == NULL) { return false; } found = __DECONST (ucl_object_t *, ucl_object_find_keyl (top, key, keylen)); if (found == NULL) { return false; } ucl_hash_delete (top->value.ov, found); ucl_object_unref (found); top->len --; return true; } bool ucl_object_delete_key (ucl_object_t *top, const char *key) { return ucl_object_delete_keyl (top, key, strlen(key)); } ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key, size_t keylen) { const ucl_object_t *found; if (top == NULL || key == NULL) { return false; } found = ucl_object_find_keyl (top, key, keylen); if (found == NULL) { return NULL; } ucl_hash_delete (top->value.ov, found); top->len --; return __DECONST (ucl_object_t *, found); } ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key) { return ucl_object_pop_keyl (top, key, strlen(key)); } bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key) { return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, false); } bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key) { return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, true, false); } bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key) { return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, true); } bool ucl_object_merge (ucl_object_t *top, ucl_object_t *elt, bool copy) { ucl_object_t *cur = NULL, *cp = NULL, *found = NULL; ucl_object_iter_t iter = NULL; if (top == NULL || top->type != UCL_OBJECT || elt == NULL || elt->type != UCL_OBJECT) { return false; } /* Mix two hashes */ while ((cur = (ucl_object_t*)ucl_hash_iterate (elt->value.ov, &iter))) { if (copy) { cp = ucl_object_copy (cur); } else { cp = ucl_object_ref (cur); } found = __DECONST(ucl_object_t *, ucl_hash_search (top->value.ov, cp->key, cp->keylen)); if (found == NULL) { /* The key does not exist */ - top->value.ov = ucl_hash_insert_object (top->value.ov, cp); + top->value.ov = ucl_hash_insert_object (top->value.ov, cp, false); top->len ++; } else { /* The key already exists, replace it */ ucl_hash_replace (top->value.ov, found, cp); ucl_object_unref (found); } } return true; } const ucl_object_t * ucl_object_find_keyl (const ucl_object_t *obj, const char *key, size_t klen) { const ucl_object_t *ret; ucl_object_t srch; if (obj == NULL || obj->type != UCL_OBJECT || key == NULL) { return NULL; } srch.key = key; srch.keylen = klen; ret = ucl_hash_search_obj (obj->value.ov, &srch); return ret; } const ucl_object_t * ucl_object_find_key (const ucl_object_t *obj, const char *key) { if (key == NULL) return NULL; return ucl_object_find_keyl (obj, key, strlen(key)); } const ucl_object_t* ucl_iterate_object (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expand_values) { - const ucl_object_t *elt; + const ucl_object_t *elt = NULL; if (obj == NULL || iter == NULL) { return NULL; } if (expand_values) { switch (obj->type) { case UCL_OBJECT: return (const ucl_object_t*)ucl_hash_iterate (obj->value.ov, iter); break; - case UCL_ARRAY: - elt = *iter; - if (elt == NULL) { - elt = obj->value.av; - if (elt == NULL) { - return NULL; + case UCL_ARRAY: { + unsigned int idx; + UCL_ARRAY_GET (vec, obj); + idx = (unsigned int)(uintptr_t)(*iter); + + if (vec != NULL) { + while (idx < kv_size (*vec)) { + if ((elt = kv_A (*vec, idx)) != NULL) { + idx ++; + break; + } + idx ++; } + *iter = (void *)(uintptr_t)idx; } - else if (elt == obj->value.av) { - return NULL; - } - *iter = elt->next ? elt->next : obj->value.av; + return elt; + break; + } default: /* Go to linear iteration */ break; } } /* Treat everything as a linear list */ elt = *iter; if (elt == NULL) { elt = obj; } else if (elt == obj) { return NULL; } *iter = __DECONST (void *, elt->next ? elt->next : obj); return elt; /* Not reached */ return NULL; } +const char safe_iter_magic[4] = {'u', 'i', 't', 'e'}; +struct ucl_object_safe_iter { + char magic[4]; /* safety check */ + const ucl_object_t *impl_it; /* implicit object iteration */ + ucl_object_iter_t expl_it; /* explicit iteration */ +}; + +#define UCL_SAFE_ITER(ptr) (struct ucl_object_safe_iter *)(ptr) +#define UCL_SAFE_ITER_CHECK(it) do { \ + assert (it != NULL); \ + assert (memcmp (it->magic, safe_iter_magic, sizeof (it->magic)) == 0); \ + } while (0) + +ucl_object_iter_t +ucl_object_iterate_new (const ucl_object_t *obj) +{ + struct ucl_object_safe_iter *it; + + it = UCL_ALLOC (sizeof (*it)); + if (it != NULL) { + memcpy (it->magic, safe_iter_magic, sizeof (it->magic)); + it->expl_it = NULL; + it->impl_it = obj; + } + + return (ucl_object_iter_t)it; +} + + +ucl_object_iter_t +ucl_object_iterate_reset (ucl_object_iter_t it, const ucl_object_t *obj) +{ + struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it); + + UCL_SAFE_ITER_CHECK (rit); + + rit->impl_it = obj; + rit->expl_it = NULL; + + return it; +} + +const ucl_object_t* +ucl_object_iterate_safe (ucl_object_iter_t it, bool expand_values) +{ + struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it); + const ucl_object_t *ret = NULL; + + UCL_SAFE_ITER_CHECK (rit); + + if (rit->impl_it == NULL) { + return NULL; + } + + if (rit->impl_it->type == UCL_OBJECT || rit->impl_it->type == UCL_ARRAY) { + ret = ucl_iterate_object (rit->impl_it, &rit->expl_it, true); + + if (ret == NULL) { + /* Need to switch to another implicit object in chain */ + rit->impl_it = rit->impl_it->next; + rit->expl_it = NULL; + return ucl_object_iterate_safe (it, expand_values); + } + } + else { + /* Just iterate over the implicit array */ + ret = rit->impl_it; + rit->impl_it = rit->impl_it->next; + if (expand_values) { + /* We flatten objects if need to expand values */ + if (ret->type == UCL_OBJECT || ret->type == UCL_ARRAY) { + return ucl_object_iterate_safe (it, expand_values); + } + } + } + + return ret; +} + +void +ucl_object_iterate_free (ucl_object_iter_t it) +{ + struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it); + + UCL_SAFE_ITER_CHECK (rit); + + UCL_FREE (sizeof (*rit), it); +} + const ucl_object_t * ucl_lookup_path (const ucl_object_t *top, const char *path_in) { const ucl_object_t *o = NULL, *found; const char *p, *c; char *err_str; unsigned index; if (path_in == NULL || top == NULL) { return NULL; } found = NULL; p = path_in; /* Skip leading dots */ while (*p == '.') { p ++; } c = p; while (*p != '\0') { p ++; if (*p == '.' || *p == '\0') { if (p > c) { switch (top->type) { case UCL_ARRAY: /* Key should be an int */ index = strtoul (c, &err_str, 10); if (err_str != NULL && (*err_str != '.' && *err_str != '\0')) { return NULL; } o = ucl_array_find_index (top, index); break; default: o = ucl_object_find_keyl (top, c, p - c); break; } if (o == NULL) { return NULL; } top = o; } if (*p != '\0') { c = p + 1; } } } found = o; return found; } ucl_object_t * ucl_object_new (void) { return ucl_object_typed_new (UCL_NULL); } ucl_object_t * ucl_object_typed_new (ucl_type_t type) { return ucl_object_new_full (type, 0); } ucl_object_t * ucl_object_new_full (ucl_type_t type, unsigned priority) { ucl_object_t *new; if (type != UCL_USERDATA) { new = UCL_ALLOC (sizeof (ucl_object_t)); if (new != NULL) { memset (new, 0, sizeof (ucl_object_t)); new->ref = 1; new->type = (type <= UCL_NULL ? type : UCL_NULL); new->next = NULL; new->prev = new; ucl_object_set_priority (new, priority); + + if (type == UCL_ARRAY) { + new->value.av = UCL_ALLOC (sizeof (ucl_array_t)); + if (new->value.av) { + memset (new->value.av, 0, sizeof (ucl_array_t)); + UCL_ARRAY_GET (vec, new); + + /* Preallocate some space for arrays */ + kv_resize (ucl_object_t *, *vec, 8); + } + } } } else { new = ucl_object_new_userdata (NULL, NULL); ucl_object_set_priority (new, priority); } return new; } ucl_object_t* ucl_object_new_userdata (ucl_userdata_dtor dtor, ucl_userdata_emitter emitter) { struct ucl_object_userdata *new; size_t nsize = sizeof (*new); new = UCL_ALLOC (nsize); if (new != NULL) { memset (new, 0, nsize); new->obj.ref = 1; new->obj.type = UCL_USERDATA; new->obj.next = NULL; new->obj.prev = (ucl_object_t *)new; new->dtor = dtor; new->emitter = emitter; } return (ucl_object_t *)new; } ucl_type_t ucl_object_type (const ucl_object_t *obj) { return obj->type; } ucl_object_t* ucl_object_fromstring (const char *str) { return ucl_object_fromstring_common (str, 0, UCL_STRING_ESCAPE); } ucl_object_t * ucl_object_fromlstring (const char *str, size_t len) { return ucl_object_fromstring_common (str, len, UCL_STRING_ESCAPE); } ucl_object_t * ucl_object_fromint (int64_t iv) { ucl_object_t *obj; obj = ucl_object_new (); if (obj != NULL) { obj->type = UCL_INT; obj->value.iv = iv; } return obj; } ucl_object_t * ucl_object_fromdouble (double dv) { ucl_object_t *obj; obj = ucl_object_new (); if (obj != NULL) { obj->type = UCL_FLOAT; obj->value.dv = dv; } return obj; } ucl_object_t* ucl_object_frombool (bool bv) { ucl_object_t *obj; obj = ucl_object_new (); if (obj != NULL) { obj->type = UCL_BOOLEAN; obj->value.iv = bv; } return obj; } bool ucl_array_append (ucl_object_t *top, ucl_object_t *elt) { - ucl_object_t *head; + UCL_ARRAY_GET (vec, top); if (elt == NULL || top == NULL) { return false; } - head = top->value.av; - if (head == NULL) { - top->value.av = elt; - elt->prev = elt; + if (vec == NULL) { + vec = UCL_ALLOC (sizeof (*vec)); + kv_init (*vec); + top->value.av = (void *)vec; } - else { - elt->prev = head->prev; - head->prev->next = elt; - head->prev = elt; - } - elt->next = NULL; + + kv_push (ucl_object_t *, *vec, elt); + top->len ++; return true; } bool ucl_array_prepend (ucl_object_t *top, ucl_object_t *elt) { - ucl_object_t *head; + UCL_ARRAY_GET (vec, top); if (elt == NULL || top == NULL) { return false; } - - head = top->value.av; - if (head == NULL) { - top->value.av = elt; - elt->prev = elt; + if (vec == NULL) { + vec = UCL_ALLOC (sizeof (*vec)); + kv_init (*vec); + top->value.av = (void *)vec; + kv_push (ucl_object_t *, *vec, elt); } else { - elt->prev = head->prev; - head->prev = elt; + /* Slow O(n) algorithm */ + kv_prepend (ucl_object_t *, *vec, elt); } - elt->next = head; - top->value.av = elt; + top->len ++; return true; } bool ucl_array_merge (ucl_object_t *top, ucl_object_t *elt, bool copy) { - ucl_object_t *cur, *tmp, *cp; + unsigned i; + ucl_object_t **obj; + UCL_ARRAY_GET (v1, top); + UCL_ARRAY_GET (v2, elt); if (elt == NULL || top == NULL || top->type != UCL_ARRAY || elt->type != UCL_ARRAY) { return false; } - DL_FOREACH_SAFE (elt->value.av, cur, tmp) { + kv_concat (ucl_object_t *, *v1, *v2); + + for (i = v2->n; i < v1->n; i ++) { + obj = &kv_A (*v1, i); + if (*obj == NULL) { + continue; + } + + top->len ++; if (copy) { - cp = ucl_object_copy (cur); + *obj = ucl_object_copy (*obj); } else { - cp = ucl_object_ref (cur); + ucl_object_ref (*obj); } - if (cp != NULL) { - ucl_array_append (top, cp); - } } return true; } ucl_object_t * ucl_array_delete (ucl_object_t *top, ucl_object_t *elt) { - ucl_object_t *head; + UCL_ARRAY_GET (vec, top); + ucl_object_t *ret = NULL; + unsigned i; - if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) { - return NULL; - } - head = top->value.av; - - if (elt->prev == elt) { - top->value.av = NULL; - } - else if (elt == head) { - elt->next->prev = elt->prev; - top->value.av = elt->next; - } - else { - elt->prev->next = elt->next; - if (elt->next) { - elt->next->prev = elt->prev; + for (i = 0; i < vec->n; i ++) { + if (kv_A (*vec, i) == elt) { + kv_del (ucl_object_t *, *vec, i); + ret = elt; + top->len --; + break; } - else { - head->prev = elt->prev; - } } - elt->next = NULL; - elt->prev = elt; - top->len --; - return elt; + return ret; } const ucl_object_t * ucl_array_head (const ucl_object_t *top) { + UCL_ARRAY_GET (vec, top); + if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) { return NULL; } - return top->value.av; + + return (vec->n > 0 ? vec->a[0] : NULL); } const ucl_object_t * ucl_array_tail (const ucl_object_t *top) { + UCL_ARRAY_GET (vec, top); + if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) { return NULL; } - return top->value.av->prev; + + return (vec->n > 0 ? vec->a[vec->n - 1] : NULL); } ucl_object_t * ucl_array_pop_last (ucl_object_t *top) { - return ucl_array_delete (top, __DECONST(ucl_object_t *, ucl_array_tail (top))); + UCL_ARRAY_GET (vec, top); + ucl_object_t **obj, *ret = NULL; + + if (vec != NULL && vec->n > 0) { + obj = &kv_A (*vec, vec->n - 1); + ret = *obj; + kv_del (ucl_object_t *, *vec, vec->n - 1); + top->len --; + } + + return ret; } ucl_object_t * ucl_array_pop_first (ucl_object_t *top) { - return ucl_array_delete (top, __DECONST(ucl_object_t *, ucl_array_head (top))); + UCL_ARRAY_GET (vec, top); + ucl_object_t **obj, *ret = NULL; + + if (vec != NULL && vec->n > 0) { + obj = &kv_A (*vec, 0); + ret = *obj; + kv_del (ucl_object_t *, *vec, 0); + top->len --; + } + + return ret; } const ucl_object_t * ucl_array_find_index (const ucl_object_t *top, unsigned int index) { - ucl_object_iter_t it = NULL; - const ucl_object_t *ret; + UCL_ARRAY_GET (vec, top); - if (top == NULL || top->type != UCL_ARRAY || top->len == 0 || - (index + 1) > top->len) { - return NULL; + if (vec != NULL && vec->n > 0 && index < vec->n) { + return kv_A (*vec, index); } - while ((ret = ucl_iterate_object (top, &it, true)) != NULL) { - if (index == 0) { - return ret; - } - --index; - } - return NULL; } ucl_object_t * ucl_array_replace_index (ucl_object_t *top, ucl_object_t *elt, unsigned int index) { - ucl_object_t *cur, *tmp; + UCL_ARRAY_GET (vec, top); + ucl_object_t *ret = NULL; - if (top == NULL || top->type != UCL_ARRAY || elt == NULL || - top->len == 0 || (index + 1) > top->len) { - return NULL; + if (vec != NULL && vec->n > 0 && index < vec->n) { + ret = kv_A (*vec, index); + kv_A (*vec, index) = elt; } - DL_FOREACH_SAFE (top->value.av, cur, tmp) { - if (index == 0) { - DL_REPLACE_ELEM (top->value.av, cur, elt); - return cur; - } - --index; - } - - return NULL; + return ret; } ucl_object_t * ucl_elt_append (ucl_object_t *head, ucl_object_t *elt) { if (head == NULL) { elt->next = NULL; elt->prev = elt; head = elt; } else { elt->prev = head->prev; head->prev->next = elt; head->prev = elt; elt->next = NULL; } return head; } bool ucl_object_todouble_safe (const ucl_object_t *obj, double *target) { if (obj == NULL || target == NULL) { return false; } switch (obj->type) { case UCL_INT: *target = obj->value.iv; /* Probaly could cause overflow */ break; case UCL_FLOAT: case UCL_TIME: *target = obj->value.dv; break; default: return false; } return true; } double ucl_object_todouble (const ucl_object_t *obj) { double result = 0.; ucl_object_todouble_safe (obj, &result); return result; } bool ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target) { if (obj == NULL || target == NULL) { return false; } switch (obj->type) { case UCL_INT: *target = obj->value.iv; break; case UCL_FLOAT: case UCL_TIME: *target = obj->value.dv; /* Loosing of decimal points */ break; default: return false; } return true; } int64_t ucl_object_toint (const ucl_object_t *obj) { int64_t result = 0; ucl_object_toint_safe (obj, &result); return result; } bool ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target) { if (obj == NULL || target == NULL) { return false; } switch (obj->type) { case UCL_BOOLEAN: *target = (obj->value.iv == true); break; default: return false; } return true; } bool ucl_object_toboolean (const ucl_object_t *obj) { bool result = false; ucl_object_toboolean_safe (obj, &result); return result; } bool ucl_object_tostring_safe (const ucl_object_t *obj, const char **target) { if (obj == NULL || target == NULL) { return false; } switch (obj->type) { case UCL_STRING: *target = ucl_copy_value_trash (obj); break; default: return false; } return true; } const char * ucl_object_tostring (const ucl_object_t *obj) { const char *result = NULL; ucl_object_tostring_safe (obj, &result); return result; } const char * ucl_object_tostring_forced (const ucl_object_t *obj) { return ucl_copy_value_trash (obj); } bool ucl_object_tolstring_safe (const ucl_object_t *obj, const char **target, size_t *tlen) { if (obj == NULL || target == NULL) { return false; } switch (obj->type) { case UCL_STRING: *target = obj->value.sv; if (tlen != NULL) { *tlen = obj->len; } break; default: return false; } return true; } const char * ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen) { const char *result = NULL; ucl_object_tolstring_safe (obj, &result, tlen); return result; } const char * ucl_object_key (const ucl_object_t *obj) { return ucl_copy_key_trash (obj); } const char * ucl_object_keyl (const ucl_object_t *obj, size_t *len) { if (len == NULL || obj == NULL) { return NULL; } *len = obj->keylen; return obj->key; } ucl_object_t * ucl_object_ref (const ucl_object_t *obj) { ucl_object_t *res = NULL; if (obj != NULL) { if (obj->flags & UCL_OBJECT_EPHEMERAL) { /* * Use deep copy for ephemeral objects, note that its refcount * is NOT increased, since ephemeral objects does not need refcount * at all */ res = ucl_object_copy (obj); } else { res = __DECONST (ucl_object_t *, obj); #ifdef HAVE_ATOMIC_BUILTINS (void)__sync_add_and_fetch (&res->ref, 1); #else res->ref ++; #endif } } return res; } static ucl_object_t * ucl_object_copy_internal (const ucl_object_t *other, bool allow_array) { ucl_object_t *new; ucl_object_iter_t it = NULL; const ucl_object_t *cur; new = malloc (sizeof (*new)); if (new != NULL) { memcpy (new, other, sizeof (*new)); if (other->flags & UCL_OBJECT_EPHEMERAL) { /* Copied object is always non ephemeral */ new->flags &= ~UCL_OBJECT_EPHEMERAL; } new->ref = 1; /* Unlink from others */ new->next = NULL; new->prev = new; /* deep copy of values stored */ if (other->trash_stack[UCL_TRASH_KEY] != NULL) { new->trash_stack[UCL_TRASH_KEY] = strdup (other->trash_stack[UCL_TRASH_KEY]); if (other->key == (const char *)other->trash_stack[UCL_TRASH_KEY]) { new->key = new->trash_stack[UCL_TRASH_KEY]; } } if (other->trash_stack[UCL_TRASH_VALUE] != NULL) { new->trash_stack[UCL_TRASH_VALUE] = strdup (other->trash_stack[UCL_TRASH_VALUE]); if (new->type == UCL_STRING) { new->value.sv = new->trash_stack[UCL_TRASH_VALUE]; } } if (other->type == UCL_ARRAY || other->type == UCL_OBJECT) { /* reset old value */ memset (&new->value, 0, sizeof (new->value)); while ((cur = ucl_iterate_object (other, &it, true)) != NULL) { if (other->type == UCL_ARRAY) { ucl_array_append (new, ucl_object_copy_internal (cur, false)); } else { ucl_object_t *cp = ucl_object_copy_internal (cur, true); if (cp != NULL) { ucl_object_insert_key (new, cp, cp->key, cp->keylen, false); } } } } else if (allow_array && other->next != NULL) { LL_FOREACH (other->next, cur) { ucl_object_t *cp = ucl_object_copy_internal (cur, false); if (cp != NULL) { DL_APPEND (new, cp); } } } } return new; } ucl_object_t * ucl_object_copy (const ucl_object_t *other) { return ucl_object_copy_internal (other, true); } void ucl_object_unref (ucl_object_t *obj) { if (obj != NULL) { #ifdef HAVE_ATOMIC_BUILTINS unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1); if (rc == 0) { #else if (--obj->ref == 0) { #endif ucl_object_free_internal (obj, true, ucl_object_dtor_unref); } } } int ucl_object_compare (const ucl_object_t *o1, const ucl_object_t *o2) { const ucl_object_t *it1, *it2; ucl_object_iter_t iter = NULL; int ret = 0; if (o1->type != o2->type) { return (o1->type) - (o2->type); } switch (o1->type) { case UCL_STRING: - if (o1->len == o2->len) { + if (o1->len == o2->len && o1->len > 0) { ret = strcmp (ucl_object_tostring(o1), ucl_object_tostring(o2)); } else { ret = o1->len - o2->len; } break; case UCL_FLOAT: case UCL_INT: case UCL_TIME: ret = ucl_object_todouble (o1) - ucl_object_todouble (o2); break; case UCL_BOOLEAN: ret = ucl_object_toboolean (o1) - ucl_object_toboolean (o2); break; case UCL_ARRAY: - if (o1->len == o2->len) { - it1 = o1->value.av; - it2 = o2->value.av; + if (o1->len == o2->len && o1->len > 0) { + UCL_ARRAY_GET (vec1, o1); + UCL_ARRAY_GET (vec2, o2); + unsigned i; + /* Compare all elements in both arrays */ - while (it1 != NULL && it2 != NULL) { - ret = ucl_object_compare (it1, it2); - if (ret != 0) { - break; + for (i = 0; i < vec1->n; i ++) { + it1 = kv_A (*vec1, i); + it2 = kv_A (*vec2, i); + + if (it1 == NULL && it2 != NULL) { + return -1; } - it1 = it1->next; - it2 = it2->next; + else if (it2 == NULL && it1 != NULL) { + return 1; + } + else if (it1 != NULL && it2 != NULL) { + ret = ucl_object_compare (it1, it2); + if (ret != 0) { + break; + } + } } } else { ret = o1->len - o2->len; } break; case UCL_OBJECT: - if (o1->len == o2->len) { + if (o1->len == o2->len && o1->len > 0) { while ((it1 = ucl_iterate_object (o1, &iter, true)) != NULL) { it2 = ucl_object_find_key (o2, ucl_object_key (it1)); if (it2 == NULL) { ret = 1; break; } ret = ucl_object_compare (it1, it2); if (ret != 0) { break; } } } else { ret = o1->len - o2->len; } break; default: ret = 0; break; } return ret; } void ucl_object_array_sort (ucl_object_t *ar, int (*cmp)(const ucl_object_t *o1, const ucl_object_t *o2)) { + UCL_ARRAY_GET (vec, ar); + if (cmp == NULL || ar == NULL || ar->type != UCL_ARRAY) { return; } - DL_SORT (ar->value.av, cmp); + qsort (vec->a, vec->n, sizeof (ucl_object_t *), + (int (*)(const void *, const void *))cmp); } #define PRIOBITS 4 unsigned int ucl_object_get_priority (const ucl_object_t *obj) { if (obj == NULL) { return 0; } return (obj->flags >> ((sizeof (obj->flags) * NBBY) - PRIOBITS)); } void ucl_object_set_priority (ucl_object_t *obj, unsigned int priority) { if (obj != NULL) { priority &= (0x1 << PRIOBITS) - 1; obj->flags |= priority << ((sizeof (obj->flags) * NBBY) - PRIOBITS); } } Index: vendor/libucl/dist/tests/basic/14.in =================================================================== --- vendor/libucl/dist/tests/basic/14.in (nonexistent) +++ vendor/libucl/dist/tests/basic/14.in (revision 279546) @@ -0,0 +1,8 @@ +# Bad comments case + +section { +# key = value; +} +.include(try=true) "./1.in" + +key = value; Property changes on: vendor/libucl/dist/tests/basic/14.in ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: vendor/libucl/dist/tests/basic/14.res =================================================================== --- vendor/libucl/dist/tests/basic/14.res (nonexistent) +++ vendor/libucl/dist/tests/basic/14.res (revision 279546) @@ -0,0 +1,4 @@ +section { +} +key = "value"; + Index: vendor/libucl/dist/tests/schema.test =================================================================== --- vendor/libucl/dist/tests/schema.test (revision 279545) +++ vendor/libucl/dist/tests/schema.test (revision 279546) @@ -1,9 +1,9 @@ #!/bin/sh PROG=${TEST_BINARY_DIR}/test_schema rm /tmp/_ucl_test_schema.out ||true for i in ${TEST_DIR}/schema/*.json ; do _name=`basename $i` printf "running schema test suite $_name... " - $PROG >> /tmp/_ucl_test_schema.out < $i && ( echo "OK" ) || ( echo "Fail" ) + $PROG >> /tmp/_ucl_test_schema.out < $i && ( echo "OK" ) || ( echo "Fail" ; exit 1 ) done Index: vendor/libucl/dist/tests/test_generate.c =================================================================== --- vendor/libucl/dist/tests/test_generate.c (revision 279545) +++ vendor/libucl/dist/tests/test_generate.c (revision 279546) @@ -1,157 +1,185 @@ /* Copyright (c) 2013, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include "ucl.h" int main (int argc, char **argv) { ucl_object_t *obj, *cur, *ar, *ref; - const ucl_object_t *found; + ucl_object_iter_t it; + const ucl_object_t *found, *it_obj; FILE *out; unsigned char *emitted; const char *fname_out = NULL; int ret = 0; switch (argc) { case 2: fname_out = argv[1]; break; } if (fname_out != NULL) { out = fopen (fname_out, "w"); if (out == NULL) { exit (-errno); } } else { out = stdout; } obj = ucl_object_typed_new (UCL_OBJECT); /* Keys replacing */ cur = ucl_object_fromstring_common ("value1", 0, UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key0", 0, false); cur = ucl_object_fromdouble (0.1); - ucl_object_replace_key (obj, cur, "key0", 0, false); + assert (ucl_object_replace_key (obj, cur, "key0", 0, false)); /* Create some strings */ cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key1", 0, false); cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key2", 0, false); cur = ucl_object_fromstring_common (" test string \n", 0, 0); ucl_object_insert_key (obj, cur, "key3", 0, false); /* Array of numbers */ ar = ucl_object_typed_new (UCL_ARRAY); cur = ucl_object_fromint (10); ucl_array_append (ar, cur); cur = ucl_object_fromdouble (10.1); ucl_array_append (ar, cur); cur = ucl_object_fromdouble (9.999); ucl_array_prepend (ar, cur); /* Removing from an array */ cur = ucl_object_fromdouble (1.0); ucl_array_append (ar, cur); cur = ucl_array_delete (ar, cur); assert (ucl_object_todouble (cur) == 1.0); ucl_object_unref (cur); cur = ucl_object_fromdouble (2.0); ucl_array_append (ar, cur); cur = ucl_array_pop_last (ar); assert (ucl_object_todouble (cur) == 2.0); ucl_object_unref (cur); cur = ucl_object_fromdouble (3.0); ucl_array_prepend (ar, cur); cur = ucl_array_pop_first (ar); assert (ucl_object_todouble (cur) == 3.0); ucl_object_unref (cur); ucl_object_insert_key (obj, ar, "key4", 0, false); cur = ucl_object_frombool (true); /* Ref object to test refcounts */ ref = ucl_object_ref (cur); ucl_object_insert_key (obj, cur, "key4", 0, false); /* Empty strings */ cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key5", 0, false); cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key6", 0, false); cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key7", 0, false); /* Numbers and booleans */ cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); ucl_object_insert_key (obj, cur, "key8", 0, false); cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); ucl_object_insert_key (obj, cur, "key9", 0, false); cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); ucl_object_insert_key (obj, cur, "key10", 0, false); cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key11", 0, false); cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT); ucl_object_insert_key (obj, cur, "key12", 0, false); cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); ucl_object_insert_key (obj, cur, "key13", 0, false); cur = ucl_object_frombool (true); ucl_object_insert_key (obj, cur, "k=3", 0, false); /* Try to find using path */ /* Should exist */ found = ucl_lookup_path (obj, "key4.1"); assert (found != NULL && ucl_object_toint (found) == 10); /* . should be ignored */ found = ucl_lookup_path (obj, ".key4.1"); assert (found != NULL && ucl_object_toint (found) == 10); /* moar dots... */ found = ucl_lookup_path (obj, ".key4........1..."); assert (found != NULL && ucl_object_toint (found) == 10); /* No such index */ found = ucl_lookup_path (obj, ".key4.3"); assert (found == NULL); /* No such key */ found = ucl_lookup_path (obj, "key9..key1"); assert (found == NULL); + + /* Test iteration */ + it = ucl_object_iterate_new (obj); + it_obj = ucl_object_iterate_safe (it, true); + /* key0 = 0.1 */ + assert (ucl_object_type (it_obj) == UCL_FLOAT); + it_obj = ucl_object_iterate_safe (it, true); + /* key1 = "" */ + assert (ucl_object_type (it_obj) == UCL_STRING); + it_obj = ucl_object_iterate_safe (it, true); + /* key2 = "" */ + assert (ucl_object_type (it_obj) == UCL_STRING); + it_obj = ucl_object_iterate_safe (it, true); + /* key3 = "" */ + assert (ucl_object_type (it_obj) == UCL_STRING); + it_obj = ucl_object_iterate_safe (it, true); + /* key4 = ([float, int, float], boolean) */ + ucl_object_iterate_reset (it, it_obj); + it_obj = ucl_object_iterate_safe (it, true); + assert (ucl_object_type (it_obj) == UCL_FLOAT); + it_obj = ucl_object_iterate_safe (it, true); + assert (ucl_object_type (it_obj) == UCL_INT); + it_obj = ucl_object_iterate_safe (it, true); + assert (ucl_object_type (it_obj) == UCL_FLOAT); + it_obj = ucl_object_iterate_safe (it, true); + assert (ucl_object_type (it_obj) == UCL_BOOLEAN); + ucl_object_iterate_free (it); emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); fprintf (out, "%s\n", emitted); ucl_object_unref (obj); if (emitted != NULL) { free (emitted); } fclose (out); /* Ref should still be accessible */ ref->value.iv = 100500; ucl_object_unref (ref); return ret; } Index: vendor/libucl/dist/tests/test_schema.c =================================================================== --- vendor/libucl/dist/tests/test_schema.c (revision 279545) +++ vendor/libucl/dist/tests/test_schema.c (revision 279546) @@ -1,158 +1,160 @@ /* Copyright (c) 2014, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include "ucl.h" static int read_stdin (char **buf) { int size = BUFSIZ, remain, ret; char *p; *buf = malloc (size); if (*buf == NULL) { return -1; } p = *buf; remain = size; while ((ret = read (STDIN_FILENO, p, remain)) > 0) { remain -= ret; p += ret; if (remain == 0) { *buf = realloc (*buf, size * 2); if (*buf == NULL) { return -1; } p = *buf + size; remain = size; size *= 2; } } return ret; } static bool perform_test (const ucl_object_t *schema, const ucl_object_t *obj, struct ucl_schema_error *err) { const ucl_object_t *valid, *data, *description; bool match; data = ucl_object_find_key (obj, "data"); description = ucl_object_find_key (obj, "description"); valid = ucl_object_find_key (obj, "valid"); if (data == NULL || description == NULL || valid == NULL) { fprintf (stdout, "Bad test case\n"); return false; } match = ucl_object_validate (schema, data, err); if (match != ucl_object_toboolean (valid)) { fprintf (stdout, "Test case '%s' failed (expected %s): '%s'\n", ucl_object_tostring (description), ucl_object_toboolean (valid) ? "valid" : "invalid", err->msg); + fprintf (stdout, "%s\n", ucl_object_emit (data, UCL_EMIT_CONFIG)); + fprintf (stdout, "%s\n", ucl_object_emit (schema, UCL_EMIT_CONFIG)); return false; } return true; } static int perform_tests (const ucl_object_t *obj) { struct ucl_schema_error err; ucl_object_iter_t iter = NULL; const ucl_object_t *schema, *tests, *description, *test; if (obj->type != UCL_OBJECT) { fprintf (stdout, "Bad test case\n"); return EXIT_FAILURE; } schema = ucl_object_find_key (obj, "schema"); tests = ucl_object_find_key (obj, "tests"); description = ucl_object_find_key (obj, "description"); if (schema == NULL || tests == NULL || description == NULL) { fprintf (stdout, "Bad test case\n"); return EXIT_FAILURE; } memset (&err, 0, sizeof (err)); while ((test = ucl_iterate_object (tests, &iter, true)) != NULL) { if (!perform_test (schema, test, &err)) { fprintf (stdout, "Test suite '%s' failed\n", ucl_object_tostring (description)); return EXIT_FAILURE; } } return 0; } int main (int argc, char **argv) { char *buf = NULL; struct ucl_parser *parser; ucl_object_t *obj = NULL; const ucl_object_t *elt; ucl_object_iter_t iter = NULL; int ret = 0; if (read_stdin (&buf) == -1) { exit (EXIT_FAILURE); } parser = ucl_parser_new (0); ucl_parser_add_string (parser, buf, 0); if (ucl_parser_get_error (parser) != NULL) { fprintf (stdout, "Error occurred: %s\n", ucl_parser_get_error (parser)); ret = 1; return EXIT_FAILURE; } obj = ucl_parser_get_object (parser); ucl_parser_free (parser); while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { ret = perform_tests (elt); if (ret != 0) { break; } } ucl_object_unref (obj); return ret; } Index: vendor/libucl/dist/uthash/utstring.h =================================================================== --- vendor/libucl/dist/uthash/utstring.h (revision 279545) +++ vendor/libucl/dist/uthash/utstring.h (revision 279546) @@ -1,412 +1,412 @@ /* Copyright (c) 2008-2013, Troy D. Hanson http://troydhanson.github.com/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* a dynamic string implementation using macros */ #ifndef UTSTRING_H #define UTSTRING_H #define UTSTRING_VERSION 1.9.8 #ifdef __GNUC__ #define _UNUSED_ __attribute__ ((__unused__)) #else #define _UNUSED_ #endif #include #include #include #ifndef oom -#define oom() exit(-1) +#define oom abort #endif typedef struct { char *d; void **pd; size_t n; /* allocd size */ size_t i; /* index of first unused byte */ } UT_string; #define utstring_reserve(s,amt) \ do { \ if (((s)->n - (s)->i) < (size_t)(amt)) { \ (s)->d = (char*)realloc((s)->d, (s)->n + amt); \ if ((s)->d == NULL) oom(); \ - (s)->n += amt; \ - if ((s)->pd) *((s)->pd) = (s)->d; \ + else {(s)->n += amt; \ + if ((s)->pd) *((s)->pd) = (s)->d;} \ } \ } while(0) #define utstring_init(s) \ do { \ (s)->n = 0; (s)->i = 0; (s)->d = NULL; \ utstring_reserve(s,128); \ (s)->d[0] = '\0'; \ } while(0) #define utstring_done(s) \ do { \ if ((s)->d != NULL) free((s)->d); \ (s)->n = 0; \ } while(0) #define utstring_free(s) \ do { \ utstring_done(s); \ free(s); \ } while(0) #define utstring_new(s) \ do { \ s = (UT_string*)calloc(1, sizeof(UT_string)); \ if (!s) oom(); \ - utstring_init(s); \ + else utstring_init(s); \ } while(0) #define utstring_renew(s) \ do { \ if (s) { \ utstring_clear(s); \ } else { \ utstring_new(s); \ } \ } while(0) #define utstring_clear(s) \ do { \ (s)->i = 0; \ (s)->d[0] = '\0'; \ } while(0) #define utstring_bincpy(s,b,l) \ do { \ utstring_reserve((s),(l)+1); \ if (l) memcpy(&(s)->d[(s)->i], b, l); \ (s)->i += l; \ (s)->d[(s)->i]='\0'; \ } while(0) #define utstring_concat(dst,src) \ do { \ utstring_reserve((dst),((src)->i)+1); \ if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \ (dst)->i += (src)->i; \ (dst)->d[(dst)->i]='\0'; \ } while(0) #define utstring_len(s) ((unsigned)((s)->i)) #define utstring_body(s) ((s)->d) _UNUSED_ static void utstring_printf_va(UT_string *s, const char *fmt, va_list ap) { int n; va_list cp; while (1) { #ifdef _WIN32 cp = ap; #else va_copy(cp, ap); #endif n = vsnprintf (&s->d[s->i], s->n-s->i, fmt, cp); va_end(cp); if ((n > -1) && (n < (int)(s->n-s->i))) { s->i += n; return; } /* Else try again with more space. */ if (n > -1) utstring_reserve(s,n+1); /* exact */ else utstring_reserve(s,(s->n)*2); /* 2x */ } } #ifdef __GNUC__ /* support printf format checking (2=the format string, 3=start of varargs) */ static void utstring_printf(UT_string *s, const char *fmt, ...) __attribute__ (( format( printf, 2, 3) )); #endif _UNUSED_ static void utstring_printf(UT_string *s, const char *fmt, ...) { va_list ap; va_start(ap,fmt); utstring_printf_va(s,fmt,ap); va_end(ap); } #define utstring_append_len(dst, src, len) \ do { \ while ((dst)->n-(dst)->i <= (len)) utstring_reserve((dst),((dst)->n)*2); \ memcpy(&(dst)->d[(dst)->i], (src), (len)); \ (dst)->i+=(len); \ (dst)->d[(dst)->i]='\0'; \ } while(0) #define utstring_append_c(dst, c) \ do { \ if ((dst)->n-(dst)->i < 2) utstring_reserve((dst),((dst)->n)*2); \ (dst)->d[(dst)->i++] = (c); \ (dst)->d[(dst)->i]='\0'; \ } while(0) /******************************************************************************* * begin substring search functions * ******************************************************************************/ /* Build KMP table from left to right. */ _UNUSED_ static void _utstring_BuildTable( const char *P_Needle, ssize_t P_NeedleLen, long *P_KMP_Table) { long i, j; i = 0; j = i - 1; P_KMP_Table[i] = j; while (i < P_NeedleLen) { while ( (j > -1) && (P_Needle[i] != P_Needle[j]) ) { j = P_KMP_Table[j]; } i++; j++; if (i < P_NeedleLen) { if (P_Needle[i] == P_Needle[j]) { P_KMP_Table[i] = P_KMP_Table[j]; } else { P_KMP_Table[i] = j; } } else { P_KMP_Table[i] = j; } } return; } /* Build KMP table from right to left. */ _UNUSED_ static void _utstring_BuildTableR( const char *P_Needle, ssize_t P_NeedleLen, long *P_KMP_Table) { long i, j; i = P_NeedleLen - 1; j = i + 1; P_KMP_Table[i + 1] = j; while (i >= 0) { while ( (j < P_NeedleLen) && (P_Needle[i] != P_Needle[j]) ) { j = P_KMP_Table[j + 1]; } i--; j--; if (i >= 0) { if (P_Needle[i] == P_Needle[j]) { P_KMP_Table[i + 1] = P_KMP_Table[j + 1]; } else { P_KMP_Table[i + 1] = j; } } else { P_KMP_Table[i + 1] = j; } } return; } /* Search data from left to right. ( Multiple search mode. ) */ _UNUSED_ static long _utstring_find( const char *P_Haystack, size_t P_HaystackLen, const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table) { long i, j; long V_FindPosition = -1; /* Search from left to right. */ i = j = 0; while ( (j < (int)P_HaystackLen) && (((P_HaystackLen - j) + i) >= P_NeedleLen) ) { while ( (i > -1) && (P_Needle[i] != P_Haystack[j]) ) { i = P_KMP_Table[i]; } i++; j++; if (i >= (int)P_NeedleLen) { /* Found. */ V_FindPosition = j - i; break; } } return V_FindPosition; } /* Search data from right to left. ( Multiple search mode. ) */ _UNUSED_ static long _utstring_findR( const char *P_Haystack, size_t P_HaystackLen, const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table) { long i, j; long V_FindPosition = -1; /* Search from right to left. */ j = (P_HaystackLen - 1); i = (P_NeedleLen - 1); while ( (j >= 0) && (j >= i) ) { while ( (i < (int)P_NeedleLen) && (P_Needle[i] != P_Haystack[j]) ) { i = P_KMP_Table[i + 1]; } i--; j--; if (i < 0) { /* Found. */ V_FindPosition = j + 1; break; } } return V_FindPosition; } /* Search data from left to right. ( One time search mode. ) */ _UNUSED_ static long utstring_find( UT_string *s, long P_StartPosition, /* Start from 0. -1 means last position. */ const char *P_Needle, ssize_t P_NeedleLen) { long V_StartPosition; long V_HaystackLen; long *V_KMP_Table; long V_FindPosition = -1; if (P_StartPosition < 0) { V_StartPosition = s->i + P_StartPosition; } else { V_StartPosition = P_StartPosition; } V_HaystackLen = s->i - V_StartPosition; if ( (V_HaystackLen >= P_NeedleLen) && (P_NeedleLen > 0) ) { V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1)); if (V_KMP_Table != NULL) { _utstring_BuildTable(P_Needle, P_NeedleLen, V_KMP_Table); V_FindPosition = _utstring_find(s->d + V_StartPosition, V_HaystackLen, P_Needle, P_NeedleLen, V_KMP_Table); if (V_FindPosition >= 0) { V_FindPosition += V_StartPosition; } free(V_KMP_Table); } } return V_FindPosition; } /* Search data from right to left. ( One time search mode. ) */ _UNUSED_ static long utstring_findR( UT_string *s, long P_StartPosition, /* Start from 0. -1 means last position. */ const char *P_Needle, ssize_t P_NeedleLen) { long V_StartPosition; long V_HaystackLen; long *V_KMP_Table; long V_FindPosition = -1; if (P_StartPosition < 0) { V_StartPosition = s->i + P_StartPosition; } else { V_StartPosition = P_StartPosition; } V_HaystackLen = V_StartPosition + 1; if ( (V_HaystackLen >= P_NeedleLen) && (P_NeedleLen > 0) ) { V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1)); if (V_KMP_Table != NULL) { _utstring_BuildTableR(P_Needle, P_NeedleLen, V_KMP_Table); V_FindPosition = _utstring_findR(s->d, V_HaystackLen, P_Needle, P_NeedleLen, V_KMP_Table); free(V_KMP_Table); } } return V_FindPosition; } /******************************************************************************* * end substring search functions * ******************************************************************************/ #endif /* UTSTRING_H */