13c5c420d35059d80fa25ba4e6c9cb73551f9b06
[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 "para.h"
19
20 #include "recv.h"
21 #include "recv.cmdline.h"
22 #include "error.h"
23
24 struct gengetopt_args_info conf;
25
26 INIT_RECV_ERRLISTS;
27
28 __printf_2_3 void para_log(int ll, char* fmt,...)
29 {
30         va_list argp;
31
32         /* ignore log message if loglevel is not high enough */
33         if (ll < conf.loglevel_arg)
34                 return;
35         va_start(argp, fmt);
36         vfprintf(stderr, fmt, argp);
37         va_end(argp);
38 }
39
40 static void *parse_config(int argc, char *argv[], int *receiver_num)
41 {
42         int i;
43
44         if (cmdline_parser(argc, argv, &conf))
45                 return NULL;
46         if (conf.list_receivers_given) {
47                 printf("available receivers: ");
48                 for (i = 0; receivers[i].name; i++)
49                         printf("%s%s", i? " " : "", receivers[i].name);
50                 printf("\nTry\n\tpara_recv -r '<receivername> -h'\n"
51                         "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/%zd\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/%zd\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_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
127         return ret;
128 }