simplify open_audiod_pipe(), remove a trailing newline which caused
[paraslash.git] / recv.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18 #include "gcc-compat.h"
19 #include "para.h"
20
21 #include "recv.h"
22 #include "recv.cmdline.h"
23 #include "error.h"
24
25 struct gengetopt_args_info conf;
26
27 INIT_RECV_ERRLISTS;
28
29 __printf_2_3 void para_log(int ll, char* fmt,...)
30 {
31 va_list argp;
32
33 /* ignore log message if loglevel is not high enough */
34 if (ll < conf.loglevel_arg)
35 return;
36 va_start(argp, fmt);
37 vfprintf(stderr, fmt, argp);
38 va_end(argp);
39 }
40
41 static void *parse_config(int argc, char *argv[], int *receiver_num)
42 {
43 int i;
44
45 if (cmdline_parser(argc, argv, &conf))
46 return NULL;
47 if (conf.list_receivers_given) {
48 printf("available receivers: ");
49 for (i = 0; receivers[i].name; i++)
50 printf("%s%s", i? " " : "", receivers[i].name);
51 printf("\nTry para_recv -r<receivername>:-h for help on <receivername>\n");
52 exit(EXIT_SUCCESS);
53 }
54 return check_receiver_arg(conf.receiver_arg, receiver_num);
55 }
56
57 int main(int argc, char *argv[])
58 {
59 int ret, eof = 0, max, r_opened = 0, receiver_num;
60 struct timeval timeout;
61 struct receiver *r = NULL;
62 fd_set rfds, wfds;
63 struct receiver_node rn;
64
65 memset(&rn, 0, sizeof(struct receiver_node));
66 for (ret = 0; receivers[ret].name; ret++)
67 receivers[ret].init(&receivers[ret]);
68 ret = -E_RECV_SYNTAX;
69 rn.conf = parse_config(argc, argv, &receiver_num);
70 if (!rn.conf) {
71 PARA_EMERG_LOG("%s", "parse failed\n");
72 goto out;
73 }
74 r = &receivers[receiver_num];
75 rn.receiver = r;
76 ret = r->open(&rn);
77 if (ret < 0)
78 goto out;
79 r_opened = 1;
80 recv:
81 FD_ZERO(&rfds);
82 FD_ZERO(&wfds);
83 timeout.tv_sec = 0;
84 timeout.tv_usec = 1000 * 1000;
85 max = -1;
86 ret = r->pre_select(&rn, &rfds, &wfds, &timeout);
87 max = MAX(max, ret);
88
89 PARA_DEBUG_LOG("timeout: %lums\n", tv2ms(&timeout));
90 ret = select(max + 1, &rfds, &wfds, NULL, &timeout);
91 if (ret < 0) {
92 if (errno == EINTR || errno == EAGAIN)
93 goto recv;
94 ret = -E_RECV_SELECT;
95 goto out;
96 }
97 ret = r->post_select(&rn, ret, &rfds, &wfds);
98 if (ret < 0)
99 goto out;
100 if (!ret)
101 eof = 1;
102 if (!rn.loaded) {
103 if (eof)
104 goto out;
105 goto recv;
106 }
107 ret = write(STDOUT_FILENO, rn.buf, rn.loaded);
108 PARA_DEBUG_LOG("wrote %d/%d\n", ret, rn.loaded);
109 if (ret < 0) {
110 ret = -E_WRITE_STDOUT;
111 goto out;
112 }
113 if (ret != rn.loaded) {
114 PARA_INFO_LOG("short write %d/%d\n", ret, rn.loaded);
115 memmove(rn.buf, rn.buf + ret, rn.loaded - ret);
116 }
117 rn.loaded -= ret;
118 if (rn.loaded || !eof)
119 goto recv;
120 out:
121 if (r_opened)
122 r->close(&rn);
123 if (r)
124 r->shutdown();
125 if (ret < 0)
126 PARA_NOTICE_LOG("%d: (%s)\n", ret, PARA_STRERROR(-ret));
127 return ret;
128 }