#include "proto.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#ifndef DISABLE_BROWSER
static char **filelist = NULL;
static size_t filelist_len = 0;
static int width = 0;
static int longest = 0;
static size_t selected = 0;
static bool search_last_file = FALSE;
char *do_browser(char *path, DIR *dir)
{
char *retval = NULL;
int kbinput;
bool meta_key, func_key, old_const_update = ISSET(CONST_UPDATE);
bool abort = FALSE;
char *prev_dir = NULL;
char *ans = NULL;
size_t old_selected;
curs_set(0);
blank_statusbar();
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
currshortcut = browser_list;
#endif
bottombars(browser_list);
wnoutrefresh(bottomwin);
UNSET(CONST_UPDATE);
ans = mallocstrcpy(NULL, "");
change_browser_directory:
kbinput = ERR;
path = mallocstrassn(path, get_full_path(path));
assert(path != NULL && path[strlen(path) - 1] == '/');
browser_init(path, dir);
assert(filelist != NULL);
qsort(filelist, filelist_len, sizeof(char *), diralphasort);
if (prev_dir != NULL) {
browser_select_filename(prev_dir);
free(prev_dir);
prev_dir = NULL;
} else
selected = 0;
old_selected = (size_t)-1;
titlebar(path);
while (!abort) {
struct stat st;
int i;
size_t fileline = selected / width;
char *new_path;
if (kbinput == ERR || old_selected != selected)
browser_refresh();
old_selected = selected;
kbinput = get_kbinput(edit, &meta_key, &func_key);
parse_browser_input(&kbinput, &meta_key, &func_key);
switch (kbinput) {
#ifndef DISABLE_MOUSE
case KEY_MOUSE:
{
int mouse_x, mouse_y;
if (!get_mouseinput(&mouse_x, &mouse_y, TRUE)) {
if (wenclose(edit, mouse_y, mouse_x)) {
mouse_y -= 2 - no_more_space();
selected = (fileline / editwinrows) *
(editwinrows * width) + (mouse_y *
width) + (mouse_x / (longest + 2));
if (mouse_x > width * (longest + 2))
selected--;
if (selected > filelist_len - 1)
selected = filelist_len - 1;
if (old_selected == selected)
unget_kbinput(NANO_ENTER_KEY, FALSE,
FALSE);
}
}
}
break;
#endif
case NANO_REFRESH_KEY:
total_redraw();
break;
case NANO_HELP_KEY:
#ifndef DISABLE_HELP
do_browser_help();
curs_set(0);
#else
nano_disabled_msg();
#endif
break;
case NANO_WHEREIS_KEY:
curs_set(1);
do_filesearch();
curs_set(0);
break;
case NANO_WHEREIS_NEXT_KEY:
do_fileresearch();
break;
case NANO_PREVPAGE_KEY:
if (selected >= (editwinrows + fileline % editwinrows) *
width)
selected -= (editwinrows + fileline % editwinrows) *
width;
else
selected = 0;
break;
case NANO_NEXTPAGE_KEY:
selected += (editwinrows - fileline % editwinrows) *
width;
if (selected > filelist_len - 1)
selected = filelist_len - 1;
break;
case NANO_FIRSTFILE_METAKEY:
if (meta_key)
selected = 0;
break;
case NANO_LASTFILE_METAKEY:
if (meta_key)
selected = filelist_len - 1;
break;
case NANO_GOTODIR_KEY:
curs_set(1);
i = do_prompt(TRUE,
#ifndef DISABLE_TABCOMP
FALSE,
#endif
gotodir_list, ans,
#ifndef NANO_TINY
NULL,
#endif
browser_refresh, _("Go To Directory"));
curs_set(0);
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
currshortcut = browser_list;
#endif
bottombars(browser_list);
if (i < 0 || answer[0] == '\n') {
statusbar(_("Cancelled"));
ans = mallocstrcpy(ans, "");
break;
} else if (i != 0) {
unget_kbinput(NANO_GOTOLINE_KEY, FALSE, FALSE);
ans = mallocstrcpy(ans, answer);
break;
}
ans = mallocstrcpy(ans, "");
sunder(answer);
align(&answer);
new_path = real_dir_from_tilde(answer);
if (new_path[0] != '/') {
new_path = charealloc(new_path, strlen(path) +
strlen(answer) + 1);
sprintf(new_path, "%s%s", path, answer);
}
#ifndef DISABLE_OPERATINGDIR
if (check_operating_dir(new_path, FALSE)) {
statusbar(
_("Can't go outside of %s in restricted mode"),
operating_dir);
free(new_path);
break;
}
#endif
dir = opendir(new_path);
if (dir == NULL) {
statusbar(_("Error reading %s: %s"), answer,
strerror(errno));
beep();
free(new_path);
break;
}
free(path);
path = new_path;
goto change_browser_directory;
case NANO_PREVLINE_KEY:
if (selected >= width)
selected -= width;
break;
case NANO_BACK_KEY:
if (selected > 0)
selected--;
break;
case NANO_NEXTLINE_KEY:
if (selected + width <= filelist_len - 1)
selected += width;
break;
case NANO_FORWARD_KEY:
if (selected < filelist_len - 1)
selected++;
break;
case NANO_ENTER_KEY:
if (strcmp(filelist[selected], "/..") == 0) {
statusbar(_("Can't move up a directory"));
beep();
break;
}
#ifndef DISABLE_OPERATINGDIR
if (check_operating_dir(filelist[selected], FALSE)) {
statusbar(
_("Can't go outside of %s in restricted mode"),
operating_dir);
beep();
break;
}
#endif
if (stat(filelist[selected], &st) == -1) {
statusbar(_("Error reading %s: %s"),
filelist[selected], strerror(errno));
beep();
break;
}
if (!S_ISDIR(st.st_mode)) {
retval = mallocstrcpy(NULL, filelist[selected]);
abort = TRUE;
break;
} else if (strcmp(tail(filelist[selected]), "..") == 0)
prev_dir = mallocstrcpy(NULL,
striponedir(filelist[selected]));
dir = opendir(filelist[selected]);
if (dir == NULL) {
statusbar(_("Error reading %s: %s"),
filelist[selected], strerror(errno));
beep();
break;
}
path = mallocstrcpy(path, filelist[selected]);
goto change_browser_directory;
case NANO_EXIT_KEY:
abort = TRUE;
break;
}
}
titlebar(NULL);
edit_refresh();
curs_set(1);
if (old_const_update)
SET(CONST_UPDATE);
free(path);
free(ans);
free_chararray(filelist, filelist_len);
filelist = NULL;
filelist_len = 0;
return retval;
}
char *do_browse_from(const char *inpath)
{
struct stat st;
char *path;
DIR *dir = NULL;
assert(inpath != NULL);
path = real_dir_from_tilde(inpath);
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
path = mallocstrassn(path, striponedir(path));
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
free(path);
path = charalloc(PATH_MAX + 1);
path = getcwd(path, PATH_MAX + 1);
if (path != NULL)
align(&path);
}
}
#ifndef DISABLE_OPERATINGDIR
if (check_operating_dir(path, FALSE))
path = mallocstrcpy(path, operating_dir);
#endif
if (path != NULL)
dir = opendir(path);
if (dir == NULL) {
if (path != NULL)
free(path);
beep();
return NULL;
}
return do_browser(path, dir);
}
void browser_init(const char *path, DIR *dir)
{
const struct dirent *nextdir;
size_t i = 0, path_len = strlen(path);
int col = 0;
int line = 0;
int filesperline = 0;
assert(path != NULL && path[strlen(path) - 1] == '/' && dir != NULL);
longest = 0;
while ((nextdir = readdir(dir)) != NULL) {
size_t d_len;
if (strcmp(nextdir->d_name, ".") == 0)
continue;
d_len = strlenpt(nextdir->d_name);
if (d_len > longest)
longest = (d_len > COLS) ? COLS : d_len;
i++;
}
rewinddir(dir);
longest += 10;
if (filelist != NULL)
free_chararray(filelist, filelist_len);
filelist_len = i;
filelist = (char **)nmalloc(filelist_len * sizeof(char *));
i = 0;
while ((nextdir = readdir(dir)) != NULL && i < filelist_len) {
if (strcmp(nextdir->d_name, ".") == 0)
continue;
filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
sprintf(filelist[i], "%s%s", path, nextdir->d_name);
i++;
}
filelist_len = i;
closedir(dir);
if (longest < 15)
longest = 15;
if (longest > COLS)
longest = COLS;
width = 0;
for (i = 0; i < filelist_len && line < editwinrows; i++) {
col += longest;
filesperline++;
col += 2;
if (col > COLS - longest) {
line++;
col = 0;
if (width == 0)
width = filesperline;
}
}
if (width == 0)
width = COLS % longest;
}
void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key)
{
get_shortcut(browser_list, kbinput, meta_key, func_key);
if (!*meta_key) {
switch (*kbinput) {
case ' ':
*kbinput = NANO_NEXTPAGE_KEY;
break;
case '-':
*kbinput = NANO_PREVPAGE_KEY;
break;
case '?':
*kbinput = NANO_HELP_KEY;
break;
case NANO_CANCEL_KEY:
case 'E':
case 'e':
*kbinput = NANO_EXIT_KEY;
break;
case 'G':
case 'g':
*kbinput = NANO_GOTODIR_KEY;
break;
case 'S':
case 's':
*kbinput = NANO_ENTER_KEY;
break;
case 'W':
case 'w':
*kbinput = NANO_WHEREIS_KEY;
break;
}
}
}
void browser_refresh(void)
{
static int uimax_digits = -1;
size_t i;
int col = 0;
int line = 0;
char *foo;
if (uimax_digits == -1)
uimax_digits = digits(UINT_MAX);
blank_edit();
wmove(edit, 0, 0);
i = width * editwinrows * ((selected / width) / editwinrows);
for (; i < filelist_len && line < editwinrows; i++) {
struct stat st;
const char *filetail = tail(filelist[i]);
size_t filetaillen = strlenpt(filetail);
size_t foolen;
int foomaxlen = 7;
bool dots = (COLS >= 15 && filetaillen >= longest -
foomaxlen - 1);
char *disp = display_string(filetail, dots ? filetaillen -
longest + foomaxlen + 4 : 0, longest, FALSE);
if (i == selected)
wattron(edit, reverse_attr);
blank_line(edit, line, col, longest);
if (dots)
mvwaddstr(edit, line, col, "...");
mvwaddstr(edit, line, dots ? col + 3 : col, disp);
free(disp);
col += longest;
if (lstat(filelist[i], &st) == -1 || S_ISLNK(st.st_mode)) {
if (stat(filelist[i], &st) == -1 || !S_ISDIR(st.st_mode))
foo = mallocstrcpy(NULL, "--");
else
foo = mallocstrcpy(NULL, _("(dir)"));
} else if (S_ISDIR(st.st_mode)) {
if (strcmp(filetail, "..") == 0) {
foo = mallocstrcpy(NULL, _("(parent dir)"));
foomaxlen = 12;
} else
foo = mallocstrcpy(NULL, _("(dir)"));
} else {
unsigned long result = st.st_size;
char modifier;
foo = charalloc(uimax_digits + 4);
if (st.st_size < (1 << 10))
modifier = ' ';
else if (st.st_size < (1 << 20)) {
result >>= 10;
modifier = 'K';
} else if (st.st_size < (1 << 30)) {
result >>= 20;
modifier = 'M';
} else {
result >>= 30;
modifier = 'G';
}
sprintf(foo, "%4lu %cB", result, modifier);
}
foolen = strlenpt(foo);
if (foolen > foomaxlen) {
null_at(&foo, actual_x(foo, foomaxlen));
foolen = foomaxlen;
}
mvwaddstr(edit, line, col - foolen, foo);
if (i == selected)
wattroff(edit, reverse_attr);
free(foo);
col += 2;
if (col > COLS - longest) {
line++;
col = 0;
}
wmove(edit, line, col);
}
wnoutrefresh(edit);
}
bool browser_select_filename(const char *needle)
{
size_t currselected;
bool found = FALSE;
for (currselected = 0; currselected < filelist_len;
currselected++) {
if (strcmp(filelist[currselected], needle) == 0) {
found = TRUE;
break;
}
}
if (found)
selected = currselected;
return found;
}
int filesearch_init(void)
{
int i = 0;
char *buf;
static char *backupstring = NULL;
if (backupstring == NULL)
backupstring = mallocstrcpy(NULL, "");
search_init_globals();
if (last_search[0] != '\0') {
char *disp = display_string(last_search, 0, COLS / 3, FALSE);
buf = charalloc(strlen(disp) + 7);
sprintf(buf, " [%s%s]", disp,
(strlenpt(last_search) > COLS / 3) ? "..." : "");
free(disp);
} else
buf = mallocstrcpy(NULL, "");
i = do_prompt(FALSE,
#ifndef DISABLE_TABCOMP
TRUE,
#endif
whereis_file_list, backupstring,
#ifndef NANO_TINY
&search_history,
#endif
browser_refresh, "%s%s%s%s%s%s", _("Search"),
#ifndef NANO_TINY
ISSET(CASE_SENSITIVE) ? _(" [Case Sensitive]") :
#endif
"",
#ifdef HAVE_REGEX_H
ISSET(USE_REGEXP) ? _(" [Regexp]") :
#endif
"",
#ifndef NANO_TINY
ISSET(BACKWARDS_SEARCH) ? _(" [Backwards]") :
#endif
"", "", buf);
free(buf);
free(backupstring);
backupstring = NULL;
if (i == -1 || (i < 0 && last_search[0] == '\0') ||
(i == 0 && answer[0] == '\0')) {
statusbar(_("Cancelled"));
return -1;
} else {
switch (i) {
case -2:
case 0:
#ifdef HAVE_REGEX_H
if (ISSET(USE_REGEXP) && !regexp_init((i == -2) ?
last_search : answer))
return -1;
#endif
break;
#ifndef NANO_TINY
case TOGGLE_CASE_KEY:
TOGGLE(CASE_SENSITIVE);
backupstring = mallocstrcpy(backupstring, answer);
return 1;
case TOGGLE_BACKWARDS_KEY:
TOGGLE(BACKWARDS_SEARCH);
backupstring = mallocstrcpy(backupstring, answer);
return 1;
#endif
#ifdef HAVE_REGEX_H
case NANO_REGEXP_KEY:
TOGGLE(USE_REGEXP);
backupstring = mallocstrcpy(backupstring, answer);
return 1;
#endif
default:
return -1;
}
}
return 0;
}
bool findnextfile(bool no_sameline, size_t begin, const char *needle)
{
size_t currselected = selected;
const char *filetail = tail(filelist[currselected]);
const char *rev_start = filetail, *found = NULL;
#ifndef NANO_TINY
if (ISSET(BACKWARDS_SEARCH))
rev_start += strlen(rev_start);
#endif
while (TRUE) {
found = strstrwrapper(filetail, needle, rev_start);
if (found != NULL && (!no_sameline || currselected != begin))
break;
if (search_last_file) {
not_found_msg(needle);
return FALSE;
}
#ifndef NANO_TINY
if (ISSET(BACKWARDS_SEARCH)) {
if (currselected > 0)
currselected--;
else {
currselected = filelist_len - 1;
statusbar(_("Search Wrapped"));
}
} else {
#endif
if (currselected < filelist_len - 1)
currselected++;
else {
currselected = 0;
statusbar(_("Search Wrapped"));
}
#ifndef NANO_TINY
}
#endif
if (currselected == begin)
search_last_file = TRUE;
filetail = tail(filelist[currselected]);
rev_start = filetail;
#ifndef NANO_TINY
if (ISSET(BACKWARDS_SEARCH))
rev_start += strlen(rev_start);
#endif
}
selected = currselected;
return TRUE;
}
void findnextfile_wrap_reset(void)
{
search_last_file = FALSE;
}
void filesearch_abort(void)
{
currshortcut = browser_list;
bottombars(browser_list);
#ifdef HAVE_REGEX_H
regexp_cleanup();
#endif
}
void do_filesearch(void)
{
size_t begin = selected;
int i;
bool didfind;
i = filesearch_init();
if (i == -1)
filesearch_abort();
#if !defined(NANO_TINY) || defined(HAVE_REGEX_H)
else if (i == 1)
do_filesearch();
#endif
if (i != 0)
return;
if (answer[0] == '\0')
answer = mallocstrcpy(answer, last_search);
else
last_search = mallocstrcpy(last_search, answer);
#ifndef NANO_TINY
if (answer[0] != '\0')
update_history(&search_history, answer);
#endif
findnextfile_wrap_reset();
didfind = findnextfile(FALSE, begin, answer);
if (selected == begin && didfind) {
didfind = findnextfile(TRUE, begin, answer);
if (selected == begin && !didfind)
statusbar(_("This is the only occurrence"));
}
filesearch_abort();
}
void do_fileresearch(void)
{
size_t begin = selected;
bool didfind;
search_init_globals();
if (last_search[0] != '\0') {
#ifdef HAVE_REGEX_H
if (ISSET(USE_REGEXP) && !regexp_init(last_search))
return;
#endif
findnextfile_wrap_reset();
didfind = findnextfile(FALSE, begin, answer);
if (selected == begin && didfind) {
didfind = findnextfile(TRUE, begin, answer);
if (selected == begin && !didfind)
statusbar(_("This is the only occurrence"));
}
} else
statusbar(_("No current search pattern"));
filesearch_abort();
}
void do_first_file(void)
{
selected = 0;
}
void do_last_file(void)
{
selected = filelist_len - 1;
}
char *striponedir(const char *path)
{
char *retval, *tmp;
assert(path != NULL);
retval = mallocstrcpy(NULL, path);
tmp = strrchr(retval, '/');
if (tmp != NULL)
null_at(&retval, tmp - retval);
return retval;
}
#endif