Merge commit '1678ee'
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005-2012 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 <regex.h>
10 #include <sys/types.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 #include "buffer_tree.h"
23 #include "version.h"
24
25 /** The gengetopt args info struct. */
26 static struct recv_args_info conf;
27
28 static int loglevel;
29 /** Always log to stderr. */
30 INIT_STDERR_LOGGING(loglevel);
31
32 /** init array of error codes used by para_recv */
33 INIT_RECV_ERRLISTS;
34
35 __noreturn static void print_help_and_die(void)
36 {
37         int d = conf.detailed_help_given;
38         const char **p = d? recv_args_info_detailed_help
39                 : recv_args_info_help;
40
41         printf_or_die("%s\n\n", RECV_CMDLINE_PARSER_PACKAGE "-"
42                 RECV_CMDLINE_PARSER_VERSION);
43         printf_or_die("%s\n\n", recv_args_info_usage);
44         for (; *p; p++)
45                 printf_or_die("%s\n", *p);
46         print_receiver_helps(d);
47         exit(0);
48 }
49
50 static void *parse_config(int argc, char *argv[], int *receiver_num)
51 {
52         if (recv_cmdline_parser(argc, argv, &conf))
53                 return NULL;
54         HANDLE_VERSION_FLAG("recv", conf);
55         if (conf.help_given || conf.detailed_help_given)
56                 print_help_and_die();
57         loglevel = get_loglevel_by_name(conf.loglevel_arg);
58         return check_receiver_arg(conf.receiver_arg, receiver_num);
59 }
60
61 /**
62  * The main function of para_recv.
63  *
64  * \param argc number of arguments
65  * \param argv vector of arguments
66  *
67  * para_recv uses the specified receiver to receive an audio stream sent by
68  * para_server. The received data is written to stdout.
69  *
70  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
71  */
72 int main(int argc, char *argv[])
73 {
74         int ret, r_opened = 0, receiver_num;
75         struct receiver *r = NULL;
76         struct receiver_node rn;
77         struct stdout_task sot;
78         static struct sched s;
79
80         s.default_timeout.tv_sec = 1;
81         s.default_timeout.tv_usec = 0;
82
83         memset(&sot, 0, sizeof(struct stdout_task));
84         memset(&rn, 0, sizeof(struct receiver_node));
85         recv_init();
86         ret = -E_RECV_SYNTAX;
87         rn.conf = parse_config(argc, argv, &receiver_num);
88         if (!rn.conf) {
89                 PARA_EMERG_LOG("parse failed\n");
90                 goto out;
91         }
92         r = &receivers[receiver_num];
93         rn.receiver = r;
94         rn.btrn = btr_new_node(&(struct btr_node_description)
95                 EMBRACE(.name = r->name));
96         ret = r->open(&rn);
97         if (ret < 0)
98                 goto out;
99         r_opened = 1;
100
101         sot.btrn = btr_new_node(&(struct btr_node_description)
102                 EMBRACE(.parent = rn.btrn, .name = "stdout"));
103         stdout_set_defaults(&sot);
104         register_task(&s, &sot.task);
105
106         rn.task.pre_select = r->pre_select;
107         rn.task.post_select = r->post_select;
108         sprintf(rn.task.status, "%s", r->name);
109         register_task(&s, &rn.task);
110
111         ret = schedule(&s);
112 out:
113         if (r_opened)
114                 r->close(&rn);
115         btr_free_node(rn.btrn);
116         btr_free_node(sot.btrn);
117         if (rn.conf)
118                 r->free_config(rn.conf);
119         if (ret < 0)
120                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
121         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
122 }