Convert para_server to lopsub.
[paraslash.git] / recv_common.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file recv_common.c common functions of para_recv and para_audiod */
8
9 #include <regex.h>
10 #include <inttypes.h>
11 #include <lopsub.h>
12
13 #include "recv_cmd.lsg.h"
14 #include "para.h"
15 #include "error.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "buffer_tree.h"
19 #include "recv.h"
20 #include "string.h"
21
22 /**
23  * Call the init function of each paraslash receiver.
24  */
25 void recv_init(void)
26 {
27         int i;
28
29         FOR_EACH_RECEIVER(i) {
30                 const struct lls_command *cmd = RECV_CMD(i);
31                 const struct receiver *r = lls_user_data(cmd);
32                 if (r && r->init)
33                         r->init();
34         }
35 }
36
37 /**
38  * Check if the given string is a valid receiver specifier.
39  *
40  * \param \ra string of the form receiver_name [options...]
41  * \param lprp Filled in on success, undefined else.
42  *
43  * This function checks whether \a ra starts with the name of a receiver,
44  * optionally followed by options for that receiver. If a valid receiver name
45  * was found the remaining part of \a ra is passed to the receiver's config
46  * parser.
47  *
48  * If a NULL pointer or an empty string is passed as the first argument, the
49  * hhtp receiver with no options is assumed.
50  *
51  * \return On success the number of the receiver is returned. On errors, the
52  * function calls exit(EXIT_FAILURE).
53  */
54 int check_receiver_arg(const char *ra, struct lls_parse_result **lprp)
55 {
56         int ret, argc, receiver_num;
57         char *errctx = NULL, **argv;
58         const struct lls_command *cmd;
59
60         *lprp = NULL;
61         if (!ra || !*ra) {
62                 argc = 1;
63                 argv = para_malloc(2 * sizeof(char*));
64                 argv[0] = para_strdup("http");
65                 argv[1] = NULL;
66         } else {
67                 ret = create_argv(ra, " \t\n", &argv);
68                 if (ret < 0) {
69                         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
70                         exit(EXIT_FAILURE);
71                 }
72                 argc = ret;
73         }
74         ret = lls(lls_lookup_subcmd(argv[0], recv_cmd_suite, &errctx));
75         if (ret < 0) {
76                 PARA_EMERG_LOG("%s: %s\n", errctx? errctx : argv[0],
77                         para_strerror(-ret));
78                 exit(EXIT_FAILURE);
79         }
80         receiver_num = ret;
81         cmd = RECV_CMD(receiver_num);
82         ret = lls(lls_parse(argc, argv, cmd, lprp, &errctx));
83         if (ret < 0) {
84                 if (errctx)
85                         PARA_ERROR_LOG("%s\n", errctx);
86                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
87                 exit(EXIT_FAILURE);
88         }
89         ret = receiver_num;
90         free_argv(argv);
91         return ret;
92 }
93
94 /**
95  * Print out the help texts to all receivers.
96  *
97  * \param detailed Whether to print the short or the detailed help.
98  */
99 void print_receiver_helps(bool detailed)
100 {
101         int i;
102
103         printf("\nAvailable receivers: ");
104         FOR_EACH_RECEIVER(i) {
105                 const struct lls_command *cmd = RECV_CMD(i);
106                 printf("%s%s", i? " " : "", lls_command_name(cmd));
107         }
108         printf("\n\n");
109         FOR_EACH_RECEIVER(i) {
110                 const struct lls_command *cmd = RECV_CMD(i);
111                 char *help = detailed? lls_long_help(cmd) : lls_short_help(cmd);
112                 if (!help)
113                         continue;
114                 printf("%s\n", help);
115                 free(help);
116         }
117 }
118
119 /**
120  * Simple pre-select hook, used by all receivers.
121  *
122  * \param s Scheduler info.
123  * \param rn The receiver node.
124  *
125  * This requests a minimal delay from the scheduler if the status of the buffer
126  * tree node indicates an error/eof condition. No file descriptors are added to
127  * the fd sets of \a s.
128  *
129  * \return The status of the btr node of the receiver node, i.e. the return
130  * value of the underlying call to \ref btr_node_status().
131  */
132 int generic_recv_pre_select(struct sched *s, struct receiver_node *rn)
133 {
134         int ret = btr_node_status(rn->btrn, 0, BTR_NT_ROOT);
135
136         if (ret < 0)
137                 sched_min_delay(s);
138         return ret;
139 }