Code refactor

This commit is contained in:
Arun Prakash Jana 2019-12-27 18:54:30 +05:30
parent bffadf09c5
commit f5cd0e563d
No known key found for this signature in database
GPG key ID: A75979F35C080412

137
src/nnn.c
View file

@ -449,21 +449,20 @@ static char * const utils[] = {
#define MSG_CONTINUE 22 #define MSG_CONTINUE 22
#define MSG_SEL_MISSING 23 #define MSG_SEL_MISSING 23
#define MSG_ACCESS 24 #define MSG_ACCESS 24
#define MSG_0_CREATED 25 #define MSG_NOT_REG_FILE 25
#define MSG_NOT_REG_FILE 26 #define MSG_PERM_DENIED 26
#define MSG_PERM_DENIED 27 #define MSG_EMPTY_FILE 27
#define MSG_EMPTY_FILE 28 #define MSG_UNSUPPORTED 28
#define MSG_UNSUPPORTED 29 #define MSG_NOT_SET 29
#define MSG_NOT_SET 30 #define MSG_DIR_CHANGED 30
#define MSG_DIR_CHANGED 31 #define MSG_EXISTS 31
#define MSG_EXISTS 32 #define MSG_FEW_COLUMNS 32
#define MSG_FEW_COLUMNS 33 #define MSG_REMOTE_OPTS 33
#define MSG_REMOTE_OPTS 34 #define MSG_RCLONE_DELAY 34
#define MSG_RCLONE_DELAY 35 #define MSG_APP_NAME 35
#define MSG_APP_NAME 36 #define MSG_ARCHIVE_OPTS 36
#define MSG_ARCHIVE_OPTS 37 #define MSG_PLUGIN_KEYS 37
#define MSG_PLUGIN_KEYS 38 #define MSG_BOOKMARK_KEYS 38
#define MSG_BOOKMARK_KEYS 39
static const char * const messages[] = { static const char * const messages[] = {
"no traversal", "no traversal",
@ -491,7 +490,6 @@ static const char * const messages[] = {
"\nPress Enter to continue", "\nPress Enter to continue",
"open failed", "open failed",
"dir inaccessible", "dir inaccessible",
"0 created",
"not regular file", "not regular file",
"permission denied", "permission denied",
"empty: edit or open with", "empty: edit or open with",
@ -2322,13 +2320,9 @@ static char *getreadline(const char *prompt, char *path, char *curpath, int *pre
refresh(); refresh();
if (chdir(curpath) == -1) { if (chdir(curpath) == -1)
printwarn(presel); printwarn(presel);
free(input); else if (input && input[0]) {
return NULL;
}
if (input && input[0]) {
add_history(input); add_history(input);
xstrlcpy(g_buf, input, CMD_LEN_MAX); xstrlcpy(g_buf, input, CMD_LEN_MAX);
free(input); free(input);
@ -2364,7 +2358,7 @@ static size_t mkpath(const char *dir, const char *name, char *out)
/* /*
* Create symbolic/hard link(s) to file(s) in selection list * Create symbolic/hard link(s) to file(s) in selection list
* Returns the number of links created * Returns the number of links created, -1 on error
*/ */
static int xlink(char *suffix, char *path, char *curfname, char *buf, int *presel, int type) static int xlink(char *suffix, char *path, char *curfname, char *buf, int *presel, int type)
{ {
@ -2390,10 +2384,10 @@ static int xlink(char *suffix, char *path, char *curfname, char *buf, int *prese
xstrlcpy(lnpath + r - 1, suffix, PATH_MAX - r - 1); xstrlcpy(lnpath + r - 1, suffix, PATH_MAX - r - 1);
if (!link_fn(buf, lnpath)) if (!link_fn(buf, lnpath))
return 1; return 1; /* One link created */
printwarn(presel); printwarn(presel);
return 0; /* One link created */ return -1;
} }
while (pos < selbufpos) { while (pos < selbufpos) {
@ -2409,9 +2403,6 @@ static int xlink(char *suffix, char *path, char *curfname, char *buf, int *prese
pbuf += len + 1; pbuf += len + 1;
} }
if (!count)
printwait(messages[MSG_0_CREATED], presel);
return count; return count;
} }
@ -5113,8 +5104,24 @@ nochange:
goto nochange; goto nochange;
} }
/* Confirm if app is CLI or GUI */ switch (sel) {
case SEL_ARCHIVE:
mkpath(path, tmp, newpath);
if (access(newpath, F_OK) == 0) {
fd = get_input(messages[MSG_OVERWRITE]);
if (r != 'y' && r != 'Y') {
clearprompt();
goto nochange;
}
}
get_archive_cmd(newpath, tmp);
(r == 's') ? archive_selection(newpath, tmp, path)
: spawn(newpath, tmp, dents[cur].name,
path, F_NORMAL | F_MULTI);
// fallthrough
case SEL_OPENWITH:
if (sel == SEL_OPENWITH) { if (sel == SEL_OPENWITH) {
/* Confirm if app is CLI or GUI */
r = get_input(messages[MSG_CLI_MODE]); r = get_input(messages[MSG_CLI_MODE]);
r = (r == 'c' ? F_CLI : r = (r == 'c' ? F_CLI :
(r == 'g' ? F_NOWAIT | F_NOTRACE | F_MULTI : 0)); (r == 'g' ? F_NOWAIT | F_NOTRACE | F_MULTI : 0));
@ -5122,24 +5129,14 @@ nochange:
cfg.filtermode ? presel = FILTER : clearprompt(); cfg.filtermode ? presel = FILTER : clearprompt();
goto nochange; goto nochange;
} }
}
switch (sel) {
case SEL_ARCHIVE:
{
char cmd[ARCHIVE_CMD_LEN];
get_archive_cmd(cmd, tmp);
(r == 's') ? archive_selection(cmd, tmp, path)
: spawn(cmd, tmp, dents[cur].name,
path, F_NORMAL | F_MULTI);
break;
}
case SEL_OPENWITH:
mkpath(path, dents[cur].name, newpath); mkpath(path, dents[cur].name, newpath);
spawn(tmp, newpath, NULL, path, r); spawn(tmp, newpath, NULL, path, r);
break; }
if (cfg.filtermode)
presel = FILTER;
copycurname();
goto begin;
case SEL_RENAME: case SEL_RENAME:
/* Skip renaming to same name */ /* Skip renaming to same name */
if (strcmp(tmp, dents[cur].name) == 0) { if (strcmp(tmp, dents[cur].name) == 0) {
@ -5150,23 +5147,10 @@ nochange:
dup = 'd'; dup = 'd';
} }
break; break;
default: default: /* SEL_NEW */
break; break;
} }
/* Complete OPEN, LAUNCH, ARCHIVE operations */
if (sel != SEL_NEW && sel != SEL_RENAME) {
/* Continue in navigate-as-you-type mode, if enabled */
if (cfg.filtermode)
presel = FILTER;
/* Save current */
copycurname();
/* Repopulate as directory content may have changed */
goto begin;
}
/* Open the descriptor to currently open directory */ /* Open the descriptor to currently open directory */
#ifdef O_DIRECTORY #ifdef O_DIRECTORY
fd = open(path, O_RDONLY | O_DIRECTORY); fd = open(path, O_RDONLY | O_DIRECTORY);
@ -5204,45 +5188,42 @@ nochange:
printwarn(&presel); printwarn(&presel);
goto nochange; goto nochange;
} }
close(fd);
xstrlcpy(lastname, tmp, NAME_MAX + 1);
} else { } else {
close(fd); /* Use fd as tmp var */
presel = 0;
/* Check if it's a dir or file */ /* Check if it's a dir or file */
if (r == 'f') { if (r == 'f') {
mkpath(path, tmp, newpath); mkpath(path, tmp, newpath);
r = xmktree(newpath, FALSE); fd = xmktree(newpath, FALSE);
} else if (r == 'd') { } else if (r == 'd') {
mkpath(path, tmp, newpath); mkpath(path, tmp, newpath);
r = xmktree(newpath, TRUE); fd = xmktree(newpath, TRUE);
} else if (r == 's' || r == 'h') { } else if (r == 's' || r == 'h') {
if (tmp[0] == '@' && tmp[1] == '\0') if (tmp[0] == '@' && tmp[1] == '\0')
tmp[0] = '\0'; tmp[0] = '\0';
r = xlink(tmp, path, (ndents ? dents[cur].name : NULL), fd = xlink(tmp, path, (ndents ? dents[cur].name : NULL),
newpath, &presel, r); newpath, &presel, r);
close(fd); }
if (r <= 0) if (!fd)
printwait(messages[MSG_FAILED], &presel);
if (fd <= 0)
goto nochange; goto nochange;
if (r == 'f' || r == 'd')
xstrlcpy(lastname, tmp, NAME_MAX + 1);
else if (ndents) {
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;
if (ndents)
copycurname(); copycurname();
goto begin;
} else {
close(fd);
break;
}
/* Check if file creation failed */
if (r == -1) {
printwarn(&presel);
close(fd);
goto nochange;
} }
} }
close(fd);
xstrlcpy(lastname, tmp, NAME_MAX + 1);
goto begin; goto begin;
} }
case SEL_PLUGKEY: // fallthrough case SEL_PLUGKEY: // fallthrough