Debian Codegraph @

rplayd/helper.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* $Id: helper.c,v 1.4 1999/03/10 07:58:03 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef HAVE_HELPERS
31
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <unistd.h>
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42 #include "rplayd.h"
43 #include "helper.h"
44 #include "misc.h"
45
46 HELPER *helpers = NULL;
47 static time_t helper_read_time;
48
49 #ifdef __STDC__
50 void
51 helper_read(char *filename)
52 #else
53 void
54 helper_read(filename)
55 char *filename;
56 #endif
57 {
58 FILE *fp;
59 char buf[MAXPATHLEN];
60 char *pat, *prog, *p, *info;
61 HELPER *hp, *hp_next, *tail;
62 int line = 0;
63 int error;
64
65 helper_read_time = time(0);
66
67 fp = fopen(filename, "r");
68 if (fp == NULL)
69 {
70 return;
71 }
72
73 while (fgets(buf, sizeof(buf), fp) != NULL)
74 {
75 line++;
76
77 switch (buf[0])
78 {
79 case '#':
80 case ' ':
81 case '\t':
82 case '\n':
83 continue;
84 }
85 p = strchr(buf, '\n');
86 if (p)
87 {
88 *p = '\0';
89 }
90
91 hp = (HELPER *) malloc(sizeof(HELPER));
92 if (hp == NULL)
93 {
94 report(REPORT_ERROR, "helper_read: out of memory\n");
95 done(1);
96 }
97 hp->next = NULL;
98 hp->program = NULL;
99 hp->format = 0;
100 hp->sample_rate = 0;
101 hp->precision = 0;
102 hp->channels = 0;
103 hp->byte_order = 0;
104
105 /* pattern */
106 pat = strtok(buf, " \t");
107 if (!pat)
108 {
109 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
110 done(1);
111 }
112 //memset ((char *) &hp->pattern, 0, sizeof (hp->pattern));
113 error = regncomp(&hp->pattern, pat, strlen(pat), REG_ICASE | REG_NOSUB);
114 if (error)
115 {
116 report(REPORT_ERROR, "helper_read: %d line %d\n", error, line);
117 done(1);
118 }
119
120 /* info */
121 info = strtok(NULL, " \t");
122 if (!info)
123 {
124 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
125 done(1);
126 }
127 for (; *info && (*info == ' ' || *info == '\t'); info++) ;
128
129 /* program */
130 prog = strtok(NULL, "");
131 if (!prog)
132 {
133 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
134 done(1);
135 }
136 for (; *prog && (*prog == ' ' || *prog == '\t'); prog++) ;
137 hp->program = strdup(prog);
138
139 /* parse info */
140 p = strtok(info, ",");
141 if (p)
142 hp->format = string_to_audio_format(p);
143 p = strtok(NULL, ",");
144 if (p)
145 hp->sample_rate = atoi(p);
146 p = strtok(NULL, ",");
147 if (p)
148 hp->precision = atoi(p);
149 p = strtok(NULL, ",");
150 if (p)
151 hp->channels = atoi(p);
152 p = strtok(NULL, ",");
153 if (p)
154 hp->byte_order = string_to_byte_order(p);
155 if (!hp->format || !hp->sample_rate || !hp->precision || !hp->channels || !hp->byte_order)
156 {
157 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
158 done(1);
159 }
160
161 report(REPORT_DEBUG, "adding helper for \"%s\"\n", pat);
162
163 if (helpers == NULL)
164 {
165 helpers = hp;
166 tail = hp;
167 }
168 else
169 {
170 tail->next = hp;
171 tail = hp;
172 }
173 }
174
175 fclose(fp);
176 }
177
178 #ifdef __STDC__
179 HELPER *
180 helper_lookup(char *sound)
181 #else
182 HELPER *
183 helper_lookup(sound)
184 char *sound;
185 #endif
186 {
187 HELPER *hp;
188
189 for (hp = helpers; hp; hp = hp->next)
190 {
191 if (regnexec(&hp->pattern, sound, strlen(sound), 0, 0, 0) == 0)
192 {
193 return hp;
194 }
195 }
196 return NULL;
197 }
198
199 #ifdef __STDC__
200 void
201 helper_reread(char *filename)
202 #else
203 void
204 helper_reread(filename)
205 char *filename;
206 #endif
207 {
208 HELPER *hp, *hp_next;
209
210 report(REPORT_DEBUG, "re-reading helpers\n");
211
212 for (hp = helpers; hp; hp = hp_next)
213 {
214 hp_next = hp->next;
215 regfree(&hp->pattern);
216 free(hp->program);
217 }
218
219 helpers = NULL;
220 helper_read(filename);
221 }
222
223 #ifdef __STDC__
224 void
225 helper_stat(char *filename)
226 #else
227 void
228 helper_stat(filename)
229 char *filename;
230 #endif
231 {
232 if (modified(filename, helper_read_time))
233 {
234 helper_reread(filename);
235 }
236 }
237
238 #endif /* HAVE_HELPERS */