Compare commits
17 Commits
a2a72c8b92
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
74fcb06828
|
|||
|
8cf1abf135
|
|||
|
b87fbc93a6
|
|||
|
ac1a21eac8
|
|||
|
6934550068
|
|||
|
cda7e1b1f3
|
|||
|
14c6d285fc
|
|||
|
ab5941aaef
|
|||
|
84d6c658e8
|
|||
|
a89fadf860
|
|||
|
4023155b67
|
|||
|
4ed58dd89a
|
|||
|
022668fb23
|
|||
|
ba5a6374b6
|
|||
|
67008963cf
|
|||
|
c1b6918db3
|
|||
|
3cf3c0215e
|
@@ -1,5 +1,5 @@
|
||||
cmake_minimum_required (VERSION 3.0)
|
||||
project (json-rpc-shell VERSION 1.1.0 LANGUAGES C)
|
||||
cmake_minimum_required (VERSION 3.0...3.27)
|
||||
project (json-rpc-shell VERSION 1.2.0 LANGUAGES C)
|
||||
|
||||
# Options
|
||||
option (WANT_READLINE "Use GNU Readline for the UI (better)" ON)
|
||||
@@ -13,22 +13,29 @@ if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
endif ()
|
||||
|
||||
# For custom modules
|
||||
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
||||
set (CMAKE_MODULE_PATH
|
||||
"${PROJECT_SOURCE_DIR}/cmake;${PROJECT_SOURCE_DIR}/liberty/cmake")
|
||||
|
||||
# Dependencies
|
||||
find_package (Curses)
|
||||
find_package (Ncursesw)
|
||||
find_package (PkgConfig REQUIRED)
|
||||
pkg_check_modules (dependencies REQUIRED libcurl jansson)
|
||||
# Note that cURL can link to a different version of libssl than we do,
|
||||
# in which case the results are undefined
|
||||
pkg_check_modules (libssl REQUIRED libssl libcrypto)
|
||||
pkg_check_modules (dependencies REQUIRED libcurl jansson libssl libcrypto)
|
||||
find_package (LibEV REQUIRED)
|
||||
pkg_check_modules (ncursesw ncursesw)
|
||||
|
||||
set (project_libraries ${dependencies_LIBRARIES}
|
||||
${libssl_LIBRARIES} ${LibEV_LIBRARIES})
|
||||
include_directories (${dependencies_INCLUDE_DIRS}
|
||||
${libssl_INCLUDE_DIRS} ${LibEV_INCLUDE_DIRS})
|
||||
set (project_libraries ${dependencies_LIBRARIES} ${LibEV_LIBRARIES})
|
||||
include_directories (${dependencies_INCLUDE_DIRS} ${LibEV_INCLUDE_DIRS})
|
||||
link_directories (${dependencies_LIBRARY_DIRS})
|
||||
|
||||
if ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
|
||||
# Need this for SIGWINCH in FreeBSD and OpenBSD respectively;
|
||||
# our POSIX version macros make it undefined
|
||||
add_definitions (-D__BSD_VISIBLE=1 -D_BSD_SOURCE=1)
|
||||
elseif (APPLE)
|
||||
add_definitions (-D_DARWIN_C_SOURCE)
|
||||
endif ()
|
||||
|
||||
# -liconv may or may not be a part of libc
|
||||
find_library (iconv_LIBRARIES iconv)
|
||||
@@ -36,15 +43,18 @@ if (iconv_LIBRARIES)
|
||||
list (APPEND project_libraries ${iconv_LIBRARIES})
|
||||
endif ()
|
||||
|
||||
if ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
|
||||
# Need this for SIGWINCH in FreeBSD and OpenBSD respectively;
|
||||
# our POSIX version macros make it undefined
|
||||
add_definitions (-D__BSD_VISIBLE=1 -D_BSD_SOURCE=1)
|
||||
endif ()
|
||||
include (CheckCSourceRuns)
|
||||
set (CMAKE_REQUIRED_LIBRARIES ${project_libraries})
|
||||
get_property (CMAKE_REQUIRED_INCLUDES
|
||||
DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY INCLUDE_DIRECTORIES)
|
||||
CHECK_C_SOURCE_RUNS ("#include <iconv.h>
|
||||
int main () { return iconv_open (\"UTF-8//TRANSLIT\", \"ISO-8859-1\")
|
||||
== (iconv_t) -1; }" ICONV_ACCEPTS_TRANSLIT)
|
||||
|
||||
if (ncursesw_FOUND)
|
||||
list (APPEND project_libraries ${ncursesw_LIBRARIES})
|
||||
include_directories (${ncursesw_INCLUDE_DIRS})
|
||||
if (Ncursesw_FOUND)
|
||||
list (APPEND project_libraries ${Ncursesw_LIBRARIES})
|
||||
include_directories (${Ncursesw_INCLUDE_DIRS})
|
||||
link_directories (${Ncursesw_LIBRARY_DIRS})
|
||||
elseif (CURSES_FOUND)
|
||||
list (APPEND project_libraries ${CURSES_LIBRARY})
|
||||
include_directories (${CURSES_INCLUDE_DIR})
|
||||
@@ -97,20 +107,40 @@ install (FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
|
||||
|
||||
# Generate documentation from text markup
|
||||
find_program (ASCIIDOCTOR_EXECUTABLE asciidoctor)
|
||||
if (NOT ASCIIDOCTOR_EXECUTABLE)
|
||||
message (FATAL_ERROR "asciidoctor not found")
|
||||
find_program (A2X_EXECUTABLE a2x)
|
||||
if (NOT ASCIIDOCTOR_EXECUTABLE AND NOT A2X_EXECUTABLE)
|
||||
message (WARNING "Neither asciidoctor nor a2x were found, "
|
||||
"falling back to a substandard manual page generator")
|
||||
endif ()
|
||||
|
||||
foreach (page ${PROJECT_NAME})
|
||||
set (page_output "${PROJECT_BINARY_DIR}/${page}.1")
|
||||
list (APPEND project_MAN_PAGES "${page_output}")
|
||||
add_custom_command (OUTPUT ${page_output}
|
||||
COMMAND ${ASCIIDOCTOR_EXECUTABLE} -b manpage
|
||||
-a release-version=${PROJECT_VERSION}
|
||||
"${PROJECT_SOURCE_DIR}/${page}.adoc"
|
||||
-o "${page_output}"
|
||||
DEPENDS ${page}.adoc
|
||||
COMMENT "Generating man page for ${page}" VERBATIM)
|
||||
if (ASCIIDOCTOR_EXECUTABLE)
|
||||
add_custom_command (OUTPUT ${page_output}
|
||||
COMMAND ${ASCIIDOCTOR_EXECUTABLE} -b manpage
|
||||
-a release-version=${PROJECT_VERSION}
|
||||
-o "${page_output}"
|
||||
"${PROJECT_SOURCE_DIR}/${page}.adoc"
|
||||
DEPENDS ${page}.adoc
|
||||
COMMENT "Generating man page for ${page}" VERBATIM)
|
||||
elseif (A2X_EXECUTABLE)
|
||||
add_custom_command (OUTPUT ${page_output}
|
||||
COMMAND ${A2X_EXECUTABLE} --doctype manpage --format manpage
|
||||
-a release-version=${PROJECT_VERSION}
|
||||
-D "${PROJECT_BINARY_DIR}"
|
||||
"${PROJECT_SOURCE_DIR}/${page}.adoc"
|
||||
DEPENDS ${page}.adoc
|
||||
COMMENT "Generating man page for ${page}" VERBATIM)
|
||||
else ()
|
||||
set (ASCIIMAN ${PROJECT_SOURCE_DIR}/liberty/tools/asciiman.awk)
|
||||
add_custom_command (OUTPUT ${page_output}
|
||||
COMMAND env LC_ALL=C asciidoc-release-version=${PROJECT_VERSION}
|
||||
awk -f ${ASCIIMAN} "${PROJECT_SOURCE_DIR}/${page}.adoc"
|
||||
> ${page_output}
|
||||
DEPENDS ${page}.adoc ${ASCIIMAN}
|
||||
COMMENT "Generating man page for ${page}" VERBATIM)
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
add_custom_target (docs ALL DEPENDS ${project_MAN_PAGES})
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
Copyright (c) 2014 - 2022, Přemysl Eric Janouch <p@janouch.name>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
19
NEWS
19
NEWS
@@ -1,3 +1,22 @@
|
||||
1.2.0 (2024-12-24)
|
||||
|
||||
* Add a backend for co-processes, such as language servers
|
||||
|
||||
* Support reading OpenRPC documents from a file
|
||||
|
||||
* Respect the NO_COLOR environment variable
|
||||
|
||||
* Miscellaneous libedit (editline) fixes
|
||||
|
||||
* Miscellaneous portability improvements
|
||||
|
||||
* json-rpc-test-server: implement OpenRPC discovery
|
||||
|
||||
* json-rpc-test-server: only serve regular files
|
||||
|
||||
* json-rpc-test-server: miscellaneous WebSocket fixes
|
||||
|
||||
|
||||
1.1.0 (2020-10-13)
|
||||
|
||||
* Add method name tab completion using OpenRPC information
|
||||
|
||||
18
README.adoc
18
README.adoc
@@ -29,19 +29,17 @@ The rest of this README will concern itself with externalities.
|
||||
|
||||
Packages
|
||||
--------
|
||||
Regular releases are sporadic. git master should be stable enough. You can get
|
||||
a package with the latest development version from Archlinux's AUR.
|
||||
Regular releases are sporadic. git master should be stable enough.
|
||||
You can get a package with the latest development version using Arch Linux's
|
||||
https://aur.archlinux.org/packages/json-rpc-shell-git[AUR],
|
||||
or as a https://git.janouch.name/p/nixexprs[Nix derivation].
|
||||
|
||||
Building
|
||||
--------
|
||||
Build dependencies: CMake, pkg-config, asciidoctor,
|
||||
liberty (included), http-parser (included) +
|
||||
Runtime dependencies: libev, Jansson, cURL, openssl,
|
||||
readline or libedit >= 2013-07-12,
|
||||
|
||||
Avoid libedit if you can, in general it works but at the moment history is
|
||||
acting up and I have no clue about fixing it. Multiline editing is also
|
||||
misbehaving there.
|
||||
Build dependencies: CMake, pkg-config, liberty (included),
|
||||
http-parser (included), asciidoctor or asciidoc (recommended but optional) +
|
||||
Runtime dependencies:
|
||||
libev, Jansson, cURL, openssl, readline or libedit >= 2013-07-12
|
||||
|
||||
$ git clone --recursive https://git.janouch.name/p/json-rpc-shell.git
|
||||
$ mkdir json-rpc-shell/build
|
||||
|
||||
@@ -7,5 +7,7 @@
|
||||
#cmakedefine HAVE_READLINE
|
||||
#cmakedefine HAVE_EDITLINE
|
||||
|
||||
#cmakedefine01 ICONV_ACCEPTS_TRANSLIT
|
||||
|
||||
#endif // ! CONFIG_H
|
||||
|
||||
|
||||
@@ -14,8 +14,9 @@ Synopsis
|
||||
|
||||
Description
|
||||
-----------
|
||||
:colon: :
|
||||
The _ENDPOINT_ must be either an HTTP or a WebSocket URL, with or without TLS
|
||||
(i.e. one of the _+++http+++://_, _+++https+++://_, _ws://_, _wss://_ schemas).
|
||||
(i.e. one of the _http{colon}//_, _https{colon}//_, _ws://_, _wss://_ schemas).
|
||||
|
||||
*json-rpc-shell* will use it to send any JSON-RPC 2.0 requests you enter on its
|
||||
command line. The server's response will be parsed and validated, stripping it
|
||||
@@ -140,8 +141,7 @@ the higher-level protocol (the "Sec-Ws-Protocol" HTTP field).
|
||||
|
||||
Bugs
|
||||
----
|
||||
The editline (libedit) frontend is more of a proof of concept that mostly seems
|
||||
to work but exhibits bugs that are not our fault.
|
||||
The editline (libedit) frontend may exhibit some unexpected behaviour.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* json-rpc-shell.c: a shell for JSON-RPC 2.0
|
||||
*
|
||||
* Copyright (c) 2014 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
* Copyright (c) 2014 - 2022, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -515,6 +515,7 @@ struct input_el
|
||||
char *entered_line; ///< Buffers the entered line
|
||||
|
||||
bool active; ///< Interface has been started
|
||||
bool need_restart; ///< Need to clear history state
|
||||
char *prompt; ///< The prompt we use
|
||||
int prompt_shown; ///< Whether the prompt is shown now
|
||||
|
||||
@@ -540,12 +541,24 @@ input_el_redisplay (struct input_el *self)
|
||||
{
|
||||
// See rl_redisplay(), however NetBSD editline's map.c v1.54 breaks VREPRINT
|
||||
// so we bind redisplay somewhere else in input_el_start()
|
||||
char x[] = { 'q' & 31, 0 };
|
||||
el_push (self->editline, x);
|
||||
wchar_t x[] = { L'q' & 31, 0 };
|
||||
el_wpush (self->editline, x);
|
||||
|
||||
// We have to do this or it gets stuck and nothing is done
|
||||
int count = 0;
|
||||
(void) el_wgets (self->editline, &count);
|
||||
int dummy_count = 0;
|
||||
(void) el_wgets (self->editline, &dummy_count);
|
||||
}
|
||||
|
||||
// Editline keeping its own history position (look for "eventno" there).
|
||||
// This is the only sane way of resetting it.
|
||||
static void
|
||||
input_el_start_over (struct input_el *self)
|
||||
{
|
||||
wchar_t x[] = { L'c' & 31, 0 };
|
||||
el_wpush (self->editline, x);
|
||||
|
||||
int dummy_count = 0;
|
||||
(void) el_wgets (self->editline, &dummy_count);
|
||||
}
|
||||
|
||||
static char *
|
||||
@@ -588,7 +601,7 @@ input_el_on_return (EditLine *editline, int key)
|
||||
int len = info->lastchar - info->buffer;
|
||||
int point = info->cursor - info->buffer;
|
||||
|
||||
wchar_t *line = calloc (sizeof *info->buffer, len + 1);
|
||||
wchar_t *line = xcalloc (len + 1, sizeof *info->buffer);
|
||||
memcpy (line, info->buffer, sizeof *info->buffer * len);
|
||||
|
||||
if (*line)
|
||||
@@ -603,13 +616,14 @@ input_el_on_return (EditLine *editline, int key)
|
||||
self->entered_line = xstrndup
|
||||
(info_mb->buffer, info_mb->lastchar - info_mb->buffer);
|
||||
|
||||
// Now we need to force editline to actually print the newline
|
||||
// Now we need to force editline into actually printing the newline
|
||||
el_cursor (editline, len++ - point);
|
||||
el_insertstr (editline, "\n");
|
||||
input_el_redisplay (self);
|
||||
|
||||
// Finally we need to discard the old line's contents
|
||||
el_wdeletestr (editline, len);
|
||||
self->need_restart = true;
|
||||
return CC_NEWLINE;
|
||||
}
|
||||
|
||||
@@ -673,8 +687,6 @@ input_el_start (struct input *input, const char *program_name)
|
||||
el_set (self->editline, EL_BIND, "^w", "ed-delete-prev-word", NULL);
|
||||
// Just what are you doing?
|
||||
el_set (self->editline, EL_BIND, "^u", "vi-kill-line-prev", NULL);
|
||||
// See input_el_redisplay(), functionally important
|
||||
el_set (self->editline, EL_BIND, "^q", "ed-redisplay", NULL);
|
||||
|
||||
// It's probably better to handle these ourselves
|
||||
input_el_addbind (self->editline, "send-line", "Send line",
|
||||
@@ -690,6 +702,11 @@ input_el_start (struct input *input, const char *program_name)
|
||||
// Source the user's defaults file
|
||||
el_source (self->editline, NULL);
|
||||
|
||||
// See input_el_redisplay(), functionally important
|
||||
el_set (self->editline, EL_BIND, "^q", "ed-redisplay", NULL);
|
||||
// This is what buffered el_wgets() does, functionally important
|
||||
el_set (self->editline, EL_BIND, "^c", "ed-start-over", NULL);
|
||||
|
||||
self->active = true;
|
||||
self->prompt_shown = 1;
|
||||
}
|
||||
@@ -745,7 +762,7 @@ input_el_hide (struct input *input)
|
||||
int len = info->lastchar - info->buffer;
|
||||
int point = info->cursor - info->buffer;
|
||||
|
||||
wchar_t *line = calloc (sizeof *info->buffer, len + 1);
|
||||
wchar_t *line = xcalloc (len + 1, sizeof *info->buffer);
|
||||
memcpy (line, info->buffer, sizeof *info->buffer * len);
|
||||
el_cursor (self->editline, len - point);
|
||||
el_wdeletestr (self->editline, len);
|
||||
@@ -977,6 +994,16 @@ input_el_on_tty_readable (struct input *input)
|
||||
// We bind the return key to process it how we need to
|
||||
struct input_el *self = (struct input_el *) input;
|
||||
|
||||
int unbuffered = 0;
|
||||
(void) el_get (self->editline, EL_UNBUFFERED, &unbuffered);
|
||||
|
||||
// We must invoke ch_reset(), which isn't done for us with EL_UNBUFFERED.
|
||||
if (unbuffered && self->need_restart)
|
||||
{
|
||||
self->need_restart = false;
|
||||
input_el_start_over (self);
|
||||
}
|
||||
|
||||
// el_gets() with EL_UNBUFFERED doesn't work with UTF-8,
|
||||
// we must use the wide-character interface
|
||||
int count = 0;
|
||||
@@ -984,8 +1011,7 @@ input_el_on_tty_readable (struct input *input)
|
||||
|
||||
// Editline works in a funny NO_TTY mode when the input is not a tty,
|
||||
// we cannot use EL_UNBUFFERED and expect sane results then
|
||||
int unbuffered = 0;
|
||||
if (!el_get (self->editline, EL_UNBUFFERED, &unbuffered) && !unbuffered)
|
||||
if (!unbuffered)
|
||||
{
|
||||
char *entered_line = buf ? input_el_wcstombs (buf) : NULL;
|
||||
self->super.on_input (entered_line, self->super.user_data);
|
||||
@@ -1199,7 +1225,7 @@ await_try_cancel (struct app_context *ctx)
|
||||
|
||||
static void on_config_attribute_change (struct config_item *item);
|
||||
|
||||
static struct config_schema g_config_connection[] =
|
||||
static const struct config_schema g_config_connection[] =
|
||||
{
|
||||
{ .name = "tls_ca_file",
|
||||
.comment = "OpenSSL CA bundle file",
|
||||
@@ -1210,7 +1236,7 @@ static struct config_schema g_config_connection[] =
|
||||
{}
|
||||
};
|
||||
|
||||
static struct config_schema g_config_attributes[] =
|
||||
static const struct config_schema g_config_attributes[] =
|
||||
{
|
||||
#define XX(x, y, z) { .name = y, .comment = z, .type = CONFIG_ITEM_STRING, \
|
||||
.on_change = on_config_attribute_change },
|
||||
@@ -4050,11 +4076,10 @@ main (int argc, char *argv[])
|
||||
setlocale (LC_CTYPE, "");
|
||||
|
||||
char *encoding = nl_langinfo (CODESET);
|
||||
#ifdef __linux__
|
||||
// XXX: not quite sure if this is actually desirable
|
||||
// TODO: instead retry with JSON_ENSURE_ASCII
|
||||
encoding = xstrdup_printf ("%s//TRANSLIT", encoding);
|
||||
#endif // __linux__
|
||||
if (ICONV_ACCEPTS_TRANSLIT)
|
||||
encoding = xstrdup_printf ("%s//TRANSLIT", encoding);
|
||||
|
||||
if ((g_ctx.term_from_utf8 = iconv_open (encoding, "UTF-8"))
|
||||
== (iconv_t) -1
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* json-rpc-test-server.c: JSON-RPC 2.0 demo server
|
||||
*
|
||||
* Copyright (c) 2015 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
* Copyright (c) 2015 - 2022, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -1464,7 +1464,7 @@ json_rpc_discover (struct server_context *ctx, json_t *params)
|
||||
|
||||
json_t *info = json_pack ("{ssss}",
|
||||
"title", PROGRAM_NAME, "version", PROGRAM_VERSION);
|
||||
json_t *methods = json_pack ("[ooo]",
|
||||
json_t *methods = json_pack ("[oooo]",
|
||||
open_rpc_describe ("date", json_pack ("{ssso}", "type", "object",
|
||||
"properties", json_pack ("{s{ss}s{ss}s{ss}s{ss}s{ss}s{ss}}",
|
||||
"year", "type", "number",
|
||||
@@ -1475,7 +1475,8 @@ json_rpc_discover (struct server_context *ctx, json_t *params)
|
||||
"seconds", "type", "number"))),
|
||||
open_rpc_describe ("ping", json_pack ("{ss}", "type", "string")),
|
||||
open_rpc_describe ("rpc.discover", json_pack ("{ss}", "$ref",
|
||||
"https://github.com/open-rpc/meta-schema/raw/master/schema.json")));
|
||||
"https://github.com/open-rpc/meta-schema/raw/master/schema.json")),
|
||||
open_rpc_describe ("wait", json_pack ("{ss}", "type", "null")));
|
||||
return json_rpc_response (NULL, json_pack ("{sssoso}",
|
||||
"openrpc", "1.2.6", "info", info, "methods", methods), NULL);
|
||||
}
|
||||
@@ -1492,6 +1493,16 @@ json_rpc_ping (struct server_context *ctx, json_t *params)
|
||||
return json_rpc_response (NULL, json_string ("pong"), NULL);
|
||||
}
|
||||
|
||||
static json_t *
|
||||
json_rpc_wait (struct server_context *ctx, json_t *params)
|
||||
{
|
||||
(void) ctx;
|
||||
(void) params;
|
||||
|
||||
sleep (1);
|
||||
return json_rpc_response (NULL, json_null (), NULL);
|
||||
}
|
||||
|
||||
static json_t *
|
||||
json_rpc_date (struct server_context *ctx, json_t *params)
|
||||
{
|
||||
@@ -1524,6 +1535,7 @@ process_json_rpc_request (struct server_context *ctx, json_t *request)
|
||||
{ "date", json_rpc_date },
|
||||
{ "ping", json_rpc_ping },
|
||||
{ "rpc.discover", json_rpc_discover },
|
||||
{ "wait", json_rpc_wait },
|
||||
};
|
||||
|
||||
if (!json_is_object (request))
|
||||
|
||||
2
liberty
2
liberty
Submodule liberty updated: d71c47f8ce...1930f138d4
Reference in New Issue
Block a user