Add dhcpcd-decode, a utility to decode dhcpcd(8) variables which are
authorRoy Marples <roy@marples.name>
Sun, 28 Sep 2014 19:45:31 +0000 (19:45 +0000)
committerRoy Marples <roy@marples.name>
Sun, 28 Sep 2014 19:45:31 +0000 (19:45 +0000)
safely encoded for shells.

README
configure
src/dhcpcd-decode/Makefile [new file with mode: 0644]
src/dhcpcd-decode/dhcpcd-decode.8 [new file with mode: 0644]
src/dhcpcd-decode/dhcpcd-decode.c [new file with mode: 0644]

diff --git a/README b/README
index a5863f058c43cfaff662001c4b624a0e69b819a7..7cb9b448205c0a49e2ef2d70a715028e5881aef5 100644 (file)
--- 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
index c5c732b0957d8818863b175c65b52da54068f32c..5bc9220216089777520af8a4a39beafe79016a95 100755 (executable)
--- 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 (file)
index 0000000..b7870d6
--- /dev/null
@@ -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 (file)
index 0000000..69d205b
--- /dev/null
@@ -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 (file)
index 0000000..b9be39b
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * dhcpcd-decode
+ * Copyright 2014 Roy Marples <roy@marples.name>
+ *
+ * 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 <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#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;
+}