]> Sergey Matveev's repositories - nnn.git/commitdiff
Add '!' command to spawn a shell in cwd
authorsin <sin@2f30.org>
Tue, 21 Oct 2014 13:21:00 +0000 (14:21 +0100)
committersin <sin@2f30.org>
Tue, 21 Oct 2014 13:21:12 +0000 (14:21 +0100)
noice.c

diff --git a/noice.c b/noice.c
index 5d8cc4756b4d9cd78b5d44a04bb54c75318b858f..b01961feb9a385e9b3ef720f2dceba243ad919f9 100644 (file)
--- a/noice.c
+++ b/noice.c
@@ -76,6 +76,24 @@ void printmsg(char *msg);
 void printwarn(void);
 void printerr(int ret, char *prefix);
 
+void
+spawn(const char *file, const char *arg)
+{
+       pid_t pid;
+       int status;
+
+       pid = fork();
+       if (pid == 0) {
+               execlp(file, file, arg, NULL);
+               _exit(1);
+       } else {
+               /* Ignore interruptions */
+               while (waitpid(pid, &status, 0) == -1)
+                       DPRINTF_D(status);
+               DPRINTF_D(pid);
+       }
+}
+
 char *
 openwith(char *file)
 {
@@ -234,6 +252,11 @@ nextsel(int *cur, int max)
                if (*cur > 0)
                        (*cur) -= MIN((LINES - 4) / 2, *cur);
                break;
+       case '!':
+               exitcurses();
+               spawn("/bin/sh", NULL);
+               initcurses();
+               break;
        }
 
        return 0;
@@ -405,13 +428,11 @@ begin:
                char *pathnew;
                char *name;
                char *bin;
-               pid_t pid;
                int fd;
                char *dir;
                char *tmp;
                regex_t re;
                struct history *hist;
-               int status;
 
 redraw:
                nlines = MIN(LINES - 4, n);
@@ -451,7 +472,11 @@ redraw:
                }
 
 nochange:
+               if (chdir(path) == -1)
+                       printwarn();
                ret = nextsel(&cur, n);
+               if (chdir(ipath) == -1)
+                       printwarn();
                switch (ret) {
                case SEL_QUIT:
                        free(path);
@@ -542,20 +567,7 @@ nochange:
                                }
 
                                exitcurses();
-
-                               /* Run program */
-                               pid = fork();
-                               if (pid == 0) {
-                                       execlp(bin, bin, pathnew, NULL);
-                                       _exit(0);
-                               } else {
-                                       /* Ignore interruptions */
-                                       while (waitpid(pid, &status,
-                                           0) == -1)
-                                               DPRINTF_D(status);
-                                       DPRINTF_D(pid);
-                               }
-
+                               spawn(bin, pathnew);
                                initcurses();
 
                                free(pathnew);