Improve common message handling

This commit is contained in:
Arun Prakash Jana 2018-01-14 12:11:46 +05:30
parent 9bcca1a45c
commit 9f4199481a
No known key found for this signature in database
GPG Key ID: A75979F35C080412
1 changed files with 35 additions and 24 deletions

59
nnn.c
View File

@ -278,14 +278,25 @@ static char * const utils[] = {
"atool" "atool"
}; };
/* Common message strings */ /* Common strings */
static const char *STR_NFTWFAIL = "nftw(3) failed"; #define STR_NFTWFAIL_ID 0
static const char *STR_ATROOT = "at /"; #define STR_ATROOT_ID 1
static const char *STR_NOHOME = "HOME not set"; #define STR_NOHOME_ID 2
static const char *STR_INPUT = "remove traversal delimiter"; #define STR_INPUT_ID 3
static const char *STR_INVBM = "invalid bookmark"; #define STR_INVBM_ID 4
static const char *STR_COPY = "set NNN_COPIER"; #define STR_COPY_ID 5
static const char *STR_DATE = "%a %d %b %Y %T %z"; #define STR_DATE_ID 6
static const char messages[][16] =
{
"nftw(3) failed",
"already at /",
"HOME not set",
"no traversal",
"invalid key",
"set NNN_COPIER",
"%a %F %T %z",
};
/* For use in functions which are isolated and don't return the buffer */ /* For use in functions which are isolated and don't return the buffer */
static char g_buf[MAX_CMD_LEN] __attribute__ ((aligned)); static char g_buf[MAX_CMD_LEN] __attribute__ ((aligned));
@ -1309,7 +1320,7 @@ get_bm_loc(char *key, char *buf)
char *home = getenv("HOME"); char *home = getenv("HOME");
if (!home) { if (!home) {
DPRINTF_S(STR_NOHOME); DPRINTF_S(messages[STR_NOHOME_ID]);
return NULL; return NULL;
} }
@ -1686,15 +1697,15 @@ show_stats(char *fpath, char *fname, struct stat *sb)
sb->st_mode & 7, perms, sb->st_uid, (getpwuid(sb->st_uid))->pw_name, sb->st_gid, (getgrgid(sb->st_gid))->gr_name); sb->st_mode & 7, perms, sb->st_uid, (getpwuid(sb->st_uid))->pw_name, sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
/* Show last access time */ /* Show last access time */
strftime(g_buf, 40, STR_DATE, localtime(&sb->st_atime)); strftime(g_buf, 40, messages[STR_DATE_ID], localtime(&sb->st_atime));
dprintf(fd, "\n\n Access: %s", g_buf); dprintf(fd, "\n\n Access: %s", g_buf);
/* Show last modification time */ /* Show last modification time */
strftime(g_buf, 40, STR_DATE, localtime(&sb->st_mtime)); strftime(g_buf, 40, messages[STR_DATE_ID], localtime(&sb->st_mtime));
dprintf(fd, "\n Modify: %s", g_buf); dprintf(fd, "\n Modify: %s", g_buf);
/* Show last status change time */ /* Show last status change time */
strftime(g_buf, 40, STR_DATE, localtime(&sb->st_ctime)); strftime(g_buf, 40, messages[STR_DATE_ID], localtime(&sb->st_ctime));
dprintf(fd, "\n Change: %s", g_buf); dprintf(fd, "\n Change: %s", g_buf);
if (S_ISREG(sb->st_mode)) { if (S_ISREG(sb->st_mode)) {
@ -1955,7 +1966,7 @@ dentfill(char *path, struct entry **dents,
mkpath(path, namep, g_buf, PATH_MAX); mkpath(path, namep, g_buf, PATH_MAX);
if (nftw(g_buf, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) { if (nftw(g_buf, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
printmsg(STR_NFTWFAIL); printmsg(messages[STR_NFTWFAIL_ID]);
dir_blocks += sb.st_blocks; dir_blocks += sb.st_blocks;
} else } else
dir_blocks += ent_blocks; dir_blocks += ent_blocks;
@ -2033,7 +2044,7 @@ dentfill(char *path, struct entry **dents,
mkpath(path, namep, g_buf, PATH_MAX); mkpath(path, namep, g_buf, PATH_MAX);
if (nftw(g_buf, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) { if (nftw(g_buf, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
printmsg(STR_NFTWFAIL); printmsg(messages[STR_NFTWFAIL_ID]);
dentp->blocks = sb.st_blocks; dentp->blocks = sb.st_blocks;
} else } else
dentp->blocks = ent_blocks; dentp->blocks = ent_blocks;
@ -2335,7 +2346,7 @@ nochange:
case SEL_BACK: case SEL_BACK:
/* There is no going back */ /* There is no going back */
if (istopdir(path)) { if (istopdir(path)) {
printmsg(STR_ATROOT); printmsg(messages[STR_ATROOT_ID]);
goto nochange; goto nochange;
} }
@ -2506,7 +2517,7 @@ nochange:
snprintf(newpath, PATH_MAX, "%s%s", home, tmp + 1); snprintf(newpath, PATH_MAX, "%s%s", home, tmp + 1);
else { else {
free(input); free(input);
printmsg(STR_NOHOME); printmsg(messages[STR_NOHOME_ID]);
goto nochange; goto nochange;
} }
} else if (tmp[0] == '-' && tmp[1] == '\0') { } else if (tmp[0] == '-' && tmp[1] == '\0') {
@ -2527,7 +2538,7 @@ nochange:
/* Show a message if already at / */ /* Show a message if already at / */
if (istopdir(path)) { if (istopdir(path)) {
printmsg(STR_ATROOT); printmsg(messages[STR_ATROOT_ID]);
free(input); free(input);
goto nochange; goto nochange;
} }
@ -2662,7 +2673,7 @@ nochange:
break; break;
if (get_bm_loc(tmp, newpath) == NULL) { if (get_bm_loc(tmp, newpath) == NULL) {
printmsg(STR_INVBM); printmsg(messages[STR_INVBM_ID]);
goto nochange; goto nochange;
} }
@ -2816,11 +2827,11 @@ nochange:
spawn(copier, newpath, NULL, NULL, F_NONE); spawn(copier, newpath, NULL, NULL, F_NONE);
printmsg(newpath); printmsg(newpath);
} else if (!copier) } else if (!copier)
printmsg(STR_COPY); printmsg(messages[STR_COPY_ID]);
goto nochange; goto nochange;
case SEL_COPYMUL: case SEL_COPYMUL:
if (!copier) { if (!copier) {
printmsg(STR_COPY); printmsg(messages[STR_COPY_ID]);
goto nochange; goto nochange;
} else if (!ndents) { } else if (!ndents) {
goto nochange; goto nochange;
@ -2878,7 +2889,7 @@ nochange:
/* Allow only relative, same dir paths */ /* Allow only relative, same dir paths */
if (tmp[0] == '/' || xstrcmp(xbasename(tmp), tmp) != 0) { if (tmp[0] == '/' || xstrcmp(xbasename(tmp), tmp) != 0) {
printmsg(STR_INPUT); printmsg(messages[STR_INPUT_ID]);
goto nochange; goto nochange;
} }
@ -2912,7 +2923,7 @@ nochange:
} }
/* Check if it's a dir or file */ /* Check if it's a dir or file */
printprompt("press 'f' (file) or 'd' (dir)"); printprompt("press 'f'(ile) or 'd'(ir)");
cleartimeout(); cleartimeout();
r = getch(); r = getch();
settimeout(); settimeout();
@ -2947,7 +2958,7 @@ nochange:
/* Allow only relative, same dir paths */ /* Allow only relative, same dir paths */
if (tmp[0] == '/' || xstrcmp(xbasename(tmp), tmp) != 0) { if (tmp[0] == '/' || xstrcmp(xbasename(tmp), tmp) != 0) {
printmsg(STR_INPUT); printmsg(messages[STR_INPUT_ID]);
goto nochange; goto nochange;
} }
@ -3104,7 +3115,7 @@ main(int argc, char *argv[])
if (ipath) { /* Open a bookmark directly */ if (ipath) { /* Open a bookmark directly */
if (get_bm_loc(ipath, cwd) == NULL) { if (get_bm_loc(ipath, cwd) == NULL) {
fprintf(stderr, "%s\n", STR_INVBM); fprintf(stderr, "%s\n", messages[STR_INVBM_ID]);
exit(1); exit(1);
} }