soap

simple xdg-open replacement with fallback
git clone git://git.2f30.org/soap
Log | Files | Refs | LICENSE

commit baec3235fc9819da97604ce7bc6056dea4e11584
Author: FRIGN <dev@frign.de>
Date:   Sat,  3 May 2014 16:38:24 +0200

initial commit

Diffstat:
ALICENSE | 21+++++++++++++++++++++
AMakefile | 38++++++++++++++++++++++++++++++++++++++
Aconfig.h | 9+++++++++
Aconfig.mk | 6++++++
Asoap.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2014 Laslo Hunhold <dev@frign.de> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,38 @@ +# soap - simple xdg-open replacement with fallback + +include config.mk + +SRC = soap.c +OBJ = ${SRC:.c=.o} + +all: options soap + +options: + @echo soap build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.mk + +soap: ${OBJ} + @echo CC -o $@ + @${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + @echo cleaning + @rm -f soap ${OBJ} + +install: all + @test -f /usr/bin/xdg-open_ || (echo backing up to /usr/bin/xdg-open_; mv /usr/bin/xdg-open /usr/bin/xdg-open_) + @echo installing new xdg-open + @cp -f soap /usr/bin/xdg-open + @chmod 755 /usr/bin/xdg-open + +uninstall: + @echo moving xdg-open_ back into place + @(test -f /usr/bin/xdg-open_ && mv /usr/bin/xdg-open_ /usr/bin/xdg-open) || echo ERROR: xdg-open_ does not exist diff --git a/config.h b/config.h @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ + +static const Pair pairs[] = { + /* regex action */ + { "\.(jpg|png|tiff)$", "feh %s" }, + { "\.gif", "wget -O /tmp/tmp_gifview.gif %s && gifview -a /tmp/tmp_gifview.gif" }, + { "\.mp3", "st -e mplayer %s" }, + { "^(http://|https://)?(www\.)?(youtube.com/watch\?|youtu\.be/)", "youtube-viewer %s" } +}; diff --git a/config.mk b/config.mk @@ -0,0 +1,6 @@ +# flags +CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} +LDFLAGS = -static -s + +# compiler and linker +CC = cc diff --git a/soap.c b/soap.c @@ -0,0 +1,56 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdlib.h> +#include <stdio.h> +#include <regex.h> + +typedef struct { + const char *regex; + const char *action; +} Pair; + +#include "config.h" + +int +main(int argc, char *argv[]){ + int g, h, i; + char cmd[BUFSIZ], sharg[BUFSIZ]; + regex_t regex; + + /* we only take on argument */ + if (argc != 2) + return EXIT_FAILURE; + + /* make the argument shell-ready + * 1) start with ' + * 2) escape ' to '\'' + * 3) close with '\0 + */ + sharg[0] = '\''; + for (g=0, h=1; argv[1][g] && h < BUFSIZ-1-3-2; ++g, ++h) { + sharg[h] = argv[1][g]; + if (argv[1][g] == '\'') { + sharg[++h] = '\\'; + sharg[++h] = '\''; + sharg[++h] = '\''; + } + } + sharg[h] = '\''; + sharg[++h] = 0; + + /* check regex and launch action if it matches argv[1] */ + for (i=0; i < sizeof(pairs)/sizeof(*pairs); ++i) { + if (regcomp(&regex, pairs[i].regex, REG_EXTENDED)) + fprintf(stderr, "Invalid regex: %s\n", pairs[i].regex); + if (!regexec(&regex, argv[1], 0, NULL, 0)) { + snprintf(cmd, sizeof cmd, pairs[i].action, sharg); + system(cmd); + return EXIT_SUCCESS; + } + regfree(&regex); + } + + /* alternatively, fall back to xdg-open_ */ + snprintf(cmd, sizeof cmd, "xdg-open_ %s", sharg); + system(cmd); + return EXIT_SUCCESS; +}