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
1 changed files with 68 additions and 87 deletions

155
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,33 +5104,39 @@ nochange:
goto nochange; goto nochange;
} }
/* Confirm if app is CLI or GUI */
if (sel == SEL_OPENWITH) {
r = get_input(messages[MSG_CLI_MODE]);
r = (r == 'c' ? F_CLI :
(r == 'g' ? F_NOWAIT | F_NOTRACE | F_MULTI : 0));
if (!r) {
cfg.filtermode ? presel = FILTER : clearprompt();
goto nochange;
}
}
switch (sel) { switch (sel) {
case SEL_ARCHIVE: case SEL_ARCHIVE:
{ mkpath(path, tmp, newpath);
char cmd[ARCHIVE_CMD_LEN]; if (access(newpath, F_OK) == 0) {
fd = get_input(messages[MSG_OVERWRITE]);
get_archive_cmd(cmd, tmp); if (r != 'y' && r != 'Y') {
clearprompt();
(r == 's') ? archive_selection(cmd, tmp, path) goto nochange;
: spawn(cmd, tmp, dents[cur].name, }
}
get_archive_cmd(newpath, tmp);
(r == 's') ? archive_selection(newpath, tmp, path)
: spawn(newpath, tmp, dents[cur].name,
path, F_NORMAL | F_MULTI); path, F_NORMAL | F_MULTI);
break; // fallthrough
}
case SEL_OPENWITH: case SEL_OPENWITH:
mkpath(path, dents[cur].name, newpath); if (sel == SEL_OPENWITH) {
spawn(tmp, newpath, NULL, path, r); /* Confirm if app is CLI or GUI */
break; r = get_input(messages[MSG_CLI_MODE]);
r = (r == 'c' ? F_CLI :
(r == 'g' ? F_NOWAIT | F_NOTRACE | F_MULTI : 0));
if (!r) {
cfg.filtermode ? presel = FILTER : clearprompt();
goto nochange;
}
mkpath(path, dents[cur].name, newpath);
spawn(tmp, newpath, NULL, path, r);
}
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)
goto nochange;
if (cfg.filtermode)
presel = FILTER;
if (ndents)
copycurname();
goto begin;
} else {
close(fd);
break;
} }
/* Check if file creation failed */ if (!fd)
if (r == -1) { printwait(messages[MSG_FAILED], &presel);
printwarn(&presel);
close(fd); if (fd <= 0)
goto nochange; goto nochange;
if (r == 'f' || r == 'd')
xstrlcpy(lastname, tmp, NAME_MAX + 1);
else if (ndents) {
if (cfg.filtermode)
presel = FILTER;
copycurname();
} }
} }
close(fd);
xstrlcpy(lastname, tmp, NAME_MAX + 1);
goto begin; goto begin;
} }
case SEL_PLUGKEY: // fallthrough case SEL_PLUGKEY: // fallthrough