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 | * * |
| 5 | * Copyright (C) 1999 Chris Allegretta * |
| 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 * |
| 8 | * the Free Software Foundation; either version 1, or (at your option) * |
| 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 | |
Chris Allegretta | 6efda54 | 2001-04-28 18:03:52 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <errno.h> |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 32 | #include <ctype.h> |
| 33 | #include <dirent.h> |
Chris Allegretta | ee733e6 | 2001-01-22 22:17:08 +0000 | [diff] [blame] | 34 | #include <pwd.h> |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 35 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 36 | #include "proto.h" |
| 37 | #include "nano.h" |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 38 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 39 | #ifndef NANO_SMALL |
| 40 | #include <libintl.h> |
| 41 | #define _(string) gettext(string) |
| 42 | #else |
| 43 | #define _(string) (string) |
| 44 | #endif |
| 45 | |
| 46 | /* Load file into edit buffer - takes data from file struct */ |
| 47 | void load_file(void) |
| 48 | { |
| 49 | current = fileage; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 50 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 51 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 52 | /* add a new entry to the open_files structure, and check for |
| 53 | duplicate entries; if a duplicate entry was found, reload the |
| 54 | currently open file (it may have been changed during duplicate |
| 55 | handling) */ |
| 56 | if (add_open_file(0, 1) == 2) |
| 57 | load_open_file(); |
| 58 | #endif |
| 59 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 60 | wmove(edit, current_y, current_x); |
| 61 | } |
| 62 | |
| 63 | /* What happens when there is no file to open? aiee! */ |
| 64 | void new_file(void) |
| 65 | { |
| 66 | fileage = nmalloc(sizeof(filestruct)); |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 67 | fileage->data = charalloc(1); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 68 | strcpy(fileage->data, ""); |
| 69 | fileage->prev = NULL; |
| 70 | fileage->next = NULL; |
| 71 | fileage->lineno = 1; |
| 72 | filebot = fileage; |
| 73 | edittop = fileage; |
| 74 | editbot = fileage; |
| 75 | current = fileage; |
| 76 | totlines = 1; |
Chris Allegretta | e642197 | 2001-07-18 01:03:36 +0000 | [diff] [blame^] | 77 | |
| 78 | #ifdef ENABLE_MULTIBUFFER |
| 79 | /* if there aren't any entries in open_files, create the entry for |
| 80 | this new file, and, of course, don't bother checking for |
| 81 | duplicates; without this, if nano is started without a filename on |
| 82 | the command line, a new file with no name will be created, but it |
| 83 | will be given no open_files entry, leading to problems later on */ |
| 84 | if (!open_files) |
| 85 | add_open_file(0, 0); |
| 86 | #endif |
| 87 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 88 | UNSET(VIEW_MODE); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | int read_byte(int fd, char *filename, char *input) |
| 93 | { |
| 94 | static char buf[BUFSIZ]; |
| 95 | static int index = 0; |
| 96 | static int size = 0; |
| 97 | |
| 98 | if (index == size) { |
| 99 | index = 0; |
| 100 | size = read(fd, buf, BUFSIZ); |
| 101 | if (size == -1) { |
| 102 | clear(); |
| 103 | refresh(); |
| 104 | resetty(); |
| 105 | endwin(); |
| 106 | perror(filename); |
| 107 | } |
| 108 | if (!size) |
| 109 | return 0; |
| 110 | } |
| 111 | *input = buf[index++]; |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | filestruct *read_line(char *buf, filestruct * prev, int *line1ins) |
| 116 | { |
| 117 | filestruct *fileptr; |
| 118 | |
| 119 | fileptr = nmalloc(sizeof(filestruct)); |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 120 | fileptr->data = charalloc(strlen(buf) + 2); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 121 | strcpy(fileptr->data, buf); |
| 122 | |
| 123 | if (*line1ins) { |
| 124 | /* Special case, insert with cursor on 1st line. */ |
| 125 | fileptr->prev = NULL; |
| 126 | fileptr->next = fileage; |
| 127 | fileptr->lineno = 1; |
| 128 | *line1ins = 0; |
| 129 | /* If we're inserting into the first line of the file, then |
| 130 | we want to make sure that our edit buffer stays on the |
| 131 | first line (and that fileage stays up to date!) */ |
| 132 | fileage = fileptr; |
| 133 | edittop = fileptr; |
| 134 | } else if (fileage == NULL) { |
| 135 | fileage = fileptr; |
| 136 | fileage->lineno = 1; |
| 137 | fileage->next = fileage->prev = NULL; |
| 138 | fileptr = filebot = fileage; |
| 139 | } else if (prev) { |
| 140 | fileptr->prev = prev; |
| 141 | fileptr->next = NULL; |
| 142 | fileptr->lineno = prev->lineno + 1; |
| 143 | prev->next = fileptr; |
| 144 | } else { |
| 145 | die(_("read_line: not on first line and prev is NULL")); |
| 146 | } |
| 147 | |
| 148 | return fileptr; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | int read_file(int fd, char *filename) |
| 153 | { |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 154 | long size, num_lines = 0, linetemp = 0; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 155 | char input[2]; /* buffer */ |
| 156 | char *buf; |
| 157 | long i = 0, bufx = 128; |
| 158 | filestruct *fileptr = current, *tmp = NULL; |
| 159 | int line1ins = 0; |
| 160 | |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 161 | buf = charalloc(bufx); |
Chris Allegretta | 8f6c069 | 2000-07-19 01:16:18 +0000 | [diff] [blame] | 162 | buf[0] = '\0'; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 163 | |
| 164 | if (fileptr != NULL && fileptr->prev != NULL) { |
| 165 | fileptr = fileptr->prev; |
| 166 | tmp = fileptr; |
| 167 | } else if (fileptr != NULL && fileptr->prev == NULL) { |
| 168 | tmp = fileage; |
| 169 | current = fileage; |
| 170 | line1ins = 1; |
| 171 | } |
| 172 | input[1] = 0; |
| 173 | /* Read the entire file into file struct */ |
| 174 | while ((size = read_byte(fd, filename, input)) > 0) { |
| 175 | linetemp = 0; |
| 176 | if (input[0] == '\n') { |
| 177 | fileptr = read_line(buf, fileptr, &line1ins); |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 178 | num_lines++; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 179 | buf[0] = 0; |
| 180 | i = 0; |
| 181 | } else { |
| 182 | /* Now we allocate a bigger buffer 128 characters at a time. |
| 183 | If we allocate a lot of space for one line, we may indeed |
| 184 | have to use a buffer this big later on, so we don't |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 185 | decrease it at all. We do free it at the end, though. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 186 | |
| 187 | if (i >= bufx - 1) { |
| 188 | buf = nrealloc(buf, bufx + 128); |
| 189 | bufx += 128; |
| 190 | } |
| 191 | buf[i] = input[0]; |
| 192 | buf[i + 1] = 0; |
| 193 | i++; |
| 194 | } |
| 195 | totsize += size; |
| 196 | } |
| 197 | |
| 198 | /* Did we not get a newline but still have stuff to do? */ |
| 199 | if (buf[0]) { |
| 200 | fileptr = read_line(buf, fileptr, &line1ins); |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 201 | num_lines++; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 202 | buf[0] = 0; |
| 203 | } |
| 204 | /* Did we even GET a file? */ |
Chris Allegretta | 9956e53 | 2000-11-26 02:09:53 +0000 | [diff] [blame] | 205 | if (totsize == 0 || fileptr == NULL) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 206 | new_file(); |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 207 | statusbar(_("Read %d lines"), num_lines); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 208 | return 1; |
| 209 | } |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 210 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 211 | if (current != NULL) { |
| 212 | fileptr->next = current; |
| 213 | current->prev = fileptr; |
| 214 | renumber(current); |
| 215 | current_x = 0; |
| 216 | placewewant = 0; |
| 217 | } else if (fileptr->next == NULL) { |
| 218 | filebot = fileptr; |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 219 | new_magicline(); |
Chris Allegretta | 321590a | 2000-12-10 06:03:40 +0000 | [diff] [blame] | 220 | totsize--; |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 221 | |
| 222 | /* Update the edit buffer */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 223 | load_file(); |
| 224 | } |
Adam Rogoyski | 1e328fb | 2000-07-08 03:57:16 +0000 | [diff] [blame] | 225 | statusbar(_("Read %d lines"), num_lines); |
| 226 | totlines += num_lines; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 227 | |
| 228 | free(buf); |
| 229 | close(fd); |
| 230 | |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | /* Open the file (and decide if it exists) */ |
| 235 | int open_file(char *filename, int insert, int quiet) |
| 236 | { |
| 237 | int fd; |
| 238 | struct stat fileinfo; |
| 239 | |
| 240 | if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) { |
| 241 | if (insert) { |
| 242 | if (!quiet) |
| 243 | statusbar(_("\"%s\" not found"), filename); |
| 244 | return -1; |
| 245 | } else { |
| 246 | /* We have a new file */ |
| 247 | statusbar(_("New File")); |
| 248 | new_file(); |
| 249 | } |
| 250 | } else if ((fd = open(filename, O_RDONLY)) == -1) { |
| 251 | if (!quiet) |
| 252 | statusbar("%s: %s", strerror(errno), filename); |
Chris Allegretta | 3a7c0be | 2000-12-18 01:09:07 +0000 | [diff] [blame] | 253 | if (!insert) |
| 254 | new_file(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 255 | return -1; |
| 256 | } else { /* File is A-OK */ |
Chris Allegretta | 7960dcf | 2000-12-13 15:01:29 +0000 | [diff] [blame] | 257 | if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) || |
| 258 | S_ISBLK(fileinfo.st_mode)) { |
| 259 | if (S_ISDIR(fileinfo.st_mode)) |
| 260 | statusbar(_("File \"%s\" is a directory"), filename); |
| 261 | else |
| 262 | /* Don't open character or block files. Sorry, /dev/sndstat! */ |
| 263 | statusbar(_("File \"%s\" is a device file"), filename); |
| 264 | |
Chris Allegretta | f45c18d | 2000-09-16 05:25:06 +0000 | [diff] [blame] | 265 | if (!insert) |
| 266 | new_file(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 267 | return -1; |
| 268 | } |
| 269 | if (!quiet) |
| 270 | statusbar(_("Reading File")); |
| 271 | read_file(fd, filename); |
| 272 | } |
| 273 | |
| 274 | return 1; |
| 275 | } |
| 276 | |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 277 | int do_insertfile(int loading_file) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 278 | { |
| 279 | int i; |
Chris Allegretta | 25f4e58 | 2000-11-25 05:03:20 +0000 | [diff] [blame] | 280 | char *realname = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 281 | |
| 282 | wrap_reset(); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 283 | |
| 284 | #ifndef DISABLE_MOUSE |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 285 | currshortcut = insertfile_list; |
| 286 | currslen = INSERTFILE_LIST_LEN; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 289 | i = statusq(1, insertfile_list, INSERTFILE_LIST_LEN, "", |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 290 | _("File to insert [from ./] ")); |
| 291 | if (i != -1) { |
| 292 | |
| 293 | #ifdef DEBUG |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 294 | fprintf(stderr, _("filename is %s"), answer); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 295 | #endif |
| 296 | |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 297 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 298 | realname = real_dir_from_tilde(answer); |
Chris Allegretta | 09a8084 | 2000-11-30 02:31:13 +0000 | [diff] [blame] | 299 | #else |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 300 | realname = mallocstrcpy(realname, answer); |
Chris Allegretta | 09a8084 | 2000-11-30 02:31:13 +0000 | [diff] [blame] | 301 | #endif |
Chris Allegretta | 25f4e58 | 2000-11-25 05:03:20 +0000 | [diff] [blame] | 302 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 303 | #ifndef DISABLE_BROWSER |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 304 | if (i == NANO_TOFILES_KEY) { |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 305 | |
| 306 | char *tmp = do_browse_from(realname); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 307 | #ifndef DISABLE_MOUSE |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 308 | currshortcut = insertfile_list; |
| 309 | currslen = INSERTFILE_LIST_LEN; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 310 | #endif |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 311 | |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 312 | #ifdef DISABLE_TABCOMP |
Chris Allegretta | 544347c | 2001-01-05 14:31:52 +0000 | [diff] [blame] | 313 | realname = NULL; |
Chris Allegretta | 123e5e6 | 2001-01-04 22:05:47 +0000 | [diff] [blame] | 314 | #endif |
| 315 | if (tmp != NULL) |
Chris Allegretta | 544347c | 2001-01-05 14:31:52 +0000 | [diff] [blame] | 316 | realname = mallocstrcpy(realname, tmp); |
Chris Allegretta | 123e5e6 | 2001-01-04 22:05:47 +0000 | [diff] [blame] | 317 | else |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 318 | return do_insertfile(loading_file); |
| 319 | } |
| 320 | #endif |
| 321 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 322 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 323 | if (loading_file) { |
| 324 | |
| 325 | /* update the current entry in the open_files structure; we |
| 326 | don't need to check for duplicate entries (the conditions |
| 327 | that could create them are taken care of elsewhere) */ |
| 328 | add_open_file(1, 0); |
| 329 | |
| 330 | free_filestruct(current); |
| 331 | new_file(); |
| 332 | UNSET(MODIFIED); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 333 | } |
| 334 | #endif |
| 335 | |
Chris Allegretta | 25f4e58 | 2000-11-25 05:03:20 +0000 | [diff] [blame] | 336 | i = open_file(realname, 1, 0); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 337 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 338 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 339 | if (loading_file) |
| 340 | filename = mallocstrcpy(filename, realname); |
| 341 | #endif |
| 342 | |
Chris Allegretta | 25f4e58 | 2000-11-25 05:03:20 +0000 | [diff] [blame] | 343 | free(realname); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 344 | |
| 345 | dump_buffer(fileage); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 346 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 347 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 348 | if (loading_file) |
| 349 | load_file(); |
| 350 | else |
| 351 | #endif |
| 352 | |
| 353 | set_modified(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 354 | |
| 355 | /* Here we want to rebuild the edit window */ |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 356 | fix_editbot(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 357 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 358 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 359 | /* If we've loaded another file, update the titlebar's contents */ |
| 360 | if (loading_file) { |
| 361 | clearok(topwin, FALSE); |
| 362 | titlebar(NULL); |
| 363 | |
| 364 | /* And re-init the shortcut list */ |
| 365 | shortcut_init(0); |
| 366 | } |
| 367 | #endif |
| 368 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 369 | /* If we've gone off the bottom, recenter; otherwise, just redraw */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 370 | if (current->lineno > editbot->lineno) |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 371 | edit_update(current, CENTER); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 372 | else |
| 373 | edit_refresh(); |
| 374 | |
| 375 | UNSET(KEEP_CUTBUFFER); |
| 376 | display_main_list(); |
| 377 | return i; |
| 378 | } else { |
| 379 | statusbar(_("Cancelled")); |
| 380 | UNSET(KEEP_CUTBUFFER); |
| 381 | display_main_list(); |
| 382 | return 0; |
| 383 | } |
| 384 | } |
| 385 | |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 386 | int do_insertfile_void(void) |
| 387 | { |
| 388 | int result = 0; |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 389 | #ifdef ENABLE_MULTIBUFFER |
| 390 | result = do_insertfile(ISSET(MULTIBUFFER)); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 391 | #else |
| 392 | result = do_insertfile(0); |
| 393 | #endif |
| 394 | |
| 395 | display_main_list(); |
| 396 | return result; |
| 397 | } |
| 398 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 399 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 400 | /* |
| 401 | * Add/update an entry to the open_files filestruct. If update is |
| 402 | * zero, a new entry is created; otherwise, the current entry is updated. |
| 403 | * If dup_fix is zero, checking for and handling duplicate entries is not |
| 404 | * done; otherwise, it is. Return 0 on success, 1 on error, or 2 on |
| 405 | * finding a duplicate entry. |
| 406 | */ |
| 407 | int add_open_file(int update, int dup_fix) |
| 408 | { |
| 409 | filestruct *tmp; |
| 410 | |
| 411 | if (!current || !filename) |
| 412 | return 1; |
| 413 | |
| 414 | /* first, if duplicate checking is allowed, do it */ |
| 415 | if (dup_fix) { |
| 416 | |
| 417 | /* if duplicates were found and handled, we're done */ |
| 418 | if (open_file_dup_fix(update)) |
| 419 | return 2; |
| 420 | } |
| 421 | |
| 422 | /* if no entries, make the first one */ |
| 423 | if (!open_files) { |
| 424 | open_files = make_new_node(NULL); |
| 425 | |
| 426 | /* if open_files->file is NULL at the nrealloc() below, we get a |
| 427 | segfault */ |
| 428 | open_files->file = open_files; |
| 429 | } |
| 430 | |
| 431 | else if (!update) { |
| 432 | |
| 433 | /* otherwise, if we're not updating, make a new entry for |
| 434 | open_files and splice it in after the current one */ |
| 435 | |
| 436 | #ifdef DEBUG |
| 437 | fprintf(stderr, _("filename is %s"), open_files->data); |
| 438 | #endif |
| 439 | |
| 440 | tmp = make_new_node(NULL); |
| 441 | splice_node(open_files, tmp, open_files->next); |
| 442 | open_files = open_files->next; |
| 443 | |
| 444 | /* if open_files->file is NULL at the nrealloc() below, we get a |
| 445 | segfault */ |
| 446 | open_files->file = open_files; |
| 447 | } |
| 448 | |
| 449 | /* save current filename */ |
| 450 | open_files->data = mallocstrcpy(open_files->data, filename); |
| 451 | |
| 452 | /* save the full path location */ |
| 453 | open_files->file_path = get_full_path(open_files->data); |
| 454 | |
| 455 | /* save current total number of lines */ |
| 456 | open_files->file_totlines = totlines; |
| 457 | |
| 458 | /* save current total size */ |
| 459 | open_files->file_totsize = totsize; |
| 460 | |
| 461 | /* save current x-coordinate position */ |
| 462 | open_files->file_current_x = current_x; |
| 463 | |
| 464 | /* save current y-coordinate position */ |
| 465 | open_files->file_current_y = current_y; |
| 466 | |
| 467 | /* save current place we want */ |
| 468 | open_files->file_placewewant = placewewant; |
| 469 | |
| 470 | /* save current line number */ |
| 471 | open_files->lineno = current->lineno; |
| 472 | |
| 473 | /* save current filestruct */ |
| 474 | open_files->file = nrealloc(open_files->file, sizeof(filestruct)); |
| 475 | while (current->prev) |
| 476 | current = current->prev; |
| 477 | open_files->file = copy_filestruct(current); |
| 478 | do_gotoline(open_files->lineno, 1); |
| 479 | placewewant = open_files->file_placewewant; |
| 480 | update_line(current, current_x); |
| 481 | |
| 482 | /* save current modification status */ |
| 483 | open_files->file_modified = ISSET(MODIFIED); |
| 484 | |
| 485 | #ifdef DEBUG |
| 486 | fprintf(stderr, _("filename is %s"), open_files->data); |
| 487 | #endif |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Update only the filename and full path stored in the current entry. |
| 494 | * Return 0 on success or 1 on error. |
| 495 | */ |
| 496 | int open_file_change_name(void) |
| 497 | { |
| 498 | if (!open_files || !filename) |
| 499 | return 1; |
| 500 | |
| 501 | /* save current filename */ |
| 502 | open_files->data = mallocstrcpy(open_files->data, filename); |
| 503 | |
| 504 | /* save the full path location */ |
| 505 | open_files->file_path = get_full_path(open_files->data); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Read the current entry in the open_files structure and set up the |
| 512 | * currently open file using that entry's information. Return 0 on |
| 513 | * success or 1 on error. |
| 514 | */ |
| 515 | int load_open_file(void) |
| 516 | { |
| 517 | if (!open_files) |
| 518 | return 1; |
| 519 | |
| 520 | /* set up the filename, the file buffer, the total number of lines in |
| 521 | the file, and the total file size */ |
| 522 | filename = mallocstrcpy(filename, open_files->data); |
| 523 | fileage = copy_filestruct(open_files->file); |
| 524 | current = fileage; |
| 525 | totlines = open_files->file_totlines; |
| 526 | totsize = open_files->file_totsize; |
| 527 | |
| 528 | /* since do_gotoline() resets the x-coordinate but not the |
| 529 | y-coordinate, set all coordinates up this way */ |
| 530 | current_y = open_files->file_current_y; |
| 531 | do_gotoline(open_files->lineno, 1); |
| 532 | current_x = open_files->file_current_x; |
| 533 | placewewant = open_files->file_placewewant; |
| 534 | update_line(current, current_x); |
| 535 | |
| 536 | /* set up modification status and update the titlebar */ |
| 537 | if (open_files->file_modified) |
| 538 | SET(MODIFIED); |
| 539 | else |
| 540 | UNSET(MODIFIED); |
| 541 | clearok(topwin, FALSE); |
| 542 | titlebar(NULL); |
| 543 | |
| 544 | /* if we're constantly displaying the cursor position, update it */ |
| 545 | if (ISSET(CONSTUPDATE)) |
| 546 | do_cursorpos(); |
| 547 | |
| 548 | /* now we're done */ |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Search the open_files structure for an entry with the same value for |
| 554 | * the file_path member as the current entry (i. e. a duplicate entry). |
| 555 | * If one is found, return a pointer to it; otherwise, return NULL. |
| 556 | * |
| 557 | * Note: This should only be called inside open_file_dup_fix(). |
| 558 | */ |
| 559 | filestruct *open_file_dup_search(void) |
| 560 | { |
| 561 | filestruct *tmp; |
| 562 | char *path; |
| 563 | |
| 564 | if (!open_files || !filename) |
| 565 | return NULL; |
| 566 | |
| 567 | tmp = open_files; |
| 568 | path = get_full_path(filename); |
| 569 | |
| 570 | /* if there's only one entry, handle it */ |
| 571 | if (!tmp->prev && !tmp->next) { |
| 572 | if (!strcmp(tmp->file_path, path)) |
| 573 | return tmp; |
| 574 | } |
| 575 | |
| 576 | /* otherwise, go to the beginning */ |
| 577 | while (tmp->prev) |
| 578 | tmp = tmp->prev; |
| 579 | |
| 580 | /* and search the entries one by one */ |
| 581 | while (tmp) { |
| 582 | |
| 583 | if (!strcmp(tmp->file_path, path)) { |
| 584 | |
| 585 | /* if it's not the current entry, we've found a duplicate */ |
| 586 | if (tmp != open_files) |
| 587 | return tmp; |
| 588 | } |
| 589 | |
| 590 | /* go to the next entry */ |
| 591 | tmp = tmp->next; |
| 592 | |
| 593 | } |
| 594 | |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Search for duplicate entries in the open_files structure using |
| 600 | * open_file_dup_search(), and, if one is found, handle it properly. |
| 601 | * Return 0 if no duplicates were found, and 1 otherwise. |
| 602 | */ |
| 603 | int open_file_dup_fix(int update) |
| 604 | { |
| 605 | filestruct *tmp = open_file_dup_search(); |
| 606 | |
| 607 | if (!tmp) |
| 608 | return 0; |
| 609 | |
| 610 | /* if there's only one entry, handle it */ |
| 611 | if (!tmp->prev && !tmp->next) |
| 612 | return 1; |
| 613 | |
| 614 | /* otherwise, if we're not updating, the user's trying to load a |
| 615 | duplicate; switch to the original instead */ |
| 616 | if (!update) { |
| 617 | open_files = tmp; |
| 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | /* if we are updating, the filename's been changed via a save; it's |
| 622 | thus more recent than the original, so remove the original */ |
| 623 | else { |
| 624 | unlink_node(tmp); |
| 625 | free_filestruct(tmp->file); |
| 626 | free(tmp->file_path); |
| 627 | delete_node(tmp); |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Open the previous entry in the open_files structure. If closing_file |
| 634 | * is zero, update the current entry before switching from it. |
| 635 | * Otherwise, we are about to close that entry, so don't bother doing so. |
| 636 | * Return 0 on success and 1 on error. |
| 637 | */ |
| 638 | int open_prevfile(int closing_file) |
| 639 | { |
| 640 | if (!open_files) |
| 641 | return 1; |
| 642 | |
| 643 | /* if we're not about to close the current entry, update it before |
| 644 | doing anything; since we're only switching, we don't need to check |
| 645 | for duplicate entries */ |
| 646 | if (!closing_file) |
| 647 | add_open_file(1, 0); |
| 648 | |
| 649 | if (!open_files->prev && !open_files->next) { |
| 650 | |
| 651 | /* only one file open */ |
| 652 | if (!closing_file) |
| 653 | statusbar(_("No more open files")); |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | if (open_files->prev) { |
| 658 | open_files = open_files->prev; |
| 659 | |
| 660 | #ifdef DEBUG |
| 661 | fprintf(stderr, _("filename is %s"), open_files->data); |
| 662 | #endif |
| 663 | |
| 664 | } |
| 665 | |
| 666 | else if (open_files->next) { |
| 667 | |
| 668 | /* if we're at the beginning, wrap around to the end */ |
| 669 | while (open_files->next) |
| 670 | open_files = open_files->next; |
| 671 | |
| 672 | #ifdef DEBUG |
| 673 | fprintf(stderr, _("filename is %s"), open_files->data); |
| 674 | #endif |
| 675 | |
| 676 | } |
| 677 | |
| 678 | load_open_file(); |
| 679 | |
| 680 | #ifdef DEBUG |
| 681 | dump_buffer(current); |
| 682 | #endif |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * Open the next entry in the open_files structure. If closing_file is |
| 689 | * zero, update the current entry before switching from it. Otherwise, we |
| 690 | * are about to close that entry, so don't bother doing so. Return 0 on |
| 691 | * success and 1 on error. |
| 692 | */ |
| 693 | int open_nextfile(int closing_file) |
| 694 | { |
| 695 | if (!open_files) |
| 696 | return 1; |
| 697 | |
| 698 | /* if we're not about to close the current entry, update it before |
| 699 | doing anything; since we're only switching, we don't need to check |
| 700 | for duplicate entries */ |
| 701 | if (!closing_file) |
| 702 | add_open_file(1, 0); |
| 703 | |
| 704 | if (!open_files->prev && !open_files->next) { |
| 705 | |
| 706 | /* only one file open */ |
| 707 | if (!closing_file) |
| 708 | statusbar(_("No more open files")); |
| 709 | return 1; |
| 710 | } |
| 711 | |
| 712 | if (open_files->next) { |
| 713 | open_files = open_files->next; |
| 714 | |
| 715 | #ifdef DEBUG |
| 716 | fprintf(stderr, _("filename is %s"), open_files->data); |
| 717 | #endif |
| 718 | |
| 719 | } |
| 720 | else if (open_files->prev) { |
| 721 | |
| 722 | /* if we're at the end, wrap around to the beginning */ |
| 723 | while (open_files->prev) { |
| 724 | open_files = open_files->prev; |
| 725 | |
| 726 | #ifdef DEBUG |
| 727 | fprintf(stderr, _("filename is %s"), open_files->data); |
| 728 | #endif |
| 729 | |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | load_open_file(); |
| 734 | |
| 735 | #ifdef DEBUG |
| 736 | dump_buffer(current); |
| 737 | #endif |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Delete an entry from the open_files filestruct. After deletion of an |
Chris Allegretta | e642197 | 2001-07-18 01:03:36 +0000 | [diff] [blame^] | 744 | * entry, the next or previous entry is opened, whichever is found first. |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 745 | * Return 0 on success or 1 on error. |
| 746 | */ |
| 747 | int close_open_file(void) |
| 748 | { |
| 749 | filestruct *tmp; |
| 750 | |
| 751 | if (!open_files) |
| 752 | return 1; |
| 753 | |
| 754 | tmp = open_files; |
Chris Allegretta | e642197 | 2001-07-18 01:03:36 +0000 | [diff] [blame^] | 755 | if (open_nextfile(1)) { |
| 756 | if (open_prevfile(1)) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 757 | return 1; |
| 758 | } |
| 759 | |
| 760 | unlink_node(tmp); |
| 761 | free_filestruct(tmp->file); |
| 762 | free(tmp->file_path); |
| 763 | delete_node(tmp); |
| 764 | |
| 765 | shortcut_init(0); |
| 766 | display_main_list(); |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * When passed "[relative path][filename]" in origpath, return "[full |
| 772 | * path][filename]" on success, or NULL on error. |
| 773 | */ |
| 774 | char *get_full_path(const char *origpath) |
| 775 | { |
| 776 | char *newpath = NULL, *last_slash, *d_here, *d_there, *d_there_file; |
| 777 | int last_slash_index; |
| 778 | |
| 779 | /* first, get the current directory */ |
| 780 | |
| 781 | #ifdef PATH_MAX |
| 782 | d_here = getcwd(NULL, PATH_MAX + 1); |
| 783 | #else |
| 784 | d_here = getcwd(NULL, 0); |
| 785 | #endif |
| 786 | |
| 787 | if (d_here) { |
| 788 | |
| 789 | align(&d_here); |
| 790 | |
| 791 | /* get the filename (with path included) and save it in both |
| 792 | d_there and d_there_file */ |
| 793 | d_there = charalloc(strlen(origpath) + 1); |
| 794 | d_there_file = charalloc(strlen(origpath) + 1); |
| 795 | strcpy(d_there, origpath); |
| 796 | strcpy(d_there_file, origpath); |
| 797 | |
| 798 | /* search for the last slash in d_there */ |
| 799 | last_slash = strrchr(d_there, '/'); |
| 800 | |
| 801 | /* if we didn't find one, copy d_here into d_there; all data is |
| 802 | then set up */ |
| 803 | if (!last_slash) { |
| 804 | d_there = nrealloc(d_there, strlen(d_here) + 1); |
| 805 | strcpy(d_there, d_here); |
| 806 | } |
| 807 | |
| 808 | else { |
| 809 | |
| 810 | /* otherwise, remove all non-path elements from d_there */ |
| 811 | last_slash_index = strlen(d_there) - strlen(last_slash); |
| 812 | null_at(d_there, last_slash_index); |
| 813 | |
| 814 | /* and remove all non-file elements from d_there_file */ |
| 815 | last_slash++; |
| 816 | d_there_file = nrealloc(d_there_file, strlen(last_slash) + 1); |
| 817 | strcpy(d_there_file, last_slash); |
| 818 | |
| 819 | /* now go to the path specified in d_there */ |
| 820 | if (chdir(d_there) != -1) { |
| 821 | |
| 822 | /* get the full pathname, and save it back in d_there */ |
| 823 | |
| 824 | free(d_there); |
| 825 | |
| 826 | #ifdef PATH_MAX |
| 827 | d_there = getcwd(NULL, PATH_MAX + 1); |
| 828 | #else |
| 829 | d_there = getcwd(NULL, 0); |
| 830 | #endif |
| 831 | |
| 832 | align(&d_there); |
| 833 | } |
| 834 | |
| 835 | /* finally, go back to where we were before, d_here (no error |
| 836 | checking is done on this chdir(), because we can do |
| 837 | nothing if it fails) */ |
| 838 | chdir(d_here); |
| 839 | } |
| 840 | |
| 841 | /* all data is set up; fill in newpath */ |
| 842 | |
| 843 | /* newpath = d_there + "/" + d_there_file */ |
| 844 | newpath = charalloc(strlen(d_there) + strlen(d_there_file) + 2); |
| 845 | strcpy(newpath, d_there); |
| 846 | strcat(newpath, "/"); |
| 847 | strcat(newpath, d_there_file); |
| 848 | |
| 849 | /* finally, clean up */ |
| 850 | free(d_there_file); |
| 851 | free(d_there); |
| 852 | free(d_here); |
| 853 | } |
| 854 | |
| 855 | return newpath; |
| 856 | } |
| 857 | #endif |
| 858 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 859 | /* |
| 860 | * Write a file out. If tmp is nonzero, we set the umask to 0600, |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 861 | * we don't set the global variable filename to its name, and don't |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 862 | * print out how many lines we wrote on the statusbar. |
| 863 | * |
Chris Allegretta | 4980517 | 2001-03-28 02:14:28 +0000 | [diff] [blame] | 864 | * tmp means we are writing a tmp file in a secure fashion. We use |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 865 | * it when spell checking or dumping the file on an error. |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 866 | * |
| 867 | * append means, not surprisingly, whether we are appending instead |
| 868 | * of overwriting. |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 869 | * |
| 870 | * nonamechange means don't change the current filename, it is ignored |
| 871 | * if tmp == 1. |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 872 | */ |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 873 | int write_file(char *name, int tmp, int append, int nonamechange) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 874 | { |
| 875 | long size, lineswritten = 0; |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 876 | static char *buf = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 877 | filestruct *fileptr; |
Chris Allegretta | 07f9ee0 | 2000-12-04 05:15:39 +0000 | [diff] [blame] | 878 | int fd, mask = 0, realexists, anyexists; |
Chris Allegretta | 7960dcf | 2000-12-13 15:01:29 +0000 | [diff] [blame] | 879 | struct stat st, lst; |
Chris Allegretta | 0f5dfef | 2000-11-24 14:02:57 +0000 | [diff] [blame] | 880 | static char *realname = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 881 | |
| 882 | if (!strcmp(name, "")) { |
| 883 | statusbar(_("Cancelled")); |
| 884 | return -1; |
| 885 | } |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 886 | titlebar(NULL); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 887 | fileptr = fileage; |
Chris Allegretta | 0f5dfef | 2000-11-24 14:02:57 +0000 | [diff] [blame] | 888 | |
| 889 | if (realname != NULL) |
| 890 | free(realname); |
| 891 | |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 892 | if (buf != NULL) |
| 893 | free(buf); |
| 894 | |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 895 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 896 | realname = real_dir_from_tilde(name); |
| 897 | #else |
| 898 | realname = mallocstrcpy(realname, name); |
| 899 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 900 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 901 | /* Save the state of file at the end of the symlink (if there is one) */ |
Chris Allegretta | f7ee9e6 | 2000-12-02 21:13:50 +0000 | [diff] [blame] | 902 | realexists = stat(realname, &st); |
| 903 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 904 | /* Stat the link itself for the check... */ |
| 905 | anyexists = lstat(realname, &lst); |
Chris Allegretta | acb6234 | 2000-07-21 22:42:46 +0000 | [diff] [blame] | 906 | |
Chris Allegretta | 07f9ee0 | 2000-12-04 05:15:39 +0000 | [diff] [blame] | 907 | /* New case: if the file exists, just give up */ |
| 908 | if (tmp && anyexists != -1) |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 909 | return -1; |
Chris Allegretta | b5bb24c | 2000-12-06 00:57:54 +0000 | [diff] [blame] | 910 | /* NOTE: If you change this statement, you MUST CHANGE the if |
Chris Allegretta | 544d9b0 | 2000-12-11 02:47:13 +0000 | [diff] [blame] | 911 | statement below (that says: |
| 912 | if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) && |
| 913 | S_ISLNK(lst.st_mode))) { |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 914 | to reflect whether or not to link/unlink/rename the file */ |
| 915 | else if (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) || tmp) { |
Chris Allegretta | 3308439 | 2000-12-09 22:50:38 +0000 | [diff] [blame] | 916 | /* Use O_EXCL if tmp == 1. This is now copied from joe, because |
Chris Allegretta | f597777 | 2000-12-09 23:58:41 +0000 | [diff] [blame] | 917 | wiggy says so *shrug*. */ |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 918 | if (append) |
| 919 | fd = open(realname, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR)); |
| 920 | else if (tmp) |
Chris Allegretta | f597777 | 2000-12-09 23:58:41 +0000 | [diff] [blame] | 921 | fd = open(realname, O_WRONLY | O_CREAT | O_EXCL, (S_IRUSR|S_IWUSR)); |
| 922 | else |
| 923 | fd = open(realname, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR)); |
Chris Allegretta | 07f9ee0 | 2000-12-04 05:15:39 +0000 | [diff] [blame] | 924 | |
| 925 | /* First, just give up if we couldn't even open the file */ |
Chris Allegretta | 5982849 | 2000-12-04 03:31:39 +0000 | [diff] [blame] | 926 | if (fd == -1) { |
Chris Allegretta | 20c131c | 2000-12-04 04:20:09 +0000 | [diff] [blame] | 927 | if (!tmp && ISSET(TEMP_OPT)) { |
Chris Allegretta | a299b03 | 2000-07-14 02:44:02 +0000 | [diff] [blame] | 928 | UNSET(TEMP_OPT); |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 929 | return do_writeout(filename, 1, 0); |
Chris Allegretta | a299b03 | 2000-07-14 02:44:02 +0000 | [diff] [blame] | 930 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 931 | statusbar(_("Could not open file for writing: %s"), |
| 932 | strerror(errno)); |
| 933 | return -1; |
| 934 | } |
Chris Allegretta | 07f9ee0 | 2000-12-04 05:15:39 +0000 | [diff] [blame] | 935 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 936 | } |
| 937 | /* Don't follow symlink. Create new file. */ |
| 938 | else { |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 939 | buf = charalloc(strlen(realname) + 8); |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 940 | strncpy(buf, realname, strlen(realname)+1); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 941 | strcat(buf, ".XXXXXX"); |
| 942 | if ((fd = mkstemp(buf)) == -1) { |
Chris Allegretta | a299b03 | 2000-07-14 02:44:02 +0000 | [diff] [blame] | 943 | if (ISSET(TEMP_OPT)) { |
| 944 | UNSET(TEMP_OPT); |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 945 | return do_writeout(filename, 1, 0); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 946 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 947 | statusbar(_("Could not open file for writing: %s"), |
| 948 | strerror(errno)); |
| 949 | return -1; |
| 950 | } |
| 951 | } |
| 952 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 953 | dump_buffer(fileage); |
| 954 | while (fileptr != NULL && fileptr->next != NULL) { |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 955 | /* Next line is so we discount the "magic line" */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 956 | if (filebot == fileptr && fileptr->data[0] == '\0') |
| 957 | break; |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 958 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 959 | size = write(fd, fileptr->data, strlen(fileptr->data)); |
| 960 | if (size == -1) { |
| 961 | statusbar(_("Could not open file for writing: %s"), |
| 962 | strerror(errno)); |
| 963 | return -1; |
| 964 | } else { |
| 965 | #ifdef DEBUG |
| 966 | fprintf(stderr, _("Wrote >%s\n"), fileptr->data); |
| 967 | #endif |
| 968 | } |
| 969 | write(fd, "\n", 1); |
| 970 | |
| 971 | fileptr = fileptr->next; |
| 972 | lineswritten++; |
| 973 | } |
| 974 | |
| 975 | if (fileptr != NULL) { |
| 976 | size = write(fd, fileptr->data, strlen(fileptr->data)); |
| 977 | if (size == -1) { |
| 978 | statusbar(_("Could not open file for writing: %s"), |
| 979 | strerror(errno)); |
| 980 | return -1; |
| 981 | } else if (size > 0) { |
| 982 | size = write(fd, "\n", 1); |
Chris Allegretta | 1a8b296 | 2001-06-30 18:41:08 +0000 | [diff] [blame] | 983 | lineswritten++; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 984 | if (size == -1) { |
| 985 | statusbar(_("Could not open file for writing: %s"), |
| 986 | strerror(errno)); |
| 987 | return -1; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | |
| 993 | if (close(fd) == -1) { |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 994 | statusbar(_("Could not close %s: %s"), realname, strerror(errno)); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 995 | unlink(buf); |
| 996 | return -1; |
| 997 | } |
| 998 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 999 | if (realexists == -1 || tmp || |
| 1000 | (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1001 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1002 | /* Use default umask as file permissions if file is a new file. */ |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1003 | mask = umask(0); |
| 1004 | umask(mask); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1005 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1006 | if (tmp) /* We don't want anyone reading our temporary file! */ |
| 1007 | mask = 0600; |
| 1008 | else |
| 1009 | mask = 0666 & ~mask; |
| 1010 | } else |
Chris Allegretta | da13f0b | 2000-12-06 05:59:31 +0000 | [diff] [blame] | 1011 | /* Use permissions from file we are overwriting. */ |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1012 | mask = st.st_mode; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1013 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1014 | if (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1015 | if (unlink(realname) == -1) { |
| 1016 | if (errno != ENOENT) { |
Chris Allegretta | f7ee9e6 | 2000-12-02 21:13:50 +0000 | [diff] [blame] | 1017 | statusbar(_("Could not open %s for writing: %s"), |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1018 | realname, strerror(errno)); |
Chris Allegretta | f7ee9e6 | 2000-12-02 21:13:50 +0000 | [diff] [blame] | 1019 | unlink(buf); |
| 1020 | return -1; |
| 1021 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1022 | } |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1023 | if (link(buf, realname) != -1) |
| 1024 | unlink(buf); |
| 1025 | else if (errno != EPERM) { |
| 1026 | statusbar(_("Could not open %s for writing: %s"), |
| 1027 | name, strerror(errno)); |
| 1028 | unlink(buf); |
| 1029 | return -1; |
| 1030 | } else if (rename(buf, realname) == -1) { /* Try a rename?? */ |
| 1031 | statusbar(_("Could not open %s for writing: %s"), |
| 1032 | realname, strerror(errno)); |
| 1033 | unlink(buf); |
| 1034 | return -1; |
| 1035 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1036 | } |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1037 | if (chmod(realname, mask) == -1) |
| 1038 | statusbar(_("Could not set permissions %o on %s: %s"), |
| 1039 | mask, realname, strerror(errno)); |
| 1040 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1041 | if (!tmp) { |
Chris Allegretta | 0241d19 | 2001-06-05 23:45:54 +0000 | [diff] [blame] | 1042 | if (!nonamechange) |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1043 | filename = mallocstrcpy(filename, realname); |
| 1044 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1045 | statusbar(_("Wrote %d lines"), lineswritten); |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1046 | UNSET(MODIFIED); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1047 | titlebar(NULL); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1048 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1049 | return 1; |
| 1050 | } |
| 1051 | |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1052 | int do_writeout(char *path, int exiting, int append) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1053 | { |
| 1054 | int i = 0; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1055 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1056 | #ifdef NANO_EXTRA |
| 1057 | static int did_cred = 0; |
| 1058 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1059 | |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1060 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1061 | currshortcut = writefile_list; |
| 1062 | currslen = WRITEFILE_LIST_LEN; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1063 | #endif |
| 1064 | |
Chris Allegretta | e1ebaf3 | 2001-01-07 05:50:36 +0000 | [diff] [blame] | 1065 | answer = mallocstrcpy(answer, path); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1066 | |
Chris Allegretta | 962c3c9 | 2000-07-24 21:52:17 +0000 | [diff] [blame] | 1067 | if ((exiting) && (ISSET(TEMP_OPT))) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1068 | if (filename[0]) { |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1069 | i = write_file(answer, 0, 0, 0); |
Chris Allegretta | 962c3c9 | 2000-07-24 21:52:17 +0000 | [diff] [blame] | 1070 | display_main_list(); |
| 1071 | return i; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1072 | } else { |
Chris Allegretta | 461b2a9 | 2000-07-24 22:05:18 +0000 | [diff] [blame] | 1073 | UNSET(TEMP_OPT); |
| 1074 | do_exit(); |
| 1075 | |
| 1076 | /* They cancelled, abort quit */ |
| 1077 | return -1; |
Chris Allegretta | 962c3c9 | 2000-07-24 21:52:17 +0000 | [diff] [blame] | 1078 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | while (1) { |
Chris Allegretta | 500b5e3 | 2001-06-21 23:58:47 +0000 | [diff] [blame] | 1082 | #ifndef NANO_SMALL |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1083 | if (ISSET(MARK_ISSET) && !exiting) |
Chris Allegretta | d21195f | 2001-06-05 22:57:43 +0000 | [diff] [blame] | 1084 | i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, "", |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1085 | _("%s Selection to File"), append ? _("Append") : _("Write")); |
| 1086 | else |
Chris Allegretta | 500b5e3 | 2001-06-21 23:58:47 +0000 | [diff] [blame] | 1087 | #endif |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1088 | i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, answer, |
| 1089 | _("File Name to %s"), append ? _("Append") : _("Write")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1090 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1091 | if (i != -1) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1092 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1093 | #ifndef DISABLE_BROWSER |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1094 | if (i == NANO_TOFILES_KEY) { |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 1095 | |
| 1096 | char *tmp = do_browse_from(answer); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1097 | |
| 1098 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1099 | currshortcut = writefile_list; |
| 1100 | currslen = WRITEFILE_LIST_LEN; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1101 | #endif |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1102 | |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1103 | if (tmp != NULL) { |
Chris Allegretta | 544347c | 2001-01-05 14:31:52 +0000 | [diff] [blame] | 1104 | answer = mallocstrcpy(answer, tmp); |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1105 | } else |
| 1106 | return do_writeout(answer, exiting, append); |
| 1107 | } else |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1108 | #endif |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1109 | if (i == NANO_APPEND_KEY) |
| 1110 | return(do_writeout(answer, exiting, 1 - append)); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1111 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1112 | #ifdef DEBUG |
| 1113 | fprintf(stderr, _("filename is %s"), answer); |
| 1114 | #endif |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1115 | |
| 1116 | #ifdef NANO_EXTRA |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1117 | if (exiting && !ISSET(TEMP_OPT) && !strcasecmp(answer, "zzy") |
| 1118 | && !did_cred) { |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1119 | do_credits(); |
| 1120 | did_cred = 1; |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1121 | return -1; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1122 | } |
| 1123 | #endif |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1124 | if (!append && strcmp(answer, filename)) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1125 | struct stat st; |
| 1126 | if (!stat(answer, &st)) { |
| 1127 | i = do_yesno(0, 0, _("File exists, OVERWRITE ?")); |
| 1128 | |
| 1129 | if (!i || (i == -1)) |
| 1130 | continue; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1133 | #ifndef NANO_SMALL |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1134 | |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1135 | /* Here's where we allow the selected text to be written to |
| 1136 | a separate file. */ |
| 1137 | if (ISSET(MARK_ISSET) && !exiting) { |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1138 | filestruct *fileagebak = fileage; |
| 1139 | filestruct *filebotbak = filebot; |
| 1140 | filestruct *cutback = cutbuffer; |
| 1141 | int oldmod = 0; |
Chris Allegretta | 500b5e3 | 2001-06-21 23:58:47 +0000 | [diff] [blame] | 1142 | cutbuffer = NULL; |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1143 | |
| 1144 | /* Okay, since write_file changes the filename, back it up */ |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1145 | if (ISSET(MODIFIED)) |
| 1146 | oldmod = 1; |
| 1147 | |
| 1148 | /* Now, non-destructively add the marked text to the |
| 1149 | cutbuffer, and write the file out using the cutbuffer ;) */ |
Chris Allegretta | 500b5e3 | 2001-06-21 23:58:47 +0000 | [diff] [blame] | 1150 | if (current->lineno <= mark_beginbuf->lineno) |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1151 | cut_marked_segment(current, current_x, mark_beginbuf, |
| 1152 | mark_beginx, 0); |
| 1153 | else |
| 1154 | cut_marked_segment(mark_beginbuf, mark_beginx, current, |
| 1155 | current_x, 0); |
| 1156 | |
| 1157 | fileage = cutbuffer; |
| 1158 | for (filebot = cutbuffer; filebot->next != NULL; |
| 1159 | filebot = filebot->next) |
| 1160 | ; |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1161 | i = write_file(answer, 0, append, 1); |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* Now restore everything */ |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1164 | fileage = fileagebak; |
| 1165 | filebot = filebotbak; |
| 1166 | cutbuffer = cutback; |
| 1167 | if (oldmod) |
| 1168 | set_modified(); |
| 1169 | } else |
| 1170 | #endif |
Chris Allegretta | ecc3d7f | 2001-06-05 23:24:55 +0000 | [diff] [blame] | 1171 | i = write_file(answer, 0, append, 0); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1172 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 1173 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1174 | /* if we're not about to exit, update the current entry in |
| 1175 | the open_files structure */ |
| 1176 | if (!exiting) { |
| 1177 | |
| 1178 | /* first, if the filename was changed during the save, |
| 1179 | update the filename and full path stored in the |
| 1180 | current entry, and then update the current entry, |
| 1181 | checking for duplicate entries */ |
Chris Allegretta | e642197 | 2001-07-18 01:03:36 +0000 | [diff] [blame^] | 1182 | if (strcmp(open_files->data, filename)) { |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1183 | open_file_change_name(); |
| 1184 | add_open_file(1, 1); |
| 1185 | } |
| 1186 | else { |
| 1187 | |
| 1188 | /* otherwise, just update the current entry without |
| 1189 | checking for duplicate entries */ |
| 1190 | add_open_file(1, 0); |
| 1191 | } |
| 1192 | } |
| 1193 | #endif |
| 1194 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1195 | display_main_list(); |
| 1196 | return i; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1197 | } else { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1198 | statusbar(_("Cancelled")); |
| 1199 | display_main_list(); |
| 1200 | return 0; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1201 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | int do_writeout_void(void) |
| 1206 | { |
Chris Allegretta | cc197ef | 2001-05-29 04:21:44 +0000 | [diff] [blame] | 1207 | return do_writeout(filename, 0, 0); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1208 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1209 | |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 1210 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1211 | |
| 1212 | /* Return a malloc()ed string containing the actual directory, used |
| 1213 | * to convert ~user and ~/ notation... |
| 1214 | */ |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1215 | char *real_dir_from_tilde(char *buf) |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1216 | { |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1217 | char *dirtmp = NULL, *find_user = NULL; |
| 1218 | int i = 1; |
Chris Allegretta | ee733e6 | 2001-01-22 22:17:08 +0000 | [diff] [blame] | 1219 | struct passwd *userdata; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1220 | |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1221 | /* set a default value for dirtmp, in the case user home dir not found */ |
| 1222 | dirtmp = mallocstrcpy(dirtmp, buf); |
| 1223 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1224 | if (buf[0] == '~') { |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1225 | if (buf[1] == 0 || buf[1] == '/') { |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1226 | if (getenv("HOME") != NULL) { |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1227 | |
| 1228 | free(dirtmp); |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1229 | dirtmp = charalloc(strlen(buf) + 2 + strlen(getenv("HOME"))); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1230 | |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1231 | sprintf(dirtmp, "%s%s", getenv("HOME"), &buf[1]); |
Chris Allegretta | bf69261 | 2001-01-08 16:59:19 +0000 | [diff] [blame] | 1232 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1233 | } |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1234 | } |
| 1235 | else { |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1236 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1237 | /* Figure how how much of the str we need to compare */ |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1238 | for (i = 1; buf[i] != '/' && buf[i] != 0; i++) |
| 1239 | ; |
| 1240 | |
| 1241 | find_user = mallocstrcpy(find_user, &buf[1]); |
| 1242 | find_user[i - 1] = 0; |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1243 | |
Chris Allegretta | ee733e6 | 2001-01-22 22:17:08 +0000 | [diff] [blame] | 1244 | for (userdata = getpwent(); userdata != NULL && |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1245 | strcmp(userdata->pw_name, find_user); |
Chris Allegretta | ee733e6 | 2001-01-22 22:17:08 +0000 | [diff] [blame] | 1246 | userdata = getpwent()); |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1247 | |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1248 | free(find_user); |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1249 | |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1250 | if (userdata != NULL) { /* User found */ |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1251 | |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1252 | free(dirtmp); |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1253 | dirtmp = charalloc(strlen(buf) + 2 + strlen(userdata->pw_dir)); |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1254 | sprintf(dirtmp, "%s%s", userdata->pw_dir, &buf[i]); |
| 1255 | |
| 1256 | } |
| 1257 | |
Chris Allegretta | ee733e6 | 2001-01-22 22:17:08 +0000 | [diff] [blame] | 1258 | endpwent(); |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1259 | } |
Chris Allegretta | e5e4d49 | 2001-01-23 03:09:15 +0000 | [diff] [blame] | 1260 | } |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1261 | |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1262 | return dirtmp; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /* Tack a slash onto the string we're completing if it's a directory */ |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1266 | int append_slash_if_dir(char *buf, int *lastWasTab, int *place) |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1267 | { |
| 1268 | char *dirptr; |
| 1269 | struct stat fileinfo; |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1270 | int ret = 0; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1271 | |
| 1272 | dirptr = real_dir_from_tilde(buf); |
| 1273 | |
| 1274 | if (stat(dirptr, &fileinfo) == -1) |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1275 | ret = 0; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1276 | else if (S_ISDIR(fileinfo.st_mode)) { |
| 1277 | strncat(buf, "/", 1); |
| 1278 | *place += 1; |
| 1279 | /* now we start over again with # of tabs so far */ |
| 1280 | *lastWasTab = 0; |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1281 | ret = 1; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | if (dirptr != buf) |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1285 | free(dirptr); |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1286 | |
| 1287 | return ret; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1288 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * These functions (username_tab_completion, cwd_tab_completion, and |
| 1292 | * input_tab were taken from busybox 0.46 (cmdedit.c). Here is the notice |
| 1293 | * from that file: |
| 1294 | * |
| 1295 | * Termios command line History and Editting, originally |
| 1296 | * intended for NetBSD sh (ash) |
| 1297 | * Copyright (c) 1999 |
| 1298 | * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu> |
| 1299 | * Etc: Dave Cinege <dcinege@psychosis.com> |
| 1300 | * Majorly adjusted/re-written for busybox: |
| 1301 | * Erik Andersen <andersee@debian.org> |
| 1302 | * |
| 1303 | * You may use this code as you wish, so long as the original author(s) |
| 1304 | * are attributed in any redistributions of the source code. |
| 1305 | * This code is 'as is' with no warranty. |
| 1306 | * This code may safely be consumed by a BSD or GPL license. |
| 1307 | */ |
| 1308 | |
| 1309 | char **username_tab_completion(char *buf, int *num_matches) |
| 1310 | { |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1311 | char **matches = (char **) NULL; |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 1312 | char *matchline = NULL; |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1313 | struct passwd *userdata; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1314 | |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 1315 | *num_matches = 0; |
Rocco Corsi | 8b6cccc | 2001-01-02 06:21:07 +0000 | [diff] [blame] | 1316 | matches = nmalloc(BUFSIZ * sizeof(char *)); |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 1317 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1318 | strcat(buf, "*"); |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 1319 | |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1320 | while ((userdata = getpwent()) != NULL) { |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1321 | |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1322 | if (check_wildcard_match(userdata->pw_name, &buf[1]) == TRUE) { |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1323 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1324 | /* Cool, found a match. Add it to the list |
| 1325 | * This makes a lot more sense to me (Chris) this way... |
| 1326 | */ |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 1327 | |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1328 | matchline = charalloc(strlen(userdata->pw_name) + 2); |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1329 | sprintf(matchline, "~%s", userdata->pw_name); |
| 1330 | matches[*num_matches] = matchline; |
| 1331 | ++*num_matches; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1332 | |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1333 | /* If there's no more room, bail out */ |
| 1334 | if (*num_matches == BUFSIZ) |
| 1335 | break; |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1336 | } |
Chris Allegretta | 2c2c5f2 | 2001-01-23 03:27:31 +0000 | [diff] [blame] | 1337 | } |
| 1338 | endpwent(); |
Chris Allegretta | 8d9b11a | 2001-01-19 04:17:24 +0000 | [diff] [blame] | 1339 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1340 | return (matches); |
| 1341 | } |
| 1342 | |
| 1343 | /* This was originally called exe_n_cwd_tab_completion, but we're not |
| 1344 | worried about executables, only filenames :> */ |
| 1345 | |
| 1346 | char **cwd_tab_completion(char *buf, int *num_matches) |
| 1347 | { |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1348 | char *dirName, *dirtmp = NULL, *tmp = NULL, *tmp2 = NULL; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1349 | char **matches = (char **) NULL; |
| 1350 | DIR *dir; |
| 1351 | struct dirent *next; |
| 1352 | |
Rocco Corsi | 8b6cccc | 2001-01-02 06:21:07 +0000 | [diff] [blame] | 1353 | matches = nmalloc(BUFSIZ * sizeof(char *)); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1354 | |
| 1355 | /* Stick a wildcard onto the buf, for later use */ |
| 1356 | strcat(buf, "*"); |
| 1357 | |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 1358 | /* Okie, if there's a / in the buffer, strip out the directory part */ |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1359 | if (strcmp(buf, "") && strstr(buf, "/")) { |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1360 | dirName = charalloc(strlen(buf) + 1); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1361 | tmp = buf + strlen(buf); |
| 1362 | while (*tmp != '/' && tmp != buf) |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1363 | tmp--; |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1364 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1365 | tmp++; |
| 1366 | |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1367 | strncpy(dirName, buf, tmp - buf + 1); |
| 1368 | dirName[tmp - buf] = 0; |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1369 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1370 | } else { |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 1371 | |
| 1372 | #ifdef PATH_MAX |
| 1373 | if ((dirName = getcwd(NULL, PATH_MAX+1)) == NULL) |
| 1374 | #else |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1375 | /* The better, but apparently segfault-causing way */ |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1376 | if ((dirName = getcwd(NULL, 0)) == NULL) |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 1377 | #endif /* PATH_MAX */ |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1378 | return matches; |
| 1379 | else |
| 1380 | tmp = buf; |
| 1381 | } |
| 1382 | |
| 1383 | #ifdef DEBUG |
| 1384 | fprintf(stderr, "\nDir = %s\n", dirName); |
| 1385 | fprintf(stderr, "\nbuf = %s\n", buf); |
| 1386 | fprintf(stderr, "\ntmp = %s\n", tmp); |
| 1387 | #endif |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1388 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1389 | dirtmp = real_dir_from_tilde(dirName); |
| 1390 | free(dirName); |
| 1391 | dirName = dirtmp; |
| 1392 | |
| 1393 | #ifdef DEBUG |
| 1394 | fprintf(stderr, "\nDir = %s\n", dirName); |
| 1395 | fprintf(stderr, "\nbuf = %s\n", buf); |
| 1396 | fprintf(stderr, "\ntmp = %s\n", tmp); |
| 1397 | #endif |
| 1398 | |
| 1399 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1400 | dir = opendir(dirName); |
| 1401 | if (!dir) { |
| 1402 | /* Don't print an error, just shut up and return */ |
| 1403 | *num_matches = 0; |
| 1404 | beep(); |
| 1405 | return (matches); |
| 1406 | } |
| 1407 | while ((next = readdir(dir)) != NULL) { |
| 1408 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1409 | #ifdef DEBUG |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 1410 | fprintf(stderr, "Comparing \'%s\'\n", next->d_name); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1411 | #endif |
| 1412 | /* See if this matches */ |
| 1413 | if (check_wildcard_match(next->d_name, tmp) == TRUE) { |
Chris Allegretta | 7d97ce7 | 2000-11-06 04:04:15 +0000 | [diff] [blame] | 1414 | |
| 1415 | /* Cool, found a match. Add it to the list |
| 1416 | * This makes a lot more sense to me (Chris) this way... |
| 1417 | */ |
| 1418 | tmp2 = NULL; |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1419 | tmp2 = charalloc(strlen(next->d_name) + 1); |
Chris Allegretta | 7d97ce7 | 2000-11-06 04:04:15 +0000 | [diff] [blame] | 1420 | strcpy(tmp2, next->d_name); |
| 1421 | matches[*num_matches] = tmp2; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1422 | ++*num_matches; |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* If there's no more room, bail out */ |
| 1425 | if (*num_matches == BUFSIZ) |
| 1426 | break; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | return (matches); |
| 1431 | } |
| 1432 | |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1433 | /* This function now has an arg which refers to how much the |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1434 | * statusbar (place) should be advanced, i.e. the new cursor pos. |
| 1435 | */ |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1436 | char *input_tab(char *buf, int place, int *lastWasTab, int *newplace) |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1437 | { |
| 1438 | /* Do TAB completion */ |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1439 | static int num_matches = 0, match_matches = 0; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1440 | static char **matches = (char **) NULL; |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1441 | int pos = place, i = 0, col = 0, editline = 0; |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1442 | int longestname = 0, is_dir = 0; |
Chris Allegretta | 3b0d144 | 2000-11-05 21:56:54 +0000 | [diff] [blame] | 1443 | char *foo; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1444 | |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1445 | if (*lastWasTab == FALSE) { |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1446 | char *tmp, *copyto, *matchBuf; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1447 | |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1448 | *lastWasTab = 1; |
| 1449 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1450 | /* Make a local copy of the string -- up to the position of the |
| 1451 | cursor */ |
Chris Allegretta | a35ec04 | 2001-01-15 02:26:12 +0000 | [diff] [blame] | 1452 | matchBuf = (char *) nmalloc((strlen(buf) + 2) * sizeof(char)); |
Chris Allegretta | 4058709 | 2001-01-16 04:45:38 +0000 | [diff] [blame] | 1453 | memset(matchBuf, '\0', (strlen(buf) + 2)); |
Chris Allegretta | e118acc | 2000-11-06 05:40:03 +0000 | [diff] [blame] | 1454 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1455 | strncpy(matchBuf, buf, place); |
| 1456 | tmp = matchBuf; |
| 1457 | |
| 1458 | /* skip any leading white space */ |
Chris Allegretta | 63a89d3 | 2000-11-18 03:05:50 +0000 | [diff] [blame] | 1459 | while (*tmp && isspace((int) *tmp)) |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1460 | ++tmp; |
| 1461 | |
| 1462 | /* Free up any memory already allocated */ |
| 1463 | if (matches != NULL) { |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1464 | for (i = i; i < num_matches; i++) |
| 1465 | free(matches[i]); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1466 | free(matches); |
| 1467 | matches = (char **) NULL; |
Chris Allegretta | 7d97ce7 | 2000-11-06 04:04:15 +0000 | [diff] [blame] | 1468 | num_matches = 0; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* If the word starts with `~' and there is no slash in the word, |
| 1472 | * then try completing this word as a username. */ |
| 1473 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1474 | /* FIXME -- this check is broken! */ |
Chris Allegretta | 1bd0ce2 | 2000-12-06 05:56:08 +0000 | [diff] [blame] | 1475 | if (*tmp == '~' && !strchr(tmp, '/')) |
| 1476 | matches = username_tab_completion(tmp, &num_matches); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1477 | |
| 1478 | /* Try to match everything in the current working directory that |
| 1479 | * matches. */ |
| 1480 | if (!matches) |
| 1481 | matches = cwd_tab_completion(tmp, &num_matches); |
| 1482 | |
| 1483 | /* Don't leak memory */ |
| 1484 | free(matchBuf); |
| 1485 | |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1486 | #ifdef DEBUG |
| 1487 | fprintf(stderr, "%d matches found...\n", num_matches); |
| 1488 | #endif |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1489 | /* Did we find exactly one match? */ |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1490 | switch (num_matches) { |
Chris Allegretta | e118acc | 2000-11-06 05:40:03 +0000 | [diff] [blame] | 1491 | case 0: |
Chris Allegretta | 2434243 | 2000-11-06 05:41:20 +0000 | [diff] [blame] | 1492 | blank_edit(); |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 1493 | wrefresh(edit); |
Chris Allegretta | e118acc | 2000-11-06 05:40:03 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | case 1: |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1496 | |
| 1497 | buf = nrealloc(buf, strlen(buf) + strlen(matches[0]) + 1); |
| 1498 | |
| 1499 | if (strcmp(buf, "") && strstr(buf, "/")) { |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1500 | for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf; |
| 1501 | tmp--); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1502 | tmp++; |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1503 | } else |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1504 | tmp = buf; |
| 1505 | |
Chris Allegretta | 04fec91 | 2000-11-25 04:43:43 +0000 | [diff] [blame] | 1506 | if (!strcmp(tmp, matches[0])) |
| 1507 | is_dir = append_slash_if_dir(buf, lastWasTab, newplace); |
| 1508 | |
| 1509 | if (is_dir) |
| 1510 | break; |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1511 | |
| 1512 | copyto = tmp; |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1513 | for (pos = 0; *tmp == matches[0][pos] && |
| 1514 | pos <= strlen(matches[0]); pos++) |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1515 | tmp++; |
| 1516 | |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 1517 | /* write out the matched name */ |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1518 | strncpy(copyto, matches[0], strlen(matches[0]) + 1); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1519 | *newplace += strlen(matches[0]) - pos; |
| 1520 | |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1521 | /* Is it a directory? */ |
| 1522 | append_slash_if_dir(buf, lastWasTab, newplace); |
| 1523 | |
Chris Allegretta | e118acc | 2000-11-06 05:40:03 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | default: |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1526 | /* Check to see if all matches share a beginning, and, if so, |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1527 | tack it onto buf and then beep */ |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1528 | |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1529 | if (strcmp(buf, "") && strstr(buf, "/")) { |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1530 | for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf; |
| 1531 | tmp--); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1532 | tmp++; |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1533 | } else |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1534 | tmp = buf; |
| 1535 | |
| 1536 | for (pos = 0; *tmp == matches[0][pos] && *tmp != 0 && |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1537 | pos <= strlen(matches[0]); pos++) |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1538 | tmp++; |
| 1539 | |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1540 | while (1) { |
| 1541 | match_matches = 0; |
| 1542 | |
| 1543 | for (i = 0; i < num_matches; i++) { |
| 1544 | if (matches[i][pos] == 0) |
| 1545 | break; |
| 1546 | else if (matches[i][pos] == matches[0][pos]) |
| 1547 | match_matches++; |
| 1548 | } |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1549 | if (match_matches == num_matches && |
| 1550 | (i == num_matches || matches[i] != 0)) { |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1551 | /* All the matches have the same character at pos+1, |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1552 | so paste it into buf... */ |
Chris Allegretta | 7586495 | 2000-11-05 22:48:35 +0000 | [diff] [blame] | 1553 | buf = nrealloc(buf, strlen(buf) + 2); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1554 | strncat(buf, matches[0] + pos, 1); |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1555 | *newplace += 1; |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1556 | pos++; |
Chris Allegretta | 7586495 | 2000-11-05 22:48:35 +0000 | [diff] [blame] | 1557 | } else { |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1558 | beep(); |
| 1559 | break; |
| 1560 | } |
| 1561 | } |
Chris Allegretta | e118acc | 2000-11-06 05:40:03 +0000 | [diff] [blame] | 1562 | break; |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1563 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1564 | } else { |
| 1565 | /* Ok -- the last char was a TAB. Since they |
| 1566 | * just hit TAB again, print a list of all the |
| 1567 | * available choices... */ |
| 1568 | if (matches && num_matches > 0) { |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1569 | |
| 1570 | /* Blank the edit window, and print the matches out there */ |
| 1571 | blank_edit(); |
| 1572 | wmove(edit, 0, 0); |
| 1573 | |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1574 | editline = 0; |
Chris Allegretta | 2c97522 | 2000-11-15 01:25:42 +0000 | [diff] [blame] | 1575 | |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1576 | /* Figure out the length of the longest filename */ |
| 1577 | for (i = 0; i < num_matches; i++) |
| 1578 | if (strlen(matches[i]) > longestname) |
| 1579 | longestname = strlen(matches[i]); |
| 1580 | |
| 1581 | if (longestname > COLS - 1) |
| 1582 | longestname = COLS - 1; |
| 1583 | |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1584 | foo = charalloc(longestname + 5); |
Chris Allegretta | 3b0d144 | 2000-11-05 21:56:54 +0000 | [diff] [blame] | 1585 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1586 | /* Print the list of matches */ |
| 1587 | for (i = 0, col = 0; i < num_matches; i++) { |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1588 | |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1589 | /* make each filename shown be the same length as the longest |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1590 | filename, with two spaces at the end */ |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1591 | snprintf(foo, longestname + 1, matches[i]); |
| 1592 | while (strlen(foo) < longestname) |
| 1593 | strcat(foo, " "); |
| 1594 | |
| 1595 | strcat(foo, " "); |
| 1596 | |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1597 | /* Disable el cursor */ |
| 1598 | curs_set(0); |
Chris Allegretta | 7586495 | 2000-11-05 22:48:35 +0000 | [diff] [blame] | 1599 | /* now, put the match on the screen */ |
| 1600 | waddnstr(edit, foo, strlen(foo)); |
| 1601 | col += strlen(foo); |
| 1602 | |
| 1603 | /* And if the next match isn't going to fit on the |
| 1604 | line, move to the next one */ |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1605 | if (col > (COLS - longestname) && matches[i + 1] != NULL) { |
Chris Allegretta | ec58a99 | 2000-11-05 21:54:23 +0000 | [diff] [blame] | 1606 | editline++; |
| 1607 | wmove(edit, editline, 0); |
Chris Allegretta | b5b89ae | 2000-11-14 18:25:26 +0000 | [diff] [blame] | 1608 | if (editline == editwinrows - 1) { |
| 1609 | waddstr(edit, _("(more)")); |
| 1610 | break; |
| 1611 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1612 | col = 0; |
| 1613 | } |
| 1614 | } |
Chris Allegretta | 3b0d144 | 2000-11-05 21:56:54 +0000 | [diff] [blame] | 1615 | free(foo); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1616 | wrefresh(edit); |
Chris Allegretta | 24dd8d6 | 2000-11-06 05:45:48 +0000 | [diff] [blame] | 1617 | } else |
| 1618 | beep(); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1619 | |
| 1620 | } |
| 1621 | |
| 1622 | edit_refresh(); |
Chris Allegretta | 442f2c5 | 2000-11-14 17:46:06 +0000 | [diff] [blame] | 1623 | curs_set(1); |
| 1624 | return buf; |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1625 | } |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1626 | #endif |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1627 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1628 | #ifndef DISABLE_BROWSER |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* Return the stat of the file pointed to by path */ |
| 1631 | struct stat filestat(const char *path) { |
| 1632 | struct stat st; |
| 1633 | |
Chris Allegretta | 0876dc9 | 2001-03-28 13:04:08 +0000 | [diff] [blame] | 1634 | stat(path, &st); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1635 | return st; |
| 1636 | } |
| 1637 | |
| 1638 | /* Our sort routine for file listings - sort directories before |
| 1639 | * files, and then alphabetically |
| 1640 | */ |
| 1641 | int diralphasort(const void *va, const void *vb) { |
| 1642 | struct stat file1info, file2info; |
| 1643 | char *a = *(char **)va, *b = *(char **)vb; |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 1644 | int aisdir, bisdir; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1645 | |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 1646 | aisdir = (stat(a, &file1info) != -1) && S_ISDIR(file1info.st_mode); |
| 1647 | bisdir = (stat(b, &file2info) != -1) && S_ISDIR(file2info.st_mode); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1648 | |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 1649 | if (aisdir && !bisdir) return -1; |
| 1650 | if (!aisdir && bisdir) return 1; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1651 | |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 1652 | #ifdef HAVE_STRCASECMP |
| 1653 | return(strcasecmp(a,b)); |
| 1654 | #else |
| 1655 | return(strcmp(a,b)); |
| 1656 | #endif |
| 1657 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1660 | |
| 1661 | /* Initialize the browser code, including the list of files in *path */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1662 | char **browser_init(char *path, int *longest, int *numents) |
| 1663 | { |
| 1664 | DIR *dir; |
| 1665 | struct dirent *next; |
| 1666 | char **filelist = (char **) NULL; |
| 1667 | int i = 0; |
| 1668 | |
| 1669 | dir = opendir(path); |
| 1670 | if (!dir) |
| 1671 | return NULL; |
| 1672 | |
| 1673 | *numents = 0; |
| 1674 | while ((next = readdir(dir)) != NULL) { |
| 1675 | if (!strcmp(next->d_name, ".")) |
| 1676 | continue; |
| 1677 | (*numents)++; |
| 1678 | if (strlen(next->d_name) > *longest) |
| 1679 | *longest = strlen(next->d_name); |
| 1680 | } |
| 1681 | rewinddir(dir); |
| 1682 | *longest += 10; |
| 1683 | |
| 1684 | filelist = nmalloc(*numents * sizeof (char *)); |
| 1685 | |
| 1686 | while ((next = readdir(dir)) != NULL) { |
| 1687 | if (!strcmp(next->d_name, ".")) |
| 1688 | continue; |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1689 | filelist[i] = charalloc(strlen(next->d_name) + strlen(path) + 2); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1690 | |
| 1691 | if (!strcmp(path, "/")) |
| 1692 | snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 1, |
| 1693 | "%s%s", path, next->d_name); |
| 1694 | else |
| 1695 | snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 2, |
| 1696 | "%s/%s", path, next->d_name); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1697 | i++; |
| 1698 | } |
| 1699 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1700 | if (*longest > COLS - 1) |
| 1701 | *longest = COLS - 1; |
| 1702 | |
| 1703 | return filelist; |
| 1704 | } |
| 1705 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1706 | /* Free our malloc()ed memory */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1707 | void free_charptrarray(char **array, int len) |
| 1708 | { |
| 1709 | int i; |
| 1710 | |
| 1711 | for (i = 0; i < len - 1; i++) |
| 1712 | free(array[i]); |
| 1713 | free(array); |
| 1714 | } |
| 1715 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1716 | /* only print the last part of a path; isn't there a shell |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1717 | command for this? */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1718 | char *tail(char *foo) |
| 1719 | { |
| 1720 | char *tmp = NULL; |
| 1721 | |
| 1722 | tmp = foo + strlen(foo); |
| 1723 | while (*tmp != '/' && tmp != foo) |
| 1724 | tmp--; |
| 1725 | |
| 1726 | tmp++; |
| 1727 | |
| 1728 | return tmp; |
| 1729 | } |
| 1730 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1731 | /* Strip one dir from the end of a string */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1732 | void striponedir(char *foo) |
| 1733 | { |
| 1734 | char *tmp = NULL; |
| 1735 | |
| 1736 | /* Don't strip the root dir */ |
| 1737 | if (!strcmp(foo, "/")) |
| 1738 | return; |
| 1739 | |
| 1740 | tmp = foo + strlen(foo); |
| 1741 | if (*tmp == '/') |
| 1742 | tmp--; |
| 1743 | |
| 1744 | while (*tmp != '/' && tmp != foo) |
| 1745 | tmp--; |
| 1746 | |
| 1747 | if (tmp != foo) |
| 1748 | *tmp = 0; |
| 1749 | else |
| 1750 | *(tmp+1) = 0; |
| 1751 | |
| 1752 | return; |
| 1753 | } |
| 1754 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1755 | /* Our browser function. inpath is the path to start browsing from */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1756 | char *do_browser(char *inpath) |
| 1757 | { |
| 1758 | struct stat st; |
| 1759 | char *foo, *retval = NULL; |
| 1760 | static char *path = NULL; |
| 1761 | int numents = 0, i = 0, j = 0, kbinput = 0, longest = 0, abort = 0; |
| 1762 | int col = 0, selected = 0, editline = 0, width = 0, filecols = 0; |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 1763 | int lineno = 0, kb; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1764 | char **filelist = (char **) NULL; |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 1765 | #ifndef DISABLE_MOUSE |
| 1766 | #ifdef NCURSES_MOUSE_VERSION |
| 1767 | MEVENT mevent; |
| 1768 | #endif |
| 1769 | #endif |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1770 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1771 | /* If path isn't the same as inpath, we are being passed a new |
| 1772 | dir as an arg. We free it here so it will be copied from |
| 1773 | inpath below */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1774 | if (path != NULL && strcmp(path, inpath)) { |
| 1775 | free(path); |
| 1776 | path = NULL; |
| 1777 | } |
| 1778 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1779 | /* if path doesn't exist, make it so */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1780 | if (path == NULL) |
| 1781 | path = mallocstrcpy(path, inpath); |
| 1782 | |
| 1783 | filelist = browser_init(path, &longest, &numents); |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 1784 | foo = charalloc(longest + 8); |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1785 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1786 | /* Sort the list by directory first, then alphabetically */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1787 | qsort(filelist, numents, sizeof(char *), diralphasort); |
| 1788 | |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 1789 | kb = keypad_on(edit, 1); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1790 | titlebar(path); |
| 1791 | bottombars(browser_list, BROWSER_LIST_LEN); |
| 1792 | curs_set(0); |
| 1793 | wmove(edit, 0, 0); |
| 1794 | i = 0; |
| 1795 | width = 0; |
| 1796 | filecols = 0; |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* Loop invariant: Microsoft sucks. */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1799 | do { |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1800 | DIR *test_dir; |
| 1801 | |
| 1802 | blank_statusbar_refresh(); |
| 1803 | |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1804 | #ifndef DISABLE_MOUSE |
Chris Allegretta | b10283c | 2001-05-07 12:08:37 +0000 | [diff] [blame] | 1805 | currshortcut = browser_list; |
| 1806 | currslen = BROWSER_LIST_LEN; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1807 | #endif |
| 1808 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1809 | editline = 0; |
| 1810 | col = 0; |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 1811 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1812 | /* Compute line number we're on now, so we don't divide by zero later */ |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 1813 | if (width == 0) |
| 1814 | lineno = selected; |
| 1815 | else |
| 1816 | lineno = selected / width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1817 | |
| 1818 | switch (kbinput) { |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1819 | |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 1820 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1821 | #ifdef NCURSES_MOUSE_VERSION |
| 1822 | case KEY_MOUSE: |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 1823 | if (getmouse(&mevent) == ERR) |
| 1824 | return retval; |
| 1825 | |
| 1826 | /* If they clicked in the edit window, they probably clicked |
| 1827 | on a file */ |
| 1828 | if (wenclose(edit, mevent.y, mevent.x)) { |
| 1829 | int selectedbackup = selected; |
| 1830 | |
| 1831 | mevent.y -= 2; |
| 1832 | |
| 1833 | /* If we're on line 0, don't toy with finding out what |
| 1834 | page we're on */ |
| 1835 | if (lineno / editwinrows == 0) |
| 1836 | selected = mevent.y * width + mevent.x / longest; |
| 1837 | else |
| 1838 | selected = (lineno / editwinrows) * editwinrows * width |
| 1839 | + mevent.y * width + mevent.x / longest; |
| 1840 | |
| 1841 | /* If we're off the screen, reset to the last item. |
| 1842 | If we clicked where we did last time, select this name! */ |
Chris Allegretta | f3fde7c | 2001-05-06 03:02:21 +0000 | [diff] [blame] | 1843 | if (selected > numents - 1) |
Chris Allegretta | 051fc6e | 2001-05-05 23:17:36 +0000 | [diff] [blame] | 1844 | selected = numents - 1; |
| 1845 | else if (selectedbackup == selected) { |
| 1846 | ungetch('s'); /* Unget the 'select' key */ |
| 1847 | break; |
| 1848 | } |
| 1849 | } else /* Must be clicking a shortcut */ |
| 1850 | do_mouse(); |
| 1851 | |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | #endif |
| 1854 | #endif |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1855 | case NANO_UP_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1856 | case KEY_UP: |
| 1857 | case 'u': |
| 1858 | if (selected - width >= 0) |
| 1859 | selected -= width; |
| 1860 | break; |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1861 | case NANO_BACK_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1862 | case KEY_LEFT: |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1863 | case NANO_BACKSPACE_KEY: |
| 1864 | case 127: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1865 | case 'l': |
| 1866 | if (selected > 0) |
| 1867 | selected--; |
| 1868 | break; |
| 1869 | case KEY_DOWN: |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1870 | case NANO_DOWN_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1871 | case 'd': |
| 1872 | if (selected + width <= numents - 1) |
| 1873 | selected += width; |
| 1874 | break; |
| 1875 | case KEY_RIGHT: |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1876 | case NANO_FORWARD_KEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1877 | case 'r': |
| 1878 | if (selected < numents - 1) |
| 1879 | selected++; |
| 1880 | break; |
| 1881 | case NANO_PREVPAGE_KEY: |
Chris Allegretta | 6e82741 | 2001-01-04 04:41:08 +0000 | [diff] [blame] | 1882 | case NANO_PREVPAGE_FKEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1883 | case KEY_PPAGE: |
Chris Allegretta | 8eac3b5 | 2001-01-08 21:14:04 +0000 | [diff] [blame] | 1884 | case '-': |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 1885 | |
| 1886 | if (lineno % editwinrows == 0) { |
Chris Allegretta | 425a266 | 2001-01-03 15:09:27 +0000 | [diff] [blame] | 1887 | if (selected - (editwinrows * width) >= 0) |
| 1888 | selected -= editwinrows * width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1889 | else |
| 1890 | selected = 0; |
| 1891 | } |
| 1892 | else if (selected - (editwinrows + |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 1893 | lineno % editwinrows) * width >= 0) |
| 1894 | |
| 1895 | selected -= (editwinrows + lineno % editwinrows) * width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1896 | else |
| 1897 | selected = 0; |
| 1898 | break; |
| 1899 | case NANO_NEXTPAGE_KEY: |
Chris Allegretta | 6e82741 | 2001-01-04 04:41:08 +0000 | [diff] [blame] | 1900 | case NANO_NEXTPAGE_FKEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1901 | case KEY_NPAGE: |
Chris Allegretta | 8eac3b5 | 2001-01-08 21:14:04 +0000 | [diff] [blame] | 1902 | case ' ': |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 1903 | if (lineno % editwinrows == 0) { |
Chris Allegretta | 425a266 | 2001-01-03 15:09:27 +0000 | [diff] [blame] | 1904 | if (selected + (editwinrows * width) <= numents - 1) |
| 1905 | selected += editwinrows * width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1906 | else |
| 1907 | selected = numents - 1; |
| 1908 | } |
| 1909 | else if (selected + (editwinrows - |
Chris Allegretta | b0f5282 | 2001-01-04 05:20:23 +0000 | [diff] [blame] | 1910 | lineno % editwinrows) * width <= numents - 1) |
| 1911 | selected += (editwinrows - lineno % editwinrows) * width; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1912 | else |
| 1913 | selected = numents - 1; |
| 1914 | break; |
| 1915 | case KEY_ENTER: |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1916 | case NANO_ENTER_KEY: |
Chris Allegretta | ba8edea | 2001-01-04 05:22:46 +0000 | [diff] [blame] | 1917 | case 's': /* More Pico compatibility */ |
| 1918 | case 'S': |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1919 | |
| 1920 | /* You can't cd up from / */ |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1921 | if (!strcmp(filelist[selected], "/..") && !strcmp(path, "/")) { |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1922 | statusbar(_("Can't move up a directory")); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | } |
| 1925 | |
| 1926 | path = mallocstrcpy(path, filelist[selected]); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1927 | |
| 1928 | st = filestat(path); |
| 1929 | if (S_ISDIR(st.st_mode)) { |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1930 | if ((test_dir = opendir(path)) == NULL) { |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1931 | /* We can't open this dir for some reason. Complain */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1932 | statusbar(_("Can't open \"%s\": %s"), path, strerror(errno)); |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1933 | striponedir(path); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1934 | align(&path); |
| 1935 | break; |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1936 | } |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1937 | closedir(test_dir); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1938 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1939 | if (!strcmp("..", tail(path))) { |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1940 | /* They want to go up a level, so strip off .. and the |
| 1941 | current dir */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1942 | striponedir(path); |
| 1943 | striponedir(path); |
| 1944 | align(&path); |
| 1945 | } |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 1946 | |
| 1947 | /* Start over again with the new path value */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1948 | return do_browser(path); |
| 1949 | } else { |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1950 | retval = path; |
| 1951 | abort = 1; |
| 1952 | } |
| 1953 | break; |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 1954 | /* Goto a specific directory */ |
| 1955 | case 'g': /* Pico compatibility */ |
| 1956 | case 'G': |
| 1957 | case NANO_GOTO_KEY: |
| 1958 | |
| 1959 | curs_set(1); |
| 1960 | j = statusq(0, gotodir_list, GOTODIR_LIST_LEN, "", _("Goto Directory")); |
| 1961 | bottombars(browser_list, BROWSER_LIST_LEN); |
| 1962 | curs_set(0); |
| 1963 | |
| 1964 | if (j < 0) { |
| 1965 | statusbar(_("Goto Cancelled")); |
| 1966 | break; |
| 1967 | } |
| 1968 | |
| 1969 | if (answer[0] != '/') { |
| 1970 | char *saveanswer = NULL; |
| 1971 | |
| 1972 | saveanswer = mallocstrcpy(saveanswer, answer); |
| 1973 | answer = realloc(answer, strlen(path) + strlen(saveanswer) + 2); |
| 1974 | sprintf(answer, "%s/%s", path, saveanswer); |
| 1975 | free(saveanswer); |
| 1976 | } |
| 1977 | |
| 1978 | if ((test_dir = opendir(answer)) == NULL) { |
| 1979 | /* We can't open this dir for some reason. Complain */ |
| 1980 | statusbar(_("Can't open \"%s\": %s"), answer, strerror(errno)); |
| 1981 | break; |
| 1982 | } |
| 1983 | closedir(test_dir); |
| 1984 | |
| 1985 | /* Start over again with the new path value */ |
| 1986 | path = mallocstrcpy(path, answer); |
| 1987 | return do_browser(path); |
| 1988 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1989 | /* Stuff we want to abort the browser */ |
| 1990 | case 'q': |
| 1991 | case 'Q': |
| 1992 | case 'e': /* Pico compatibility, yeech */ |
| 1993 | case 'E': |
Chris Allegretta | a2c02e9 | 2001-07-02 01:31:44 +0000 | [diff] [blame] | 1994 | case NANO_CANCEL_KEY: |
Chris Allegretta | 6e82741 | 2001-01-04 04:41:08 +0000 | [diff] [blame] | 1995 | case NANO_EXIT_FKEY: |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1996 | abort = 1; |
| 1997 | break; |
| 1998 | } |
| 1999 | if (abort) |
| 2000 | break; |
| 2001 | |
Rocco Corsi | 12f294c | 2001-04-14 06:50:24 +0000 | [diff] [blame] | 2002 | blank_edit(); |
| 2003 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2004 | if (width) |
| 2005 | i = width * editwinrows * ((selected / width) / editwinrows); |
| 2006 | else |
| 2007 | i = 0; |
| 2008 | |
| 2009 | wmove(edit, 0, 0); |
| 2010 | for (j = i; j < numents && editline <= editwinrows - 1; j++) { |
| 2011 | filecols++; |
| 2012 | |
| 2013 | strncpy(foo, tail(filelist[j]), strlen(tail(filelist[j])) + 1); |
| 2014 | while (strlen(foo) < longest) |
| 2015 | strcat(foo, " "); |
| 2016 | col += strlen(foo); |
| 2017 | |
| 2018 | /* Put file info in the string also */ |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2019 | /* We use lstat here to detect links; then, if we find a |
| 2020 | symlink, we examine it via stat() to see if it is a |
Chris Allegretta | 0876dc9 | 2001-03-28 13:04:08 +0000 | [diff] [blame] | 2021 | directory or just a file symlink */ |
| 2022 | lstat(filelist[j], &st); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2023 | if (S_ISDIR(st.st_mode)) |
| 2024 | strcpy(foo + longest - 5, "(dir)"); |
| 2025 | else { |
Chris Allegretta | 0876dc9 | 2001-03-28 13:04:08 +0000 | [diff] [blame] | 2026 | if (S_ISLNK(st.st_mode)) { |
| 2027 | /* Aha! It's a symlink! Now, is it a dir? If so, |
| 2028 | mark it as such */ |
| 2029 | st = filestat(filelist[j]); |
| 2030 | if (S_ISDIR(st.st_mode)) |
| 2031 | strcpy(foo + longest - 5, "(dir)"); |
| 2032 | else |
| 2033 | strcpy(foo + longest - 2, "--"); |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2034 | } else if (st.st_size < (1 << 10)) /* less than 1 K */ |
| 2035 | sprintf(foo + longest - 7, "%4d B", |
Chris Allegretta | d461562 | 2001-05-23 21:54:47 +0000 | [diff] [blame] | 2036 | (int) st.st_size); |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2037 | else if (st.st_size >= (1 << 30)) /* at least 1 gig */ |
| 2038 | sprintf(foo + longest - 7, "%4d GB", |
| 2039 | (int) st.st_size >> 30); |
| 2040 | else if (st.st_size >= (1 << 20)) /* at least 1 meg */ |
| 2041 | sprintf(foo + longest - 7, "%4d MB", |
| 2042 | (int) st.st_size >> 20); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2043 | else /* Its more than 1 k and less than a meg */ |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2044 | sprintf(foo + longest - 7, "%4d KB", |
| 2045 | (int) st.st_size >> 10); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2048 | /* Hilight the currently selected file/dir */ |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2049 | if (j == selected) { |
| 2050 | #ifdef ENABLE_COLOR |
| 2051 | color_on(edit, COLOR_STATUSBAR); |
| 2052 | #else |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2053 | wattron(edit, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2054 | |
| 2055 | #endif |
| 2056 | } |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2057 | waddnstr(edit, foo, strlen(foo)); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2058 | if (j == selected) { |
| 2059 | #ifdef ENABLE_COLOR |
| 2060 | color_off(edit, COLOR_STATUSBAR); |
| 2061 | #else |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2062 | wattroff(edit, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2063 | #endif |
| 2064 | } |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2065 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2066 | /* And add some space between the cols */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2067 | waddstr(edit, " "); |
| 2068 | col += 2; |
| 2069 | |
| 2070 | /* And if the next entry isn't going to fit on the |
| 2071 | line, move to the next one */ |
| 2072 | if (col > (COLS - longest)) { |
| 2073 | editline++; |
| 2074 | wmove(edit, editline, 0); |
| 2075 | col = 0; |
| 2076 | if (width == 0) |
| 2077 | width = filecols; |
| 2078 | } |
| 2079 | } |
| 2080 | wrefresh(edit); |
| 2081 | } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY); |
| 2082 | curs_set(1); |
| 2083 | blank_edit(); |
| 2084 | titlebar(NULL); |
| 2085 | edit_refresh(); |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 2086 | kb = keypad_on(edit, kb); |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2087 | |
Chris Allegretta | 0e9d3c6 | 2001-01-04 04:59:17 +0000 | [diff] [blame] | 2088 | /* cleanup */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2089 | free_charptrarray(filelist, numents); |
| 2090 | free(foo); |
| 2091 | return retval; |
| 2092 | } |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2093 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2094 | /* 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] | 2095 | starts do_browser from there, else from the current dir */ |
| 2096 | char *do_browse_from(char *inpath) |
| 2097 | { |
| 2098 | struct stat st; |
| 2099 | char *tmp = NULL; |
| 2100 | |
| 2101 | tmp = mallocstrcpy(tmp, inpath); |
| 2102 | |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2103 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2104 | /* If there's no / in the string, we may as well start from . */ |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2105 | if (tmp == NULL || *tmp == '\0' || !strstr(tmp, "/")) { |
| 2106 | #ifdef PATH_MAX |
| 2107 | char *from = getcwd(NULL, PATH_MAX+1); |
| 2108 | #else |
Chris Allegretta | 60c6542 | 2001-04-03 14:26:35 +0000 | [diff] [blame] | 2109 | char *from = getcwd(NULL, 0); |
Chris Allegretta | 90d3074 | 2001-04-03 01:02:38 +0000 | [diff] [blame] | 2110 | #endif /* PATH_MAX */ |
| 2111 | return do_browser(from ? from : "./"); |
| 2112 | } |
Chris Allegretta | 150469a | 2001-01-05 21:13:14 +0000 | [diff] [blame] | 2113 | |
| 2114 | /* If the string is a directory, pass do_browser that */ |
| 2115 | st = filestat(tmp); |
| 2116 | if (S_ISDIR(st.st_mode)) |
| 2117 | return do_browser(tmp); |
| 2118 | |
| 2119 | /* Okay, there's a dir in there, but not at the end of the string... |
| 2120 | try stripping it off */ |
| 2121 | striponedir(tmp); |
| 2122 | align(&tmp); |
| 2123 | return do_browser(tmp); |
| 2124 | |
| 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 2129 | #endif |
| 2130 | |