Add some more derived files to .gitignore.
[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 "para.h"
10
11 #include "list.h"
12 #include "sched.h"
13 #include "ggo.h"
14 #include "recv.h"
15 #include "string.h"
16
17 DEFINE_RECEIVER_ARRAY;
18 static void *parse_receiver_args(int receiver_num, char *options)
19 {
20         struct receiver *r = &receivers[receiver_num];
21         char **argv;
22         int argc, i;
23         void *conf;
24
25
26 //      PARA_DEBUG_LOG("%s, options: %s\n", r->name,
27 //              options? options : "(none)");
28         if (options) {
29                 PARA_DEBUG_LOG("options: %s\n", options);
30                 argc = split_args(options, &argv, " \t");
31                 for (i = argc - 1; i >= 0; i--)
32                         argv[i + 1] = argv[i];
33                 argv[0] = para_strdup(r->name);
34                 argc += 1;
35                 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
36         } else {
37                 argc = 1;
38                 argv = para_malloc(2 * sizeof(char*));
39                 argv[0] = NULL;
40                 argv[1] = NULL;
41         }
42         conf = r->parse_config(argc, argv);
43         free(argv[0]);
44         free(argv);
45         return conf;
46 }
47
48 /**
49  * check if given string is a valid command line for any receiver
50  *
51  * \param \ra string of the form receiver_name:options
52  * \param receiver_num contains the number of the receiver upon success
53  *
54  * This function checks whether \a ra starts with the name of a supported
55  * paraslash receiver, optinally followed by a colon and any options for that
56  * receiver. If a valid receiver name was found and further are present, the
57  * remaining part of \a ra is passed to that receiver's config parser.
58  *
59  * \return On success, a pointer to the gengetopt args info struct is returned
60  * and \a receiver_num contains the number of the receiver. Otherwise this function
61  * returns \p NULL.
62  */
63 void *check_receiver_arg(char *ra, int *receiver_num)
64 {
65         int j;
66
67         PARA_DEBUG_LOG("checking %s\n", ra);
68         for (j = 0; receivers[j].name; j++) {
69                 const char *name = receivers[j].name;
70                 size_t len = strlen(name);
71                 char c;
72                 if (strlen(ra) < len)
73                         continue;
74                 if (strncmp(name, ra, len))
75                         continue;
76                 c = ra[len];
77                 if (c && c != ' ')
78                         continue;
79                 if (c && !receivers[j].parse_config)
80                         return NULL;
81                 *receiver_num = j;
82                 return parse_receiver_args(j, c? ra + len + 1: NULL);
83         }
84         PARA_ERROR_LOG("receiver not found\n");
85         return NULL;
86 }
87
88 /**
89  * Print out the help texts to all receivers.
90  *
91  * \param detailed Whether the detailed help should be printed.
92  */
93 void print_receiver_helps(int detailed)
94 {
95         int i;
96
97         printf_or_die("\nAvailable receivers: \n\t");
98         FOR_EACH_RECEIVER(i)
99                 printf_or_die("%s%s", i? " " : "", receivers[i].name);
100         printf_or_die("\n\n");
101         FOR_EACH_RECEIVER(i) {
102                 struct receiver *r = receivers + i;
103
104                 if (!r->help.short_help)
105                         continue;
106                 printf_or_die("Options for %s:\n", r->name);
107                 ggo_print_help(&r->help, detailed);
108         }
109 }