From cd3bd5e54ea416bbb0e5bf13bd04f8b39b2d4fd7 Mon Sep 17 00:00:00 2001 From: "John Ankarstr\\xf6m" Date: Tue, 1 Jun 2021 03:36:57 +0200 Subject: Add -p option (print last path on quit, but not sigint) --- noice.1 | 8 ++++++++ noice.c | 57 +++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/noice.1 b/noice.1 index dddd302..46e4c01 100644 --- a/noice.1 +++ b/noice.1 @@ -6,6 +6,7 @@ .Nd small file browser .Sh SYNOPSIS .Nm +.Op Fl p .Op Ar dir .Sh DESCRIPTION .Nm @@ -24,6 +25,13 @@ is a relative path, will not go back beyond the first component of the path using standard navigation key presses. .Pp +If the +.Fl p +flag is passed, +.Nm +will print the last path upon exit. +This can be circumvented by exiting via SIGINT. +.Pp .Nm supports both vi-like and emacs-like key bindings in the default configuration. diff --git a/noice.c b/noice.c index 3b8068e..a097a55 100644 --- a/noice.c +++ b/noice.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -89,9 +90,9 @@ struct entry { }; /* Global context */ +int cur, idle, ndents; +int print = 0; struct entry *dents; -int ndents, cur; -int idle; /* * Layout: @@ -181,10 +182,14 @@ void spawn(char *file, char *arg, char *dir) { pid_t pid; - int status; + int status, ttyfd; pid = fork(); if (pid == 0) { + if ((ttyfd = open("/dev/tty", O_RDWR)) == -1) + err(1, "open"); + dup2(ttyfd, 1); + dup2(ttyfd, 0); if (dir != NULL) chdir(dir); execlp(file, file, arg, NULL); @@ -311,16 +316,18 @@ initcolor(void) void initcurses(void) { + FILE *tty; + SCREEN *scr; char *term; - if (initscr() == NULL) { - term = getenv("TERM"); - if (term != NULL) - fprintf(stderr, "error opening terminal: %s\n", term); - else - fprintf(stderr, "failed to initialize curses\n"); + if ((tty = fopen("/dev/tty", "r+")) == NULL) + err(1, "fopen"); + + if ((scr = newterm(NULL, tty, tty)) == NULL) { + fprintf(stderr, "failed to initialize curses\n"); exit(1); } + if (usecolor && has_colors()) initcolor(); cbreak(); @@ -674,6 +681,7 @@ nochange: switch (nextsel(&run, &env)) { case SEL_QUIT: dentfree(dents); + if (print) printf("%s\n", path); return; case SEL_BACK: /* There is no going back */ @@ -871,30 +879,39 @@ nochange: void usage(char *argv0) { - fprintf(stderr, "usage: %s [dir]\n", argv0); + fprintf(stderr, "usage: %s -p [dir]\n", argv0); + exit(1); +} + +void +sigint(int s) +{ + exitcurses(); exit(1); } int main(int argc, char *argv[]) { - char cwd[PATH_MAX], *ipath; - char *ifilter; + char *ifilter, *ipath, *name, cwd[PATH_MAX]; - if (argc > 2) - usage(argv[0]); + name = argv[0]; - /* Confirm we are in a terminal */ - if (!isatty(0) || !isatty(1)) { - fprintf(stderr, "stdin or stdout is not a tty\n"); - exit(1); + if (argc > 1 && argv[1][0] == '-') { + if (argv[1][1] == 'p') print = 1; + else usage(name); + argc--; + argv++; } + if (argc > 2) + usage(name); + if (getuid() == 0) showhidden = 1; initfilter(showhidden, &ifilter); - if (argv[1] != NULL) { + if (argc > 1) { ipath = argv[1]; } else { ipath = getcwd(cwd, sizeof(cwd)); @@ -902,7 +919,7 @@ main(int argc, char *argv[]) ipath = "/"; } - signal(SIGINT, SIG_IGN); + signal(SIGINT, sigint); /* Test initial path */ if (canopendir(ipath) == 0) { -- cgit v1.2.3