From: Roy Marples Date: Sun, 28 Sep 2014 19:45:31 +0000 (+0000) Subject: Add dhcpcd-decode, a utility to decode dhcpcd(8) variables which are X-Git-Tag: dhcpcd-ui-0.7.3~27 X-Git-Url: https://roy.marples.name/git Add dhcpcd-decode, a utility to decode dhcpcd(8) variables which are safely encoded for shells. --- diff --git a/README b/README index a5863f0..7cb9b44 100644 --- a/README +++ b/README @@ -6,11 +6,13 @@ specific parts. There are GTK+-2 (works with GTK+-3) and Qt-4 front ends. an EFL (e17) front end is planned. -A basic command line tool, dhcpcd-online can report on network -availability from dhcpcd (requires dhcpcd-6.4.4) +dhcpcd-decode can decode strings dhcpcd has encoded safely +for shells. +dhcpcd-online can report on network availability from dhcpcd +(requires dhcpcd-6.4.4) Switches to control building of various parts: - --with-dhcpcd-online + --with-tools --with-gtk --with-qt --with-icons diff --git a/configure b/configure index c5c732b..5bc9220 100755 --- a/configure +++ b/configure @@ -10,7 +10,7 @@ LIBDHCPCD=no HOST= HOSTCC= BUILD= -WITH_DHCPCD_ONLINE=yes +WITH_TOOLS=yes WITH_GTK= WITH_QT= WITH_ICONS= @@ -44,8 +44,8 @@ for x; do --includedir) eval INCLUDEDIR="$INCLUDEDIR${INCLUDEDIR:+ }$var";; --datadir|--infodir) ;; # ignore autotools --disable-maintainer-mode|--disable-dependency-tracking) ;; - --with-dhcpcd-online) WITH_DHCPCD_ONLINE=${var:-yes};; - --without-dhcpcd-online) WITH_DHCPCD_ONLINE=no;; + --with-tools) WITH_TOOLS=${var:-yes};; + --without-tools) WITH_TOOLS=no;; --with-gtk|--with-gtk+) WITH_GTK=${var:-yes};; --without-gtk|--without-gtk+) WITH_GTK=no;; --with-qt) WITH_QT=${var:-yes};; @@ -448,8 +448,8 @@ if [ "$WITH_ICONS" = yes ]; then echo "MKICONS= icons" >>$CONFIG_MK fi -if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_DHCPCD_ONLINE" != no ]; then - UI="dhcpcd-online${UI:+ }$UI" +if [ -n "$WITH_TOOLS" -a "$WITH_TOOLS" != no ]; then + UI="dhcpcd-decode dhcpcd-online${UI:+ }$UI" fi echo "UI= ${UI:+libdhcpcd }$UI" >>$CONFIG_MK diff --git a/src/dhcpcd-decode/Makefile b/src/dhcpcd-decode/Makefile new file mode 100644 index 0000000..b7870d6 --- /dev/null +++ b/src/dhcpcd-decode/Makefile @@ -0,0 +1,14 @@ +PROG= dhcpcd-decode +SRCS= dhcpcd-decode.c + +MAN8= dhcpcd-decode.8 + +TOPDIR= ../.. +include ${TOPDIR}/iconfig.mk + +CPPFLAGS+= -I${TOPDIR} + +LDADD+= ${LIB_DHCPCD} ${LIB_INTL} + +include ../libdhcpcd/Makefile.inc +include ${MKDIR}/prog.mk diff --git a/src/dhcpcd-decode/dhcpcd-decode.8 b/src/dhcpcd-decode/dhcpcd-decode.8 new file mode 100644 index 0000000..69d205b --- /dev/null +++ b/src/dhcpcd-decode/dhcpcd-decode.8 @@ -0,0 +1,59 @@ +.\" Copyright (c) 2014 Roy Marples +.\" All rights reserved +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd September 28, 2014 +.Dt DHCPCD-DECODE +.Os +.Sh NAME +.Nm dhcpcd-decode +.Nd a utility to decode +.Xr dhcpcd 8 +shell variables +.Sh SYNOPSIS +.Nm +.Op Fl s +.Op Fl x +.Op Ar argument ... +.Sh DESCRIPTION +.Nm +will decode each +.Va argument +from the shell safe encoding +.Xr dhcpcd 8 +applies and write the result to +.Pa stdout . +.Nm +will do the same for each newline received from +.Pa stdin . +.Bl -tag +.It Fl s +decode the shell safe encoding only. +Any non graphic octal escaped encoding will remain. +.It Fl x +decode each argument as hex into binary using ``:'' as a delimiter. +.El +.Sh AUTHORS +.An Roy Marples Aq roy@marples.name +.Sh BUGS +Please report them to http://roy.marples.name/projects/dhcpcd-ui diff --git a/src/dhcpcd-decode/dhcpcd-decode.c b/src/dhcpcd-decode/dhcpcd-decode.c new file mode 100644 index 0000000..b9be39b --- /dev/null +++ b/src/dhcpcd-decode/dhcpcd-decode.c @@ -0,0 +1,100 @@ +/* + * dhcpcd-decode + * Copyright 2014 Roy Marples + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include "dhcpcd.h" + +static void +process(char *src, ssize_t (*decode)(char *, size_t, const char *)) +{ + char *buf; + size_t buflen; + ssize_t dl; + + buflen = strlen(src) + 1; + buf = malloc(buflen); + if (buf == NULL) + err(EXIT_FAILURE, "malloc"); + if ((dl = decode(buf, buflen, src)) == -1) { + free(buf); + err(EXIT_FAILURE, "decode"); + } + if (fwrite(buf, 1, (size_t)dl, stdout) != (size_t)dl) { + free(buf); + err(EXIT_FAILURE, "fwrite"); + } + free(buf); + fputc('\n', stdout); +} + +int +main(int argc, char **argv) +{ + int opt; + ssize_t (*decode)(char *, size_t, const char *); + + decode = dhcpcd_decode; + while ((opt = getopt(argc, argv, "sx")) != -1) { + switch (opt) { + case 's': + decode = dhcpcd_decode_shell; + break; + case 'x': + decode = dhcpcd_decode_hex; + break; + case '?': + fprintf(stderr, "usage: dhcpcd-decode " + "[-q] [-t timeout]\n"); + return EXIT_FAILURE; + } + } + + for (; optind < argc; optind++) + process(argv[optind], decode); + + if (!isatty(fileno(stdin))) { + char *arg; + size_t len; + ssize_t argl; + + arg = NULL; + len = 0; + while ((argl = getline(&arg, &len, stdin)) != -1) { + if (arg[argl - 1] == '\n') + arg[argl - 1] = '\0'; + process(arg, decode); + } + free(arg); + } + + return EXIT_SUCCESS; +}