31c18a31eab3c8c7d79b50f5d59840bfcf0e9ece
[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 para_recv -r<receivername>:-h for help on <receivername>\n");
51                 exit(EXIT_SUCCESS);
52         }
53         return check_receiver_arg(conf.receiver_arg, receiver_num);
54 }
55
56 int main(int argc, char *argv[])
57 {
58         int ret, eof = 0, max, r_opened = 0, receiver_num;
59         struct timeval timeout;
60         struct  receiver *r = NULL;
61         fd_set rfds, wfds;
62         struct receiver_node rn;
63
64         memset(&rn, 0, sizeof(struct receiver_node));
65         for (ret = 0; receivers[ret].name; ret++)
66                 receivers[ret].init(&receivers[ret]);
67         ret = -E_RECV_SYNTAX;
68         rn.conf = parse_config(argc, argv, &receiver_num);
69         if (!rn.conf) {
70                 PARA_EMERG_LOG("%s", "parse failed\n");
71                 goto out;
72         }
73         r = &receivers[receiver_num];
74         rn.receiver = r;
75         ret = r->open(&rn);
76         if (ret < 0)
77                 goto out;
78         r_opened = 1;
79 recv:
80         FD_ZERO(&rfds);
81         FD_ZERO(&wfds);
82         timeout.tv_sec = 0;
83         timeout.tv_usec = 1000 * 1000;
84         max = -1;
85         ret = r->pre_select(&rn, &rfds, &wfds, &timeout);
86         max = MAX(max, ret);
87
88         PARA_DEBUG_LOG("timeout: %lums\n", tv2ms(&timeout));
89         ret = select(max + 1, &rfds, &wfds, NULL, &timeout);
90         if (ret < 0) {
91                 if (errno == EINTR || errno == EAGAIN)
92                         goto recv;
93                 ret = -E_RECV_SELECT;
94                 goto out;
95         }
96         ret = r->post_select(&rn, ret, &rfds, &wfds);
97         if (ret < 0)
98                 goto out;
99         if (!ret)
100                 eof = 1;
101         if (!rn.loaded) {
102                 if (eof)
103                         goto out;
104                 goto recv;
105         }
106         ret = write(STDOUT_FILENO, rn.buf, rn.loaded);
107         PARA_DEBUG_LOG("wrote %d/%zd\n", ret, rn.loaded);
108         if (ret < 0) {
109                 ret = -E_WRITE_STDOUT;
110                 goto out;
111         }
112         if (ret != rn.loaded) {
113                 PARA_INFO_LOG("short write %d/%zd\n", ret, rn.loaded);
114                 memmove(rn.buf, rn.buf + ret, rn.loaded - ret);
115         }
116         rn.loaded -= ret;
117         if (rn.loaded || !eof)
118                 goto recv;
119 out:
120         if (r_opened)
121                 r->close(&rn);
122         if (r)
123                 r->shutdown();
124         if (ret < 0)
125                 PARA_NOTICE_LOG("%d: (%s)\n", ret, PARA_STRERROR(-ret));
126         return ret;
127 }