Try multiple paths for nnncp (without X case)

This commit is contained in:
Arun Prakash Jana 2018-10-21 14:51:50 +05:30
parent 7aa4a0a3db
commit 7c9068627c
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 32 additions and 23 deletions

View file

@ -392,13 +392,19 @@ Note that the filename is not escaped. So copying may still fail for filenames h
#### copy file paths when X is missing #### copy file paths when X is missing
A very common scenario on headless remote servers connected via SSH. As the clipboard is missing, `nnn` copies the path names to the tmp file `$HOME/.nnncp`. A very common scenario on headless remote servers connected via SSH. As the clipboard is missing, `nnn` copies the path names to the tmp file `DIR/.nnncp`, where `DIR` (by priority) is:
$HOME or,
$TMPDIR or,
/tmp
`nnn` needs to know X is unavailable: `nnn` needs to know X is unavailable:
export NNN_NO_X=1 export NNN_NO_X=1
Use <kbd>^Y</kbd> and/or <kbd>^K</kbd> to copy file paths as usual. To use the copied paths from the cmdline, use command substitution: To see the path to the copy file, run `nnn`, press `?` and look up `NNN_NO_X`.
Use <kbd>^Y</kbd> and/or <kbd>^K</kbd> to copy file paths as usual. To use the copied paths from the cmdline, use command substitution. For example, if `DIR` above is `/home/user`:
# bash/zsh # bash/zsh
ls -ltr `cat /home/user/.nnncp` ls -ltr `cat /home/user/.nnncp`

41
nnn.c
View file

@ -276,7 +276,7 @@ static blkcnt_t dir_blocks;
static ulong num_files; static ulong num_files;
static uint open_max; static uint open_max;
static bm bookmark[BM_MAX]; static bm bookmark[BM_MAX];
static size_t g_homelen; static size_t g_tmpfplen; /* path to tmp files for copy without X, keybind help and file stats */
static uchar g_crc; static uchar g_crc;
static uchar BLK_SHIFT = 9; static uchar BLK_SHIFT = 9;
@ -289,8 +289,8 @@ static char g_buf[MAX_CMD_LEN] __attribute__ ((aligned));
/* Buffer for file path copy file */ /* Buffer for file path copy file */
static char g_cppath[MAX_HOME_LEN] __attribute__ ((aligned)); static char g_cppath[MAX_HOME_LEN] __attribute__ ((aligned));
/* Buffer to store HOME path, for help and file details */ /* Buffer to store tmp file path */
static char g_homepath[MAX_HOME_LEN] __attribute__ ((aligned)); static char g_tmpfpath[MAX_HOME_LEN] __attribute__ ((aligned));
#ifdef LINUX_INOTIFY #ifdef LINUX_INOTIFY
static int inotify_fd, inotify_wd = -1; static int inotify_fd, inotify_wd = -1;
@ -1779,14 +1779,14 @@ show_stats(char *fpath, char *fname, struct stat *sb)
char *perms = get_lsperms(sb->st_mode, desc); char *perms = get_lsperms(sb->st_mode, desc);
char *p, *begin = g_buf; char *p, *begin = g_buf;
if (g_homepath[0]) if (g_tmpfpath[0])
xstrlcpy(g_homepath + g_homelen - 1, "/.nnnXXXXXX", MAX_HOME_LEN - g_homelen); xstrlcpy(g_tmpfpath + g_tmpfplen - 1, "/.nnnXXXXXX", MAX_HOME_LEN - g_tmpfplen);
else { else {
printmsg(messages[STR_NOHOME_ID]); printmsg(messages[STR_NOHOME_ID]);
return -1; return -1;
} }
int fd = mkstemp(g_homepath); int fd = mkstemp(g_tmpfpath);
if (fd == -1) if (fd == -1)
return -1; return -1;
@ -1877,8 +1877,8 @@ show_stats(char *fpath, char *fname, struct stat *sb)
close(fd); close(fd);
exitcurses(); exitcurses();
get_output(NULL, 0, "cat", g_homepath, NULL, 1); get_output(NULL, 0, "cat", g_tmpfpath, NULL, 1);
unlink(g_homepath); unlink(g_tmpfpath);
refresh(); refresh();
return 0; return 0;
} }
@ -1937,14 +1937,14 @@ handle_archive(char *fpath, char *arg, char *dir)
static int static int
show_help(char *path) show_help(char *path)
{ {
if (g_homepath[0]) if (g_tmpfpath[0])
xstrlcpy(g_homepath + g_homelen - 1, "/.nnnXXXXXX", MAX_HOME_LEN - g_homelen); xstrlcpy(g_tmpfpath + g_tmpfplen - 1, "/.nnnXXXXXX", MAX_HOME_LEN - g_tmpfplen);
else { else {
printmsg(messages[STR_NOHOME_ID]); printmsg(messages[STR_NOHOME_ID]);
return -1; return -1;
} }
int i = 0, fd = mkstemp(g_homepath); int i = 0, fd = mkstemp(g_tmpfpath);
char *start, *end; char *start, *end;
static char helpstr[] = { static char helpstr[] = {
"cKey Desc\n" "cKey Desc\n"
@ -2063,8 +2063,8 @@ show_help(char *path)
close(fd); close(fd);
exitcurses(); exitcurses();
get_output(NULL, 0, "cat", g_homepath, NULL, 1); get_output(NULL, 0, "cat", g_tmpfpath, NULL, 1);
unlink(g_homepath); unlink(g_tmpfpath);
refresh(); refresh();
return 0; return 0;
} }
@ -3492,15 +3492,18 @@ main(int argc, char *argv[])
if (getenv("NNN_QUOTE_ON")) if (getenv("NNN_QUOTE_ON"))
cfg.quote = 1; cfg.quote = 1;
if (getenv("HOME")) { if (getenv("HOME"))
g_homelen = xstrlcpy(g_homepath, getenv("HOME"), MAX_HOME_LEN); g_tmpfplen = xstrlcpy(g_tmpfpath, getenv("HOME"), MAX_HOME_LEN);
else if (getenv("TMPDIR"))
g_tmpfplen = xstrlcpy(g_tmpfpath, getenv("TMPDIR"), MAX_HOME_LEN);
else if (xdiraccess("/tmp"))
g_tmpfplen = xstrlcpy(g_tmpfpath, "/tmp", MAX_HOME_LEN);
/* Check if X11 is available */ /* Check if X11 is available */
if (getenv("NNN_NO_X")) { if (g_tmpfplen && getenv("NNN_NO_X")) {
cfg.noxdisplay = 1; cfg.noxdisplay = 1;
xstrlcpy(g_cppath, g_homepath, MAX_HOME_LEN); xstrlcpy(g_cppath, g_tmpfpath, MAX_HOME_LEN);
xstrlcpy(g_cppath + g_homelen - 1, "/.nnncp", MAX_HOME_LEN - g_homelen); xstrlcpy(g_cppath + g_tmpfplen - 1, "/.nnncp", MAX_HOME_LEN - g_tmpfplen);
}
} }
signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN);