diff --git a/src/dbg.h b/src/dbg.h new file mode 100644 index 00000000..4d5b54a4 --- /dev/null +++ b/src/dbg.h @@ -0,0 +1,88 @@ +/* + * BSD 2-Clause License + * + * Copyright (C) 2014-2016, Lazaros Koromilas + * Copyright (C) 2014-2016, Dimitris Papastamos + * Copyright (C) 2016-2019, Arun Prakash Jana + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#ifdef DBGMODE +static int DEBUG_FD; + +static int xprintf(int fd, const char *fmt, ...) +{ + char buf[BUFSIZ]; + int r; + va_list ap; + + va_start(ap, fmt); + r = vsnprintf(buf, sizeof(buf), fmt, ap); + if (r > 0) + r = write(fd, buf, r); + va_end(ap); + return r; +} + +static int enabledbg() +{ + FILE *fp = fopen("/tmp/nnndbg", "w"); + + if (!fp) { + perror("dbg(1)"); + + fp = fopen("./nnndbg", "w"); + if (!fp) { + perror("dbg(2)"); + return -1; + } + } + + DEBUG_FD = dup(fileno(fp)); + fclose(fp); + if (DEBUG_FD == -1) { + perror("dbg(3)"); + return -1; + } + + return 0; +} + +static void disabledbg() +{ + close(DEBUG_FD); +} + +#define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x) +#define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x) +#define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x) +#define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=%p\n", x) +#else +#define DPRINTF_D(x) +#define DPRINTF_U(x) +#define DPRINTF_S(x) +#define DPRINTF_P(x) +#endif /* DBGMODE */ diff --git a/src/nnn.c b/src/nnn.c index 7efaf37b..65cd3942 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -94,76 +94,17 @@ #include #include -#ifndef S_BLKSIZE -#define S_BLKSIZE 512 /* S_BLKSIZE is missing on Android NDK (Termux) */ -#endif - #include "nnn.h" - -#ifdef DBGMODE -static int DEBUG_FD; - -static int -xprintf(int fd, const char *fmt, ...) -{ - char buf[BUFSIZ]; - int r; - va_list ap; - - va_start(ap, fmt); - r = vsnprintf(buf, sizeof(buf), fmt, ap); - if (r > 0) - r = write(fd, buf, r); - va_end(ap); - return r; -} - -static int -enabledbg() -{ - FILE *fp = fopen("/tmp/nnndbg", "w"); - - if (!fp) { - perror("dbg(1)"); - - fp = fopen("./nnndbg", "w"); - if (!fp) { - perror("dbg(2)"); - return -1; - } - } - - DEBUG_FD = dup(fileno(fp)); - fclose(fp); - if (DEBUG_FD == -1) { - perror("dbg(3)"); - return -1; - } - - return 0; -} - -static void -disabledbg() -{ - close(DEBUG_FD); -} - -#define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x) -#define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x) -#define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x) -#define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=%p\n", x) -#else -#define DPRINTF_D(x) -#define DPRINTF_U(x) -#define DPRINTF_S(x) -#define DPRINTF_P(x) -#endif /* DBGMODE */ +#include "dbg.h" /* Macro definitions */ #define VERSION "2.3" #define GENERAL_INFO "BSD 2-Clause\nhttps://github.com/jarun/nnn" +#ifndef S_BLKSIZE +#define S_BLKSIZE 512 /* S_BLKSIZE is missing on Android NDK (Termux) */ +#endif + #define LEN(x) (sizeof(x) / sizeof(*(x))) #undef MIN #define MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -227,30 +168,6 @@ disabledbg() #define FREE 0 #define CAPACITY 1 -/* Function macros */ -#define exitcurses() endwin() -#define clearprompt() printmsg("") -#define printwarn() printmsg(strerror(errno)) -#define istopdir(path) ((path)[1] == '\0' && (path)[0] == '/') -#define copycurname() xstrlcpy(lastname, dents[cur].name, NAME_MAX + 1) -#define settimeout() timeout(1000) -#define cleartimeout() timeout(-1) -#define errexit() printerr(__LINE__) -#define setdirwatch() (cfg.filtermode ? (presel = FILTER) : (dir_changed = TRUE)) -/* We don't care about the return value from strcmp() */ -#define xstrcmp(a, b) (*(a) != *(b) ? -1 : strcmp((a), (b))) -/* A faster version of xisdigit */ -#define xisdigit(c) ((unsigned int) (c) - '0' <= 9) -#define xerror() perror(xitoa(__LINE__)) - -#ifdef LINUX_INOTIFY -#define EVENT_SIZE (sizeof(struct inotify_event)) -#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) -#elif defined(BSD_KQUEUE) -#define NUM_EVENT_SLOTS 1 -#define NUM_EVENT_FDS 1 -#endif - /* TYPE DEFINITIONS */ typedef unsigned long ulong; typedef unsigned int uint; @@ -359,18 +276,6 @@ static char g_cppath[PATH_MAX] __attribute__ ((aligned)); /* Buffer to store tmp file path */ static char g_tmpfpath[HOME_LEN_MAX] __attribute__ ((aligned)); -#ifdef LINUX_INOTIFY -static int inotify_fd, inotify_wd = -1; -static uint INOTIFY_MASK = IN_ATTRIB | IN_CREATE | IN_DELETE | IN_DELETE_SELF - | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO; -#elif defined(BSD_KQUEUE) -static int kq, event_fd = -1; -static struct kevent events_to_monitor[NUM_EVENT_FDS]; -static uint KQUEUE_FFLAGS = NOTE_DELETE | NOTE_EXTEND | NOTE_LINK - | NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE; -static struct timespec gtimeout; -#endif - /* Replace-str for xargs on different platforms */ #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) #define REPLACE_STR 'J' @@ -495,6 +400,39 @@ static const char * const envs[] = { "PAGER", }; +/* Event handling */ +#ifdef LINUX_INOTIFY +#define EVENT_SIZE (sizeof(struct inotify_event)) +#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) +static int inotify_fd, inotify_wd = -1; +static uint INOTIFY_MASK = IN_ATTRIB | IN_CREATE | IN_DELETE | IN_DELETE_SELF + | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO; +#elif defined(BSD_KQUEUE) +#define NUM_EVENT_SLOTS 1 +#define NUM_EVENT_FDS 1 +static int kq, event_fd = -1; +static struct kevent events_to_monitor[NUM_EVENT_FDS]; +static uint KQUEUE_FFLAGS = NOTE_DELETE | NOTE_EXTEND | NOTE_LINK + | NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE; +static struct timespec gtimeout; +#endif + +/* Function macros */ +#define exitcurses() endwin() +#define clearprompt() printmsg("") +#define printwarn() printmsg(strerror(errno)) +#define istopdir(path) ((path)[1] == '\0' && (path)[0] == '/') +#define copycurname() xstrlcpy(lastname, dents[cur].name, NAME_MAX + 1) +#define settimeout() timeout(1000) +#define cleartimeout() timeout(-1) +#define errexit() printerr(__LINE__) +#define setdirwatch() (cfg.filtermode ? (presel = FILTER) : (dir_changed = TRUE)) +/* We don't care about the return value from strcmp() */ +#define xstrcmp(a, b) (*(a) != *(b) ? -1 : strcmp((a), (b))) +/* A faster version of xisdigit */ +#define xisdigit(c) ((unsigned int) (c) - '0' <= 9) +#define xerror() perror(xitoa(__LINE__)) + /* Forward declarations */ static void redraw(char *path); static void spawn(char *file, char *arg1, char *arg2, const char *dir, uchar flag);