vss: Update offset directly when setting the REPOS flag.
[paraslash.git] / recv_common.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
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 <stdbool.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "recv.h"
17 #include "string.h"
18 #include "buffer_tree.h"
19
20 DEFINE_RECEIVER_ARRAY;
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                 receivers[i].init(&receivers[i]);
31 }
32
33 static void *parse_receiver_args(int receiver_num, char *options)
34 {
35         struct receiver *r = &receivers[receiver_num];
36         char **argv;
37         int argc, i;
38         void *conf;
39
40
41         PARA_DEBUG_LOG("%s, options: %s\n", r->name,
42                 options? options : "(none)");
43         if (options) {
44                 PARA_DEBUG_LOG("options: %s\n", options);
45                 argc = create_argv(options, " \t", &argv);
46                 if (argc < 0)
47                         return NULL;
48                 for (i = argc - 1; i >= 0; i--)
49                         argv[i + 1] = argv[i];
50                 argv[0] = NULL;
51                 argc++;
52         } else {
53                 argc = 1;
54                 argv = para_malloc(2 * sizeof(char*));
55                 argv[0] = NULL;
56                 argv[1] = NULL;
57         }
58         conf = r->parse_config(argc, argv);
59         for (i = 1; i < argc; i++)
60                 free(argv[i]);
61         free(argv);
62         return conf;
63 }
64
65 /**
66  * check if given string is a valid command line for any receiver
67  *
68  * \param \ra string of the form receiver_name:options
69  * \param receiver_num contains the number of the receiver upon success
70  *
71  * This function checks whether \a ra starts with the name of a supported
72  * paraslash receiver, optinally followed by a colon and any options for that
73  * receiver. If a valid receiver name was found and further are present, the
74  * remaining part of \a ra is passed to that receiver's config parser.
75  *
76  * \return On success, a pointer to the gengetopt args info struct is returned
77  * and \a receiver_num contains the number of the receiver. Otherwise this function
78  * returns \p NULL.
79  */
80 void *check_receiver_arg(char *ra, int *receiver_num)
81 {
82         int j;
83
84         PARA_DEBUG_LOG("checking %s\n", ra);
85         for (j = 0; receivers[j].name; j++) {
86                 const char *name = receivers[j].name;
87                 size_t len = strlen(name);
88                 char c;
89                 if (strlen(ra) < len)
90                         continue;
91                 if (strncmp(name, ra, len))
92                         continue;
93                 c = ra[len];
94                 if (c && c != ' ')
95                         continue;
96                 if (c && !receivers[j].parse_config)
97                         return NULL;
98                 *receiver_num = j;
99                 return parse_receiver_args(j, c? ra + len + 1: NULL);
100         }
101         PARA_ERROR_LOG("receiver not found\n");
102         return NULL;
103 }
104
105 /**
106  * Print out the help texts to all receivers.
107  *
108  * \param detailed Whether the detailed help should be printed.
109  */
110 void print_receiver_helps(int detailed)
111 {
112         int i;
113
114         printf_or_die("\nAvailable receivers: \n\t");
115         FOR_EACH_RECEIVER(i)
116                 printf_or_die("%s%s", i? " " : "", receivers[i].name);
117         printf_or_die("\n\n");
118         FOR_EACH_RECEIVER(i) {
119                 struct receiver *r = receivers + i;
120                 if (!r->help.short_help)
121                         continue;
122                 printf_or_die("Options for %s:\n", r->name);
123                 ggo_print_help(&r->help, detailed);
124         }
125 }
126
127 int generic_recv_pre_select(struct sched *s, struct task *t)
128 {
129         struct receiver_node *rn = container_of(t, struct receiver_node, task);
130         int ret = btr_node_status(rn->btrn, 0, BTR_NT_ROOT);
131
132         t->error = 0;
133         if (ret < 0) {
134                 s->timeout.tv_sec = 0;
135                 s->timeout.tv_usec = 1;
136         }
137         return ret;
138 }