2000-06-19 04:22:15 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* files.c -- This file is part of GNU nano. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2019-02-24 18:32:17 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2019 Free Software Foundation, Inc. *
|
2019-03-10 15:29:57 +00:00
|
|
|
* Copyright (C) 2015-2019 Benno Schulenberg *
|
2016-08-29 13:14:18 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty *
|
|
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU General Public License for more details. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
#include <errno.h>
|
2017-08-06 11:32:44 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2001-01-22 22:17:08 +00:00
|
|
|
#include <pwd.h>
|
2017-02-21 22:04:44 +00:00
|
|
|
#endif
|
2017-08-06 11:32:44 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2000-06-21 03:00:43 +00:00
|
|
|
|
2016-04-23 18:39:02 +00:00
|
|
|
#define LOCKBUFSIZE 8192
|
|
|
|
|
2016-01-20 15:56:40 +00:00
|
|
|
/* Verify that the containing directory of the given filename exists. */
|
|
|
|
bool has_valid_path(const char *filename)
|
2016-01-20 15:14:52 +00:00
|
|
|
{
|
2019-10-13 10:24:27 +00:00
|
|
|
char *namecopy = copy_of(filename);
|
2017-12-29 18:27:33 +00:00
|
|
|
char *parentdir = dirname(namecopy);
|
|
|
|
struct stat parentinfo;
|
|
|
|
bool validity = FALSE;
|
|
|
|
|
|
|
|
if (stat(parentdir, &parentinfo) == -1) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
statusline(ALERT, _("Directory '%s' does not exist"), parentdir);
|
|
|
|
else
|
|
|
|
statusline(ALERT, _("Path '%s': %s"), parentdir, strerror(errno));
|
|
|
|
} else if (!S_ISDIR(parentinfo.st_mode))
|
|
|
|
statusline(ALERT, _("Path '%s' is not a directory"), parentdir);
|
|
|
|
else if (access(parentdir, X_OK) == -1)
|
|
|
|
statusline(ALERT, _("Path '%s' is not accessible"), parentdir);
|
|
|
|
else if (ISSET(LOCKING) && access(parentdir, W_OK) == -1)
|
|
|
|
statusline(MILD, _("Directory '%s' is not writable"), parentdir);
|
2016-01-31 13:06:06 +00:00
|
|
|
else
|
2017-12-29 18:27:33 +00:00
|
|
|
validity = TRUE;
|
|
|
|
|
|
|
|
free(namecopy);
|
|
|
|
|
|
|
|
return validity;
|
2016-01-20 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 14:31:27 +00:00
|
|
|
/* Add an item to the circular list of openfile structs. */
|
2005-07-08 20:09:16 +00:00
|
|
|
void make_new_buffer(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2018-10-30 18:34:03 +00:00
|
|
|
openfilestruct *newnode = nmalloc(sizeof(openfilestruct));
|
2016-02-25 18:58:17 +00:00
|
|
|
|
2019-04-22 17:38:19 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile == NULL) {
|
|
|
|
/* Make the first open file the only element in the list. */
|
|
|
|
newnode->prev = newnode;
|
|
|
|
newnode->next = newnode;
|
2019-04-22 17:38:19 +00:00
|
|
|
|
2019-04-22 17:45:42 +00:00
|
|
|
startfile = newnode;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else {
|
|
|
|
/* Add the new open file after the current one in the list. */
|
|
|
|
newnode->prev = openfile;
|
|
|
|
newnode->next = openfile->next;
|
|
|
|
openfile->next->prev = newnode;
|
|
|
|
openfile->next = newnode;
|
2019-04-22 17:38:19 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* There is more than one file open: show "Close" in help lines. */
|
|
|
|
exitfunc->desc = close_tag;
|
|
|
|
more_than_one = !inhelp || more_than_one;
|
|
|
|
}
|
2019-04-22 17:38:19 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Make the new buffer the current one, and start initializing it. */
|
|
|
|
openfile = newnode;
|
2017-04-19 14:31:27 +00:00
|
|
|
|
2019-10-13 10:24:27 +00:00
|
|
|
openfile->filename = copy_of("");
|
2002-03-28 01:59:34 +00:00
|
|
|
|
2019-05-16 14:14:15 +00:00
|
|
|
openfile->filetop = make_new_node(NULL);
|
2019-10-13 10:24:27 +00:00
|
|
|
openfile->filetop->data = copy_of("");
|
2019-05-16 14:14:15 +00:00
|
|
|
openfile->filebot = openfile->filetop;
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2019-05-16 14:14:15 +00:00
|
|
|
openfile->current = openfile->filetop;
|
|
|
|
openfile->current_x = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->placewewant = 0;
|
|
|
|
openfile->current_y = 0;
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2019-05-16 14:14:15 +00:00
|
|
|
openfile->edittop = openfile->filetop;
|
|
|
|
openfile->firstcolumn = 0;
|
|
|
|
|
|
|
|
openfile->totsize = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->modified = FALSE;
|
2019-04-21 15:31:29 +00:00
|
|
|
#ifdef ENABLE_WRAPPING
|
|
|
|
openfile->spillage_line = NULL;
|
|
|
|
#endif
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->mark = NULL;
|
2005-07-12 20:09:24 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->fmt = NIX_FILE;
|
2002-04-22 23:52:34 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->undotop = NULL;
|
|
|
|
openfile->current_undo = NULL;
|
|
|
|
openfile->last_saved = NULL;
|
|
|
|
openfile->last_action = OTHER;
|
2015-07-10 17:25:51 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_stat = NULL;
|
|
|
|
openfile->lock_filename = NULL;
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->syntax = NULL;
|
|
|
|
openfile->colorstrings = NULL;
|
2005-07-13 20:18:46 +00:00
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2016-01-15 14:42:07 +00:00
|
|
|
/* Mark the current file as modified if it isn't already, and then
|
|
|
|
* update the titlebar to display the file's new status. */
|
|
|
|
void set_modified(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->modified)
|
|
|
|
return;
|
2016-01-15 14:42:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->modified = TRUE;
|
|
|
|
titlebar(NULL);
|
2016-01-15 14:42:07 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (!ISSET(LOCKING) || openfile->filename[0] == '\0')
|
|
|
|
return;
|
2016-01-15 14:42:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->lock_filename != NULL) {
|
|
|
|
char *fullname = get_full_path(openfile->filename);
|
2017-02-24 10:57:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
write_lockfile(openfile->lock_filename, fullname, TRUE);
|
|
|
|
free(fullname);
|
|
|
|
}
|
2016-01-15 14:42:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-01 03:24:39 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Actually write the lockfile. This function will ALWAYS annihilate
|
|
|
|
* any previous version of the file. We'll borrow INSECURE_BACKUP here
|
|
|
|
* to decide about lockfile paranoia here as well...
|
|
|
|
*
|
|
|
|
* Args:
|
|
|
|
* lockfilename: file name for lock
|
|
|
|
* origfilename: name of the file the lock is for
|
|
|
|
* modified: whether to set the modified bit in the file
|
|
|
|
*
|
2016-05-21 10:45:10 +00:00
|
|
|
* Returns 1 on success, and 0 on failure (but continue anyway). */
|
2013-01-01 03:24:39 +00:00
|
|
|
int write_lockfile(const char *lockfilename, const char *origfilename, bool modified)
|
|
|
|
{
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2017-12-29 18:27:33 +00:00
|
|
|
int cflags, fd;
|
|
|
|
FILE *filestream;
|
|
|
|
pid_t mypid;
|
|
|
|
uid_t myuid;
|
|
|
|
struct passwd *mypwuid;
|
|
|
|
struct stat fileinfo;
|
|
|
|
char *lockdata = charalloc(1024);
|
|
|
|
char myhostname[32];
|
|
|
|
size_t lockdatalen = 1024;
|
|
|
|
size_t wroteamt;
|
|
|
|
|
|
|
|
mypid = getpid();
|
|
|
|
myuid = geteuid();
|
|
|
|
|
|
|
|
/* First run things that might fail before blowing away the old state. */
|
|
|
|
if ((mypwuid = getpwuid(myuid)) == NULL) {
|
|
|
|
/* TRANSLATORS: Keep the next eight messages at most 76 characters. */
|
2019-06-19 17:34:19 +00:00
|
|
|
statusline(MILD, _("Couldn't determine my identity for lock file"));
|
2017-12-29 18:27:33 +00:00
|
|
|
goto free_the_data;
|
2015-11-06 20:14:37 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (gethostname(myhostname, 31) < 0) {
|
|
|
|
if (errno == ENAMETOOLONG)
|
|
|
|
myhostname[31] = '\0';
|
|
|
|
else {
|
2019-06-19 17:34:19 +00:00
|
|
|
statusline(MILD, _("Couldn't determine hostname: %s"), strerror(errno));
|
2017-12-29 18:27:33 +00:00
|
|
|
goto free_the_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the lockfile exists, try to delete it. */
|
|
|
|
if (stat(lockfilename, &fileinfo) != -1)
|
|
|
|
if (delete_lockfile(lockfilename) < 0)
|
|
|
|
goto free_the_data;
|
|
|
|
|
|
|
|
if (ISSET(INSECURE_BACKUP))
|
|
|
|
cflags = O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
else
|
|
|
|
cflags = O_WRONLY | O_CREAT | O_EXCL | O_APPEND;
|
|
|
|
|
|
|
|
/* Try to create the lockfile. */
|
|
|
|
fd = open(lockfilename, cflags,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
if (fd < 0) {
|
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"),
|
|
|
|
lockfilename, strerror(errno));
|
|
|
|
goto free_the_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to associate a stream with the now open lockfile. */
|
|
|
|
filestream = fdopen(fd, "wb");
|
|
|
|
|
|
|
|
if (filestream == NULL) {
|
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"), lockfilename,
|
|
|
|
strerror(errno));
|
|
|
|
goto free_the_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the lock data we will store:
|
|
|
|
*
|
|
|
|
* byte 0 - 0x62
|
|
|
|
* byte 1 - 0x30
|
|
|
|
* bytes 2-12 - program name which created the lock
|
|
|
|
* bytes 24-27 - PID (little endian) of creator process
|
|
|
|
* bytes 28-44 - username of who created the lock
|
|
|
|
* bytes 68-100 - hostname of where the lock was created
|
|
|
|
* bytes 108-876 - filename the lock is for
|
|
|
|
* byte 1007 - 0x55 if file is modified
|
|
|
|
*
|
|
|
|
* Looks like VIM also stores undo state in this file, so we're
|
|
|
|
* gonna have to figure out how to slap a 'OMG don't use recover on
|
|
|
|
* our lockfile' message in here...
|
|
|
|
*
|
|
|
|
* This is likely very wrong, so this is a WIP. */
|
|
|
|
memset(lockdata, 0, lockdatalen);
|
|
|
|
lockdata[0] = 0x62;
|
|
|
|
lockdata[1] = 0x30;
|
|
|
|
lockdata[24] = mypid % 256;
|
|
|
|
lockdata[25] = (mypid / 256) % 256;
|
|
|
|
lockdata[26] = (mypid / (256 * 256)) % 256;
|
|
|
|
lockdata[27] = mypid / (256 * 256 * 256);
|
|
|
|
snprintf(&lockdata[2], 11, "nano %s", VERSION);
|
|
|
|
strncpy(&lockdata[28], mypwuid->pw_name, 16);
|
|
|
|
strncpy(&lockdata[68], myhostname, 31);
|
|
|
|
if (origfilename != NULL)
|
|
|
|
strncpy(&lockdata[108], origfilename, 768);
|
|
|
|
if (modified == TRUE)
|
|
|
|
lockdata[1007] = 0x55;
|
|
|
|
|
|
|
|
wroteamt = fwrite(lockdata, sizeof(char), lockdatalen, filestream);
|
|
|
|
if (wroteamt < lockdatalen) {
|
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"),
|
|
|
|
lockfilename, ferror(filestream));
|
2017-12-29 20:35:14 +00:00
|
|
|
fclose(filestream);
|
2017-12-29 18:27:33 +00:00
|
|
|
goto free_the_data;
|
|
|
|
}
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (fclose(filestream) == EOF) {
|
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"),
|
|
|
|
lockfilename, strerror(errno));
|
|
|
|
goto free_the_data;
|
|
|
|
}
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2019-06-09 18:02:58 +00:00
|
|
|
openfile->lock_filename = (char *)lockfilename;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(lockdata);
|
|
|
|
return 1;
|
2015-08-03 08:32:52 +00:00
|
|
|
|
2016-05-21 10:45:10 +00:00
|
|
|
free_the_data:
|
2017-12-29 18:27:33 +00:00
|
|
|
free(lockdata);
|
|
|
|
return 0;
|
2017-02-21 22:04:44 +00:00
|
|
|
#else
|
2017-12-29 18:27:33 +00:00
|
|
|
return 1;
|
2017-02-21 22:04:44 +00:00
|
|
|
#endif
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:52:51 +00:00
|
|
|
/* Delete the lockfile. Return -1 if unsuccessful, and 1 otherwise. */
|
2013-01-01 03:24:39 +00:00
|
|
|
int delete_lockfile(const char *lockfilename)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (unlink(lockfilename) < 0 && errno != ENOENT) {
|
|
|
|
statusline(MILD, _("Error deleting lock file %s: %s"), lockfilename,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Deal with lockfiles. Return -1 on refusing to override the lockfile,
|
|
|
|
* and 1 on successfully creating it; 0 means we were not successful in
|
2016-05-23 12:52:51 +00:00
|
|
|
* creating the lockfile but we should continue to load the file. */
|
2013-01-01 03:24:39 +00:00
|
|
|
int do_lockfile(const char *filename)
|
|
|
|
{
|
2019-10-13 10:24:27 +00:00
|
|
|
char *namecopy = copy_of(filename);
|
|
|
|
char *secondcopy = copy_of(filename);
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t locknamesize = strlen(filename) + strlen(locking_prefix)
|
|
|
|
+ strlen(locking_suffix) + 3;
|
|
|
|
char *lockfilename = charalloc(locknamesize);
|
|
|
|
static char lockprog[11], lockuser[17];
|
|
|
|
struct stat fileinfo;
|
|
|
|
int lockfd, lockpid, retval = -1;
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
snprintf(lockfilename, locknamesize, "%s/%s%s%s", dirname(namecopy),
|
|
|
|
locking_prefix, basename(secondcopy), locking_suffix);
|
2015-06-27 15:10:58 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (stat(lockfilename, &fileinfo) != -1) {
|
|
|
|
size_t readtot = 0;
|
|
|
|
size_t readamt = 0;
|
|
|
|
char *lockbuf, *question, *pidstring, *postedname, *promptstr;
|
2019-02-10 15:11:57 +00:00
|
|
|
int room, choice;
|
2016-08-30 09:45:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if ((lockfd = open(lockfilename, O_RDONLY)) < 0) {
|
|
|
|
statusline(MILD, _("Error opening lock file %s: %s"),
|
|
|
|
lockfilename, strerror(errno));
|
|
|
|
goto free_the_name;
|
|
|
|
}
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
lockbuf = charalloc(LOCKBUFSIZE);
|
|
|
|
do {
|
|
|
|
readamt = read(lockfd, &lockbuf[readtot], LOCKBUFSIZE - readtot);
|
|
|
|
readtot += readamt;
|
|
|
|
} while (readamt > 0 && readtot < LOCKBUFSIZE);
|
2016-04-06 08:41:42 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
close(lockfd);
|
2016-05-18 19:04:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (readtot < 48) {
|
|
|
|
statusline(MILD, _("Error reading lock file %s: "
|
|
|
|
"Not enough data read"), lockfilename);
|
|
|
|
free(lockbuf);
|
|
|
|
goto free_the_name;
|
|
|
|
}
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
strncpy(lockprog, &lockbuf[2], 10);
|
|
|
|
lockpid = (((unsigned char)lockbuf[27] * 256 + (unsigned char)lockbuf[26]) * 256 +
|
|
|
|
(unsigned char)lockbuf[25]) * 256 + (unsigned char)lockbuf[24];
|
|
|
|
strncpy(lockuser, &lockbuf[28], 16);
|
|
|
|
free(lockbuf);
|
|
|
|
|
|
|
|
pidstring = charalloc(11);
|
|
|
|
sprintf (pidstring, "%u", (unsigned int)lockpid);
|
|
|
|
|
2018-01-25 10:00:19 +00:00
|
|
|
/* Display newlines in filenames as ^J. */
|
|
|
|
as_an_at = FALSE;
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* TRANSLATORS: The second %s is the name of the user, the third that of the editor. */
|
|
|
|
question = _("File %s is being edited (by %s with %s, PID %s); continue?");
|
2019-04-24 06:49:18 +00:00
|
|
|
room = COLS - breadth(question) + 7 - breadth(lockuser) -
|
|
|
|
breadth(lockprog) - breadth(pidstring);
|
2017-12-29 18:27:33 +00:00
|
|
|
if (room < 4)
|
2019-10-13 10:24:27 +00:00
|
|
|
postedname = copy_of("_");
|
2019-04-24 06:49:18 +00:00
|
|
|
else if (room < breadth(filename)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
char *fragment = display_string(filename,
|
2019-04-24 06:49:18 +00:00
|
|
|
breadth(filename) - room + 3, room, FALSE, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
postedname = charalloc(strlen(fragment) + 4);
|
|
|
|
strcpy(postedname, "...");
|
|
|
|
strcat(postedname, fragment);
|
|
|
|
free(fragment);
|
|
|
|
} else
|
2019-02-27 20:37:53 +00:00
|
|
|
postedname = display_string(filename, 0, room, FALSE, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* Allow extra space for username (14), program name (8), PID (8),
|
|
|
|
* and terminating \0 (1), minus the %s (2) for the file name. */
|
|
|
|
promptstr = charalloc(strlen(question) + 29 + strlen(postedname));
|
|
|
|
sprintf(promptstr, question, postedname, lockuser, lockprog, pidstring);
|
|
|
|
free(postedname);
|
|
|
|
free(pidstring);
|
|
|
|
|
2019-02-10 15:11:57 +00:00
|
|
|
choice = do_yesno_prompt(FALSE, promptstr);
|
2017-12-29 18:27:33 +00:00
|
|
|
free(promptstr);
|
|
|
|
|
2019-02-10 15:11:57 +00:00
|
|
|
if (choice < 1) {
|
2017-12-29 18:27:33 +00:00
|
|
|
wipe_statusbar();
|
|
|
|
goto free_the_name;
|
|
|
|
}
|
2015-06-27 15:10:58 +00:00
|
|
|
}
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
retval = write_lockfile(lockfilename, filename, FALSE);
|
2016-01-29 16:58:02 +00:00
|
|
|
|
|
|
|
free_the_name:
|
2017-12-29 18:27:33 +00:00
|
|
|
free(namecopy);
|
|
|
|
free(secondcopy);
|
|
|
|
if (retval < 1)
|
|
|
|
free(lockfilename);
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return retval;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
2016-02-09 20:53:11 +00:00
|
|
|
|
|
|
|
/* Perform a stat call on the given filename, allocating a stat struct
|
|
|
|
* if necessary. On success, *pstat points to the stat's result. On
|
|
|
|
* failure, *pstat is freed and made NULL. */
|
|
|
|
void stat_with_alloc(const char *filename, struct stat **pstat)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (*pstat == NULL)
|
|
|
|
*pstat = (struct stat *)nmalloc(sizeof(struct stat));
|
2016-02-09 20:53:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (stat(filename, *pstat) != 0) {
|
|
|
|
free(*pstat);
|
|
|
|
*pstat = NULL;
|
|
|
|
}
|
2016-02-09 20:53:11 +00:00
|
|
|
}
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2018-03-21 10:36:00 +00:00
|
|
|
/* This does one of three things. If the filename is "", it just creates
|
|
|
|
* a new empty buffer. When the filename is not empty, it reads that file
|
|
|
|
* into a new buffer when requested, otherwise into the existing buffer. */
|
|
|
|
bool open_buffer(const char *filename, bool new_buffer)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
char *realname;
|
|
|
|
/* The filename after tilde expansion. */
|
2019-05-27 07:38:08 +00:00
|
|
|
struct stat fileinfo;
|
2017-12-29 18:27:33 +00:00
|
|
|
FILE *f;
|
|
|
|
int rc;
|
|
|
|
/* rc == -2 means that we have a new file. -1 means that the
|
|
|
|
* open() failed. 0 means that the open() succeeded. */
|
|
|
|
|
|
|
|
/* Display newlines in filenames as ^J. */
|
|
|
|
as_an_at = FALSE;
|
2016-12-26 11:35:50 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2019-05-29 09:42:23 +00:00
|
|
|
if (outside_of_confinement(filename, FALSE)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(ALERT, _("Can't read file from outside of %s"),
|
|
|
|
operating_dir);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-07-08 20:09:16 +00:00
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
realname = real_dir_from_tilde(filename);
|
2016-04-23 16:13:43 +00:00
|
|
|
|
2019-05-27 07:38:08 +00:00
|
|
|
/* Don't try to open directories, character files, or block files. */
|
|
|
|
if (*filename != '\0' && stat(realname, &fileinfo) == 0) {
|
|
|
|
if (S_ISDIR(fileinfo.st_mode)) {
|
2019-05-12 08:17:36 +00:00
|
|
|
statusline(ALERT, _("\"%s\" is a directory"), realname);
|
2017-12-29 18:27:33 +00:00
|
|
|
free(realname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-27 07:38:08 +00:00
|
|
|
if (S_ISCHR(fileinfo.st_mode) || S_ISBLK(fileinfo.st_mode)) {
|
|
|
|
statusline(ALERT, _("\"%s\" is a device file"), realname);
|
|
|
|
free(realname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-27 07:55:24 +00:00
|
|
|
#ifdef NANO_TINY
|
|
|
|
if (S_ISFIFO(fileinfo.st_mode)) {
|
|
|
|
statusline(ALERT, _("\"%s\" is a FIFO"), realname);
|
|
|
|
free(realname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-17 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're going to load into a new buffer, first create the new
|
|
|
|
* buffer and (if possible) lock the corresponding file. */
|
|
|
|
if (new_buffer) {
|
|
|
|
make_new_buffer();
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2019-05-29 09:42:23 +00:00
|
|
|
if (has_valid_path(realname)) {
|
2015-01-14 02:36:30 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(LOCKING) && filename[0] != '\0') {
|
|
|
|
/* When not overriding an existing lock, discard the buffer. */
|
|
|
|
if (do_lockfile(realname) < 0) {
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2019-06-16 17:09:03 +00:00
|
|
|
if (openfile != openfile->next)
|
|
|
|
close_buffer();
|
2015-01-20 06:15:34 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
free(realname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 15:33:41 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2016-05-17 11:37:53 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If the filename isn't blank, and we are not in NOREAD_MODE,
|
|
|
|
* open the file. Otherwise, treat it as a new file. */
|
|
|
|
rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ?
|
2019-05-24 10:42:48 +00:00
|
|
|
open_file(realname, new_buffer, &f) : -2;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If we have a non-new file, read it in. Then, if the buffer has
|
|
|
|
* no stat, update the stat, if applicable. */
|
|
|
|
if (rc > 0) {
|
2019-05-26 11:32:28 +00:00
|
|
|
install_handler_for_Ctrl_C();
|
2019-05-24 17:58:35 +00:00
|
|
|
|
2018-03-22 09:41:39 +00:00
|
|
|
read_file(f, rc, realname, !new_buffer);
|
2019-05-24 17:58:35 +00:00
|
|
|
|
2019-05-26 11:32:28 +00:00
|
|
|
restore_handler_for_Ctrl_C();
|
2019-05-24 17:58:35 +00:00
|
|
|
|
2019-05-26 11:32:28 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->current_stat == NULL)
|
|
|
|
stat_with_alloc(realname, &openfile->current_stat);
|
2001-10-14 19:05:10 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-07-08 20:09:16 +00:00
|
|
|
|
2018-03-21 09:41:47 +00:00
|
|
|
/* If we have a file, and we've loaded it into a new buffer, set
|
|
|
|
* the filename and put the cursor at the start of the buffer. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (rc != -1 && new_buffer) {
|
2018-03-21 09:41:47 +00:00
|
|
|
openfile->filename = mallocstrcpy(openfile->filename, realname);
|
2019-03-21 16:23:49 +00:00
|
|
|
openfile->current = openfile->filetop;
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = 0;
|
|
|
|
openfile->placewewant = 0;
|
|
|
|
}
|
2005-07-13 20:18:46 +00:00
|
|
|
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're loading into a new buffer, update the colors to account
|
|
|
|
* for it, if applicable. */
|
|
|
|
if (new_buffer)
|
|
|
|
color_update();
|
2005-07-13 20:18:46 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
free(realname);
|
|
|
|
return TRUE;
|
2005-07-09 04:42:47 +00:00
|
|
|
}
|
2003-09-23 04:25:05 +00:00
|
|
|
|
2017-10-31 18:32:42 +00:00
|
|
|
#ifdef ENABLE_SPELLER
|
2019-05-21 12:12:42 +00:00
|
|
|
/* Open the specified file, and if that succeeds, remove the text of the marked
|
|
|
|
* region or of the entire buffer and read the file contents into its place. */
|
2019-10-09 17:42:22 +00:00
|
|
|
bool replace_buffer(const char *filename, undo_type action, bool marked,
|
|
|
|
const char *operation)
|
2005-11-08 19:15:58 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_cutbuffer = cutbuffer;
|
2019-05-21 12:12:42 +00:00
|
|
|
int descriptor;
|
|
|
|
FILE *f;
|
2005-11-08 19:15:58 +00:00
|
|
|
|
2019-05-24 10:42:48 +00:00
|
|
|
descriptor = open_file(filename, FALSE, &f);
|
2016-04-17 15:33:18 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (descriptor < 0)
|
2019-05-24 15:22:04 +00:00
|
|
|
return FALSE;
|
2005-11-08 19:15:58 +00:00
|
|
|
|
2018-07-30 21:45:31 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-09 17:42:22 +00:00
|
|
|
add_undo(COUPLE_BEGIN, operation);
|
2018-07-30 21:45:31 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-21 10:47:22 +00:00
|
|
|
/* When nothing is marked, start at the top of the buffer. */
|
|
|
|
if (!marked) {
|
|
|
|
openfile->current = openfile->filetop;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Throw away the marked region or the whole buffer. */
|
2018-07-30 21:38:53 +00:00
|
|
|
cutbuffer = NULL;
|
2018-07-30 21:45:31 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(action, NULL);
|
2018-07-30 21:45:31 +00:00
|
|
|
#endif
|
2019-08-09 17:34:03 +00:00
|
|
|
do_snip(FALSE, marked, !marked, FALSE);
|
2018-07-30 21:45:31 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-21 10:47:22 +00:00
|
|
|
update_undo(action);
|
2018-07-30 21:45:31 +00:00
|
|
|
#endif
|
2019-03-21 16:18:50 +00:00
|
|
|
free_lines(cutbuffer);
|
2018-07-30 21:38:53 +00:00
|
|
|
cutbuffer = was_cutbuffer;
|
2005-11-08 19:15:58 +00:00
|
|
|
|
2019-05-21 10:47:22 +00:00
|
|
|
/* Insert the spell-checked file into the cleared area. */
|
2018-07-30 22:07:43 +00:00
|
|
|
read_file(f, descriptor, filename, TRUE);
|
2018-07-30 21:45:31 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2019-10-09 17:42:22 +00:00
|
|
|
add_undo(COUPLE_END, operation);
|
2018-07-30 21:45:31 +00:00
|
|
|
#endif
|
2019-05-24 15:22:04 +00:00
|
|
|
return TRUE;
|
2005-11-08 19:15:58 +00:00
|
|
|
}
|
2017-10-31 18:32:42 +00:00
|
|
|
#endif /* ENABLE_SPELLER */
|
2005-11-08 19:15:58 +00:00
|
|
|
|
2017-05-01 14:48:13 +00:00
|
|
|
/* Update the titlebar and the multiline cache to match the current buffer. */
|
|
|
|
void prepare_for_display(void)
|
2005-07-09 04:42:47 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Update the titlebar, since the filename may have changed. */
|
|
|
|
if (!inhelp)
|
|
|
|
titlebar(NULL);
|
2005-07-14 23:06:22 +00:00
|
|
|
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If there are multiline coloring regexes, and there is no
|
|
|
|
* multiline cache data yet, precalculate it now. */
|
|
|
|
if (openfile->syntax && openfile->syntax->nmultis > 0 &&
|
2019-03-21 16:23:49 +00:00
|
|
|
openfile->filetop->multidata == NULL)
|
2017-12-29 18:27:33 +00:00
|
|
|
precalc_multicolorinfo();
|
2005-07-14 23:06:22 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
have_palette = FALSE;
|
2017-05-01 14:48:13 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2018-07-12 11:09:17 +00:00
|
|
|
/* Show name of current buffer and its number of lines on the status bar. */
|
|
|
|
void mention_name_and_linecount(void)
|
|
|
|
{
|
|
|
|
size_t count = openfile->filebot->lineno -
|
|
|
|
(openfile->filebot->data[0] == '\0' ? 1 : 0);
|
2018-07-13 09:15:48 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (openfile->fmt != NIX_FILE)
|
2018-09-02 11:02:50 +00:00
|
|
|
/* TRANSLATORS: first %s is file name, second %s is file format. */
|
2018-07-13 09:15:48 +00:00
|
|
|
statusline(HUSH, P_("%s -- %zu line (%s)", "%s -- %zu lines (%s)", count),
|
|
|
|
openfile->filename[0] == '\0' ?
|
|
|
|
_("New Buffer") : tail(openfile->filename), count,
|
|
|
|
openfile->fmt == DOS_FILE ? _("DOS") : _("Mac"));
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
statusline(HUSH, P_("%s -- %zu line", "%s -- %zu lines", count),
|
2018-07-12 11:09:17 +00:00
|
|
|
openfile->filename[0] == '\0' ?
|
|
|
|
_("New Buffer") : tail(openfile->filename), count);
|
|
|
|
}
|
|
|
|
|
2019-05-31 07:33:28 +00:00
|
|
|
/* Update title bar and such after switching to another buffer.*/
|
|
|
|
void redecorate_after_switch(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2019-05-31 07:33:28 +00:00
|
|
|
/* If only one file buffer is open, there is nothing to update. */
|
2019-05-30 15:18:30 +00:00
|
|
|
if (openfile == openfile->next) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusbar(_("No more open file buffers"));
|
|
|
|
return;
|
|
|
|
}
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2017-01-21 16:54:47 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When not in softwrap mode, make sure firstcolumn is zero. It might
|
|
|
|
* be nonzero if we had softwrap mode on while in this buffer, and then
|
|
|
|
* turned softwrap mode off while in a different buffer. */
|
|
|
|
if (!ISSET(SOFTWRAP))
|
|
|
|
openfile->firstcolumn = 0;
|
2017-01-21 16:54:47 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Update titlebar and multiline info to match the current buffer. */
|
|
|
|
prepare_for_display();
|
2005-07-09 04:42:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Ensure that the main loop will redraw the help lines. */
|
|
|
|
currmenu = MMOST;
|
2017-07-13 08:55:02 +00:00
|
|
|
|
2019-01-21 11:18:37 +00:00
|
|
|
/* Prevent a possible Shift selection from getting cancelled. */
|
|
|
|
shift_held = TRUE;
|
|
|
|
|
2018-07-12 11:09:17 +00:00
|
|
|
/* Indicate on the status bar where we switched to. */
|
|
|
|
mention_name_and_linecount();
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:14:06 +00:00
|
|
|
/* Switch to the previous entry in the list of open files. */
|
2017-08-31 20:00:53 +00:00
|
|
|
void switch_to_prev_buffer(void)
|
2002-02-22 04:30:50 +00:00
|
|
|
{
|
2019-05-31 07:33:28 +00:00
|
|
|
openfile = openfile->prev;
|
|
|
|
redecorate_after_switch();
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2017-08-31 20:14:06 +00:00
|
|
|
/* Switch to the next entry in the list of open files. */
|
2017-08-31 20:00:53 +00:00
|
|
|
void switch_to_next_buffer(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2019-05-31 07:33:28 +00:00
|
|
|
openfile = openfile->next;
|
|
|
|
redecorate_after_switch();
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2019-06-01 09:10:48 +00:00
|
|
|
/* Remove the current buffer from the circular list of buffers. */
|
|
|
|
void close_buffer(void)
|
2019-06-01 08:52:38 +00:00
|
|
|
{
|
2019-06-01 09:10:48 +00:00
|
|
|
openfilestruct *orphan = openfile;
|
|
|
|
|
|
|
|
if (orphan == startfile)
|
2019-06-01 08:52:38 +00:00
|
|
|
startfile = startfile->next;
|
|
|
|
|
2019-06-01 09:10:48 +00:00
|
|
|
orphan->prev->next = orphan->next;
|
|
|
|
orphan->next->prev = orphan->prev;
|
2019-06-01 08:52:38 +00:00
|
|
|
|
2019-06-01 09:10:48 +00:00
|
|
|
free(orphan->filename);
|
|
|
|
free_lines(orphan->filetop);
|
2019-06-01 08:52:38 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-06-01 09:10:48 +00:00
|
|
|
free(orphan->current_stat);
|
|
|
|
free(orphan->lock_filename);
|
2019-06-01 08:52:38 +00:00
|
|
|
/* Free the undo stack. */
|
2019-06-01 09:10:48 +00:00
|
|
|
discard_until(NULL, orphan, TRUE);
|
2019-06-01 08:52:38 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-01 09:10:48 +00:00
|
|
|
openfile = orphan->prev;
|
|
|
|
free(orphan);
|
2002-02-22 04:30:50 +00:00
|
|
|
|
2019-05-30 14:37:16 +00:00
|
|
|
/* When just one buffer remains open, show "Exit" in the help lines. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile == openfile->next)
|
|
|
|
exitfunc->desc = exit_tag;
|
2002-02-22 04:30:50 +00:00
|
|
|
}
|
2017-05-01 18:20:34 +00:00
|
|
|
#endif /* ENABLE_MULTIBUFFER */
|
2002-02-22 04:30:50 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* Encode any NUL bytes in the given line of text, which is of length buf_len,
|
|
|
|
* and return a dynamically allocated copy of the resultant string. */
|
|
|
|
char *encode_data(char *buf, size_t buf_len)
|
2004-09-05 21:40:31 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
unsunder(buf, buf_len);
|
|
|
|
buf[buf_len] = '\0';
|
2004-08-27 21:02:38 +00:00
|
|
|
|
2019-10-13 10:24:27 +00:00
|
|
|
return copy_of(buf);
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2004-11-05 16:24:35 +00:00
|
|
|
|
2018-03-22 09:41:39 +00:00
|
|
|
/* Read the given open file f into the current buffer. filename should be
|
|
|
|
* set to the name of the file. undoable means that undo records should be
|
|
|
|
* created and that the file does not need to be checked for writability. */
|
|
|
|
void read_file(FILE *f, int fd, const char *filename, bool undoable)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
ssize_t was_lineno = openfile->current->lineno;
|
|
|
|
/* The line number where we start the insertion. */
|
|
|
|
size_t was_leftedge = 0;
|
|
|
|
/* The leftedge where we start the insertion. */
|
|
|
|
size_t num_lines = 0;
|
|
|
|
/* The number of lines in the file. */
|
|
|
|
size_t len = 0;
|
|
|
|
/* The length of the current line of the file. */
|
|
|
|
char input = '\0';
|
|
|
|
/* The current input character. */
|
|
|
|
char *buf;
|
|
|
|
/* The buffer in which we assemble each line of the file. */
|
|
|
|
size_t bufx = MAX_BUF_SIZE;
|
|
|
|
/* The allocated size of the line buffer; increased as needed. */
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *topline;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The top of the new buffer where we store the read file. */
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *bottomline;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The bottom of the new buffer. */
|
|
|
|
int input_int;
|
|
|
|
/* The current value we read from the file, whether an input
|
|
|
|
* character or EOF. */
|
2019-05-28 09:06:42 +00:00
|
|
|
int errornumber;
|
|
|
|
/* The error code, in case an error occurred during reading. */
|
2017-12-29 18:27:33 +00:00
|
|
|
bool writable = TRUE;
|
|
|
|
/* Whether the file is writable (in case we care). */
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
int format = 0;
|
|
|
|
/* 0 = *nix, 1 = DOS, 2 = Mac, 3 = both DOS and Mac. */
|
2004-10-01 18:34:30 +00:00
|
|
|
#endif
|
2002-09-03 22:58:40 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
buf = charalloc(bufx);
|
2004-09-05 21:40:31 +00:00
|
|
|
|
2008-08-03 04:48:05 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (undoable)
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(INSERT, NULL);
|
2017-02-08 04:09:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP))
|
|
|
|
was_leftedge = leftedge_for(xplustabs(), openfile->current);
|
2008-08-03 04:48:05 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Create an empty buffer. */
|
|
|
|
topline = make_new_node(NULL);
|
|
|
|
bottomline = topline;
|
2017-02-09 18:26:27 +00:00
|
|
|
|
2019-03-31 23:17:45 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
block_sigwinch(TRUE);
|
|
|
|
#endif
|
|
|
|
|
2018-06-08 16:38:19 +00:00
|
|
|
/* Lock the file before starting to read it, to avoid the overhead
|
|
|
|
* of locking it for each single byte that we read from it. */
|
|
|
|
flockfile(f);
|
|
|
|
|
2019-05-24 17:58:35 +00:00
|
|
|
control_C_was_pressed = FALSE;
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Read the entire file into the new buffer. */
|
2018-06-08 16:38:19 +00:00
|
|
|
while ((input_int = getc_unlocked(f)) != EOF) {
|
2019-05-24 17:58:35 +00:00
|
|
|
|
2019-05-28 15:01:31 +00:00
|
|
|
if (control_C_was_pressed) {
|
|
|
|
statusline(ALERT, _("Interrupted"));
|
2019-05-24 17:58:35 +00:00
|
|
|
break;
|
2019-05-28 15:01:31 +00:00
|
|
|
}
|
2019-05-24 17:58:35 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
input = (char)input_int;
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If it's a *nix file ("\n") or a DOS file ("\r\n"), and file
|
|
|
|
* conversion isn't disabled, handle it! */
|
|
|
|
if (input == '\n') {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If it's a DOS file or a DOS/Mac file ('\r' before '\n' on
|
|
|
|
* the first line if we think it's a *nix file, or on any
|
|
|
|
* line otherwise), and file conversion isn't disabled,
|
|
|
|
* handle it! */
|
|
|
|
if ((num_lines == 0 || format != 0) && !ISSET(NO_CONVERT) &&
|
|
|
|
len > 0 && buf[len - 1] == '\r') {
|
|
|
|
if (format == 0 || format == 2)
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
/* If it's a Mac file ('\r' without '\n' on the first line if we
|
|
|
|
* think it's a *nix file, or on any line otherwise), and file
|
|
|
|
* conversion isn't disabled, handle it! */
|
|
|
|
} else if ((num_lines == 0 || format != 0) && !ISSET(NO_CONVERT) &&
|
|
|
|
len > 0 && buf[len - 1] == '\r') {
|
|
|
|
/* If we currently think the file is a *nix file, set format
|
|
|
|
* to Mac. If we currently think the file is a DOS file,
|
|
|
|
* set format to both DOS and Mac. */
|
|
|
|
if (format == 0 || format == 1)
|
|
|
|
format += 2;
|
2005-03-31 17:00:43 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
} else {
|
|
|
|
/* Store the character. */
|
|
|
|
buf[len] = input;
|
|
|
|
|
|
|
|
/* Keep track of the total length of the line. It might have
|
|
|
|
* nulls in it, so we can't just use strlen() later. */
|
|
|
|
len++;
|
|
|
|
|
|
|
|
/* If needed, increase the buffer size, MAX_BUF_SIZE characters at
|
|
|
|
* a time. Don't bother decreasing it; it is freed at the end. */
|
|
|
|
if (len == bufx) {
|
|
|
|
bufx += MAX_BUF_SIZE;
|
|
|
|
buf = charealloc(buf, bufx);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-09 18:26:27 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If it's a DOS or Mac line, strip the '\r' from it. */
|
|
|
|
if (len > 0 && buf[len - 1] == '\r' && !ISSET(NO_CONVERT))
|
|
|
|
buf[--len] = '\0';
|
2017-02-09 18:26:27 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Store the data and make a new line. */
|
|
|
|
bottomline->data = encode_data(buf, len);
|
|
|
|
bottomline->next = make_new_node(bottomline);
|
|
|
|
bottomline = bottomline->next;
|
|
|
|
num_lines++;
|
2017-02-09 18:26:27 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Reset the length in preparation for the next line. */
|
|
|
|
len = 0;
|
2017-02-09 18:26:27 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If it happens to be a Mac line, store the character after the \r
|
|
|
|
* as the first character of the next line. */
|
|
|
|
if (input != '\n')
|
|
|
|
buf[len++] = input;
|
2017-02-09 18:26:27 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2017-02-09 18:26:27 +00:00
|
|
|
|
2019-05-28 09:06:42 +00:00
|
|
|
errornumber = errno;
|
|
|
|
|
2018-06-08 16:38:19 +00:00
|
|
|
/* We are done with the file, unlock it. */
|
|
|
|
funlockfile(f);
|
|
|
|
|
2019-03-31 23:17:45 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
block_sigwinch(FALSE);
|
|
|
|
#endif
|
|
|
|
|
2019-05-28 09:06:42 +00:00
|
|
|
/* When reading from stdin, restore the terminal and reenter curses mode. */
|
|
|
|
if (isendwin()) {
|
2019-05-28 10:31:16 +00:00
|
|
|
if (!isatty(STANDARD_INPUT))
|
2019-05-28 09:06:42 +00:00
|
|
|
reconnect_and_store_state();
|
|
|
|
terminal_init();
|
|
|
|
doupdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there was a real error during the reading, let the user know. */
|
|
|
|
if (ferror(f) && errornumber != EINTR && errornumber != 0)
|
|
|
|
statusline(ALERT, strerror(errornumber));
|
2017-12-29 18:27:33 +00:00
|
|
|
fclose(f);
|
2019-05-28 09:06:42 +00:00
|
|
|
|
2018-03-22 09:41:39 +00:00
|
|
|
if (fd > 0 && !undoable) {
|
2017-12-29 18:27:33 +00:00
|
|
|
close(fd);
|
2019-05-12 08:36:15 +00:00
|
|
|
writable = (ISSET(VIEW_MODE) || access(filename, W_OK) == 0);
|
2005-11-05 17:20:39 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If the file ended with newline, or it was entirely empty, make the
|
|
|
|
* last line blank. Otherwise, put the last read data in. */
|
|
|
|
if (len == 0)
|
2019-10-13 10:24:27 +00:00
|
|
|
bottomline->data = copy_of("");
|
2017-12-29 18:27:33 +00:00
|
|
|
else {
|
|
|
|
bool mac_line_needs_newline = FALSE;
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If the final character is '\r', and file conversion isn't disabled,
|
|
|
|
* set format to Mac if we currently think the file is a *nix file, or
|
|
|
|
* to DOS-and-Mac if we currently think it is a DOS file. */
|
|
|
|
if (buf[len - 1] == '\r' && !ISSET(NO_CONVERT)) {
|
|
|
|
if (format < 2)
|
|
|
|
format += 2;
|
|
|
|
|
|
|
|
/* Strip the carriage return. */
|
|
|
|
buf[--len] = '\0';
|
|
|
|
|
|
|
|
/* Indicate we need to put a blank line in after this one. */
|
|
|
|
mac_line_needs_newline = TRUE;
|
|
|
|
}
|
2017-02-09 18:26:27 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Store the data of the final line. */
|
|
|
|
bottomline->data = encode_data(buf, len);
|
|
|
|
num_lines++;
|
|
|
|
|
|
|
|
if (mac_line_needs_newline) {
|
|
|
|
bottomline->next = make_new_node(bottomline);
|
|
|
|
bottomline = bottomline->next;
|
2019-10-13 10:24:27 +00:00
|
|
|
bottomline->data = copy_of("");
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-11-05 17:20:39 +00:00
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(buf);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Insert the just read buffer into the current one. */
|
|
|
|
ingraft_buffer(topline);
|
2005-11-05 17:20:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Set the desired x position at the end of what was inserted. */
|
|
|
|
openfile->placewewant = xplustabs();
|
2005-11-05 17:20:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (!writable)
|
|
|
|
statusline(ALERT, _("File '%s' is unwritable"), filename);
|
2016-09-11 19:40:50 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
else if (format == 3) {
|
|
|
|
/* TRANSLATORS: Keep the next four messages at most 78 characters. */
|
|
|
|
statusline(HUSH, P_("Read %zu line (Converted from DOS and Mac format)",
|
|
|
|
"Read %zu lines (Converted from DOS and Mac format)",
|
|
|
|
num_lines), num_lines);
|
|
|
|
} else if (format == 2) {
|
|
|
|
openfile->fmt = MAC_FILE;
|
|
|
|
statusline(HUSH, P_("Read %zu line (Converted from Mac format)",
|
|
|
|
"Read %zu lines (Converted from Mac format)",
|
|
|
|
num_lines), num_lines);
|
|
|
|
} else if (format == 1) {
|
|
|
|
openfile->fmt = DOS_FILE;
|
|
|
|
statusline(HUSH, P_("Read %zu line (Converted from DOS format)",
|
|
|
|
"Read %zu lines (Converted from DOS format)",
|
|
|
|
num_lines), num_lines);
|
|
|
|
}
|
2016-09-11 19:43:47 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
|
|
|
statusline(HUSH, P_("Read %zu line", "Read %zu lines",
|
|
|
|
num_lines), num_lines);
|
2015-08-04 18:49:57 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we inserted less than a screenful, don't center the cursor. */
|
|
|
|
if (undoable && less_than_a_screenful(was_lineno, was_leftedge))
|
|
|
|
focusing = FALSE;
|
2016-04-12 08:24:57 +00:00
|
|
|
|
2015-08-09 16:31:01 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (undoable)
|
|
|
|
update_undo(INSERT);
|
2016-09-11 19:43:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(MAKE_IT_UNIX))
|
|
|
|
openfile->fmt = NIX_FILE;
|
2015-08-09 16:31:01 +00:00
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2002-05-11 03:04:44 +00:00
|
|
|
|
2016-11-26 16:41:31 +00:00
|
|
|
/* Open the file with the given name. If the file does not exist, display
|
|
|
|
* "New File" if newfie is TRUE, and say "File not found" otherwise.
|
2009-09-03 05:45:13 +00:00
|
|
|
* Return -2 if we say "New File", -1 if the file isn't opened, and the
|
2016-11-26 16:41:31 +00:00
|
|
|
* obtained fd otherwise. *f is set to the opened file. */
|
2019-05-24 10:42:48 +00:00
|
|
|
int open_file(const char *filename, bool newfie, FILE **f)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
struct stat fileinfo, fileinfo2;
|
|
|
|
int fd;
|
|
|
|
char *full_filename = get_full_path(filename);
|
|
|
|
|
|
|
|
/* If the full path is unusable (due to some component's permissions),
|
|
|
|
* but the relative path is okay, then just use that one. */
|
|
|
|
if (full_filename == NULL || (stat(full_filename, &fileinfo) == -1 &&
|
|
|
|
stat(filename, &fileinfo2) != -1))
|
|
|
|
full_filename = mallocstrcpy(full_filename, filename);
|
|
|
|
|
|
|
|
if (stat(full_filename, &fileinfo) == -1) {
|
|
|
|
if (newfie) {
|
2019-05-24 10:42:48 +00:00
|
|
|
statusbar(_("New File"));
|
2017-12-29 18:27:33 +00:00
|
|
|
free(full_filename);
|
|
|
|
return -2;
|
|
|
|
}
|
2016-11-26 16:41:31 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(ALERT, _("File \"%s\" not found"), filename);
|
|
|
|
free(full_filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-26 16:41:31 +00:00
|
|
|
|
2019-05-27 07:55:24 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-18 17:09:02 +00:00
|
|
|
if (S_ISFIFO(fileinfo.st_mode))
|
|
|
|
statusbar(_("Reading from FIFO..."));
|
|
|
|
|
2019-05-20 11:07:09 +00:00
|
|
|
block_sigwinch(TRUE);
|
2019-05-26 11:32:28 +00:00
|
|
|
install_handler_for_Ctrl_C();
|
2019-05-24 08:39:33 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Try opening the file. */
|
|
|
|
fd = open(full_filename, O_RDONLY);
|
2004-10-05 20:11:31 +00:00
|
|
|
|
2019-05-24 08:39:33 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-26 11:32:28 +00:00
|
|
|
restore_handler_for_Ctrl_C();
|
2019-05-20 11:07:09 +00:00
|
|
|
block_sigwinch(FALSE);
|
2019-05-24 08:39:33 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-26 12:17:35 +00:00
|
|
|
if (fd == -1) {
|
2019-05-26 17:45:58 +00:00
|
|
|
if (errno == EINTR || errno == 0)
|
2019-05-26 12:17:35 +00:00
|
|
|
statusline(ALERT, _("Interrupted"));
|
|
|
|
else
|
|
|
|
statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno));
|
|
|
|
} else {
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The file is A-OK. Associate a stream with it. */
|
|
|
|
*f = fdopen(fd, "rb");
|
|
|
|
|
|
|
|
if (*f == NULL) {
|
|
|
|
statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno));
|
|
|
|
close(fd);
|
2019-05-24 15:22:04 +00:00
|
|
|
fd = -1;
|
2019-05-29 09:42:23 +00:00
|
|
|
} else
|
2019-05-18 17:09:02 +00:00
|
|
|
statusbar(_("Reading..."));
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-07-20 21:08:38 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(full_filename);
|
2007-04-18 18:22:13 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return fd;
|
2009-09-03 05:45:13 +00:00
|
|
|
}
|
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
/* This function will return the name of the first available extension
|
|
|
|
* of a filename (starting with [name][suffix], then [name][suffix].1,
|
|
|
|
* etc.). Memory is allocated for the return value. If no writable
|
|
|
|
* extension exists, we return "". */
|
|
|
|
char *get_next_filename(const char *name, const char *suffix)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
unsigned long i = 0;
|
|
|
|
char *buf;
|
|
|
|
size_t wholenamelen;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
wholenamelen = strlen(name) + strlen(suffix);
|
2005-11-09 15:13:00 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Reserve space for: the name plus the suffix plus a dot plus
|
|
|
|
* possibly five digits plus a null byte. */
|
|
|
|
buf = charalloc(wholenamelen + 7);
|
|
|
|
sprintf(buf, "%s%s", name, suffix);
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
|
|
|
struct stat fs;
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (stat(buf, &fs) == -1)
|
|
|
|
return buf;
|
2015-05-08 21:11:30 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Limit the number of backup files to a hundred thousand. */
|
|
|
|
if (++i == 100000)
|
|
|
|
break;
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
sprintf(buf + wholenamelen, ".%lu", i);
|
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* There is no possible save file: blank out the filename. */
|
|
|
|
*buf = '\0';
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return buf;
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 18:06:45 +00:00
|
|
|
/* Insert a file into the current buffer, or into a new buffer when
|
|
|
|
* the MULTIBUFFER flag is set. */
|
|
|
|
void do_insertfile(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2019-05-08 17:17:16 +00:00
|
|
|
int response;
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *msg;
|
2019-10-13 10:24:27 +00:00
|
|
|
char *given = copy_of("");
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The last answer the user typed at the statusbar prompt. */
|
2014-06-20 15:35:26 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-02 15:09:22 +00:00
|
|
|
format_type was_fmt = openfile->fmt;
|
2017-12-29 18:27:33 +00:00
|
|
|
bool execute = FALSE;
|
2007-08-16 02:34:23 +00:00
|
|
|
#endif
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Display newlines in filenames as ^J. */
|
|
|
|
as_an_at = FALSE;
|
2016-12-20 18:27:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (execute) {
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(MULTIBUFFER))
|
2018-08-29 18:05:37 +00:00
|
|
|
/* TRANSLATORS: The next six messages are prompts. */
|
2017-12-29 18:27:33 +00:00
|
|
|
msg = _("Command to execute in new buffer");
|
|
|
|
else
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
msg = _("Command to execute");
|
|
|
|
} else
|
2017-11-12 19:08:28 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
{
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(MULTIBUFFER))
|
2018-08-19 10:35:15 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if ISSET(NO_CONVERT)
|
|
|
|
msg = _("File to read unconverted into new buffer [from %s]");
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
msg = _("File to read into new buffer [from %s]");
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
2017-03-23 21:03:21 +00:00
|
|
|
#endif
|
2018-08-19 10:35:15 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if ISSET(NO_CONVERT)
|
|
|
|
msg = _("File to insert unconverted [from %s]");
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
msg = _("File to insert [from %s]");
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2004-02-06 21:20:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
present_path = mallocstrcpy(present_path, "./");
|
2016-04-30 19:22:16 +00:00
|
|
|
|
2019-05-08 17:17:16 +00:00
|
|
|
response = do_prompt(TRUE, TRUE,
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-08 17:32:30 +00:00
|
|
|
execute ? MEXTCMD :
|
2004-11-25 04:39:07 +00:00
|
|
|
#endif
|
2019-05-08 17:32:30 +00:00
|
|
|
MINSERTFILE, given,
|
2017-10-29 09:30:01 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-08 17:32:30 +00:00
|
|
|
execute ? &execute_history :
|
2017-10-29 09:30:01 +00:00
|
|
|
#endif
|
2019-05-08 17:32:30 +00:00
|
|
|
NULL, edit_refresh, msg,
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2019-05-08 17:32:30 +00:00
|
|
|
operating_dir != NULL ? operating_dir :
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2019-05-08 17:32:30 +00:00
|
|
|
"./");
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If we're in multibuffer mode and the filename or command is
|
|
|
|
* blank, open a new buffer instead of canceling. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response == -1 || (response == -2 && !ISSET(MULTIBUFFER))) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ssize_t was_current_lineno = openfile->current->lineno;
|
|
|
|
size_t was_current_x = openfile->current_x;
|
2017-05-08 17:08:23 +00:00
|
|
|
#if !defined(NANO_TINY) || defined(ENABLE_BROWSER) || defined(ENABLE_MULTIBUFFER)
|
2019-05-08 17:17:16 +00:00
|
|
|
functionptrtype func = func_from_key(&response);
|
2015-12-05 11:38:26 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
given = mallocstrcpy(given, answer);
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func == flip_newbuffer) {
|
|
|
|
/* Allow toggling only when not in view mode. */
|
|
|
|
if (!ISSET(VIEW_MODE))
|
|
|
|
TOGGLE(MULTIBUFFER);
|
|
|
|
else
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
2017-05-08 11:20:07 +00:00
|
|
|
#ifndef NANO_TINY
|
2018-08-19 10:35:15 +00:00
|
|
|
if (func == flip_convert) {
|
|
|
|
TOGGLE(NO_CONVERT);
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func == flip_execute) {
|
|
|
|
execute = !execute;
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-02 13:07:04 +00:00
|
|
|
if (func == flip_pipe) {
|
|
|
|
add_or_remove_pipe_symbol_from_answer();
|
|
|
|
given = mallocstrcpy(given, answer);
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-08 11:20:07 +00:00
|
|
|
#endif
|
2017-05-08 17:08:23 +00:00
|
|
|
#ifdef ENABLE_BROWSER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func == to_files_void) {
|
|
|
|
char *chosen = do_browse_from(answer);
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If no file was chosen, go back to the prompt. */
|
|
|
|
if (chosen == NULL)
|
|
|
|
continue;
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(answer);
|
|
|
|
answer = chosen;
|
2019-05-08 17:17:16 +00:00
|
|
|
response = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2018-05-23 09:57:55 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we don't have a file yet, go back to the prompt. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response != 0 && (!ISSET(MULTIBUFFER) || response != -2))
|
2017-12-29 18:27:33 +00:00
|
|
|
continue;
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (execute) {
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When in multibuffer mode, first open a blank buffer. */
|
|
|
|
if (ISSET(MULTIBUFFER))
|
2018-03-21 10:36:00 +00:00
|
|
|
open_buffer("", TRUE);
|
2005-07-09 04:42:47 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the command is not empty, execute it and read its output
|
|
|
|
* into the buffer, and add the command to the history list. */
|
|
|
|
if (*answer != '\0') {
|
|
|
|
execute_command(answer);
|
2017-10-29 18:42:12 +00:00
|
|
|
#ifdef ENABLE_HISTORIES
|
2017-12-29 18:27:33 +00:00
|
|
|
update_history(&execute_history, answer);
|
2017-10-03 19:39:09 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2006-07-13 03:06:36 +00:00
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If this is a new buffer, put the cursor at the top. */
|
|
|
|
if (ISSET(MULTIBUFFER)) {
|
2019-03-21 16:23:49 +00:00
|
|
|
openfile->current = openfile->filetop;
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = 0;
|
|
|
|
openfile->placewewant = 0;
|
|
|
|
|
|
|
|
set_modified();
|
|
|
|
}
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
} else
|
2006-07-13 03:06:36 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2017-12-29 18:27:33 +00:00
|
|
|
{
|
|
|
|
/* Make sure the specified path is tilde-expanded. */
|
|
|
|
answer = free_and_assign(answer, real_dir_from_tilde(answer));
|
2005-07-09 04:42:47 +00:00
|
|
|
|
2018-03-21 10:36:00 +00:00
|
|
|
/* Read the file into a new buffer or into current buffer. */
|
|
|
|
open_buffer(answer, ISSET(MULTIBUFFER));
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2001-10-22 03:15:31 +00:00
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(MULTIBUFFER)) {
|
2017-10-29 18:42:12 +00:00
|
|
|
#ifdef ENABLE_HISTORIES
|
2018-09-30 11:27:08 +00:00
|
|
|
if (ISSET(POSITIONLOG)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
ssize_t priorline, priorcol;
|
2014-06-20 16:33:12 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (!execute)
|
2014-06-20 16:33:12 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
if (has_old_position(answer, &priorline, &priorcol))
|
|
|
|
do_gotolinecolumn(priorline, priorcol, FALSE, FALSE);
|
|
|
|
}
|
2017-11-13 18:58:29 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Update title bar and color info for this new buffer. */
|
|
|
|
prepare_for_display();
|
|
|
|
} else
|
2017-05-01 18:20:34 +00:00
|
|
|
#endif /* ENABLE_MULTIBUFFER */
|
2017-12-29 18:27:33 +00:00
|
|
|
{
|
|
|
|
/* If the file actually changed, mark it as modified. */
|
|
|
|
if (openfile->current->lineno != was_current_lineno ||
|
|
|
|
openfile->current_x != was_current_x)
|
|
|
|
set_modified();
|
2017-11-12 17:45:53 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Ensure that the buffer retains the format that it had. */
|
2019-10-02 15:09:22 +00:00
|
|
|
openfile->fmt = was_fmt;
|
2017-11-12 17:45:53 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2016-05-23 18:44:58 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(given);
|
2002-02-15 19:17:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 14:04:51 +00:00
|
|
|
/* If the current mode of operation allows it, go insert a file. */
|
2005-07-08 02:47:05 +00:00
|
|
|
void do_insertfile_void(void)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2019-10-09 16:58:30 +00:00
|
|
|
if (!in_restricted_mode())
|
2017-12-29 18:27:33 +00:00
|
|
|
do_insertfile();
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* For the given bare path (or path plus filename), return the canonical,
|
|
|
|
* absolute path (plus filename) when the path exists, and NULL when not. */
|
2002-09-13 18:14:04 +00:00
|
|
|
char *get_full_path(const char *origpath)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2019-09-17 15:38:42 +00:00
|
|
|
char *allocation, *here, *target, *last_slash;
|
|
|
|
char *just_filename = NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
int attempts = 0;
|
|
|
|
struct stat fileinfo;
|
|
|
|
bool path_only;
|
|
|
|
|
|
|
|
if (origpath == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-09-17 14:46:11 +00:00
|
|
|
allocation = charalloc(PATH_MAX + 1);
|
|
|
|
here = getcwd(allocation, PATH_MAX + 1);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If getting the current directory failed, go up one level and try again,
|
|
|
|
* until we find an existing directory, and use that as the current one. */
|
2019-09-17 14:39:21 +00:00
|
|
|
while (here == NULL && attempts < 20) {
|
2017-12-29 18:27:33 +00:00
|
|
|
IGNORE_CALL_RESULT(chdir(".."));
|
2019-09-17 14:46:11 +00:00
|
|
|
here = getcwd(allocation, PATH_MAX + 1);
|
2017-12-29 18:27:33 +00:00
|
|
|
attempts++;
|
2016-12-16 20:28:28 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If we found a directory, make sure its path ends in a slash. */
|
2019-09-17 14:39:21 +00:00
|
|
|
if (here != NULL) {
|
|
|
|
if (strcmp(here, "/") != 0) {
|
|
|
|
here = charealloc(here, strlen(here) + 2);
|
|
|
|
strcat(here, "/");
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-13 10:24:27 +00:00
|
|
|
here = copy_of("");
|
2019-09-17 14:46:11 +00:00
|
|
|
free(allocation);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 14:39:21 +00:00
|
|
|
target = real_dir_from_tilde(origpath);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* Determine whether the target path refers to a directory. If stat()ing
|
|
|
|
* target fails, however, assume that it refers to a new, unsaved file. */
|
2019-09-17 14:39:21 +00:00
|
|
|
path_only = (stat(target, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode));
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If the target is a directory, make sure its path ends in a slash. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (path_only) {
|
2019-09-17 15:38:42 +00:00
|
|
|
size_t length = strlen(target);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
if (target[length - 1] != '/') {
|
|
|
|
target = charealloc(target, length + 2);
|
2019-09-17 14:39:21 +00:00
|
|
|
strcat(target, "/");
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 14:39:21 +00:00
|
|
|
last_slash = strrchr(target, '/');
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If the target path does not contain a slash, then it is a bare filename
|
|
|
|
* and must therefore be located in the working directory. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (last_slash == NULL) {
|
2019-09-17 14:39:21 +00:00
|
|
|
just_filename = target;
|
|
|
|
target = here;
|
2005-02-09 18:56:21 +00:00
|
|
|
} else {
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If target contains a filename, separate the two. */
|
|
|
|
if (!path_only) {
|
2019-10-13 10:24:27 +00:00
|
|
|
just_filename = copy_of(last_slash + 1);
|
2019-09-17 15:38:42 +00:00
|
|
|
*(last_slash + 1) = '\0';
|
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If we can't change to the target directory, give up. Otherwise,
|
|
|
|
* get the canonical path to this target directory. */
|
2019-09-17 14:39:21 +00:00
|
|
|
if (chdir(target) == -1) {
|
|
|
|
free(target);
|
|
|
|
target = NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else {
|
2019-09-17 14:39:21 +00:00
|
|
|
free(target);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 14:46:11 +00:00
|
|
|
allocation = charalloc(PATH_MAX + 1);
|
|
|
|
target = getcwd(allocation, PATH_MAX + 1);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If we got a result, make sure it ends in a slash.
|
|
|
|
* Otherwise, ensure that we return NULL. */
|
2019-09-17 14:39:21 +00:00
|
|
|
if (target != NULL) {
|
|
|
|
if (strcmp(target, "/") != 0) {
|
|
|
|
target = charealloc(target, strlen(target) + 2);
|
|
|
|
strcat(target, "/");
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path_only = TRUE;
|
2019-09-17 14:46:11 +00:00
|
|
|
free(allocation);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* Finally, go back to where we were before. We don't check
|
|
|
|
* for an error, since we can't do anything if we get one. */
|
2019-09-17 14:39:21 +00:00
|
|
|
IGNORE_CALL_RESULT(chdir(here));
|
2016-12-16 20:28:28 +00:00
|
|
|
}
|
2006-07-05 14:14:06 +00:00
|
|
|
|
2019-09-17 14:39:21 +00:00
|
|
|
free(here);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 15:38:42 +00:00
|
|
|
/* If we were given more than a bare path, concatenate the target path
|
|
|
|
* with the filename portion to get the full, absolute file path. */
|
2019-09-17 14:39:21 +00:00
|
|
|
if (!path_only && target != NULL) {
|
|
|
|
target = charealloc(target, strlen(target) + strlen(just_filename) + 1);
|
|
|
|
strcat(target, just_filename);
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
2016-02-10 12:32:43 +00:00
|
|
|
|
2019-09-17 14:39:21 +00:00
|
|
|
free(just_filename);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-09-17 14:39:21 +00:00
|
|
|
return target;
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2019-10-09 12:36:28 +00:00
|
|
|
/* Check whether the given path refers to a directory that is writable.
|
|
|
|
* Return the absolute form of the path on success, and NULL on failure. */
|
2002-09-13 18:14:04 +00:00
|
|
|
char *check_writable_directory(const char *path)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
char *full_path = get_full_path(path);
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (full_path == NULL)
|
|
|
|
return NULL;
|
2002-02-16 20:03:44 +00:00
|
|
|
|
2019-10-09 12:36:28 +00:00
|
|
|
if (full_path[strlen(full_path) - 1] != '/' || access(full_path, W_OK) != 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
free(full_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return full_path;
|
2002-02-15 19:17:02 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 12:20:29 +00:00
|
|
|
/* Create, safely, a temporary file in the standard temp directory.
|
|
|
|
* On success, return the malloc()ed filename, plus the corresponding
|
|
|
|
* file stream opened in read-write mode. On error, return NULL. */
|
|
|
|
char *safe_tempfile(FILE **stream)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2019-10-09 12:20:29 +00:00
|
|
|
const char *env_dir = getenv("TMPDIR");
|
|
|
|
char *tempdir = NULL, *tempfile_name = NULL;
|
2019-10-17 09:15:34 +00:00
|
|
|
mode_t was_mask;
|
2019-02-10 16:04:55 +00:00
|
|
|
int fd;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-02-10 16:04:55 +00:00
|
|
|
/* Get the absolute path for the first directory among $TMPDIR
|
|
|
|
* and P_tmpdir that is writable, otherwise use /tmp/. */
|
2019-10-09 12:20:29 +00:00
|
|
|
if (env_dir != NULL)
|
|
|
|
tempdir = check_writable_directory(env_dir);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-10-09 12:20:29 +00:00
|
|
|
if (tempdir == NULL)
|
|
|
|
tempdir = check_writable_directory(P_tmpdir);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-10-09 12:20:29 +00:00
|
|
|
if (tempdir == NULL)
|
2019-10-13 10:24:27 +00:00
|
|
|
tempdir = copy_of("/tmp/");
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-10-09 12:20:29 +00:00
|
|
|
tempfile_name = charealloc(tempdir, strlen(tempdir) + 12);
|
2019-02-10 16:17:35 +00:00
|
|
|
strcat(tempfile_name, "nano.XXXXXX");
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2019-10-17 09:15:34 +00:00
|
|
|
was_mask = umask(S_IRWXG | S_IRWXO);
|
2005-06-05 19:33:27 +00:00
|
|
|
|
2019-02-10 16:17:35 +00:00
|
|
|
fd = mkstemp(tempfile_name);
|
2005-06-05 19:33:27 +00:00
|
|
|
|
2019-10-17 09:15:34 +00:00
|
|
|
umask(was_mask);
|
2019-10-09 12:20:29 +00:00
|
|
|
|
|
|
|
if (fd == -1) {
|
2019-02-10 16:17:35 +00:00
|
|
|
free(tempfile_name);
|
2019-10-09 12:20:29 +00:00
|
|
|
return NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2019-10-09 12:20:29 +00:00
|
|
|
*stream = fdopen(fd, "r+b");
|
2005-06-05 19:33:27 +00:00
|
|
|
|
2019-02-10 16:17:35 +00:00
|
|
|
return tempfile_name;
|
2002-02-15 19:17:02 +00:00
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-08-05 19:30:27 +00:00
|
|
|
/* Change to the specified operating directory, when it's valid. */
|
2002-09-13 18:14:04 +00:00
|
|
|
void init_operating_dir(void)
|
|
|
|
{
|
2018-12-27 20:08:57 +00:00
|
|
|
char *target = get_full_path(operating_dir);
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the operating directory is inaccessible, fail. */
|
2018-12-27 20:08:57 +00:00
|
|
|
if (target == NULL || chdir(target) == -1)
|
|
|
|
die(_("Invalid operating directory: %s\n"), operating_dir);
|
2016-12-16 20:28:28 +00:00
|
|
|
|
2018-12-27 20:08:57 +00:00
|
|
|
free(operating_dir);
|
2018-12-27 20:17:37 +00:00
|
|
|
operating_dir = charealloc(target, strlen(target) + 1);
|
2002-09-13 18:14:04 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 08:04:31 +00:00
|
|
|
/* Check whether the given path is outside of the operating directory.
|
|
|
|
* Return TRUE if it is, and FALSE otherwise. If allow_tabcomp is TRUE,
|
|
|
|
* incomplete names that can grow into matches for the operating directory
|
|
|
|
* are considered to be inside, so that tab completion will work. */
|
2017-08-12 13:52:07 +00:00
|
|
|
bool outside_of_confinement(const char *currpath, bool allow_tabcomp)
|
2001-09-19 03:19:43 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
char *fullpath;
|
|
|
|
bool is_inside, begins_to_be;
|
|
|
|
|
|
|
|
/* If no operating directory is set, there is nothing to check. */
|
|
|
|
if (operating_dir == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
fullpath = get_full_path(currpath);
|
|
|
|
|
|
|
|
/* If fullpath is NULL, it means some directory in the path doesn't
|
|
|
|
* exist or is unreadable. If allow_tabcomp is FALSE, then currpath
|
|
|
|
* is what the user typed somewhere. We don't want to report a
|
|
|
|
* non-existent directory as being outside the operating directory,
|
|
|
|
* so we return FALSE. If allow_tabcomp is TRUE, then currpath
|
|
|
|
* exists, but is not executable. So we say it is outside the
|
|
|
|
* operating directory. */
|
|
|
|
if (fullpath == NULL)
|
|
|
|
return allow_tabcomp;
|
|
|
|
|
|
|
|
is_inside = (strstr(fullpath, operating_dir) == fullpath);
|
|
|
|
begins_to_be = (allow_tabcomp &&
|
|
|
|
strstr(operating_dir, fullpath) == operating_dir);
|
|
|
|
free(fullpath);
|
|
|
|
|
|
|
|
return (!is_inside && !begins_to_be);
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
#endif
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Although this sucks, it sucks less than having a single 'my system is
|
|
|
|
* messed up and I'm blanket allowing insecure file writing operations'. */
|
2011-02-07 02:06:20 +00:00
|
|
|
int prompt_failed_backupwrite(const char *filename)
|
|
|
|
{
|
2019-02-10 15:11:57 +00:00
|
|
|
static int choice;
|
2017-12-29 20:35:14 +00:00
|
|
|
static char *prevfile = NULL;
|
2019-02-10 16:04:55 +00:00
|
|
|
/* The last filename we were passed, so we don't keep asking this. */
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (prevfile == NULL || strcmp(filename, prevfile)) {
|
2019-02-10 15:11:57 +00:00
|
|
|
choice = do_yesno_prompt(FALSE, _("Failed to write backup file; "
|
2017-12-29 18:27:33 +00:00
|
|
|
"continue saving? (Say N if unsure.) "));
|
|
|
|
prevfile = mallocstrcpy(prevfile, filename);
|
|
|
|
}
|
|
|
|
|
2019-02-10 15:11:57 +00:00
|
|
|
return choice;
|
2011-02-07 02:06:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 19:55:03 +00:00
|
|
|
/* Transform the specified backup directory to an absolute path,
|
|
|
|
* and verify that it is usable. */
|
2004-02-28 16:24:31 +00:00
|
|
|
void init_backup_dir(void)
|
|
|
|
{
|
2018-12-27 20:08:57 +00:00
|
|
|
char *target = get_full_path(backup_dir);
|
2017-08-05 19:55:03 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we can't get an absolute path (which means it doesn't exist or
|
2017-12-29 20:35:14 +00:00
|
|
|
* isn't accessible), or it's not a directory, fail. */
|
2018-12-27 20:08:57 +00:00
|
|
|
if (target == NULL || target[strlen(target) - 1] != '/')
|
|
|
|
die(_("Invalid backup directory: %s\n"), backup_dir);
|
2017-08-05 19:55:03 +00:00
|
|
|
|
2018-12-27 20:08:57 +00:00
|
|
|
free(backup_dir);
|
2018-12-27 20:17:37 +00:00
|
|
|
backup_dir = charealloc(target, strlen(target) + 1);
|
2004-02-28 16:24:31 +00:00
|
|
|
}
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2004-02-28 16:24:31 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* Read from inn, write to out. We assume inn is opened for reading,
|
2005-07-08 20:09:16 +00:00
|
|
|
* and out for writing. We return 0 on success, -1 on read error, or -2
|
2017-04-04 07:29:31 +00:00
|
|
|
* on write error. inn is always closed by this function, out is closed
|
|
|
|
* only if close_out is true. */
|
|
|
|
int copy_file(FILE *inn, FILE *out, bool close_out)
|
2003-12-24 08:03:54 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
int retval = 0;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
size_t charsread;
|
|
|
|
int (*flush_out_fnc)(FILE *) = (close_out) ? fclose : fflush;
|
2005-04-19 20:13:13 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
do {
|
|
|
|
charsread = fread(buf, sizeof(char), BUFSIZ, inn);
|
|
|
|
if (charsread == 0 && ferror(inn)) {
|
|
|
|
retval = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fwrite(buf, sizeof(char), charsread, out) < charsread) {
|
|
|
|
retval = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (charsread > 0);
|
|
|
|
|
|
|
|
if (fclose(inn) == EOF)
|
|
|
|
retval = -1;
|
|
|
|
if (flush_out_fnc(out) == EOF)
|
|
|
|
retval = -2;
|
|
|
|
|
|
|
|
return retval;
|
2003-12-24 08:03:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:07:41 +00:00
|
|
|
/* Write the current buffer to disk. If stream isn't NULL, we write to a
|
2019-10-16 11:47:30 +00:00
|
|
|
* temporary file that is already open. If tmp is TRUE (when spell checking
|
|
|
|
* or emergency dumping, for example), we set the umask to disallow anyone else
|
|
|
|
* from accessing the file, and don't print out how many lines we wrote on the
|
|
|
|
* status bar. If method is APPEND or PREPEND, it means we will be appending
|
|
|
|
* or prepending instead of overwriting the given file. If fullbuffer is TRUE
|
|
|
|
* and when writing normally, we set the current filename and stat info.
|
|
|
|
* Return TRUE on success, and FALSE otherwise. */
|
2019-10-16 11:07:41 +00:00
|
|
|
bool write_file(const char *name, FILE *stream, bool tmp,
|
2017-12-29 18:27:33 +00:00
|
|
|
kind_of_writing_type method, bool fullbuffer)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2014-06-09 20:26:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-06-12 09:03:03 +00:00
|
|
|
bool isactualfile = FALSE;
|
2019-10-16 11:47:30 +00:00
|
|
|
/* Becomes TRUE when the file is non-temporary and exists. */
|
2016-02-07 13:03:48 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
struct stat st;
|
|
|
|
/* The status fields filled in by stat(). */
|
2019-10-17 09:15:34 +00:00
|
|
|
mode_t was_mask = 0;
|
|
|
|
/* Will be set to the umask from when nano was started. */
|
2017-12-29 18:27:33 +00:00
|
|
|
char *realname;
|
|
|
|
/* The filename after tilde expansion. */
|
2019-10-16 11:07:41 +00:00
|
|
|
FILE *f = stream;
|
2019-10-16 11:47:30 +00:00
|
|
|
/* The actual file, corresponding to realname, we are writing to. */
|
2017-12-29 18:27:33 +00:00
|
|
|
char *tempname = NULL;
|
|
|
|
/* The name of the temporary file we write to on prepend. */
|
2019-10-16 09:04:26 +00:00
|
|
|
linestruct *line = openfile->filetop;
|
2019-10-16 08:47:44 +00:00
|
|
|
/* An iterator for moving through the lines of the buffer. */
|
|
|
|
size_t lineswritten = 0;
|
|
|
|
/* The number of lines written, for feedback on the status bar. */
|
2019-10-16 11:47:30 +00:00
|
|
|
bool retval = FALSE;
|
|
|
|
/* The return value, to become TRUE when writing has succeeded. */
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (*name == '\0')
|
2019-10-16 10:27:27 +00:00
|
|
|
die("Tried to write a nameless file");
|
2000-11-24 14:02:57 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
realname = real_dir_from_tilde(name);
|
2006-11-27 05:03:54 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're writing a temporary file, we're probably going outside
|
|
|
|
* the operating directory, so skip the operating directory test. */
|
|
|
|
if (!tmp && outside_of_confinement(realname, FALSE)) {
|
|
|
|
statusline(ALERT, _("Can't write outside of %s"), operating_dir);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the temp file exists and isn't already open, give up. */
|
2019-10-16 11:07:41 +00:00
|
|
|
if (tmp && (lstat(realname, &st) != -1) && stream == NULL)
|
2017-12-29 18:27:33 +00:00
|
|
|
goto cleanup_and_exit;
|
2004-11-26 20:17:49 +00:00
|
|
|
|
2014-06-09 20:26:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Check whether the file (at the end of the symlink) exists. */
|
2019-06-12 08:48:03 +00:00
|
|
|
if (!tmp)
|
2019-06-12 09:03:03 +00:00
|
|
|
isactualfile = (stat(realname, &st) != -1);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-06-12 09:10:27 +00:00
|
|
|
/* If we haven't stat()d this file before (say, the user just specified
|
|
|
|
* it interactively), stat and save the value now, or else we will chase
|
|
|
|
* null pointers when we do modtime checks and such during backup. */
|
2019-06-12 09:03:03 +00:00
|
|
|
if (openfile->current_stat == NULL && isactualfile)
|
2017-12-29 18:27:33 +00:00
|
|
|
stat_with_alloc(realname, &openfile->current_stat);
|
|
|
|
|
2019-06-12 09:10:27 +00:00
|
|
|
/* We back up only if the backup toggle is set, and the file exists and
|
|
|
|
* isn't temporary. Furthermore, if we aren't appending, prepending, or
|
|
|
|
* writing a selection, we back up only if the file has not been modified
|
|
|
|
* by someone else since nano opened it. */
|
2019-06-12 09:03:03 +00:00
|
|
|
if (ISSET(BACKUP_FILE) && isactualfile && openfile->current_stat &&
|
2017-12-29 18:27:33 +00:00
|
|
|
(method != OVERWRITE || openfile->mark ||
|
|
|
|
openfile->current_stat->st_mtime == st.st_mtime)) {
|
|
|
|
static struct timespec filetime[2];
|
|
|
|
char *backupname;
|
|
|
|
int backup_cflags, backup_fd;
|
2019-10-17 09:49:21 +00:00
|
|
|
FILE *original = NULL, *backup_file = NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* Save the original file's access and modification times. */
|
|
|
|
filetime[0].tv_sec = openfile->current_stat->st_atime;
|
|
|
|
filetime[1].tv_sec = openfile->current_stat->st_mtime;
|
|
|
|
|
2019-10-17 09:31:34 +00:00
|
|
|
/* Open the file of which a backup must be made. */
|
2019-10-17 09:49:21 +00:00
|
|
|
original = fopen(realname, "rb");
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-10-17 09:49:21 +00:00
|
|
|
if (original == NULL) {
|
2019-10-17 09:31:34 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
/* If we can't read from the original file, go on, since saving
|
|
|
|
* only the current buffer is better than saving nothing. */
|
|
|
|
goto skip_backup;
|
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If backup_dir is set, we set backupname to
|
|
|
|
* backup_dir/backupname~[.number], where backupname is the
|
|
|
|
* canonicalized absolute pathname of realname with every '/'
|
|
|
|
* replaced with a '!'. This means that /home/foo/file is
|
|
|
|
* backed up in backup_dir/!home!foo!file~[.number]. */
|
|
|
|
if (backup_dir != NULL) {
|
|
|
|
char *backuptemp = get_full_path(realname);
|
|
|
|
|
|
|
|
/* If we can't get a canonical absolute path, just use the
|
|
|
|
* filename portion of the given path. Otherwise, replace
|
|
|
|
* slashes with exclamation marks in the full path. */
|
|
|
|
if (backuptemp == NULL)
|
2019-10-13 10:24:27 +00:00
|
|
|
backuptemp = copy_of(tail(realname));
|
2017-12-29 18:27:33 +00:00
|
|
|
else {
|
2019-10-16 08:47:44 +00:00
|
|
|
for (int i = 0; backuptemp[i] != '\0'; i++)
|
2017-12-29 18:27:33 +00:00
|
|
|
if (backuptemp[i] == '/')
|
|
|
|
backuptemp[i] = '!';
|
|
|
|
}
|
|
|
|
|
|
|
|
backupname = charalloc(strlen(backup_dir) + strlen(backuptemp) + 1);
|
|
|
|
sprintf(backupname, "%s%s", backup_dir, backuptemp);
|
|
|
|
free(backuptemp);
|
|
|
|
backuptemp = get_next_filename(backupname, "~");
|
|
|
|
if (*backuptemp == '\0') {
|
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, _("Too many backup files?"));
|
|
|
|
free(backuptemp);
|
|
|
|
free(backupname);
|
|
|
|
/* If we can't write to the backup, DON'T go on, since
|
|
|
|
* whatever caused the backup-file write to fail (e.g.
|
|
|
|
* disk full) may well cause the real file write to fail
|
|
|
|
* too, which means we could lose the original! */
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
} else {
|
|
|
|
free(backupname);
|
|
|
|
backupname = backuptemp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
backupname = charalloc(strlen(realname) + 2);
|
|
|
|
sprintf(backupname, "%s~", realname);
|
2004-02-28 16:24:31 +00:00
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* First, unlink any existing backups. Next, open the backup
|
|
|
|
* file with O_CREAT and O_EXCL. If it succeeds, we have a file
|
|
|
|
* descriptor to a new backup file. */
|
|
|
|
if (unlink(backupname) < 0 && errno != ENOENT && !ISSET(INSECURE_BACKUP)) {
|
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
|
|
|
free(backupname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2010-04-07 05:48:24 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(INSECURE_BACKUP))
|
|
|
|
backup_cflags = O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
else
|
|
|
|
backup_cflags = O_WRONLY | O_CREAT | O_EXCL | O_APPEND;
|
2010-06-24 14:47:00 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
backup_fd = open(backupname, backup_cflags,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
2010-04-07 05:48:24 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (backup_fd >= 0)
|
|
|
|
backup_file = fdopen(backup_fd, "wb");
|
2017-08-15 11:46:20 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (backup_file == NULL) {
|
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
|
|
|
free(backupname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-04-19 21:47:01 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Only try chowning the backup when we're root. */
|
|
|
|
if (geteuid() == NANO_ROOT_UID &&
|
|
|
|
fchown(backup_fd, openfile->current_stat->st_uid,
|
|
|
|
openfile->current_stat->st_gid) == -1 &&
|
|
|
|
!ISSET(INSECURE_BACKUP)) {
|
|
|
|
fclose(backup_file);
|
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
|
|
|
free(backupname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2010-05-23 04:30:23 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Set the backup's mode bits. */
|
|
|
|
if (fchmod(backup_fd, openfile->current_stat->st_mode) == -1 &&
|
|
|
|
!ISSET(INSECURE_BACKUP)) {
|
|
|
|
fclose(backup_file);
|
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
|
|
|
free(backupname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Copy the file. */
|
2019-10-17 09:49:21 +00:00
|
|
|
if (copy_file(original, backup_file, FALSE) != 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
fclose(backup_file);
|
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2010-06-24 14:47:00 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* And set the backup's timestamps. */
|
|
|
|
if (futimens(backup_fd, filetime) == -1 && !ISSET(INSECURE_BACKUP)) {
|
|
|
|
fclose(backup_file);
|
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
fclose(backup_file);
|
|
|
|
free(backupname);
|
|
|
|
}
|
2006-04-06 05:18:23 +00:00
|
|
|
|
2019-06-12 09:10:27 +00:00
|
|
|
skip_backup:
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2000-07-21 22:42:46 +00:00
|
|
|
|
2019-10-16 17:17:42 +00:00
|
|
|
/* When going to create an emergency file, don't let others access it. */
|
2019-10-17 09:15:34 +00:00
|
|
|
if (stream == NULL && tmp)
|
|
|
|
was_mask = umask(S_IRWXG | S_IRWXO);
|
2006-04-07 04:37:14 +00:00
|
|
|
|
2018-07-24 17:31:03 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-16 15:16:48 +00:00
|
|
|
/* When prepending, first copy the existing file to a temporary file. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (method == PREPEND) {
|
2019-10-16 15:16:48 +00:00
|
|
|
int fd_src;
|
|
|
|
FILE *source = NULL, *target = NULL;
|
2005-04-19 21:47:01 +00:00
|
|
|
|
2019-10-16 15:40:52 +00:00
|
|
|
fd_src = open(realname, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd_src != -1) {
|
|
|
|
source = fdopen(fd_src, "rb");
|
|
|
|
if (source == NULL)
|
|
|
|
close(fd_src);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source == NULL) {
|
2019-10-16 14:05:40 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
2019-10-16 15:40:52 +00:00
|
|
|
strerror(errno));
|
2019-10-16 14:05:40 +00:00
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2019-10-16 15:16:48 +00:00
|
|
|
tempname = safe_tempfile(&target);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (tempname == NULL) {
|
|
|
|
statusline(ALERT, _("Error writing temp file: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2019-10-16 15:40:52 +00:00
|
|
|
if (copy_file(source, target, TRUE) != 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(ALERT, _("Error writing temp file: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
unlink(tempname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-05-31 04:28:15 +00:00
|
|
|
}
|
2002-04-22 23:52:34 +00:00
|
|
|
|
2019-06-12 08:34:53 +00:00
|
|
|
if (S_ISFIFO(st.st_mode))
|
2019-05-18 17:09:02 +00:00
|
|
|
statusbar(_("Writing to FIFO..."));
|
2019-05-27 07:55:24 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2019-05-18 17:09:02 +00:00
|
|
|
|
2019-10-16 11:07:41 +00:00
|
|
|
if (stream == NULL) {
|
2019-10-16 08:47:44 +00:00
|
|
|
int fd;
|
2019-05-26 09:45:51 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-20 11:07:09 +00:00
|
|
|
block_sigwinch(TRUE);
|
2019-05-26 11:32:28 +00:00
|
|
|
install_handler_for_Ctrl_C();
|
2019-05-26 09:45:51 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Now open the file in place. Use O_EXCL if tmp is TRUE. This
|
|
|
|
* is copied from joe, because wiggy says so *shrug*. */
|
|
|
|
fd = open(realname, O_WRONLY | O_CREAT | ((method == APPEND) ?
|
|
|
|
O_APPEND : (tmp ? O_EXCL : O_TRUNC)), S_IRUSR |
|
|
|
|
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
|
2019-05-26 09:45:51 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-26 11:32:28 +00:00
|
|
|
restore_handler_for_Ctrl_C();
|
2019-05-20 11:07:09 +00:00
|
|
|
block_sigwinch(FALSE);
|
2019-05-26 09:45:51 +00:00
|
|
|
#endif
|
2019-10-16 17:17:42 +00:00
|
|
|
/* When this is an emergency file, restore the original umask. */
|
|
|
|
if (tmp)
|
2019-10-17 09:15:34 +00:00
|
|
|
umask(was_mask);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If we couldn't open the file, give up. */
|
|
|
|
if (fd == -1) {
|
2019-05-26 17:45:58 +00:00
|
|
|
if (errno == EINTR || errno == 0)
|
2019-05-26 12:17:35 +00:00
|
|
|
statusline(ALERT, _("Interrupted"));
|
|
|
|
else
|
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2017-12-29 18:27:33 +00:00
|
|
|
strerror(errno));
|
|
|
|
if (tempname != NULL)
|
|
|
|
unlink(tempname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-05-28 23:32:45 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
f = fdopen(fd, (method == APPEND) ? "ab" : "wb");
|
2002-04-22 23:52:34 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
}
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2019-06-12 07:46:19 +00:00
|
|
|
if (!tmp)
|
|
|
|
statusbar(_("Writing..."));
|
2019-05-18 17:09:02 +00:00
|
|
|
|
2019-10-16 09:04:26 +00:00
|
|
|
while (line != NULL) {
|
|
|
|
size_t data_len = strlen(line->data), size;
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2018-11-05 08:38:07 +00:00
|
|
|
/* Decode LFs as the NULs that they are, before writing to disk. */
|
2019-10-16 09:04:26 +00:00
|
|
|
sunder(line->data);
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2019-10-16 09:04:26 +00:00
|
|
|
size = fwrite(line->data, sizeof(char), data_len, f);
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2018-11-05 08:38:07 +00:00
|
|
|
/* Re-encode any embedded NULs as LFs. */
|
2019-10-16 09:04:26 +00:00
|
|
|
unsunder(line->data, data_len);
|
2005-04-15 17:48:20 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (size < data_len) {
|
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
fclose(f);
|
|
|
|
goto cleanup_and_exit;
|
2005-11-04 05:44:01 +00:00
|
|
|
}
|
2001-09-22 04:20:25 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're on the last line of the file, don't write a newline
|
|
|
|
* character after it. If the last line of the file is blank,
|
|
|
|
* this means that zero bytes are written, in which case we
|
|
|
|
* don't count the last line in the total lines written. */
|
2019-10-16 09:04:26 +00:00
|
|
|
if (line == openfile->filebot) {
|
|
|
|
if (line->data[0] == '\0')
|
2017-12-29 18:27:33 +00:00
|
|
|
lineswritten--;
|
|
|
|
} else {
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (openfile->fmt == DOS_FILE || openfile->fmt == MAC_FILE) {
|
|
|
|
if (putc('\r', f) == EOF) {
|
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
fclose(f);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (openfile->fmt != MAC_FILE)
|
2001-09-21 02:37:01 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
if (putc('\n', f) == EOF) {
|
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
fclose(f);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-11-04 05:44:01 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-10-16 09:04:26 +00:00
|
|
|
line = line->next;
|
2017-12-29 18:27:33 +00:00
|
|
|
lineswritten++;
|
2005-11-04 05:44:01 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2018-07-24 17:31:03 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-16 15:16:48 +00:00
|
|
|
/* When prepending, append the temporary file to what we wrote above. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (method == PREPEND) {
|
2019-10-16 15:16:48 +00:00
|
|
|
int fd_src;
|
|
|
|
FILE *source = NULL;
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2019-10-16 15:16:48 +00:00
|
|
|
fd_src = open(tempname, O_RDONLY);
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2019-10-16 15:16:48 +00:00
|
|
|
if (fd_src != -1) {
|
|
|
|
source = fdopen(fd_src, "rb");
|
|
|
|
if (source == NULL)
|
|
|
|
close(fd_src);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2019-10-16 15:16:48 +00:00
|
|
|
if (source == NULL) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), tempname,
|
|
|
|
strerror(errno));
|
|
|
|
fclose(f);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2019-10-16 15:16:48 +00:00
|
|
|
if (copy_file(source, f, TRUE) != 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2002-04-16 03:15:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
unlink(tempname);
|
2018-07-24 17:31:03 +00:00
|
|
|
} else
|
|
|
|
#endif
|
2019-10-17 09:31:34 +00:00
|
|
|
if (fclose(f) != 0) {
|
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
|
|
|
strerror(errno));
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2015-08-11 17:43:08 +00:00
|
|
|
|
2018-05-31 14:27:13 +00:00
|
|
|
/* When having written an entire buffer, update some administrivia. */
|
|
|
|
if (fullbuffer && method == OVERWRITE && !tmp) {
|
|
|
|
/* If the filename was changed, check if this means a new syntax. */
|
|
|
|
if (strcmp(openfile->filename, realname) != 0) {
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *oldname, *newname;
|
2017-08-15 15:18:34 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
oldname = openfile->syntax ? openfile->syntax->name : "";
|
2016-06-08 12:17:30 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->filename = mallocstrcpy(openfile->filename, realname);
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* See if the applicable syntax has changed. */
|
|
|
|
color_update();
|
|
|
|
color_init();
|
|
|
|
|
|
|
|
newname = openfile->syntax ? openfile->syntax->name : "";
|
|
|
|
|
|
|
|
/* If the syntax changed, discard and recompute the multidata. */
|
|
|
|
if (strcmp(oldname, newname) != 0) {
|
2019-10-16 09:11:19 +00:00
|
|
|
linestruct *lin = openfile->filetop;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-10-16 09:11:19 +00:00
|
|
|
while (lin != NULL) {
|
|
|
|
free(lin->multidata);
|
|
|
|
lin->multidata = NULL;
|
|
|
|
lin = lin->next;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2018-05-31 14:27:13 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
precalc_multicolorinfo();
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
}
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2018-05-31 14:27:13 +00:00
|
|
|
/* Get or update the stat info to reflect the current state. */
|
|
|
|
stat_with_alloc(realname, &openfile->current_stat);
|
2017-12-18 19:08:06 +00:00
|
|
|
|
2018-05-31 14:27:13 +00:00
|
|
|
/* Record at which point in the undo stack the file was saved. */
|
|
|
|
openfile->last_saved = openfile->current_undo;
|
|
|
|
openfile->last_action = OTHER;
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
2018-05-31 14:27:13 +00:00
|
|
|
openfile->modified = FALSE;
|
|
|
|
titlebar(NULL);
|
2018-05-31 14:03:05 +00:00
|
|
|
}
|
2005-07-15 19:37:32 +00:00
|
|
|
|
2018-05-31 14:03:05 +00:00
|
|
|
if (!tmp)
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(HUSH, P_("Wrote %zu line", "Wrote %zu lines",
|
2018-05-31 14:27:13 +00:00
|
|
|
lineswritten), lineswritten);
|
2017-12-29 18:27:33 +00:00
|
|
|
retval = TRUE;
|
2002-09-13 18:14:04 +00:00
|
|
|
|
|
|
|
cleanup_and_exit:
|
2017-12-29 18:27:33 +00:00
|
|
|
free(realname);
|
|
|
|
free(tempname);
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return retval;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2007-01-11 21:52:29 +00:00
|
|
|
/* Write a marked selection from a file out to disk. Return TRUE on
|
|
|
|
* success or FALSE on error. */
|
2019-10-16 11:07:41 +00:00
|
|
|
bool write_marked_file(const char *name, FILE *stream, bool tmp,
|
2017-12-29 18:27:33 +00:00
|
|
|
kind_of_writing_type method)
|
2004-01-23 19:34:03 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
bool retval;
|
|
|
|
bool added_magicline = FALSE;
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *top, *bot;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t top_x, bot_x;
|
|
|
|
|
|
|
|
/* Partition the buffer so that it contains only the marked text. */
|
2019-04-28 09:01:51 +00:00
|
|
|
get_region((const linestruct **)&top, &top_x,
|
2019-03-21 16:08:52 +00:00
|
|
|
(const linestruct **)&bot, &bot_x, NULL);
|
2019-05-31 17:00:20 +00:00
|
|
|
partition_buffer(top, top_x, bot, bot_x);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-01-06 14:35:31 +00:00
|
|
|
/* If we are using a magic line, and the last line of the partition
|
2017-12-29 18:27:33 +00:00
|
|
|
* isn't blank, then add a newline at the end of the buffer. */
|
2019-04-07 06:47:29 +00:00
|
|
|
if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') {
|
2017-12-29 18:27:33 +00:00
|
|
|
new_magicline();
|
|
|
|
added_magicline = TRUE;
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:07:41 +00:00
|
|
|
retval = write_file(name, stream, tmp, method, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (added_magicline)
|
|
|
|
remove_magicline();
|
|
|
|
|
|
|
|
/* Unpartition the buffer so that it contains all the text again. */
|
2019-05-31 17:00:20 +00:00
|
|
|
unpartition_buffer();
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
return retval;
|
2004-01-23 19:34:03 +00:00
|
|
|
}
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Write the current file to disk. If the mark is on, write the current
|
2015-12-23 16:34:44 +00:00
|
|
|
* marked selection to disk. If exiting is TRUE, write the entire file
|
2017-10-14 09:55:09 +00:00
|
|
|
* to disk regardless of whether the mark is on. Do not ask for a name
|
|
|
|
* when withprompt is FALSE nor when the TEMP_FILE flag is set and the
|
|
|
|
* file already has a name. Return 0 on error, 1 on success, and 2 when
|
|
|
|
* the buffer is to be discarded. */
|
2017-09-27 20:42:48 +00:00
|
|
|
int do_writeout(bool exiting, bool withprompt)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
bool result = FALSE;
|
|
|
|
kind_of_writing_type method = OVERWRITE;
|
|
|
|
char *given;
|
|
|
|
/* The filename we offer, or what the user typed so far. */
|
|
|
|
bool maychange = (openfile->filename[0] == '\0');
|
|
|
|
/* Whether it's okay to save the file under a different name. */
|
2017-11-01 19:33:14 +00:00
|
|
|
#ifdef ENABLE_EXTRA
|
2017-12-29 18:27:33 +00:00
|
|
|
static bool did_credits = FALSE;
|
2000-11-24 20:45:14 +00:00
|
|
|
#endif
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Display newlines in filenames as ^J. */
|
|
|
|
as_an_at = FALSE;
|
2016-12-20 18:27:41 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-13 14:11:15 +00:00
|
|
|
given = copy_of((openfile->mark && !exiting) ? "" : openfile->filename);
|
|
|
|
#else
|
|
|
|
given = copy_of(openfile->filename);
|
2004-03-15 20:26:30 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
|
|
|
const char *msg;
|
2019-03-28 21:28:47 +00:00
|
|
|
int response = 0, choice = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
functionptrtype func;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *formatstr, *backupstr;
|
|
|
|
|
|
|
|
formatstr = (openfile->fmt == DOS_FILE) ? _(" [DOS Format]") :
|
|
|
|
(openfile->fmt == MAC_FILE) ? _(" [Mac Format]") : "";
|
|
|
|
backupstr = ISSET(BACKUP_FILE) ? _(" [Backup]") : "";
|
|
|
|
|
|
|
|
/* When the mark is on, offer to write the selection to disk, but
|
|
|
|
* not when in restricted mode, because it would allow writing to
|
|
|
|
* a file not specified on the command line. */
|
|
|
|
if (openfile->mark && !exiting && !ISSET(RESTRICTED))
|
|
|
|
/* TRANSLATORS: The next six strings are prompts. */
|
|
|
|
msg = (method == PREPEND) ? _("Prepend Selection to File") :
|
|
|
|
(method == APPEND) ? _("Append Selection to File") :
|
|
|
|
_("Write Selection to File");
|
|
|
|
else if (method != OVERWRITE)
|
|
|
|
msg = (method == PREPEND) ? _("File Name to Prepend to") :
|
|
|
|
_("File Name to Append to");
|
|
|
|
else
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2017-12-29 18:27:33 +00:00
|
|
|
msg = _("File Name to Write");
|
|
|
|
|
|
|
|
present_path = mallocstrcpy(present_path, "./");
|
|
|
|
|
|
|
|
/* When we shouldn't prompt, use the existing filename. */
|
|
|
|
if ((!withprompt || (ISSET(TEMP_FILE) && exiting)) &&
|
|
|
|
openfile->filename[0] != '\0')
|
|
|
|
answer = mallocstrcpy(answer, openfile->filename);
|
|
|
|
else {
|
|
|
|
/* Ask for (confirmation of) the filename. Disable tab completion
|
|
|
|
* when using restricted mode and the filename isn't blank. */
|
2019-02-20 18:24:18 +00:00
|
|
|
response = do_prompt(!ISSET(RESTRICTED) || openfile->filename[0] == '\0',
|
2017-12-29 18:27:33 +00:00
|
|
|
TRUE, MWRITEFILE, given, NULL,
|
|
|
|
edit_refresh, "%s%s%s", msg,
|
2006-02-18 21:32:29 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
formatstr, backupstr
|
2004-03-15 20:26:30 +00:00
|
|
|
#else
|
2017-12-29 18:27:33 +00:00
|
|
|
"", ""
|
2004-03-15 20:26:30 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
);
|
|
|
|
}
|
2004-03-15 20:26:30 +00:00
|
|
|
|
2019-02-20 18:24:18 +00:00
|
|
|
if (response < 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
break;
|
|
|
|
}
|
2017-11-22 18:26:47 +00:00
|
|
|
|
2019-02-20 18:24:18 +00:00
|
|
|
func = func_from_key(&response);
|
2014-07-27 21:07:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Upon request, abandon the buffer. */
|
|
|
|
if (func == discard_buffer) {
|
|
|
|
free(given);
|
|
|
|
return 2;
|
|
|
|
}
|
2015-12-23 16:34:44 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
given = mallocstrcpy(given, answer);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-05-08 17:08:23 +00:00
|
|
|
#ifdef ENABLE_BROWSER
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func == to_files_void) {
|
|
|
|
char *chosen = do_browse_from(answer);
|
2001-05-21 12:56:25 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (chosen == NULL)
|
|
|
|
continue;
|
2005-07-08 20:09:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(answer);
|
|
|
|
answer = chosen;
|
|
|
|
} else
|
2017-05-08 17:08:23 +00:00
|
|
|
#endif
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func == dos_format_void) {
|
|
|
|
openfile->fmt = (openfile->fmt == DOS_FILE) ? NIX_FILE : DOS_FILE;
|
|
|
|
continue;
|
|
|
|
} else if (func == mac_format_void) {
|
|
|
|
openfile->fmt = (openfile->fmt == MAC_FILE) ? NIX_FILE : MAC_FILE;
|
|
|
|
continue;
|
|
|
|
} else if (func == backup_file_void) {
|
|
|
|
TOGGLE(BACKUP_FILE);
|
|
|
|
continue;
|
|
|
|
} else if (func == prepend_void) {
|
|
|
|
method = (method == PREPEND) ? OVERWRITE : PREPEND;
|
|
|
|
continue;
|
|
|
|
} else if (func == append_void) {
|
|
|
|
method = (method == APPEND) ? OVERWRITE : APPEND;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 11:24:41 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func == do_help_void) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-01 19:33:14 +00:00
|
|
|
#ifdef ENABLE_EXTRA
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the current file has been modified, we've pressed
|
|
|
|
* Ctrl-X at the edit window to exit, we've pressed "y" at
|
|
|
|
* the "Save modified buffer" prompt to save, we've entered
|
|
|
|
* "zzy" as the filename to save under (hence "xyzzy"), and
|
|
|
|
* this is the first time we've done this, show an Easter
|
|
|
|
* egg. Display the credits. */
|
|
|
|
if (!did_credits && exiting && !ISSET(TEMP_FILE) &&
|
|
|
|
strcasecmp(answer, "zzy") == 0) {
|
2018-07-12 18:44:36 +00:00
|
|
|
if (LINES > 5 && COLS > 31) {
|
|
|
|
do_credits();
|
|
|
|
did_credits = TRUE;
|
|
|
|
} else
|
2018-08-29 18:05:37 +00:00
|
|
|
/* TRANSLATORS: Concisely say the screen is too small. */
|
2018-07-14 10:07:25 +00:00
|
|
|
statusbar(_("Too tiny"));
|
2017-12-29 18:27:33 +00:00
|
|
|
break;
|
2017-11-22 18:43:50 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
#endif
|
2006-12-15 02:49:44 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (method == OVERWRITE) {
|
|
|
|
bool name_exists, do_warning;
|
|
|
|
char *full_answer, *full_filename;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
full_answer = get_full_path(answer);
|
|
|
|
full_filename = get_full_path(openfile->filename);
|
|
|
|
name_exists = (stat((full_answer == NULL) ?
|
|
|
|
answer : full_answer, &st) != -1);
|
|
|
|
if (openfile->filename[0] == '\0')
|
|
|
|
do_warning = name_exists;
|
|
|
|
else
|
|
|
|
do_warning = (strcmp((full_answer == NULL) ?
|
|
|
|
answer : full_answer, (full_filename == NULL) ?
|
|
|
|
openfile->filename : full_filename) != 0);
|
|
|
|
|
|
|
|
free(full_filename);
|
|
|
|
free(full_answer);
|
|
|
|
|
|
|
|
if (do_warning) {
|
|
|
|
/* When in restricted mode, we aren't allowed to overwrite
|
|
|
|
* an existing file with the current buffer, nor to change
|
|
|
|
* the name of the current file if it already has one. */
|
|
|
|
if (ISSET(RESTRICTED)) {
|
|
|
|
/* TRANSLATORS: Restricted mode forbids overwriting. */
|
|
|
|
warn_and_shortly_pause(_("File exists -- "
|
|
|
|
"cannot overwrite"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!maychange) {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (exiting || !openfile->mark)
|
2016-03-20 14:34:46 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
{
|
|
|
|
if (do_yesno_prompt(FALSE, _("Save file under "
|
|
|
|
"DIFFERENT NAME? ")) < 1)
|
|
|
|
continue;
|
|
|
|
maychange = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name_exists) {
|
|
|
|
char *question = _("File \"%s\" exists; OVERWRITE? ");
|
2018-01-25 10:00:19 +00:00
|
|
|
char *name = display_string(answer, 0,
|
2019-04-24 06:49:18 +00:00
|
|
|
COLS - breadth(question) + 1, FALSE, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
char *message = charalloc(strlen(question) +
|
2018-01-25 10:00:19 +00:00
|
|
|
strlen(name) + 1);
|
|
|
|
|
|
|
|
sprintf(message, question, name);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-02-10 14:40:08 +00:00
|
|
|
choice = do_yesno_prompt(FALSE, message);
|
2018-01-25 10:00:19 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(message);
|
2018-01-25 10:00:19 +00:00
|
|
|
free(name);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-02-10 14:40:08 +00:00
|
|
|
if (choice < 1)
|
2017-12-29 18:27:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-10-22 22:11:46 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Complain if the file exists, the name hasn't changed,
|
|
|
|
* and the stat information we had before does not match
|
|
|
|
* what we have now. */
|
|
|
|
else if (name_exists && openfile->current_stat &&
|
|
|
|
(openfile->current_stat->st_mtime < st.st_mtime ||
|
|
|
|
openfile->current_stat->st_dev != st.st_dev ||
|
|
|
|
openfile->current_stat->st_ino != st.st_ino)) {
|
|
|
|
|
|
|
|
warn_and_shortly_pause(_("File on disk has changed"));
|
|
|
|
|
2019-02-10 15:11:57 +00:00
|
|
|
choice = do_yesno_prompt(FALSE, _("File was modified "
|
2017-12-29 18:27:33 +00:00
|
|
|
"since you opened it; continue saving? "));
|
|
|
|
wipe_statusbar();
|
|
|
|
|
|
|
|
/* When in tool mode and not called by 'savefile',
|
|
|
|
* overwrite the file right here when requested. */
|
|
|
|
if (ISSET(TEMP_FILE) && withprompt) {
|
|
|
|
free(given);
|
2019-02-10 15:11:57 +00:00
|
|
|
if (choice == 1)
|
2017-12-29 20:35:14 +00:00
|
|
|
return write_file(openfile->filename, NULL,
|
|
|
|
FALSE, OVERWRITE, TRUE);
|
2019-02-10 15:11:57 +00:00
|
|
|
else if (choice == 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return 2;
|
|
|
|
else
|
|
|
|
return 0;
|
2019-02-10 15:11:57 +00:00
|
|
|
} else if (choice != 1) {
|
2017-12-29 18:27:33 +00:00
|
|
|
free(given);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 18:43:50 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Here's where we allow the selected text to be written to
|
|
|
|
* a separate file. If we're using restricted mode, this
|
|
|
|
* function is disabled, since it allows reading from or
|
|
|
|
* writing to files not specified on the command line. */
|
2007-01-11 23:10:03 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->mark && !exiting && withprompt && !ISSET(RESTRICTED))
|
|
|
|
result = write_marked_file(answer, NULL, FALSE, method);
|
|
|
|
else
|
2006-11-07 21:08:17 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
result = write_file(answer, NULL, FALSE, method, TRUE);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-10-05 20:11:31 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(given);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return result ? 1 : 0;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 16:34:44 +00:00
|
|
|
/* Write the current buffer to disk, or discard it. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_writeout_void(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the user chose to discard the buffer, close it. */
|
|
|
|
if (do_writeout(FALSE, TRUE) == 2)
|
|
|
|
close_and_go();
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2015-07-25 19:25:50 +00:00
|
|
|
/* If it has a name, write the current file to disk without prompting. */
|
|
|
|
void do_savefile(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (do_writeout(FALSE, FALSE) == 2)
|
|
|
|
close_and_go();
|
2015-07-25 19:25:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:08:38 +00:00
|
|
|
/* Convert the tilde notation when the given path begins with ~/ or ~user/.
|
|
|
|
* Return an allocated string containing the expanded path. */
|
2019-09-17 12:18:03 +00:00
|
|
|
char *real_dir_from_tilde(const char *path)
|
2000-11-24 14:00:16 +00:00
|
|
|
{
|
2019-09-17 12:18:03 +00:00
|
|
|
char *tilded, *retval;
|
2019-09-17 12:11:45 +00:00
|
|
|
size_t i = 1;
|
2001-01-23 03:09:15 +00:00
|
|
|
|
2019-09-17 12:18:03 +00:00
|
|
|
if (*path != '~')
|
2019-10-13 10:24:27 +00:00
|
|
|
return copy_of(path);
|
2019-09-17 12:08:38 +00:00
|
|
|
|
2019-09-17 12:11:45 +00:00
|
|
|
/* Figure out how much of the string we need to compare. */
|
2019-09-17 12:18:03 +00:00
|
|
|
while (path[i] != '/' && path[i] != '\0')
|
2019-09-17 12:11:45 +00:00
|
|
|
i++;
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2019-09-17 12:11:45 +00:00
|
|
|
if (i == 1) {
|
|
|
|
get_homedir();
|
2019-10-13 10:24:27 +00:00
|
|
|
tilded = copy_of(homedir);
|
2019-09-17 12:11:45 +00:00
|
|
|
} else {
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2019-09-17 12:11:45 +00:00
|
|
|
const struct passwd *userdata;
|
|
|
|
|
2019-09-17 12:18:03 +00:00
|
|
|
tilded = mallocstrncpy(NULL, path, i + 1);
|
|
|
|
tilded[i] = '\0';
|
2019-09-17 12:11:45 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
userdata = getpwent();
|
2019-09-17 12:18:03 +00:00
|
|
|
} while (userdata && strcmp(userdata->pw_name, tilded + 1) != 0);
|
2019-09-17 12:11:45 +00:00
|
|
|
endpwent();
|
2019-09-17 12:18:03 +00:00
|
|
|
|
2019-09-17 12:11:45 +00:00
|
|
|
if (userdata != NULL)
|
2019-09-17 12:18:03 +00:00
|
|
|
tilded = mallocstrcpy(tilded, userdata->pw_dir);
|
2017-02-21 22:04:44 +00:00
|
|
|
#else
|
2019-10-13 10:38:46 +00:00
|
|
|
tilded = copy_of("");
|
2017-02-21 22:04:44 +00:00
|
|
|
#endif
|
2019-09-17 12:11:45 +00:00
|
|
|
}
|
2001-01-23 03:09:15 +00:00
|
|
|
|
2019-09-17 12:18:03 +00:00
|
|
|
retval = charalloc(strlen(tilded) + strlen(path + i) + 1);
|
|
|
|
sprintf(retval, "%s%s", tilded, path + i);
|
2000-11-25 04:43:43 +00:00
|
|
|
|
2019-09-17 12:18:03 +00:00
|
|
|
free(tilded);
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return retval;
|
2000-11-24 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:15:51 +00:00
|
|
|
#if defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER)
|
2005-02-11 16:02:54 +00:00
|
|
|
/* Our sort routine for file listings. Sort alphabetically and
|
2005-06-21 04:11:04 +00:00
|
|
|
* case-insensitively, and sort directories before filenames. */
|
2005-02-08 20:37:53 +00:00
|
|
|
int diralphasort(const void *va, const void *vb)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
struct stat fileinfo;
|
|
|
|
const char *a = *(const char *const *)va;
|
|
|
|
const char *b = *(const char *const *)vb;
|
|
|
|
bool aisdir = stat(a, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode);
|
|
|
|
bool bisdir = stat(b, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode);
|
|
|
|
|
|
|
|
if (aisdir && !bisdir)
|
|
|
|
return -1;
|
|
|
|
if (!aisdir && bisdir)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Standard function brain damage: We should be sorting
|
|
|
|
* alphabetically and case-insensitively according to the current
|
|
|
|
* locale, but there's no standard strcasecoll() function, so we
|
|
|
|
* have to use multibyte strcasecmp() instead. */
|
|
|
|
return mbstrcasecmp(a, b);
|
2005-02-08 20:37:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-08 17:15:51 +00:00
|
|
|
#ifdef ENABLE_TABCOMP
|
2018-11-19 15:38:54 +00:00
|
|
|
/* Return TRUE when the given path is a directory. */
|
|
|
|
bool is_dir(const char *path)
|
2000-11-24 14:00:16 +00:00
|
|
|
{
|
2018-11-19 15:38:54 +00:00
|
|
|
char *realpath = real_dir_from_tilde(path);
|
2017-12-29 18:27:33 +00:00
|
|
|
struct stat fileinfo;
|
|
|
|
bool retval;
|
2007-01-11 21:36:29 +00:00
|
|
|
|
2018-11-19 15:38:54 +00:00
|
|
|
retval = (stat(realpath, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode));
|
2007-01-11 21:36:29 +00:00
|
|
|
|
2018-11-19 15:38:54 +00:00
|
|
|
free(realpath);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return retval;
|
2000-11-24 14:00:16 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2005-11-17 18:58:56 +00:00
|
|
|
/* These functions, username_tab_completion(), cwd_tab_completion()
|
|
|
|
* (originally exe_n_cwd_tab_completion()), and input_tab(), were
|
|
|
|
* adapted from busybox 0.46 (cmdedit.c). Here is the notice from that
|
2007-08-10 22:12:39 +00:00
|
|
|
* file, with the copyright years updated:
|
2000-11-05 17:54:41 +00:00
|
|
|
*
|
|
|
|
* Termios command line History and Editting, originally
|
|
|
|
* intended for NetBSD sh (ash)
|
2007-08-10 22:12:39 +00:00
|
|
|
* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
|
2000-11-05 17:54:41 +00:00
|
|
|
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Etc: Dave Cinege <dcinege@psychosis.com>
|
|
|
|
* Majorly adjusted/re-written for busybox:
|
|
|
|
* Erik Andersen <andersee@debian.org>
|
|
|
|
*
|
|
|
|
* You may use this code as you wish, so long as the original author(s)
|
|
|
|
* are attributed in any redistributions of the source code.
|
|
|
|
* This code is 'as is' with no warranty.
|
2005-02-08 20:37:53 +00:00
|
|
|
* This code may safely be consumed by a BSD or GPL license. */
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2007-10-11 15:38:32 +00:00
|
|
|
/* We consider the first buf_len characters of buf for ~username tab
|
2005-02-08 20:37:53 +00:00
|
|
|
* completion. */
|
|
|
|
char **username_tab_completion(const char *buf, size_t *num_matches,
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t buf_len)
|
2000-11-05 17:54:41 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
char **matches = NULL;
|
2017-07-01 11:25:11 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2017-12-29 18:27:33 +00:00
|
|
|
const struct passwd *userdata;
|
2017-07-01 11:25:11 +00:00
|
|
|
#endif
|
2000-11-24 14:00:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
*num_matches = 0;
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2017-12-29 18:27:33 +00:00
|
|
|
while ((userdata = getpwent()) != NULL) {
|
|
|
|
if (strncmp(userdata->pw_name, buf + 1, buf_len - 1) == 0) {
|
|
|
|
/* Cool, found a match. Add it to the list. This makes a
|
|
|
|
* lot more sense to me (Chris) this way... */
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* ...unless the match exists outside the operating
|
|
|
|
* directory, in which case just go to the next match. */
|
|
|
|
if (outside_of_confinement(userdata->pw_dir, TRUE))
|
|
|
|
continue;
|
2001-09-19 03:19:43 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
matches = (char **)nrealloc(matches, (*num_matches + 1) *
|
|
|
|
sizeof(char *));
|
|
|
|
matches[*num_matches] = charalloc(strlen(userdata->pw_name) + 2);
|
|
|
|
sprintf(matches[*num_matches], "~%s", userdata->pw_name);
|
|
|
|
++(*num_matches);
|
|
|
|
}
|
2000-11-24 14:00:16 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
endpwent();
|
2017-02-21 22:04:44 +00:00
|
|
|
#endif
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return matches;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 15:38:32 +00:00
|
|
|
/* We consider the first buf_len characters of buf for filename tab
|
2005-11-17 18:58:56 +00:00
|
|
|
* completion. */
|
2018-03-22 18:54:20 +00:00
|
|
|
char **cwd_tab_completion(const char *buf, bool allow_files,
|
|
|
|
size_t *num_matches, size_t buf_len)
|
2000-11-05 17:54:41 +00:00
|
|
|
{
|
2019-10-13 10:24:27 +00:00
|
|
|
char *dirname = copy_of(buf);
|
2017-12-29 18:27:33 +00:00
|
|
|
char *slash, *filename;
|
|
|
|
size_t filenamelen;
|
|
|
|
char **matches = NULL;
|
|
|
|
DIR *dir;
|
|
|
|
const struct dirent *nextdir;
|
|
|
|
|
|
|
|
*num_matches = 0;
|
|
|
|
dirname[buf_len] = '\0';
|
|
|
|
|
|
|
|
/* If there's a / in the name, split out filename and directory parts. */
|
|
|
|
slash = strrchr(dirname, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
char *wasdirname = dirname;
|
|
|
|
|
2019-10-13 10:24:27 +00:00
|
|
|
filename = copy_of(++slash);
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Cut off the filename part after the slash. */
|
|
|
|
*slash = '\0';
|
|
|
|
dirname = real_dir_from_tilde(dirname);
|
|
|
|
/* A non-absolute path is relative to the current browser directory. */
|
|
|
|
if (dirname[0] != '/') {
|
|
|
|
dirname = charealloc(dirname, strlen(present_path) +
|
|
|
|
strlen(wasdirname) + 1);
|
|
|
|
sprintf(dirname, "%s%s", present_path, wasdirname);
|
|
|
|
}
|
|
|
|
free(wasdirname);
|
|
|
|
} else {
|
|
|
|
filename = dirname;
|
2019-10-13 10:24:27 +00:00
|
|
|
dirname = copy_of(present_path);
|
2016-04-30 19:22:16 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
dir = opendir(dirname);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (dir == NULL) {
|
|
|
|
/* Don't print an error, just shut up and return. */
|
|
|
|
beep();
|
|
|
|
free(filename);
|
|
|
|
free(dirname);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
filenamelen = strlen(filename);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while ((nextdir = readdir(dir)) != NULL) {
|
|
|
|
bool skip_match = FALSE;
|
2006-02-07 21:11:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* See if this matches. */
|
|
|
|
if (strncmp(nextdir->d_name, filename, filenamelen) == 0 &&
|
|
|
|
(*filename == '.' || (strcmp(nextdir->d_name, ".") != 0 &&
|
|
|
|
strcmp(nextdir->d_name, "..") != 0))) {
|
|
|
|
/* Cool, found a match. Add it to the list. This makes a
|
|
|
|
* lot more sense to me (Chris) this way... */
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
char *tmp = charalloc(strlen(dirname) + strlen(nextdir->d_name) + 1);
|
|
|
|
sprintf(tmp, "%s%s", dirname, nextdir->d_name);
|
2006-02-07 21:11:05 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* ...unless the match exists outside the operating
|
|
|
|
* directory, in which case just go to the next match. */
|
|
|
|
skip_match = outside_of_confinement(tmp, TRUE);
|
2006-02-07 21:11:05 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* ...or unless the match isn't a directory and allow_files
|
|
|
|
* isn't set, in which case just go to the next match. */
|
|
|
|
skip_match = skip_match || (!allow_files && !is_dir(tmp));
|
2006-02-07 21:11:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(tmp);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (skip_match)
|
|
|
|
continue;
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
matches = (char **)nrealloc(matches, (*num_matches + 1) *
|
|
|
|
sizeof(char *));
|
2019-10-13 10:24:27 +00:00
|
|
|
matches[*num_matches] = copy_of(nextdir->d_name);
|
2017-12-29 18:27:33 +00:00
|
|
|
++(*num_matches);
|
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
2005-07-08 20:09:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
closedir(dir);
|
|
|
|
free(dirname);
|
|
|
|
free(filename);
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return matches;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
2005-05-26 18:11:32 +00:00
|
|
|
/* Do tab completion. place refers to how much the statusbar cursor
|
2006-02-18 21:32:29 +00:00
|
|
|
* position should be advanced. refresh_func is the function we will
|
|
|
|
* call to refresh the edit window. */
|
2016-04-17 16:09:24 +00:00
|
|
|
char *input_tab(char *buf, bool allow_files, size_t *place,
|
2017-12-29 18:27:33 +00:00
|
|
|
bool *lastwastab, void (*refresh_func)(void), bool *listed)
|
2000-11-05 17:54:41 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t num_matches = 0, buf_len;
|
|
|
|
char **matches = NULL;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
*listed = FALSE;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
|
|
|
* then try completing this word as a username. */
|
|
|
|
if (*place > 0 && *buf == '~') {
|
|
|
|
const char *slash = strchr(buf, '/');
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (slash == NULL || slash >= buf + *place)
|
|
|
|
matches = username_tab_completion(buf, &num_matches, *place);
|
|
|
|
}
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Match against files relative to the current working directory. */
|
|
|
|
if (matches == NULL)
|
|
|
|
matches = cwd_tab_completion(buf, allow_files, &num_matches, *place);
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
buf_len = strlen(buf);
|
2007-10-11 15:49:08 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (num_matches == 0 || *place != buf_len)
|
|
|
|
beep();
|
|
|
|
else {
|
|
|
|
size_t match, common_len = 0;
|
|
|
|
char *mzero, *glued;
|
|
|
|
const char *lastslash = revstrstr(buf, "/", buf + *place);
|
|
|
|
size_t lastslash_len = (lastslash == NULL) ? 0 : lastslash - buf + 1;
|
|
|
|
char char1[MAXCHARLEN], char2[MAXCHARLEN];
|
|
|
|
int len1, len2;
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Get the number of characters that all matches have in common. */
|
|
|
|
while (TRUE) {
|
|
|
|
len1 = parse_mbchar(matches[0] + common_len, char1, NULL);
|
2016-04-16 09:15:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
for (match = 1; match < num_matches; match++) {
|
|
|
|
len2 = parse_mbchar(matches[match] + common_len, char2, NULL);
|
2016-12-26 10:01:40 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (len1 != len2 || strncmp(char1, char2, len2) != 0)
|
|
|
|
break;
|
|
|
|
}
|
2000-11-25 04:43:43 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (match < num_matches || matches[0][common_len] == '\0')
|
|
|
|
break;
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
common_len += len1;
|
|
|
|
}
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
mzero = charalloc(lastslash_len + common_len + 1);
|
2006-10-03 18:46:00 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
strncpy(mzero, buf, lastslash_len);
|
|
|
|
strncpy(mzero + lastslash_len, matches[0], common_len);
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
common_len += lastslash_len;
|
|
|
|
mzero[common_len] = '\0';
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Cover also the case of the user specifying a relative path. */
|
|
|
|
glued = charalloc(strlen(present_path) + strlen(mzero) + 1);
|
|
|
|
sprintf(glued, "%s%s", present_path, mzero);
|
2016-04-30 19:22:16 +00:00
|
|
|
|
2019-05-19 16:58:02 +00:00
|
|
|
if (num_matches == 1 && (is_dir(mzero) || is_dir(glued)))
|
2017-12-29 18:27:33 +00:00
|
|
|
mzero[common_len++] = '/';
|
2005-05-26 03:32:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (num_matches > 1 && (common_len != *place || !*lastwastab))
|
|
|
|
beep();
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the matches have something in common, show that part. */
|
|
|
|
if (common_len != *place) {
|
|
|
|
buf = charealloc(buf, common_len + buf_len - *place + 1);
|
2019-09-18 13:20:08 +00:00
|
|
|
memmove(buf + common_len, buf + *place, buf_len - *place + 1);
|
2017-12-29 18:27:33 +00:00
|
|
|
strncpy(buf, mzero, common_len);
|
|
|
|
*place = common_len;
|
|
|
|
}
|
2016-02-21 13:33:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (!*lastwastab)
|
|
|
|
*lastwastab = TRUE;
|
|
|
|
else if (num_matches > 1) {
|
2018-03-22 18:32:17 +00:00
|
|
|
size_t longest_name = 0, ncols;
|
|
|
|
int editline = 0;
|
2000-11-05 21:54:23 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Sort the list of available choices. */
|
|
|
|
qsort(matches, num_matches, sizeof(char *), diralphasort);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Find the length of the longest among the choices. */
|
|
|
|
for (match = 0; match < num_matches; match++) {
|
2019-04-24 06:49:18 +00:00
|
|
|
size_t namelen = breadth(matches[match]);
|
2005-05-26 18:20:05 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (namelen > longest_name)
|
|
|
|
longest_name = namelen;
|
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (longest_name > COLS - 1)
|
|
|
|
longest_name = COLS - 1;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Each column will be (longest_name + 2) columns wide, i.e.
|
|
|
|
* two spaces between columns, except that there will be
|
|
|
|
* only one space after the last column. */
|
|
|
|
ncols = (COLS + 1) / (longest_name + 2);
|
2000-11-15 01:25:42 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Blank the edit window and hide the cursor. */
|
|
|
|
blank_edit();
|
|
|
|
curs_set(0);
|
|
|
|
wmove(edit, 0, 0);
|
2000-11-05 21:56:54 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Now print the list of matches out there. */
|
|
|
|
for (match = 0; match < num_matches; match++) {
|
|
|
|
char *disp;
|
2000-11-14 18:25:26 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
wmove(edit, editline, (longest_name + 2) * (match % ncols));
|
2000-11-05 21:54:23 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (match % ncols == 0 && editline == editwinrows - 1 &&
|
|
|
|
num_matches - match > ncols) {
|
|
|
|
waddstr(edit, _("(more)"));
|
|
|
|
break;
|
|
|
|
}
|
2000-11-05 21:54:23 +00:00
|
|
|
|
2019-02-27 20:37:53 +00:00
|
|
|
disp = display_string(matches[match], 0, longest_name, FALSE, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
waddstr(edit, disp);
|
|
|
|
free(disp);
|
2000-11-05 22:48:35 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if ((match + 1) % ncols == 0)
|
|
|
|
editline++;
|
|
|
|
}
|
2005-07-01 22:58:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
wnoutrefresh(edit);
|
|
|
|
*listed = TRUE;
|
|
|
|
}
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(glued);
|
|
|
|
free(mzero);
|
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free_chararray(matches, num_matches);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When we didn't list any matches now, refresh the edit window, just
|
|
|
|
* in case a previous tab showed a list, so we know where we are. */
|
|
|
|
if (!*listed)
|
|
|
|
refresh_func();
|
2016-04-23 13:38:40 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return buf;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
2017-05-08 17:15:51 +00:00
|
|
|
#endif /* ENABLE_TABCOMP */
|