cli: change the redefine logic to read from the stdin

Procedure redefinition is a very cool feature but when other libraries redefine the same function can be very tricky to avoid compilation error.

This PR proposed a change of logic and use a customizzation function definition to read from the stdin, so we can avoid future error at compile time.

However, we could be check also the os or compiler that cause the error and redefine the missing function.

Changelog-None: Fixed compilation error due redefinition procedure.

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
Vincenzo Palazzo
2022-01-12 21:36:56 +01:00
committed by Christian Decker
parent 52b1f5a8f4
commit 35eaaf8e63
6 changed files with 33 additions and 8 deletions

15
cli/config_cli.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef LIGHTNING_CLI_CONFIG_CLI_H
#define LIGHTNING_CLI_CONFIG_CLI_H
#include "config.h"
#include <stdio.h>
#ifndef CLN_TEST
/* Redefinition procedure is a very cool feature, but
if we try to redefine a procedure that is already
redefined somewhere (like read in alpine) we can have
tricky compilation error */
#define cli_read read
#endif
#endif /* LIGHTNING_CLI_CONFIG_CLI_H */

View File

@@ -2,6 +2,7 @@
* Helper to submit via JSON-RPC and get back response.
*/
#include "config.h"
#include "config_cli.h"
#include <ccan/asort/asort.h>
#include <ccan/err/err.h>
#include <ccan/json_escape/json_escape.h>
@@ -15,7 +16,6 @@
#include <common/utils.h>
#include <common/version.h>
#include <libgen.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
@@ -386,7 +386,7 @@ static size_t read_nofail(int fd, void *buf, size_t len)
ssize_t i;
assert(len > 0);
i = read(fd, buf, len);
i = cli_read(fd, buf, len);
if (i == 0)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"reading response: socket closed");
@@ -572,7 +572,7 @@ static void enable_notifications(int fd)
memset(rbuf, 0, sizeof(rbuf));
while (!strends(rbuf, "\n\n")) {
size_t len = strlen(rbuf);
if (read(fd, rbuf + len, sizeof(rbuf) - len) < 0)
if (cli_read(fd, rbuf + len, sizeof(rbuf) - len) < 0)
err(ERROR_TALKING_TO_LIGHTNINGD,
"Reading enable response");
}
@@ -756,7 +756,7 @@ int main(int argc, char *argv[])
while (parserr <= 0) {
/* Read more if parser says, or we have 0 tokens. */
if (parserr == 0 || parserr == JSMN_ERROR_PART) {
ssize_t i = read(fd, resp + off, tal_bytelen(resp) - 1 - off);
ssize_t i = cli_read(fd, resp + off, tal_bytelen(resp) - 1 - off);
if (i == 0)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"reading response: socket closed");

8
cli/test/config_test.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef LIGHTNING_CLI_TEST_CONFIG_TEST_H
#define LIGHTNING_CLI_TEST_CONFIG_TEST_H
#include "config.h"
#define CLN_TEST 1
#endif /* LIGHTNING_CLI_TEST_CONFIG_TEST_H */

View File

@@ -1,4 +1,5 @@
#include "config.h"
#include "config_test.h"
#include <common/amount.h>
#include <common/bigsize.h>
#include <common/channel_id.h>
@@ -18,7 +19,7 @@ int test_printf(const char *format, ...);
int test_chdir(const char *path);
#define main test_main
#define read test_read
#define cli_read test_read
#define socket test_socket
#define connect test_connect
#define getpid test_getpid
@@ -174,4 +175,3 @@ int main(int argc UNUSED, char *argv[])
common_shutdown();
return 0;
}

View File

@@ -1,4 +1,5 @@
#include "config.h"
#include "config_test.h"
#include <common/amount.h>
#include <common/bigsize.h>
#include <common/channel_id.h>
@@ -18,7 +19,7 @@ int test_printf(const char *format, ...);
int test_chdir(const char *path);
#define main test_main
#define read test_read
#define cli_read test_read
#define socket test_socket
#define connect test_connect
#define getpid test_getpid

View File

@@ -1,4 +1,5 @@
#include "config.h"
#include "config_test.h"
#include <common/amount.h>
#include <common/bigsize.h>
#include <common/channel_id.h>
@@ -20,7 +21,7 @@ int test_fputc(int c, FILE *stream);
int test_chdir(const char *path);
#define main test_main
#define read test_read
#define cli_read test_read
#define socket test_socket
#define connect test_connect
#define getpid test_getpid