gui: Check return value of para_exec_cmdline_pid().
[paraslash.git] / write_common.c
1 /*
2 * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write_common.c common functions of para_audiod and para_write */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "string.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "write.h"
18 #include "error.h"
19
20 /** the array containing the names of all supported writers */
21 const char *writer_names[] ={WRITER_NAMES};
22
23 /** the array of supported writers */
24 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
25
26 /**
27 * Call the init function of each supported paraslash writer.
28 */
29 void writer_init(void)
30 {
31 int i;
32
33 FOR_EACH_WRITER(i)
34 writers[i].init(&writers[i]);
35 }
36 /**
37 * Check if given string is a valid command line for any writer.
38 *
39 * \param \wa String of the form writer_name:options.
40 * \param writer_num Contains the number of the writer upon success.
41 *
42 * This function checks whether \a wa starts with the name of a supported
43 * paraslash writer, optionally followed by a colon and any options for that
44 * writer. If a valid writer name was found and further are present, the
45 * remaining part of \a wa is passed to that writer's config parser.
46 *
47 * \return On success, a pointer to the gengetopt args info struct is returned
48 * and \a writer_num contains the number of the writer. Otherwise this function
49 * prints an error message and calls exit().
50 */
51 void *check_writer_arg_or_die(const char *wa, int *writer_num)
52 {
53 int i;
54
55 PARA_INFO_LOG("checking %s\n", wa);
56 FOR_EACH_WRITER(i) {
57 const char *name = writer_names[i];
58 size_t len = strlen(name);
59 char c;
60 if (strlen(wa) < len)
61 continue;
62 if (strncmp(name, wa, len))
63 continue;
64 c = wa[len];
65 if (c && c != ' ')
66 continue;
67 *writer_num = i;
68 return writers[i].parse_config_or_die(c? wa + len + 1 : "");
69 }
70 PARA_EMERG_LOG("invalid writer %s\n", wa);
71 exit(EXIT_FAILURE);
72 }
73
74 /**
75 * Open a writer node and register the corresponding task.
76 *
77 * \param wn The writer node to open.
78 * \param parent The parent btr node (the source for the writer node).
79 * \param s The scheduler instance to register the task to.
80 *
81 * The configuration of the writer node stored in \p wn->conf must be
82 * initialized before this function may be called.
83 */
84 void register_writer_node(struct writer_node *wn, struct btr_node *parent,
85 struct sched *s)
86 {
87 struct writer *w = writers + wn->writer_num;
88 char *name = make_message("%s writer", writer_names[wn->writer_num]);
89
90 wn->btrn = btr_new_node(&(struct btr_node_description)
91 EMBRACE(.name = name, .parent = parent,
92 .handler = w->execute, .context = wn));
93 strcpy(wn->task.status, name);
94 free(name);
95 wn->task.post_select = w->post_select;
96 wn->task.pre_select = w->pre_select;
97 register_task(s, &wn->task);
98 }
99
100 /**
101 * Print the help text of all writers to stdout.
102 *
103 * \param detailed Whether to print the detailed help text.
104 */
105 void print_writer_helps(int detailed)
106 {
107 int i;
108
109 printf_or_die("\nAvailable writers: \n\t");
110 FOR_EACH_WRITER(i)
111 printf_or_die("%s%s", i? " " : "", writer_names[i]);
112 printf_or_die("\n\n");
113 FOR_EACH_WRITER(i) {
114 struct writer *w = writers + i;
115
116 if (!w->help.short_help)
117 continue;
118 printf_or_die("Options for %s:\n", writer_names[i]);
119 ggo_print_help(&w->help, detailed);
120 }
121 }
122
123 static void get_btr_value(struct btr_node *btrn, const char *cmd,
124 int32_t *result)
125 {
126 char *buf = NULL;
127 int ret = btr_exec_up(btrn, cmd, &buf);
128
129 if (ret < 0) {
130 /*
131 * This really should not happen. It means one of our parent
132 * nodes died unexpectedly. Proceed with fingers crossed.
133 */
134 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
135 *result = 0;
136 return;
137 }
138 ret = para_atoi32(buf, result);
139 assert(ret >= 0);
140 free(buf);
141 }
142
143 /**
144 * Ask parent btr nodes for the sample rate of the current stream.
145 *
146 * \param btrn Where to start the search.
147 * \param result Filled in by this function.
148 *
149 * This function is assumed to succeed and terminates on errors.
150 */
151 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
152 {
153 get_btr_value(btrn, "sample_rate", result);
154 }
155
156 /**
157 * Ask parent btr nodes for the channel count of the current stream.
158 *
159 * \param btrn See \ref get_btr_sample_rate.
160 * \param result See \ref get_btr_sample_rate.
161 */
162 void get_btr_channels(struct btr_node *btrn, int32_t *result)
163 {
164 get_btr_value(btrn, "channels", result);
165 }
166
167 /**
168 * Ask parent btr nodes for the number of bits per sample and the byte sex.
169 *
170 * \param btrn See \ref get_btr_sample_rate.
171 * \param result Contains the sample format as an enum sample_format type.
172 */
173 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
174 {
175 get_btr_value(btrn, "sample_format", result);
176 }