sad

simple audio daemon
git clone git://git.2f30.org/sad.git
Log | Files | Refs | LICENSE

commit 7cff0b9615c4e8e594eefe0be5d15679d938f559
parent 9b18afe1d83984c90ba93b96d588d178edee4217
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat Dec 27 00:35:15 +0100

cmd: add regex search "search"

Diffstat:
cmd.c | 13++++++++++++-
playlist.c | 35+++++++++++++++++++++++++++++++++++
sad.h | 1+
3 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/cmd.c b/cmd.c @@ -229,7 +229,6 @@ cmdplaylist(int fd, char *arg) dprintf(fd, "ERR unexpected argument\n"); return; } - dumpplaylist(fd); dprintf(fd, "OK\n"); } @@ -259,6 +258,17 @@ cmdping(int fd, char *arg) dprintf(fd, "pong\nOK\n"); } +static void +cmdsearch(int fd, char *arg) +{ + if (!arg[0]) { + dprintf(fd, "ERR expects search string\n"); + return; + } + if (searchplaylist(fd, arg) != -1) + dprintf(fd, "OK\n"); +} + static Cmd cmds[] = { { "status", cmdstatus }, { "volume", cmdvolume }, @@ -274,6 +284,7 @@ static Cmd cmds[] = { { "close", cmdclose }, { "kill", cmdkill }, { "ping", cmdping }, + { "search", cmdsearch } }; /* shamelessly taken from isakmpd ui.c */ diff --git a/playlist.c b/playlist.c @@ -1,7 +1,9 @@ #include <sys/select.h> +#include <sys/types.h> #include <err.h> #include <limits.h> +#include <regex.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -135,6 +137,39 @@ dumpplaylist(int fd) } } +static int +wregcomp(int fd, regex_t *preg, const char *regex, int cflags) +{ + char errbuf[BUFSIZ] = ""; + int r; + + if ((r = regcomp(preg, regex, cflags)) == 0) + return r; + + regerror(r, preg, errbuf, sizeof(errbuf)); + dprintf(fd, "ERR invalid regex: %s\n", errbuf); + return r; +} + +int +searchplaylist(int fd, const char *search) +{ + Song *s; + int i; + regex_t re; + + if (wregcomp(fd, &re, search, REG_EXTENDED | REG_ICASE)) + return -1; /* invalid regex */ + + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + if (!regexec(&re, s->path, 0, NULL, REG_NOSUB)) + dprintf(fd, "%d: %s\n", s->id, s->path); + } + regfree(&re); + return 0; +} + void clearplaylist(void) { diff --git a/sad.h b/sad.h @@ -65,6 +65,7 @@ Song *getcursong(void); void putcursong(Song *); void dumpplaylist(int); void clearplaylist(void); +int searchplaylist(int, const char *); /* wav.c */ extern Decoder wavdecoder;