simplify osx_write_post_select()
[paraslash.git] / audioc.c
index 23f67fb..0f4c5c5 100644 (file)
--- a/audioc.c
+++ b/audioc.c
 #include "para.h"
 #include "net.h"
 #include "string.h"
+#include "fd.h"
+#include "error.h"
 
-struct gengetopt_args_info conf;
+INIT_AUDIOC_ERRLISTS;
+
+struct audioc_args_info conf;
 char *tmpfifo;
-enum {E_SYNTAX, E_READ, E_WRITE, E_SOCKET, E_INIT_SOCK_ADDR, E_CONNECT, E_CREDENTIALS, E_SELECT, E_OVERRUN};
 
 
-void para_log(__unused int ll, __unused char* fmt,...) /* no logging */
+/*
+ * client log function
+ */
+void para_log(int ll, const char* fmt,...)
 {
-}
+       va_list argp;
 
-/* audioc does not use encryption */
-void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
-void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
+       /* ignore log message if loglevel is not high enough */
+       if (ll < conf.loglevel_arg)
+               return;
+       va_start(argp, fmt);
+       vfprintf(stderr, fmt, argp);
+       va_end(argp);
+}
 
 static char *concat_args(const int argc, char * const *argv)
 {
@@ -50,7 +60,7 @@ static char *concat_args(const int argc, char * const *argv)
 static char *configfile_exists(void)
 {
        static char *config_file;
-        struct stat statbuf;
+       struct stat statbuf;
 
 
        if (!config_file) {
@@ -58,7 +68,7 @@ static char *configfile_exists(void)
                config_file = make_message("%s/.paraslash/audioc.conf", home);
                free(home);
        }
-        if (!stat(config_file, &statbuf))
+       if (!stat(config_file, &statbuf))
                return config_file;
        return NULL;
 }
@@ -66,16 +76,16 @@ static char *configfile_exists(void)
 int main(int argc, char *argv[])
 {
        struct sockaddr_un unix_addr;
-       int ret = -E_SYNTAX, fd, loaded = 0;
+       int ret = -E_AUDIOC_SYNTAX, fd, loaded = 0;
        char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
                *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
 
 
-       if (cmdline_parser(argc, argv, &conf))
+       if (audioc_cmdline_parser(argc, argv, &conf))
                goto out;
        cf = configfile_exists();
        if (cf) {
-               if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
+               if (audioc_cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
                        fprintf(stderr, "parse error in config file\n");
                        exit(EXIT_FAILURE);
                }
@@ -96,19 +106,19 @@ int main(int argc, char *argv[])
                tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
                        home, hn, randname);
 
-       ret = -E_SOCKET;
-       fd = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
+       ret = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
        unlink(tmpsocket_name);
-       if (fd < 0)
+       if (ret < 0)
                goto out;
+       fd = ret;
        ret = -E_INIT_SOCK_ADDR;
        if (init_unix_addr(&unix_addr, socket_name) < 0)
                goto out;
-       ret = - E_CONNECT;
+       ret = - E_AUDIOC_CONNECT;
        if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
                goto out;
-       ret = -E_CREDENTIALS;
-       if (send_cred_buffer(fd, args) < 0)
+       ret = send_cred_buffer(fd, args);
+       if (ret < 0)
                goto out;
        for (;;) {
                int max_fileno = -1, check_write = 0;
@@ -116,30 +126,23 @@ int main(int argc, char *argv[])
                fd_set rfd, wfd;
                FD_ZERO(&rfd);
                FD_ZERO(&wfd);
-               if (loaded && loaded > 10000)
-                       fprintf(stderr, "loaded: %d\n", loaded);
-               if (loaded < conf.bufsize_arg) {
-                       FD_SET(fd, &rfd);
-                       max_fileno = MAX(max_fileno, fd);
-               }
+               if (loaded < conf.bufsize_arg)
+                       para_fd_set(fd, &rfd, &max_fileno);
                if (loaded > 0) {
-                       FD_SET(STDOUT_FILENO, &wfd);
-                       max_fileno = MAX(max_fileno, STDOUT_FILENO);
+                       para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
                        check_write = 1;
                }
-               ret = -E_OVERRUN;
+               ret = -E_AUDIOC_OVERRUN;
                if (max_fileno < 0)
                        goto out;
-               ret = select(max_fileno + 1, &rfd, &wfd, NULL, NULL);
-               if (ret < 0) {
-                       ret = -E_SELECT;
+               ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
+               if (ret < 0)
                        goto out;
-               }
                if (loaded < conf.bufsize_arg && FD_ISSET(fd, &rfd)) {
-                       len = recv_bin_buffer(fd, buf + loaded, 
+                       len = recv_bin_buffer(fd, buf + loaded,
                                conf.bufsize_arg - loaded);
                        if (len <= 0) {
-                               ret = len < 0? -E_READ : 0;
+                               ret = len < 0? -E_AUDIOC_READ : 0;
                                goto out;
                        }
                        loaded += len;
@@ -147,7 +150,7 @@ int main(int argc, char *argv[])
                if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
                        ret = write(STDOUT_FILENO, buf, loaded);
                        if (ret < 0) {
-                               ret = -E_WRITE;
+                               ret = -E_AUDIOC_WRITE;
                                goto out;
                        }
                        loaded -= ret;
@@ -156,5 +159,7 @@ int main(int argc, char *argv[])
 out:
        if (!ret && loaded && buf)
                ret = write(STDOUT_FILENO, buf, loaded);
-       return ret < 0? -ret : EXIT_SUCCESS;
+       if (ret < 0)
+               PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+       return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
 }