2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
23 #include "recv.cmdline.h"
28 struct gengetopt_args_info conf;
32 __printf_2_3 void para_log(int ll, const char* fmt,...)
36 /* ignore log message if loglevel is not high enough */
37 if (ll < conf.loglevel_arg)
40 vfprintf(stderr, fmt, argp);
44 static void *parse_config(int argc, char *argv[], int *receiver_num)
48 if (cmdline_parser(argc, argv, &conf))
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");
58 return check_receiver_arg(conf.receiver_arg, receiver_num);
62 int main(int argc, char *argv[])
64 int ret, eof = 0, max, r_opened = 0, receiver_num;
65 struct timeval timeout;
66 struct receiver *r = NULL;
68 struct receiver_node rn;
70 memset(&rn, 0, sizeof(struct receiver_node));
71 for (ret = 0; receivers[ret].name; ret++)
72 receivers[ret].init(&receivers[ret]);
74 rn.conf = parse_config(argc, argv, &receiver_num);
76 PARA_EMERG_LOG("%s", "parse failed\n");
79 r = &receivers[receiver_num];
89 timeout.tv_usec = 999 * 1000;
91 ret = r->pre_select(&rn, &rfds, &wfds, &timeout);
92 max = PARA_MAX(max, ret);
94 PARA_DEBUG_LOG("timeout: %lums, max: %d\n", tv2ms(&timeout), max);
95 ret = para_select(max + 1, &rfds, &wfds, &timeout);
100 ret = r->post_select(&rn, ret, &rfds, &wfds);
110 ret = write(STDOUT_FILENO, rn.buf, rn.loaded);
111 PARA_DEBUG_LOG("wrote %d/%zd\n", ret, rn.loaded);
113 ret = -E_WRITE_STDOUT;
116 if (ret != rn.loaded) {
117 PARA_INFO_LOG("short write %d/%zd\n", ret, rn.loaded);
118 memmove(rn.buf, rn.buf + ret, rn.loaded - ret);
121 if (rn.loaded || !eof)
129 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
134 void rn_event_handler(struct task *t)
136 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
140 int main(int argc, char *argv[])
142 int ret, r_opened = 0, receiver_num;
143 struct receiver *r = NULL;
144 struct receiver_node rn;
145 struct stdout_task sot;
149 s.default_timeout.tv_sec = 1;
150 s.default_timeout.tv_usec = 0;
152 memset(&rn, 0, sizeof(struct receiver_node));
153 for (ret = 0; receivers[ret].name; ret++)
154 receivers[ret].init(&receivers[ret]);
155 ret = -E_RECV_SYNTAX;
156 rn.conf = parse_config(argc, argv, &receiver_num);
158 PARA_EMERG_LOG("%s", "parse failed\n");
161 r = &receivers[receiver_num];
168 stdout_set_defaults(&sot);
170 sot.loaded = &rn.loaded;
172 register_task(&sot.task);
174 rn.task.private_data = &rn;
175 rn.task.pre_select = r->pre_select;
176 rn.task.post_select = r->post_select;
177 rn.task.event_handler = rn_event_handler;
179 sprintf(rn.task.status, "receiver node");
180 register_task(&rn.task);
190 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));