Support file path copy when X is unavailable

This commit is contained in:
Arun Prakash Jana 2018-02-24 07:49:59 +05:30
parent 04dc774116
commit b22d8c32f9
No known key found for this signature in database
GPG key ID: A75979F35C080412

35
nnn.c
View file

@ -194,6 +194,9 @@ disabledbg()
#define NUM_EVENT_FDS 1 #define NUM_EVENT_FDS 1
#endif #endif
/* File path to copy file names when X is not available */
#define TMP_CP_PATH "/tmp/nnncp"
/* TYPE DEFINITIONS */ /* TYPE DEFINITIONS */
typedef unsigned long ulong; typedef unsigned long ulong;
typedef unsigned int uint; typedef unsigned int uint;
@ -230,13 +233,14 @@ typedef struct {
ushort dircolor : 1; /* Current status of dir color */ ushort dircolor : 1; /* Current status of dir color */
ushort metaviewer : 1; /* Index of metadata viewer in utils[] */ ushort metaviewer : 1; /* Index of metadata viewer in utils[] */
ushort quote : 1; /* Copy paths within quotes */ ushort quote : 1; /* Copy paths within quotes */
ushort noxdisplay : 1; /* X11 is not available */
ushort color : 3; /* Color code for directories */ ushort color : 3; /* Color code for directories */
} settings; } settings;
/* GLOBALS */ /* GLOBALS */
/* Configuration */ /* Configuration */
static settings cfg = {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 4}; static settings cfg = {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 4};
static struct entry *dents; static struct entry *dents;
static char *pnamebuf, *pcopybuf; static char *pnamebuf, *pcopybuf;
@ -617,6 +621,19 @@ xbasename(char *path)
return base ? base + 1 : path; return base ? base + 1 : path;
} }
/* Writes buflen char(s) from buf to a file */
static void
writecp(const char *buf, const size_t buflen)
{
FILE *fp = fopen(TMP_CP_PATH, "w");
if (fp) {
fwrite(buf, 1, buflen, fp);
fclose(fp);
} else
printwarn();
}
static bool static bool
appendfilepath(const char *path, const size_t len) appendfilepath(const char *path, const size_t len)
{ {
@ -2854,11 +2871,18 @@ nochange:
g_buf[r] = '\''; g_buf[r] = '\'';
g_buf[r + 1] = '\0'; g_buf[r + 1] = '\0';
if (cfg.noxdisplay)
writecp(g_buf, r + 1); /* Truncate NULL from end */
else
spawn(copier, g_buf, NULL, NULL, F_NONE); spawn(copier, g_buf, NULL, NULL, F_NONE);
g_buf[r] = '\0'; g_buf[r] = '\0';
printmsg(g_buf + 1); printmsg(g_buf + 1);
} else { } else {
mkpath(path, dents[cur].name, newpath, PATH_MAX); r = mkpath(path, dents[cur].name, newpath, PATH_MAX);
if (cfg.noxdisplay)
writecp(newpath, r - 1); /* Truncate NULL from end */
else
spawn(copier, newpath, NULL, NULL, F_NONE); spawn(copier, newpath, NULL, NULL, F_NONE);
printmsg(newpath); printmsg(newpath);
} }
@ -2906,6 +2930,9 @@ nochange:
} }
if (copybufpos) { if (copybufpos) {
if (cfg.noxdisplay)
writecp(pcopybuf, copybufpos - 1); /* Truncate NULL from end */
else
spawn(copier, pcopybuf, NULL, NULL, F_NONE); spawn(copier, pcopybuf, NULL, NULL, F_NONE);
DPRINTF_S(pcopybuf); DPRINTF_S(pcopybuf);
if (!len) if (!len)
@ -3233,6 +3260,10 @@ main(int argc, char *argv[])
if (getenv("NNN_QUOTE_ON")) if (getenv("NNN_QUOTE_ON"))
cfg.quote = 1; cfg.quote = 1;
/* Check if X11 is available */
if (getenv("NNN_NO_X"))
cfg.noxdisplay = 1;
signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN);
/* Test initial path */ /* Test initial path */