|
|
|
|
@@ -1,7 +1,7 @@
|
|
|
|
|
//
|
|
|
|
|
// sdn: simple directory navigator
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2017 - 2022, Přemysl Eric Janouch <p@janouch.name>
|
|
|
|
|
// Copyright (c) 2017 - 2024, Přemysl Eric Janouch <p@janouch.name>
|
|
|
|
|
//
|
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
// purpose with or without fee is hereby granted.
|
|
|
|
|
@@ -39,27 +39,32 @@
|
|
|
|
|
#include <libgen.h>
|
|
|
|
|
#include <pwd.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <sys/acl.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <acl/libacl.h>
|
|
|
|
|
#include <ncurses.h>
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
// ACL information is not important enough to be ported
|
|
|
|
|
#include <acl/libacl.h>
|
|
|
|
|
#include <sys/acl.h>
|
|
|
|
|
#include <sys/xattr.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/event.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <ncurses.h>
|
|
|
|
|
|
|
|
|
|
// To implement cbreak() with disabled ^S that gets reënabled on endwin()
|
|
|
|
|
#define NCURSES_INTERNALS
|
|
|
|
|
#include <term.h>
|
|
|
|
|
#undef CTRL // term.h -> termios.h -> sys/ttydefaults.h, too simplistic
|
|
|
|
|
|
|
|
|
|
// Unicode is complex enough already and we might make assumptions
|
|
|
|
|
#ifndef __STDC_ISO_10646__
|
|
|
|
|
#error Unicode required for wchar_t
|
|
|
|
|
// Unicode is complex enough already and we might make assumptions,
|
|
|
|
|
// though macOS doesn't define this despite using UCS-4,
|
|
|
|
|
// and we won't build on Windows that seems to be the only one to use UTF-16.
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Trailing return types make C++ syntax suck considerably less
|
|
|
|
|
@@ -303,7 +308,21 @@ fun xdg_config_write (const string &suffix) -> unique_ptr<fstream> {
|
|
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
|
|
using ncstring = basic_string<cchar_t>;
|
|
|
|
|
// This should be basic_string, however that crashes on macOS
|
|
|
|
|
using ncstring = vector<cchar_t>;
|
|
|
|
|
|
|
|
|
|
fun operator+ (const ncstring &lhs, const ncstring &rhs) -> ncstring {
|
|
|
|
|
ncstring result;
|
|
|
|
|
result.reserve (lhs.size () + rhs.size ());
|
|
|
|
|
result.insert (result.end (), lhs.begin (), lhs.end ());
|
|
|
|
|
result.insert (result.end (), rhs.begin (), rhs.end ());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun operator+= (ncstring &lhs, const ncstring &rhs) -> ncstring & {
|
|
|
|
|
lhs.insert (lhs.end (), rhs.begin (), rhs.end ());
|
|
|
|
|
return lhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun cchar (chtype attrs, wchar_t c) -> cchar_t {
|
|
|
|
|
cchar_t ch {}; wchar_t ws[] = {c, 0};
|
|
|
|
|
@@ -416,7 +435,8 @@ enum { ALT = 1 << 24, SYM = 1 << 25 }; // Outside the range of Unicode
|
|
|
|
|
XX(SEARCH) XX(RENAME) XX(RENAME_PREFILL) XX(MKDIR) \
|
|
|
|
|
XX(TOGGLE_FULL) XX(REVERSE_SORT) XX(SHOW_HIDDEN) XX(REDRAW) XX(RELOAD) \
|
|
|
|
|
XX(INPUT_ABORT) XX(INPUT_CONFIRM) XX(INPUT_B_DELETE) XX(INPUT_DELETE) \
|
|
|
|
|
XX(INPUT_B_KILL_LINE) XX(INPUT_KILL_LINE) XX(INPUT_QUOTED_INSERT) \
|
|
|
|
|
XX(INPUT_B_KILL_WORD) XX(INPUT_B_KILL_LINE) XX(INPUT_KILL_LINE) \
|
|
|
|
|
XX(INPUT_QUOTED_INSERT) \
|
|
|
|
|
XX(INPUT_BACKWARD) XX(INPUT_FORWARD) XX(INPUT_BEGINNING) XX(INPUT_END)
|
|
|
|
|
|
|
|
|
|
#define XX(name) ACTION_ ## name,
|
|
|
|
|
@@ -459,7 +479,8 @@ static map<wint_t, action> g_input_actions {
|
|
|
|
|
// Sometimes terminfo is wrong, we need to accept both of these
|
|
|
|
|
{L'\b', ACTION_INPUT_B_DELETE}, {CTRL ('?'), ACTION_INPUT_B_DELETE},
|
|
|
|
|
{KEY (BACKSPACE), ACTION_INPUT_B_DELETE}, {KEY (DC), ACTION_INPUT_DELETE},
|
|
|
|
|
{CTRL ('D'), ACTION_INPUT_DELETE}, {CTRL ('U'), ACTION_INPUT_B_KILL_LINE},
|
|
|
|
|
{CTRL ('W'), ACTION_INPUT_B_KILL_WORD}, {CTRL ('D'), ACTION_INPUT_DELETE},
|
|
|
|
|
{CTRL ('U'), ACTION_INPUT_B_KILL_LINE},
|
|
|
|
|
{CTRL ('K'), ACTION_INPUT_KILL_LINE},
|
|
|
|
|
{CTRL ('V'), ACTION_INPUT_QUOTED_INSERT},
|
|
|
|
|
{CTRL ('B'), ACTION_INPUT_BACKWARD}, {KEY (LEFT), ACTION_INPUT_BACKWARD},
|
|
|
|
|
@@ -493,7 +514,7 @@ static const char *g_ls_colors[] = {LS(XX)};
|
|
|
|
|
|
|
|
|
|
struct stringcaseless {
|
|
|
|
|
bool operator () (const string &a, const string &b) const {
|
|
|
|
|
const auto &c = locale::classic();
|
|
|
|
|
const auto &c = locale::classic ();
|
|
|
|
|
return lexicographical_compare (begin (a), end (a), begin (b), end (b),
|
|
|
|
|
[&](char m, char n) { return tolower (m, c) < tolower (n, c); });
|
|
|
|
|
}
|
|
|
|
|
@@ -536,7 +557,7 @@ static struct {
|
|
|
|
|
bool no_chdir; ///< Do not tell the shell to chdir
|
|
|
|
|
bool quitting; ///< Whether we should quit already
|
|
|
|
|
|
|
|
|
|
int inotify_fd, inotify_wd = -1; ///< File watch
|
|
|
|
|
int watch_fd, watch_wd = -1; ///< File watch (inotify/kqueue)
|
|
|
|
|
bool out_of_date; ///< Entries may be out of date
|
|
|
|
|
|
|
|
|
|
const wchar_t *editor; ///< Prompt string for editing
|
|
|
|
|
@@ -595,8 +616,10 @@ fun ls_format (const entry &e, bool for_target) -> chtype {
|
|
|
|
|
set (LS_MULTIHARDLINK);
|
|
|
|
|
if ((info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
|
|
|
|
|
set (LS_EXECUTABLE);
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
if (lgetxattr (name.c_str (), "security.capability", NULL, 0) >= 0)
|
|
|
|
|
set (LS_CAPABILITY);
|
|
|
|
|
#endif
|
|
|
|
|
if ((info.st_mode & S_ISGID))
|
|
|
|
|
set (LS_SETGID);
|
|
|
|
|
if ((info.st_mode & S_ISUID))
|
|
|
|
|
@@ -638,6 +661,25 @@ fun ls_format (const entry &e, bool for_target) -> chtype {
|
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun suffixize (off_t size, unsigned shift, wchar_t suffix, std::wstring &out)
|
|
|
|
|
-> bool {
|
|
|
|
|
// Prevent implementation-defined and undefined behaviour
|
|
|
|
|
if (size < 0 || shift >= sizeof size * 8)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
off_t divided = size >> shift;
|
|
|
|
|
if (divided >= 10) {
|
|
|
|
|
out.assign (std::to_wstring (divided)).append (1, suffix);
|
|
|
|
|
return true;
|
|
|
|
|
} else if (divided > 0) {
|
|
|
|
|
unsigned times_ten = size / double (off_t (1) << shift) * 10.0;
|
|
|
|
|
out.assign ({L'0' + wchar_t (times_ten / 10), L'.',
|
|
|
|
|
L'0' + wchar_t (times_ten % 10), suffix});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun make_entry (const struct dirent *f) -> entry {
|
|
|
|
|
entry e;
|
|
|
|
|
e.filename = f->d_name;
|
|
|
|
|
@@ -671,11 +713,13 @@ fun make_entry (const struct dirent *f) -> entry {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto mode = decode_mode (info.st_mode);
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
// We're using a laughably small subset of libacl: this translates to
|
|
|
|
|
// two lgetxattr() calls, the results of which are compared with
|
|
|
|
|
// specific architecture-dependent constants. Linux-only.
|
|
|
|
|
if (acl_extended_file_nofollow (f->d_name) > 0)
|
|
|
|
|
mode += L"+";
|
|
|
|
|
#endif
|
|
|
|
|
e.cols[entry::MODES] = apply_attrs (mode, 0);
|
|
|
|
|
|
|
|
|
|
auto usr = g.unames.find (info.st_uid);
|
|
|
|
|
@@ -688,11 +732,12 @@ fun make_entry (const struct dirent *f) -> entry {
|
|
|
|
|
? apply_attrs (grp->second, 0)
|
|
|
|
|
: apply_attrs (to_wstring (info.st_gid), 0);
|
|
|
|
|
|
|
|
|
|
auto size = to_wstring (info.st_size);
|
|
|
|
|
if (info.st_size >> 40) size = to_wstring (info.st_size >> 40) + L"T";
|
|
|
|
|
else if (info.st_size >> 30) size = to_wstring (info.st_size >> 30) + L"G";
|
|
|
|
|
else if (info.st_size >> 20) size = to_wstring (info.st_size >> 20) + L"M";
|
|
|
|
|
else if (info.st_size >> 10) size = to_wstring (info.st_size >> 10) + L"K";
|
|
|
|
|
std::wstring size;
|
|
|
|
|
if (!suffixize (info.st_size, 40, L'T', size) &&
|
|
|
|
|
!suffixize (info.st_size, 30, L'G', size) &&
|
|
|
|
|
!suffixize (info.st_size, 20, L'M', size) &&
|
|
|
|
|
!suffixize (info.st_size, 10, L'K', size))
|
|
|
|
|
size = to_wstring (info.st_size);
|
|
|
|
|
e.cols[entry::SIZE] = apply_attrs (size, 0);
|
|
|
|
|
|
|
|
|
|
wchar_t buf[32] = L"";
|
|
|
|
|
@@ -704,8 +749,8 @@ fun make_entry (const struct dirent *f) -> entry {
|
|
|
|
|
auto &fn = e.cols[entry::FILENAME] =
|
|
|
|
|
apply_attrs (to_wide (e.filename), ls_format (e, false));
|
|
|
|
|
if (!e.target_path.empty ()) {
|
|
|
|
|
fn.append (apply_attrs (L" -> ", 0));
|
|
|
|
|
fn.append (apply_attrs (to_wide (e.target_path), ls_format (e, true)));
|
|
|
|
|
fn += apply_attrs (L" -> ", 0);
|
|
|
|
|
fn += apply_attrs (to_wide (e.target_path), ls_format (e, true));
|
|
|
|
|
}
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
@@ -775,8 +820,8 @@ fun update () {
|
|
|
|
|
print (info, info_width);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto start = sanitize (prompt + line.substr (0, g.editor_cursor));
|
|
|
|
|
move (LINES - 1, compute_width (start));
|
|
|
|
|
line.resize (g.editor_cursor);
|
|
|
|
|
move (LINES - 1, compute_width (sanitize (prompt + line)));
|
|
|
|
|
curs_set (1);
|
|
|
|
|
} else if (!g.message.empty ()) {
|
|
|
|
|
move (LINES - 1, 0);
|
|
|
|
|
@@ -870,7 +915,7 @@ fun reload (bool keep_anchor) {
|
|
|
|
|
show_message (strerror (errno));
|
|
|
|
|
if (g.cwd != "/") {
|
|
|
|
|
struct dirent f = {};
|
|
|
|
|
strncpy(f.d_name, "..", sizeof f.d_name);
|
|
|
|
|
strncpy (f.d_name, "..", sizeof f.d_name);
|
|
|
|
|
f.d_type = DT_DIR;
|
|
|
|
|
g.entries.push_back (make_entry (&f));
|
|
|
|
|
}
|
|
|
|
|
@@ -899,23 +944,38 @@ readfail:
|
|
|
|
|
g.cursor = max (0, min (g.cursor, int (g.entries.size ()) - 1));
|
|
|
|
|
g.offset = max (0, min (g.offset, int (g.entries.size ()) - 1));
|
|
|
|
|
|
|
|
|
|
if (g.inotify_wd != -1)
|
|
|
|
|
inotify_rm_watch (g.inotify_fd, g.inotify_wd);
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
if (g.watch_wd != -1)
|
|
|
|
|
inotify_rm_watch (g.watch_fd, g.watch_wd);
|
|
|
|
|
|
|
|
|
|
// We don't show atime, so access and open are merely spam
|
|
|
|
|
g.inotify_wd = inotify_add_watch (g.inotify_fd, ".",
|
|
|
|
|
g.watch_wd = inotify_add_watch (g.watch_fd, ".",
|
|
|
|
|
(IN_ALL_EVENTS | IN_ONLYDIR | IN_EXCL_UNLINK) & ~(IN_ACCESS | IN_OPEN));
|
|
|
|
|
#else
|
|
|
|
|
if (g.watch_wd != -1)
|
|
|
|
|
close (g.watch_wd);
|
|
|
|
|
|
|
|
|
|
if ((g.watch_wd = open (".", O_RDONLY | O_DIRECTORY | O_CLOEXEC)) >= 0) {
|
|
|
|
|
// At least the macOS kqueue doesn't report anything too specific
|
|
|
|
|
struct kevent ev {};
|
|
|
|
|
EV_SET (&ev, g.watch_wd, EVFILT_VNODE, EV_ADD | EV_CLEAR,
|
|
|
|
|
NOTE_WRITE | NOTE_LINK, 0, nullptr);
|
|
|
|
|
(void) kevent (g.watch_fd, &ev, 1, nullptr, 0, nullptr);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun run_program (initializer_list<const char *> list, const string &filename) {
|
|
|
|
|
auto args = (!filename.empty() && filename.front() == '-' ? " -- " : " ")
|
|
|
|
|
+ shell_escape (filename);
|
|
|
|
|
if (g.ext_helpers) {
|
|
|
|
|
// XXX: this doesn't try them all out, though it shouldn't make any
|
|
|
|
|
// noticeable difference
|
|
|
|
|
// XXX: this doesn't try them all out,
|
|
|
|
|
// though it shouldn't make any noticeable difference
|
|
|
|
|
const char *found = nullptr;
|
|
|
|
|
for (auto program : list)
|
|
|
|
|
if ((found = program))
|
|
|
|
|
break;
|
|
|
|
|
g.ext_helper = found + (" " + shell_escape (filename));
|
|
|
|
|
g.ext_helper.assign (found).append (args);
|
|
|
|
|
g.quitting = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@@ -931,8 +991,8 @@ fun run_program (initializer_list<const char *> list, const string &filename) {
|
|
|
|
|
tcsetpgrp (STDOUT_FILENO, getpgid (0));
|
|
|
|
|
|
|
|
|
|
for (auto program : list)
|
|
|
|
|
if (program) execl ("/bin/sh", "/bin/sh", "-c", (string (program)
|
|
|
|
|
+ " " + shell_escape (filename)).c_str (), NULL);
|
|
|
|
|
if (program) execl ("/bin/sh", "/bin/sh", "-c",
|
|
|
|
|
(program + args).c_str (), NULL);
|
|
|
|
|
_exit (EXIT_FAILURE);
|
|
|
|
|
default:
|
|
|
|
|
// ...and make sure of it in the parent as well
|
|
|
|
|
@@ -1034,7 +1094,7 @@ fun show_help () {
|
|
|
|
|
|
|
|
|
|
fun match (const wstring &needle, int push) -> int {
|
|
|
|
|
string pattern = to_mb (needle) + "*";
|
|
|
|
|
bool jump_to_first = push || fnmatch (pattern.c_str(),
|
|
|
|
|
bool jump_to_first = push || fnmatch (pattern.c_str (),
|
|
|
|
|
g.entries[g.cursor].filename.c_str (), 0) == FNM_NOMATCH;
|
|
|
|
|
int best = g.cursor, matches = 0, step = push + !push;
|
|
|
|
|
for (int i = 0, count = g.entries.size (); i < count; i++) {
|
|
|
|
|
@@ -1322,6 +1382,17 @@ fun handle_editor (wint_t c) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ACTION_INPUT_B_KILL_WORD:
|
|
|
|
|
{
|
|
|
|
|
int i = g.editor_cursor;
|
|
|
|
|
while (i && g.editor_line[--i] == L' ');
|
|
|
|
|
while (i-- && g.editor_line[i] != L' ');
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
g.editor_line.erase (i, g.editor_cursor - i);
|
|
|
|
|
g.editor_cursor = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ACTION_INPUT_B_KILL_LINE:
|
|
|
|
|
g.editor_line.erase (0, g.editor_cursor);
|
|
|
|
|
g.editor_cursor = 0;
|
|
|
|
|
@@ -1337,7 +1408,8 @@ fun handle_editor (wint_t c) {
|
|
|
|
|
if (auto handler = g.editor_on[action]) {
|
|
|
|
|
handler ();
|
|
|
|
|
} else if (c & (ALT | SYM)) {
|
|
|
|
|
beep ();
|
|
|
|
|
if (c != KEY (RESIZE))
|
|
|
|
|
beep ();
|
|
|
|
|
} else {
|
|
|
|
|
g.editor_line.insert (g.editor_cursor, 1, c);
|
|
|
|
|
g.editor_cursor++;
|
|
|
|
|
@@ -1514,19 +1586,27 @@ fun handle (wint_t c) -> bool {
|
|
|
|
|
return !g.quitting;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun inotify_check () {
|
|
|
|
|
// Only provide simple indication that contents might have changed
|
|
|
|
|
char buf[4096]; ssize_t len;
|
|
|
|
|
fun watch_check () {
|
|
|
|
|
bool changed = false;
|
|
|
|
|
while ((len = read (g.inotify_fd, buf, sizeof buf)) > 0) {
|
|
|
|
|
// Only provide simple indication that contents might have changed,
|
|
|
|
|
// if only because kqueue can't do any better
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
char buf[4096]; ssize_t len;
|
|
|
|
|
while ((len = read (g.watch_fd, buf, sizeof buf)) > 0) {
|
|
|
|
|
const inotify_event *e;
|
|
|
|
|
for (char *ptr = buf; ptr < buf + len; ptr += sizeof *e + e->len) {
|
|
|
|
|
e = (const inotify_event *) buf;
|
|
|
|
|
if (e->wd == g.inotify_wd)
|
|
|
|
|
changed = g.out_of_date = true;
|
|
|
|
|
if (e->wd == g.watch_wd)
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (changed)
|
|
|
|
|
#else
|
|
|
|
|
struct kevent ev {};
|
|
|
|
|
struct timespec timeout {};
|
|
|
|
|
if (kevent (g.watch_fd, nullptr, 0, &ev, 1, &timeout) > 0)
|
|
|
|
|
changed = ev.filter == EVFILT_VNODE && (ev.fflags & NOTE_WRITE);
|
|
|
|
|
#endif
|
|
|
|
|
if ((g.out_of_date = changed))
|
|
|
|
|
update ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1848,10 +1928,17 @@ int main (int argc, char *argv[]) {
|
|
|
|
|
// So that the neither us nor our children stop on tcsetpgrp()
|
|
|
|
|
signal (SIGTTOU, SIG_IGN);
|
|
|
|
|
|
|
|
|
|
if ((g.inotify_fd = inotify_init1 (IN_NONBLOCK)) < 0) {
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
if ((g.watch_fd = inotify_init1 (IN_NONBLOCK)) < 0) {
|
|
|
|
|
cerr << "cannot initialize inotify" << endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
if ((g.watch_fd = kqueue ()) < 0) {
|
|
|
|
|
cerr << "cannot initialize kqueue" << endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
locale::global (locale (""));
|
|
|
|
|
load_bindings ();
|
|
|
|
|
@@ -1888,7 +1975,7 @@ int main (int argc, char *argv[]) {
|
|
|
|
|
|
|
|
|
|
wint_t c;
|
|
|
|
|
while (!read_key (c) || handle (c)) {
|
|
|
|
|
inotify_check ();
|
|
|
|
|
watch_check ();
|
|
|
|
|
if (g.sort_flash_ttl && !--g.sort_flash_ttl)
|
|
|
|
|
update ();
|
|
|
|
|
if (g.message_ttl && !--g.message_ttl) {
|
|
|
|
|
|