mood.c: Remove some superfluous includes.
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 #include <lopsub.h>
12
13 #include "recv_cmd.lsg.h"
14 #include "recv.lsg.h"
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "buffer_tree.h"
19 #include "recv.h"
20 #include "fd.h"
21 #include "string.h"
22 #include "error.h"
23 #include "stdout.h"
24 #include "version.h"
25
26 /** Array of error strings. */
27 DEFINE_PARA_ERRLIST;
28
29 #define CMD_PTR (lls_cmd(0, recv_suite))
30 #define OPT_RESULT(_name, _lpr) \
31         (lls_opt_result(LSG_RECV_PARA_RECV_OPT_ ## _name, lpr))
32 #define OPT_GIVEN(_name, _lpr) (lls_opt_given(OPT_RESULT(_name, _lpr)))
33 #define OPT_UINT32_VAL(_name, _lpr) (lls_uint32_val(0, OPT_RESULT(_name, _lpr)))
34 #define OPT_STRING_VAL(_name, _lpr) (lls_string_val(0, OPT_RESULT(_name, _lpr)))
35
36 static int loglevel;
37 /** Always log to stderr. */
38 INIT_STDERR_LOGGING(loglevel);
39
40 static void handle_help_flag(struct lls_parse_result *lpr)
41 {
42         char *help;
43
44         if (OPT_GIVEN(DETAILED_HELP, lpr))
45                 help = lls_long_help(CMD_PTR);
46         else if (OPT_GIVEN(HELP, lpr))
47                 help = lls_short_help(CMD_PTR);
48         else
49                 return;
50         printf("%s\n", help);
51         free(help);
52         print_receiver_helps(OPT_GIVEN(DETAILED_HELP, lpr));
53         exit(EXIT_SUCCESS);
54 }
55
56 /**
57  * The main function of para_recv.
58  *
59  * \param argc number of arguments
60  * \param argv vector of arguments
61  *
62  * para_recv uses the specified receiver to receive an audio stream sent by
63  * para_server. The received data is written to stdout.
64  *
65  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
66  */
67 int main(int argc, char *argv[])
68 {
69         int ret;
70         const struct receiver *r = NULL;
71         struct receiver_node rn;
72         struct stdout_task sot = {.btrn = NULL};
73         static struct sched s;
74         struct task_info ti;
75         const struct lls_command *cmd;
76         struct lls_parse_result *lpr; /* command line */
77         struct lls_parse_result *receiver_lpr; /* receiver specific options */
78         char *errctx;
79
80         ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
81         if (ret < 0)
82                 goto out;
83         loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
84         version_handle_flag("recv", OPT_GIVEN(VERSION, lpr));
85         handle_help_flag(lpr);
86         recv_init();
87         memset(&rn, 0, sizeof(struct receiver_node));
88         ret = check_receiver_arg(OPT_STRING_VAL(RECEIVER, lpr), &receiver_lpr);
89         if (ret < 0)
90                 goto free_lpr;
91         cmd = lls_cmd(ret, recv_cmd_suite);
92         r = lls_user_data(cmd);
93         rn.receiver = r;
94         rn.lpr = receiver_lpr;
95         rn.btrn = btr_new_node(&(struct btr_node_description)
96                 EMBRACE(.name = lls_command_name(cmd)));
97         ret = r->open(&rn);
98         if (ret < 0)
99                 goto remove_btrn;
100         sot.btrn = btr_new_node(&(struct btr_node_description)
101                 EMBRACE(.parent = rn.btrn, .name = "stdout"));
102         stdout_task_register(&sot, &s);
103
104         ti.name = lls_command_name(cmd);
105         ti.pre_select = r->pre_select;
106         ti.post_select = r->post_select;
107         ti.context = &rn;
108         rn.task = task_register(&ti, &s);
109
110         s.default_timeout.tv_sec = 1;
111         s.default_timeout.tv_usec = 0;
112         ret = schedule(&s);
113         sched_shutdown(&s);
114         r->close(&rn);
115         btr_remove_node(&sot.btrn);
116 remove_btrn:
117         btr_remove_node(&rn.btrn);
118         lls_free_parse_result(receiver_lpr, cmd);
119 free_lpr:
120         lls_free_parse_result(lpr, CMD_PTR);
121 out:
122         if (ret < 0) {
123                 if (errctx)
124                         PARA_ERROR_LOG("%s\n", errctx);
125                 free(errctx);
126                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
127         }
128         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
129 }