aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2015-12-31 13:59:18 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2015-12-31 15:58:17 +0200
commit00e6c3c3ed06a258a02943fc49fa7c528025d747 (patch)
tree52530660be48fb5f4611bcc9393886bab8a70b72
parent7f204cc788de3e03a51087b1273deb5b59288cf2 (diff)
downloadpies-00e6c3c3ed06a258a02943fc49fa7c528025d747.tar.gz
pies-00e6c3c3ed06a258a02943fc49fa7c528025d747.tar.bz2
Command-line control interface.
* configure.ac (DEFAULT_CONTROL_URL): New subst variable. * grecs: Upgrade. * ident/pam.c (overwrite_and_free): Free ptr. * lib/Makefile.am: Add new sources. * src/addrfmt.c: Move to lib/addrfmt.c * lib/grecsasrt.c: New file. * lib/grecsasrt.h: New file. * lib/mkfilename.c: New file. * lib/netrc.c: New file. * lib/pp.c: New file. * lib/split3.c: New file. * src/url.c: Move from lib/url.c (pies_url_free_user, pies_url_free_passwd): New finctions. * lib/libpies.h (strsplit3): New proto. (pies_url_create, pies_url_destroy) (pies_url_get_arg, pies_url_copy) (pies_url_free_user, pies_url_free_passwd) (netrc_scan) (pp_add_option, pp_command_line, mkfilename) (sockaddr_to_str, sockaddr_to_astr): New protos * src/Makefile.am (bin_PROGRAMS): New program: piesctl (pies_SOURCES): Remove addrfmt.c and url.c. (noinst_HEADERS, BUILT_SOURCES): Add piesctl-cl.h * src/cmdline.opt: Use pp_* function family to build preprocessor command line. * src/ctl.c (http_header_hash): Use case-insensitive hashing. (ctlio_finalize_reply): Don't close connection after sending 401 response. (input): Remove ws and wsflags. All uses changed. (input_append): Use strsplit3 to parse the request line. * src/pies.c: Use pp_* function family to build preprocessor command line. Move assert_, mkfilename and _cb+_url functions into libpies. * src/pies.h (pies_sockaddr_storage): Move to libpies.h * src/piesctl.c: New file. * src/piesctl-cl.opt: New file.
-rw-r--r--configure.ac4
m---------grecs0
-rw-r--r--ident/pam.c3
-rw-r--r--lib/Makefile.am23
-rw-r--r--lib/addrfmt.c (renamed from src/addrfmt.c)63
-rw-r--r--lib/grecsasrt.c78
-rw-r--r--lib/grecsasrt.h22
-rw-r--r--lib/libpies.h66
-rw-r--r--lib/mkfilename.c42
-rw-r--r--lib/netrc.c217
-rw-r--r--lib/pp.c71
-rw-r--r--lib/split3.c83
-rw-r--r--lib/url.c (renamed from src/url.c)94
-rw-r--r--src/.gitignore2
-rw-r--r--src/Makefile.am21
-rw-r--r--src/cmdline.opt4
-rw-r--r--src/ctl.c43
-rw-r--r--src/pies.c121
-rw-r--r--src/pies.h37
-rw-r--r--src/piesctl-cl.opt95
-rw-r--r--src/piesctl.c1067
21 files changed, 1937 insertions, 219 deletions
diff --git a/configure.ac b/configure.ac
index 1e5ba2a..1ee26ec 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,2 +134,6 @@ AH_BOTTOM([
+AC_SUBST([DEFAULT_CONTROL_URL],[unix:///tmp/%s.ctl])
+AC_ARG_VAR([DEFAULT_CONTROL_URL],
+ [URL of the default control socket])
+
IMPRIMATUR_INIT
diff --git a/grecs b/grecs
-Subproject 8312f45f48ed9d995a15ee6707257f4c8946528
+Subproject 3b73967c62da68d865f32ca91c8407e65b8ddc0
diff --git a/ident/pam.c b/ident/pam.c
index b7b9ef4..7302242 100644
--- a/ident/pam.c
+++ b/ident/pam.c
@@ -38,3 +38,4 @@ struct pam_cred
*s++ = 0; \
- } while (0)
+ free (ptr); \
+ } while (0)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 89812ca..a0d6614 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,3 +1,3 @@
# This file is part of GNU Pies.
-# Copyright (C) 2005, 2006, 2007, 2008, 2010, 2013 Sergey Poznyakoff
+# Copyright (C) 2005-2008, 2010, 2013, 2015 Sergey Poznyakoff
#
@@ -18,13 +18,26 @@ noinst_LIBRARIES=libpies.a
-noinst_HEADERS = libpies.h
+noinst_HEADERS = libpies.h grecsasrt.h
libpies_a_SOURCES=\
+ addrfmt.c\
arraymember.c\
+ grecsasrt.c\
+ mkfilename.c\
+ netrc.c\
parsetime.c\
proctitle.c\
- strtotok.c
+ pp.c\
+ split3.c\
+ strtotok.c\
+ url.c
-libpies_a_LIBADD=$(LIBOBJS)
+libpies_a_LIBADD=\
+ $(LIBOBJS)\
+ @GRECS_LDADD@
-AM_CPPFLAGS = -I$(top_srcdir)/gnu -I../gnu
+AM_CPPFLAGS = \
+ -I$(top_srcdir)/gnu\
+ -I../gnu\
+ @GRECS_INCLUDES@\
+ -DDEFAULT_PREPROCESSOR="$(DEFAULT_PREPROCESSOR)"
diff --git a/src/addrfmt.c b/lib/addrfmt.c
index 59754ae..ff615dd 100644
--- a/src/addrfmt.c
+++ b/lib/addrfmt.c
@@ -1,3 +1,3 @@
/* This file is part of GNU Pies
- Copyright (C) 2009, 2010, 2013 Sergey Poznyakoff
+ Copyright (C) 2009, 2010, 2013, 2015 Sergey Poznyakoff
@@ -19,3 +19,3 @@
#endif
-#include "pies.h"
+#include "libpies.h"
#include <sys/types.h>
@@ -39,3 +39,3 @@ str2port (char *str)
if (serv != NULL)
- port = ntohs(serv->s_port);
+ port = ntohs (serv->s_port);
else
@@ -81,2 +81,43 @@ _my_stpcpy (char **pbuf, size_t *psize, const char *src)
+static size_t
+format_uint (char **bufptr, size_t *buflen, unsigned n)
+{
+ char *buf = *bufptr;
+ size_t len = *buflen;
+ size_t i;
+
+ if (buf && len == 0)
+ return 0;
+
+ for (i = 0;;)
+ {
+ unsigned x = n % 10;
+ if (buf)
+ {
+ if (len == 1)
+ break;
+ *buf++ = x + '0';
+ --len;
+ }
+ n /= 10;
+ ++i;
+ if (n == 0)
+ break;
+ }
+
+ if (buf)
+ {
+ char *p = *bufptr;
+ *bufptr = buf;
+ *buf = 0;
+ while (--buf > p)
+ {
+ char c = *p;
+ *p++ = *buf;
+ *buf = c;
+ }
+ }
+ return i;
+}
+
void
@@ -86,3 +127,2 @@ sockaddr_to_str (const struct sockaddr *sa, int salen,
{
- char buf[INT_BUFSIZE_BOUND (uintmax_t)]; /* FIXME: too much */
size_t len = 0;
@@ -93,6 +133,5 @@ sockaddr_to_str (const struct sockaddr *sa, int salen,
struct sockaddr_in s_in = *(struct sockaddr_in *)sa;
- len += _my_stpcpy (&bufptr, &buflen, inet_ntoa(s_in.sin_addr));
+ len += _my_stpcpy (&bufptr, &buflen, inet_ntoa (s_in.sin_addr));
len += _my_stpcpy (&bufptr, &buflen, ":");
- len += _my_stpcpy (&bufptr, &buflen,
- umaxtostr(ntohs (s_in.sin_port), buf));
+ len += format_uint (&bufptr, &buflen, ntohs (s_in.sin_port));
break;
@@ -115,3 +154,3 @@ sockaddr_to_str (const struct sockaddr *sa, int salen,
len += _my_stpcpy (&bufptr, &buflen, "{Unsupported family: ");
- len += _my_stpcpy (&bufptr, &buflen, umaxtostr (sa->sa_family, buf));
+ len += format_uint (&bufptr, &buflen, sa->sa_family);
len += _my_stpcpy (&bufptr, &buflen, "}");
@@ -128,5 +167,7 @@ sockaddr_to_astr (const struct sockaddr *sa, int salen)
- sockaddr_to_str(sa, salen, NULL, 0, &size);
- p = xmalloc (size);
- sockaddr_to_str(sa, salen, p, size, NULL);
+ sockaddr_to_str (sa, salen, NULL, 0, &size);
+ p = malloc (size);
+ if (!p)
+ grecs_alloc_die ();
+ sockaddr_to_str (sa, salen, p, size, NULL);
return p;
diff --git a/lib/grecsasrt.c b/lib/grecsasrt.c
new file mode 100644
index 0000000..29bd0bf
--- /dev/null
+++ b/lib/grecsasrt.c
@@ -0,0 +1,78 @@
+/* This file is part of GNU Pies
+ Copyright (C) 2015 Sergey Poznyakoff
+
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <errno.h>
+#include <grecs.h>
+#include "libpies.h"
+#include "grecsasrt.h"
+
+int
+assert_grecs_value_type (grecs_locus_t *locus,
+ const grecs_value_t *value, int type)
+{
+ if (GRECS_VALUE_EMPTY_P (value))
+ {
+ grecs_error (locus, 0, _("expected %s"),
+ grecs_data_type_string (type));
+ return 1;
+ }
+ if (value->type != type)
+ {
+ grecs_error (locus, 0, _("expected %s, but found %s"),
+ grecs_data_type_string (type),
+ grecs_data_type_string (value->type));
+ return 1;
+ }
+ return 0;
+}
+
+int
+assert_scalar_stmt (grecs_locus_t *locus, enum grecs_callback_command cmd)
+{
+ if (cmd != grecs_callback_set_value)
+ {
+ grecs_error (locus, 0, _("unexpected block statement"));
+ return 1;
+ }
+ return 0;
+}
+
+int
+conf_callback_url (enum grecs_callback_command cmd,
+ grecs_locus_t *locus,
+ void *varptr, grecs_value_t *value, void *cb_data)
+{
+ struct pies_url *url;
+
+ if (assert_scalar_stmt (locus, cmd)
+ || assert_grecs_value_type (locus, value, GRECS_TYPE_STRING))
+ return 1;
+ if (pies_url_create (&url, value->v.string))
+ {
+ grecs_error (locus, 0, _("%s: cannot create URL: %s"),
+ value->v.string, strerror (errno));
+ return 0;
+ }
+ if (varptr)
+ *(struct pies_url **) varptr = url;
+ else
+ pies_url_destroy (&url);
+ return 0;
+}
+
diff --git a/lib/grecsasrt.h b/lib/grecsasrt.h
new file mode 100644
index 0000000..bf75731
--- /dev/null
+++ b/lib/grecsasrt.h
@@ -0,0 +1,22 @@
+/* This file is part of GNU Pies.
+ Copyright (C) 2009, 2010, 2011, 2013 Sergey Poznyakoff
+
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+int assert_grecs_value_type (grecs_locus_t *locus,
+ const grecs_value_t *value, int type);
+int assert_scalar_stmt (grecs_locus_t *locus, enum grecs_callback_command cmd);
+int conf_callback_url (enum grecs_callback_command cmd,
+ grecs_locus_t *locus,
+ void *varptr, grecs_value_t *value, void *cb_data);
diff --git a/lib/libpies.h b/lib/libpies.h
index b9c8af7..dab36df 100644
--- a/lib/libpies.h
+++ b/lib/libpies.h
@@ -1,3 +1,3 @@
/* This file is part of GNU Pies.
- Copyright (C) 2009, 2010, 2011, 2013 Sergey Poznyakoff
+ Copyright (C) 2009, 2010, 2011, 2013, 2015 Sergey Poznyakoff
@@ -16,2 +16,3 @@
+#include <sys/types.h>
#include <unistd.h>
@@ -19,4 +20,6 @@
#include <gettext.h>
+#include <netinet/in.h>
+#include <sys/un.h>
+#include <grecs.h>
-
#if defined HAVE_SYSCONF && defined _SC_OPEN_MAX
@@ -28,10 +31,2 @@
#endif
-
-
-
-void *xmalloc (size_t size);
-void *xzalloc (size_t size);
-void *xcalloc (size_t count, size_t size);
-char *xstrdup (const char *str);
-
@@ -39,3 +34,2 @@ void mf_proctitle_init (int argc, char *argv[], char *env[]);
void mf_proctitle_format (const char *fmt, ...);
-
@@ -43,3 +37,2 @@ size_t longtostr (long i, char *buf, size_t size);
size_t ulongtostr (unsigned long i, char *buf, size_t size);
-
@@ -60,2 +53,51 @@ int toktostr (struct tokendef *tab, int tok, const char **pres);
int is_array_member (char * const * ar, char const *str);
+int strsplit3 (const char *input, char *result[3], int flag);
+
+/* url.c */
+struct pies_url
+{
+ char *string;
+ char *scheme;
+ char *host;
+ char *port_s;
+ int port;
+ char *proto_s;
+ int proto;
+ char *path;
+ char *user;
+ char *passwd;
+ int argc;
+ char **argv;
+};
+
+int pies_url_create (struct pies_url **purl, const char *str);
+void pies_url_destroy (struct pies_url **purl);
+const char *pies_url_get_arg (struct pies_url *url, const char *argname);
+int pies_url_copy (struct pies_url **purl, struct pies_url *src);
+void pies_url_free_user (struct pies_url *url);
+void pies_url_free_passwd (struct pies_url *url);
+
+void netrc_scan (struct pies_url *url);
+
+
+/* pp.c */
+void pp_add_option (const char *opt, const char *arg);
+char *pp_command_line (void);
+
+char *mkfilename (const char *dir, const char *name, const char *suf);
+
+union pies_sockaddr_storage
+{
+ struct sockaddr s;
+ struct sockaddr_in s_in;
+ struct sockaddr_un s_un;
+};
+
+/* addrfmt.c */
+void sockaddr_to_str (const struct sockaddr *sa, int salen,
+ char *bufptr, size_t buflen,
+ size_t *plen);
+char *sockaddr_to_astr (const struct sockaddr *sa, int salen);
+
+
diff --git a/lib/mkfilename.c b/lib/mkfilename.c
new file mode 100644
index 0000000..8841fe4
--- /dev/null
+++ b/lib/mkfilename.c
@@ -0,0 +1,42 @@
+/* This file is part of GNU Pies
+ Copyright (C) 2015 Sergey Poznyakoff
+
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include "libpies.h"
+#include <grecs.h>
+
+char *
+mkfilename (const char *dir, const char *name, const char *suf)
+{
+ size_t dirlen = strlen (dir);
+ char *s;
+
+ while (dirlen > 0 && dir[dirlen-1] == '/')
+ dirlen--;
+
+ s = malloc (dirlen + 1 + strlen (name) + (suf ? strlen (suf) : 0) + 1);
+ if (!s)
+ grecs_alloc_die ();
+ strcpy (s, dir);
+ strcat (s, "/");
+ strcat (s, name);
+ if (suf)
+ strcat (s, suf);
+ return s;
+}
+
diff --git a/lib/netrc.c b/lib/netrc.c
new file mode 100644
index 0000000..ed8053f
--- /dev/null
+++ b/lib/netrc.c
@@ -0,0 +1,217 @@
+/* This file is part of GNU Pies
+ Copyright (C) 2015 Sergey Poznyakoff
+
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include "libpies.h"
+#include <unistd.h>
+#include <pwd.h>
+#include <grecs.h>
+#include <wordsplit.h>
+#include <errno.h>
+#include <netdb.h>
+
+/* Compare two hostnames. Return 0 if they have the same address type,
+ address length *and* at least one of the addresses of A matches
+ B */
+static int
+hostcmp (const char *a, const char *b)
+{
+ struct hostent *hp = gethostbyname (a);
+ char **addrlist;
+ char *dptr;
+ char **addr;
+ size_t i, count;
+ size_t entry_length;
+ int entry_type;
+
+ if (!hp)
+ return 1;
+
+ for (count = 1, addr = hp->h_addr_list; *addr; addr++)
+ count++;
+ addrlist = grecs_malloc (count * (sizeof *addrlist + hp->h_length)
+ - hp->h_length);
+ dptr = (char *) (addrlist + count);
+ for (i = 0; i < count - 1; i++)
+ {
+ memcpy (dptr, hp->h_addr_list[i], hp->h_length);
+ addrlist[i] = dptr;
+ dptr += hp->h_length;
+ }
+ addrlist[i] = NULL;
+ entry_length = hp->h_length;
+ entry_type = hp->h_addrtype;
+
+ hp = gethostbyname (b);
+ if (!hp || entry_length != hp->h_length || entry_type != hp->h_addrtype)
+ {
+ grecs_free (addrlist);
+ return 1;
+ }
+
+ for (addr = addrlist; *addr; addr++)
+ {
+ char **p;
+
+ for (p = hp->h_addr_list; *p; p++)
+ {
+ if (memcmp (*addr, *p, entry_length) == 0)
+ {
+ grecs_free (addrlist);
+ return 0;
+ }
+ }
+ }
+ grecs_free (addrlist);
+ return 1;
+}
+
+static int
+match_url (size_t argc, char **argv, struct pies_url *url)
+{
+ if (hostcmp (argv[1], url->host ? url->host : "localhost") == 0)
+ {
+ if (argc >= 4 && strcmp (argv[2], "port") == 0)
+ {
+ unsigned long n = strtoul (argv[3], NULL, 10);
+ if (n == url->port)
+ return 1;
+ }
+ else
+ return 1;
+ }
+ return 0;
+}
+
+static void
+parse_args (struct grecs_locus *loc, char **argv,
+ char **username, char **password)
+{
+ if (*username)
+ {
+ grecs_free (*username);
+ *username = NULL;
+ }
+ if (*password)
+ {
+ grecs_free (*password);
+ *password = NULL;
+ }
+
+ while (*argv)
+ {
+ if (!argv[1])
+ {
+ grecs_error (loc, 0, _("incomplete sentence"));
+ break;
+ }
+ if (strcmp (*argv, "login") == 0)
+ *username = grecs_strdup (argv[1]);
+ else if (strcmp (*argv, "password") == 0)
+ *password = grecs_strdup (argv[1]);
+ argv += 2;
+ }
+}
+
+/* Parse traditional .netrc file. Set up auth_args fields in accordance with
+ it. */
+void
+netrc_scan_file (FILE *fp, struct grecs_locus *loc, struct pies_url *url)
+{
+ char *buf = NULL;
+ size_t n = 0;
+ struct wordsplit ws;
+ int wsflags = WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE | WRDSF_SQUEEZE_DELIMS;
+ char *username = NULL;
+ char *password = NULL;
+
+ while (grecs_getline (&buf, &n, fp) > 0)
+ {
+ loc->beg.line++;
+
+ if (wordsplit (buf, &ws, wsflags))
+ {
+ grecs_error (loc, 0, "wordsplit: %s", wordsplit_strerror (&ws));
+ continue;
+ }
+ wsflags |= WRDSF_REUSE;
+
+ if (ws.ws_wordc == 0)
+ continue;
+
+ if (strcmp (ws.ws_wordv[0], "machine") == 0)
+ {
+ if (match_url (ws.ws_wordc, ws.ws_wordv, url))
+ {
+ parse_args (loc, ws.ws_wordv + 2, &username, &password);
+ break;
+ }
+ }
+ else if (strcmp (ws.ws_wordv[0], "default") == 0)
+ parse_args (loc, ws.ws_wordv + 1, &username, &password);
+ else
+ grecs_error (loc, 0, _("ignoring unrecognized line\n"));
+ }
+ grecs_free (buf);
+
+ if (wsflags & WRDSF_REUSE)
+ wordsplit_free (&ws);
+
+ url->user = username;
+ url->passwd = password;
+}
+
+void
+netrc_scan (struct pies_url *url)
+{
+ FILE *fp;
+ struct grecs_locus loc;
+ char *filename;
+ char *homedir;
+
+ if (url->user)
+ return;
+
+ homedir = getenv ("HOME");
+ if (!homedir)
+ {
+ struct passwd *pwd = getpwuid (getuid ());
+ if (!pwd)
+ return;
+ homedir = pwd->pw_dir;
+ }
+
+ filename = mkfilename (homedir, ".netrc", NULL);
+ fp = fopen (filename, "r");
+ if (!fp)
+ {
+ if (errno != ENOENT)
+ grecs_error (NULL, 0,
+ _("cannot open configuration file %s: %s"),
+ filename, strerror (errno));
+ free (filename);
+ return;
+ }
+
+ loc.beg.file = loc.end.file = (char*) filename;
+ loc.beg.col = loc.end.col = 0;
+ loc.beg.line = 0;
+ netrc_scan_file (fp, &loc, url);
+ fclose (fp);
+ free (filename);
+}
diff --git a/lib/pp.c b/lib/pp.c
new file mode 100644
index 0000000..9960d7f
--- /dev/null
+++ b/lib/pp.c
@@ -0,0 +1,71 @@
+/* This file is part of GNU Pies.
+ Copyright (C) 2015 Sergey Poznyakoff
+
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <grecs.h>
+#include <sysexits.h>
+
+struct grecs_txtacc *pp_cmd;
+
+void
+pp_add_option (const char *opt, const char *arg)
+{
+ if (!DEFAULT_PREPROCESSOR)
+ {
+ grecs_error (NULL, 0, _("no preprocessor configured"));
+ exit (EX_CONFIG);
+ }
+ if (!pp_cmd)
+ {
+ pp_cmd = grecs_txtacc_create ();
+ grecs_txtacc_grow_string (pp_cmd, DEFAULT_PREPROCESSOR);
+ }
+ grecs_txtacc_grow_char (pp_cmd, ' ');
+ grecs_txtacc_grow_string (pp_cmd, opt);
+
+ if (arg)
+ {
+ grecs_txtacc_grow_char (pp_cmd, '\'');
+ for (; *arg; ++arg)
+ {
+ if (*arg == '\'')
+ grecs_txtacc_grow_string (pp_cmd, "'\\''");
+ else
+ grecs_txtacc_grow (pp_cmd, arg, 1);
+ }
+ grecs_txtacc_grow_char (pp_cmd, '\'');
+ }
+}
+
+char *
+pp_command_line (void)
+{
+ char *ret;
+
+ if (!DEFAULT_PREPROCESSOR)
+ return NULL;
+
+ if (!pp_cmd)
+ {
+ pp_cmd = grecs_txtacc_create ();
+ grecs_txtacc_grow_string (pp_cmd, DEFAULT_PREPROCESSOR);
+ }
+ grecs_txtacc_grow_char (pp_cmd, 0);
+ ret = grecs_txtacc_finish (pp_cmd, 1);
+ grecs_txtacc_free (pp_cmd);
+ pp_cmd = NULL;
+ return ret;
+}
diff --git a/lib/split3.c b/lib/split3.c
new file mode 100644
index 0000000..ee302e8
--- /dev/null
+++ b/lib/split3.c
@@ -0,0 +1,83 @@
+/* This file is part of GNU Pies.
+ Copyright (C) 2007, 2008, 2009, 2010, 2013 Sergey Poznyakoff
+
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "libpies.h"
+
+static int
+split3 (const char *input, char *res[3], int flag)
+{
+ size_t len;
+ char *p;
+
+ p = strchr (input, ' ');
+ if (!p)
+ return 1;
+
+ len = p - input;
+ res[0] = malloc (len + 1);
+ if (!res[0])
+ return -1;
+ memcpy (res[0], input, len);
+ res[0][len] = 0;
+
+ input = p + 1;
+
+ p = (flag ? strrchr : strchr) (input, ' ');
+
+ if (!p)
+ return 1;
+
+ len = p - input;
+ res[1] = malloc (len + 1);
+ if (!res[1])
+ return -1;
+ memcpy (res[1], input, len);
+ res[1][len] = 0;
+
+ res[2] = strdup (p + 1);
+ if (!res[2])
+ return -1;
+
+ return 0;
+}
+
+int
+strsplit3 (const char *input, char *result[3], int flag)
+{
+ char *tmp[3] = { NULL, NULL, NULL };
+ int rc = split3 (input, tmp, flag);
+ if (rc)
+ {
+ int ec = errno;
+ free (tmp[0]);
+ free (tmp[1]);
+ free (tmp[2]);
+ errno = ec;
+ }
+ else
+ {
+ result[0] = tmp[0];
+ result[1] = tmp[1];
+ result[2] = tmp[2];
+ }
+ return 0;
+}
diff --git a/src/url.c b/lib/url.c
index a02a41f..41f5c04 100644
--- a/src/url.c
+++ b/lib/url.c
@@ -1,3 +1,3 @@
/* This file is part of GNU Pies
- Copyright (C) 2009, 2010, 2013 Sergey Poznyakoff
+ Copyright (C) 2009, 2010, 2013, 2015 Sergey Poznyakoff
@@ -19,3 +19,2 @@
#endif
-#include "pies.h"
#include <netdb.h>
@@ -24,2 +23,4 @@
#include <limits.h>
+#include "libpies.h"
+#include <wordsplit.h>
@@ -217,2 +218,23 @@ url_parse_scheme (struct pies_url *url, const char *str)
void
+pies_url_free_user (struct pies_url *url)
+{
+ if (url->user)
+ {
+ free (url->user);
+ url->user = NULL;
+ }
+}
+
+void
+pies_url_free_passwd (struct pies_url *url)
+{
+ if (url->passwd)
+ {
+ memset (url->passwd, 0, strlen (url->passwd));
+ free (url->passwd);
+ url->passwd = NULL;
+ }
+}
+
+void
pies_url_destroy (struct pies_url **purl)
@@ -230,4 +252,4 @@ pies_url_destroy (struct pies_url **purl)
free (url->path);
- free (url->user);
- free (url->passwd);
+ pies_url_free_user (url);
+ pies_url_free_passwd (url);
for (i = 0; i < url->argc; i++)
@@ -240,2 +262,66 @@ pies_url_destroy (struct pies_url **purl)
+static int
+strasgn (char **dst, char *src)
+{
+ if (!src)
+ *dst = NULL;
+ else
+ {
+ *dst = strdup (src);
+ if (!*dst)
+ return 1;
+ }
+ return 0;
+}
+
+static int
+do_url_copy (struct pies_url *dest, struct pies_url *src)
+{
+#define STRASGN(a,b) if (strasgn (a, b)) return -1;
+ size_t i;
+
+ STRASGN (&dest->string, src->string);
+ STRASGN (&dest->scheme, src->scheme);
+ STRASGN (&dest->host, src->host);
+ STRASGN (&dest->port_s, src->port_s);
+ dest->port = src->port;
+ STRASGN (&dest->proto_s, src->proto_s);
+ dest->proto = src->proto;
+ STRASGN (&dest->path, src->path);
+ STRASGN (&dest->user, src->user);
+ STRASGN (&dest->passwd, src->passwd);
+ dest->argv = calloc (src->argc + 1, sizeof (dest->argv[0]));
+ if (!dest->argv)
+ return -1;
+ for (i = 0; i < src->argc; i++)
+ {
+ if (!(dest->argv[i] = strdup (src->argv[0])))
+ {
+ dest->argc = i;
+ return -1;
+ }
+ }
+ dest->argv[i] = NULL;
+ dest->argc = i;
+ return 0;
+#undef STRASGN
+}
+
+int
+pies_url_copy (struct pies_url **purl, struct pies_url *src)
+{
+ struct pies_url *dest = malloc (sizeof (*dest));
+
+ if (!dest)
+ return -1;
+ memset (dest, 0, sizeof (*dest));
+ if (do_url_copy (dest, src))
+ {
+ pies_url_destroy (&dest);
+ return -1;
+ }
+ *purl = dest;
+ return 0;
+}
+
int
diff --git a/src/.gitignore b/src/.gitignore
index 4ad4290..a83b9ab 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -8 +8,3 @@ pies
pies.rc
+piesctl
+piesctl-cl.h
diff --git a/src/Makefile.am b/src/Makefile.am
index ab4546f..982bd12 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -17,2 +17,3 @@
sbin_PROGRAMS = pies
+bin_PROGRAMS = piesctl
@@ -20,3 +21,2 @@ pies_SOURCES = \
acl.c\
- addrfmt.c\
ctl.c\
@@ -35,3 +35,2 @@ pies_SOURCES = \
sysvinit.c\
- url.c\
utmp.c\
@@ -45,3 +44,4 @@ noinst_HEADERS = \
pies.h\
- prog.h
+ prog.h\
+ piesctl-cl.h
@@ -49,3 +49,3 @@ meta1lex.c: meta1gram.h
-BUILT_SOURCES=cmdline.h
+BUILT_SOURCES=cmdline.h piesctl-cl.h
@@ -60,4 +60,5 @@ SUFFIXES=.opt .c .h
cmdline.h: cmdline.opt
+piesctl-cl.h: piesctl-cl.opt
-LDADD = \
+pies_LDADD = \
../ident/libident.a\
@@ -69,2 +70,9 @@ LDADD = \
+piesctl_SOURCES = piesctl.c
+
+piesctl_LDADD = \
+ ../lib/libpies.a\
+ @GRECS_LDADD@\
+ ../gnu/libgnu.a
+
pkgstatedir=$(localstatedir)/pies
@@ -80,3 +88,4 @@ AM_CPPFLAGS=\
-DDEFAULT_INCLUDE_DIR=\"$(pkgdatadir)/include\"\
- -DDEFAULT_STATE_DIR=\"$(pkgstatedir)\"
+ -DDEFAULT_STATE_DIR=\"$(pkgstatedir)\"\
+ -DDEFAULT_CONTROL_URL=\"$(DEFAULT_CONTROL_URL)\"
diff --git a/src/cmdline.opt b/src/cmdline.opt
index a0dc703..18791b6 100644
--- a/src/cmdline.opt
+++ b/src/cmdline.opt
@@ -131,3 +131,3 @@ OPTION(define,D,[<NAME[=VALUE]>],
BEGIN
- add_pp_option ("-D", optarg);
+ pp_add_option ("-D", optarg);
END
@@ -137,3 +137,3 @@ OPTION(undefine,U,NAME,
BEGIN
- add_pp_option ("-U", optarg);
+ pp_add_option ("-U", optarg);
END
diff --git a/src/ctl.c b/src/ctl.c
index 1da1370..ce13dab 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -23,4 +23,2 @@
-#define DEFAULT_CONTROL_URL "unix:///tmp/%s.ctl"
-
struct control control;
@@ -173,3 +171,3 @@ http_header_hash (void *data, unsigned long n_buckets)
const struct http_header *p = data;
- return grecs_hash_string (p->name, n_buckets);