Chris Allegretta | 11b0011 | 2000-08-06 21:13:45 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 2 | /************************************************************************** |
| 3 | * files.c * |
| 4 | * * |
David Lawrence Ramsey | c13b7f0 | 2005-01-01 07:28:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2005 Chris Allegretta * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
Chris Allegretta | 3a24f3f | 2001-10-24 11:33:54 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; either version 2, or (at your option) * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 9 | * any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 19 | * * |
| 20 | **************************************************************************/ |
| 21 | |
Jordi Mallach | 55381aa | 2004-11-17 23:17:05 +0000 | [diff] [blame] | 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
Chris Allegretta | 6efda54 | 2001-04-28 18:03:52 +0000 | [diff] [blame] | 25 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
Chris Allegretta | bb88ece | 2002-03-25 13:40:39 +0000 | [diff] [blame] | 30 | #include <sys/wait.h> |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 31 | #include <utime.h> |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
| 33 | #include <errno.h> |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 34 | #include <ctype.h> |
| 35 | #include <dirent.h> |
Chris Allegretta | ee733e6 | 2001-01-22 22:17:08 +0000 | [diff] [blame] | 36 | #include <pwd.h> |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 37 | #include <assert.h> |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 38 | #include "proto.h" |
| 39 | #include "nano.h" |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 40 | |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 41 | static file_format fmt = NIX_FILE; |
| 42 | /* The format of the current file. */ |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 43 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 44 | /* What happens when there is no file to open? aiee! */ |
| 45 | void new_file(void) |
| 46 | { |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 47 | fileage = make_new_node(NULL); |
David Lawrence Ramsey | f6e4d33 | 2005-01-14 17:17:11 +0000 | [diff] [blame] | 48 | fileage->data = mallocstrcpy(NULL, ""); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 49 | filebot = fileage; |
| 50 | edittop = fileage; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 51 | current = fileage; |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 52 | current_x = 0; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 53 | totlines = 1; |
Chris Allegretta | 1b3381b | 2001-09-28 21:59:01 +0000 | [diff] [blame] | 54 | totsize = 0; |
Chris Allegretta | e642197 | 2001-07-18 01:03:36 +0000 | [diff] [blame] | 55 | |
Chris Allegretta | 7c27be4 | 2002-05-05 23:03:54 +0000 | [diff] [blame] | 56 | #ifdef ENABLE_COLOR |
| 57 | update_color(); |
David Lawrence Ramsey | 760a2dc | 2004-01-14 06:38:00 +0000 | [diff] [blame] | 58 | if (ISSET(COLOR_SYNTAX)) |
| 59 | edit_refresh(); |
Chris Allegretta | 7c27be4 | 2002-05-05 23:03:54 +0000 | [diff] [blame] | 60 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 61 | } |
| 62 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 63 | /* We make a new line of text from buf. buf is length len. If |
| 64 | * first_line_ins is TRUE, then we put the new line at the top of the |
| 65 | * file. Otherwise, we assume prev is the last line of the file, and |
| 66 | * put our line after prev. */ |
| 67 | filestruct *read_line(char *buf, filestruct *prev, bool *first_line_ins, |
| 68 | size_t len) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 69 | { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 70 | filestruct *fileptr = (filestruct *)nmalloc(sizeof(filestruct)); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 71 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 72 | /* Convert nulls to newlines. len is the string's real length |
| 73 | * here. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 74 | unsunder(buf, len); |
| 75 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 76 | assert(strlen(buf) == len); |
| 77 | |
| 78 | fileptr->data = mallocstrcpy(NULL, buf); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 79 | |
Chris Allegretta | 9184189 | 2001-09-21 02:37:01 +0000 | [diff] [blame] | 80 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 81 | /* If it's a DOS file (CR LF), and file conversion isn't disabled, |
| 82 | * strip out the CR part. */ |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 83 | if (!ISSET(NO_CONVERT) && len > 0 && buf[len - 1] == '\r') |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 84 | fileptr->data[len - 1] = '\0'; |
Chris Allegretta | 9184189 | 2001-09-21 02:37:01 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 87 | if (*first_line_ins || fileage == NULL) { |
| 88 | /* Special case: we're inserting with the cursor on the first |
| 89 | * line. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 90 | fileptr->prev = NULL; |
| 91 | fileptr->next = fileage; |
| 92 | fileptr->lineno = 1; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 93 | if (*first_line_ins) { |
| 94 | *first_line_ins = FALSE; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 95 | /* If we're inserting into the first line of the file, then |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 96 | * we want to make sure that our edit buffer stays on the |
| 97 | * first line and that fileage stays up to date. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 98 | edittop = fileptr; |
| 99 | } else |
| 100 | filebot = fileptr; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 101 | fileage = fileptr; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 102 | } else { |
| 103 | assert(prev != NULL); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 104 | fileptr->prev = prev; |
| 105 | fileptr->next = NULL; |
| 106 | fileptr->lineno = prev->lineno + 1; |
| 107 | prev->next = fileptr; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return fileptr; |
| 111 | } |
| 112 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 113 | /* Load a file into the edit buffer. This takes data from the file |
| 114 | * struct. */ |
| 115 | void load_file(void) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 116 | { |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 117 | current = fileage; |
| 118 | |
| 119 | #ifdef ENABLE_MULTIBUFFER |
| 120 | /* Add a new entry to the open_files structure. */ |
| 121 | add_open_file(FALSE); |
| 122 | |
| 123 | /* Reinitialize the shortcut list. */ |
| 124 | shortcut_init(FALSE); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 125 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void read_file(FILE *f, const char *filename) |
| 129 | { |
| 130 | size_t num_lines = 0; |
| 131 | /* The number of lines in the file. */ |
David Lawrence Ramsey | 96de1b0 | 2005-01-27 21:00:10 +0000 | [diff] [blame] | 132 | size_t num_chars; |
| 133 | /* The number of characters in the file. */ |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 134 | size_t len = 0; |
| 135 | /* The length of the current line of the file. */ |
| 136 | size_t i = 0; |
| 137 | /* The position in the current line of the file. */ |
| 138 | size_t bufx = 128; |
| 139 | /* The size of each chunk of the file that we read. */ |
| 140 | char input = '\0'; |
| 141 | /* The current input character. */ |
| 142 | char *buf; |
| 143 | /* The buffer where we store chunks of the file. */ |
| 144 | filestruct *fileptr = current; |
| 145 | /* The current line of the file. */ |
| 146 | bool first_line_ins = FALSE; |
| 147 | /* Whether we're inserting with the cursor on the first line. */ |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 148 | int input_int; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 149 | /* The current value we read from the file, whether an input |
| 150 | * character or EOF. */ |
| 151 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 152 | int format = 0; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 153 | /* 0 = *nix, 1 = DOS, 2 = Mac, 3 = both DOS and Mac. */ |
| 154 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 155 | |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 156 | buf = charalloc(bufx); |
Chris Allegretta | 8f6c069 | 2000-07-19 01:16:18 +0000 | [diff] [blame] | 157 | buf[0] = '\0'; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 158 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 159 | if (current != NULL) { |
| 160 | if (current == fileage) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 161 | first_line_ins = TRUE; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 162 | else |
| 163 | fileptr = current->prev; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 164 | } |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 165 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 166 | /* For the assertion in read_line(), it must be true that if current |
| 167 | * is NULL, then so is fileage. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 168 | assert(current != NULL || fileage == NULL); |
| 169 | |
David Lawrence Ramsey | 7c1f17a | 2004-10-03 13:47:26 +0000 | [diff] [blame] | 170 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 171 | /* We don't know which file format we have yet, so assume it's a |
| 172 | * *nix file for now. */ |
| 173 | fmt = NIX_FILE; |
David Lawrence Ramsey | 7c1f17a | 2004-10-03 13:47:26 +0000 | [diff] [blame] | 174 | #endif |
| 175 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 176 | /* Read the entire file into the file struct. */ |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 177 | while ((input_int = getc(f)) != EOF) { |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 178 | input = (char)input_int; |
Chris Allegretta | 2598c66 | 2002-03-28 01:59:34 +0000 | [diff] [blame] | 179 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 180 | /* If it's a *nix file (LF) or a DOS file (CR LF), and file |
| 181 | * conversion isn't disabled, handle it! */ |
Chris Allegretta | f6cba64 | 2002-03-26 13:05:54 +0000 | [diff] [blame] | 182 | if (input == '\n') { |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 183 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 184 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 185 | /* If there's a CR before the LF, set format to DOS if we |
| 186 | * currently think this is a *nix file, or to both if we |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 187 | * currently think it's a Mac file. */ |
| 188 | if (!ISSET(NO_CONVERT) && i > 0 && buf[i - 1] == '\r' && |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 189 | (format == 0 || format == 2)) |
| 190 | format++; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 191 | #endif |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 192 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 193 | /* Read in the line properly. */ |
| 194 | fileptr = read_line(buf, fileptr, &first_line_ins, len); |
| 195 | |
| 196 | /* Reset the line length in preparation for the next |
| 197 | * line. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 198 | len = 0; |
| 199 | |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 200 | num_lines++; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 201 | buf[0] = '\0'; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 202 | i = 0; |
Chris Allegretta | 0319176 | 2001-09-22 06:38:38 +0000 | [diff] [blame] | 203 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 204 | /* If it's a Mac file (CR without an LF), and file conversion |
| 205 | * isn't disabled, handle it! */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 206 | } else if (!ISSET(NO_CONVERT) && i > 0 && buf[i - 1] == '\r') { |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 207 | |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 208 | /* If we currently think the file is a *nix file, set format |
| 209 | * to Mac. If we currently think the file is a DOS file, |
| 210 | * set format to both DOS and Mac. */ |
| 211 | if (format == 0 || format == 1) |
| 212 | format += 2; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 213 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 214 | /* Read in the line properly. */ |
| 215 | fileptr = read_line(buf, fileptr, &first_line_ins, len); |
| 216 | |
| 217 | /* Reset the line length in preparation for the next line. |
| 218 | * Since we've already read in the next character, reset it |
| 219 | * to 1 instead of 0. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 220 | len = 1; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 221 | |
Chris Allegretta | 0319176 | 2001-09-22 06:38:38 +0000 | [diff] [blame] | 222 | num_lines++; |
Chris Allegretta | f6cba64 | 2002-03-26 13:05:54 +0000 | [diff] [blame] | 223 | buf[0] = input; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 224 | buf[1] = '\0'; |
Chris Allegretta | 0319176 | 2001-09-22 06:38:38 +0000 | [diff] [blame] | 225 | i = 1; |
| 226 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 227 | } else { |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 228 | /* Calculate the total length of the line. It might have |
| 229 | * nulls in it, so we can't just use strlen() here. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 230 | len++; |
| 231 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 232 | /* Now we allocate a bigger buffer 128 characters at a time. |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 233 | * If we allocate a lot of space for one line, we may indeed |
| 234 | * have to use a buffer this big later on, so we don't |
| 235 | * decrease it at all. We do free it at the end, though. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 236 | if (i >= bufx - 1) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 237 | bufx += 128; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 238 | buf = charealloc(buf, bufx); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 239 | } |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 240 | |
Chris Allegretta | f6cba64 | 2002-03-26 13:05:54 +0000 | [diff] [blame] | 241 | buf[i] = input; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 242 | buf[i + 1] = '\0'; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 243 | i++; |
| 244 | } |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 245 | } |
| 246 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 247 | /* This conditional duplicates previous read_byte() behavior. |
| 248 | * Perhaps this could use some better handling. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 249 | if (ferror(f)) |
| 250 | nperror(filename); |
| 251 | fclose(f); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 252 | |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 253 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 254 | /* If file conversion isn't disabled and the last character in this |
| 255 | * file is a CR, read it in properly as a Mac format line. */ |
| 256 | if (len == 0 && !ISSET(NO_CONVERT) && input == '\r') { |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 257 | len = 1; |
| 258 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 259 | buf[0] = input; |
| 260 | buf[1] = '\0'; |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 261 | } |
| 262 | #endif |
Chris Allegretta | 52c5a6e | 2002-03-21 05:07:28 +0000 | [diff] [blame] | 263 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 264 | /* Did we not get a newline and still have stuff to do? */ |
| 265 | if (len > 0) { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 266 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 267 | /* If file conversion isn't disabled and the last character in |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 268 | * this file is a CR, set format to Mac if we currently think |
| 269 | * the file is a *nix file, or to both DOS and Mac if we |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 270 | * currently think the file is a DOS file. */ |
| 271 | if (!ISSET(NO_CONVERT) && buf[len - 1] == '\r' && |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 272 | (format == 0 || format == 1)) |
| 273 | format += 2; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 274 | #endif |
| 275 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 276 | /* Read in the last line properly. */ |
| 277 | fileptr = read_line(buf, fileptr, &first_line_ins, len); |
| 278 | num_lines++; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 279 | } |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 280 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 281 | free(buf); |
| 282 | |
| 283 | /* If we didn't get a file and we don't already have one, make a new |
| 284 | * file. */ |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 285 | if (fileptr == NULL) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 286 | new_file(); |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 287 | |
Chris Allegretta | f6cba64 | 2002-03-26 13:05:54 +0000 | [diff] [blame] | 288 | /* Did we try to insert a file of 0 bytes? */ |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 289 | if (num_lines != 0) { |
| 290 | if (current != NULL) { |
| 291 | fileptr->next = current; |
| 292 | current->prev = fileptr; |
| 293 | renumber(current); |
| 294 | current_x = 0; |
| 295 | placewewant = 0; |
| 296 | } else if (fileptr->next == NULL) { |
| 297 | filebot = fileptr; |
| 298 | new_magicline(); |
David Lawrence Ramsey | daa533a | 2005-02-07 05:31:51 +0000 | [diff] [blame] | 299 | totsize--; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 300 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 301 | } |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 302 | |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 303 | get_totals(fileage, filebot, NULL, &num_chars); |
| 304 | totsize += num_chars; |
| 305 | |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 306 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 307 | if (format == 3) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 308 | statusbar( |
| 309 | P_("Read %lu line (Converted from DOS and Mac format)", |
| 310 | "Read %lu lines (Converted from DOS and Mac format)", |
| 311 | (unsigned long)num_lines), (unsigned long)num_lines); |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 312 | else if (format == 2) { |
| 313 | fmt = MAC_FILE; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 314 | statusbar(P_("Read %lu line (Converted from Mac format)", |
| 315 | "Read %lu lines (Converted from Mac format)", |
| 316 | (unsigned long)num_lines), (unsigned long)num_lines); |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 317 | } else if (format == 1) { |
| 318 | fmt = DOS_FILE; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 319 | statusbar(P_("Read %lu line (Converted from DOS format)", |
| 320 | "Read %lu lines (Converted from DOS format)", |
| 321 | (unsigned long)num_lines), (unsigned long)num_lines); |
David Lawrence Ramsey | 7c1f17a | 2004-10-03 13:47:26 +0000 | [diff] [blame] | 322 | } else |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 323 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 324 | statusbar(P_("Read %lu line", "Read %lu lines", |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 325 | (unsigned long)num_lines), (unsigned long)num_lines); |
David Lawrence Ramsey | 7bf00de | 2003-09-23 04:25:05 +0000 | [diff] [blame] | 326 | |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 327 | totlines += num_lines; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 328 | } |
| 329 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 330 | /* Open the file (and decide if it exists). If newfie is TRUE, display |
| 331 | * "New File" if the file is missing. Otherwise, say "[filename] not |
| 332 | * found". |
| 333 | * |
| 334 | * Return -2 if we say "New File". Otherwise, -1 if the file isn't |
| 335 | * opened, 0 otherwise. The file might still have an error while |
| 336 | * reading with a 0 return value. *f is set to the opened file. */ |
| 337 | int open_file(const char *filename, bool newfie, FILE **f) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 338 | { |
| 339 | int fd; |
| 340 | struct stat fileinfo; |
| 341 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 342 | assert(f != NULL); |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 343 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 344 | if (filename == NULL || filename[0] == '\0' || |
| 345 | stat(filename, &fileinfo) == -1) { |
| 346 | if (newfie) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 347 | statusbar(_("New File")); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 348 | return -2; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 349 | } |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 350 | statusbar(_("\"%s\" not found"), filename); |
| 351 | return -1; |
Chris Allegretta | 54c1f79 | 2003-01-26 04:11:09 +0000 | [diff] [blame] | 352 | } else if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) || |
| 353 | S_ISBLK(fileinfo.st_mode)) { |
| 354 | /* Don't open character or block files. Sorry, /dev/sndstat! */ |
David Lawrence Ramsey | e08765d | 2004-11-26 20:17:49 +0000 | [diff] [blame] | 355 | statusbar(S_ISDIR(fileinfo.st_mode) ? _("\"%s\" is a directory") |
| 356 | : _("File \"%s\" is a device file"), filename); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 357 | return -1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 358 | } else if ((fd = open(filename, O_RDONLY)) == -1) { |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 359 | statusbar(_("Error reading %s: %s"), filename, strerror(errno)); |
| 360 | return -1; |
| 361 | } else { |
David Lawrence Ramsey | e08765d | 2004-11-26 20:17:49 +0000 | [diff] [blame] | 362 | /* File is A-OK. Open it in binary mode for our own end-of-line |
| 363 | * character munging. */ |
| 364 | *f = fdopen(fd, "rb"); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 365 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 366 | if (*f == NULL) { |
David Lawrence Ramsey | e08765d | 2004-11-26 20:17:49 +0000 | [diff] [blame] | 367 | statusbar(_("Error reading %s: %s"), filename, |
| 368 | strerror(errno)); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 369 | close(fd); |
| 370 | } else |
| 371 | statusbar(_("Reading File")); |
| 372 | } |
| 373 | return 0; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 376 | /* This function will return the name of the first available extension |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 377 | * of a filename (starting with filename.save, then filename.save.1, |
| 378 | * etc.). Memory is allocated for the return value. If no writable |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 379 | * extension exists, we return "". */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 380 | char *get_next_filename(const char *name) |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 381 | { |
| 382 | int i = 0; |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 383 | char *buf; |
| 384 | size_t namelen = strlen(name); |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 385 | |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 386 | buf = charalloc(namelen + num_of_digits(INT_MAX) + 7); |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 387 | strcpy(buf, name); |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 388 | strcpy(buf + namelen, ".save"); |
| 389 | namelen += 5; |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 390 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 391 | while (TRUE) { |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 392 | struct stat fs; |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 393 | |
| 394 | if (stat(buf, &fs) == -1) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 395 | return buf; |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 396 | if (i == INT_MAX) |
| 397 | break; |
| 398 | |
| 399 | i++; |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 400 | sprintf(buf + namelen, ".%d", i); |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 403 | /* We get here only if there is no possible save file. */ |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 404 | null_at(&buf, 0); |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 405 | return buf; |
| 406 | } |
| 407 | |
David Lawrence Ramsey | 146bb60 | 2004-08-27 21:02:38 +0000 | [diff] [blame] | 408 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 409 | void execute_command(const char *command) |
| 410 | { |
| 411 | #ifdef ENABLE_MULTIBUFFER |
| 412 | if (ISSET(MULTIBUFFER)) { |
| 413 | /* Update the current entry in the open_files structure. */ |
| 414 | add_open_file(TRUE); |
| 415 | new_file(); |
| 416 | UNSET(MODIFIED); |
| 417 | UNSET(MARK_ISSET); |
| 418 | } |
| 419 | #endif /* ENABLE_MULTIBUFFER */ |
| 420 | open_pipe(command); |
| 421 | #ifdef ENABLE_MULTIBUFFER |
| 422 | /* Add this new entry to the open_files structure. */ |
| 423 | if (ISSET(MULTIBUFFER)) |
| 424 | load_file(); |
| 425 | #endif /* ENABLE_MULTIBUFFER */ |
| 426 | } |
| 427 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | 146bb60 | 2004-08-27 21:02:38 +0000 | [diff] [blame] | 428 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 429 | /* name is a file name to open. We make a new buffer if necessary, then |
| 430 | * open and read the file. */ |
| 431 | void load_buffer(const char *name) |
| 432 | { |
David Lawrence Ramsey | 3d002ed | 2004-10-31 22:45:24 +0000 | [diff] [blame] | 433 | bool new_buffer = (fileage == NULL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 434 | #ifdef ENABLE_MULTIBUFFER |
| 435 | || ISSET(MULTIBUFFER) |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 436 | #endif |
David Lawrence Ramsey | 3d002ed | 2004-10-31 22:45:24 +0000 | [diff] [blame] | 437 | ); |
David Lawrence Ramsey | 1c293d2 | 2004-11-05 16:22:27 +0000 | [diff] [blame] | 438 | /* new_buffer says whether we load into this buffer or a new |
| 439 | * one. If new_buffer is TRUE, we display "New File" if the |
| 440 | * file is not found, and if it is found we set filename and add |
| 441 | * a new open_files entry. */ |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 442 | FILE *f; |
| 443 | int rc; |
| 444 | /* rc == -2 means that the statusbar displayed "New File". -1 |
| 445 | * means that the open failed. 0 means success. */ |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 446 | |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 447 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 448 | if (check_operating_dir(name, FALSE)) { |
David Lawrence Ramsey | 8a2c0ba | 2004-11-24 20:36:36 +0000 | [diff] [blame] | 449 | statusbar(_("Can't insert file from outside of %s"), |
| 450 | operating_dir); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 451 | return; |
| 452 | } |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 453 | #endif |
Chris Allegretta | f7c6811 | 2002-09-03 22:58:40 +0000 | [diff] [blame] | 454 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 455 | #ifdef ENABLE_MULTIBUFFER |
| 456 | /* Update the current entry in the open_files structure. */ |
| 457 | add_open_file(TRUE); |
| 458 | #endif |
| 459 | |
| 460 | rc = open_file(name, new_buffer, &f); |
| 461 | |
| 462 | #ifdef ENABLE_MULTIBUFFER |
| 463 | if (rc != -1 && ISSET(MULTIBUFFER)) { |
| 464 | UNSET(MODIFIED); |
| 465 | #ifndef NANO_SMALL |
| 466 | UNSET(MARK_ISSET); |
| 467 | #endif |
| 468 | } |
| 469 | #endif |
| 470 | |
| 471 | if (rc != -1 && new_buffer) { |
| 472 | filename = mallocstrcpy(filename, name); |
| 473 | new_file(); |
| 474 | } |
| 475 | |
| 476 | if (rc == 0) { |
David Lawrence Ramsey | 78d644a | 2004-11-05 16:24:35 +0000 | [diff] [blame] | 477 | file_format fmt_save = fmt; |
| 478 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 479 | read_file(f, filename); |
David Lawrence Ramsey | 78d644a | 2004-11-05 16:24:35 +0000 | [diff] [blame] | 480 | |
| 481 | /* If we're not loading into a new buffer, preserve the file |
| 482 | * format. */ |
| 483 | if (!new_buffer) |
| 484 | fmt = fmt_save; |
| 485 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 486 | #ifndef NANO_SMALL |
| 487 | stat(filename, &originalfilestat); |
| 488 | #endif |
| 489 | } |
| 490 | |
| 491 | /* Add this new entry to the open_files structure if we have |
| 492 | * multibuffer support, or to the main filestruct if we don't. */ |
| 493 | if (rc != -1 && new_buffer) |
| 494 | load_file(); |
| 495 | } |
| 496 | |
David Lawrence Ramsey | be908f6 | 2004-10-01 18:34:30 +0000 | [diff] [blame] | 497 | void do_insertfile( |
| 498 | #ifndef NANO_SMALL |
| 499 | bool execute |
| 500 | #else |
| 501 | void |
| 502 | #endif |
| 503 | ) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 504 | { |
| 505 | int i; |
| 506 | const char *msg; |
David Lawrence Ramsey | 670a56c | 2004-09-28 16:03:03 +0000 | [diff] [blame] | 507 | char *ans = mallocstrcpy(NULL, ""); |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 508 | /* The last answer the user typed on the statusbar. */ |
David Lawrence Ramsey | b73c0c0 | 2004-11-05 15:25:53 +0000 | [diff] [blame] | 509 | filestruct *edittop_save = edittop; |
David Lawrence Ramsey | 036a3d4 | 2004-11-15 21:49:21 +0000 | [diff] [blame] | 510 | int current_y_save = current_y; |
David Lawrence Ramsey | 4a3879f | 2004-11-15 20:42:31 +0000 | [diff] [blame] | 511 | bool at_edittop = FALSE; |
| 512 | /* Whether we're at the top of the edit window. */ |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 513 | |
David Lawrence Ramsey | 487149e | 2004-10-05 20:24:48 +0000 | [diff] [blame] | 514 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 515 | wrap_reset(); |
David Lawrence Ramsey | 487149e | 2004-10-05 20:24:48 +0000 | [diff] [blame] | 516 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 517 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 518 | while (TRUE) { |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 519 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 520 | if (execute) { |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 521 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 522 | if (ISSET(MULTIBUFFER)) |
| 523 | msg = N_("Command to execute in new buffer [from %s] "); |
| 524 | else |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 525 | #endif |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 526 | msg = N_("Command to execute [from %s] "); |
| 527 | } else { |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 528 | #endif |
| 529 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 530 | if (ISSET(MULTIBUFFER)) { |
| 531 | msg = N_("File to insert into new buffer [from %s] "); |
| 532 | } else |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 533 | #endif |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 534 | msg = N_("File to insert [from %s] "); |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 535 | #ifndef NANO_SMALL |
| 536 | } |
| 537 | #endif |
David Lawrence Ramsey | 04a8d1c | 2004-09-28 22:14:58 +0000 | [diff] [blame] | 538 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 539 | i = statusq(TRUE, |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 540 | #ifndef NANO_SMALL |
David Lawrence Ramsey | be908f6 | 2004-10-01 18:34:30 +0000 | [diff] [blame] | 541 | execute ? extcmd_list : |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 542 | #endif |
| 543 | insertfile_list, ans, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 544 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 545 | NULL, |
Chris Allegretta | f7c6811 | 2002-09-03 22:58:40 +0000 | [diff] [blame] | 546 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 547 | _(msg), |
| 548 | #ifndef DISABLE_OPERATINGDIR |
| 549 | operating_dir != NULL && strcmp(operating_dir, ".") != 0 ? |
| 550 | operating_dir : |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 551 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 552 | "./"); |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 553 | |
David Lawrence Ramsey | fdd3bec | 2004-11-07 21:30:55 +0000 | [diff] [blame] | 554 | /* If we're in multibuffer mode and the filename or command is |
| 555 | * blank, open a new buffer instead of canceling. */ |
David Lawrence Ramsey | c136f75 | 2004-11-07 18:35:53 +0000 | [diff] [blame] | 556 | if (i == -1 || (i == -2 |
David Lawrence Ramsey | a084ad9 | 2004-11-07 18:22:33 +0000 | [diff] [blame] | 557 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | c136f75 | 2004-11-07 18:35:53 +0000 | [diff] [blame] | 558 | && !ISSET(MULTIBUFFER) |
David Lawrence Ramsey | a084ad9 | 2004-11-07 18:22:33 +0000 | [diff] [blame] | 559 | #endif |
David Lawrence Ramsey | c136f75 | 2004-11-07 18:35:53 +0000 | [diff] [blame] | 560 | )) |
David Lawrence Ramsey | a084ad9 | 2004-11-07 18:22:33 +0000 | [diff] [blame] | 561 | { |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 562 | statusbar(_("Cancelled")); |
| 563 | break; |
| 564 | } else { |
David Lawrence Ramsey | 31f9936 | 2004-11-05 16:01:04 +0000 | [diff] [blame] | 565 | size_t pww_save = placewewant; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 566 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 567 | ans = mallocstrcpy(ans, answer); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 568 | |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 569 | #if !defined(NANO_SMALL) && defined(ENABLE_MULTIBUFFER) |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 570 | if (i == TOGGLE_MULTIBUFFER_KEY) { |
| 571 | /* Don't allow toggling if we're in view mode. */ |
| 572 | if (!ISSET(VIEW_MODE)) |
| 573 | TOGGLE(MULTIBUFFER); |
| 574 | continue; |
| 575 | } |
David Lawrence Ramsey | 04a8d1c | 2004-09-28 22:14:58 +0000 | [diff] [blame] | 576 | #endif |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 577 | |
Chris Allegretta | 434d686 | 2003-02-03 04:55:17 +0000 | [diff] [blame] | 578 | #ifndef DISABLE_BROWSER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 579 | if (i == NANO_TOFILES_KEY) { |
| 580 | char *tmp = do_browse_from(answer); |
Chris Allegretta | 434d686 | 2003-02-03 04:55:17 +0000 | [diff] [blame] | 581 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 582 | if (tmp == NULL) |
| 583 | continue; |
David Lawrence Ramsey | b49daec | 2004-10-05 02:46:24 +0000 | [diff] [blame] | 584 | free(answer); |
| 585 | answer = tmp; |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 586 | |
| 587 | /* We have a file now. Get out of the statusbar prompt |
| 588 | * cleanly. */ |
| 589 | statusq_abort(); |
David Lawrence Ramsey | b49daec | 2004-10-05 02:46:24 +0000 | [diff] [blame] | 590 | } |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 591 | #endif |
| 592 | |
Chris Allegretta | 52c5a6e | 2002-03-21 05:07:28 +0000 | [diff] [blame] | 593 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 594 | if (i == NANO_TOOTHERINSERT_KEY) { |
| 595 | execute = !execute; |
| 596 | continue; |
| 597 | } |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 598 | #endif |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 599 | |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 600 | #ifdef ENABLE_MULTIBUFFER |
| 601 | if (!ISSET(MULTIBUFFER)) { |
| 602 | #endif |
| 603 | /* If we're not inserting into a new buffer, partition |
| 604 | * the filestruct so that it contains no text and hence |
David Lawrence Ramsey | 4a3879f | 2004-11-15 20:42:31 +0000 | [diff] [blame] | 605 | * looks like a new buffer, and keep track of whether |
| 606 | * the top of the partition is the top of the edit |
| 607 | * window. */ |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 608 | filepart = partition_filestruct(current, current_x, |
| 609 | current, current_x); |
David Lawrence Ramsey | 4a3879f | 2004-11-15 20:42:31 +0000 | [diff] [blame] | 610 | at_edittop = (fileage == edittop); |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 611 | #ifdef ENABLE_MULTIBUFFER |
| 612 | } |
| 613 | #endif |
| 614 | |
| 615 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 616 | if (execute) |
| 617 | execute_command(answer); |
| 618 | else { |
David Lawrence Ramsey | d717d4b | 2004-07-10 16:53:17 +0000 | [diff] [blame] | 619 | #endif |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 620 | answer = mallocstrassn(answer, real_dir_from_tilde(answer)); |
| 621 | load_buffer(answer); |
David Lawrence Ramsey | d717d4b | 2004-07-10 16:53:17 +0000 | [diff] [blame] | 622 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 623 | } |
David Lawrence Ramsey | d717d4b | 2004-07-10 16:53:17 +0000 | [diff] [blame] | 624 | #endif |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 625 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 626 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 627 | if (!ISSET(MULTIBUFFER)) |
| 628 | #endif |
| 629 | { |
| 630 | filestruct *top_save = fileage; |
| 631 | |
David Lawrence Ramsey | 036a3d4 | 2004-11-15 21:49:21 +0000 | [diff] [blame] | 632 | /* If we didn't insert into a new buffer, and we were at |
| 633 | * the top of the edit window before, set the saved |
| 634 | * value of edittop to the new top of the edit window, |
| 635 | * and update the current y-coordinate to account for |
| 636 | * the number of lines inserted. */ |
| 637 | if (at_edittop) |
| 638 | edittop_save = fileage; |
| 639 | current_y += current_y_save; |
| 640 | |
David Lawrence Ramsey | 56cf034 | 2004-11-15 18:44:30 +0000 | [diff] [blame] | 641 | /* If we didn't insert into a new buffer, unpartition |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 642 | * the filestruct so that it contains all the text |
| 643 | * again. Note that we've replaced the non-text |
| 644 | * originally in the partition with the text in the |
| 645 | * inserted file/executed command output. */ |
David Lawrence Ramsey | 74d8707 | 2004-11-22 00:16:23 +0000 | [diff] [blame] | 646 | unpartition_filestruct(&filepart); |
David Lawrence Ramsey | c15802f | 2004-11-07 18:09:41 +0000 | [diff] [blame] | 647 | |
| 648 | /* Renumber starting with the beginning line of the old |
| 649 | * partition. */ |
| 650 | renumber(top_save); |
| 651 | |
| 652 | /* Set edittop back to what it was before. */ |
| 653 | edittop = edittop_save; |
| 654 | } |
| 655 | |
| 656 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 657 | if (ISSET(MULTIBUFFER)) { |
| 658 | /* Update the titlebar. */ |
| 659 | titlebar(NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 660 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 661 | /* Reinitialize the shortcut list. */ |
| 662 | shortcut_init(FALSE); |
| 663 | } else { |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 664 | #endif |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 665 | /* Mark the file as modified. */ |
| 666 | set_modified(); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 667 | |
David Lawrence Ramsey | b73c0c0 | 2004-11-05 15:25:53 +0000 | [diff] [blame] | 668 | /* Restore the old place we want. */ |
David Lawrence Ramsey | 31f9936 | 2004-11-05 16:01:04 +0000 | [diff] [blame] | 669 | placewewant = pww_save; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 670 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 671 | } |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 672 | #endif |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 673 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 674 | /* Refresh the screen. */ |
| 675 | edit_refresh(); |
| 676 | |
| 677 | break; |
| 678 | } |
| 679 | } /* while (TRUE) */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 680 | |
David Lawrence Ramsey | 670a56c | 2004-09-28 16:03:03 +0000 | [diff] [blame] | 681 | free(ans); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 682 | } |
| 683 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 684 | void do_insertfile_void(void) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 685 | { |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 686 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 687 | if (ISSET(VIEW_MODE) && !ISSET(MULTIBUFFER)) |
| 688 | statusbar(_("Key illegal in non-multibuffer mode")); |
Chris Allegretta | 32da456 | 2002-01-02 15:12:21 +0000 | [diff] [blame] | 689 | else |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 690 | #endif |
David Lawrence Ramsey | be908f6 | 2004-10-01 18:34:30 +0000 | [diff] [blame] | 691 | do_insertfile( |
| 692 | #ifndef NANO_SMALL |
| 693 | FALSE |
| 694 | #endif |
| 695 | ); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 696 | |
| 697 | display_main_list(); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 700 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 701 | /* Create a new openfilestruct node. */ |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 702 | openfilestruct *make_new_opennode(void) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 703 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 704 | openfilestruct *newnode = |
| 705 | (openfilestruct *)nmalloc(sizeof(openfilestruct)); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 706 | newnode->filename = NULL; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 707 | return newnode; |
| 708 | } |
| 709 | |
| 710 | /* Splice a node into an existing openfilestruct. */ |
| 711 | void splice_opennode(openfilestruct *begin, openfilestruct *newnode, |
David Lawrence Ramsey | f708037 | 2004-07-08 17:15:10 +0000 | [diff] [blame] | 712 | openfilestruct *end) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 713 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 714 | assert(newnode != NULL && begin != NULL); |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 715 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 716 | newnode->next = end; |
| 717 | newnode->prev = begin; |
| 718 | begin->next = newnode; |
| 719 | if (end != NULL) |
| 720 | end->prev = newnode; |
| 721 | } |
| 722 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 723 | /* Unlink a node from the rest of the openfilestruct, and delete it. */ |
| 724 | void unlink_opennode(openfilestruct *fileptr) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 725 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 726 | assert(fileptr != NULL && fileptr->prev != NULL && fileptr->next != NULL && fileptr != fileptr->prev && fileptr != fileptr->next); |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 727 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 728 | fileptr->prev->next = fileptr->next; |
| 729 | fileptr->next->prev = fileptr->prev; |
| 730 | delete_opennode(fileptr); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* Delete a node from the openfilestruct. */ |
| 734 | void delete_opennode(openfilestruct *fileptr) |
| 735 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 736 | assert(fileptr != NULL && fileptr->filename != NULL && fileptr->fileage != NULL); |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 737 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 738 | free(fileptr->filename); |
| 739 | free_filestruct(fileptr->fileage); |
| 740 | free(fileptr); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 741 | } |
| 742 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 743 | #ifdef DEBUG |
| 744 | /* Deallocate all memory associated with this and later files, including |
| 745 | * the lines of text. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 746 | void free_openfilestruct(openfilestruct *src) |
| 747 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 748 | assert(src != NULL); |
David Lawrence Ramsey | 23c4450 | 2005-01-27 20:49:07 +0000 | [diff] [blame] | 749 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 750 | while (src != src->next) { |
| 751 | src = src->next; |
| 752 | delete_opennode(src->prev); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 753 | } |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 754 | delete_opennode(src); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 755 | } |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 756 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 757 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 758 | /* Add/update an entry to the open_files openfilestruct. If update is |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 759 | * FALSE, a new entry is created; otherwise, the current entry is |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 760 | * updated. */ |
David Lawrence Ramsey | 3e189a8 | 2004-10-03 19:18:48 +0000 | [diff] [blame] | 761 | void add_open_file(bool update) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 762 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 763 | if (open_files == NULL && update) |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 764 | return; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 765 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 766 | /* If there are no entries in open_files, make the first one. */ |
| 767 | if (open_files == NULL) { |
| 768 | open_files = make_new_opennode(); |
| 769 | splice_opennode(open_files, open_files, open_files); |
| 770 | /* Otherwise, if we're not updating, make a new entry for |
| 771 | * open_files and splice it in after the current entry. */ |
| 772 | } else if (!update) { |
| 773 | splice_opennode(open_files, make_new_opennode(), |
| 774 | open_files->next); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 775 | open_files = open_files->next; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 776 | } |
| 777 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 778 | /* Save the current filename. */ |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 779 | open_files->filename = mallocstrcpy(open_files->filename, filename); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 780 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 781 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 782 | /* Save the current file's stat. */ |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 783 | open_files->originalfilestat = originalfilestat; |
| 784 | #endif |
| 785 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 786 | /* Save the current file buffer. */ |
| 787 | open_files->fileage = fileage; |
| 788 | open_files->filebot = filebot; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 789 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 790 | /* Save the current top of the edit window. */ |
| 791 | open_files->edittop = edittop; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 792 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 793 | /* Save the current line. */ |
| 794 | open_files->current = current; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 795 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 796 | /* Save the current cursor position. */ |
| 797 | open_files->current_x = current_x; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 798 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 799 | /* Save the current place we want. */ |
| 800 | open_files->placewewant = placewewant; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 801 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 802 | /* Save the current total number of lines. */ |
| 803 | open_files->totlines = totlines; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 804 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 805 | /* Save the current total size. */ |
| 806 | open_files->totsize = totsize; |
David Lawrence Ramsey | cb34a67 | 2004-02-06 21:20:05 +0000 | [diff] [blame] | 807 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 808 | /* Start with no flags saved. */ |
| 809 | open_files->flags = 0; |
| 810 | |
| 811 | /* Save the current modification status. */ |
| 812 | if (ISSET(MODIFIED)) |
| 813 | open_files->flags |= MODIFIED; |
| 814 | |
David Lawrence Ramsey | a3370c4 | 2004-04-05 01:08:14 +0000 | [diff] [blame] | 815 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 816 | /* Save the current marking status and mark, if applicable. */ |
| 817 | if (ISSET(MARK_ISSET)) { |
| 818 | open_files->flags |= MARK_ISSET; |
| 819 | open_files->mark_beginbuf = mark_beginbuf; |
| 820 | open_files->mark_beginx = mark_beginx; |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 821 | } |
| 822 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 823 | /* Save the current file format. */ |
| 824 | open_files->fmt = fmt; |
| 825 | #endif |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 826 | |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 827 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 828 | fprintf(stderr, "filename is %s\n", open_files->filename); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 829 | #endif |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 830 | } |
| 831 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 832 | /* Read the current entry in the open_files structure and set up the |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 833 | * currently open file buffer using that entry's information. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 834 | void load_open_file(void) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 835 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 836 | assert(open_files != NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 837 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 838 | /* Restore the current filename. */ |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 839 | filename = mallocstrcpy(filename, open_files->filename); |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 840 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 841 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 842 | /* Restore the current file's stat. */ |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 843 | originalfilestat = open_files->originalfilestat; |
| 844 | #endif |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 845 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 846 | /* Restore the current file buffer. */ |
| 847 | fileage = open_files->fileage; |
| 848 | filebot = open_files->filebot; |
| 849 | |
| 850 | /* Restore the current top of the edit window. */ |
| 851 | edittop = open_files->edittop; |
| 852 | |
| 853 | /* Restore the current line. */ |
| 854 | current = open_files->current; |
| 855 | |
| 856 | /* Restore the current cursor position. */ |
| 857 | current_x = open_files->current_x; |
| 858 | |
| 859 | /* Restore the current place we want. */ |
| 860 | placewewant = open_files->placewewant; |
| 861 | |
| 862 | /* Restore the current total number of lines. */ |
| 863 | totlines = open_files->totlines; |
| 864 | |
| 865 | /* Restore the current total size. */ |
| 866 | totsize = open_files->totsize; |
| 867 | |
| 868 | /* Restore the current modification status. */ |
| 869 | if (open_files->flags & MODIFIED) |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 870 | SET(MODIFIED); |
| 871 | else |
| 872 | UNSET(MODIFIED); |
| 873 | |
| 874 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 875 | /* Restore the current marking status and mark, if applicable. */ |
| 876 | if (open_files->flags & MARK_ISSET) { |
| 877 | mark_beginbuf = open_files->mark_beginbuf; |
| 878 | mark_beginx = open_files->mark_beginx; |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 879 | SET(MARK_ISSET); |
| 880 | } else |
| 881 | UNSET(MARK_ISSET); |
David Lawrence Ramsey | 7c1f17a | 2004-10-03 13:47:26 +0000 | [diff] [blame] | 882 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 883 | /* Restore the current file format. */ |
| 884 | fmt = open_files->fmt; |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 885 | #endif |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 886 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 887 | #ifdef ENABLE_COLOR |
| 888 | update_color(); |
| 889 | #endif |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 890 | edit_refresh(); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 891 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 892 | /* Update the titlebar. */ |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 893 | clearok(topwin, FALSE); |
| 894 | titlebar(NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 895 | } |
| 896 | |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 897 | /* Open either the next or previous file buffer. */ |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 898 | void open_prevnext_file(bool next) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 899 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 900 | add_open_file(TRUE); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 901 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 902 | assert(open_files != NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 903 | |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 904 | /* If only one file buffer is open, indicate it on the statusbar and |
| 905 | * get out. */ |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 906 | if (open_files == open_files->next) { |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 907 | statusbar(_("No more open file buffers")); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 908 | return; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 909 | } |
| 910 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 911 | /* Switch to the next or previous file, depending on the value of |
| 912 | * next. */ |
| 913 | open_files = next ? open_files->next : open_files->prev; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 914 | |
| 915 | #ifdef DEBUG |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 916 | fprintf(stderr, "filename is %s\n", open_files->filename); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 917 | #endif |
| 918 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 919 | /* Load the file we switched to. */ |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 920 | load_open_file(); |
| 921 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 922 | /* And indicate the switch on the statusbar. */ |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 923 | statusbar(_("Switched to %s"), |
David Lawrence Ramsey | 3e189a8 | 2004-10-03 19:18:48 +0000 | [diff] [blame] | 924 | ((open_files->filename[0] == '\0') ? _("New Buffer") : |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 925 | open_files->filename)); |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 926 | |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 927 | #ifdef DEBUG |
| 928 | dump_buffer(current); |
| 929 | #endif |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 930 | } |
| 931 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 932 | /* Open the previous entry in the open_files structure. This function |
| 933 | * is used by the shortcut list. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 934 | void open_prevfile_void(void) |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 935 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 936 | open_prevnext_file(FALSE); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 937 | } |
| 938 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 939 | /* Open the next entry in the open_files structure. This function is |
| 940 | * used by the shortcut list. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 941 | void open_nextfile_void(void) |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 942 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 943 | open_prevnext_file(TRUE); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 944 | } |
| 945 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 946 | /* Delete an entry from the open_files filestruct. After deletion of an |
| 947 | * entry, the next entry is opened. Return TRUE on success or FALSE if |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 948 | * there are no more open file buffers. */ |
David Lawrence Ramsey | 3e189a8 | 2004-10-03 19:18:48 +0000 | [diff] [blame] | 949 | bool close_open_file(void) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 950 | { |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 951 | assert(open_files != NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 952 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 953 | /* If only one file is open, get out. */ |
| 954 | if (open_files == open_files->next) |
David Lawrence Ramsey | 3e189a8 | 2004-10-03 19:18:48 +0000 | [diff] [blame] | 955 | return FALSE; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 956 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 957 | /* Open the next file. */ |
| 958 | open_nextfile_void(); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 959 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 960 | /* Close the file we had open before. */ |
| 961 | unlink_opennode(open_files->prev); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 962 | |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 963 | /* Reinitialize the shortcut list. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 964 | shortcut_init(FALSE); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 965 | display_main_list(); |
David Lawrence Ramsey | 5b3dd0f | 2004-11-25 04:39:07 +0000 | [diff] [blame] | 966 | |
David Lawrence Ramsey | 3e189a8 | 2004-10-03 19:18:48 +0000 | [diff] [blame] | 967 | return TRUE; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 968 | } |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 969 | #endif /* ENABLE_MULTIBUFFER */ |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 970 | |
David Lawrence Ramsey | ca018c3 | 2004-11-26 20:14:19 +0000 | [diff] [blame] | 971 | #if !defined(DISABLE_SPELLER) || !defined(DISABLE_OPERATINGDIR) |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 972 | /* When passed "[relative path]" or "[relative path][filename]" in |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 973 | * origpath, return "[full path]" or "[full path][filename]" on success, |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 974 | * or NULL on error. Do this if the file doesn't exist but the relative |
| 975 | * path does, since the file could exist in memory but not yet on disk). |
| 976 | * Don't do this if the relative path doesn't exist, since we won't be |
| 977 | * able to go there. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 978 | char *get_full_path(const char *origpath) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 979 | { |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 980 | char *d_here, *d_there = NULL; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 981 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 982 | if (origpath == NULL) |
| 983 | return NULL; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 984 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 985 | /* Get the current directory. */ |
| 986 | #if PATH_MAX != -1 |
| 987 | d_here = charalloc(PATH_MAX + 1); |
| 988 | #else |
| 989 | d_here = NULL; |
| 990 | #endif |
| 991 | d_here = getcwd(d_here, PATH_MAX + 1); |
| 992 | #if PATH_MAX != -1 |
| 993 | align(&d_here); |
| 994 | #endif |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 995 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 996 | if (d_here != NULL) { |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 997 | const char *last_slash; |
| 998 | char *d_there_file = NULL; |
| 999 | bool path_only; |
| 1000 | struct stat fileinfo; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1001 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1002 | /* If the current directory isn't "/", tack a slash onto the end |
| 1003 | * of it. */ |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 1004 | if (strcmp(d_here, "/") != 0) { |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1005 | d_here = charealloc(d_here, strlen(d_here) + 2); |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 1006 | strcat(d_here, "/"); |
| 1007 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1008 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1009 | d_there = real_dir_from_tilde(origpath); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1010 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1011 | assert(d_there != NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1012 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1013 | /* Stat d_there. If stat() fails, assume that d_there refers to |
| 1014 | * a new file that hasn't been saved to disk yet. Set path_only |
| 1015 | * to TRUE if d_there refers to a directory, and FALSE if |
| 1016 | * d_there refers to a file. */ |
| 1017 | path_only = !stat(d_there, &fileinfo) && |
| 1018 | S_ISDIR(fileinfo.st_mode); |
| 1019 | |
| 1020 | /* If path_only is TRUE, make sure d_there ends in a slash. */ |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1021 | if (path_only) { |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1022 | size_t d_there_len = strlen(d_there); |
| 1023 | |
| 1024 | if (d_there[d_there_len - 1] != '/') { |
| 1025 | d_there = charealloc(d_there, d_there_len + 2); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1026 | strcat(d_there, "/"); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1030 | /* Search for the last slash in d_there. */ |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1031 | last_slash = strrchr(d_there, '/'); |
| 1032 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1033 | /* If we didn't find one, then make sure the answer is in the |
| 1034 | * format "d_here/d_there". */ |
| 1035 | if (last_slash == NULL) { |
| 1036 | assert(!path_only); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1037 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1038 | d_there_file = d_there; |
| 1039 | d_there = d_here; |
| 1040 | } else { |
| 1041 | /* If path_only is FALSE, then save the filename portion of |
| 1042 | * the answer, everything after the last slash, in |
| 1043 | * d_there_file. */ |
| 1044 | if (!path_only) |
| 1045 | d_there_file = mallocstrcpy(NULL, last_slash + 1); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1046 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1047 | /* And remove the filename portion of the answer from |
| 1048 | * d_there. */ |
| 1049 | null_at(&d_there, last_slash - d_there + 1); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1050 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1051 | /* Go to the path specified in d_there. */ |
| 1052 | if (chdir(d_there) == -1) { |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1053 | free(d_there); |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1054 | d_there = NULL; |
| 1055 | } else { |
| 1056 | /* Get the full path and save it in d_there. */ |
| 1057 | free(d_there); |
| 1058 | #if PATH_MAX != -1 |
| 1059 | d_there = charalloc(PATH_MAX + 1); |
| 1060 | #else |
| 1061 | d_there = NULL; |
| 1062 | #endif |
| 1063 | d_there = getcwd(d_there, PATH_MAX + 1); |
| 1064 | #if PATH_MAX != -1 |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1065 | align(&d_there); |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1066 | #endif |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 1067 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1068 | if (d_there == NULL) |
| 1069 | /* If we couldn't get the full path, set path_only |
| 1070 | * to TRUE so that we clean up correctly, free all |
| 1071 | * allocated memory, and return NULL. */ |
| 1072 | path_only = TRUE; |
| 1073 | else if (strcmp(d_there, "/") != 0) { |
| 1074 | /* Make sure d_there ends in a slash. */ |
| 1075 | d_there = charealloc(d_there, strlen(d_there) + 2); |
| 1076 | strcat(d_there, "/"); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1077 | } |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1078 | |
| 1079 | /* Finally, go back to the path specified in d_here, |
| 1080 | * where we were before. */ |
| 1081 | chdir(d_here); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1084 | /* Free d_here, since we're done using it. */ |
| 1085 | free(d_here); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1086 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1087 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1088 | /* At this point, if path_only is FALSE and d_there exists, |
| 1089 | * d_there contains the path portion of the answer and |
| 1090 | * d_there_file contains the filename portion of the answer. If |
| 1091 | * this is the case, tack d_there_file onto the end of |
| 1092 | * d_there, so that d_there contains the complete answer. */ |
| 1093 | if (!path_only && d_there) { |
| 1094 | d_there = charealloc(d_there, strlen(d_there) + |
| 1095 | strlen(d_there_file) + 1); |
| 1096 | strcat(d_there, d_there_file); |
| 1097 | } |
| 1098 | |
| 1099 | /* Free d_there_file, since we're done using it. */ |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1100 | free(d_there_file); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1103 | return d_there; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1104 | } |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 1105 | #endif /* !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */ |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1106 | |
| 1107 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1108 | /* Return the full version of path, as returned by get_full_path(). On |
| 1109 | * error, if path doesn't reference a directory, or if the directory |
| 1110 | * isn't writable, return NULL. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1111 | char *check_writable_directory(const char *path) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1112 | { |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1113 | char *full_path = get_full_path(path); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1114 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1115 | /* If get_full_path() fails, return NULL. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1116 | if (full_path == NULL) |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1117 | return NULL; |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1118 | |
David Lawrence Ramsey | d7ca85c | 2005-02-09 18:56:21 +0000 | [diff] [blame^] | 1119 | /* If we can't write to path or path isn't a directory, return |
| 1120 | * NULL. */ |
| 1121 | if (access(full_path, W_OK) != 0 || |
| 1122 | full_path[strlen(full_path) - 1] != '/') { |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1123 | free(full_path); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1124 | return NULL; |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1125 | } |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1126 | |
| 1127 | /* otherwise, return the full path */ |
| 1128 | return full_path; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * This function accepts a directory name and filename prefix the same |
| 1133 | * way that tempnam() does, determines the location for its temporary |
| 1134 | * file the same way that tempnam() does, safely creates the temporary |
| 1135 | * file there via mkstemp(), and returns the name of the temporary file |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1136 | * the same way that tempnam() does. It does not reference the value of |
| 1137 | * TMP_MAX because the total number of random filenames that it can |
| 1138 | * generate using one prefix is equal to 256**6, which is a sufficiently |
| 1139 | * large number to handle most cases. Since the behavior after tempnam() |
| 1140 | * generates TMP_MAX random filenames is implementation-defined, my |
| 1141 | * implementation is to go on generating random filenames regardless of |
| 1142 | * it. |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1143 | */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1144 | char *safe_tempnam(const char *dirname, const char *filename_prefix) |
| 1145 | { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1146 | char *full_tempdir = NULL; |
| 1147 | const char *TMPDIR_env; |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1148 | int filedesc; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1149 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1150 | /* if $TMPDIR is set and non-empty, set tempdir to it, run it through |
| 1151 | get_full_path(), and save the result in full_tempdir; otherwise, |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1152 | leave full_tempdir set to NULL */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1153 | TMPDIR_env = getenv("TMPDIR"); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1154 | if (TMPDIR_env != NULL && TMPDIR_env[0] != '\0') |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1155 | full_tempdir = check_writable_directory(TMPDIR_env); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1156 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1157 | /* if $TMPDIR is blank or isn't set, or isn't a writable |
| 1158 | directory, and dirname isn't NULL, try it; otherwise, leave |
| 1159 | full_tempdir set to NULL */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1160 | if (full_tempdir == NULL && dirname != NULL) |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1161 | full_tempdir = check_writable_directory(dirname); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* if $TMPDIR is blank or isn't set, or if it isn't a writable |
| 1164 | directory, and dirname is NULL, try P_tmpdir instead */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1165 | if (full_tempdir == NULL) |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1166 | full_tempdir = check_writable_directory(P_tmpdir); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* if P_tmpdir didn't work, use /tmp instead */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1169 | if (full_tempdir == NULL) { |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1170 | full_tempdir = charalloc(6); |
| 1171 | strcpy(full_tempdir, "/tmp/"); |
| 1172 | } |
| 1173 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1174 | full_tempdir = charealloc(full_tempdir, strlen(full_tempdir) + 12); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* like tempnam(), use only the first 5 characters of the prefix */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1177 | strncat(full_tempdir, filename_prefix, 5); |
| 1178 | strcat(full_tempdir, "XXXXXX"); |
| 1179 | filedesc = mkstemp(full_tempdir); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1180 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1181 | /* if mkstemp succeeded, close the resulting file; afterwards, it'll be |
| 1182 | 0 bytes long, so delete it; finally, return the filename (all that's |
| 1183 | left of it) */ |
| 1184 | if (filedesc != -1) { |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1185 | close(filedesc); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1186 | unlink(full_tempdir); |
| 1187 | return full_tempdir; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1188 | } |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1189 | |
| 1190 | free(full_tempdir); |
| 1191 | return NULL; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1192 | } |
| 1193 | #endif /* !DISABLE_SPELLER */ |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1194 | |
| 1195 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1196 | /* Initialize full_operating_dir based on operating_dir. */ |
| 1197 | void init_operating_dir(void) |
| 1198 | { |
| 1199 | assert(full_operating_dir == NULL); |
| 1200 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1201 | if (operating_dir == NULL) |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1202 | return; |
David Lawrence Ramsey | 807681a | 2004-02-27 20:50:01 +0000 | [diff] [blame] | 1203 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1204 | full_operating_dir = get_full_path(operating_dir); |
| 1205 | |
| 1206 | /* If get_full_path() failed or the operating directory is |
David Lawrence Ramsey | 95b7bdb | 2004-02-27 03:21:23 +0000 | [diff] [blame] | 1207 | * inaccessible, unset operating_dir. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1208 | if (full_operating_dir == NULL || chdir(full_operating_dir) == -1) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1209 | free(full_operating_dir); |
| 1210 | full_operating_dir = NULL; |
| 1211 | free(operating_dir); |
| 1212 | operating_dir = NULL; |
| 1213 | } |
| 1214 | } |
| 1215 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1216 | /* Check to see if we're inside the operating directory. Return 0 if we |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1217 | * are, or 1 otherwise. If allow_tabcomp is nonzero, allow incomplete |
| 1218 | * names that would be matches for the operating directory, so that tab |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1219 | * completion will work. */ |
David Lawrence Ramsey | 3e81914 | 2004-12-31 04:10:28 +0000 | [diff] [blame] | 1220 | int check_operating_dir(const char *currpath, bool allow_tabcomp) |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1221 | { |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1222 | /* The char *full_operating_dir is global for mem cleanup. It |
| 1223 | * should have already been initialized by init_operating_dir(). |
| 1224 | * Also, a relative operating directory path will only be handled |
| 1225 | * properly if this is done. */ |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1226 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1227 | char *fullpath; |
| 1228 | int retval = 0; |
| 1229 | const char *whereami1, *whereami2 = NULL; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1230 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1231 | /* If no operating directory is set, don't bother doing anything. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1232 | if (operating_dir == NULL) |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1233 | return 0; |
David Lawrence Ramsey | 6a2877d | 2004-02-28 02:08:15 +0000 | [diff] [blame] | 1234 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1235 | assert(full_operating_dir != NULL); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1236 | |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1237 | fullpath = get_full_path(currpath); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1238 | |
| 1239 | /* fullpath == NULL means some directory in the path doesn't exist |
| 1240 | * or is unreadable. If allow_tabcomp is zero, then currpath is |
| 1241 | * what the user typed somewhere. We don't want to report a |
| 1242 | * non-existent directory as being outside the operating directory, |
| 1243 | * so we return 0. If allow_tabcomp is nonzero, then currpath |
| 1244 | * exists, but is not executable. So we say it isn't in the |
| 1245 | * operating directory. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1246 | if (fullpath == NULL) |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1247 | return allow_tabcomp; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1248 | |
| 1249 | whereami1 = strstr(fullpath, full_operating_dir); |
| 1250 | if (allow_tabcomp) |
| 1251 | whereami2 = strstr(full_operating_dir, fullpath); |
| 1252 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1253 | /* If both searches failed, we're outside the operating directory. |
| 1254 | * Otherwise, check the search results; if the full operating |
| 1255 | * directory path is not at the beginning of the full current path |
| 1256 | * (for normal usage) and vice versa (for tab completion, if we're |
| 1257 | * allowing it), we're outside the operating directory. */ |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1258 | if (whereami1 != fullpath && whereami2 != full_operating_dir) |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1259 | retval = 1; |
| 1260 | free(fullpath); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Otherwise, we're still inside it. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1263 | return retval; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1264 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1265 | #endif |
| 1266 | |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 1267 | #ifndef NANO_SMALL |
| 1268 | void init_backup_dir(void) |
| 1269 | { |
| 1270 | char *full_backup_dir; |
| 1271 | |
| 1272 | if (backup_dir == NULL) |
| 1273 | return; |
| 1274 | |
| 1275 | full_backup_dir = get_full_path(backup_dir); |
| 1276 | |
| 1277 | /* If get_full_path() failed or the backup directory is |
| 1278 | * inaccessible, unset backup_dir. */ |
| 1279 | if (full_backup_dir == NULL || |
| 1280 | full_backup_dir[strlen(full_backup_dir) - 1] != '/') { |
| 1281 | free(full_backup_dir); |
| 1282 | free(backup_dir); |
| 1283 | backup_dir = NULL; |
| 1284 | } else { |
| 1285 | free(backup_dir); |
| 1286 | backup_dir = full_backup_dir; |
| 1287 | } |
| 1288 | } |
| 1289 | #endif |
| 1290 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1291 | /* Read from inn, write to out. We assume inn is opened for reading, |
| 1292 | * and out for writing. We return 0 on success, -1 on read error, -2 on |
| 1293 | * write error. */ |
| 1294 | int copy_file(FILE *inn, FILE *out) |
| 1295 | { |
| 1296 | char buf[BUFSIZ]; |
| 1297 | size_t charsread; |
| 1298 | int retval = 0; |
| 1299 | |
| 1300 | assert(inn != NULL && out != NULL); |
| 1301 | do { |
| 1302 | charsread = fread(buf, sizeof(char), BUFSIZ, inn); |
| 1303 | if (charsread == 0 && ferror(inn)) { |
| 1304 | retval = -1; |
| 1305 | break; |
| 1306 | } |
| 1307 | if (fwrite(buf, sizeof(char), charsread, out) < charsread) { |
| 1308 | retval = -2; |
| 1309 | break; |
| 1310 | } |
| 1311 | } while (charsread > 0); |
| 1312 | if (fclose(inn) == EOF) |
| 1313 | retval = -1; |
| 1314 | if (fclose(out) == EOF) |
| 1315 | retval = -2; |
| 1316 | return retval; |
| 1317 | } |
| 1318 | |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1319 | /* Write a file out. If tmp is FALSE, we set the umask to disallow |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1320 | * anyone else from accessing the file, we don't set the global variable |
| 1321 | * filename to its name, and we don't print out how many lines we wrote |
| 1322 | * on the statusbar. |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1323 | * |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1324 | * tmp means we are writing a temporary file in a secure fashion. We |
| 1325 | * use it when spell checking or dumping the file on an error. |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1326 | * |
Chris Allegretta | 0e9b7aa | 2002-04-16 03:15:47 +0000 | [diff] [blame] | 1327 | * append == 1 means we are appending instead of overwriting. |
| 1328 | * append == 2 means we are prepending instead of overwriting. |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1329 | * |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 1330 | * nonamechange means don't change the current filename. It is ignored |
| 1331 | * if tmp is FALSE or if we're appending/prepending. |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1332 | * |
| 1333 | * Return -1 on error, 1 on success. */ |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1334 | int write_file(const char *name, bool tmp, int append, bool |
| 1335 | nonamechange) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1336 | { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1337 | int retval = -1; |
| 1338 | /* Instead of returning in this function, you should always |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1339 | * merely set retval and then goto cleanup_and_exit. */ |
| 1340 | size_t lineswritten = 0; |
| 1341 | const filestruct *fileptr = fileage; |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 1342 | int fd; |
David Lawrence Ramsey | 0af8604 | 2004-07-07 23:26:08 +0000 | [diff] [blame] | 1343 | /* The file descriptor we use. */ |
David Lawrence Ramsey | 2552907 | 2004-07-06 14:02:44 +0000 | [diff] [blame] | 1344 | mode_t original_umask = 0; |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1345 | /* Our umask, from when nano started. */ |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1346 | bool realexists; |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1347 | /* The result of stat(). TRUE if the file exists, FALSE |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1348 | * otherwise. If name is a link that points nowhere, realexists |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1349 | * is FALSE. */ |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1350 | struct stat st; |
| 1351 | /* The status fields filled in by stat(). */ |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1352 | bool anyexists; |
David Lawrence Ramsey | 0af8604 | 2004-07-07 23:26:08 +0000 | [diff] [blame] | 1353 | /* The result of lstat(). Same as realexists unless name is a |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1354 | * link. */ |
| 1355 | struct stat lst; |
| 1356 | /* The status fields filled in by lstat(). */ |
| 1357 | char *realname; |
David Lawrence Ramsey | 0af8604 | 2004-07-07 23:26:08 +0000 | [diff] [blame] | 1358 | /* name after tilde expansion. */ |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1359 | FILE *f; |
| 1360 | /* The actual file, realname, we are writing to. */ |
| 1361 | char *tempname = NULL; |
| 1362 | /* The temp file name we write to on prepend. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1363 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1364 | assert(name != NULL); |
David Lawrence Ramsey | 951d714 | 2004-10-04 15:23:47 +0000 | [diff] [blame] | 1365 | if (name[0] == '\0') |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1366 | return -1; |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 1367 | if (!tmp) |
| 1368 | titlebar(NULL); |
Chris Allegretta | 0f5dfef | 2000-11-24 14:02:57 +0000 | [diff] [blame] | 1369 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1370 | realname = real_dir_from_tilde(name); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1371 | |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1372 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1373 | /* If we're writing a temporary file, we're probably going outside |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1374 | * the operating directory, so skip the operating directory test. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1375 | if (!tmp && check_operating_dir(realname, FALSE) != 0) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1376 | statusbar(_("Can't write outside of %s"), operating_dir); |
| 1377 | goto cleanup_and_exit; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1378 | } |
| 1379 | #endif |
| 1380 | |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1381 | anyexists = (lstat(realname, &lst) != -1); |
David Lawrence Ramsey | e08765d | 2004-11-26 20:17:49 +0000 | [diff] [blame] | 1382 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1383 | /* New case: if the file exists, just give up. */ |
| 1384 | if (tmp && anyexists) |
| 1385 | goto cleanup_and_exit; |
David Lawrence Ramsey | e08765d | 2004-11-26 20:17:49 +0000 | [diff] [blame] | 1386 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1387 | /* If NOFOLLOW_SYMLINKS is set, it doesn't make sense to prepend or |
| 1388 | * append to a symlink. Here we warn about the contradiction. */ |
| 1389 | if (ISSET(NOFOLLOW_SYMLINKS) && anyexists && S_ISLNK(lst.st_mode)) { |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1390 | statusbar( |
| 1391 | _("Cannot prepend or append to a symlink with --nofollow set")); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1392 | goto cleanup_and_exit; |
| 1393 | } |
| 1394 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1395 | /* Save the state of file at the end of the symlink (if there is |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1396 | * one). */ |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1397 | realexists = (stat(realname, &st) != -1); |
Chris Allegretta | f7ee9e6 | 2000-12-02 21:13:50 +0000 | [diff] [blame] | 1398 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1399 | #ifndef NANO_SMALL |
| 1400 | /* We backup only if the backup toggle is set, the file isn't |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1401 | * temporary, and the file already exists. Furthermore, if we |
| 1402 | * aren't appending, prepending, or writing a selection, we backup |
| 1403 | * only if the file has not been modified by someone else since nano |
| 1404 | * opened it. */ |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1405 | if (ISSET(BACKUP_FILE) && !tmp && realexists && |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1406 | (append != 0 || ISSET(MARK_ISSET) || |
| 1407 | originalfilestat.st_mtime == st.st_mtime)) { |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1408 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1409 | FILE *backup_file; |
| 1410 | char *backupname; |
| 1411 | struct utimbuf filetime; |
| 1412 | int copy_status; |
| 1413 | |
| 1414 | /* Save the original file's access and modification times. */ |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1415 | filetime.actime = originalfilestat.st_atime; |
| 1416 | filetime.modtime = originalfilestat.st_mtime; |
| 1417 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1418 | /* Open the original file to copy to the backup. */ |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1419 | f = fopen(realname, "rb"); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1420 | if (f == NULL) { |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1421 | statusbar(_("Error reading %s: %s"), realname, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1422 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1423 | goto cleanup_and_exit; |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 1426 | /* If backup_dir is set, we set backupname to |
| 1427 | * backup_dir/backupname~, where backupnae is the canonicalized |
| 1428 | * absolute pathname of realname with every '/' replaced with a |
| 1429 | * '!'. This means that /home/foo/file is backed up in |
| 1430 | * backup_dir/!home!foo!file~. */ |
| 1431 | if (backup_dir != NULL) { |
| 1432 | char *canon_realname = get_full_path(realname); |
| 1433 | size_t i; |
| 1434 | |
| 1435 | if (canon_realname == NULL) |
| 1436 | /* If get_full_path() failed, we don't have a |
| 1437 | * canonicalized absolute pathname, so just use the |
| 1438 | * filename portion of the pathname. We use tail() so |
| 1439 | * that e.g. ../backupname will be backed up in |
| 1440 | * backupdir/backupname~ instead of |
| 1441 | * backupdir/../backupname~. */ |
| 1442 | canon_realname = mallocstrcpy(NULL, tail(realname)); |
| 1443 | else { |
| 1444 | for (i = 0; canon_realname[i] != '\0'; i++) { |
| 1445 | if (canon_realname[i] == '/') |
| 1446 | canon_realname[i] = '!'; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | backupname = charalloc(strlen(backup_dir) + |
| 1451 | strlen(canon_realname) + 2); |
| 1452 | sprintf(backupname, "%s%s~", backup_dir, canon_realname); |
| 1453 | free(canon_realname); |
| 1454 | } else { |
| 1455 | backupname = charalloc(strlen(realname) + 2); |
| 1456 | sprintf(backupname, "%s~", realname); |
| 1457 | } |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1458 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1459 | /* Open the destination backup file. Before we write to it, we |
| 1460 | * set its permissions, so no unauthorized person can read it as |
| 1461 | * we write. */ |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1462 | backup_file = fopen(backupname, "wb"); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1463 | if (backup_file == NULL || |
| 1464 | chmod(backupname, originalfilestat.st_mode) == -1) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1465 | statusbar(_("Error writing %s: %s"), backupname, |
| 1466 | strerror(errno)); |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1467 | free(backupname); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1468 | if (backup_file != NULL) |
| 1469 | fclose(backup_file); |
| 1470 | fclose(f); |
| 1471 | goto cleanup_and_exit; |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1475 | fprintf(stderr, "Backing up %s to %s\n", realname, backupname); |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1476 | #endif |
| 1477 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1478 | /* Copy the file. */ |
| 1479 | copy_status = copy_file(f, backup_file); |
David Lawrence Ramsey | e08765d | 2004-11-26 20:17:49 +0000 | [diff] [blame] | 1480 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1481 | /* And set metadata. */ |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1482 | if (copy_status != 0 || chown(backupname, |
| 1483 | originalfilestat.st_uid, originalfilestat.st_gid) == -1 |
| 1484 | || utime(backupname, &filetime) == -1) { |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1485 | free(backupname); |
| 1486 | if (copy_status == -1) |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1487 | statusbar(_("Error reading %s: %s"), realname, |
| 1488 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1489 | else |
| 1490 | statusbar(_("Error writing %s: %s"), backupname, |
| 1491 | strerror(errno)); |
| 1492 | goto cleanup_and_exit; |
| 1493 | } |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1494 | free(backupname); |
| 1495 | } |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1496 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1497 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1498 | /* If NOFOLLOW_SYMLINKS and the file is a link, we aren't doing |
| 1499 | * prepend or append. So we delete the link first, and just |
| 1500 | * overwrite. */ |
| 1501 | if (ISSET(NOFOLLOW_SYMLINKS) && anyexists && S_ISLNK(lst.st_mode) && |
| 1502 | unlink(realname) == -1) { |
| 1503 | statusbar(_("Error writing %s: %s"), realname, strerror(errno)); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1504 | goto cleanup_and_exit; |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1505 | } |
Chris Allegretta | 07f9ee0 | 2000-12-04 05:15:39 +0000 | [diff] [blame] | 1506 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1507 | original_umask = umask(0); |
| 1508 | umask(original_umask); |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1509 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1510 | /* If we create a temp file, we don't let anyone else access it. We |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1511 | * create a temp file if tmp is TRUE or if we're prepending. */ |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1512 | if (tmp || append == 2) |
| 1513 | umask(S_IRWXG | S_IRWXO); |
| 1514 | |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1515 | /* If we're prepending, copy the file to a temp file. */ |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1516 | if (append == 2) { |
| 1517 | int fd_source; |
| 1518 | FILE *f_source = NULL; |
| 1519 | |
| 1520 | tempname = charalloc(strlen(realname) + 8); |
| 1521 | strcpy(tempname, realname); |
| 1522 | strcat(tempname, ".XXXXXX"); |
| 1523 | fd = mkstemp(tempname); |
| 1524 | f = NULL; |
| 1525 | if (fd != -1) { |
| 1526 | f = fdopen(fd, "wb"); |
| 1527 | if (f == NULL) |
| 1528 | close(fd); |
| 1529 | } |
| 1530 | if (f == NULL) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1531 | statusbar(_("Error writing %s: %s"), tempname, |
| 1532 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1533 | unlink(tempname); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1534 | goto cleanup_and_exit; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1535 | } |
Chris Allegretta | 07f9ee0 | 2000-12-04 05:15:39 +0000 | [diff] [blame] | 1536 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1537 | fd_source = open(realname, O_RDONLY | O_CREAT); |
| 1538 | if (fd_source != -1) { |
| 1539 | f_source = fdopen(fd_source, "rb"); |
| 1540 | if (f_source == NULL) |
| 1541 | close(fd_source); |
| 1542 | } |
| 1543 | if (f_source == NULL) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1544 | statusbar(_("Error reading %s: %s"), realname, |
| 1545 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1546 | fclose(f); |
| 1547 | unlink(tempname); |
| 1548 | goto cleanup_and_exit; |
| 1549 | } |
| 1550 | |
| 1551 | if (copy_file(f_source, f) != 0) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1552 | statusbar(_("Error writing %s: %s"), tempname, |
| 1553 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1554 | unlink(tempname); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1555 | goto cleanup_and_exit; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1559 | /* Now open the file in place. Use O_EXCL if tmp is TRUE. This is |
| 1560 | * now copied from joe, because wiggy says so *shrug*. */ |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1561 | fd = open(realname, O_WRONLY | O_CREAT | |
| 1562 | (append == 1 ? O_APPEND : (tmp ? O_EXCL : O_TRUNC)), |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1563 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1564 | |
David Lawrence Ramsey | c8c69d5 | 2004-07-03 03:22:23 +0000 | [diff] [blame] | 1565 | /* Set the umask back to the user's original value. */ |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1566 | umask(original_umask); |
| 1567 | |
| 1568 | /* First, just give up if we couldn't even open the file. */ |
| 1569 | if (fd == -1) { |
| 1570 | statusbar(_("Error writing %s: %s"), realname, strerror(errno)); |
David Lawrence Ramsey | ca92bec | 2004-10-22 03:33:54 +0000 | [diff] [blame] | 1571 | /* tempname has been set only if we're prepending. */ |
| 1572 | if (tempname != NULL) |
| 1573 | unlink(tempname); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1574 | goto cleanup_and_exit; |
| 1575 | } |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 1576 | |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1577 | f = fdopen(fd, append == 1 ? "ab" : "wb"); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1578 | if (f == NULL) { |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1579 | statusbar(_("Error writing %s: %s"), realname, strerror(errno)); |
| 1580 | close(fd); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1581 | goto cleanup_and_exit; |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
David Lawrence Ramsey | 32d19ce | 2004-05-24 05:05:07 +0000 | [diff] [blame] | 1584 | /* There might not be a magicline. There won't be when writing out |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1585 | * a selection. */ |
| 1586 | assert(fileage != NULL && filebot != NULL); |
| 1587 | while (fileptr != filebot) { |
| 1588 | size_t data_len = strlen(fileptr->data); |
| 1589 | size_t size; |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1590 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1591 | /* Newlines to nulls, just before we write to disk. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1592 | sunder(fileptr->data); |
| 1593 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1594 | size = fwrite(fileptr->data, sizeof(char), data_len, f); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1595 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1596 | /* Nulls to newlines; data_len is the string's real length. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1597 | unsunder(fileptr->data, data_len); |
| 1598 | |
Chris Allegretta | 0547eb3 | 2002-04-22 23:52:34 +0000 | [diff] [blame] | 1599 | if (size < data_len) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1600 | statusbar(_("Error writing %s: %s"), realname, |
| 1601 | strerror(errno)); |
Chris Allegretta | b275175 | 2002-04-23 10:22:33 +0000 | [diff] [blame] | 1602 | fclose(f); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1603 | goto cleanup_and_exit; |
Chris Allegretta | b275175 | 2002-04-23 10:22:33 +0000 | [diff] [blame] | 1604 | } |
Chris Allegretta | 9184189 | 2001-09-21 02:37:01 +0000 | [diff] [blame] | 1605 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1606 | if (fmt == DOS_FILE || fmt == MAC_FILE) { |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1607 | if (putc('\r', f) == EOF) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1608 | statusbar(_("Error writing %s: %s"), realname, |
| 1609 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1610 | fclose(f); |
| 1611 | goto cleanup_and_exit; |
| 1612 | } |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1613 | } |
Chris Allegretta | 8fa1e28 | 2001-09-22 04:20:25 +0000 | [diff] [blame] | 1614 | |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1615 | if (fmt != MAC_FILE) { |
Chris Allegretta | 9184189 | 2001-09-21 02:37:01 +0000 | [diff] [blame] | 1616 | #endif |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1617 | if (putc('\n', f) == EOF) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1618 | statusbar(_("Error writing %s: %s"), realname, |
| 1619 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1620 | fclose(f); |
| 1621 | goto cleanup_and_exit; |
| 1622 | } |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1623 | #ifndef NANO_SMALL |
| 1624 | } |
| 1625 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1626 | |
| 1627 | fileptr = fileptr->next; |
| 1628 | lineswritten++; |
| 1629 | } |
| 1630 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1631 | /* If we're prepending, open the temp file, and append it to f. */ |
| 1632 | if (append == 2) { |
| 1633 | int fd_source; |
| 1634 | FILE *f_source = NULL; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1635 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1636 | fd_source = open(tempname, O_RDONLY | O_CREAT); |
| 1637 | if (fd_source != -1) { |
| 1638 | f_source = fdopen(fd_source, "rb"); |
| 1639 | if (f_source == NULL) |
| 1640 | close(fd_source); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1641 | } |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1642 | if (f_source == NULL) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1643 | statusbar(_("Error reading %s: %s"), tempname, |
| 1644 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1645 | fclose(f); |
| 1646 | goto cleanup_and_exit; |
| 1647 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1648 | |
David Lawrence Ramsey | 2c4e34c | 2004-10-22 03:30:39 +0000 | [diff] [blame] | 1649 | if (copy_file(f_source, f) == -1 || unlink(tempname) == -1) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1650 | statusbar(_("Error writing %s: %s"), realname, |
| 1651 | strerror(errno)); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1652 | goto cleanup_and_exit; |
| 1653 | } |
| 1654 | } else if (fclose(f) == EOF) { |
| 1655 | statusbar(_("Error writing %s: %s"), realname, strerror(errno)); |
| 1656 | unlink(tempname); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1657 | goto cleanup_and_exit; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1660 | if (!tmp && append == 0) { |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1661 | if (!nonamechange) { |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1662 | filename = mallocstrcpy(filename, realname); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1663 | #ifdef ENABLE_COLOR |
| 1664 | update_color(); |
David Lawrence Ramsey | 760a2dc | 2004-01-14 06:38:00 +0000 | [diff] [blame] | 1665 | if (ISSET(COLOR_SYNTAX)) |
| 1666 | edit_refresh(); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1667 | #endif |
| 1668 | } |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1669 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1670 | #ifndef NANO_SMALL |
| 1671 | /* Update originalfilestat to reference the file as it is now. */ |
| 1672 | stat(filename, &originalfilestat); |
| 1673 | #endif |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1674 | statusbar(P_("Wrote %u line", "Wrote %u lines", lineswritten), |
| 1675 | lineswritten); |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1676 | UNSET(MODIFIED); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1677 | titlebar(NULL); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1678 | } |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1679 | |
| 1680 | retval = 1; |
| 1681 | |
| 1682 | cleanup_and_exit: |
| 1683 | free(realname); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1684 | free(tempname); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1685 | return retval; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1688 | #ifndef NANO_SMALL |
| 1689 | /* Write a marked selection from a file out. First, set fileage and |
| 1690 | * filebot as the top and bottom of the mark, respectively. Then call |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 1691 | * write_file() with the values of name, temp, and append, and with |
| 1692 | * nonamechange set to TRUE so that we don't change the current |
| 1693 | * filename. Finally, set fileage and filebot back to their old values |
| 1694 | * and return. */ |
David Lawrence Ramsey | 8c16bac | 2004-11-06 15:10:57 +0000 | [diff] [blame] | 1695 | int write_marked(const char *name, bool tmp, int append) |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1696 | { |
| 1697 | int retval = -1; |
David Lawrence Ramsey | 7cfea8b | 2004-10-08 15:35:33 +0000 | [diff] [blame] | 1698 | bool old_modified = ISSET(MODIFIED); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1699 | /* write_file() unsets the MODIFIED flag. */ |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1700 | bool added_magicline; |
| 1701 | /* Whether we added a magicline after filebot. */ |
| 1702 | filestruct *top, *bot; |
| 1703 | size_t top_x, bot_x; |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1704 | |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1705 | /* Partition the filestruct so that it contains only the marked |
| 1706 | * text. */ |
| 1707 | mark_order((const filestruct **)&top, &top_x, |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1708 | (const filestruct **)&bot, &bot_x, NULL); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1709 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1710 | |
| 1711 | /* If the line at filebot is blank, treat it as the magicline and |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1712 | * hence the end of the file. Otherwise, add a magicline and treat |
| 1713 | * it as the end of the file. */ |
| 1714 | added_magicline = (filebot->data[0] != '\0'); |
| 1715 | if (added_magicline) |
| 1716 | new_magicline(); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1717 | |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 1718 | retval = write_file(name, tmp, append, TRUE); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1719 | |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1720 | /* If we added a magicline, remove it now. */ |
| 1721 | if (added_magicline) |
| 1722 | remove_magicline(); |
| 1723 | |
| 1724 | /* Unpartition the filestruct so that it contains all the text |
| 1725 | * again. */ |
David Lawrence Ramsey | 74d8707 | 2004-11-22 00:16:23 +0000 | [diff] [blame] | 1726 | unpartition_filestruct(&filepart); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1727 | |
David Lawrence Ramsey | 7cfea8b | 2004-10-08 15:35:33 +0000 | [diff] [blame] | 1728 | if (old_modified) |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1729 | set_modified(); |
| 1730 | |
| 1731 | return retval; |
| 1732 | } |
| 1733 | #endif /* !NANO_SMALL */ |
| 1734 | |
David Lawrence Ramsey | 951d714 | 2004-10-04 15:23:47 +0000 | [diff] [blame] | 1735 | int do_writeout(bool exiting) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1736 | { |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1737 | int i; |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1738 | int retval = 0, append = 0; |
| 1739 | char *ans; |
| 1740 | /* The last answer the user typed on the statusbar. */ |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1741 | #ifdef NANO_EXTRA |
David Lawrence Ramsey | 056c5ef | 2004-10-05 16:14:19 +0000 | [diff] [blame] | 1742 | static bool did_cred = FALSE; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1743 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1744 | |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1745 | currshortcut = writefile_list; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1746 | |
David Lawrence Ramsey | edab0cc | 2004-07-03 03:09:12 +0000 | [diff] [blame] | 1747 | if (exiting && filename[0] != '\0' && ISSET(TEMP_FILE)) { |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1748 | retval = write_file(filename, FALSE, 0, FALSE); |
| 1749 | |
| 1750 | /* Write succeeded. */ |
| 1751 | if (retval == 1) |
| 1752 | return retval; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Chris Allegretta | 500b5e3 | 2001-06-21 23:58:47 +0000 | [diff] [blame] | 1755 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1756 | if (ISSET(MARK_ISSET) && !exiting) |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1757 | ans = mallocstrcpy(NULL, ""); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1758 | else |
| 1759 | #endif |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1760 | ans = mallocstrcpy(NULL, filename); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1761 | |
| 1762 | while (TRUE) { |
| 1763 | const char *msg; |
| 1764 | #ifndef NANO_SMALL |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1765 | const char *formatstr, *backupstr; |
| 1766 | |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 1767 | if (fmt == DOS_FILE) |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1768 | formatstr = N_(" [DOS Format]"); |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 1769 | else if (fmt == MAC_FILE) |
David Lawrence Ramsey | 360191c | 2004-10-01 21:37:36 +0000 | [diff] [blame] | 1770 | formatstr = N_(" [Mac Format]"); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1771 | else |
| 1772 | formatstr = ""; |
| 1773 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1774 | if (ISSET(BACKUP_FILE)) |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1775 | backupstr = N_(" [Backup]"); |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1776 | else |
| 1777 | backupstr = ""; |
| 1778 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1779 | /* Be nice to the translation folks. */ |
Chris Allegretta | 9519eb0 | 2001-09-27 20:46:25 +0000 | [diff] [blame] | 1780 | if (ISSET(MARK_ISSET) && !exiting) { |
Chris Allegretta | 0e9b7aa | 2002-04-16 03:15:47 +0000 | [diff] [blame] | 1781 | if (append == 2) |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1782 | msg = N_("Prepend Selection to File"); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1783 | else if (append == 1) |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1784 | msg = N_("Append Selection to File"); |
Chris Allegretta | 9519eb0 | 2001-09-27 20:46:25 +0000 | [diff] [blame] | 1785 | else |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1786 | msg = N_("Write Selection to File"); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1787 | } else |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1788 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1789 | if (append == 2) |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1790 | msg = N_("File Name to Prepend to"); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1791 | else if (append == 1) |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1792 | msg = N_("File Name to Append to"); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1793 | else |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 1794 | msg = N_("File Name to Write"); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1795 | |
David Lawrence Ramsey | f7b5d93 | 2004-07-05 14:27:29 +0000 | [diff] [blame] | 1796 | /* If we're using restricted mode, the filename isn't blank, |
| 1797 | * and we're at the "Write File" prompt, disable tab |
| 1798 | * completion. */ |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 1799 | i = statusq(!ISSET(RESTRICTED) || filename[0] == '\0', |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1800 | writefile_list, ans, |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1801 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1802 | NULL, "%s%s%s", _(msg), formatstr, backupstr |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1803 | #else |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1804 | "%s", _(msg) |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1805 | #endif |
| 1806 | ); |
| 1807 | |
David Lawrence Ramsey | 951d714 | 2004-10-04 15:23:47 +0000 | [diff] [blame] | 1808 | if (i < 0) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1809 | statusbar(_("Cancelled")); |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1810 | retval = -1; |
| 1811 | break; |
| 1812 | } else { |
| 1813 | ans = mallocstrcpy(ans, answer); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1814 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1815 | #ifndef DISABLE_BROWSER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1816 | if (i == NANO_TOFILES_KEY) { |
| 1817 | char *tmp = do_browse_from(answer); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1818 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1819 | currshortcut = writefile_list; |
| 1820 | |
| 1821 | if (tmp == NULL) |
| 1822 | continue; |
| 1823 | free(answer); |
| 1824 | answer = tmp; |
| 1825 | |
| 1826 | /* We have a file now. Get out of the statusbar prompt |
| 1827 | * cleanly. */ |
| 1828 | statusq_abort(); |
| 1829 | } else |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1830 | #endif /* !DISABLE_BROWSER */ |
Chris Allegretta | cf287c8 | 2002-07-20 13:57:41 +0000 | [diff] [blame] | 1831 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1832 | if (i == TOGGLE_DOS_KEY) { |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 1833 | fmt = (fmt == DOS_FILE) ? NIX_FILE : DOS_FILE; |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1834 | continue; |
| 1835 | } else if (i == TOGGLE_MAC_KEY) { |
David Lawrence Ramsey | 3e0c8a6 | 2004-11-04 04:08:18 +0000 | [diff] [blame] | 1836 | fmt = (fmt == MAC_FILE) ? NIX_FILE : MAC_FILE; |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1837 | continue; |
| 1838 | } else if (i == TOGGLE_BACKUP_KEY) { |
| 1839 | TOGGLE(BACKUP_FILE); |
| 1840 | continue; |
| 1841 | } else |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1842 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1843 | if (i == NANO_PREPEND_KEY) { |
| 1844 | append = (append == 2) ? 0 : 2; |
| 1845 | continue; |
| 1846 | } else if (i == NANO_APPEND_KEY) { |
| 1847 | append = (append == 1) ? 0 : 1; |
| 1848 | continue; |
| 1849 | } |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1850 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1851 | #ifdef DEBUG |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1852 | fprintf(stderr, "filename is %s\n", answer); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1853 | #endif |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1854 | |
| 1855 | #ifdef NANO_EXTRA |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1856 | if (exiting && !ISSET(TEMP_FILE) && |
| 1857 | strcasecmp(answer, "zzy") == 0 && !did_cred) { |
| 1858 | do_credits(); |
| 1859 | did_cred = TRUE; |
| 1860 | retval = -1; |
| 1861 | break; |
David Lawrence Ramsey | bc503c8 | 2003-11-19 23:59:14 +0000 | [diff] [blame] | 1862 | } |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1863 | #endif |
| 1864 | if (append == 0 && strcmp(answer, filename) != 0) { |
| 1865 | struct stat st; |
| 1866 | |
| 1867 | if (!stat(answer, &st)) { |
| 1868 | i = do_yesno(FALSE, _("File exists, OVERWRITE ? ")); |
| 1869 | if (i == 0 || i == -1) |
| 1870 | continue; |
| 1871 | /* If we're using restricted mode, we aren't allowed to |
| 1872 | * change the name of a file once it has one because |
| 1873 | * that would allow reading from or writing to files not |
| 1874 | * specified on the command line. In this case, don't |
| 1875 | * bother showing the "Different Name" prompt. */ |
| 1876 | } else if (!ISSET(RESTRICTED) && filename[0] != '\0' |
| 1877 | #ifndef NANO_SMALL |
| 1878 | && (exiting || !ISSET(MARK_ISSET)) |
| 1879 | #endif |
| 1880 | ) { |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1881 | i = do_yesno(FALSE, |
| 1882 | _("Save file under DIFFERENT NAME ? ")); |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1883 | if (i == 0 || i == -1) |
| 1884 | continue; |
| 1885 | } |
| 1886 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1887 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1888 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1889 | /* Here's where we allow the selected text to be written to |
| 1890 | * a separate file. If we're using restricted mode, this is |
| 1891 | * disabled since it allows reading from or writing to files |
| 1892 | * not specified on the command line. */ |
| 1893 | if (!ISSET(RESTRICTED) && !exiting && ISSET(MARK_ISSET)) |
| 1894 | retval = write_marked(answer, FALSE, append); |
| 1895 | else |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1896 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1897 | retval = write_file(answer, FALSE, append, FALSE); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1898 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 1899 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1900 | /* If we're not about to exit, update the current entry in |
| 1901 | * the open_files structure. */ |
| 1902 | if (!exiting) |
| 1903 | add_open_file(TRUE); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1904 | #endif |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1905 | |
| 1906 | break; |
| 1907 | } |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1908 | } /* while (TRUE) */ |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1909 | |
| 1910 | free(ans); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1911 | |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1912 | return retval; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1915 | void do_writeout_void(void) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1916 | { |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1917 | do_writeout(FALSE); |
David Lawrence Ramsey | 045883a | 2004-10-05 20:11:31 +0000 | [diff] [blame] | 1918 | display_main_list(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1919 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1920 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1921 | /* Return a malloc()ed string containing the actual directory, used to |
| 1922 | * convert ~user/ and ~/ notation. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1923 | char *real_dir_from_tilde(const char *buf) |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1924 | { |
Chris Allegretta | d865da1 | 2002-07-29 23:46:38 +0000 | [diff] [blame] | 1925 | char *dirtmp = NULL; |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1926 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1927 | if (buf == NULL) |
| 1928 | return NULL; |
| 1929 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1930 | if (buf[0] == '~') { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1931 | size_t i; |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1932 | const char *tilde_dir; |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1933 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1934 | /* Figure out how much of the str we need to compare. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1935 | for (i = 1; buf[i] != '/' && buf[i] != '\0'; i++) |
| 1936 | ; |
| 1937 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1938 | /* Get the home directory. */ |
| 1939 | if (i == 1) { |
| 1940 | get_homedir(); |
| 1941 | tilde_dir = homedir; |
| 1942 | } else { |
| 1943 | const struct passwd *userdata; |
| 1944 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1945 | do { |
| 1946 | userdata = getpwent(); |
| 1947 | } while (userdata != NULL && |
David Lawrence Ramsey | 2dbcc80 | 2004-11-26 18:10:07 +0000 | [diff] [blame] | 1948 | strncmp(userdata->pw_name, buf + 1, i - 1) != 0); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1949 | endpwent(); |
| 1950 | tilde_dir = userdata->pw_dir; |
Chris Allegretta | d865da1 | 2002-07-29 23:46:38 +0000 | [diff] [blame] | 1951 | } |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1952 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1953 | if (tilde_dir != NULL) { |
| 1954 | dirtmp = charalloc(strlen(tilde_dir) + strlen(buf + i) + 1); |
| 1955 | sprintf(dirtmp, "%s%s", tilde_dir, buf + i); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1956 | } |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1957 | } |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1958 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1959 | /* Set a default value for dirtmp, in case the user's home directory |
| 1960 | * isn't found. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1961 | if (dirtmp == NULL) |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1962 | dirtmp = mallocstrcpy(NULL, buf); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1963 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1964 | return dirtmp; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1967 | #if !defined(DISABLE_TABCOMP) || !defined(DISABLE_BROWSER) |
| 1968 | /* Our sort routine for file listings. Sort directories before |
| 1969 | * filenames, alphabetically and ignoring case differences. Sort |
| 1970 | * filenames the same way, except for ignoring an initial dot. */ |
| 1971 | int diralphasort(const void *va, const void *vb) |
| 1972 | { |
| 1973 | struct stat fileinfo; |
| 1974 | const char *a = *(const char *const *)va; |
| 1975 | const char *b = *(const char *const *)vb; |
| 1976 | bool aisdir = stat(a, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode); |
| 1977 | bool bisdir = stat(b, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode); |
| 1978 | |
| 1979 | if (aisdir && !bisdir) |
| 1980 | return -1; |
| 1981 | if (!aisdir && bisdir) |
| 1982 | return 1; |
| 1983 | |
| 1984 | if (*a == '.') |
| 1985 | a++; |
| 1986 | if (*b == '.') |
| 1987 | b++; |
| 1988 | |
| 1989 | return strcasecmp(a, b); |
| 1990 | } |
| 1991 | #endif |
| 1992 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1993 | #ifndef DISABLE_TABCOMP |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1994 | /* Is the given file a directory? */ |
| 1995 | int is_dir(const char *buf) |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1996 | { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 1997 | char *dirptr = real_dir_from_tilde(buf); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1998 | struct stat fileinfo; |
| 1999 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2000 | int ret = (stat(dirptr, &fileinfo) != -1 && |
| 2001 | S_ISDIR(fileinfo.st_mode)); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2002 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2003 | assert(buf != NULL && dirptr != buf); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2004 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2005 | free(dirptr); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2006 | |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 2007 | return ret; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2008 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2009 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2010 | /* These functions (username_tab_completion(), cwd_tab_completion(), and |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2011 | * input_tab()) were taken from busybox 0.46 (cmdedit.c). Here is the |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2012 | * notice from that file: |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2013 | * |
| 2014 | * Termios command line History and Editting, originally |
| 2015 | * intended for NetBSD sh (ash) |
| 2016 | * Copyright (c) 1999 |
| 2017 | * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu> |
| 2018 | * Etc: Dave Cinege <dcinege@psychosis.com> |
| 2019 | * Majorly adjusted/re-written for busybox: |
| 2020 | * Erik Andersen <andersee@debian.org> |
| 2021 | * |
| 2022 | * You may use this code as you wish, so long as the original author(s) |
| 2023 | * are attributed in any redistributions of the source code. |
| 2024 | * This code is 'as is' with no warranty. |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2025 | * This code may safely be consumed by a BSD or GPL license. */ |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2026 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2027 | /* We consider the first buflen characters of buf for ~username tab |
| 2028 | * completion. */ |
| 2029 | char **username_tab_completion(const char *buf, size_t *num_matches, |
| 2030 | size_t buflen) |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2031 | { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2032 | char **matches = NULL; |
| 2033 | const struct passwd *userdata; |
| 2034 | |
| 2035 | assert(buf != NULL && num_matches != NULL && buflen > 0); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2036 | |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 2037 | *num_matches = 0; |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 2038 | |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 2039 | while ((userdata = getpwent()) != NULL) { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2040 | if (strncmp(userdata->pw_name, buf + 1, buflen - 1) == 0) { |
| 2041 | /* Cool, found a match. Add it to the list. This makes a |
| 2042 | * lot more sense to me (Chris) this way... */ |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 2043 | |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2044 | #ifndef DISABLE_OPERATINGDIR |
| 2045 | /* ...unless the match exists outside the operating |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2046 | * directory, in which case just go to the next match. */ |
| 2047 | if (check_operating_dir(userdata->pw_dir, TRUE)) |
| 2048 | continue; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2049 | #endif |
| 2050 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2051 | matches = (char **)nrealloc(matches, (*num_matches + 1) * |
| 2052 | sizeof(char *)); |
| 2053 | matches[*num_matches] = |
| 2054 | charalloc(strlen(userdata->pw_name) + 2); |
| 2055 | sprintf(matches[*num_matches], "~%s", userdata->pw_name); |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 2056 | ++(*num_matches); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2057 | } |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 2058 | } |
| 2059 | endpwent(); |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 2060 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2061 | return matches; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | /* This was originally called exe_n_cwd_tab_completion, but we're not |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2065 | * worried about executables, only filenames :> */ |
| 2066 | char **cwd_tab_completion(const char *buf, size_t *num_matches, size_t |
| 2067 | buflen) |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2068 | { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2069 | char *dirname = mallocstrcpy(NULL, buf); |
| 2070 | char *filename; |
| 2071 | #ifndef DISABLE_OPERATINGDIR |
| 2072 | size_t dirnamelen; |
| 2073 | #endif |
| 2074 | size_t filenamelen; |
| 2075 | char **matches = NULL; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2076 | DIR *dir; |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2077 | const struct dirent *next; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2078 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2079 | assert(dirname != NULL && num_matches != NULL && buflen >= 0); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2080 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2081 | *num_matches = 0; |
| 2082 | null_at(&dirname, buflen); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2083 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2084 | /* Okie, if there's a / in the buffer, strip out the directory |
| 2085 | * part. */ |
| 2086 | filename = strrchr(dirname, '/'); |
| 2087 | if (filename != NULL) { |
| 2088 | char *tmpdirname = filename + 1; |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2089 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2090 | filename = mallocstrcpy(NULL, tmpdirname); |
| 2091 | *tmpdirname = '\0'; |
| 2092 | tmpdirname = dirname; |
| 2093 | dirname = real_dir_from_tilde(dirname); |
| 2094 | free(tmpdirname); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2095 | } else { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2096 | filename = dirname; |
| 2097 | dirname = mallocstrcpy(NULL, "./"); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2100 | assert(dirname[strlen(dirname) - 1] == '/'); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2101 | |
Chris Allegretta | cf287c8 | 2002-07-20 13:57:41 +0000 | [diff] [blame] | 2102 | dir = opendir(dirname); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2103 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2104 | if (dir == NULL) { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2105 | /* Don't print an error, just shut up and return. */ |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2106 | beep(); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2107 | free(filename); |
| 2108 | free(dirname); |
| 2109 | return NULL; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2110 | } |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2111 | |
| 2112 | #ifndef DISABLE_OPERATINGDIR |
| 2113 | dirnamelen = strlen(dirname); |
| 2114 | #endif |
| 2115 | filenamelen = strlen(filename); |
| 2116 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2117 | while ((next = readdir(dir)) != NULL) { |
| 2118 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2119 | #ifdef DEBUG |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 2120 | fprintf(stderr, "Comparing \'%s\'\n", next->d_name); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2121 | #endif |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2122 | /* See if this matches. */ |
| 2123 | if (strncmp(next->d_name, filename, filenamelen) == 0 && |
| 2124 | (*filename == '.' || (strcmp(next->d_name, ".") != 0 && |
| 2125 | strcmp(next->d_name, "..") != 0))) { |
| 2126 | /* Cool, found a match. Add it to the list. This makes a |
| 2127 | * lot more sense to me (Chris) this way... */ |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2128 | |
| 2129 | #ifndef DISABLE_OPERATINGDIR |
| 2130 | /* ...unless the match exists outside the operating |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2131 | * directory, in which case just go to the next match. To |
| 2132 | * properly do operating directory checking, we have to add |
| 2133 | * the directory name to the beginning of the proposed match |
| 2134 | * before we check it. */ |
| 2135 | char *tmp2 = charalloc(strlen(dirname) + |
| 2136 | strlen(next->d_name) + 1); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2137 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2138 | sprintf(tmp2, "%s%s", dirname, next->d_name); |
| 2139 | if (check_operating_dir(tmp2, TRUE)) { |
| 2140 | free(tmp2); |
| 2141 | continue; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2142 | } |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2143 | free(tmp2); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2144 | #endif |
| 2145 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2146 | matches = (char **)nrealloc(matches, (*num_matches + 1) * |
| 2147 | sizeof(char *)); |
| 2148 | matches[*num_matches] = mallocstrcpy(NULL, next->d_name); |
| 2149 | ++(*num_matches); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
Chris Allegretta | 3cdf6ff | 2003-02-08 02:02:02 +0000 | [diff] [blame] | 2152 | closedir(dir); |
| 2153 | free(dirname); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2154 | free(filename); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2155 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2156 | return matches; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2159 | /* Do tab completion. This function now has an arg which refers to how |
| 2160 | * much the statusbar cursor position (place) should be advanced. */ |
| 2161 | char *input_tab(char *buf, size_t *place, bool *lastwastab, bool *list) |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2162 | { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2163 | size_t num_matches = 0; |
| 2164 | char **matches = NULL; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2165 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2166 | assert(buf != NULL && place != NULL && *place <= strlen(buf) && lastwastab != NULL && list != NULL); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2167 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2168 | *list = 0; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2169 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2170 | /* If the word starts with `~' and there is no slash in the word, |
| 2171 | * then try completing this word as a username. */ |
| 2172 | if (*place > 0 && *buf == '~') { |
| 2173 | const char *bob = strchr(buf, '/'); |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 2174 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2175 | if (bob == NULL || bob >= buf + *place) |
| 2176 | matches = username_tab_completion(buf, &num_matches, |
| 2177 | *place); |
| 2178 | } |
Chris Allegretta | e118acc | 2000-11-06 05:40:03 +0000 | [diff] [blame] | 2179 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2180 | /* Match against files relative to the current working directory. */ |
| 2181 | if (matches == NULL) |
| 2182 | matches = cwd_tab_completion(buf, &num_matches, *place); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2183 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2184 | if (num_matches <= 0) |
| 2185 | beep(); |
| 2186 | else { |
| 2187 | size_t match, common_len = 0; |
| 2188 | size_t lastslash = strrchrn(buf, '/', *place); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2189 | char *mzero; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2190 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2191 | while (TRUE) { |
| 2192 | for (match = 1; match < num_matches; match++) { |
| 2193 | if (matches[0][common_len] != |
| 2194 | matches[match][common_len]) |
| 2195 | break; |
| 2196 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2197 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2198 | if (match < num_matches || matches[0][common_len] == '\0') |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 2199 | break; |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2200 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2201 | common_len++; |
| 2202 | } |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2203 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2204 | mzero = charalloc(lastslash + common_len + 1); |
| 2205 | sprintf(mzero, "%.*s%.*s", lastslash, buf, common_len, |
| 2206 | matches[0]); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2207 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2208 | common_len += lastslash; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2209 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2210 | assert(common_len >= *place); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 2211 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2212 | if (num_matches == 1 && is_dir(mzero)) { |
| 2213 | mzero[common_len] = '/'; |
| 2214 | common_len++; |
| 2215 | assert(common_len > *place); |
| 2216 | } |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 2217 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2218 | if (num_matches > 1 && (common_len != *place || |
| 2219 | *lastwastab == FALSE)) |
| 2220 | beep(); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2221 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2222 | /* If there is more match to display on the statusbar, show it. |
| 2223 | * We reset lastwastab to FALSE: it requires hitting Tab twice |
| 2224 | * in succession with no statusbar changes to see a match |
| 2225 | * list. */ |
| 2226 | if (common_len != *place) { |
| 2227 | size_t buflen = strlen(buf); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2228 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2229 | *lastwastab = FALSE; |
| 2230 | buf = charealloc(buf, common_len + buflen - *place + 1); |
| 2231 | charmove(buf + common_len, buf + *place, buflen - *place + 1); |
| 2232 | strncpy(buf, mzero, common_len); |
| 2233 | *place = common_len; |
| 2234 | } else if (*lastwastab == FALSE || num_matches < 2) |
| 2235 | *lastwastab = TRUE; |
| 2236 | else { |
| 2237 | int longest_name = 0, editline = 0; |
| 2238 | size_t columns; |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 2239 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2240 | /* Now we show a list of the available choices. */ |
| 2241 | assert(num_matches > 1); |
| 2242 | |
| 2243 | /* Sort the list. */ |
| 2244 | qsort(matches, num_matches, sizeof(char *), diralphasort); |
| 2245 | |
| 2246 | for (match = 0; match < num_matches; match++) { |
| 2247 | common_len = strnlenpt(matches[match], COLS - 1); |
| 2248 | if (common_len > COLS - 1) { |
| 2249 | longest_name = COLS - 1; |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | } |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2252 | if (common_len > longest_name) |
| 2253 | longest_name = common_len; |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 2254 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2255 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2256 | assert(longest_name <= COLS - 1); |
| 2257 | |
| 2258 | /* Each column will be longest_name + 2 characters wide, |
| 2259 | * i.e, two spaces between columns, except that there will |
| 2260 | * be only one space after the last column. */ |
| 2261 | columns = (COLS + 1) / (longest_name + 2); |
| 2262 | |
| 2263 | /* Blank the edit window, and print the matches out |
| 2264 | * there. */ |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2265 | blank_edit(); |
| 2266 | wmove(edit, 0, 0); |
| 2267 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2268 | /* Disable el cursor. */ |
| 2269 | curs_set(0); |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 2270 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2271 | for (match = 0; match < num_matches; match++) { |
| 2272 | char *disp; |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 2273 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2274 | wmove(edit, editline, (longest_name + 2) * |
| 2275 | (match % columns)); |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 2276 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2277 | if (match % columns == 0 && editline == editwinrows - 1 |
| 2278 | && num_matches - match > columns) { |
| 2279 | waddstr(edit, _("(more)")); |
| 2280 | break; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2281 | } |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2282 | |
| 2283 | disp = display_string(matches[match], 0, longest_name, |
| 2284 | FALSE); |
| 2285 | waddstr(edit, disp); |
| 2286 | free(disp); |
| 2287 | |
| 2288 | if ((match + 1) % columns == 0) |
| 2289 | editline++; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2290 | } |
| 2291 | wrefresh(edit); |
David Lawrence Ramsey | 4d44d2d | 2004-08-01 22:35:31 +0000 | [diff] [blame] | 2292 | *list = TRUE; |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | free(mzero); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2298 | free_charptrarray(matches, num_matches); |
| 2299 | |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2300 | /* Only refresh the edit window if we don't have a list of filename |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2301 | * matches on it. */ |
David Lawrence Ramsey | 4d44d2d | 2004-08-01 22:35:31 +0000 | [diff] [blame] | 2302 | if (*list == FALSE) |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2303 | edit_refresh(); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2304 | |
| 2305 | /* Enable el cursor. */ |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2306 | curs_set(1); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2307 | |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 2308 | return buf; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 2309 | } |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2310 | #endif /* !DISABLE_TABCOMP */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2311 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2312 | /* Only print the last part of a path. Isn't there a shell command for |
| 2313 | * this? */ |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2314 | const char *tail(const char *foo) |
| 2315 | { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2316 | const char *tmp = strrchr(foo, '/'); |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2317 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2318 | if (tmp == NULL) |
| 2319 | tmp = foo; |
| 2320 | else if (*tmp == '/') |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2321 | tmp++; |
| 2322 | |
| 2323 | return tmp; |
| 2324 | } |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2325 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 2326 | #ifndef DISABLE_BROWSER |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2327 | /* Free our malloc()ed memory. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2328 | void free_charptrarray(char **array, size_t len) |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2329 | { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2330 | for (; len > 0; len--) |
| 2331 | free(array[len - 1]); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2332 | free(array); |
| 2333 | } |
| 2334 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2335 | /* Strip one dir from the end of a string. */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2336 | void striponedir(char *foo) |
| 2337 | { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2338 | char *tmp; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2339 | |
Chris Allegretta | e9b5c6f | 2003-02-12 23:49:56 +0000 | [diff] [blame] | 2340 | assert(foo != NULL); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2341 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2342 | tmp = strrchr(foo, '/'); |
| 2343 | if (tmp != NULL) |
| 2344 | *tmp = '\0'; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2347 | int readable_dir(const char *path) |
| 2348 | { |
| 2349 | DIR *dir = opendir(path); |
| 2350 | |
| 2351 | /* If dir is NULL, don't do closedir(), since that changes errno. */ |
| 2352 | if (dir != NULL) |
| 2353 | closedir(dir); |
| 2354 | return dir != NULL; |
| 2355 | } |
| 2356 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2357 | /* Initialize the browser code, including the list of files in *path */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2358 | char **browser_init(const char *path, int *longest, int *numents) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2359 | { |
| 2360 | DIR *dir; |
| 2361 | struct dirent *next; |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2362 | char **filelist; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2363 | int i = 0; |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2364 | size_t path_len; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2365 | |
| 2366 | dir = opendir(path); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2367 | if (dir == NULL) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2368 | return NULL; |
| 2369 | |
| 2370 | *numents = 0; |
| 2371 | while ((next = readdir(dir)) != NULL) { |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2372 | if (strcmp(next->d_name, ".") == 0) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2373 | continue; |
| 2374 | (*numents)++; |
| 2375 | if (strlen(next->d_name) > *longest) |
| 2376 | *longest = strlen(next->d_name); |
| 2377 | } |
| 2378 | rewinddir(dir); |
| 2379 | *longest += 10; |
| 2380 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 2381 | filelist = (char **)nmalloc(*numents * sizeof (char *)); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2382 | |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2383 | if (strcmp(path, "/") == 0) |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2384 | path = ""; |
| 2385 | path_len = strlen(path); |
| 2386 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2387 | while ((next = readdir(dir)) != NULL) { |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2388 | if (strcmp(next->d_name, ".") == 0) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2389 | continue; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2390 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2391 | filelist[i] = charalloc(strlen(next->d_name) + path_len + 2); |
| 2392 | sprintf(filelist[i], "%s/%s", path, next->d_name); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2393 | i++; |
| 2394 | } |
Chris Allegretta | e9b5c6f | 2003-02-12 23:49:56 +0000 | [diff] [blame] | 2395 | closedir(dir); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2396 | |
| 2397 | if (*longest > COLS - 1) |
| 2398 | *longest = COLS - 1; |
| 2399 | |
| 2400 | return filelist; |
| 2401 | } |
| 2402 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2403 | /* Our browser function. inpath is the path to start browsing from */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2404 | char *do_browser(const char *inpath) |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2405 | { |
| 2406 | struct stat st; |
| 2407 | char *foo, *retval = NULL; |
| 2408 | static char *path = NULL; |
David Lawrence Ramsey | a0b5ba2 | 2004-08-25 15:39:10 +0000 | [diff] [blame] | 2409 | int numents = 0, i = 0, j = 0, longest = 0, abort = 0, col = 0; |
| 2410 | int selected = 0, editline = 0, width = 0, filecols = 0, lineno = 0; |
| 2411 | int kbinput = ERR; |
David Lawrence Ramsey | eb16f43 | 2004-09-27 01:04:50 +0000 | [diff] [blame] | 2412 | bool meta_key, func_key; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2413 | char **filelist = (char **)NULL; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2414 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2415 | MEVENT mevent; |
| 2416 | #endif |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2417 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2418 | assert(inpath != NULL); |
| 2419 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2420 | /* If path isn't the same as inpath, we are being passed a new |
| 2421 | dir as an arg. We free it here so it will be copied from |
| 2422 | inpath below */ |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2423 | if (path != NULL && strcmp(path, inpath) != 0) { |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2424 | free(path); |
| 2425 | path = NULL; |
| 2426 | } |
| 2427 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2428 | /* if path doesn't exist, make it so */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2429 | if (path == NULL) |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2430 | path = mallocstrcpy(NULL, inpath); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2431 | |
| 2432 | filelist = browser_init(path, &longest, &numents); |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 2433 | foo = charalloc(longest + 8); |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2434 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 2435 | /* Sort the list. */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2436 | qsort(filelist, numents, sizeof(char *), diralphasort); |
| 2437 | |
| 2438 | titlebar(path); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2439 | bottombars(browser_list); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2440 | curs_set(0); |
| 2441 | wmove(edit, 0, 0); |
| 2442 | i = 0; |
| 2443 | width = 0; |
| 2444 | filecols = 0; |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2445 | |
| 2446 | /* Loop invariant: Microsoft sucks. */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2447 | do { |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2448 | char *new_path; |
| 2449 | /* Used by the Go To Directory prompt. */ |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2450 | |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 2451 | check_statusblank(); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2452 | |
Chris Allegretta | b10283c | 2001-05-07 12:08:37 +0000 | [diff] [blame] | 2453 | currshortcut = browser_list; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 2454 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2455 | editline = 0; |
| 2456 | col = 0; |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 2457 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2458 | /* Compute line number we're on now, so we don't divide by zero later */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2459 | lineno = selected; |
| 2460 | if (width != 0) |
| 2461 | lineno /= width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2462 | |
| 2463 | switch (kbinput) { |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 2464 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2465 | #ifndef DISABLE_MOUSE |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2466 | case KEY_MOUSE: |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2467 | if (getmouse(&mevent) == ERR) |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2468 | return retval; |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2469 | |
| 2470 | /* If they clicked in the edit window, they probably clicked |
| 2471 | on a file */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2472 | if (wenclose(edit, mevent.y, mevent.x)) { |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2473 | int selectedbackup = selected; |
| 2474 | |
| 2475 | mevent.y -= 2; |
| 2476 | |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2477 | /* Longest is the width of each column. There are two |
| 2478 | * spaces between each column. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2479 | selected = (lineno / editwinrows) * editwinrows * width |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2480 | + mevent.y * width + mevent.x / (longest + 2); |
| 2481 | |
| 2482 | /* If they clicked beyond the end of a row, select the |
| 2483 | * end of that row. */ |
| 2484 | if (mevent.x > width * (longest + 2)) |
| 2485 | selected--; |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2486 | |
| 2487 | /* If we're off the screen, reset to the last item. |
| 2488 | If we clicked where we did last time, select this name! */ |
Chris Allegretta | f3fde7c | 2001-05-06 03:02:21 +0000 | [diff] [blame] | 2489 | if (selected > numents - 1) |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2490 | selected = numents - 1; |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2491 | else if (selectedbackup == selected) |
David Lawrence Ramsey | 63e73cb | 2004-11-28 04:52:57 +0000 | [diff] [blame] | 2492 | /* Put back the 'select' key */ |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 2493 | unget_kbinput('s', FALSE, FALSE); |
David Lawrence Ramsey | c59979f | 2004-10-23 02:47:39 +0000 | [diff] [blame] | 2494 | } else { |
| 2495 | /* Must be clicking a shortcut */ |
David Lawrence Ramsey | a0b5ba2 | 2004-08-25 15:39:10 +0000 | [diff] [blame] | 2496 | int mouse_x, mouse_y; |
| 2497 | get_mouseinput(&mouse_x, &mouse_y, TRUE); |
| 2498 | } |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 2499 | |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | #endif |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 2502 | case NANO_PREVLINE_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2503 | if (selected - width >= 0) |
| 2504 | selected -= width; |
| 2505 | break; |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 2506 | case NANO_BACK_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2507 | if (selected > 0) |
| 2508 | selected--; |
| 2509 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 2510 | case NANO_NEXTLINE_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2511 | if (selected + width <= numents - 1) |
| 2512 | selected += width; |
| 2513 | break; |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 2514 | case NANO_FORWARD_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2515 | if (selected < numents - 1) |
| 2516 | selected++; |
| 2517 | break; |
| 2518 | case NANO_PREVPAGE_KEY: |
Chris Allegretta | 6e82741 | 2001-01-04 04:41:08 +0000 | [diff] [blame] | 2519 | case NANO_PREVPAGE_FKEY: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2520 | case '-': /* Pico compatibility */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2521 | if (selected >= (editwinrows + lineno % editwinrows) * width) |
David Lawrence Ramsey | c372488 | 2004-05-27 18:39:16 +0000 | [diff] [blame] | 2522 | selected -= (editwinrows + lineno % editwinrows) * width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2523 | else |
| 2524 | selected = 0; |
| 2525 | break; |
| 2526 | case NANO_NEXTPAGE_KEY: |
Chris Allegretta | 6e82741 | 2001-01-04 04:41:08 +0000 | [diff] [blame] | 2527 | case NANO_NEXTPAGE_FKEY: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2528 | case ' ': /* Pico compatibility */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2529 | selected += (editwinrows - lineno % editwinrows) * width; |
| 2530 | if (selected >= numents) |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2531 | selected = numents - 1; |
| 2532 | break; |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2533 | case NANO_HELP_KEY: |
| 2534 | case NANO_HELP_FKEY: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2535 | case '?': /* Pico compatibility */ |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 2536 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | e7638ea | 2004-06-01 19:49:38 +0000 | [diff] [blame] | 2537 | do_help(); |
David Lawrence Ramsey | ae064bf | 2004-06-01 20:38:00 +0000 | [diff] [blame] | 2538 | curs_set(0); |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 2539 | #else |
| 2540 | nano_disabled_msg(); |
| 2541 | #endif |
David Lawrence Ramsey | e7638ea | 2004-06-01 19:49:38 +0000 | [diff] [blame] | 2542 | break; |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 2543 | case NANO_ENTER_KEY: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2544 | case 'S': /* Pico compatibility */ |
| 2545 | case 's': |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2546 | /* You can't cd up from / */ |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2547 | if (strcmp(filelist[selected], "/..") == 0 && |
| 2548 | strcmp(path, "/") == 0) { |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2549 | statusbar(_("Can't move up a directory")); |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2550 | beep(); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2551 | break; |
| 2552 | } |
| 2553 | |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2554 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2555 | /* Note: the selected file can be outside the operating |
| 2556 | * directory if it is .. or if it is a symlink to |
| 2557 | * directory outside the operating directory. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2558 | if (check_operating_dir(filelist[selected], FALSE) != 0) { |
David Lawrence Ramsey | 36dd87b | 2004-07-18 17:43:43 +0000 | [diff] [blame] | 2559 | statusbar(_("Can't go outside of %s in restricted mode"), |
| 2560 | operating_dir); |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2561 | beep(); |
| 2562 | break; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2563 | } |
| 2564 | #endif |
| 2565 | |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2566 | if (stat(filelist[selected], &st) == -1) { |
David Lawrence Ramsey | 36dd87b | 2004-07-18 17:43:43 +0000 | [diff] [blame] | 2567 | statusbar(_("Can't open \"%s\": %s"), filelist[selected], |
| 2568 | strerror(errno)); |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2569 | beep(); |
| 2570 | break; |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2573 | if (!S_ISDIR(st.st_mode)) { |
| 2574 | retval = mallocstrcpy(retval, filelist[selected]); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2575 | abort = 1; |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2576 | break; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2577 | } |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2578 | |
| 2579 | new_path = mallocstrcpy(NULL, filelist[selected]); |
| 2580 | |
| 2581 | if (strcmp("..", tail(new_path)) == 0) { |
| 2582 | /* They want to go up a level, so strip off .. and the |
| 2583 | current dir */ |
| 2584 | striponedir(new_path); |
| 2585 | /* SPK for '.' path, get the current path via getcwd */ |
| 2586 | if (strcmp(new_path, ".") == 0) { |
| 2587 | free(new_path); |
| 2588 | new_path = getcwd(NULL, PATH_MAX + 1); |
| 2589 | } |
| 2590 | striponedir(new_path); |
| 2591 | } |
| 2592 | |
| 2593 | if (!readable_dir(new_path)) { |
| 2594 | /* We can't open this dir for some reason. Complain */ |
David Lawrence Ramsey | 36dd87b | 2004-07-18 17:43:43 +0000 | [diff] [blame] | 2595 | statusbar(_("Can't open \"%s\": %s"), new_path, |
| 2596 | strerror(errno)); |
Chris Allegretta | 0eab236 | 2003-02-03 03:39:05 +0000 | [diff] [blame] | 2597 | free(new_path); |
| 2598 | break; |
| 2599 | } |
| 2600 | |
| 2601 | free_charptrarray(filelist, numents); |
| 2602 | free(foo); |
| 2603 | free(path); |
| 2604 | path = new_path; |
| 2605 | return do_browser(path); |
| 2606 | |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2607 | /* Go to a specific directory */ |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 2608 | case NANO_GOTOLINE_KEY: |
| 2609 | case NANO_GOTOLINE_FKEY: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2610 | case 'G': /* Pico compatibility */ |
| 2611 | case 'g': |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2612 | curs_set(1); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 2613 | j = statusq(FALSE, gotodir_list, "", |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2614 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 2615 | NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2616 | #endif |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2617 | _("Go To Directory")); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2618 | bottombars(browser_list); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2619 | curs_set(0); |
| 2620 | |
| 2621 | if (j < 0) { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2622 | statusbar(_("Cancelled")); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2623 | break; |
| 2624 | } |
| 2625 | |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2626 | new_path = real_dir_from_tilde(answer); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2627 | |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2628 | if (new_path[0] != '/') { |
| 2629 | new_path = charealloc(new_path, strlen(path) + strlen(answer) + 2); |
| 2630 | sprintf(new_path, "%s/%s", path, answer); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2633 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2634 | if (check_operating_dir(new_path, FALSE) != 0) { |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2635 | statusbar(_("Can't go outside of %s in restricted mode"), operating_dir); |
| 2636 | free(new_path); |
| 2637 | break; |
| 2638 | } |
| 2639 | #endif |
| 2640 | |
| 2641 | if (!readable_dir(new_path)) { |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2642 | /* We can't open this dir for some reason. Complain */ |
| 2643 | statusbar(_("Can't open \"%s\": %s"), answer, strerror(errno)); |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2644 | free(new_path); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2645 | break; |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2646 | } |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* Start over again with the new path value */ |
Chris Allegretta | fdcb9e9 | 2003-02-12 02:52:04 +0000 | [diff] [blame] | 2649 | free_charptrarray(filelist, numents); |
| 2650 | free(foo); |
Chris Allegretta | f80a59c | 2003-01-30 00:57:33 +0000 | [diff] [blame] | 2651 | free(path); |
| 2652 | path = new_path; |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2653 | return do_browser(path); |
| 2654 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2655 | /* Stuff we want to abort the browser */ |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 2656 | case NANO_CANCEL_KEY: |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 2657 | case NANO_EXIT_KEY: |
Chris Allegretta | 6e82741 | 2001-01-04 04:41:08 +0000 | [diff] [blame] | 2658 | case NANO_EXIT_FKEY: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2659 | case 'E': /* Pico compatibility */ |
| 2660 | case 'e': |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2661 | abort = 1; |
| 2662 | break; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2663 | } |
| 2664 | if (abort) |
| 2665 | break; |
| 2666 | |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2667 | blank_edit(); |
| 2668 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2669 | if (width != 0) |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2670 | i = width * editwinrows * ((selected / width) / editwinrows); |
| 2671 | else |
| 2672 | i = 0; |
| 2673 | |
| 2674 | wmove(edit, 0, 0); |
| 2675 | for (j = i; j < numents && editline <= editwinrows - 1; j++) { |
| 2676 | filecols++; |
| 2677 | |
| 2678 | strncpy(foo, tail(filelist[j]), strlen(tail(filelist[j])) + 1); |
| 2679 | while (strlen(foo) < longest) |
| 2680 | strcat(foo, " "); |
| 2681 | col += strlen(foo); |
| 2682 | |
| 2683 | /* Put file info in the string also */ |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2684 | /* We use lstat here to detect links; then, if we find a |
| 2685 | symlink, we examine it via stat() to see if it is a |
Chris Allegretta | 0876dc9 | 2001-03-28 13:04:08 +0000 | [diff] [blame] | 2686 | directory or just a file symlink */ |
| 2687 | lstat(filelist[j], &st); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2688 | if (S_ISDIR(st.st_mode)) |
| 2689 | strcpy(foo + longest - 5, "(dir)"); |
| 2690 | else { |
Chris Allegretta | 0876dc9 | 2001-03-28 13:04:08 +0000 | [diff] [blame] | 2691 | if (S_ISLNK(st.st_mode)) { |
| 2692 | /* Aha! It's a symlink! Now, is it a dir? If so, |
| 2693 | mark it as such */ |
David Lawrence Ramsey | 3af54d3 | 2004-02-25 04:43:27 +0000 | [diff] [blame] | 2694 | stat(filelist[j], &st); |
Chris Allegretta | 0876dc9 | 2001-03-28 13:04:08 +0000 | [diff] [blame] | 2695 | if (S_ISDIR(st.st_mode)) |
| 2696 | strcpy(foo + longest - 5, "(dir)"); |
| 2697 | else |
| 2698 | strcpy(foo + longest - 2, "--"); |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2699 | } else if (st.st_size < (1 << 10)) /* less than 1 K */ |
| 2700 | sprintf(foo + longest - 7, "%4d B", |
Chris Allegretta | d461562 | 2001-05-23 21:54:47 +0000 | [diff] [blame] | 2701 | (int) st.st_size); |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2702 | else if (st.st_size >= (1 << 30)) /* at least 1 gig */ |
| 2703 | sprintf(foo + longest - 7, "%4d GB", |
| 2704 | (int) st.st_size >> 30); |
| 2705 | else if (st.st_size >= (1 << 20)) /* at least 1 meg */ |
| 2706 | sprintf(foo + longest - 7, "%4d MB", |
| 2707 | (int) st.st_size >> 20); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2708 | else /* It's more than 1 k and less than a meg */ |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2709 | sprintf(foo + longest - 7, "%4d KB", |
| 2710 | (int) st.st_size >> 10); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2713 | /* Highlight the currently selected file/dir */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2714 | if (j == selected) |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2715 | wattron(edit, A_REVERSE); |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2716 | waddstr(edit, foo); |
| 2717 | if (j == selected) |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2718 | wattroff(edit, A_REVERSE); |
| 2719 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2720 | /* And add some space between the cols */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2721 | waddstr(edit, " "); |
| 2722 | col += 2; |
| 2723 | |
| 2724 | /* And if the next entry isn't going to fit on the |
| 2725 | line, move to the next one */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 2726 | if (col > COLS - longest) { |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2727 | editline++; |
| 2728 | wmove(edit, editline, 0); |
| 2729 | col = 0; |
| 2730 | if (width == 0) |
| 2731 | width = filecols; |
| 2732 | } |
| 2733 | } |
David Lawrence Ramsey | a0b5ba2 | 2004-08-25 15:39:10 +0000 | [diff] [blame] | 2734 | wrefresh(edit); |
David Lawrence Ramsey | eb16f43 | 2004-09-27 01:04:50 +0000 | [diff] [blame] | 2735 | } while ((kbinput = get_kbinput(edit, &meta_key, &func_key)) != |
| 2736 | NANO_EXIT_KEY && kbinput != NANO_EXIT_FKEY); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2737 | curs_set(1); |
| 2738 | blank_edit(); |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 2739 | titlebar(NULL); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2740 | edit_refresh(); |
| 2741 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2742 | /* cleanup */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2743 | free_charptrarray(filelist, numents); |
| 2744 | free(foo); |
| 2745 | return retval; |
| 2746 | } |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2747 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2748 | /* Browser front end, checks to see if inpath has a dir in it and, if so, |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2749 | starts do_browser from there, else from the current dir */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2750 | char *do_browse_from(const char *inpath) |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2751 | { |
| 2752 | struct stat st; |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2753 | char *bob; |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2754 | /* The result of do_browser; the selected file name. */ |
| 2755 | char *path; |
| 2756 | /* inpath, tilde expanded. */ |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2757 | |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2758 | assert(inpath != NULL); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2759 | |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2760 | path = real_dir_from_tilde(inpath); |
| 2761 | |
| 2762 | /* |
| 2763 | * Perhaps path is a directory. If so, we will pass that to |
| 2764 | * do_browser. Otherwise, perhaps path is a directory / a file. So |
| 2765 | * we try stripping off the last path element. If it still isn't a |
| 2766 | * directory, just use the current directory. */ |
| 2767 | |
| 2768 | if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { |
| 2769 | striponedir(path); |
Chris Allegretta | e9b5c6f | 2003-02-12 23:49:56 +0000 | [diff] [blame] | 2770 | if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { |
| 2771 | free(path); |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2772 | path = getcwd(NULL, PATH_MAX + 1); |
Chris Allegretta | e9b5c6f | 2003-02-12 23:49:56 +0000 | [diff] [blame] | 2773 | } |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2774 | } |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2775 | |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2776 | #ifndef DISABLE_OPERATINGDIR |
| 2777 | /* If the resulting path isn't in the operating directory, use that. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2778 | if (check_operating_dir(path, FALSE) != 0) |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2779 | path = mallocstrcpy(path, operating_dir); |
| 2780 | #endif |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2781 | |
Chris Allegretta | 858d9d9 | 2003-01-30 00:53:32 +0000 | [diff] [blame] | 2782 | if (!readable_dir(path)) { |
| 2783 | beep(); |
| 2784 | bob = NULL; |
| 2785 | } else |
| 2786 | bob = do_browser(path); |
| 2787 | free(path); |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2788 | return bob; |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2789 | } |
Chris Allegretta | cf287c8 | 2002-07-20 13:57:41 +0000 | [diff] [blame] | 2790 | #endif /* !DISABLE_BROWSER */ |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2791 | |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2792 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2793 | /* Return $HOME/.nano_history, or NULL if we can't find the homedir. |
| 2794 | * The string is dynamically allocated, and should be freed. */ |
| 2795 | char *histfilename(void) |
| 2796 | { |
| 2797 | char *nanohist = NULL; |
| 2798 | |
| 2799 | if (homedir != NULL) { |
| 2800 | size_t homelen = strlen(homedir); |
| 2801 | |
| 2802 | nanohist = charalloc(homelen + 15); |
| 2803 | strcpy(nanohist, homedir); |
| 2804 | strcpy(nanohist + homelen, "/.nano_history"); |
| 2805 | } |
| 2806 | return nanohist; |
| 2807 | } |
| 2808 | |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2809 | void load_history(void) |
| 2810 | { |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2811 | char *nanohist = histfilename(); |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2812 | |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2813 | /* assume do_rcfile() has reported missing home dir */ |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2814 | if (nanohist != NULL) { |
| 2815 | FILE *hist = fopen(nanohist, "r"); |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2816 | |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 2817 | if (hist == NULL) { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2818 | if (errno != ENOENT) { |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2819 | /* Don't save history when we quit. */ |
| 2820 | UNSET(HISTORYLOG); |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2821 | rcfile_error(N_("Error reading %s: %s"), nanohist, strerror(errno)); |
David Lawrence Ramsey | 7dfec43 | 2004-08-12 15:20:14 +0000 | [diff] [blame] | 2822 | fprintf(stderr, _("\nPress Return to continue starting nano\n")); |
| 2823 | while (getchar() != '\n') |
| 2824 | ; |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2825 | } |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2826 | } else { |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2827 | historyheadtype *history = &search_history; |
| 2828 | char *line = NULL; |
| 2829 | size_t buflen = 0; |
| 2830 | ssize_t read; |
| 2831 | |
| 2832 | while ((read = getline(&line, &buflen, hist)) >= 0) { |
| 2833 | if (read > 0 && line[read - 1] == '\n') { |
| 2834 | read--; |
| 2835 | line[read] = '\0'; |
| 2836 | } |
| 2837 | if (read > 0) { |
| 2838 | unsunder(line, read); |
| 2839 | update_history(history, line); |
| 2840 | } else |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2841 | history = &replace_history; |
| 2842 | } |
| 2843 | fclose(hist); |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2844 | free(line); |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2845 | UNSET(HISTORY_CHANGED); |
| 2846 | } |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2847 | free(nanohist); |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2851 | bool writehist(FILE *hist, historyheadtype *histhead) |
| 2852 | { |
| 2853 | historytype *h; |
| 2854 | |
| 2855 | /* write oldest first */ |
| 2856 | for (h = histhead->tail; h->prev != NULL; h = h->prev) { |
| 2857 | size_t len = strlen(h->data); |
| 2858 | |
| 2859 | sunder(h->data); |
| 2860 | if (fwrite(h->data, sizeof(char), len, hist) < len || |
| 2861 | putc('\n', hist) == EOF) |
| 2862 | return FALSE; |
| 2863 | } |
| 2864 | return TRUE; |
| 2865 | } |
| 2866 | |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2867 | /* save histories to ~/.nano_history */ |
| 2868 | void save_history(void) |
| 2869 | { |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2870 | char *nanohist; |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2871 | |
| 2872 | /* don't save unchanged or empty histories */ |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 2873 | if ((search_history.count == 0 && replace_history.count == 0) || |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2874 | !ISSET(HISTORY_CHANGED) || ISSET(VIEW_MODE)) |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2875 | return; |
| 2876 | |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2877 | nanohist = histfilename(); |
Chris Allegretta | 1debce2 | 2003-02-13 22:00:19 +0000 | [diff] [blame] | 2878 | |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2879 | if (nanohist != NULL) { |
| 2880 | FILE *hist = fopen(nanohist, "wb"); |
| 2881 | |
David Lawrence Ramsey | 36dd87b | 2004-07-18 17:43:43 +0000 | [diff] [blame] | 2882 | if (hist == NULL) |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2883 | rcfile_error(N_("Error writing %s: %s"), nanohist, strerror(errno)); |
David Lawrence Ramsey | 36dd87b | 2004-07-18 17:43:43 +0000 | [diff] [blame] | 2884 | else { |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2885 | /* set rw only by owner for security ?? */ |
| 2886 | chmod(nanohist, S_IRUSR | S_IWUSR); |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 2887 | |
| 2888 | if (!writehist(hist, &search_history) || |
| 2889 | putc('\n', hist) == EOF || |
| 2890 | !writehist(hist, &replace_history)) |
David Lawrence Ramsey | 1e7f1ea | 2004-08-12 02:11:35 +0000 | [diff] [blame] | 2891 | rcfile_error(N_("Error writing %s: %s"), nanohist, strerror(errno)); |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 2892 | fclose(hist); |
| 2893 | } |
| 2894 | free(nanohist); |
| 2895 | } |
| 2896 | } |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2897 | #endif /* !NANO_SMALL && ENABLE_NANORC */ |