| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| •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 |
|
| 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 |
|
| 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 |
|
| 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 |