Fix para_client's --loglevel option.
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file recv.c the stand-alone audio stream receiver */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "recv.h"
17 #include "recv.cmdline.h"
18 #include "fd.h"
19 #include "string.h"
20 #include "error.h"
21 #include "stdout.h"
22
23 /** the gengetopt args info struct */
24 struct recv_args_info conf;
25
26 static int loglevel;
27 /** Always log to stderr. */
28 INIT_STDERR_LOGGING(loglevel);
29
30 /** init array of error codes used by para_recv */
31 INIT_RECV_ERRLISTS;
32
33 __noreturn static void print_help_and_die(void)
34 {
35         int d = conf.detailed_help_given;
36         const char **p = d? recv_args_info_detailed_help
37                 : recv_args_info_help;
38
39         printf_or_die("%s\n\n", RECV_CMDLINE_PARSER_PACKAGE "-"
40                 RECV_CMDLINE_PARSER_VERSION);
41         printf_or_die("%s\n\n", recv_args_info_usage);
42         for (; *p; p++)
43                 printf_or_die("%s\n", *p);
44         print_receiver_helps(d);
45         exit(0);
46 }
47
48 static void *parse_config(int argc, char *argv[], int *receiver_num)
49 {
50         if (recv_cmdline_parser(argc, argv, &conf))
51                 return NULL;
52         HANDLE_VERSION_FLAG("recv", conf);
53         if (conf.help_given || conf.detailed_help_given)
54                 print_help_and_die();
55         return check_receiver_arg(conf.receiver_arg, receiver_num);
56 }
57
58 /**
59  * the main function of para_recv
60  *
61  * \param argc number of arguments
62  * \param argv vector of arguments
63  *
64  * para_recv uses the specified receiver to receive an audio stream sent by
65  * para_server. The received data is written to stdout.
66  *
67  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
68  */
69 int main(int argc, char *argv[])
70 {
71         int ret, r_opened = 0, receiver_num;
72         struct receiver *r = NULL;
73         struct receiver_node rn;
74         struct stdout_task sot;
75         static struct sched s;
76
77         s.default_timeout.tv_sec = 1;
78         s.default_timeout.tv_usec = 0;
79
80         memset(&sot, 0, sizeof(struct stdout_task));
81         memset(&rn, 0, sizeof(struct receiver_node));
82         FOR_EACH_RECEIVER(ret)
83                 receivers[ret].init(&receivers[ret]);
84         ret = -E_RECV_SYNTAX;
85         rn.conf = parse_config(argc, argv, &receiver_num);
86         if (!rn.conf) {
87                 PARA_EMERG_LOG("parse failed\n");
88                 goto out;
89         }
90         loglevel = get_loglevel_by_name(conf.loglevel_arg);
91         r = &receivers[receiver_num];
92         rn.receiver = r;
93         ret = r->open(&rn);
94         if (ret < 0)
95                 goto out;
96         r_opened = 1;
97
98         stdout_set_defaults(&sot);
99         sot.buf = rn.buf;
100         sot.loaded = &rn.loaded;
101         sot.input_error = &rn.task.error;
102         register_task(&sot.task);
103
104         rn.task.pre_select = r->pre_select;
105         rn.task.post_select = r->post_select;
106         sprintf(rn.task.status, "receiver node");
107         register_task(&rn.task);
108
109         ret = schedule(&s);
110 out:
111         if (r_opened)
112                 r->close(&rn);
113         if (r)
114                 r->shutdown();
115         if (ret < 0)
116                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
117         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
118 }