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