From 751dc4ee66090c0625e5389a8fa52d0616513291 Mon Sep 17 00:00:00 2001 From: hkc Date: Mon, 22 Apr 2024 15:57:27 +0300 Subject: [PATCH] Added interactive mode --- src/sfxc.c | 77 ++++++++++++++++++++++++++++++++++++++++++++---------- src/sfxd.c | 1 + 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/sfxc.c b/src/sfxc.c index 80401f5..13ae5d3 100644 --- a/src/sfxc.c +++ b/src/sfxc.c @@ -1,18 +1,36 @@ #include "common.h" +#include #include #include #include #include #include #include +#include + +struct sockaddr_un sa_server, sa_client; + +void cleanup(void) { + fprintf(stderr, "[client:dbg] Removing receiving socket.\n"); + if (sa_client.sun_path[0] != '\0') { + unlink(sa_client.sun_path); + } +} + +void on_sigint(int sig) { + (void)sig; + cleanup(); +} int main(int argc, char **argv) { - struct sockaddr_un sa_server, sa_client; int sock_fd; static char buffer[BUFFER_SIZE]; EXPECT(sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0), > 0); + signal(SIGINT, on_sigint); + atexit(cleanup); + memset(&sa_server, 0, sizeof(sa_server)); memset(&sa_client, 0, sizeof(sa_client)); @@ -22,21 +40,52 @@ int main(int argc, char **argv) { EXPECT(bind(sock_fd, (struct sockaddr *)&sa_client, sizeof(struct sockaddr_un)), == 0); - for (int i = 1; i < argc; i++) { - strncat(buffer, argv[i], BUFFER_SIZE - strlen(buffer) - 1); - if (i != argc - 1) { - strncat(buffer, " ", BUFFER_SIZE - strlen(buffer) - 1); + if (argc == 1) { + fprintf(stderr, "[client:inf] You're in the interactive mode. Press Ctrl+C or Ctrl+D to exit.\n"); + + struct timeval timeout; + fd_set fds; + int retval, data_len; + while (1) { + timeout.tv_sec = 1; + timeout.tv_usec = 0; + FD_ZERO(&fds); + FD_SET(STDIN_FILENO, &fds); + FD_SET(sock_fd, &fds); + + EXPECT(retval = select(sock_fd + 1, &fds, 0, 0, &timeout), != -1); + if (retval > 0) { + if (FD_ISSET(STDIN_FILENO, &fds)) { + EXPECT(data_len = read(STDIN_FILENO, buffer, BUFFER_SIZE), != -1); + if (data_len == 0) { + break; + } + buffer[data_len - 1] = '\0'; + EXPECT(sendto(sock_fd, buffer, strlen(buffer), 0, (struct sockaddr *)&sa_server, sizeof(sa_server)), == (ssize_t)strlen(buffer)); + } + + if (FD_ISSET(sock_fd, &fds)) { + while ((data_len = recv(sock_fd, buffer, BUFFER_SIZE, 0)) > 0) { + printf("%.*s", data_len, buffer); + } + } + } + } + } else { + for (int i = 1; i < argc; i++) { + strncat(buffer, argv[i], BUFFER_SIZE - strlen(buffer) - 1); + if (i != argc - 1) { + strncat(buffer, " ", BUFFER_SIZE - strlen(buffer) - 1); + } + } + + printf("send: '%s'\n", buffer); + EXPECT(sendto(sock_fd, buffer, strlen(buffer), 0, (struct sockaddr *)&sa_server, sizeof(sa_server)), == (ssize_t)strlen(buffer)); + int data_len; + while ((data_len = recv(sock_fd, buffer, BUFFER_SIZE, 0)) > 0) { + printf("%.*s", data_len, buffer); } } - printf("send: '%s'\n", buffer); - - EXPECT(sendto(sock_fd, buffer, strlen(buffer), 0, (struct sockaddr *)&sa_server, sizeof(sa_server)), == (ssize_t)strlen(buffer)); - int data_len; - while ((data_len = recv(sock_fd, buffer, BUFFER_SIZE, 0)) > 0) { - printf("%.*s", data_len, buffer); - } - - unlink(sa_client.sun_path); exit(EXIT_SUCCESS); } diff --git a/src/sfxd.c b/src/sfxd.c index 1dcee01..6844f4d 100644 --- a/src/sfxd.c +++ b/src/sfxd.c @@ -190,6 +190,7 @@ ssize_t send_txt(struct msg_target tgt, const char *fmt, ...) { void execute_command(struct msg_target tgt, const char *command, const char *params) { send_txt(tgt, "INF: Received '%s' with '%s'\n", command, params); + send_txt((struct msg_target){ stdout, 0, 0, 0 }, "INF: Received '%s' with '%s'\n", command, params); if (0 == strcmp(command, "load")) { const char *key = params;