Code refactor

This commit is contained in:
Arun Prakash Jana 2019-03-09 09:13:36 +05:30
parent 5dd5710b31
commit 7a2fdc0e1d
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 43 additions and 45 deletions

View file

@ -22,7 +22,7 @@ Noice is Not Noice, a noicer fork...
<p align="center"><i>nnn in action! (Thanks Luke Smith for the video!)</i></a></p> <p align="center"><i>nnn in action! (Thanks Luke Smith for the video!)</i></a></p>
`nnn` is smooth... like butter. It's also one of the fastest and most lightweight file managers you have ever used. It comes in a `~55KB` binary using `~3.5MB` resident memory at runtime. `nnn` is smooth... like butter. It's also one of the fastest and most lightweight file managers you have ever used. It comes in a `~50KB` binary using `~3.5MB` resident memory at runtime.
`nnn` integrates seamlessly with your DE and favourite GUI utilities, has a unique _navigate-as-you-type_ mode with auto-select, disk usage analyzer mode, bookmarks, contexts, application launcher, familiar navigation shortcuts, subshell spawning, quick notes and much more. `nnn` integrates seamlessly with your DE and favourite GUI utilities, has a unique _navigate-as-you-type_ mode with auto-select, disk usage analyzer mode, bookmarks, contexts, application launcher, familiar navigation shortcuts, subshell spawning, quick notes and much more.

View file

@ -237,6 +237,7 @@ disabledbg()
#define xstrcmp(a, b) (*(a) != *(b) ? -1 : strcmp((a), (b))) #define xstrcmp(a, b) (*(a) != *(b) ? -1 : strcmp((a), (b)))
/* A faster version of xisdigit */ /* A faster version of xisdigit */
#define xisdigit(c) ((unsigned int) (c) - '0' <= 9) #define xisdigit(c) ((unsigned int) (c) - '0' <= 9)
#define xerror() perror(xitoa(__LINE__))
#ifdef LINUX_INOTIFY #ifdef LINUX_INOTIFY
#define EVENT_SIZE (sizeof(struct inotify_event)) #define EVENT_SIZE (sizeof(struct inotify_event))
@ -529,11 +530,38 @@ static uchar crc8fast(const uchar * const message, size_t n)
return remainder; return remainder;
} }
static void sigint_handler(int sig, siginfo_t *siginfo, void *context) static void sigint_handler(int sig)
{ {
interrupted = TRUE; interrupted = TRUE;
} }
static uint xatoi(const char *str)
{
int val = 0;
if (!str)
return 0;
while (xisdigit(*str)) {
val = val * 10 + (*str - '0');
++str;
}
return val;
}
static char *xitoa(uint val)
{
static const char hexbuf[] = "0123456789";
static char ascbuf[32] = {0};
int i;
for (i = 30; val && i; --i, val /= 10)
ascbuf[i] = hexbuf[val % 10];
return &ascbuf[++i];
}
/* Messages show up at the bottom */ /* Messages show up at the bottom */
static void printmsg(const char *msg) static void printmsg(const char *msg)
{ {
@ -544,7 +572,7 @@ static void printmsg(const char *msg)
static void printerr(int linenum) static void printerr(int linenum)
{ {
exitcurses(); exitcurses();
fprintf(stderr, "line %d: (%d) %s\n", linenum, errno, strerror(errno)); perror(xitoa(linenum));
if (!cfg.picker && g_cppath[0]) if (!cfg.picker && g_cppath[0])
unlink(g_cppath); unlink(g_cppath);
free(pcopybuf); free(pcopybuf);
@ -769,33 +797,6 @@ static char *xbasename(char *path)
return base ? base + 1 : path; return base ? base + 1 : path;
} }
static uint xatoi(const char *str)
{
int val = 0;
if (!str)
return 0;
while (xisdigit(*str)) {
val = val * 10 + (*str - '0');
++str;
}
return val;
}
static char *xitoa(uint val)
{
static const char hexbuf[] = "0123456789";
static char ascbuf[32] = {0};
int i;
for (i = 30; val && i; --i, val /= 10)
ascbuf[i] = hexbuf[val % 10];
return &ascbuf[++i];
}
/* Writes buflen char(s) from buf to a file */ /* Writes buflen char(s) from buf to a file */
static void writecp(const char *buf, const size_t buflen) static void writecp(const char *buf, const size_t buflen)
{ {
@ -3904,7 +3905,6 @@ int main(int argc, char *argv[])
char cwd[PATH_MAX] __attribute__ ((aligned)); char cwd[PATH_MAX] __attribute__ ((aligned));
char *ipath = NULL; char *ipath = NULL;
int opt; int opt;
struct sigaction act;
while ((opt = getopt(argc, argv, "Slib:enp:svwh")) != -1) { while ((opt = getopt(argc, argv, "Slib:enp:svwh")) != -1) {
switch (opt) { switch (opt) {
@ -3936,8 +3936,8 @@ int main(int argc, char *argv[])
else { else {
/* copier used as tmp var */ /* copier used as tmp var */
copier = realpath(optarg, g_cppath); copier = realpath(optarg, g_cppath);
if (!g_cppath[0]) { if (!copier) {
fprintf(stderr, "%s\n", strerror(errno)); xerror();
return 1; return 1;
} }
} }
@ -3963,7 +3963,7 @@ int main(int argc, char *argv[])
/* Confirm we are in a terminal */ /* Confirm we are in a terminal */
if (!cfg.picker && !(isatty(0) && isatty(1))) { if (!cfg.picker && !(isatty(0) && isatty(1))) {
fprintf(stderr, "stdin/stdout !tty\n"); xerror();
return 1; return 1;
} }
@ -3973,7 +3973,7 @@ int main(int argc, char *argv[])
while (opt < CTX_MAX) { while (opt < CTX_MAX) {
if (*copier) { if (*copier) {
if (*copier < '0' || *copier > '7') { if (*copier < '0' || *copier > '7') {
fprintf(stderr, "invalid color code\n"); fprintf(stderr, "0 <= code <= 7\n");
return 1; return 1;
} }
@ -3987,7 +3987,7 @@ int main(int argc, char *argv[])
/* Parse bookmarks string */ /* Parse bookmarks string */
if (!parsebmstr()) { if (!parsebmstr()) {
fprintf(stderr, "%s: malformed\n", env_cfg[NNN_BMS]); fprintf(stderr, "%s\n", env_cfg[NNN_BMS]);
return 1; return 1;
} }
@ -4006,7 +4006,7 @@ int main(int argc, char *argv[])
} else { } else {
ipath = realpath(argv[optind], cwd); ipath = realpath(argv[optind], cwd);
if (!ipath) { if (!ipath) {
fprintf(stderr, "%s: no such dir\n", argv[optind]); xerror();
return 1; return 1;
} }
} }
@ -4045,13 +4045,13 @@ int main(int argc, char *argv[])
/* Initialize inotify */ /* Initialize inotify */
inotify_fd = inotify_init1(IN_NONBLOCK); inotify_fd = inotify_init1(IN_NONBLOCK);
if (inotify_fd < 0) { if (inotify_fd < 0) {
fprintf(stderr, "inotify init! %s\n", strerror(errno)); xerror();
return 1; return 1;
} }
#elif defined(BSD_KQUEUE) #elif defined(BSD_KQUEUE)
kq = kqueue(); kq = kqueue();
if (kq < 0) { if (kq < 0) {
fprintf(stderr, "kqueue init! %s\n", strerror(errno)); xerror();
return 1; return 1;
} }
#endif #endif
@ -4112,18 +4112,16 @@ int main(int argc, char *argv[])
#endif #endif
/* Ignore/handle certain signals */ /* Ignore/handle certain signals */
memset(&act, 0, sizeof(act)); struct sigaction act = {.sa_handler = sigint_handler};
act.sa_sigaction = &sigint_handler;
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGINT, &act, NULL) < 0) { if (sigaction(SIGINT, &act, NULL) < 0) {
fprintf(stderr, "sigaction\n"); xerror();
return 1; return 1;
} }
signal(SIGQUIT, SIG_IGN); signal(SIGQUIT, SIG_IGN);
/* Test initial path */ /* Test initial path */
if (!xdiraccess(ipath)) { if (!xdiraccess(ipath)) {
fprintf(stderr, "%s: %s\n", ipath, strerror(errno)); xerror();
return 1; return 1;
} }
@ -4158,7 +4156,7 @@ int main(int argc, char *argv[])
if (copybufpos) { if (copybufpos) {
opt = selectiontofd(1); opt = selectiontofd(1);
if (opt != (int)(copybufpos)) if (opt != (int)(copybufpos))
fprintf(stderr, "%s\n", strerror(errno)); xerror();
} }
} else if (!cfg.picker && g_cppath[0]) } else if (!cfg.picker && g_cppath[0])
unlink(g_cppath); unlink(g_cppath);