gui: Output "xxx tag not set" for unset tags rather than empty strings.
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file audioc.c the client program used to connect to para_audiod */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12
13 #include "audioc.cmdline.h"
14 #include "para.h"
15 #include "error.h"
16 #include "net.h"
17 #include "string.h"
18 #include "fd.h"
19
20 INIT_AUDIOC_ERRLISTS;
21
22 /** the gengetopt structure containing command line args */
23 struct audioc_args_info conf;
24
25 static int loglevel;
26 INIT_STDERR_LOGGING(loglevel);
27
28 static char *concat_args(unsigned argc, char * const *argv)
29 {
30 int i; char *buf = NULL;
31 for (i = 0; i < argc; i++) {
32 buf = para_strcat(buf, argv[i]);
33 if (i != argc - 1)
34 buf = para_strcat(buf, "\n");
35 }
36 return buf;
37 }
38
39 static char *configfile_exists(void)
40 {
41 static char *config_file;
42 struct stat statbuf;
43
44
45 if (!config_file) {
46 char *home = para_homedir();
47 config_file = make_message("%s/.paraslash/audioc.conf", home);
48 free(home);
49 }
50 if (!stat(config_file, &statbuf))
51 return config_file;
52 return NULL;
53 }
54
55 /**
56 * the client program to connect to para_audiod
57 *
58 * \param argc usual argument count
59 * \param argv usual argument vector
60 *
61 * It creates a temporary local socket in order to communicate with para_audiod.
62 * Authentication consists in sending a ucred buffer that contains the user id.
63 *
64 * Any output received through the local socket is sent to stdout.
65 *
66 * \return EXIT_SUCCESS or EXIT_FAILURE
67 *
68 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
69 */
70 int main(int argc, char *argv[])
71 {
72 int ret = -E_AUDIOC_SYNTAX, fd;
73 char *cf, *buf = NULL, *args;
74 size_t bufsize, loaded = 0;
75
76 if (audioc_cmdline_parser(argc, argv, &conf))
77 goto out;
78 HANDLE_VERSION_FLAG("audioc", conf);
79 cf = configfile_exists();
80 if (cf) {
81 struct audioc_cmdline_parser_params params = {
82 .override = 0,
83 .initialize = 0,
84 .check_required = 0,
85 .check_ambiguity = 0
86 };
87 if (audioc_cmdline_parser_config_file(cf, &conf, &params)) {
88 fprintf(stderr, "parse error in config file\n");
89 exit(EXIT_FAILURE);
90 }
91 }
92 loglevel = get_loglevel_by_name(conf.loglevel_arg);
93 args = conf.inputs_num?
94 concat_args(conf.inputs_num, conf.inputs) :
95 para_strdup("stat");
96 bufsize = conf.bufsize_arg;
97 buf = para_malloc(bufsize);
98
99 if (conf.socket_given) {
100 ret = create_remote_socket(conf.socket_arg);
101 } else {
102 char *hn = para_hostname(),
103 *socket_name = make_message("/var/paraslash/audiod_socket.%s", hn);
104
105 ret = create_remote_socket(socket_name);
106 free(hn);
107 free(socket_name);
108 }
109 if (ret < 0)
110 goto out;
111 fd = ret;
112 ret = mark_fd_nonblocking(fd);
113 if (ret < 0)
114 goto out;
115 ret = mark_fd_nonblocking(STDOUT_FILENO);
116 if (ret < 0)
117 goto out;
118 ret = send_cred_buffer(fd, args);
119 if (ret < 0)
120 goto out;
121 for (;;) {
122 int max_fileno = -1, check_write = 0;
123 ssize_t len;
124 fd_set rfd, wfd;
125 FD_ZERO(&rfd);
126 FD_ZERO(&wfd);
127 if (loaded < bufsize)
128 para_fd_set(fd, &rfd, &max_fileno);
129 if (loaded > 0) {
130 para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
131 check_write = 1;
132 }
133 ret = -E_AUDIOC_OVERRUN;
134 if (max_fileno < 0)
135 goto out;
136 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
137 if (ret < 0)
138 goto out;
139 if (loaded < bufsize && FD_ISSET(fd, &rfd)) {
140 len = recv_bin_buffer(fd, buf + loaded,
141 bufsize - loaded);
142 if (len <= 0) {
143 ret = len < 0? -E_AUDIOC_READ : 0;
144 goto out;
145 }
146 loaded += len;
147 }
148 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
149 ret = write(STDOUT_FILENO, buf, loaded);
150 if (ret < 0) {
151 ret = -E_AUDIOC_WRITE;
152 goto out;
153 }
154 loaded -= ret;
155 if (loaded && ret)
156 memmove(buf, buf + ret, loaded);
157 }
158 }
159 out:
160 if (!ret && loaded && buf)
161 ret = write(STDOUT_FILENO, buf, loaded);
162 if (ret < 0)
163 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
164 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
165 }