Compare commits

..

No commits in common. "2dfbf934f4bc1e4018bcd0684b7dba294eeac55a" and "5dc7e334b241726dc72bebe693e6fe0a77c914b2" have entirely different histories.

2 changed files with 14 additions and 78 deletions

View File

@ -1,36 +1,18 @@
#include "common.h" #include "common.h"
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <sys/select.h>
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) { int main(int argc, char **argv) {
struct sockaddr_un sa_server, sa_client;
int sock_fd; int sock_fd;
static char buffer[BUFFER_SIZE]; static char buffer[BUFFER_SIZE];
EXPECT(sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0), > 0); 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_server, 0, sizeof(sa_server));
memset(&sa_client, 0, sizeof(sa_client)); memset(&sa_client, 0, sizeof(sa_client));
@ -40,38 +22,6 @@ int main(int argc, char **argv) {
EXPECT(bind(sock_fd, (struct sockaddr *)&sa_client, sizeof(struct sockaddr_un)), == 0); EXPECT(bind(sock_fd, (struct sockaddr *)&sa_client, sizeof(struct sockaddr_un)), == 0);
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++) { for (int i = 1; i < argc; i++) {
strncat(buffer, argv[i], BUFFER_SIZE - strlen(buffer) - 1); strncat(buffer, argv[i], BUFFER_SIZE - strlen(buffer) - 1);
if (i != argc - 1) { if (i != argc - 1) {
@ -80,12 +30,13 @@ int main(int argc, char **argv) {
} }
printf("send: '%s'\n", 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)); EXPECT(sendto(sock_fd, buffer, strlen(buffer), 0, (struct sockaddr *)&sa_server, sizeof(sa_server)), == (ssize_t)strlen(buffer));
int data_len; int data_len;
while ((data_len = recv(sock_fd, buffer, BUFFER_SIZE, 0)) > 0) { while ((data_len = recv(sock_fd, buffer, BUFFER_SIZE, 0)) > 0) {
printf("%.*s", data_len, buffer); printf("%.*s", data_len, buffer);
} }
}
unlink(sa_client.sun_path);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -52,7 +52,6 @@ ssize_t send_data(struct msg_target tgt, const void *data, ssize_t len);
ssize_t send_txt(struct msg_target tgt, const char *fmt, ...); ssize_t send_txt(struct msg_target tgt, const char *fmt, ...);
void execute_command(struct msg_target tgt, const char *command, const char *params); void execute_command(struct msg_target tgt, const char *command, const char *params);
bool execute_file(struct msg_target tgt, const char *path, int depth); bool execute_file(struct msg_target tgt, const char *path, int depth);
void send_help(struct msg_target tgt);
bool ipc_init(const char *sock_path); bool ipc_init(const char *sock_path);
void ipc_loop(void); void ipc_loop(void);
@ -191,7 +190,6 @@ ssize_t send_txt(struct msg_target tgt, const char *fmt, ...) {
void execute_command(struct msg_target tgt, const char *command, const char *params) { 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(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")) { if (0 == strcmp(command, "load")) {
const char *key = params; const char *key = params;
@ -306,25 +304,12 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
send_txt(tgt, ".pool.use = %d\n", sounds_pool.use); send_txt(tgt, ".pool.use = %d\n", sounds_pool.use);
send_txt(tgt, ".pool.cap = %d\n", sounds_pool.cap); send_txt(tgt, ".pool.cap = %d\n", sounds_pool.cap);
#endif #endif
} else if (0 == strcmp(command, "help")) {
send_help(tgt);
} else if (0 == strcmp(command, "meow")) {
send_txt(tgt, "mrr~\n");
} else if (0 == strcmp(command, "")) { } else if (0 == strcmp(command, "")) {
} else { } else {
send_txt(tgt, "ERR: unknown command: %s\n", command); send_txt(tgt, "ERR: unknown command: %s\n", command);
} // commands } // commands
} }
void send_help(struct msg_target tgt) {
send_txt(tgt, "load NAME PATH load PATH sound as NAME\n");
send_txt(tgt, "play NAME play previously loaded NAME\n");
send_txt(tgt, "play:rs NAME play _ _ _ with random pitch\n");
send_txt(tgt, "set:pitch NAME MIN MAX set pitch range for NAME\n");
send_txt(tgt, "set:volume NAME VOLUME set loudness for NAME\n");
send_txt(tgt, "source PATH load instructions from PATH\n");
}
static char buffer_exec_file[BUFFER_SIZE]; static char buffer_exec_file[BUFFER_SIZE];
bool execute_file(struct msg_target tgt, const char *path, int depth) { bool execute_file(struct msg_target tgt, const char *path, int depth) {
send_txt(tgt, "DBG: soucing file %s at depth %d\n", path, depth); send_txt(tgt, "DBG: soucing file %s at depth %d\n", path, depth);