add DB's overhaul of the file browser code to increase efficiency and
add multibyte character support, plus a few tweaks of mine git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2320 35c25a1d-7b9e-4130-9fde-d3aeb78583b8master
parent
b680326c37
commit
7d367710ae
16
ChangeLog
16
ChangeLog
|
@ -149,13 +149,15 @@ CVS code -
|
||||||
get the value of totsize in a few more places. Changes to
|
get the value of totsize in a few more places. Changes to
|
||||||
read_line(), read_file(), do_delete(), do_input(),
|
read_line(), read_file(), do_delete(), do_input(),
|
||||||
get_totals(), and do_cursorpos(). (DLR)
|
get_totals(), and do_cursorpos(). (DLR)
|
||||||
- Overhaul the tab completion code and a few related functions
|
- Overhaul the tab completion code, the file browser code, and
|
||||||
to increase efficiency and support multibyte characters. New
|
related functions to increase efficiency and support multibyte
|
||||||
functions strrchrn() and is_dir(); changes to get_full_path(),
|
characters. New functions strrchrn() and is_dir(); changes to
|
||||||
check_writable_directory(), safe_tempnam(), diralphasort(),
|
get_full_path(), check_writable_directory(), safe_tempnam(),
|
||||||
username_tab_completion(), cwd_tab_completion(), input_tab(),
|
diralphasort(), username_tab_completion(),
|
||||||
tail(), and striponedir(); removal of append_slash_if_dir()
|
cwd_tab_completion(), input_tab(), tail(), striponedir(),
|
||||||
and check_wildcard_match(). (David Benbennick) DLR: Move the
|
browser_init(), do_browser(), and do_browse_from(); removal of
|
||||||
|
append_slash_if_dir(), readable_dir(), and
|
||||||
|
check_wildcard_match(). (David Benbennick) DLR: Move the
|
||||||
routine to get the current user's home directory into the new
|
routine to get the current user's home directory into the new
|
||||||
function get_homedir(), and use it where necessary. Also add
|
function get_homedir(), and use it where necessary. Also add
|
||||||
a few miscellaneous tweaks.
|
a few miscellaneous tweaks.
|
||||||
|
|
466
src/files.c
466
src/files.c
|
@ -2305,167 +2305,183 @@ void free_charptrarray(char **array, size_t len)
|
||||||
free(array);
|
free(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Strip one dir from the end of a string. */
|
/* Strip one directory from the end of path. */
|
||||||
void striponedir(char *foo)
|
void striponedir(char *path)
|
||||||
{
|
{
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
assert(foo != NULL);
|
assert(path != NULL);
|
||||||
|
|
||||||
tmp = strrchr(foo, '/');
|
tmp = strrchr(path, '/');
|
||||||
if (tmp != NULL)
|
if (tmp != NULL)
|
||||||
*tmp = '\0';
|
*tmp = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
int readable_dir(const char *path)
|
/* Return a list of files contained in the directory path. *longest is
|
||||||
|
* the maximum display length of a file, up to COLS - 1 (but at least
|
||||||
|
* 7). *numents is the number of files. We assume path exists and is a
|
||||||
|
* directory. If neither is true, we return NULL. */
|
||||||
|
char **browser_init(const char *path, int *longest, size_t *numents, DIR
|
||||||
|
*dir)
|
||||||
{
|
{
|
||||||
DIR *dir = opendir(path);
|
const struct dirent *next;
|
||||||
|
|
||||||
/* If dir is NULL, don't do closedir(), since that changes errno. */
|
|
||||||
if (dir != NULL)
|
|
||||||
closedir(dir);
|
|
||||||
return (dir != NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initialize the browser code, including the list of files in *path */
|
|
||||||
char **browser_init(const char *path, int *longest, int *numents)
|
|
||||||
{
|
|
||||||
DIR *dir;
|
|
||||||
struct dirent *next;
|
|
||||||
char **filelist;
|
char **filelist;
|
||||||
int i = 0;
|
size_t i, path_len;
|
||||||
size_t path_len;
|
|
||||||
|
|
||||||
dir = opendir(path);
|
assert(dir != NULL);
|
||||||
if (dir == NULL)
|
|
||||||
return NULL;
|
*longest = 0;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
|
||||||
*numents = 0;
|
|
||||||
while ((next = readdir(dir)) != NULL) {
|
while ((next = readdir(dir)) != NULL) {
|
||||||
|
size_t dlen;
|
||||||
|
|
||||||
|
/* Don't show the . entry. */
|
||||||
if (strcmp(next->d_name, ".") == 0)
|
if (strcmp(next->d_name, ".") == 0)
|
||||||
continue;
|
continue;
|
||||||
(*numents)++;
|
i++;
|
||||||
if (strlen(next->d_name) > *longest)
|
|
||||||
*longest = strlen(next->d_name);
|
dlen = strlenpt(next->d_name);
|
||||||
|
if (dlen > *longest)
|
||||||
|
*longest = dlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*numents = i;
|
||||||
rewinddir(dir);
|
rewinddir(dir);
|
||||||
*longest += 10;
|
*longest += 10;
|
||||||
|
|
||||||
filelist = (char **)nmalloc(*numents * sizeof (char *));
|
filelist = (char **)nmalloc(*numents * sizeof(char *));
|
||||||
|
|
||||||
if (strcmp(path, "/") == 0)
|
|
||||||
path = "";
|
|
||||||
path_len = strlen(path);
|
path_len = strlen(path);
|
||||||
|
|
||||||
while ((next = readdir(dir)) != NULL) {
|
i = 0;
|
||||||
|
|
||||||
|
while ((next = readdir(dir)) != NULL && i < *numents) {
|
||||||
|
/* Don't show the "." entry. */
|
||||||
if (strcmp(next->d_name, ".") == 0)
|
if (strcmp(next->d_name, ".") == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
filelist[i] = charalloc(strlen(next->d_name) + path_len + 2);
|
filelist[i] = charalloc(path_len + strlen(next->d_name) + 1);
|
||||||
sprintf(filelist[i], "%s/%s", path, next->d_name);
|
sprintf(filelist[i], "%s%s", path, next->d_name);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Maybe the number of files in the directory changed between the
|
||||||
|
* first time we scanned and the second. i is the actual length of
|
||||||
|
* filelist, so record it. */
|
||||||
|
*numents = i;
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
if (*longest > COLS - 1)
|
if (*longest > COLS - 1)
|
||||||
*longest = COLS - 1;
|
*longest = COLS - 1;
|
||||||
|
if (*longest < 7)
|
||||||
|
*longest = 7;
|
||||||
|
|
||||||
return filelist;
|
return filelist;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Our browser function. inpath is the path to start browsing from */
|
/* Our browser function. path is the path to start browsing from.
|
||||||
char *do_browser(const char *inpath)
|
* Assume path has already been tilde-expanded. */
|
||||||
|
char *do_browser(char *path, DIR *dir)
|
||||||
{
|
{
|
||||||
struct stat st;
|
int kbinput, longest, selected, width;
|
||||||
char *foo, *retval = NULL;
|
bool meta_key, func_key, old_constupdate = ISSET(CONSTUPDATE);
|
||||||
static char *path = NULL;
|
size_t numents;
|
||||||
int numents = 0, i = 0, j = 0, longest = 0, abort = 0, col = 0;
|
char **filelist, *retval = NULL;
|
||||||
int selected = 0, editline = 0, width = 0, filecols = 0, lineno = 0;
|
|
||||||
int kbinput = ERR;
|
curs_set(0);
|
||||||
bool meta_key, func_key;
|
blank_statusbar();
|
||||||
char **filelist = (char **)NULL;
|
bottombars(browser_list);
|
||||||
#ifndef DISABLE_MOUSE
|
wrefresh(bottomwin);
|
||||||
MEVENT mevent;
|
|
||||||
|
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
|
||||||
|
/* Set currshortcut so the user can click in the shortcut area, and
|
||||||
|
* so the browser help screen will come up. */
|
||||||
|
currshortcut = browser_list;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
assert(inpath != NULL);
|
UNSET(CONSTUPDATE);
|
||||||
|
|
||||||
/* If path isn't the same as inpath, we are being passed a new
|
change_browser_directory:
|
||||||
dir as an arg. We free it here so it will be copied from
|
/* We go here after the user selects a new directory. */
|
||||||
inpath below */
|
|
||||||
if (path != NULL && strcmp(path, inpath) != 0) {
|
|
||||||
free(path);
|
|
||||||
path = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if path doesn't exist, make it so */
|
kbinput = ERR;
|
||||||
if (path == NULL)
|
selected = 0;
|
||||||
path = mallocstrcpy(NULL, inpath);
|
width = 0;
|
||||||
|
|
||||||
filelist = browser_init(path, &longest, &numents);
|
path = mallocstrassn(path, get_full_path(path));
|
||||||
foo = charalloc(longest + 8);
|
|
||||||
|
/* Assume that path exists and ends with a slash. */
|
||||||
|
assert(path != NULL && path[strlen(path) - 1] == '/');
|
||||||
|
|
||||||
|
/* Get the list of files. */
|
||||||
|
filelist = browser_init(path, &longest, &numents, dir);
|
||||||
|
|
||||||
|
assert(filelist != NULL);
|
||||||
|
|
||||||
/* Sort the list. */
|
/* Sort the list. */
|
||||||
qsort(filelist, numents, sizeof(char *), diralphasort);
|
qsort(filelist, numents, sizeof(char *), diralphasort);
|
||||||
|
|
||||||
titlebar(path);
|
titlebar(path);
|
||||||
bottombars(browser_list);
|
|
||||||
curs_set(0);
|
|
||||||
wmove(edit, 0, 0);
|
|
||||||
i = 0;
|
|
||||||
width = 0;
|
|
||||||
filecols = 0;
|
|
||||||
|
|
||||||
/* Loop invariant: Microsoft sucks. */
|
/* Loop invariant: Microsoft sucks. */
|
||||||
do {
|
do {
|
||||||
|
bool abort = FALSE;
|
||||||
|
int j, col = 0, editline = 0, lineno;
|
||||||
|
int filecols = 0;
|
||||||
|
/* Used only if width == 0, to calculate the number of files
|
||||||
|
* per row below. */
|
||||||
|
struct stat st;
|
||||||
char *new_path;
|
char *new_path;
|
||||||
/* Used by the Go To Directory prompt. */
|
/* Used by the Go To Directory prompt. */
|
||||||
|
#ifndef DISABLE_MOUSE
|
||||||
|
MEVENT mevent;
|
||||||
|
#endif
|
||||||
|
|
||||||
check_statusblank();
|
check_statusblank();
|
||||||
|
|
||||||
currshortcut = browser_list;
|
/* Compute the line number we're on now, so that we don't divide
|
||||||
|
* by zero later. */
|
||||||
editline = 0;
|
|
||||||
col = 0;
|
|
||||||
|
|
||||||
/* Compute line number we're on now, so we don't divide by zero later */
|
|
||||||
lineno = selected;
|
lineno = selected;
|
||||||
if (width != 0)
|
if (width != 0)
|
||||||
lineno /= width;
|
lineno /= width;
|
||||||
|
|
||||||
switch (kbinput) {
|
switch (kbinput) {
|
||||||
|
|
||||||
#ifndef DISABLE_MOUSE
|
#ifndef DISABLE_MOUSE
|
||||||
case KEY_MOUSE:
|
case KEY_MOUSE:
|
||||||
if (getmouse(&mevent) == ERR)
|
if (getmouse(&mevent) == ERR)
|
||||||
return retval;
|
break;
|
||||||
|
|
||||||
/* If they clicked in the edit window, they probably clicked
|
/* If we clicked in the edit window, we probably clicked
|
||||||
on a file */
|
* on a file. */
|
||||||
if (wenclose(edit, mevent.y, mevent.x)) {
|
if (wenclose(edit, mevent.y, mevent.x)) {
|
||||||
int selectedbackup = selected;
|
int selectedbackup = selected;
|
||||||
|
|
||||||
mevent.y -= 2;
|
mevent.y -= 2;
|
||||||
|
|
||||||
/* Longest is the width of each column. There are two
|
/* longest is the width of each column. There are
|
||||||
* spaces between each column. */
|
* two spaces between each column. */
|
||||||
selected = (lineno / editwinrows) * editwinrows * width
|
selected = (lineno / editwinrows) * editwinrows *
|
||||||
+ mevent.y * width + mevent.x / (longest + 2);
|
width + mevent.y * width + mevent.x /
|
||||||
|
(longest + 2);
|
||||||
|
|
||||||
/* If they clicked beyond the end of a row, select the
|
/* If they clicked beyond the end of a row, select
|
||||||
* end of that row. */
|
* the end of that row. */
|
||||||
if (mevent.x > width * (longest + 2))
|
if (mevent.x > width * (longest + 2))
|
||||||
selected--;
|
selected--;
|
||||||
|
|
||||||
/* If we're off the screen, reset to the last item.
|
/* If we're off the screen, reset to the last item.
|
||||||
If we clicked where we did last time, select this name! */
|
* If we clicked the same place as last time, select
|
||||||
|
* this name! */
|
||||||
if (selected > numents - 1)
|
if (selected > numents - 1)
|
||||||
selected = numents - 1;
|
selected = numents - 1;
|
||||||
else if (selectedbackup == selected)
|
else if (selectedbackup == selected)
|
||||||
/* Put back the 'select' key */
|
/* Put back the 'select' key. */
|
||||||
unget_kbinput('s', FALSE, FALSE);
|
unget_kbinput('s', FALSE, FALSE);
|
||||||
} else {
|
} else {
|
||||||
/* Must be clicking a shortcut */
|
/* We must have clicked a shortcut. Put back the
|
||||||
|
* equivalent shortcut key. */
|
||||||
int mouse_x, mouse_y;
|
int mouse_x, mouse_y;
|
||||||
get_mouseinput(&mouse_x, &mouse_y, TRUE);
|
get_mouseinput(&mouse_x, &mouse_y, TRUE);
|
||||||
}
|
}
|
||||||
|
@ -2473,7 +2489,7 @@ char *do_browser(const char *inpath)
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
case NANO_PREVLINE_KEY:
|
case NANO_PREVLINE_KEY:
|
||||||
if (selected - width >= 0)
|
if (selected >= width)
|
||||||
selected -= width;
|
selected -= width;
|
||||||
break;
|
break;
|
||||||
case NANO_BACK_KEY:
|
case NANO_BACK_KEY:
|
||||||
|
@ -2490,22 +2506,25 @@ char *do_browser(const char *inpath)
|
||||||
break;
|
break;
|
||||||
case NANO_PREVPAGE_KEY:
|
case NANO_PREVPAGE_KEY:
|
||||||
case NANO_PREVPAGE_FKEY:
|
case NANO_PREVPAGE_FKEY:
|
||||||
case '-': /* Pico compatibility */
|
case '-': /* Pico compatibility. */
|
||||||
if (selected >= (editwinrows + lineno % editwinrows) * width)
|
if (selected >= (editwinrows + lineno % editwinrows) *
|
||||||
selected -= (editwinrows + lineno % editwinrows) * width;
|
width)
|
||||||
|
selected -= (editwinrows + lineno % editwinrows) *
|
||||||
|
width;
|
||||||
else
|
else
|
||||||
selected = 0;
|
selected = 0;
|
||||||
break;
|
break;
|
||||||
case NANO_NEXTPAGE_KEY:
|
case NANO_NEXTPAGE_KEY:
|
||||||
case NANO_NEXTPAGE_FKEY:
|
case NANO_NEXTPAGE_FKEY:
|
||||||
case ' ': /* Pico compatibility */
|
case ' ': /* Pico compatibility. */
|
||||||
selected += (editwinrows - lineno % editwinrows) * width;
|
selected += (editwinrows - lineno % editwinrows) *
|
||||||
|
width;
|
||||||
if (selected >= numents)
|
if (selected >= numents)
|
||||||
selected = numents - 1;
|
selected = numents - 1;
|
||||||
break;
|
break;
|
||||||
case NANO_HELP_KEY:
|
case NANO_HELP_KEY:
|
||||||
case NANO_HELP_FKEY:
|
case NANO_HELP_FKEY:
|
||||||
case '?': /* Pico compatibility */
|
case '?': /* Pico compatibility. */
|
||||||
#ifndef DISABLE_HELP
|
#ifndef DISABLE_HELP
|
||||||
do_help();
|
do_help();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
|
@ -2514,11 +2533,10 @@ char *do_browser(const char *inpath)
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case NANO_ENTER_KEY:
|
case NANO_ENTER_KEY:
|
||||||
case 'S': /* Pico compatibility */
|
case 'S': /* Pico compatibility. */
|
||||||
case 's':
|
case 's':
|
||||||
/* You can't cd up from / */
|
/* You can't move up from "/". */
|
||||||
if (strcmp(filelist[selected], "/..") == 0 &&
|
if (strcmp(filelist[selected], "/..") == 0) {
|
||||||
strcmp(path, "/") == 0) {
|
|
||||||
statusbar(_("Can't move up a directory"));
|
statusbar(_("Can't move up a directory"));
|
||||||
beep();
|
beep();
|
||||||
break;
|
break;
|
||||||
|
@ -2526,10 +2544,11 @@ char *do_browser(const char *inpath)
|
||||||
|
|
||||||
#ifndef DISABLE_OPERATINGDIR
|
#ifndef DISABLE_OPERATINGDIR
|
||||||
/* Note: the selected file can be outside the operating
|
/* Note: the selected file can be outside the operating
|
||||||
* directory if it is .. or if it is a symlink to
|
* directory if it's ".." or if it's a symlink to a
|
||||||
* directory outside the operating directory. */
|
* directory outside the operating directory. */
|
||||||
if (check_operating_dir(filelist[selected], FALSE)) {
|
if (check_operating_dir(filelist[selected], FALSE)) {
|
||||||
statusbar(_("Can't go outside of %s in restricted mode"),
|
statusbar(
|
||||||
|
_("Can't go outside of %s in restricted mode"),
|
||||||
operating_dir);
|
operating_dir);
|
||||||
beep();
|
beep();
|
||||||
break;
|
break;
|
||||||
|
@ -2537,59 +2556,48 @@ char *do_browser(const char *inpath)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (stat(filelist[selected], &st) == -1) {
|
if (stat(filelist[selected], &st) == -1) {
|
||||||
statusbar(_("Can't open \"%s\": %s"), filelist[selected],
|
statusbar(_("Error reading %s: %s"),
|
||||||
strerror(errno));
|
filelist[selected], strerror(errno));
|
||||||
beep();
|
beep();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!S_ISDIR(st.st_mode)) {
|
if (!S_ISDIR(st.st_mode)) {
|
||||||
retval = mallocstrcpy(retval, filelist[selected]);
|
retval = mallocstrcpy(retval, filelist[selected]);
|
||||||
abort = 1;
|
abort = TRUE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_path = mallocstrcpy(NULL, filelist[selected]);
|
dir = opendir(filelist[selected]);
|
||||||
|
if (dir == NULL) {
|
||||||
if (strcmp("..", tail(new_path)) == 0) {
|
/* We can't open this dir for some reason.
|
||||||
/* They want to go up a level, so strip off .. and the
|
* Complain. */
|
||||||
current dir */
|
statusbar(_("Error reading %s: %s"),
|
||||||
striponedir(new_path);
|
filelist[selected], strerror(errno));
|
||||||
/* SPK for '.' path, get the current path via getcwd */
|
|
||||||
if (strcmp(new_path, ".") == 0) {
|
|
||||||
free(new_path);
|
|
||||||
new_path = getcwd(NULL, PATH_MAX + 1);
|
|
||||||
}
|
|
||||||
striponedir(new_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!readable_dir(new_path)) {
|
|
||||||
/* We can't open this dir for some reason. Complain */
|
|
||||||
statusbar(_("Can't open \"%s\": %s"), new_path,
|
|
||||||
strerror(errno));
|
|
||||||
free(new_path);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path = mallocstrcpy(path, filelist[selected]);
|
||||||
|
|
||||||
|
/* Start over again with the new path value. */
|
||||||
free_charptrarray(filelist, numents);
|
free_charptrarray(filelist, numents);
|
||||||
free(foo);
|
goto change_browser_directory;
|
||||||
free(path);
|
|
||||||
path = new_path;
|
|
||||||
return do_browser(path);
|
|
||||||
|
|
||||||
/* Go to a specific directory */
|
/* Go to a specific directory. */
|
||||||
case NANO_GOTOLINE_KEY:
|
case NANO_GOTOLINE_KEY:
|
||||||
case NANO_GOTOLINE_FKEY:
|
case NANO_GOTOLINE_FKEY:
|
||||||
case 'G': /* Pico compatibility */
|
case 'G': /* Pico compatibility. */
|
||||||
case 'g':
|
case 'g':
|
||||||
curs_set(1);
|
curs_set(1);
|
||||||
|
|
||||||
j = statusq(FALSE, gotodir_list, "",
|
j = statusq(FALSE, gotodir_list, "",
|
||||||
#ifndef NANO_SMALL
|
#ifndef NANO_SMALL
|
||||||
NULL,
|
NULL,
|
||||||
#endif
|
#endif
|
||||||
_("Go To Directory"));
|
_("Go To Directory"));
|
||||||
bottombars(browser_list);
|
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
|
bottombars(browser_list);
|
||||||
|
|
||||||
if (j < 0) {
|
if (j < 0) {
|
||||||
statusbar(_("Cancelled"));
|
statusbar(_("Cancelled"));
|
||||||
|
@ -2599,166 +2607,202 @@ char *do_browser(const char *inpath)
|
||||||
new_path = real_dir_from_tilde(answer);
|
new_path = real_dir_from_tilde(answer);
|
||||||
|
|
||||||
if (new_path[0] != '/') {
|
if (new_path[0] != '/') {
|
||||||
new_path = charealloc(new_path, strlen(path) + strlen(answer) + 2);
|
new_path = charealloc(new_path, strlen(path) +
|
||||||
sprintf(new_path, "%s/%s", path, answer);
|
strlen(answer) + 1);
|
||||||
|
sprintf(new_path, "%s%s", path, answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef DISABLE_OPERATINGDIR
|
#ifndef DISABLE_OPERATINGDIR
|
||||||
if (check_operating_dir(new_path, FALSE)) {
|
if (check_operating_dir(new_path, FALSE)) {
|
||||||
statusbar(_("Can't go outside of %s in restricted mode"), operating_dir);
|
statusbar(
|
||||||
|
_("Can't go outside of %s in restricted mode"),
|
||||||
|
operating_dir);
|
||||||
free(new_path);
|
free(new_path);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!readable_dir(new_path)) {
|
dir = opendir(new_path);
|
||||||
/* We can't open this dir for some reason. Complain */
|
if (dir == NULL) {
|
||||||
statusbar(_("Can't open \"%s\": %s"), answer, strerror(errno));
|
/* We can't open this dir for some reason.
|
||||||
|
* Complain. */
|
||||||
|
statusbar(_("Error reading %s: %s"), answer,
|
||||||
|
strerror(errno));
|
||||||
free(new_path);
|
free(new_path);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start over again with the new path value */
|
/* Start over again with the new path value. */
|
||||||
free_charptrarray(filelist, numents);
|
|
||||||
free(foo);
|
|
||||||
free(path);
|
free(path);
|
||||||
path = new_path;
|
path = new_path;
|
||||||
return do_browser(path);
|
free_charptrarray(filelist, numents);
|
||||||
|
goto change_browser_directory;
|
||||||
|
|
||||||
/* Stuff we want to abort the browser */
|
/* Abort the browser. */
|
||||||
case NANO_CANCEL_KEY:
|
case NANO_CANCEL_KEY:
|
||||||
case NANO_EXIT_KEY:
|
case NANO_EXIT_KEY:
|
||||||
case NANO_EXIT_FKEY:
|
case NANO_EXIT_FKEY:
|
||||||
case 'E': /* Pico compatibility */
|
case 'E': /* Pico compatibility. */
|
||||||
case 'e':
|
case 'e':
|
||||||
abort = 1;
|
abort = TRUE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (abort)
|
if (abort)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
blank_edit();
|
blank_edit();
|
||||||
|
|
||||||
if (width != 0)
|
if (width != 0)
|
||||||
i = width * editwinrows * ((selected / width) / editwinrows);
|
j = width * editwinrows *
|
||||||
|
((selected / width) / editwinrows);
|
||||||
else
|
else
|
||||||
i = 0;
|
j = 0;
|
||||||
|
|
||||||
wmove(edit, 0, 0);
|
wmove(edit, 0, 0);
|
||||||
for (j = i; j < numents && editline <= editwinrows - 1; j++) {
|
|
||||||
filecols++;
|
|
||||||
|
|
||||||
strncpy(foo, tail(filelist[j]), strlen(tail(filelist[j])) + 1);
|
{
|
||||||
while (strlen(foo) < longest)
|
int foo_len = mb_cur_max() * 7;
|
||||||
strcat(foo, " ");
|
char *foo = charalloc(foo_len + 1);
|
||||||
col += strlen(foo);
|
|
||||||
|
|
||||||
/* Put file info in the string also */
|
for (; j < numents && editline <= editwinrows - 1; j++) {
|
||||||
/* We use lstat here to detect links; then, if we find a
|
char *disp = display_string(tail(filelist[j]), 0,
|
||||||
symlink, we examine it via stat() to see if it is a
|
longest, FALSE);
|
||||||
directory or just a file symlink */
|
|
||||||
lstat(filelist[j], &st);
|
|
||||||
if (S_ISDIR(st.st_mode))
|
|
||||||
strcpy(foo + longest - 5, "(dir)");
|
|
||||||
else {
|
|
||||||
if (S_ISLNK(st.st_mode)) {
|
|
||||||
/* Aha! It's a symlink! Now, is it a dir? If so,
|
|
||||||
mark it as such */
|
|
||||||
stat(filelist[j], &st);
|
|
||||||
if (S_ISDIR(st.st_mode))
|
|
||||||
strcpy(foo + longest - 5, "(dir)");
|
|
||||||
else
|
|
||||||
strcpy(foo + longest - 2, "--");
|
|
||||||
} else if (st.st_size < (1 << 10)) /* less than 1 K */
|
|
||||||
sprintf(foo + longest - 7, "%4d B",
|
|
||||||
(int) st.st_size);
|
|
||||||
else if (st.st_size >= (1 << 30)) /* at least 1 gig */
|
|
||||||
sprintf(foo + longest - 7, "%4d GB",
|
|
||||||
(int) st.st_size >> 30);
|
|
||||||
else if (st.st_size >= (1 << 20)) /* at least 1 meg */
|
|
||||||
sprintf(foo + longest - 7, "%4d MB",
|
|
||||||
(int) st.st_size >> 20);
|
|
||||||
else /* It's more than 1 k and less than a meg */
|
|
||||||
sprintf(foo + longest - 7, "%4d KB",
|
|
||||||
(int) st.st_size >> 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Highlight the currently selected file/dir */
|
/* Highlight the currently selected file/dir. */
|
||||||
if (j == selected)
|
if (j == selected)
|
||||||
wattron(edit, A_REVERSE);
|
wattron(edit, A_REVERSE);
|
||||||
waddstr(edit, foo);
|
|
||||||
|
mvwaddnstr(edit, editline, col, hblank, longest);
|
||||||
|
mvwaddstr(edit, editline, col, disp);
|
||||||
|
free(disp);
|
||||||
|
|
||||||
|
col += longest;
|
||||||
|
filecols++;
|
||||||
|
|
||||||
|
/* Show file info also. We don't want to report file
|
||||||
|
* sizes for links, so we use lstat(). Also, stat() and
|
||||||
|
* lstat() return an error if, for example, the file is
|
||||||
|
* deleted while the file browser is open. In that
|
||||||
|
* case, we report "--" as the file info. */
|
||||||
|
if (lstat(filelist[j], &st) == -1 ||
|
||||||
|
S_ISLNK(st.st_mode)) {
|
||||||
|
/* Aha! It's a symlink! Now, is it a dir? If so,
|
||||||
|
* mark it as such. */
|
||||||
|
if (stat(filelist[j], &st) == 0 &&
|
||||||
|
S_ISDIR(st.st_mode)) {
|
||||||
|
strncpy(foo, _("(dir)"), foo_len);
|
||||||
|
foo[foo_len] = '\0';
|
||||||
|
} else
|
||||||
|
strcpy(foo, "--");
|
||||||
|
} else if (S_ISDIR(st.st_mode))
|
||||||
|
strncpy(foo, _("(dir)"), foo_len);
|
||||||
|
else if (st.st_size < (1 << 10)) /* less than 1 k. */
|
||||||
|
sprintf(foo, "%4u B", (unsigned int)st.st_size);
|
||||||
|
else if (st.st_size < (1 << 20)) /* less than 1 meg. */
|
||||||
|
sprintf(foo, "%4u KB",
|
||||||
|
(unsigned int)(st.st_size >> 10));
|
||||||
|
else if (st.st_size < (1 << 30)) /* less than 1 gig. */
|
||||||
|
sprintf(foo, "%4u MB",
|
||||||
|
(unsigned int)(st.st_size >> 20));
|
||||||
|
else
|
||||||
|
sprintf(foo, "%4u GB",
|
||||||
|
(unsigned int)(st.st_size >> 30));
|
||||||
|
|
||||||
|
mvwaddnstr(edit, editline, col - strlen(foo), foo,
|
||||||
|
foo_len);
|
||||||
|
|
||||||
if (j == selected)
|
if (j == selected)
|
||||||
wattroff(edit, A_REVERSE);
|
wattroff(edit, A_REVERSE);
|
||||||
|
|
||||||
/* And add some space between the cols */
|
/* Add some space between the columns. */
|
||||||
waddstr(edit, " ");
|
|
||||||
col += 2;
|
col += 2;
|
||||||
|
|
||||||
/* And if the next entry isn't going to fit on the
|
/* If the next entry isn't going to fit on the line,
|
||||||
line, move to the next one */
|
* move to the next line. */
|
||||||
if (col > COLS - longest) {
|
if (col > COLS - longest) {
|
||||||
editline++;
|
editline++;
|
||||||
wmove(edit, editline, 0);
|
|
||||||
col = 0;
|
col = 0;
|
||||||
if (width == 0)
|
if (width == 0)
|
||||||
width = filecols;
|
width = filecols;
|
||||||
}
|
}
|
||||||
|
wmove(edit, editline, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(foo);
|
||||||
|
}
|
||||||
|
|
||||||
wrefresh(edit);
|
wrefresh(edit);
|
||||||
} while ((kbinput = get_kbinput(edit, &meta_key, &func_key)) !=
|
} while ((kbinput = get_kbinput(edit, &meta_key, &func_key)) !=
|
||||||
NANO_EXIT_KEY && kbinput != NANO_EXIT_FKEY);
|
NANO_EXIT_KEY && kbinput != NANO_EXIT_FKEY);
|
||||||
curs_set(1);
|
|
||||||
blank_edit();
|
blank_edit();
|
||||||
titlebar(NULL);
|
titlebar(NULL);
|
||||||
edit_refresh();
|
edit_refresh();
|
||||||
|
curs_set(1);
|
||||||
|
if (old_constupdate)
|
||||||
|
SET(CONSTUPDATE);
|
||||||
|
|
||||||
/* cleanup */
|
/* Clean up. */
|
||||||
free_charptrarray(filelist, numents);
|
free_charptrarray(filelist, numents);
|
||||||
free(foo);
|
free(path);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Browser front end, checks to see if inpath has a dir in it and, if so,
|
/* Browser front end, checks to see if inpath has a dir in it and, if
|
||||||
starts do_browser from there, else from the current dir */
|
so, starts do_browser from there, else from the current dir */
|
||||||
char *do_browse_from(const char *inpath)
|
char *do_browse_from(const char *inpath)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char *bob;
|
|
||||||
/* The result of do_browser; the selected file name. */
|
|
||||||
char *path;
|
char *path;
|
||||||
/* inpath, tilde expanded. */
|
/* This holds the tilde-expanded version of inpath. */
|
||||||
|
DIR *dir;
|
||||||
|
|
||||||
assert(inpath != NULL);
|
assert(inpath != NULL);
|
||||||
|
|
||||||
path = real_dir_from_tilde(inpath);
|
path = real_dir_from_tilde(inpath);
|
||||||
|
|
||||||
/*
|
/* Perhaps path is a directory. If so, we'll pass it to
|
||||||
* Perhaps path is a directory. If so, we will pass that to
|
* do_browser(). Or perhaps path is a directory / a file. If so,
|
||||||
* do_browser. Otherwise, perhaps path is a directory / a file. So
|
* we'll try stripping off the last path element and passing it to
|
||||||
* we try stripping off the last path element. If it still isn't a
|
* do_browser(). Or perhaps path doesn't have a directory portion
|
||||||
* directory, just use the current directory. */
|
* at all. If so, we'll just pass the current directory to
|
||||||
|
* do_browser(). */
|
||||||
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
striponedir(path);
|
striponedir(path);
|
||||||
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
free(path);
|
free(path);
|
||||||
path = getcwd(NULL, PATH_MAX + 1);
|
#if PATH_MAX != -1
|
||||||
|
path = charalloc(PATH_MAX + 1);
|
||||||
|
#else
|
||||||
|
path = NULL;
|
||||||
|
#endif
|
||||||
|
path = getcwd(path, PATH_MAX + 1);
|
||||||
|
#if PATH_MAX != -1
|
||||||
|
align(&path);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef DISABLE_OPERATINGDIR
|
#ifndef DISABLE_OPERATINGDIR
|
||||||
/* If the resulting path isn't in the operating directory, use that. */
|
/* If the resulting path isn't in the operating directory, use
|
||||||
if (check_operating_dir(path, FALSE))
|
* the operating directory instead. */
|
||||||
path = mallocstrcpy(path, operating_dir);
|
if (check_operating_dir(path, FALSE)) {
|
||||||
|
if (path != NULL)
|
||||||
|
free(path);
|
||||||
|
path = mallocstrcpy(NULL, operating_dir);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!readable_dir(path)) {
|
dir = opendir(path);
|
||||||
|
if (dir == NULL) {
|
||||||
beep();
|
beep();
|
||||||
bob = NULL;
|
|
||||||
} else
|
|
||||||
bob = do_browser(path);
|
|
||||||
free(path);
|
free(path);
|
||||||
return bob;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return do_browser(path, dir);
|
||||||
}
|
}
|
||||||
#endif /* !DISABLE_BROWSER */
|
#endif /* !DISABLE_BROWSER */
|
||||||
|
|
||||||
|
@ -2791,8 +2835,10 @@ void load_history(void)
|
||||||
if (errno != ENOENT) {
|
if (errno != ENOENT) {
|
||||||
/* Don't save history when we quit. */
|
/* Don't save history when we quit. */
|
||||||
UNSET(HISTORYLOG);
|
UNSET(HISTORYLOG);
|
||||||
rcfile_error(N_("Error reading %s: %s"), nanohist, strerror(errno));
|
rcfile_error(N_("Error reading %s: %s"), nanohist,
|
||||||
fprintf(stderr, _("\nPress Return to continue starting nano\n"));
|
strerror(errno));
|
||||||
|
fprintf(stderr,
|
||||||
|
_("\nPress Return to continue starting nano\n"));
|
||||||
while (getchar() != '\n')
|
while (getchar() != '\n')
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@ -2853,7 +2899,8 @@ void save_history(void)
|
||||||
FILE *hist = fopen(nanohist, "wb");
|
FILE *hist = fopen(nanohist, "wb");
|
||||||
|
|
||||||
if (hist == NULL)
|
if (hist == NULL)
|
||||||
rcfile_error(N_("Error writing %s: %s"), nanohist, strerror(errno));
|
rcfile_error(N_("Error writing %s: %s"), nanohist,
|
||||||
|
strerror(errno));
|
||||||
else {
|
else {
|
||||||
/* set rw only by owner for security ?? */
|
/* set rw only by owner for security ?? */
|
||||||
chmod(nanohist, S_IRUSR | S_IWUSR);
|
chmod(nanohist, S_IRUSR | S_IWUSR);
|
||||||
|
@ -2861,7 +2908,8 @@ void save_history(void)
|
||||||
if (!writehist(hist, &search_history) ||
|
if (!writehist(hist, &search_history) ||
|
||||||
putc('\n', hist) == EOF ||
|
putc('\n', hist) == EOF ||
|
||||||
!writehist(hist, &replace_history))
|
!writehist(hist, &replace_history))
|
||||||
rcfile_error(N_("Error writing %s: %s"), nanohist, strerror(errno));
|
rcfile_error(N_("Error writing %s: %s"), nanohist,
|
||||||
|
strerror(errno));
|
||||||
fclose(hist);
|
fclose(hist);
|
||||||
}
|
}
|
||||||
free(nanohist);
|
free(nanohist);
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <dirent.h>
|
||||||
#ifdef HAVE_REGEX_H
|
#ifdef HAVE_REGEX_H
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -298,10 +299,10 @@ char *input_tab(char *buf, size_t *place, bool *lastwastab, bool *list);
|
||||||
const char *tail(const char *foo);
|
const char *tail(const char *foo);
|
||||||
#ifndef DISABLE_BROWSER
|
#ifndef DISABLE_BROWSER
|
||||||
void free_charptrarray(char **array, size_t len);
|
void free_charptrarray(char **array, size_t len);
|
||||||
void striponedir(char *foo);
|
void striponedir(char *path);
|
||||||
int readable_dir(const char *path);
|
char **browser_init(const char *path, int *longest, size_t *numents, DIR
|
||||||
char **browser_init(const char *path, int *longest, int *numents);
|
*dir);
|
||||||
char *do_browser(const char *inpath);
|
char *do_browser(char *path, DIR *dir);
|
||||||
char *do_browse_from(const char *inpath);
|
char *do_browse_from(const char *inpath);
|
||||||
#endif
|
#endif
|
||||||
#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
|
#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
|
||||||
|
|
Loading…
Reference in New Issue