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