- General: - Preliminary prepend code. This may be a bad idea, but I've been wanting it for awhile now and we'll see how bad it messes everything up. Changes to files.c:do_writeout(), write_file()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1188 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
Chris Allegretta 2002-04-16 03:15:47 +00:00
parent 7f80e6639f
commit 0e9b7aa16d
4 changed files with 52 additions and 11 deletions

View File

@ -8,6 +8,9 @@ CVS code -
New functions nano.c:make_new_opennode(), free_openfilestruct(), New functions nano.c:make_new_opennode(), free_openfilestruct(),
delete_opennode(), unlink_opennode(), splice_opennode(), delete_opennode(), unlink_opennode(), splice_opennode(),
new struct openfilestruct in nano.h. new struct openfilestruct in nano.h.
- Preliminary prepend code. This may be a bad idea, but I've
been wanting it for awhile now and we'll see how bad it messes
everything up. Changes to files.c:do_writeout(), write_file().
- configure.ac: - configure.ac:
- Define NDEBUG to silence asserts (David Benbennick). - Define NDEBUG to silence asserts (David Benbennick).
- files.c: - files.c:

52
files.c
View File

@ -1178,8 +1178,8 @@ int check_operating_dir(char *currpath, int allow_tabcomp)
* tmp means we are writing a tmp file in a secure fashion. We use * tmp means we are writing a tmp file in a secure fashion. We use
* it when spell checking or dumping the file on an error. * it when spell checking or dumping the file on an error.
* *
* append means, not surprisingly, whether we are appending instead * append == 1 means we are appending instead of overwriting.
* of overwriting. * append == 2 means we are prepending instead of overwriting.
* *
* nonamechange means don't change the current filename, it is ignored * nonamechange means don't change the current filename, it is ignored
* if tmp == 1. * if tmp == 1.
@ -1187,9 +1187,9 @@ int check_operating_dir(char *currpath, int allow_tabcomp)
int write_file(char *name, int tmp, int append, int nonamechange) int write_file(char *name, int tmp, int append, int nonamechange)
{ {
long size, lineswritten = 0; long size, lineswritten = 0;
char *buf = NULL; char *buf = NULL, prechar;
filestruct *fileptr; filestruct *fileptr;
int fd, mask = 0, realexists, anyexists; int fd, fd2, mask = 0, realexists, anyexists;
struct stat st, lst; struct stat st, lst;
char *realname = NULL; char *realname = NULL;
@ -1238,7 +1238,8 @@ int write_file(char *name, int tmp, int append, int nonamechange)
if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) && if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) &&
S_ISLNK(lst.st_mode))) { S_ISLNK(lst.st_mode))) {
to reflect whether or not to link/unlink/rename the file */ to reflect whether or not to link/unlink/rename the file */
else if (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) || tmp) { else if (append != 2 && (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode)
|| tmp)) {
/* Use O_EXCL if tmp == 1. This is now copied from joe, because /* Use O_EXCL if tmp == 1. This is now copied from joe, because
wiggy says so *shrug*. */ wiggy says so *shrug*. */
if (append) if (append)
@ -1343,6 +1344,26 @@ int write_file(char *name, int tmp, int append, int nonamechange)
return -1; return -1;
} }
/* if we're prepending, open the real file, and append it here */
if (append == 2) {
if ((fd = open(buf, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR))) == -1) {
statusbar(_("Could not reopen %s: %s"), buf, strerror(errno));
return -1;
}
if ((fd2 = open(realname, O_RDONLY)) == -1) {
statusbar(_("Could open %s for prepend: %s"), realname, strerror(errno));
return -1;
}
while (read(fd2, &prechar, 1) > 0)
write(fd, &prechar, 1);
close(fd);
close(fd2);
}
if (realexists == -1 || tmp || if (realexists == -1 || tmp ||
(!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
@ -1358,7 +1379,8 @@ int write_file(char *name, int tmp, int append, int nonamechange)
/* Use permissions from file we are overwriting. */ /* Use permissions from file we are overwriting. */
mask = st.st_mode; mask = st.st_mode;
if (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { if (append == 2 ||
(!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode)))) {
if (unlink(realname) == -1) { if (unlink(realname) == -1) {
if (errno != ENOENT) { if (errno != ENOENT) {
statusbar(_("Could not open %s for writing: %s"), statusbar(_("Could not open %s for writing: %s"),
@ -1438,7 +1460,10 @@ int do_writeout(char *path, int exiting, int append)
/* Be nice to the translation folks */ /* Be nice to the translation folks */
if (ISSET(MARK_ISSET) && !exiting) { if (ISSET(MARK_ISSET) && !exiting) {
if (append) if (append == 2)
i = statusq(1, writefile_list, "",
"%s%s", _("Prepend Selection to File"), formatstr);
else if (append)
i = statusq(1, writefile_list, "", i = statusq(1, writefile_list, "",
"%s%s", _("Append Selection to File"), formatstr); "%s%s", _("Append Selection to File"), formatstr);
else else
@ -1447,7 +1472,10 @@ int do_writeout(char *path, int exiting, int append)
} else } else
#endif #endif
{ {
if (append) if (append == 2)
i = statusq(1, writefile_list, answer,
"%s%s", _("File Name to Prepend"), formatstr);
else if (append)
i = statusq(1, writefile_list, answer, i = statusq(1, writefile_list, answer,
"%s%s", _("File Name to Append"), formatstr); "%s%s", _("File Name to Append"), formatstr);
else else
@ -1481,8 +1509,12 @@ int do_writeout(char *path, int exiting, int append)
UNSET(DOS_FILE); UNSET(DOS_FILE);
TOGGLE(MAC_FILE); TOGGLE(MAC_FILE);
return(do_writeout(answer, exiting, append)); return(do_writeout(answer, exiting, append));
} else if (i == NANO_APPEND_KEY) } else if (i == NANO_PREPEND_KEY)
return(do_writeout(answer, exiting, 1 - append)); return(do_writeout(answer, exiting, 2));
else if (i == NANO_APPEND_KEY && append != 1)
return(do_writeout(answer, exiting, 1));
else if (i == NANO_APPEND_KEY)
return(do_writeout(answer, exiting, 0));
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, _("filename is %s"), answer); fprintf(stderr, _("filename is %s"), answer);

View File

@ -272,7 +272,7 @@ void shortcut_init(int unjustify)
"", *nano_backspace_msg = "", *nano_tab_msg = "", *nano_backspace_msg = "", *nano_tab_msg =
"", *nano_enter_msg = "", *nano_cancel_msg = "", *nano_enter_msg = "", *nano_cancel_msg =
"", *nano_unjustify_msg = "", *nano_append_msg = "", *nano_unjustify_msg = "", *nano_append_msg =
""; "", *nano_prepend_msg = "";
#ifdef ENABLE_MULTIBUFFER #ifdef ENABLE_MULTIBUFFER
char *nano_openprev_msg = "", *nano_opennext_msg = ""; char *nano_openprev_msg = "", *nano_opennext_msg = "";
@ -330,6 +330,7 @@ void shortcut_init(int unjustify)
nano_gotodir_msg = _("Goto Directory"); nano_gotodir_msg = _("Goto Directory");
nano_cancel_msg = _("Cancel the current function"); nano_cancel_msg = _("Cancel the current function");
nano_append_msg = _("Append to the current file"); nano_append_msg = _("Append to the current file");
nano_prepend_msg = _("Prepend to the current file");
nano_reverse_msg = _("Search backwards"); nano_reverse_msg = _("Search backwards");
nano_dos_msg = _("Write file out in DOS format"); nano_dos_msg = _("Write file out in DOS format");
nano_mac_msg = _("Write file out in Mac format"); nano_mac_msg = _("Write file out in Mac format");
@ -632,6 +633,10 @@ void shortcut_init(int unjustify)
NANO_APPEND_KEY, _("Append"), NANO_APPEND_KEY, _("Append"),
nano_append_msg, 0, 0, 0, NOVIEW, 0); nano_append_msg, 0, 0, 0, NOVIEW, 0);
sc_init_one(&writefile_list,
NANO_PREPEND_KEY, _("Prepend"),
nano_prepend_msg, 0, 0, 0, NOVIEW, 0);
sc_init_one(&writefile_list, NANO_CANCEL_KEY, sc_init_one(&writefile_list, NANO_CANCEL_KEY,
_("Cancel"), nano_cancel_msg, 0, 0, 0, VIEW, 0); _("Cancel"), nano_cancel_msg, 0, 0, 0, VIEW, 0);

1
nano.h
View File

@ -289,6 +289,7 @@ know what you're doing */
#define NANO_FROMSEARCHTOGOTO_KEY NANO_CONTROL_T #define NANO_FROMSEARCHTOGOTO_KEY NANO_CONTROL_T
#define NANO_TOFILES_KEY NANO_CONTROL_T #define NANO_TOFILES_KEY NANO_CONTROL_T
#define NANO_APPEND_KEY NANO_ALT_A #define NANO_APPEND_KEY NANO_ALT_A
#define NANO_PREPEND_KEY NANO_ALT_P
#define NANO_OPENPREV_KEY NANO_ALT_LCARAT #define NANO_OPENPREV_KEY NANO_ALT_LCARAT
#define NANO_OPENNEXT_KEY NANO_ALT_RCARAT #define NANO_OPENNEXT_KEY NANO_ALT_RCARAT
#define NANO_OPENPREV_ALTKEY NANO_ALT_COMMA #define NANO_OPENPREV_ALTKEY NANO_ALT_COMMA