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