Skip to content

Commit

Permalink
Add an $ofs variable for controlling the delimiter used in list flatt…
Browse files Browse the repository at this point in the history
…ening.
  • Loading branch information
sysvinit committed Sep 17, 2021
1 parent c52067a commit 0ccebac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 deletions glom.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,28 @@ extern List *varsub(List *var, List *subs) {
}

extern List *flatten(List *s) {
List *r;
size_t step;
char *f;
List *r, *p, *ofs;
size_t size, step, sl;
char *sep, *f;
if (s == NULL || s->n == NULL)
return s;
ofs = varlookup("ofs");
if (ofs != NULL && ofs->w != NULL)
sep = ofs->w;
else
sep = " ";
sl = strlen(sep);
for (size = 0, p = s; p != NULL; p = p->n)
size += strlen(p->w) + (p->n == NULL ? 0 : sl);
r = nnew(List);
f = r->w = nalloc(listlen(s) + 1);
f = r->w = nalloc(size + 1);
r->m = NULL; /* flattened lists come from variables, so no meta */
r->n = NULL;
strcpy(f, s->w);
f += strlen(s->w);
do {
*f++ = ' ';
memcpy(f, sep, sl);
f += sl;
s = s->n;
step = strlen(s->w);
memcpy(f, s->w, step);
Expand Down
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern int main(int argc, char *argv[], char *envp[]) {
inithash();
initparse();
assigndefault("ifs", " ", "\t", "\n", (void *)0);
assigndefault("ofs", " ", (void *)0);
assigndefault("nl", "\n", (void *)0);
#ifdef DEFAULTPATH
assigndefault("path", DEFAULTPATH, (void *)0);
Expand Down

0 comments on commit 0ccebac

Please sign in to comment.