commit df00adaaab6055bb54114462d6b219dbefc34e9f
parent 9f28e4b1417e9623801170c697cc00435eb1b9b0
Author: Quentin Rameau <quinq@fifth.space>
Date: Tue, 26 Sep 2017 18:16:11 +0200
Handle output redirection to a pipe
Diffstat:
M | sacc.c | | | 51 | ++++++++++++++++++++++++++++++++++++++++++++++++--- |
1 file changed, 48 insertions(+), 3 deletions(-)
diff --git a/sacc.c b/sacc.c
@@ -27,6 +27,7 @@ static char *mainurl;
static Item *mainentry;
static int devnullfd;
static int parent = 1;
+static int interactive;
void
die(const char *fmt, ...)
@@ -168,6 +169,25 @@ typedisplay(char t)
}
static void
+printdir(Item *item)
+{
+ Dir *dir;
+ Item *items;
+ size_t i, nitems;
+
+ if (!item || !(dir = item->dat))
+ return;
+
+ items = dir->items;
+ nitems = dir->nitems;
+
+ for (i = 0; i < nitems; ++i) {
+ printf("%s%s\n",
+ typedisplay(items[i].type), items[i].username);
+ }
+}
+
+static void
displaytextitem(Item *item)
{
FILE *pagerin;
@@ -626,6 +646,26 @@ searchitem(Item *entry, Item *item)
}
static void
+printout(Item *hole)
+{
+ if (!hole)
+ return;
+
+ switch (hole->type) {
+ case '0':
+ if (dig(hole, hole))
+ puts(hole->raw);
+ return;
+ case '1':
+ case '+':
+ if (dig(hole, hole))
+ printdir(hole);
+ default:
+ return;
+ }
+}
+
+static void
delve(Item *hole)
{
Item *entry = NULL;
@@ -732,7 +772,8 @@ cleanup(void)
rmdir(tmpdir);
free(mainentry);
free(mainurl);
- uicleanup();
+ if (interactive)
+ uicleanup();
}
static void
@@ -752,7 +793,8 @@ setup(void)
die("open: /dev/null: %s", strerror(errno));
if (mkdir(tmpdir, S_IRWXU) < 0 && errno != EEXIST)
die("mkdir: %s: %s", tmpdir, strerror(errno));
- uisetup();
+ if(interactive = isatty(1))
+ uisetup();
}
int
@@ -766,7 +808,10 @@ main(int argc, char *argv[])
mainurl = xstrdup(argv[1]);
mainentry = moldentry(mainurl);
- delve(mainentry);
+ if (interactive)
+ delve(mainentry);
+ else
+ printout(mainentry);
exit(0);
}