Merge branch 'master' into my-osx
[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 "list.h"
21 #include "sched.h"
22 #include "recv.h"
23 #include "recv.cmdline.h"
24 #include "fd.h"
25 #include "error.h"
26 #include "stdout.h"
27
28 struct recv_args_info conf;
29
30 INIT_RECV_ERRLISTS;
31
32 __printf_2_3 void para_log(int ll, const char* fmt,...)
33 {
34         va_list argp;
35
36         /* ignore log message if loglevel is not high enough */
37         if (ll < conf.loglevel_arg)
38                 return;
39         va_start(argp, fmt);
40         vfprintf(stderr, fmt, argp);
41         va_end(argp);
42 }
43
44 static void *parse_config(int argc, char *argv[], int *receiver_num)
45 {
46         int i;
47
48         if (recv_cmdline_parser(argc, argv, &conf))
49                 return NULL;
50         if (conf.list_receivers_given) {
51                 printf("available receivers: ");
52                 for (i = 0; receivers[i].name; i++)
53                         printf("%s%s", i? " " : "", receivers[i].name);
54                 printf("\nTry\n\tpara_recv -r '<receivername> -h'\n"
55                         "for help on <receivername>.\n");
56                 exit(EXIT_SUCCESS);
57         }
58         return check_receiver_arg(conf.receiver_arg, receiver_num);
59 }
60
61 void rn_event_handler(struct task *t)
62 {
63         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
64         unregister_task(t);
65 }
66
67 int main(int argc, char *argv[])
68 {
69         int ret, r_opened = 0, receiver_num;
70         struct  receiver *r = NULL;
71         struct receiver_node rn;
72         struct stdout_task sot;
73         struct sched s;
74
75         s.default_timeout.tv_sec = 1;
76         s.default_timeout.tv_usec = 0;
77
78         memset(&rn, 0, sizeof(struct receiver_node));
79         for (ret = 0; receivers[ret].name; ret++)
80                 receivers[ret].init(&receivers[ret]);
81         ret = -E_RECV_SYNTAX;
82         rn.conf = parse_config(argc, argv, &receiver_num);
83         if (!rn.conf) {
84                 PARA_EMERG_LOG("%s", "parse failed\n");
85                 goto out;
86         }
87         r = &receivers[receiver_num];
88         rn.receiver = r;
89         ret = r->open(&rn);
90         if (ret < 0)
91                 goto out;
92         r_opened = 1;
93
94         stdout_set_defaults(&sot);
95         sot.buf = rn.buf;
96         sot.loaded = &rn.loaded;
97         sot.input_eof = &rn.eof;
98         register_task(&sot.task);
99
100         rn.task.private_data = &rn;
101         rn.task.pre_select = r->pre_select;
102         rn.task.post_select = r->post_select;
103         rn.task.event_handler = rn_event_handler;
104         sprintf(rn.task.status, "receiver node");
105         register_task(&rn.task);
106
107
108         ret = sched(&s);
109 out:
110         if (r_opened)
111                 r->close(&rn);
112         if (r)
113                 r->shutdown();
114         if (ret < 0)
115                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
116         return ret;
117 }