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