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