Debian Codegraph @

rplayd/helper.c

raw symbols ◀ ▶ SCIP
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 <time.h>
43 #include "rplayd.h"
44 #include "helper.h"
45 #include "misc.h"
46
•47 /* Make sure MAXPATHLEN is defined. */
48 #ifndef MAXPATHLEN
49 #define MAXPATHLEN 4096
50 #endif
51
52 HELPER *helpers = NULL;
53 static time_t helper_read_time;
54
55 #ifdef __STDC__
56 void
57 helper_read(char *filename)
58 #else
59 void
60 helper_read(filename)
61 char *filename;
62 #endif
63 {
64 FILE *fp;
65 char buf[MAXPATHLEN];
66 char *pat, *prog, *p, *info;
67 HELPER *hp, *hp_next, *tail;
68 int line = 0;
69 int error;
70
71 helper_read_time = time(0);
72
73 fp = fopen(filename, "r");
74 if (fp == NULL)
75 {
76 return;
77 }
78
79 while (fgets(buf, sizeof(buf), fp) != NULL)
80 {
81 line++;
82
83 switch (buf[0])
84 {
85 case '#':
86 case ' ':
87 case '\t':
88 case '\n':
89 continue;
90 }
91 p = strchr(buf, '\n');
92 if (p)
93 {
94 *p = '\0';
95 }
96
97 hp = (HELPER *) malloc(sizeof(HELPER));
98 if (hp == NULL)
99 {
100 report(REPORT_ERROR, "helper_read: out of memory\n");
101 done(1);
102 }
103 hp->next = NULL;
104 hp->program = NULL;
105 hp->format = 0;
106 hp->sample_rate = 0;
107 hp->precision = 0;
108 hp->channels = 0;
109 hp->byte_order = 0;
110
111 /* pattern */
112 pat = strtok(buf, " \t");
113 if (!pat)
114 {
115 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
116 done(1);
117 }
118 //memset ((char *) &hp->pattern, 0, sizeof (hp->pattern));
•119 error = regcomp(&hp->pattern, pat, REG_ICASE | REG_NOSUB);
120 if (error)
121 {
122 report(REPORT_ERROR, "helper_read: %d line %d\n", error, line);
123 done(1);
124 }
125
126 /* info */
127 info = strtok(NULL, " \t");
128 if (!info)
129 {
130 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
131 done(1);
132 }
133 for (; *info && (*info == ' ' || *info == '\t'); info++) ;
134
135 /* program */
136 prog = strtok(NULL, "");
137 if (!prog)
138 {
139 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
140 done(1);
141 }
142 for (; *prog && (*prog == ' ' || *prog == '\t'); prog++) ;
143 hp->program = strdup(prog);
144
145 /* parse info */
146 p = strtok(info, ",");
147 if (p)
148 hp->format = string_to_audio_format(p);
149 p = strtok(NULL, ",");
150 if (p)
151 hp->sample_rate = atoi(p);
152 p = strtok(NULL, ",");
153 if (p)
154 hp->precision = atoi(p);
155 p = strtok(NULL, ",");
156 if (p)
157 hp->channels = atoi(p);
158 p = strtok(NULL, ",");
159 if (p)
160 hp->byte_order = string_to_byte_order(p);
161 if (!hp->format || !hp->sample_rate || !hp->precision || !hp->channels || !hp->byte_order)
162 {
163 report(REPORT_ERROR, "helper_read: parse error line %d\n", line);
164 done(1);
165 }
166
167 report(REPORT_DEBUG, "adding helper for \"%s\"\n", pat);
168
169 if (helpers == NULL)
170 {
171 helpers = hp;
172 tail = hp;
173 }
174 else
175 {
176 tail->next = hp;
177 tail = hp;
178 }
179 }
180
181 fclose(fp);
182 }
183
184 #ifdef __STDC__
185 HELPER *
186 helper_lookup(char *sound)
187 #else
188 HELPER *
189 helper_lookup(sound)
190 char *sound;
191 #endif
192 {
193 HELPER *hp;
194
195 for (hp = helpers; hp; hp = hp->next)
196 {
•197 if (regexec(&hp->pattern, sound, 0, 0, 0) == 0)
198 {
199 return hp;
200 }
201 }
202 return NULL;
203 }
204
205 #ifdef __STDC__
206 void
207 helper_reread(char *filename)
208 #else
209 void
210 helper_reread(filename)
211 char *filename;
212 #endif
213 {
214 HELPER *hp, *hp_next;
215
216 report(REPORT_DEBUG, "re-reading helpers\n");
217
218 for (hp = helpers; hp; hp = hp_next)
219 {
220 hp_next = hp->next;
221 regfree(&hp->pattern);
222 free(hp->program);
223 }
224
225 helpers = NULL;
226 helper_read(filename);
227 }
228
229 #ifdef __STDC__
230 void
231 helper_stat(char *filename)
232 #else
233 void
234 helper_stat(filename)
235 char *filename;
236 #endif
237 {
238 if (modified(filename, helper_read_time))
239 {
240 helper_reread(filename);
241 }
242 }
243
244 #endif /* HAVE_HELPERS */