summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/execline/elglob.c3
-rw-r--r--src/execline/multisubstitute.c2
-rw-r--r--src/libexecline/exlsn_elglob.c28
3 files changed, 23 insertions, 10 deletions
diff --git a/src/execline/elglob.c b/src/execline/elglob.c
index 654e899..3e13e32 100644
--- a/src/execline/elglob.c
+++ b/src/execline/elglob.c
@@ -1,9 +1,10 @@
/* ISC license. */
#include <skalibs/strerr.h>
+
#include "exlsn.h"
-#define USAGE "elglob [ -v ] [ -w ] [ -s ] [ -m ] [ -e ] [ -0 ] key pattern prog..."
+#define USAGE "elglob [ -v ] [ -w ] [ -s ] [ -m ] [ -e ] [ -0 ] [ -n ] [ -d delim ] key pattern prog..."
int main (int argc, char const **argv, char const *const *envp)
{
diff --git a/src/execline/multisubstitute.c b/src/execline/multisubstitute.c
index da4e222..be78740 100644
--- a/src/execline/multisubstitute.c
+++ b/src/execline/multisubstitute.c
@@ -1,7 +1,9 @@
/* ISC license. */
#include <string.h>
+
#include <skalibs/strerr.h>
+
#include <execline/execline.h>
#include "exlsn.h"
diff --git a/src/libexecline/exlsn_elglob.c b/src/libexecline/exlsn_elglob.c
index c526e41..a5b66f3 100644
--- a/src/libexecline/exlsn_elglob.c
+++ b/src/libexecline/exlsn_elglob.c
@@ -1,12 +1,16 @@
/* ISC license. */
+#include <sys/uio.h>
#include <errno.h>
#include <glob.h>
#include <string.h>
+
#include <skalibs/sgetopt.h>
#include <skalibs/strerr.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
+#include <skalibs/netstring.h>
+
#include <execline/execline.h>
#include "exlsn.h"
@@ -20,16 +24,15 @@ static int elgloberrfunc (char const *s, int e)
int exlsn_elglob (int argc, char const **argv, char const *const *envp, exlsn_t *info)
{
glob_t pglob ;
- subgetopt localopt = SUBGETOPT_ZERO ;
- elsubst_t blah ;
+ elsubst_t blah = { .var = info->vars.len, .value = info->values.len, .n = 1 } ;
int flags = GLOB_NOSORT | GLOB_NOCHECK ;
- unsigned int i = 0 ;
int verbose = 0 ;
- blah.var = info->vars.len ;
- blah.value = info->values.len ;
+ int dochomp = 0 ;
+ char const *delim = 0 ;
+ subgetopt localopt = SUBGETOPT_ZERO ;
for (;;)
{
- int opt = subgetopt_r(argc, argv, "vwsme0", &localopt) ;
+ int opt = subgetopt_r(argc, argv, "vwsme0nd:", &localopt) ;
if (opt < 0) break ;
switch (opt)
{
@@ -39,10 +42,13 @@ int exlsn_elglob (int argc, char const **argv, char const *const *envp, exlsn_t
case 'm' : flags |= GLOB_MARK ; break ;
case 'e' : flags |= GLOB_NOESCAPE ; break ;
case '0' : flags &= ~GLOB_NOCHECK ; break ;
+ case 'n' : dochomp = 1 ; break ;
+ case 'd' : delim = localopt.arg ; break ;
default : return -3 ;
}
}
argc -= localopt.ind ; argv += localopt.ind ;
+ if (!delim) delim = "" ; else if (!*delim) delim = 0 ;
if (argc < 2) return -3 ;
if (!*argv[0] || el_vardupl(argv[0], info->vars.s, info->vars.len)) return -2 ;
@@ -60,11 +66,15 @@ int exlsn_elglob (int argc, char const **argv, char const *const *envp, exlsn_t
}
default: goto err ;
}
- for ( ; i < (unsigned int)pglob.gl_pathc ; i++)
- if (!stralloc_catb(&info->values, pglob.gl_pathv[i], strlen(pglob.gl_pathv[i]) + 1))
+ for (size_t i = 0 ; i < pglob.gl_pathc ; i++)
+ if (delim ? !stralloc_cats(&info->values, pglob.gl_pathv[i]) || !stralloc_catb(&info->values, delim, 1) :
+ !netstring_appendb(&info->values, pglob.gl_pathv[i], strlen(pglob.gl_pathv[i])))
goto globerr ;
- blah.n = pglob.gl_pathc ;
+ if (delim && *delim && dochomp) info->values.len-- ;
+ if (delim && !*delim) blah.n = pglob.gl_pathc ;
+ else if (!stralloc_0(&info->values)) goto globerr ;
globfree(&pglob) ;
+
if (!genalloc_append(elsubst_t, &info->data, &blah)) goto err ;
(void)envp ;
return localopt.ind + 2 ;