Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * fileio.c: read from and write to a file |
| 12 | */ |
| 13 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | f4888d0 | 2009-12-02 12:31:27 +0000 | [diff] [blame] | 16 | #if defined(__TANDEM) || defined(__MINT__) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | # include <limits.h> /* for SSIZE_MAX */ |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | /* Is there any system that doesn't have access()? */ |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 21 | #define USE_MCH_ACCESS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 23 | static char_u *next_fenc(char_u **pp, int *alloced); |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 24 | #ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 25 | static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 28 | static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 30 | static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 31 | static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 32 | static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 33 | |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 34 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 35 | filemess( |
| 36 | buf_T *buf, |
| 37 | char_u *name, |
| 38 | char_u *s, |
| 39 | int attr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 40 | { |
| 41 | int msg_scroll_save; |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 42 | int prev_msg_col = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | |
| 44 | if (msg_silent != 0) |
| 45 | return; |
| 46 | msg_add_fname(buf, name); /* put file name in IObuff with quotes */ |
| 47 | /* If it's extremely long, truncate it. */ |
| 48 | if (STRLEN(IObuff) > IOSIZE - 80) |
| 49 | IObuff[IOSIZE - 80] = NUL; |
| 50 | STRCAT(IObuff, s); |
| 51 | /* |
| 52 | * For the first message may have to start a new line. |
| 53 | * For further ones overwrite the previous one, reset msg_scroll before |
| 54 | * calling filemess(). |
| 55 | */ |
| 56 | msg_scroll_save = msg_scroll; |
| 57 | if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
| 58 | msg_scroll = FALSE; |
| 59 | if (!msg_scroll) /* wait a bit when overwriting an error msg */ |
| 60 | check_for_delay(FALSE); |
| 61 | msg_start(); |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 62 | if (prev_msg_col != 0 && msg_col == 0) |
| 63 | msg_putchar('\r'); // overwrite any previous message. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | msg_scroll = msg_scroll_save; |
| 65 | msg_scrolled_ign = TRUE; |
| 66 | /* may truncate the message to avoid a hit-return prompt */ |
| 67 | msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); |
| 68 | msg_clr_eos(); |
| 69 | out_flush(); |
| 70 | msg_scrolled_ign = FALSE; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Read lines from file "fname" into the buffer after line "from". |
| 75 | * |
| 76 | * 1. We allocate blocks with lalloc, as big as possible. |
| 77 | * 2. Each block is filled with characters from the file with a single read(). |
| 78 | * 3. The lines are inserted in the buffer with ml_append(). |
| 79 | * |
| 80 | * (caller must check that fname != NULL, unless READ_STDIN is used) |
| 81 | * |
| 82 | * "lines_to_skip" is the number of lines that must be skipped |
| 83 | * "lines_to_read" is the number of lines that are appended |
| 84 | * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. |
| 85 | * |
| 86 | * flags: |
| 87 | * READ_NEW starting to edit a new buffer |
| 88 | * READ_FILTER reading filter output |
| 89 | * READ_STDIN read from stdin instead of a file |
| 90 | * READ_BUFFER read from curbuf instead of a file (converting after reading |
| 91 | * stdin) |
| 92 | * READ_DUMMY read into a dummy buffer (to check if file contents changed) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 93 | * READ_KEEP_UNDO don't clear undo info or read it from a file |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 94 | * READ_FIFO read from fifo/socket instead of a file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | * |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 96 | * return FAIL for failure, NOTDONE for directory (failure), or OK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | */ |
| 98 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 99 | readfile( |
| 100 | char_u *fname, |
| 101 | char_u *sfname, |
| 102 | linenr_T from, |
| 103 | linenr_T lines_to_skip, |
| 104 | linenr_T lines_to_read, |
| 105 | exarg_T *eap, /* can be NULL! */ |
| 106 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | { |
| 108 | int fd = 0; |
| 109 | int newfile = (flags & READ_NEW); |
| 110 | int check_readonly; |
| 111 | int filtering = (flags & READ_FILTER); |
| 112 | int read_stdin = (flags & READ_STDIN); |
| 113 | int read_buffer = (flags & READ_BUFFER); |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 114 | int read_fifo = (flags & READ_FIFO); |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 115 | int set_options = newfile || read_buffer |
| 116 | || (eap != NULL && eap->read_edit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
| 118 | colnr_T read_buf_col = 0; /* next char to read from this line */ |
| 119 | char_u c; |
| 120 | linenr_T lnum = from; |
| 121 | char_u *ptr = NULL; /* pointer into read buffer */ |
| 122 | char_u *buffer = NULL; /* read buffer */ |
| 123 | char_u *new_buffer = NULL; /* init to shut up gcc */ |
| 124 | char_u *line_start = NULL; /* init to shut up gcc */ |
| 125 | int wasempty; /* buffer was empty before reading */ |
| 126 | colnr_T len; |
| 127 | long size = 0; |
| 128 | char_u *p; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 129 | off_T filesize = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | int skip_read = FALSE; |
| 131 | #ifdef FEAT_CRYPT |
| 132 | char_u *cryptkey = NULL; |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 133 | int did_ask_for_key = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 135 | #ifdef FEAT_PERSISTENT_UNDO |
| 136 | context_sha256_T sha_ctx; |
| 137 | int read_undo_file = FALSE; |
| 138 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | int split = 0; /* number of split lines */ |
| 140 | #define UNKNOWN 0x0fffffff /* file size is unknown */ |
| 141 | linenr_T linecnt; |
| 142 | int error = FALSE; /* errors encountered */ |
| 143 | int ff_error = EOL_UNKNOWN; /* file format with errors */ |
| 144 | long linerest = 0; /* remaining chars in line */ |
| 145 | #ifdef UNIX |
| 146 | int perm = 0; |
| 147 | int swap_mode = -1; /* protection bits for swap file */ |
| 148 | #else |
| 149 | int perm; |
| 150 | #endif |
| 151 | int fileformat = 0; /* end-of-line format */ |
| 152 | int keep_fileformat = FALSE; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 153 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | int file_readonly; |
| 155 | linenr_T skip_count = 0; |
| 156 | linenr_T read_count = 0; |
| 157 | int msg_save = msg_scroll; |
| 158 | linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of |
| 159 | * last read was missing the eol */ |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 160 | int try_mac; |
| 161 | int try_dos; |
| 162 | int try_unix; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | int file_rewind = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | int can_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 165 | linenr_T conv_error = 0; /* line nr with conversion error */ |
| 166 | linenr_T illegal_byte = 0; /* line nr with illegal byte */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
| 168 | in destination encoding */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 169 | int bad_char_behavior = BAD_REPLACE; |
| 170 | /* BAD_KEEP, BAD_DROP or character to |
| 171 | * replace with */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
| 173 | int fio_flags = 0; |
| 174 | char_u *fenc; /* fileencoding to use */ |
| 175 | int fenc_alloced; /* fenc_next is in allocated memory */ |
| 176 | char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ |
| 177 | int advance_fenc = FALSE; |
| 178 | long real_size = 0; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 179 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 181 | # ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | int did_iconv = FALSE; /* TRUE when iconv() failed and trying |
| 183 | 'charconvert' next */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 184 | # endif |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 185 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 186 | int converted = FALSE; /* TRUE if conversion done */ |
| 187 | int notconverted = FALSE; /* TRUE if conversion wanted but it |
| 188 | wasn't possible */ |
| 189 | char_u conv_rest[CONV_RESTLEN]; |
| 190 | int conv_restlen = 0; /* nr of bytes in conv_rest[] */ |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 191 | pos_T orig_start; |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 192 | buf_T *old_curbuf; |
| 193 | char_u *old_b_ffname; |
| 194 | char_u *old_b_fname; |
| 195 | int using_b_ffname; |
| 196 | int using_b_fname; |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 197 | |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 198 | au_did_filetype = FALSE; /* reset before triggering any autocommands */ |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 199 | |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 200 | curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * If there is no file name yet, use the one for the read file. |
| 204 | * BF_NOTEDITED is set to reflect this. |
| 205 | * Don't do this for a read from a filter. |
| 206 | * Only do this when 'cpoptions' contains the 'f' flag. |
| 207 | */ |
| 208 | if (curbuf->b_ffname == NULL |
| 209 | && !filtering |
| 210 | && fname != NULL |
| 211 | && vim_strchr(p_cpo, CPO_FNAMER) != NULL |
| 212 | && !(flags & READ_DUMMY)) |
| 213 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 214 | if (set_rw_fname(fname, sfname) == FAIL) |
| 215 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 218 | /* Remember the initial values of curbuf, curbuf->b_ffname and |
| 219 | * curbuf->b_fname to detect whether they are altered as a result of |
| 220 | * executing nasty autocommands. Also check if "fname" and "sfname" |
| 221 | * point to one of these values. */ |
| 222 | old_curbuf = curbuf; |
| 223 | old_b_ffname = curbuf->b_ffname; |
| 224 | old_b_fname = curbuf->b_fname; |
| 225 | using_b_ffname = (fname == curbuf->b_ffname) |
| 226 | || (sfname == curbuf->b_ffname); |
| 227 | using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 228 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 229 | /* After reading a file the cursor line changes but we don't want to |
| 230 | * display the line. */ |
| 231 | ex_no_reprint = TRUE; |
| 232 | |
Bram Moolenaar | 55b7cf8 | 2006-09-09 12:52:42 +0000 | [diff] [blame] | 233 | /* don't display the file info for another buffer now */ |
| 234 | need_fileinfo = FALSE; |
| 235 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 236 | /* |
| 237 | * For Unix: Use the short file name whenever possible. |
| 238 | * Avoids problems with networks and when directory names are changed. |
| 239 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 240 | * another directory, which we don't detect. |
| 241 | */ |
| 242 | if (sfname == NULL) |
| 243 | sfname = fname; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 244 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | fname = sfname; |
| 246 | #endif |
| 247 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 248 | /* |
| 249 | * The BufReadCmd and FileReadCmd events intercept the reading process by |
| 250 | * executing the associated commands instead. |
| 251 | */ |
| 252 | if (!filtering && !read_stdin && !read_buffer) |
| 253 | { |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 254 | orig_start = curbuf->b_op_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 255 | |
| 256 | /* Set '[ mark to the line above where the lines go (line 1 if zero). */ |
| 257 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 258 | curbuf->b_op_start.col = 0; |
| 259 | |
| 260 | if (newfile) |
| 261 | { |
| 262 | if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, |
| 263 | FALSE, curbuf, eap)) |
| 264 | #ifdef FEAT_EVAL |
| 265 | return aborting() ? FAIL : OK; |
| 266 | #else |
| 267 | return OK; |
| 268 | #endif |
| 269 | } |
| 270 | else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, |
| 271 | FALSE, NULL, eap)) |
| 272 | #ifdef FEAT_EVAL |
| 273 | return aborting() ? FAIL : OK; |
| 274 | #else |
| 275 | return OK; |
| 276 | #endif |
| 277 | |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 278 | curbuf->b_op_start = orig_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 279 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 280 | |
| 281 | if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) |
| 282 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 283 | else |
| 284 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 285 | |
| 286 | /* |
| 287 | * If the name ends in a path separator, we can't open it. Check here, |
| 288 | * because reading the file may actually work, but then creating the swap |
| 289 | * file may destroy it! Reported on MS-DOS and Win 95. |
| 290 | * If the name is too long we might crash further on, quit here. |
| 291 | */ |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 292 | if (fname != NULL && *fname != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 293 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 294 | p = fname + STRLEN(fname); |
| 295 | if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 296 | { |
| 297 | filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); |
| 298 | msg_end(); |
| 299 | msg_scroll = msg_save; |
| 300 | return FAIL; |
| 301 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 304 | if (!read_stdin && !read_buffer && !read_fifo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 305 | { |
Bram Moolenaar | 4e4f529 | 2013-08-30 17:07:01 +0200 | [diff] [blame] | 306 | #ifdef UNIX |
| 307 | /* |
| 308 | * On Unix it is possible to read a directory, so we have to |
| 309 | * check for it before the mch_open(). |
| 310 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 311 | perm = mch_getperm(fname); |
| 312 | if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 313 | && !S_ISFIFO(perm) /* ... or fifo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | && !S_ISSOCK(perm) /* ... or socket */ |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 315 | # ifdef OPEN_CHR_FILES |
| 316 | && !(S_ISCHR(perm) && is_dev_fd_file(fname)) |
| 317 | /* ... or a character special file named /dev/fd/<n> */ |
| 318 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | ) |
| 320 | { |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 321 | int retval = FAIL; |
| 322 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | if (S_ISDIR(perm)) |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 324 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 325 | filemess(curbuf, fname, (char_u *)_("is a directory"), 0); |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 326 | retval = NOTDONE; |
| 327 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | else |
| 329 | filemess(curbuf, fname, (char_u *)_("is not a file"), 0); |
| 330 | msg_end(); |
| 331 | msg_scroll = msg_save; |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 332 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | } |
Bram Moolenaar | 4e4f529 | 2013-08-30 17:07:01 +0200 | [diff] [blame] | 334 | #endif |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 335 | #if defined(MSWIN) |
Bram Moolenaar | c67764a | 2006-10-12 19:14:26 +0000 | [diff] [blame] | 336 | /* |
| 337 | * MS-Windows allows opening a device, but we will probably get stuck |
| 338 | * trying to read it. |
| 339 | */ |
| 340 | if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) |
| 341 | { |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 342 | filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
Bram Moolenaar | c67764a | 2006-10-12 19:14:26 +0000 | [diff] [blame] | 343 | msg_end(); |
| 344 | msg_scroll = msg_save; |
| 345 | return FAIL; |
| 346 | } |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 347 | #endif |
Bram Moolenaar | 4e4f529 | 2013-08-30 17:07:01 +0200 | [diff] [blame] | 348 | } |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 349 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 350 | /* Set default or forced 'fileformat' and 'binary'. */ |
| 351 | set_file_options(set_options, eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * When opening a new file we take the readonly flag from the file. |
| 355 | * Default is r/w, can be set to r/o below. |
| 356 | * Don't reset it when in readonly mode |
| 357 | * Only set/reset b_p_ro when BF_CHECK_RO is set. |
| 358 | */ |
| 359 | check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 360 | if (check_readonly && !readonlymode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 361 | curbuf->b_p_ro = FALSE; |
| 362 | |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 363 | if (newfile && !read_stdin && !read_buffer && !read_fifo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 364 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 365 | /* Remember time of file. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 366 | if (mch_stat((char *)fname, &st) >= 0) |
| 367 | { |
| 368 | buf_store_time(curbuf, &st, fname); |
| 369 | curbuf->b_mtime_read = curbuf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 370 | #ifdef UNIX |
| 371 | /* |
| 372 | * Use the protection bits of the original file for the swap file. |
| 373 | * This makes it possible for others to read the name of the |
| 374 | * edited file from the swapfile, but only if they can read the |
| 375 | * edited file. |
| 376 | * Remove the "write" and "execute" bits for group and others |
| 377 | * (they must not write the swapfile). |
| 378 | * Add the "read" and "write" bits for the user, otherwise we may |
| 379 | * not be able to write to the file ourselves. |
| 380 | * Setting the bits is done below, after creating the swap file. |
| 381 | */ |
| 382 | swap_mode = (st.st_mode & 0644) | 0600; |
| 383 | #endif |
| 384 | #ifdef FEAT_CW_EDITOR |
| 385 | /* Get the FSSpec on MacOS |
| 386 | * TODO: Update it properly when the buffer name changes |
| 387 | */ |
| 388 | (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); |
| 389 | #endif |
| 390 | #ifdef VMS |
| 391 | curbuf->b_fab_rfm = st.st_fab_rfm; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 392 | curbuf->b_fab_rat = st.st_fab_rat; |
| 393 | curbuf->b_fab_mrs = st.st_fab_mrs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 394 | #endif |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | curbuf->b_mtime = 0; |
| 399 | curbuf->b_mtime_read = 0; |
| 400 | curbuf->b_orig_size = 0; |
| 401 | curbuf->b_orig_mode = 0; |
| 402 | } |
| 403 | |
| 404 | /* Reset the "new file" flag. It will be set again below when the |
| 405 | * file doesn't exist. */ |
| 406 | curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * for UNIX: check readonly with perm and mch_access() |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 411 | * for Amiga: check readonly by trying to open the file for writing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 412 | */ |
| 413 | file_readonly = FALSE; |
| 414 | if (read_stdin) |
| 415 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 416 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 417 | /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
| 418 | setmode(0, O_BINARY); |
| 419 | #endif |
| 420 | } |
| 421 | else if (!read_buffer) |
| 422 | { |
| 423 | #ifdef USE_MCH_ACCESS |
| 424 | if ( |
| 425 | # ifdef UNIX |
| 426 | !(perm & 0222) || |
| 427 | # endif |
| 428 | mch_access((char *)fname, W_OK)) |
| 429 | file_readonly = TRUE; |
| 430 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 431 | #else |
| 432 | if (!newfile |
| 433 | || readonlymode |
| 434 | || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) |
| 435 | { |
| 436 | file_readonly = TRUE; |
| 437 | /* try to open ro */ |
| 438 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 439 | } |
| 440 | #endif |
| 441 | } |
| 442 | |
| 443 | if (fd < 0) /* cannot open at all */ |
| 444 | { |
| 445 | #ifndef UNIX |
| 446 | int isdir_f; |
| 447 | #endif |
| 448 | msg_scroll = msg_save; |
| 449 | #ifndef UNIX |
| 450 | /* |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 451 | * On Amiga we can't open a directory, check here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 452 | */ |
| 453 | isdir_f = (mch_isdir(fname)); |
| 454 | perm = mch_getperm(fname); /* check if the file exists */ |
| 455 | if (isdir_f) |
| 456 | { |
| 457 | filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); |
| 458 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 459 | } |
| 460 | else |
| 461 | #endif |
| 462 | if (newfile) |
| 463 | { |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 464 | if (perm < 0 |
| 465 | #ifdef ENOENT |
| 466 | && errno == ENOENT |
| 467 | #endif |
| 468 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 469 | { |
| 470 | /* |
| 471 | * Set the 'new-file' flag, so that when the file has |
| 472 | * been created by someone else, a ":w" will complain. |
| 473 | */ |
| 474 | curbuf->b_flags |= BF_NEW; |
| 475 | |
| 476 | /* Create a swap file now, so that other Vims are warned |
| 477 | * that we are editing this file. Don't do this for a |
| 478 | * "nofile" or "nowrite" buffer type. */ |
| 479 | #ifdef FEAT_QUICKFIX |
| 480 | if (!bt_dontwrite(curbuf)) |
| 481 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 482 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 483 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 484 | /* SwapExists autocommand may mess things up */ |
| 485 | if (curbuf != old_curbuf |
| 486 | || (using_b_ffname |
| 487 | && (old_b_ffname != curbuf->b_ffname)) |
| 488 | || (using_b_fname |
| 489 | && (old_b_fname != curbuf->b_fname))) |
| 490 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 491 | emsg(_(e_auchangedbuf)); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 492 | return FAIL; |
| 493 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 494 | } |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 495 | if (dir_of_file_exists(fname)) |
| 496 | filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); |
| 497 | else |
| 498 | filemess(curbuf, sfname, |
| 499 | (char_u *)_("[New DIRECTORY]"), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 500 | #ifdef FEAT_VIMINFO |
| 501 | /* Even though this is a new file, it might have been |
| 502 | * edited before and deleted. Get the old marks. */ |
| 503 | check_marks_read(); |
| 504 | #endif |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 505 | /* Set forced 'fileencoding'. */ |
| 506 | if (eap != NULL) |
| 507 | set_forced_fenc(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, |
| 509 | FALSE, curbuf, eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | /* remember the current fileformat */ |
| 511 | save_file_ff(curbuf); |
| 512 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 513 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 514 | if (aborting()) /* autocmds may abort script processing */ |
| 515 | return FAIL; |
| 516 | #endif |
| 517 | return OK; /* a new file is not an error */ |
| 518 | } |
| 519 | else |
| 520 | { |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 521 | filemess(curbuf, sfname, (char_u *)( |
| 522 | # ifdef EFBIG |
| 523 | (errno == EFBIG) ? _("[File too big]") : |
| 524 | # endif |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 525 | # ifdef EOVERFLOW |
| 526 | (errno == EOVERFLOW) ? _("[File too big]") : |
| 527 | # endif |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 528 | _("[Permission Denied]")), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 529 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return FAIL; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Only set the 'ro' flag for readonly files the first time they are |
| 538 | * loaded. Help files always get readonly mode |
| 539 | */ |
| 540 | if ((check_readonly && file_readonly) || curbuf->b_help) |
| 541 | curbuf->b_p_ro = TRUE; |
| 542 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 543 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 544 | { |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 545 | /* Don't change 'eol' if reading from buffer as it will already be |
| 546 | * correctly set when reading stdin. */ |
| 547 | if (!read_buffer) |
| 548 | { |
| 549 | curbuf->b_p_eol = TRUE; |
| 550 | curbuf->b_start_eol = TRUE; |
| 551 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 552 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 553 | curbuf->b_start_bomb = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Create a swap file now, so that other Vims are warned that we are |
| 557 | * editing this file. |
| 558 | * Don't do this for a "nofile" or "nowrite" buffer type. */ |
| 559 | #ifdef FEAT_QUICKFIX |
| 560 | if (!bt_dontwrite(curbuf)) |
| 561 | #endif |
| 562 | { |
| 563 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 564 | if (!read_stdin && (curbuf != old_curbuf |
| 565 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 566 | || (using_b_fname && (old_b_fname != curbuf->b_fname)))) |
| 567 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 568 | emsg(_(e_auchangedbuf)); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 569 | if (!read_buffer) |
| 570 | close(fd); |
| 571 | return FAIL; |
| 572 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 573 | #ifdef UNIX |
| 574 | /* Set swap file protection bits after creating it. */ |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 575 | if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
| 576 | && curbuf->b_ml.ml_mfp->mf_fname != NULL) |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 577 | { |
| 578 | char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname; |
| 579 | |
| 580 | /* |
| 581 | * If the group-read bit is set but not the world-read bit, then |
| 582 | * the group must be equal to the group of the original file. If |
| 583 | * we can't make that happen then reset the group-read bit. This |
| 584 | * avoids making the swap file readable to more users when the |
| 585 | * primary group of the user is too permissive. |
| 586 | */ |
| 587 | if ((swap_mode & 044) == 040) |
| 588 | { |
| 589 | stat_T swap_st; |
| 590 | |
| 591 | if (mch_stat((char *)swap_fname, &swap_st) >= 0 |
| 592 | && st.st_gid != swap_st.st_gid |
Bram Moolenaar | 02e802b | 2018-04-19 21:15:27 +0200 | [diff] [blame] | 593 | # ifdef HAVE_FCHOWN |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 594 | && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid) |
Bram Moolenaar | 02e802b | 2018-04-19 21:15:27 +0200 | [diff] [blame] | 595 | == -1 |
Bram Moolenaar | 1f131ae | 2018-05-21 13:39:40 +0200 | [diff] [blame] | 596 | # endif |
Bram Moolenaar | 02e802b | 2018-04-19 21:15:27 +0200 | [diff] [blame] | 597 | ) |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 598 | swap_mode &= 0600; |
| 599 | } |
| 600 | |
| 601 | (void)mch_setperm(swap_fname, (long)swap_mode); |
| 602 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 603 | #endif |
| 604 | } |
| 605 | |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 606 | // If "Quit" selected at ATTENTION dialog, don't load the file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 607 | if (swap_exists_action == SEA_QUIT) |
| 608 | { |
| 609 | if (!read_buffer && !read_stdin) |
| 610 | close(fd); |
| 611 | return FAIL; |
| 612 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 613 | |
| 614 | ++no_wait_return; /* don't wait for return yet */ |
| 615 | |
| 616 | /* |
| 617 | * Set '[ mark to the line above where the lines go (line 1 if zero). |
| 618 | */ |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 619 | orig_start = curbuf->b_op_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 620 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 621 | curbuf->b_op_start.col = 0; |
| 622 | |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 623 | try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
| 624 | try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
| 625 | try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
| 626 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 627 | if (!read_buffer) |
| 628 | { |
| 629 | int m = msg_scroll; |
| 630 | int n = msg_scrolled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 631 | |
| 632 | /* |
| 633 | * The file must be closed again, the autocommands may want to change |
| 634 | * the file before reading it. |
| 635 | */ |
| 636 | if (!read_stdin) |
| 637 | close(fd); /* ignore errors */ |
| 638 | |
| 639 | /* |
| 640 | * The output from the autocommands should not overwrite anything and |
| 641 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 642 | * output was done. |
| 643 | */ |
| 644 | msg_scroll = TRUE; |
| 645 | if (filtering) |
| 646 | apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, |
| 647 | FALSE, curbuf, eap); |
| 648 | else if (read_stdin) |
| 649 | apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, |
| 650 | FALSE, curbuf, eap); |
| 651 | else if (newfile) |
| 652 | apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, |
| 653 | FALSE, curbuf, eap); |
| 654 | else |
| 655 | apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, |
| 656 | FALSE, NULL, eap); |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 657 | /* autocommands may have changed it */ |
| 658 | try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
| 659 | try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
| 660 | try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 661 | curbuf->b_op_start = orig_start; |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 662 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | if (msg_scrolled == n) |
| 664 | msg_scroll = m; |
| 665 | |
| 666 | #ifdef FEAT_EVAL |
| 667 | if (aborting()) /* autocmds may abort script processing */ |
| 668 | { |
| 669 | --no_wait_return; |
| 670 | msg_scroll = msg_save; |
| 671 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 672 | return FAIL; |
| 673 | } |
| 674 | #endif |
| 675 | /* |
| 676 | * Don't allow the autocommands to change the current buffer. |
| 677 | * Try to re-open the file. |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 678 | * |
| 679 | * Don't allow the autocommands to change the buffer name either |
| 680 | * (cd for example) if it invalidates fname or sfname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 681 | */ |
| 682 | if (!read_stdin && (curbuf != old_curbuf |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 683 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 684 | || (using_b_fname && (old_b_fname != curbuf->b_fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
| 686 | { |
| 687 | --no_wait_return; |
| 688 | msg_scroll = msg_save; |
| 689 | if (fd < 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 690 | emsg(_("E200: *ReadPre autocommands made the file unreadable")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 692 | emsg(_("E201: *ReadPre autocommands must not change current buffer")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 694 | return FAIL; |
| 695 | } |
| 696 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 697 | |
| 698 | /* Autocommands may add lines to the file, need to check if it is empty */ |
| 699 | wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
| 700 | |
| 701 | if (!recoverymode && !filtering && !(flags & READ_DUMMY)) |
| 702 | { |
| 703 | /* |
| 704 | * Show the user that we are busy reading the input. Sometimes this |
| 705 | * may take a while. When reading from stdin another program may |
| 706 | * still be running, don't move the cursor to the last line, unless |
| 707 | * always using the GUI. |
| 708 | */ |
| 709 | if (read_stdin) |
| 710 | { |
Bram Moolenaar | 234d162 | 2017-11-18 14:55:23 +0100 | [diff] [blame] | 711 | if (!is_not_a_term()) |
| 712 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 713 | #ifndef ALWAYS_USE_GUI |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 714 | # ifdef VIMDLL |
| 715 | if (!gui.in_use) |
| 716 | # endif |
| 717 | mch_msg(_("Vim: Reading from stdin...\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 718 | #endif |
| 719 | #ifdef FEAT_GUI |
Bram Moolenaar | 234d162 | 2017-11-18 14:55:23 +0100 | [diff] [blame] | 720 | /* Also write a message in the GUI window, if there is one. */ |
| 721 | if (gui.in_use && !gui.dying && !gui.starting) |
| 722 | { |
| 723 | p = (char_u *)_("Reading from stdin..."); |
| 724 | gui_write(p, (int)STRLEN(p)); |
| 725 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 726 | #endif |
Bram Moolenaar | 234d162 | 2017-11-18 14:55:23 +0100 | [diff] [blame] | 727 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | } |
| 729 | else if (!read_buffer) |
| 730 | filemess(curbuf, sfname, (char_u *)"", 0); |
| 731 | } |
| 732 | |
| 733 | msg_scroll = FALSE; /* overwrite the file message */ |
| 734 | |
| 735 | /* |
| 736 | * Set linecnt now, before the "retry" caused by a wrong guess for |
| 737 | * fileformat, and after the autocommands, which may change them. |
| 738 | */ |
| 739 | linecnt = curbuf->b_ml.ml_line_count; |
| 740 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 741 | /* "++bad=" argument. */ |
| 742 | if (eap != NULL && eap->bad_char != 0) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 743 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 744 | bad_char_behavior = eap->bad_char; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 745 | if (set_options) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 746 | curbuf->b_bad_char = eap->bad_char; |
| 747 | } |
| 748 | else |
| 749 | curbuf->b_bad_char = 0; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 750 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 751 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 752 | * Decide which 'encoding' to use or use first. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 753 | */ |
| 754 | if (eap != NULL && eap->force_enc != 0) |
| 755 | { |
| 756 | fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 757 | fenc_alloced = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 758 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | } |
| 760 | else if (curbuf->b_p_bin) |
| 761 | { |
| 762 | fenc = (char_u *)""; /* binary: don't convert */ |
| 763 | fenc_alloced = FALSE; |
| 764 | } |
| 765 | else if (curbuf->b_help) |
| 766 | { |
| 767 | char_u firstline[80]; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 768 | int fc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 769 | |
| 770 | /* Help files are either utf-8 or latin1. Try utf-8 first, if this |
| 771 | * fails it must be latin1. |
| 772 | * Always do this when 'encoding' is "utf-8". Otherwise only do |
| 773 | * this when needed to avoid [converted] remarks all the time. |
| 774 | * It is needed when the first line contains non-ASCII characters. |
| 775 | * That is only in *.??x files. */ |
| 776 | fenc = (char_u *)"latin1"; |
| 777 | c = enc_utf8; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 778 | if (!c && !read_stdin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 780 | fc = fname[STRLEN(fname) - 1]; |
| 781 | if (TOLOWER_ASC(fc) == 'x') |
| 782 | { |
| 783 | /* Read the first line (and a bit more). Immediately rewind to |
| 784 | * the start of the file. If the read() fails "len" is -1. */ |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 785 | len = read_eintr(fd, firstline, 80); |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 786 | vim_lseek(fd, (off_T)0L, SEEK_SET); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 787 | for (p = firstline; p < firstline + len; ++p) |
| 788 | if (*p >= 0x80) |
| 789 | { |
| 790 | c = TRUE; |
| 791 | break; |
| 792 | } |
| 793 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | if (c) |
| 797 | { |
| 798 | fenc_next = fenc; |
| 799 | fenc = (char_u *)"utf-8"; |
| 800 | |
| 801 | /* When the file is utf-8 but a character doesn't fit in |
| 802 | * 'encoding' don't retry. In help text editing utf-8 bytes |
| 803 | * doesn't make sense. */ |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 804 | if (!enc_utf8) |
| 805 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 806 | } |
| 807 | fenc_alloced = FALSE; |
| 808 | } |
| 809 | else if (*p_fencs == NUL) |
| 810 | { |
| 811 | fenc = curbuf->b_p_fenc; /* use format from buffer */ |
| 812 | fenc_alloced = FALSE; |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | fenc_next = p_fencs; /* try items in 'fileencodings' */ |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 817 | fenc = next_fenc(&fenc_next, &fenc_alloced); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 819 | |
| 820 | /* |
| 821 | * Jump back here to retry reading the file in different ways. |
| 822 | * Reasons to retry: |
| 823 | * - encoding conversion failed: try another one from "fenc_next" |
| 824 | * - BOM detected and fenc was set, need to setup conversion |
| 825 | * - "fileformat" check failed: try another |
| 826 | * |
| 827 | * Variables set for special retry actions: |
| 828 | * "file_rewind" Rewind the file to start reading it again. |
| 829 | * "advance_fenc" Advance "fenc" using "fenc_next". |
| 830 | * "skip_read" Re-use already read bytes (BOM detected). |
| 831 | * "did_iconv" iconv() conversion failed, try 'charconvert'. |
| 832 | * "keep_fileformat" Don't reset "fileformat". |
| 833 | * |
| 834 | * Other status indicators: |
| 835 | * "tmpname" When != NULL did conversion with 'charconvert'. |
| 836 | * Output file has to be deleted afterwards. |
| 837 | * "iconv_fd" When != -1 did conversion with iconv(). |
| 838 | */ |
| 839 | retry: |
| 840 | |
| 841 | if (file_rewind) |
| 842 | { |
| 843 | if (read_buffer) |
| 844 | { |
| 845 | read_buf_lnum = 1; |
| 846 | read_buf_col = 0; |
| 847 | } |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 848 | else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 849 | { |
| 850 | /* Can't rewind the file, give up. */ |
| 851 | error = TRUE; |
| 852 | goto failed; |
| 853 | } |
| 854 | /* Delete the previously read lines. */ |
| 855 | while (lnum > from) |
| 856 | ml_delete(lnum--, FALSE); |
| 857 | file_rewind = FALSE; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 858 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 859 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 860 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 861 | curbuf->b_start_bomb = FALSE; |
| 862 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 863 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* |
| 867 | * When retrying with another "fenc" and the first time "fileformat" |
| 868 | * will be reset. |
| 869 | */ |
| 870 | if (keep_fileformat) |
| 871 | keep_fileformat = FALSE; |
| 872 | else |
| 873 | { |
| 874 | if (eap != NULL && eap->force_ff != 0) |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 875 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | fileformat = get_fileformat_force(curbuf, eap); |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 877 | try_unix = try_dos = try_mac = FALSE; |
| 878 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | else if (curbuf->b_p_bin) |
| 880 | fileformat = EOL_UNIX; /* binary: use Unix format */ |
| 881 | else if (*p_ffs == NUL) |
| 882 | fileformat = get_fileformat(curbuf);/* use format from buffer */ |
| 883 | else |
| 884 | fileformat = EOL_UNKNOWN; /* detect from file */ |
| 885 | } |
| 886 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 887 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 888 | if (iconv_fd != (iconv_t)-1) |
| 889 | { |
| 890 | /* aborted conversion with iconv(), close the descriptor */ |
| 891 | iconv_close(iconv_fd); |
| 892 | iconv_fd = (iconv_t)-1; |
| 893 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 894 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 895 | |
| 896 | if (advance_fenc) |
| 897 | { |
| 898 | /* |
| 899 | * Try the next entry in 'fileencodings'. |
| 900 | */ |
| 901 | advance_fenc = FALSE; |
| 902 | |
| 903 | if (eap != NULL && eap->force_enc != 0) |
| 904 | { |
| 905 | /* Conversion given with "++cc=" wasn't possible, read |
| 906 | * without conversion. */ |
| 907 | notconverted = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 908 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | if (fenc_alloced) |
| 910 | vim_free(fenc); |
| 911 | fenc = (char_u *)""; |
| 912 | fenc_alloced = FALSE; |
| 913 | } |
| 914 | else |
| 915 | { |
| 916 | if (fenc_alloced) |
| 917 | vim_free(fenc); |
| 918 | if (fenc_next != NULL) |
| 919 | { |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 920 | fenc = next_fenc(&fenc_next, &fenc_alloced); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | } |
| 922 | else |
| 923 | { |
| 924 | fenc = (char_u *)""; |
| 925 | fenc_alloced = FALSE; |
| 926 | } |
| 927 | } |
| 928 | if (tmpname != NULL) |
| 929 | { |
| 930 | mch_remove(tmpname); /* delete converted file */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 931 | VIM_CLEAR(tmpname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 936 | * Conversion may be required when the encoding of the file is different |
| 937 | * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 938 | */ |
| 939 | fio_flags = 0; |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 940 | converted = need_conversion(fenc); |
| 941 | if (converted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | { |
| 943 | |
| 944 | /* "ucs-bom" means we need to check the first bytes of the file |
| 945 | * for a BOM. */ |
| 946 | if (STRCMP(fenc, ENC_UCSBOM) == 0) |
| 947 | fio_flags = FIO_UCSBOM; |
| 948 | |
| 949 | /* |
| 950 | * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be |
| 951 | * done. This is handled below after read(). Prepare the |
| 952 | * fio_flags to avoid having to parse the string each time. |
| 953 | * Also check for Unicode to Latin1 conversion, because iconv() |
| 954 | * appears not to handle this correctly. This works just like |
| 955 | * conversion to UTF-8 except how the resulting character is put in |
| 956 | * the buffer. |
| 957 | */ |
| 958 | else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 959 | fio_flags = get_fio_flags(fenc); |
| 960 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 961 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 962 | /* |
| 963 | * Conversion from an MS-Windows codepage to UTF-8 or another codepage |
| 964 | * is handled with MultiByteToWideChar(). |
| 965 | */ |
| 966 | if (fio_flags == 0) |
| 967 | fio_flags = get_win_fio_flags(fenc); |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 968 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 969 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 970 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
| 972 | if (fio_flags == 0) |
| 973 | fio_flags = get_mac_fio_flags(fenc); |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 974 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 975 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 976 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 977 | /* |
| 978 | * Try using iconv() if we can't convert internally. |
| 979 | */ |
| 980 | if (fio_flags == 0 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 981 | # ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 982 | && !did_iconv |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 983 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 984 | ) |
| 985 | iconv_fd = (iconv_t)my_iconv_open( |
| 986 | enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 987 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 989 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 990 | /* |
| 991 | * Use the 'charconvert' expression when conversion is required |
| 992 | * and we can't do it internally or with iconv(). |
| 993 | */ |
| 994 | if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 995 | && !read_fifo |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 996 | # ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | && iconv_fd == (iconv_t)-1 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 998 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | ) |
| 1000 | { |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1001 | # ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | did_iconv = FALSE; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1003 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1004 | /* Skip conversion when it's already done (retry for wrong |
| 1005 | * "fileformat"). */ |
| 1006 | if (tmpname == NULL) |
| 1007 | { |
| 1008 | tmpname = readfile_charconvert(fname, fenc, &fd); |
| 1009 | if (tmpname == NULL) |
| 1010 | { |
| 1011 | /* Conversion failed. Try another one. */ |
| 1012 | advance_fenc = TRUE; |
| 1013 | if (fd < 0) |
| 1014 | { |
| 1015 | /* Re-opening the original file failed! */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1016 | emsg(_("E202: Conversion made file unreadable!")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1017 | error = TRUE; |
| 1018 | goto failed; |
| 1019 | } |
| 1020 | goto retry; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | else |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1025 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | { |
| 1027 | if (fio_flags == 0 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1028 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1029 | && iconv_fd == (iconv_t)-1 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1030 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | ) |
| 1032 | { |
| 1033 | /* Conversion wanted but we can't. |
| 1034 | * Try the next conversion in 'fileencodings' */ |
| 1035 | advance_fenc = TRUE; |
| 1036 | goto retry; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1041 | /* Set "can_retry" when it's possible to rewind the file and try with |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1042 | * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1043 | * stdin or fixed at a specific encoding. */ |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 1044 | can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (!skip_read) |
| 1047 | { |
| 1048 | linerest = 0; |
| 1049 | filesize = 0; |
| 1050 | skip_count = lines_to_skip; |
| 1051 | read_count = lines_to_read; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1052 | conv_restlen = 0; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1053 | #ifdef FEAT_PERSISTENT_UNDO |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 1054 | read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0 |
| 1055 | && curbuf->b_ffname != NULL |
| 1056 | && curbuf->b_p_udf |
| 1057 | && !filtering |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 1058 | && !read_fifo |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 1059 | && !read_stdin |
| 1060 | && !read_buffer); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1061 | if (read_undo_file) |
| 1062 | sha256_start(&sha_ctx); |
| 1063 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1064 | #ifdef FEAT_CRYPT |
| 1065 | if (curbuf->b_cryptstate != NULL) |
| 1066 | { |
| 1067 | /* Need to free the state, but keep the key, don't want to ask for |
| 1068 | * it again. */ |
| 1069 | crypt_free_state(curbuf->b_cryptstate); |
| 1070 | curbuf->b_cryptstate = NULL; |
| 1071 | } |
| 1072 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | while (!error && !got_int) |
| 1076 | { |
| 1077 | /* |
| 1078 | * We allocate as much space for the file as we can get, plus |
| 1079 | * space for the old line plus room for one terminating NUL. |
| 1080 | * The amount is limited by the fact that read() only can read |
| 1081 | * upto max_unsigned characters (and other things). |
| 1082 | */ |
Bram Moolenaar | 13d3b05 | 2018-04-29 13:34:47 +0200 | [diff] [blame] | 1083 | if (!skip_read) |
| 1084 | { |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 1085 | #if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
Bram Moolenaar | 13d3b05 | 2018-04-29 13:34:47 +0200 | [diff] [blame] | 1086 | size = SSIZE_MAX; /* use max I/O size, 52K */ |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 1087 | #else |
Bram Moolenaar | 13d3b05 | 2018-04-29 13:34:47 +0200 | [diff] [blame] | 1088 | /* Use buffer >= 64K. Add linerest to double the size if the |
| 1089 | * line gets very long, to avoid a lot of copying. But don't |
| 1090 | * read more than 1 Mbyte at a time, so we can be interrupted. |
| 1091 | */ |
| 1092 | size = 0x10000L + linerest; |
| 1093 | if (size > 0x100000L) |
| 1094 | size = 0x100000L; |
Bram Moolenaar | 13d3b05 | 2018-04-29 13:34:47 +0200 | [diff] [blame] | 1095 | #endif |
| 1096 | } |
| 1097 | |
| 1098 | /* Protect against the argument of lalloc() going negative. */ |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 1099 | if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | { |
| 1101 | ++split; |
| 1102 | *ptr = NL; /* split line by inserting a NL */ |
| 1103 | size = 1; |
| 1104 | } |
| 1105 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1106 | { |
| 1107 | if (!skip_read) |
| 1108 | { |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1109 | for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | { |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 1111 | if ((new_buffer = lalloc(size + linerest + 1, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1112 | FALSE)) != NULL) |
| 1113 | break; |
| 1114 | } |
| 1115 | if (new_buffer == NULL) |
| 1116 | { |
| 1117 | do_outofmem_msg((long_u)(size * 2 + linerest + 1)); |
| 1118 | error = TRUE; |
| 1119 | break; |
| 1120 | } |
| 1121 | if (linerest) /* copy characters from the previous buffer */ |
| 1122 | mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); |
| 1123 | vim_free(buffer); |
| 1124 | buffer = new_buffer; |
| 1125 | ptr = buffer + linerest; |
| 1126 | line_start = buffer; |
| 1127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1128 | /* May need room to translate into. |
| 1129 | * For iconv() we don't really know the required space, use a |
| 1130 | * factor ICONV_MULT. |
| 1131 | * latin1 to utf-8: 1 byte becomes up to 2 bytes |
| 1132 | * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes |
| 1133 | * become up to 4 bytes, size must be multiple of 2 |
| 1134 | * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be |
| 1135 | * multiple of 2 |
| 1136 | * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be |
| 1137 | * multiple of 4 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1138 | real_size = (int)size; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1139 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | if (iconv_fd != (iconv_t)-1) |
| 1141 | size = size / ICONV_MULT; |
| 1142 | else |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1143 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1144 | if (fio_flags & FIO_LATIN1) |
| 1145 | size = size / 2; |
| 1146 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1147 | size = (size * 2 / 3) & ~1; |
| 1148 | else if (fio_flags & FIO_UCS4) |
| 1149 | size = (size * 2 / 3) & ~3; |
| 1150 | else if (fio_flags == FIO_UCSBOM) |
| 1151 | size = size / ICONV_MULT; /* worst case */ |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1152 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | else if (fio_flags & FIO_CODEPAGE) |
| 1154 | size = size / ICONV_MULT; /* also worst case */ |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1155 | #endif |
| 1156 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1157 | else if (fio_flags & FIO_MACROMAN) |
| 1158 | size = size / ICONV_MULT; /* also worst case */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1159 | #endif |
| 1160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | if (conv_restlen > 0) |
| 1162 | { |
| 1163 | /* Insert unconverted bytes from previous line. */ |
| 1164 | mch_memmove(ptr, conv_rest, conv_restlen); |
| 1165 | ptr += conv_restlen; |
| 1166 | size -= conv_restlen; |
| 1167 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1168 | |
| 1169 | if (read_buffer) |
| 1170 | { |
| 1171 | /* |
| 1172 | * Read bytes from curbuf. Used for converting text read |
| 1173 | * from stdin. |
| 1174 | */ |
| 1175 | if (read_buf_lnum > from) |
| 1176 | size = 0; |
| 1177 | else |
| 1178 | { |
| 1179 | int n, ni; |
| 1180 | long tlen; |
| 1181 | |
| 1182 | tlen = 0; |
| 1183 | for (;;) |
| 1184 | { |
| 1185 | p = ml_get(read_buf_lnum) + read_buf_col; |
| 1186 | n = (int)STRLEN(p); |
| 1187 | if ((int)tlen + n + 1 > size) |
| 1188 | { |
| 1189 | /* Filled up to "size", append partial line. |
| 1190 | * Change NL to NUL to reverse the effect done |
| 1191 | * below. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1192 | n = (int)(size - tlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1193 | for (ni = 0; ni < n; ++ni) |
| 1194 | { |
| 1195 | if (p[ni] == NL) |
| 1196 | ptr[tlen++] = NUL; |
| 1197 | else |
| 1198 | ptr[tlen++] = p[ni]; |
| 1199 | } |
| 1200 | read_buf_col += n; |
| 1201 | break; |
| 1202 | } |
| 1203 | else |
| 1204 | { |
| 1205 | /* Append whole line and new-line. Change NL |
| 1206 | * to NUL to reverse the effect done below. */ |
| 1207 | for (ni = 0; ni < n; ++ni) |
| 1208 | { |
| 1209 | if (p[ni] == NL) |
| 1210 | ptr[tlen++] = NUL; |
| 1211 | else |
| 1212 | ptr[tlen++] = p[ni]; |
| 1213 | } |
| 1214 | ptr[tlen++] = NL; |
| 1215 | read_buf_col = 0; |
| 1216 | if (++read_buf_lnum > from) |
| 1217 | { |
| 1218 | /* When the last line didn't have an |
| 1219 | * end-of-line don't add it now either. */ |
| 1220 | if (!curbuf->b_p_eol) |
| 1221 | --tlen; |
| 1222 | size = tlen; |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | /* |
| 1232 | * Read bytes from the file. |
| 1233 | */ |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 1234 | size = read_eintr(fd, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1237 | #ifdef FEAT_CRYPT |
| 1238 | /* |
| 1239 | * At start of file: Check for magic number of encryption. |
| 1240 | */ |
| 1241 | if (filesize == 0 && size > 0) |
| 1242 | cryptkey = check_for_cryptkey(cryptkey, ptr, &size, |
| 1243 | &filesize, newfile, sfname, |
| 1244 | &did_ask_for_key); |
| 1245 | /* |
| 1246 | * Decrypt the read bytes. This is done before checking for |
| 1247 | * EOF because the crypt layer may be buffering. |
| 1248 | */ |
Bram Moolenaar | 829aa64 | 2017-08-23 22:32:35 +0200 | [diff] [blame] | 1249 | if (cryptkey != NULL && curbuf->b_cryptstate != NULL |
| 1250 | && size > 0) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1251 | { |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 1252 | # ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1253 | if (crypt_works_inplace(curbuf->b_cryptstate)) |
| 1254 | { |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 1255 | # endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1256 | crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 1257 | # ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1258 | } |
| 1259 | else |
| 1260 | { |
| 1261 | char_u *newptr = NULL; |
| 1262 | int decrypted_size; |
| 1263 | |
| 1264 | decrypted_size = crypt_decode_alloc( |
| 1265 | curbuf->b_cryptstate, ptr, size, &newptr); |
| 1266 | |
| 1267 | /* If the crypt layer is buffering, not producing |
| 1268 | * anything yet, need to read more. */ |
Bram Moolenaar | 1c17ffa | 2018-04-24 15:19:04 +0200 | [diff] [blame] | 1269 | if (decrypted_size == 0) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1270 | continue; |
| 1271 | |
| 1272 | if (linerest == 0) |
| 1273 | { |
| 1274 | /* Simple case: reuse returned buffer (may be |
| 1275 | * NULL, checked later). */ |
| 1276 | new_buffer = newptr; |
| 1277 | } |
| 1278 | else |
| 1279 | { |
| 1280 | long_u new_size; |
| 1281 | |
| 1282 | /* Need new buffer to add bytes carried over. */ |
| 1283 | new_size = (long_u)(decrypted_size + linerest + 1); |
| 1284 | new_buffer = lalloc(new_size, FALSE); |
| 1285 | if (new_buffer == NULL) |
| 1286 | { |
| 1287 | do_outofmem_msg(new_size); |
| 1288 | error = TRUE; |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | mch_memmove(new_buffer, buffer, linerest); |
| 1293 | if (newptr != NULL) |
| 1294 | mch_memmove(new_buffer + linerest, newptr, |
| 1295 | decrypted_size); |
| 1296 | } |
| 1297 | |
| 1298 | if (new_buffer != NULL) |
| 1299 | { |
| 1300 | vim_free(buffer); |
| 1301 | buffer = new_buffer; |
| 1302 | new_buffer = NULL; |
| 1303 | line_start = buffer; |
| 1304 | ptr = buffer + linerest; |
| 1305 | } |
| 1306 | size = decrypted_size; |
| 1307 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 1308 | # endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1309 | } |
| 1310 | #endif |
| 1311 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | if (size <= 0) |
| 1313 | { |
| 1314 | if (size < 0) /* read error */ |
| 1315 | error = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1316 | else if (conv_restlen > 0) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1317 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1318 | /* |
| 1319 | * Reached end-of-file but some trailing bytes could |
| 1320 | * not be converted. Truncated file? |
| 1321 | */ |
| 1322 | |
| 1323 | /* When we did a conversion report an error. */ |
| 1324 | if (fio_flags != 0 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1325 | #ifdef USE_ICONV |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1326 | || iconv_fd != (iconv_t)-1 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1327 | #endif |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1328 | ) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1329 | { |
Bram Moolenaar | e8d9530 | 2013-04-24 16:34:02 +0200 | [diff] [blame] | 1330 | if (can_retry) |
| 1331 | goto rewind_retry; |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1332 | if (conv_error == 0) |
| 1333 | conv_error = curbuf->b_ml.ml_line_count |
| 1334 | - linecnt + 1; |
| 1335 | } |
| 1336 | /* Remember the first linenr with an illegal byte */ |
| 1337 | else if (illegal_byte == 0) |
| 1338 | illegal_byte = curbuf->b_ml.ml_line_count |
| 1339 | - linecnt + 1; |
| 1340 | if (bad_char_behavior == BAD_DROP) |
| 1341 | { |
| 1342 | *(ptr - conv_restlen) = NUL; |
| 1343 | conv_restlen = 0; |
| 1344 | } |
| 1345 | else |
| 1346 | { |
| 1347 | /* Replace the trailing bytes with the replacement |
| 1348 | * character if we were converting; if we weren't, |
| 1349 | * leave the UTF8 checking code to do it, as it |
| 1350 | * works slightly differently. */ |
| 1351 | if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1352 | #ifdef USE_ICONV |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1353 | || iconv_fd != (iconv_t)-1 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1354 | #endif |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1355 | )) |
| 1356 | { |
| 1357 | while (conv_restlen > 0) |
| 1358 | { |
| 1359 | *(--ptr) = bad_char_behavior; |
| 1360 | --conv_restlen; |
| 1361 | } |
| 1362 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1363 | fio_flags = 0; /* don't convert this */ |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1364 | #ifdef USE_ICONV |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 1365 | if (iconv_fd != (iconv_t)-1) |
| 1366 | { |
| 1367 | iconv_close(iconv_fd); |
| 1368 | iconv_fd = (iconv_t)-1; |
| 1369 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1370 | #endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1373 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1374 | } |
| 1375 | skip_read = FALSE; |
| 1376 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | /* |
| 1378 | * At start of file (or after crypt magic number): Check for BOM. |
| 1379 | * Also check for a BOM for other Unicode encodings, but not after |
| 1380 | * converting with 'charconvert' or when a BOM has already been |
| 1381 | * found. |
| 1382 | */ |
| 1383 | if ((filesize == 0 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1384 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1385 | || (cryptkey != NULL |
| 1386 | && filesize == crypt_get_header_len( |
| 1387 | crypt_get_method_nr(curbuf))) |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1388 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | ) |
| 1390 | && (fio_flags == FIO_UCSBOM |
| 1391 | || (!curbuf->b_p_bomb |
| 1392 | && tmpname == NULL |
| 1393 | && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) |
| 1394 | { |
| 1395 | char_u *ccname; |
| 1396 | int blen; |
| 1397 | |
| 1398 | /* no BOM detection in a short file or in binary mode */ |
| 1399 | if (size < 2 || curbuf->b_p_bin) |
| 1400 | ccname = NULL; |
| 1401 | else |
| 1402 | ccname = check_for_bom(ptr, size, &blen, |
| 1403 | fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); |
| 1404 | if (ccname != NULL) |
| 1405 | { |
| 1406 | /* Remove BOM from the text */ |
| 1407 | filesize += blen; |
| 1408 | size -= blen; |
| 1409 | mch_memmove(ptr, ptr + blen, (size_t)size); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1410 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1411 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1412 | curbuf->b_p_bomb = TRUE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1413 | curbuf->b_start_bomb = TRUE; |
| 1414 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | if (fio_flags == FIO_UCSBOM) |
| 1418 | { |
| 1419 | if (ccname == NULL) |
| 1420 | { |
| 1421 | /* No BOM detected: retry with next encoding. */ |
| 1422 | advance_fenc = TRUE; |
| 1423 | } |
| 1424 | else |
| 1425 | { |
| 1426 | /* BOM detected: set "fenc" and jump back */ |
| 1427 | if (fenc_alloced) |
| 1428 | vim_free(fenc); |
| 1429 | fenc = ccname; |
| 1430 | fenc_alloced = FALSE; |
| 1431 | } |
| 1432 | /* retry reading without getting new bytes or rewinding */ |
| 1433 | skip_read = TRUE; |
| 1434 | goto retry; |
| 1435 | } |
| 1436 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1437 | |
| 1438 | /* Include not converted bytes. */ |
| 1439 | ptr -= conv_restlen; |
| 1440 | size += conv_restlen; |
| 1441 | conv_restlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1442 | /* |
| 1443 | * Break here for a read error or end-of-file. |
| 1444 | */ |
| 1445 | if (size <= 0) |
| 1446 | break; |
| 1447 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1449 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1450 | if (iconv_fd != (iconv_t)-1) |
| 1451 | { |
| 1452 | /* |
| 1453 | * Attempt conversion of the read bytes to 'encoding' using |
| 1454 | * iconv(). |
| 1455 | */ |
| 1456 | const char *fromp; |
| 1457 | char *top; |
| 1458 | size_t from_size; |
| 1459 | size_t to_size; |
| 1460 | |
| 1461 | fromp = (char *)ptr; |
| 1462 | from_size = size; |
| 1463 | ptr += size; |
| 1464 | top = (char *)ptr; |
| 1465 | to_size = real_size - size; |
| 1466 | |
| 1467 | /* |
| 1468 | * If there is conversion error or not enough room try using |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1469 | * another conversion. Except for when there is no |
| 1470 | * alternative (help files). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1471 | */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1472 | while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
| 1473 | &top, &to_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1474 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 1475 | || from_size > CONV_RESTLEN) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1476 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1477 | if (can_retry) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1478 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1479 | if (conv_error == 0) |
| 1480 | conv_error = readfile_linenr(linecnt, |
| 1481 | ptr, (char_u *)top); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1482 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1483 | /* Deal with a bad byte and continue with the next. */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1484 | ++fromp; |
| 1485 | --from_size; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1486 | if (bad_char_behavior == BAD_KEEP) |
| 1487 | { |
| 1488 | *top++ = *(fromp - 1); |
| 1489 | --to_size; |
| 1490 | } |
| 1491 | else if (bad_char_behavior != BAD_DROP) |
| 1492 | { |
| 1493 | *top++ = bad_char_behavior; |
| 1494 | --to_size; |
| 1495 | } |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1496 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1497 | |
| 1498 | if (from_size > 0) |
| 1499 | { |
| 1500 | /* Some remaining characters, keep them for the next |
| 1501 | * round. */ |
| 1502 | mch_memmove(conv_rest, (char_u *)fromp, from_size); |
| 1503 | conv_restlen = (int)from_size; |
| 1504 | } |
| 1505 | |
| 1506 | /* move the linerest to before the converted characters */ |
| 1507 | line_start = ptr - linerest; |
| 1508 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1509 | size = (long)((char_u *)top - ptr); |
| 1510 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1511 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1512 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1513 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1514 | if (fio_flags & FIO_CODEPAGE) |
| 1515 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1516 | char_u *src, *dst; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1517 | WCHAR ucs2buf[3]; |
| 1518 | int ucs2len; |
| 1519 | int codepage = FIO_GET_CP(fio_flags); |
| 1520 | int bytelen; |
| 1521 | int found_bad; |
| 1522 | char replstr[2]; |
| 1523 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1524 | /* |
| 1525 | * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1526 | * a codepage, using standard MS-Windows functions. This |
| 1527 | * requires two steps: |
| 1528 | * 1. convert from 'fileencoding' to ucs-2 |
| 1529 | * 2. convert from ucs-2 to 'encoding' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | * |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1531 | * Because there may be illegal bytes AND an incomplete byte |
| 1532 | * sequence at the end, we may have to do the conversion one |
| 1533 | * character at a time to get it right. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1535 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1536 | /* Replacement string for WideCharToMultiByte(). */ |
| 1537 | if (bad_char_behavior > 0) |
| 1538 | replstr[0] = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1539 | else |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1540 | replstr[0] = '?'; |
| 1541 | replstr[1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1544 | * Move the bytes to the end of the buffer, so that we have |
| 1545 | * room to put the result at the start. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1547 | src = ptr + real_size - size; |
| 1548 | mch_memmove(src, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1550 | /* |
| 1551 | * Do the conversion. |
| 1552 | */ |
| 1553 | dst = ptr; |
| 1554 | size = size; |
| 1555 | while (size > 0) |
| 1556 | { |
| 1557 | found_bad = FALSE; |
| 1558 | |
| 1559 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 1560 | if (codepage == CP_UTF8) |
| 1561 | { |
| 1562 | /* Handle CP_UTF8 input ourselves to be able to handle |
| 1563 | * trailing bytes properly. |
| 1564 | * Get one UTF-8 character from src. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1565 | bytelen = (int)utf_ptr2len_len(src, size); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1566 | if (bytelen > size) |
| 1567 | { |
| 1568 | /* Only got some bytes of a character. Normally |
| 1569 | * it's put in "conv_rest", but if it's too long |
| 1570 | * deal with it as if they were illegal bytes. */ |
| 1571 | if (bytelen <= CONV_RESTLEN) |
| 1572 | break; |
| 1573 | |
| 1574 | /* weird overlong byte sequence */ |
| 1575 | bytelen = size; |
| 1576 | found_bad = TRUE; |
| 1577 | } |
| 1578 | else |
| 1579 | { |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 1580 | int u8c = utf_ptr2char(src); |
| 1581 | |
Bram Moolenaar | 86e0108 | 2005-12-29 22:45:34 +0000 | [diff] [blame] | 1582 | if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1583 | found_bad = TRUE; |
| 1584 | ucs2buf[0] = u8c; |
| 1585 | ucs2len = 1; |
| 1586 | } |
| 1587 | } |
| 1588 | else |
| 1589 | # endif |
| 1590 | { |
| 1591 | /* We don't know how long the byte sequence is, try |
| 1592 | * from one to three bytes. */ |
| 1593 | for (bytelen = 1; bytelen <= size && bytelen <= 3; |
| 1594 | ++bytelen) |
| 1595 | { |
| 1596 | ucs2len = MultiByteToWideChar(codepage, |
| 1597 | MB_ERR_INVALID_CHARS, |
| 1598 | (LPCSTR)src, bytelen, |
| 1599 | ucs2buf, 3); |
| 1600 | if (ucs2len > 0) |
| 1601 | break; |
| 1602 | } |
| 1603 | if (ucs2len == 0) |
| 1604 | { |
| 1605 | /* If we have only one byte then it's probably an |
| 1606 | * incomplete byte sequence. Otherwise discard |
| 1607 | * one byte as a bad character. */ |
| 1608 | if (size == 1) |
| 1609 | break; |
| 1610 | found_bad = TRUE; |
| 1611 | bytelen = 1; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | if (!found_bad) |
| 1616 | { |
| 1617 | int i; |
| 1618 | |
| 1619 | /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ |
| 1620 | if (enc_utf8) |
| 1621 | { |
| 1622 | /* From UCS-2 to UTF-8. Cannot fail. */ |
| 1623 | for (i = 0; i < ucs2len; ++i) |
| 1624 | dst += utf_char2bytes(ucs2buf[i], dst); |
| 1625 | } |
| 1626 | else |
| 1627 | { |
| 1628 | BOOL bad = FALSE; |
| 1629 | int dstlen; |
| 1630 | |
| 1631 | /* From UCS-2 to "enc_codepage". If the |
| 1632 | * conversion uses the default character "?", |
| 1633 | * the data doesn't fit in this encoding. */ |
| 1634 | dstlen = WideCharToMultiByte(enc_codepage, 0, |
| 1635 | (LPCWSTR)ucs2buf, ucs2len, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1636 | (LPSTR)dst, (int)(src - dst), |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1637 | replstr, &bad); |
| 1638 | if (bad) |
| 1639 | found_bad = TRUE; |
| 1640 | else |
| 1641 | dst += dstlen; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | if (found_bad) |
| 1646 | { |
| 1647 | /* Deal with bytes we can't convert. */ |
| 1648 | if (can_retry) |
| 1649 | goto rewind_retry; |
| 1650 | if (conv_error == 0) |
| 1651 | conv_error = readfile_linenr(linecnt, ptr, dst); |
| 1652 | if (bad_char_behavior != BAD_DROP) |
| 1653 | { |
| 1654 | if (bad_char_behavior == BAD_KEEP) |
| 1655 | { |
| 1656 | mch_memmove(dst, src, bytelen); |
| 1657 | dst += bytelen; |
| 1658 | } |
| 1659 | else |
| 1660 | *dst++ = bad_char_behavior; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | src += bytelen; |
| 1665 | size -= bytelen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1667 | |
| 1668 | if (size > 0) |
| 1669 | { |
| 1670 | /* An incomplete byte sequence remaining. */ |
| 1671 | mch_memmove(conv_rest, src, size); |
| 1672 | conv_restlen = size; |
| 1673 | } |
| 1674 | |
| 1675 | /* The new size is equal to how much "dst" was advanced. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1676 | size = (long)(dst - ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1677 | } |
| 1678 | else |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1679 | #endif |
| 1680 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | if (fio_flags & FIO_MACROMAN) |
| 1682 | { |
| 1683 | /* |
| 1684 | * Conversion from Apple MacRoman char encoding to UTF-8 or |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1685 | * latin1. This is in os_mac_conv.c. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1686 | */ |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1687 | if (macroman2enc(ptr, &size, real_size) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1688 | goto rewind_retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | } |
| 1690 | else |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1691 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1692 | if (fio_flags != 0) |
| 1693 | { |
| 1694 | int u8c; |
| 1695 | char_u *dest; |
| 1696 | char_u *tail = NULL; |
| 1697 | |
| 1698 | /* |
| 1699 | * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. |
| 1700 | * "enc_utf8" not set: Convert Unicode to Latin1. |
| 1701 | * Go from end to start through the buffer, because the number |
| 1702 | * of bytes may increase. |
| 1703 | * "dest" points to after where the UTF-8 bytes go, "p" points |
| 1704 | * to after the next character to convert. |
| 1705 | */ |
| 1706 | dest = ptr + real_size; |
| 1707 | if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) |
| 1708 | { |
| 1709 | p = ptr + size; |
| 1710 | if (fio_flags == FIO_UTF8) |
| 1711 | { |
| 1712 | /* Check for a trailing incomplete UTF-8 sequence */ |
| 1713 | tail = ptr + size - 1; |
| 1714 | while (tail > ptr && (*tail & 0xc0) == 0x80) |
| 1715 | --tail; |
| 1716 | if (tail + utf_byte2len(*tail) <= ptr + size) |
| 1717 | tail = NULL; |
| 1718 | else |
| 1719 | p = tail; |
| 1720 | } |
| 1721 | } |
| 1722 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1723 | { |
| 1724 | /* Check for a trailing byte */ |
| 1725 | p = ptr + (size & ~1); |
| 1726 | if (size & 1) |
| 1727 | tail = p; |
| 1728 | if ((fio_flags & FIO_UTF16) && p > ptr) |
| 1729 | { |
| 1730 | /* Check for a trailing leading word */ |
| 1731 | if (fio_flags & FIO_ENDIAN_L) |
| 1732 | { |
| 1733 | u8c = (*--p << 8); |
| 1734 | u8c += *--p; |
| 1735 | } |
| 1736 | else |
| 1737 | { |
| 1738 | u8c = *--p; |
| 1739 | u8c += (*--p << 8); |
| 1740 | } |
| 1741 | if (u8c >= 0xd800 && u8c <= 0xdbff) |
| 1742 | tail = p; |
| 1743 | else |
| 1744 | p += 2; |
| 1745 | } |
| 1746 | } |
| 1747 | else /* FIO_UCS4 */ |
| 1748 | { |
| 1749 | /* Check for trailing 1, 2 or 3 bytes */ |
| 1750 | p = ptr + (size & ~3); |
| 1751 | if (size & 3) |
| 1752 | tail = p; |
| 1753 | } |
| 1754 | |
| 1755 | /* If there is a trailing incomplete sequence move it to |
| 1756 | * conv_rest[]. */ |
| 1757 | if (tail != NULL) |
| 1758 | { |
| 1759 | conv_restlen = (int)((ptr + size) - tail); |
| 1760 | mch_memmove(conv_rest, (char_u *)tail, conv_restlen); |
| 1761 | size -= conv_restlen; |
| 1762 | } |
| 1763 | |
| 1764 | |
| 1765 | while (p > ptr) |
| 1766 | { |
| 1767 | if (fio_flags & FIO_LATIN1) |
| 1768 | u8c = *--p; |
| 1769 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1770 | { |
| 1771 | if (fio_flags & FIO_ENDIAN_L) |
| 1772 | { |
| 1773 | u8c = (*--p << 8); |
| 1774 | u8c += *--p; |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | u8c = *--p; |
| 1779 | u8c += (*--p << 8); |
| 1780 | } |
| 1781 | if ((fio_flags & FIO_UTF16) |
| 1782 | && u8c >= 0xdc00 && u8c <= 0xdfff) |
| 1783 | { |
| 1784 | int u16c; |
| 1785 | |
| 1786 | if (p == ptr) |
| 1787 | { |
| 1788 | /* Missing leading word. */ |
| 1789 | if (can_retry) |
| 1790 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1791 | if (conv_error == 0) |
| 1792 | conv_error = readfile_linenr(linecnt, |
| 1793 | ptr, p); |
| 1794 | if (bad_char_behavior == BAD_DROP) |
| 1795 | continue; |
| 1796 | if (bad_char_behavior != BAD_KEEP) |
| 1797 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | /* found second word of double-word, get the first |
| 1801 | * word and compute the resulting character */ |
| 1802 | if (fio_flags & FIO_ENDIAN_L) |
| 1803 | { |
| 1804 | u16c = (*--p << 8); |
| 1805 | u16c += *--p; |
| 1806 | } |
| 1807 | else |
| 1808 | { |
| 1809 | u16c = *--p; |
| 1810 | u16c += (*--p << 8); |
| 1811 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1812 | u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
| 1813 | + (u8c & 0x3ff); |
| 1814 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1815 | /* Check if the word is indeed a leading word. */ |
| 1816 | if (u16c < 0xd800 || u16c > 0xdbff) |
| 1817 | { |
| 1818 | if (can_retry) |
| 1819 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1820 | if (conv_error == 0) |
| 1821 | conv_error = readfile_linenr(linecnt, |
| 1822 | ptr, p); |
| 1823 | if (bad_char_behavior == BAD_DROP) |
| 1824 | continue; |
| 1825 | if (bad_char_behavior != BAD_KEEP) |
| 1826 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1827 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | else if (fio_flags & FIO_UCS4) |
| 1831 | { |
| 1832 | if (fio_flags & FIO_ENDIAN_L) |
| 1833 | { |
Bram Moolenaar | dc1c981 | 2017-10-27 22:15:24 +0200 | [diff] [blame] | 1834 | u8c = (unsigned)*--p << 24; |
| 1835 | u8c += (unsigned)*--p << 16; |
| 1836 | u8c += (unsigned)*--p << 8; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1837 | u8c += *--p; |
| 1838 | } |
| 1839 | else /* big endian */ |
| 1840 | { |
| 1841 | u8c = *--p; |
Bram Moolenaar | dc1c981 | 2017-10-27 22:15:24 +0200 | [diff] [blame] | 1842 | u8c += (unsigned)*--p << 8; |
| 1843 | u8c += (unsigned)*--p << 16; |
| 1844 | u8c += (unsigned)*--p << 24; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | else /* UTF-8 */ |
| 1848 | { |
| 1849 | if (*--p < 0x80) |
| 1850 | u8c = *p; |
| 1851 | else |
| 1852 | { |
| 1853 | len = utf_head_off(ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1854 | p -= len; |
| 1855 | u8c = utf_ptr2char(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | if (len == 0) |
| 1857 | { |
| 1858 | /* Not a valid UTF-8 character, retry with |
| 1859 | * another fenc when possible, otherwise just |
| 1860 | * report the error. */ |
| 1861 | if (can_retry) |
| 1862 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1863 | if (conv_error == 0) |
| 1864 | conv_error = readfile_linenr(linecnt, |
| 1865 | ptr, p); |
| 1866 | if (bad_char_behavior == BAD_DROP) |
| 1867 | continue; |
| 1868 | if (bad_char_behavior != BAD_KEEP) |
| 1869 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1870 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | if (enc_utf8) /* produce UTF-8 */ |
| 1874 | { |
| 1875 | dest -= utf_char2len(u8c); |
| 1876 | (void)utf_char2bytes(u8c, dest); |
| 1877 | } |
| 1878 | else /* produce Latin1 */ |
| 1879 | { |
| 1880 | --dest; |
| 1881 | if (u8c >= 0x100) |
| 1882 | { |
| 1883 | /* character doesn't fit in latin1, retry with |
| 1884 | * another fenc when possible, otherwise just |
| 1885 | * report the error. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1886 | if (can_retry) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1887 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1888 | if (conv_error == 0) |
| 1889 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 1890 | if (bad_char_behavior == BAD_DROP) |
| 1891 | ++dest; |
| 1892 | else if (bad_char_behavior == BAD_KEEP) |
| 1893 | *dest = u8c; |
| 1894 | else if (eap != NULL && eap->bad_char != 0) |
| 1895 | *dest = bad_char_behavior; |
| 1896 | else |
| 1897 | *dest = 0xBF; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1898 | } |
| 1899 | else |
| 1900 | *dest = u8c; |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | /* move the linerest to before the converted characters */ |
| 1905 | line_start = dest - linerest; |
| 1906 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1907 | size = (long)((ptr + real_size) - dest); |
| 1908 | ptr = dest; |
| 1909 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1910 | else if (enc_utf8 && !curbuf->b_p_bin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1911 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1912 | int incomplete_tail = FALSE; |
| 1913 | |
| 1914 | /* Reading UTF-8: Check if the bytes are valid UTF-8. */ |
| 1915 | for (p = ptr; ; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1916 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1917 | int todo = (int)((ptr + size) - p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1918 | int l; |
| 1919 | |
| 1920 | if (todo <= 0) |
| 1921 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | if (*p >= 0x80) |
| 1923 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1924 | /* A length of 1 means it's an illegal byte. Accept |
| 1925 | * an incomplete character at the end though, the next |
| 1926 | * read() will get the next bytes, we'll check it |
| 1927 | * then. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1928 | l = utf_ptr2len_len(p, todo); |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1929 | if (l > todo && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1930 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1931 | /* Avoid retrying with a different encoding when |
| 1932 | * a truncated file is more likely, or attempting |
| 1933 | * to read the rest of an incomplete sequence when |
| 1934 | * we have already done so. */ |
| 1935 | if (p > ptr || filesize > 0) |
| 1936 | incomplete_tail = TRUE; |
| 1937 | /* Incomplete byte sequence, move it to conv_rest[] |
| 1938 | * and try to read the rest of it, unless we've |
| 1939 | * already done so. */ |
| 1940 | if (p > ptr) |
| 1941 | { |
| 1942 | conv_restlen = todo; |
| 1943 | mch_memmove(conv_rest, p, conv_restlen); |
| 1944 | size -= conv_restlen; |
| 1945 | break; |
| 1946 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1947 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1948 | if (l == 1 || l > todo) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1949 | { |
| 1950 | /* Illegal byte. If we can try another encoding |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1951 | * do that, unless at EOF where a truncated |
| 1952 | * file is more likely than a conversion error. */ |
| 1953 | if (can_retry && !incomplete_tail) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1954 | break; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1955 | #ifdef USE_ICONV |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1956 | /* When we did a conversion report an error. */ |
| 1957 | if (iconv_fd != (iconv_t)-1 && conv_error == 0) |
| 1958 | conv_error = readfile_linenr(linecnt, ptr, p); |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1959 | #endif |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1960 | /* Remember the first linenr with an illegal byte */ |
| 1961 | if (conv_error == 0 && illegal_byte == 0) |
| 1962 | illegal_byte = readfile_linenr(linecnt, ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1963 | |
| 1964 | /* Drop, keep or replace the bad byte. */ |
| 1965 | if (bad_char_behavior == BAD_DROP) |
| 1966 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1967 | mch_memmove(p, p + 1, todo - 1); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1968 | --p; |
| 1969 | --size; |
| 1970 | } |
| 1971 | else if (bad_char_behavior != BAD_KEEP) |
| 1972 | *p = bad_char_behavior; |
| 1973 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1974 | else |
| 1975 | p += l - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | } |
| 1977 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1978 | if (p < ptr + size && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1979 | { |
| 1980 | /* Detected a UTF-8 error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1981 | rewind_retry: |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1982 | /* Retry reading with another conversion. */ |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1983 | #if defined(FEAT_EVAL) && defined(USE_ICONV) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1984 | if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
| 1985 | /* iconv() failed, try 'charconvert' */ |
| 1986 | did_iconv = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1987 | else |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1988 | #endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1989 | /* use next item from 'fileencodings' */ |
| 1990 | advance_fenc = TRUE; |
| 1991 | file_rewind = TRUE; |
| 1992 | goto retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1993 | } |
| 1994 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1995 | |
| 1996 | /* count the number of characters (after conversion!) */ |
| 1997 | filesize += size; |
| 1998 | |
| 1999 | /* |
| 2000 | * when reading the first part of a file: guess EOL type |
| 2001 | */ |
| 2002 | if (fileformat == EOL_UNKNOWN) |
| 2003 | { |
| 2004 | /* First try finding a NL, for Dos and Unix */ |
| 2005 | if (try_dos || try_unix) |
| 2006 | { |
Bram Moolenaar | c6b7217 | 2015-02-27 17:48:09 +0100 | [diff] [blame] | 2007 | /* Reset the carriage return counter. */ |
| 2008 | if (try_mac) |
| 2009 | try_mac = 1; |
| 2010 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2011 | for (p = ptr; p < ptr + size; ++p) |
| 2012 | { |
| 2013 | if (*p == NL) |
| 2014 | { |
| 2015 | if (!try_unix |
| 2016 | || (try_dos && p > ptr && p[-1] == CAR)) |
| 2017 | fileformat = EOL_DOS; |
| 2018 | else |
| 2019 | fileformat = EOL_UNIX; |
| 2020 | break; |
| 2021 | } |
Bram Moolenaar | 05eb612 | 2015-02-17 14:15:19 +0100 | [diff] [blame] | 2022 | else if (*p == CAR && try_mac) |
| 2023 | try_mac++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ |
| 2027 | if (fileformat == EOL_UNIX && try_mac) |
| 2028 | { |
| 2029 | /* Need to reset the counters when retrying fenc. */ |
| 2030 | try_mac = 1; |
| 2031 | try_unix = 1; |
| 2032 | for (; p >= ptr && *p != CAR; p--) |
| 2033 | ; |
| 2034 | if (p >= ptr) |
| 2035 | { |
| 2036 | for (p = ptr; p < ptr + size; ++p) |
| 2037 | { |
| 2038 | if (*p == NL) |
| 2039 | try_unix++; |
| 2040 | else if (*p == CAR) |
| 2041 | try_mac++; |
| 2042 | } |
| 2043 | if (try_mac > try_unix) |
| 2044 | fileformat = EOL_MAC; |
| 2045 | } |
| 2046 | } |
Bram Moolenaar | 05eb612 | 2015-02-17 14:15:19 +0100 | [diff] [blame] | 2047 | else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
| 2048 | /* Looking for CR but found no end-of-line markers at |
| 2049 | * all: use the default format. */ |
| 2050 | fileformat = default_fileformat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | /* No NL found: may use Mac format */ |
| 2054 | if (fileformat == EOL_UNKNOWN && try_mac) |
| 2055 | fileformat = EOL_MAC; |
| 2056 | |
| 2057 | /* Still nothing found? Use first format in 'ffs' */ |
| 2058 | if (fileformat == EOL_UNKNOWN) |
| 2059 | fileformat = default_fileformat(); |
| 2060 | |
| 2061 | /* if editing a new file: may set p_tx and p_ff */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2062 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | set_fileformat(fileformat, OPT_LOCAL); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * This loop is executed once for every character read. |
| 2069 | * Keep it fast! |
| 2070 | */ |
| 2071 | if (fileformat == EOL_MAC) |
| 2072 | { |
| 2073 | --ptr; |
| 2074 | while (++ptr, --size >= 0) |
| 2075 | { |
| 2076 | /* catch most common case first */ |
| 2077 | if ((c = *ptr) != NUL && c != CAR && c != NL) |
| 2078 | continue; |
| 2079 | if (c == NUL) |
| 2080 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2081 | else if (c == NL) |
| 2082 | *ptr = CAR; /* NLs are replaced by CRs! */ |
| 2083 | else |
| 2084 | { |
| 2085 | if (skip_count == 0) |
| 2086 | { |
| 2087 | *ptr = NUL; /* end of line */ |
| 2088 | len = (colnr_T) (ptr - line_start + 1); |
| 2089 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2090 | { |
| 2091 | error = TRUE; |
| 2092 | break; |
| 2093 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2094 | #ifdef FEAT_PERSISTENT_UNDO |
| 2095 | if (read_undo_file) |
| 2096 | sha256_update(&sha_ctx, line_start, len); |
| 2097 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | ++lnum; |
| 2099 | if (--read_count == 0) |
| 2100 | { |
| 2101 | error = TRUE; /* break loop */ |
| 2102 | line_start = ptr; /* nothing left to write */ |
| 2103 | break; |
| 2104 | } |
| 2105 | } |
| 2106 | else |
| 2107 | --skip_count; |
| 2108 | line_start = ptr + 1; |
| 2109 | } |
| 2110 | } |
| 2111 | } |
| 2112 | else |
| 2113 | { |
| 2114 | --ptr; |
| 2115 | while (++ptr, --size >= 0) |
| 2116 | { |
| 2117 | if ((c = *ptr) != NUL && c != NL) /* catch most common case */ |
| 2118 | continue; |
| 2119 | if (c == NUL) |
| 2120 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2121 | else |
| 2122 | { |
| 2123 | if (skip_count == 0) |
| 2124 | { |
| 2125 | *ptr = NUL; /* end of line */ |
| 2126 | len = (colnr_T)(ptr - line_start + 1); |
| 2127 | if (fileformat == EOL_DOS) |
| 2128 | { |
Bram Moolenaar | 2aa5f69 | 2017-01-24 15:46:48 +0100 | [diff] [blame] | 2129 | if (ptr > line_start && ptr[-1] == CAR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2130 | { |
Bram Moolenaar | 2aa5f69 | 2017-01-24 15:46:48 +0100 | [diff] [blame] | 2131 | /* remove CR before NL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | ptr[-1] = NUL; |
| 2133 | --len; |
| 2134 | } |
| 2135 | /* |
| 2136 | * Reading in Dos format, but no CR-LF found! |
| 2137 | * When 'fileformats' includes "unix", delete all |
| 2138 | * the lines read so far and start all over again. |
| 2139 | * Otherwise give an error message later. |
| 2140 | */ |
| 2141 | else if (ff_error != EOL_DOS) |
| 2142 | { |
| 2143 | if ( try_unix |
| 2144 | && !read_stdin |
| 2145 | && (read_buffer |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 2146 | || vim_lseek(fd, (off_T)0L, SEEK_SET) |
| 2147 | == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | { |
| 2149 | fileformat = EOL_UNIX; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2150 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2151 | set_fileformat(EOL_UNIX, OPT_LOCAL); |
| 2152 | file_rewind = TRUE; |
| 2153 | keep_fileformat = TRUE; |
| 2154 | goto retry; |
| 2155 | } |
| 2156 | ff_error = EOL_DOS; |
| 2157 | } |
| 2158 | } |
| 2159 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2160 | { |
| 2161 | error = TRUE; |
| 2162 | break; |
| 2163 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2164 | #ifdef FEAT_PERSISTENT_UNDO |
| 2165 | if (read_undo_file) |
| 2166 | sha256_update(&sha_ctx, line_start, len); |
| 2167 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2168 | ++lnum; |
| 2169 | if (--read_count == 0) |
| 2170 | { |
| 2171 | error = TRUE; /* break loop */ |
| 2172 | line_start = ptr; /* nothing left to write */ |
| 2173 | break; |
| 2174 | } |
| 2175 | } |
| 2176 | else |
| 2177 | --skip_count; |
| 2178 | line_start = ptr + 1; |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | linerest = (long)(ptr - line_start); |
| 2183 | ui_breakcheck(); |
| 2184 | } |
| 2185 | |
| 2186 | failed: |
| 2187 | /* not an error, max. number of lines reached */ |
| 2188 | if (error && read_count == 0) |
| 2189 | error = FALSE; |
| 2190 | |
| 2191 | /* |
| 2192 | * If we get EOF in the middle of a line, note the fact and |
| 2193 | * complete the line ourselves. |
| 2194 | * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. |
| 2195 | */ |
| 2196 | if (!error |
| 2197 | && !got_int |
| 2198 | && linerest != 0 |
| 2199 | && !(!curbuf->b_p_bin |
| 2200 | && fileformat == EOL_DOS |
| 2201 | && *line_start == Ctrl_Z |
| 2202 | && ptr == line_start + 1)) |
| 2203 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2204 | /* remember for when writing */ |
| 2205 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2206 | curbuf->b_p_eol = FALSE; |
| 2207 | *ptr = NUL; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2208 | len = (colnr_T)(ptr - line_start + 1); |
| 2209 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | error = TRUE; |
| 2211 | else |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2212 | { |
| 2213 | #ifdef FEAT_PERSISTENT_UNDO |
| 2214 | if (read_undo_file) |
| 2215 | sha256_update(&sha_ctx, line_start, len); |
| 2216 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | read_no_eol_lnum = ++lnum; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2218 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2221 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2222 | save_file_ff(curbuf); /* remember the current file format */ |
| 2223 | |
| 2224 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2225 | if (curbuf->b_cryptstate != NULL) |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2226 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2227 | crypt_free_state(curbuf->b_cryptstate); |
| 2228 | curbuf->b_cryptstate = NULL; |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2229 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2230 | if (cryptkey != NULL && cryptkey != curbuf->b_p_key) |
| 2231 | crypt_free_key(cryptkey); |
| 2232 | /* Don't set cryptkey to NULL, it's used below as a flag that |
| 2233 | * encryption was used. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2234 | #endif |
| 2235 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2236 | /* If editing a new file: set 'fenc' for the current buffer. |
| 2237 | * Also for ":read ++edit file". */ |
| 2238 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2239 | set_string_option_direct((char_u *)"fenc", -1, fenc, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2240 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | if (fenc_alloced) |
| 2242 | vim_free(fenc); |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 2243 | #ifdef USE_ICONV |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | if (iconv_fd != (iconv_t)-1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2245 | iconv_close(iconv_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2246 | #endif |
| 2247 | |
| 2248 | if (!read_buffer && !read_stdin) |
| 2249 | close(fd); /* errors are ignored */ |
Bram Moolenaar | f05da21 | 2009-11-17 16:13:15 +0000 | [diff] [blame] | 2250 | #ifdef HAVE_FD_CLOEXEC |
| 2251 | else |
| 2252 | { |
| 2253 | int fdflags = fcntl(fd, F_GETFD); |
| 2254 | if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 2255 | (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
Bram Moolenaar | f05da21 | 2009-11-17 16:13:15 +0000 | [diff] [blame] | 2256 | } |
| 2257 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | vim_free(buffer); |
| 2259 | |
| 2260 | #ifdef HAVE_DUP |
| 2261 | if (read_stdin) |
| 2262 | { |
| 2263 | /* Use stderr for stdin, makes shell commands work. */ |
| 2264 | close(0); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 2265 | vim_ignored = dup(2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2266 | } |
| 2267 | #endif |
| 2268 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2269 | if (tmpname != NULL) |
| 2270 | { |
| 2271 | mch_remove(tmpname); /* delete converted file */ |
| 2272 | vim_free(tmpname); |
| 2273 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2274 | --no_wait_return; /* may wait for return now */ |
| 2275 | |
| 2276 | /* |
| 2277 | * In recovery mode everything but autocommands is skipped. |
| 2278 | */ |
| 2279 | if (!recoverymode) |
| 2280 | { |
| 2281 | /* need to delete the last line, which comes from the empty buffer */ |
| 2282 | if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
| 2283 | { |
| 2284 | #ifdef FEAT_NETBEANS_INTG |
| 2285 | netbeansFireChanges = 0; |
| 2286 | #endif |
| 2287 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 2288 | #ifdef FEAT_NETBEANS_INTG |
| 2289 | netbeansFireChanges = 1; |
| 2290 | #endif |
| 2291 | --linecnt; |
| 2292 | } |
| 2293 | linecnt = curbuf->b_ml.ml_line_count - linecnt; |
| 2294 | if (filesize == 0) |
| 2295 | linecnt = 0; |
| 2296 | if (newfile || read_buffer) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2297 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2298 | redraw_curbuf_later(NOT_VALID); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2299 | #ifdef FEAT_DIFF |
| 2300 | /* After reading the text into the buffer the diff info needs to |
| 2301 | * be updated. */ |
| 2302 | diff_invalidate(curbuf); |
| 2303 | #endif |
| 2304 | #ifdef FEAT_FOLDING |
| 2305 | /* All folds in the window are invalid now. Mark them for update |
| 2306 | * before triggering autocommands. */ |
| 2307 | foldUpdateAll(curwin); |
| 2308 | #endif |
| 2309 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2310 | else if (linecnt) /* appended at least one line */ |
| 2311 | appended_lines_mark(from, linecnt); |
| 2312 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | #ifndef ALWAYS_USE_GUI |
| 2314 | /* |
| 2315 | * If we were reading from the same terminal as where messages go, |
| 2316 | * the screen will have been messed up. |
| 2317 | * Switch on raw mode now and clear the screen. |
| 2318 | */ |
| 2319 | if (read_stdin) |
| 2320 | { |
| 2321 | settmode(TMODE_RAW); /* set to raw mode */ |
| 2322 | starttermcap(); |
| 2323 | screenclear(); |
| 2324 | } |
| 2325 | #endif |
| 2326 | |
| 2327 | if (got_int) |
| 2328 | { |
| 2329 | if (!(flags & READ_DUMMY)) |
| 2330 | { |
| 2331 | filemess(curbuf, sfname, (char_u *)_(e_interr), 0); |
| 2332 | if (newfile) |
| 2333 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 2334 | } |
| 2335 | msg_scroll = msg_save; |
| 2336 | #ifdef FEAT_VIMINFO |
| 2337 | check_marks_read(); |
| 2338 | #endif |
| 2339 | return OK; /* an interrupt isn't really an error */ |
| 2340 | } |
| 2341 | |
| 2342 | if (!filtering && !(flags & READ_DUMMY)) |
| 2343 | { |
| 2344 | msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ |
| 2345 | c = FALSE; |
| 2346 | |
| 2347 | #ifdef UNIX |
Bram Moolenaar | d569bb0 | 2018-08-11 13:57:20 +0200 | [diff] [blame] | 2348 | if (S_ISFIFO(perm)) /* fifo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | { |
| 2350 | STRCAT(IObuff, _("[fifo]")); |
| 2351 | c = TRUE; |
| 2352 | } |
Bram Moolenaar | d569bb0 | 2018-08-11 13:57:20 +0200 | [diff] [blame] | 2353 | if (S_ISSOCK(perm)) /* or socket */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2354 | { |
| 2355 | STRCAT(IObuff, _("[socket]")); |
| 2356 | c = TRUE; |
| 2357 | } |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2358 | # ifdef OPEN_CHR_FILES |
| 2359 | if (S_ISCHR(perm)) /* or character special */ |
| 2360 | { |
| 2361 | STRCAT(IObuff, _("[character special]")); |
| 2362 | c = TRUE; |
| 2363 | } |
| 2364 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | #endif |
| 2366 | if (curbuf->b_p_ro) |
| 2367 | { |
| 2368 | STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); |
| 2369 | c = TRUE; |
| 2370 | } |
| 2371 | if (read_no_eol_lnum) |
| 2372 | { |
| 2373 | msg_add_eol(); |
| 2374 | c = TRUE; |
| 2375 | } |
| 2376 | if (ff_error == EOL_DOS) |
| 2377 | { |
| 2378 | STRCAT(IObuff, _("[CR missing]")); |
| 2379 | c = TRUE; |
| 2380 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2381 | if (split) |
| 2382 | { |
| 2383 | STRCAT(IObuff, _("[long lines split]")); |
| 2384 | c = TRUE; |
| 2385 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2386 | if (notconverted) |
| 2387 | { |
| 2388 | STRCAT(IObuff, _("[NOT converted]")); |
| 2389 | c = TRUE; |
| 2390 | } |
| 2391 | else if (converted) |
| 2392 | { |
| 2393 | STRCAT(IObuff, _("[converted]")); |
| 2394 | c = TRUE; |
| 2395 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | #ifdef FEAT_CRYPT |
| 2397 | if (cryptkey != NULL) |
| 2398 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2399 | crypt_append_msg(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2400 | c = TRUE; |
| 2401 | } |
| 2402 | #endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2403 | if (conv_error != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2405 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2406 | _("[CONVERSION ERROR in line %ld]"), (long)conv_error); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2407 | c = TRUE; |
| 2408 | } |
| 2409 | else if (illegal_byte > 0) |
| 2410 | { |
| 2411 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2412 | _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); |
| 2413 | c = TRUE; |
| 2414 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 2415 | else if (error) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2416 | { |
| 2417 | STRCAT(IObuff, _("[READ ERRORS]")); |
| 2418 | c = TRUE; |
| 2419 | } |
| 2420 | if (msg_add_fileformat(fileformat)) |
| 2421 | c = TRUE; |
| 2422 | #ifdef FEAT_CRYPT |
| 2423 | if (cryptkey != NULL) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2424 | msg_add_lines(c, (long)linecnt, filesize |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2425 | - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | else |
| 2427 | #endif |
| 2428 | msg_add_lines(c, (long)linecnt, filesize); |
| 2429 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 2430 | VIM_CLEAR(keep_msg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | msg_scrolled_ign = TRUE; |
| 2432 | #ifdef ALWAYS_USE_GUI |
| 2433 | /* Don't show the message when reading stdin, it would end up in a |
| 2434 | * message box (which might be shown when exiting!) */ |
| 2435 | if (read_stdin || read_buffer) |
| 2436 | p = msg_may_trunc(FALSE, IObuff); |
| 2437 | else |
| 2438 | #endif |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 2439 | { |
| 2440 | if (msg_col > 0) |
| 2441 | msg_putchar('\r'); // overwrite previous message |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2442 | p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0); |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 2443 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2444 | if (read_stdin || read_buffer || restart_edit != 0 |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2445 | || (msg_scrolled != 0 && !need_wait_return)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | /* Need to repeat the message after redrawing when: |
| 2447 | * - When reading from stdin (the screen will be cleared next). |
| 2448 | * - When restart_edit is set (otherwise there will be a delay |
| 2449 | * before redrawing). |
| 2450 | * - When the screen was scrolled but there is no wait-return |
| 2451 | * prompt. */ |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2452 | set_keep_msg(p, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2453 | msg_scrolled_ign = FALSE; |
| 2454 | } |
| 2455 | |
| 2456 | /* with errors writing the file requires ":w!" */ |
| 2457 | if (newfile && (error |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2458 | || conv_error != 0 |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 2459 | || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2460 | curbuf->b_p_ro = TRUE; |
| 2461 | |
| 2462 | u_clearline(); /* cannot use "U" command after adding lines */ |
| 2463 | |
| 2464 | /* |
| 2465 | * In Ex mode: cursor at last new line. |
| 2466 | * Otherwise: cursor at first new line. |
| 2467 | */ |
| 2468 | if (exmode_active) |
| 2469 | curwin->w_cursor.lnum = from + linecnt; |
| 2470 | else |
| 2471 | curwin->w_cursor.lnum = from + 1; |
| 2472 | check_cursor_lnum(); |
| 2473 | beginline(BL_WHITE | BL_FIX); /* on first non-blank */ |
| 2474 | |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2475 | if (!cmdmod.lockmarks) |
| 2476 | { |
| 2477 | // Set '[ and '] marks to the newly read lines. |
| 2478 | curbuf->b_op_start.lnum = from + 1; |
| 2479 | curbuf->b_op_start.col = 0; |
| 2480 | curbuf->b_op_end.lnum = from + linecnt; |
| 2481 | curbuf->b_op_end.col = 0; |
| 2482 | } |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2483 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 2484 | #ifdef MSWIN |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2485 | /* |
| 2486 | * Work around a weird problem: When a file has two links (only |
| 2487 | * possible on NTFS) and we write through one link, then stat() it |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 2488 | * through the other link, the timestamp information may be wrong. |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2489 | * It's correct again after reading the file, thus reset the timestamp |
| 2490 | * here. |
| 2491 | */ |
| 2492 | if (newfile && !read_stdin && !read_buffer |
| 2493 | && mch_stat((char *)fname, &st) >= 0) |
| 2494 | { |
| 2495 | buf_store_time(curbuf, &st, fname); |
| 2496 | curbuf->b_mtime_read = curbuf->b_mtime; |
| 2497 | } |
| 2498 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | } |
| 2500 | msg_scroll = msg_save; |
| 2501 | |
| 2502 | #ifdef FEAT_VIMINFO |
| 2503 | /* |
| 2504 | * Get the marks before executing autocommands, so they can be used there. |
| 2505 | */ |
| 2506 | check_marks_read(); |
| 2507 | #endif |
| 2508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2509 | /* |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 2510 | * We remember if the last line of the read didn't have |
| 2511 | * an eol even when 'binary' is off, to support turning 'fixeol' off, |
| 2512 | * or writing the read again with 'binary' on. The latter is required |
| 2513 | * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2514 | */ |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2515 | curbuf->b_no_eol_lnum = read_no_eol_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2516 | |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 2517 | /* When reloading a buffer put the cursor at the first line that is |
| 2518 | * different. */ |
| 2519 | if (flags & READ_KEEP_UNDO) |
| 2520 | u_find_first_changed(); |
| 2521 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2522 | #ifdef FEAT_PERSISTENT_UNDO |
| 2523 | /* |
| 2524 | * When opening a new file locate undo info and read it. |
| 2525 | */ |
| 2526 | if (read_undo_file) |
| 2527 | { |
| 2528 | char_u hash[UNDO_HASH_SIZE]; |
| 2529 | |
| 2530 | sha256_finish(&sha_ctx, hash); |
Bram Moolenaar | 6ed8ed8 | 2010-05-30 20:40:11 +0200 | [diff] [blame] | 2531 | u_read_undo(NULL, hash, fname); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2532 | } |
| 2533 | #endif |
| 2534 | |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 2535 | if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2536 | { |
| 2537 | int m = msg_scroll; |
| 2538 | int n = msg_scrolled; |
| 2539 | |
| 2540 | /* Save the fileformat now, otherwise the buffer will be considered |
| 2541 | * modified if the format/encoding was automatically detected. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2542 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | save_file_ff(curbuf); |
| 2544 | |
| 2545 | /* |
| 2546 | * The output from the autocommands should not overwrite anything and |
| 2547 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 2548 | * output was done. |
| 2549 | */ |
| 2550 | msg_scroll = TRUE; |
| 2551 | if (filtering) |
| 2552 | apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, |
| 2553 | FALSE, curbuf, eap); |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 2554 | else if (newfile || (read_buffer && sfname != NULL)) |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 2555 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
| 2557 | FALSE, curbuf, eap); |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 2558 | if (!au_did_filetype && *curbuf->b_p_ft != NUL) |
| 2559 | /* |
| 2560 | * EVENT_FILETYPE was not triggered but the buffer already has a |
| 2561 | * filetype. Trigger EVENT_FILETYPE using the existing filetype. |
| 2562 | */ |
| 2563 | apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname, |
| 2564 | TRUE, curbuf); |
| 2565 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2566 | else |
| 2567 | apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, |
| 2568 | FALSE, NULL, eap); |
| 2569 | if (msg_scrolled == n) |
| 2570 | msg_scroll = m; |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2571 | # ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2572 | if (aborting()) /* autocmds may abort script processing */ |
| 2573 | return FAIL; |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2574 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2575 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | |
| 2577 | if (recoverymode && error) |
| 2578 | return FAIL; |
| 2579 | return OK; |
| 2580 | } |
| 2581 | |
Bram Moolenaar | f04507d | 2016-08-20 15:05:39 +0200 | [diff] [blame] | 2582 | #if defined(OPEN_CHR_FILES) || defined(PROTO) |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2583 | /* |
| 2584 | * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", |
| 2585 | * which is the name of files used for process substitution output by |
| 2586 | * some shells on some operating systems, e.g., bash on SunOS. |
| 2587 | * Do not accept "/dev/fd/[012]", opening these may hang Vim. |
| 2588 | */ |
Bram Moolenaar | f04507d | 2016-08-20 15:05:39 +0200 | [diff] [blame] | 2589 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2590 | is_dev_fd_file(char_u *fname) |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2591 | { |
| 2592 | return (STRNCMP(fname, "/dev/fd/", 8) == 0 |
| 2593 | && VIM_ISDIGIT(fname[8]) |
| 2594 | && *skipdigits(fname + 9) == NUL |
| 2595 | && (fname[9] != NUL |
| 2596 | || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); |
| 2597 | } |
| 2598 | #endif |
| 2599 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2600 | /* |
| 2601 | * From the current line count and characters read after that, estimate the |
| 2602 | * line number where we are now. |
| 2603 | * Used for error messages that include a line number. |
| 2604 | */ |
| 2605 | static linenr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2606 | readfile_linenr( |
| 2607 | linenr_T linecnt, /* line count before reading more bytes */ |
| 2608 | char_u *p, /* start of more bytes read */ |
| 2609 | char_u *endp) /* end of more bytes read */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2610 | { |
| 2611 | char_u *s; |
| 2612 | linenr_T lnum; |
| 2613 | |
| 2614 | lnum = curbuf->b_ml.ml_line_count - linecnt + 1; |
| 2615 | for (s = p; s < endp; ++s) |
| 2616 | if (*s == '\n') |
| 2617 | ++lnum; |
| 2618 | return lnum; |
| 2619 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2620 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2621 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2622 | * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
| 2623 | * equal to the buffer "buf". Used for calling readfile(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2624 | * Returns OK or FAIL. |
| 2625 | */ |
| 2626 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2627 | prep_exarg(exarg_T *eap, buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | { |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 2629 | eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2630 | if (eap->cmd == NULL) |
| 2631 | return FAIL; |
| 2632 | |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 2633 | sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc); |
| 2634 | eap->force_enc = 8; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2635 | eap->bad_char = buf->b_bad_char; |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 2636 | eap->force_ff = *buf->b_p_ff; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2637 | |
| 2638 | eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2639 | eap->read_edit = FALSE; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2640 | eap->forceit = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2641 | return OK; |
| 2642 | } |
| 2643 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2644 | /* |
| 2645 | * Set default or forced 'fileformat' and 'binary'. |
| 2646 | */ |
| 2647 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2648 | set_file_options(int set_options, exarg_T *eap) |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2649 | { |
| 2650 | /* set default 'fileformat' */ |
| 2651 | if (set_options) |
| 2652 | { |
| 2653 | if (eap != NULL && eap->force_ff != 0) |
| 2654 | set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
| 2655 | else if (*p_ffs != NUL) |
| 2656 | set_fileformat(default_fileformat(), OPT_LOCAL); |
| 2657 | } |
| 2658 | |
| 2659 | /* set or reset 'binary' */ |
| 2660 | if (eap != NULL && eap->force_bin != 0) |
| 2661 | { |
| 2662 | int oldval = curbuf->b_p_bin; |
| 2663 | |
| 2664 | curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
| 2665 | set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); |
| 2666 | } |
| 2667 | } |
| 2668 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2669 | /* |
| 2670 | * Set forced 'fileencoding'. |
| 2671 | */ |
| 2672 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2673 | set_forced_fenc(exarg_T *eap) |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2674 | { |
| 2675 | if (eap->force_enc != 0) |
| 2676 | { |
| 2677 | char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 2678 | |
| 2679 | if (fenc != NULL) |
| 2680 | set_string_option_direct((char_u *)"fenc", -1, |
| 2681 | fenc, OPT_FREE|OPT_LOCAL, 0); |
| 2682 | vim_free(fenc); |
| 2683 | } |
| 2684 | } |
| 2685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | /* |
| 2687 | * Find next fileencoding to use from 'fileencodings'. |
| 2688 | * "pp" points to fenc_next. It's advanced to the next item. |
| 2689 | * When there are no more items, an empty string is returned and *pp is set to |
| 2690 | * NULL. |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 2691 | * When *pp is not set to NULL, the result is in allocated memory and "alloced" |
| 2692 | * is set to TRUE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2693 | */ |
| 2694 | static char_u * |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 2695 | next_fenc(char_u **pp, int *alloced) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | { |
| 2697 | char_u *p; |
| 2698 | char_u *r; |
| 2699 | |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 2700 | *alloced = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | if (**pp == NUL) |
| 2702 | { |
| 2703 | *pp = NULL; |
| 2704 | return (char_u *)""; |
| 2705 | } |
| 2706 | p = vim_strchr(*pp, ','); |
| 2707 | if (p == NULL) |
| 2708 | { |
| 2709 | r = enc_canonize(*pp); |
| 2710 | *pp += STRLEN(*pp); |
| 2711 | } |
| 2712 | else |
| 2713 | { |
| 2714 | r = vim_strnsave(*pp, (int)(p - *pp)); |
| 2715 | *pp = p + 1; |
| 2716 | if (r != NULL) |
| 2717 | { |
| 2718 | p = enc_canonize(r); |
| 2719 | vim_free(r); |
| 2720 | r = p; |
| 2721 | } |
| 2722 | } |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 2723 | if (r != NULL) |
| 2724 | *alloced = TRUE; |
| 2725 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2726 | { |
Bram Moolenaar | f077db2 | 2019-08-13 00:18:24 +0200 | [diff] [blame] | 2727 | // out of memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | r = (char_u *)""; |
| 2729 | *pp = NULL; |
| 2730 | } |
| 2731 | return r; |
| 2732 | } |
| 2733 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 2734 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | /* |
| 2736 | * Convert a file with the 'charconvert' expression. |
| 2737 | * This closes the file which is to be read, converts it and opens the |
| 2738 | * resulting file for reading. |
| 2739 | * Returns name of the resulting converted file (the caller should delete it |
| 2740 | * after reading it). |
| 2741 | * Returns NULL if the conversion failed ("*fdp" is not set) . |
| 2742 | */ |
| 2743 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2744 | readfile_charconvert( |
| 2745 | char_u *fname, /* name of input file */ |
| 2746 | char_u *fenc, /* converted from */ |
| 2747 | int *fdp) /* in/out: file descriptor of file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | { |
| 2749 | char_u *tmpname; |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2750 | char *errmsg = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2751 | |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 2752 | tmpname = vim_tempname('r', FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2753 | if (tmpname == NULL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2754 | errmsg = _("Can't find temp file for conversion"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2755 | else |
| 2756 | { |
| 2757 | close(*fdp); /* close the input file, ignore errors */ |
| 2758 | *fdp = -1; |
| 2759 | if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, |
| 2760 | fname, tmpname) == FAIL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2761 | errmsg = _("Conversion with 'charconvert' failed"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2762 | if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, |
| 2763 | O_RDONLY | O_EXTRA, 0)) < 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2764 | errmsg = _("can't read output of 'charconvert'"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | if (errmsg != NULL) |
| 2768 | { |
| 2769 | /* Don't use emsg(), it breaks mappings, the retry with |
| 2770 | * another type of conversion might still work. */ |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2771 | msg(errmsg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | if (tmpname != NULL) |
| 2773 | { |
| 2774 | mch_remove(tmpname); /* delete converted file */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 2775 | VIM_CLEAR(tmpname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | /* If the input file is closed, open it (caller should check for error). */ |
| 2780 | if (*fdp < 0) |
| 2781 | *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 2782 | |
| 2783 | return tmpname; |
| 2784 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2785 | #endif |
| 2786 | |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 2787 | #if defined(FEAT_CRYPT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | /* |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2789 | * Check for magic number used for encryption. Applies to the current buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2790 | * If found, the magic number is removed from ptr[*sizep] and *sizep and |
| 2791 | * *filesizep are updated. |
| 2792 | * Return the (new) encryption key, NULL for no encryption. |
| 2793 | */ |
| 2794 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2795 | check_for_cryptkey( |
| 2796 | char_u *cryptkey, /* previous encryption key or NULL */ |
| 2797 | char_u *ptr, /* pointer to read bytes */ |
| 2798 | long *sizep, /* length of read bytes */ |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 2799 | off_T *filesizep, /* nr of bytes used from file */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2800 | int newfile, /* editing a new buffer */ |
| 2801 | char_u *fname, /* file name to display */ |
| 2802 | int *did_ask) /* flag: whether already asked for key */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2803 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2804 | int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
Bram Moolenaar | cf81aef | 2013-08-25 17:46:08 +0200 | [diff] [blame] | 2805 | int b_p_ro = curbuf->b_p_ro; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2806 | |
| 2807 | if (method >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2808 | { |
Bram Moolenaar | cf81aef | 2013-08-25 17:46:08 +0200 | [diff] [blame] | 2809 | /* Mark the buffer as read-only until the decryption has taken place. |
| 2810 | * Avoids accidentally overwriting the file with garbage. */ |
| 2811 | curbuf->b_p_ro = TRUE; |
| 2812 | |
Bram Moolenaar | 2be7950 | 2014-08-13 21:58:28 +0200 | [diff] [blame] | 2813 | /* Set the cryptmethod local to the buffer. */ |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2814 | crypt_set_cm_option(curbuf, method); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2815 | if (cryptkey == NULL && !*did_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2816 | { |
| 2817 | if (*curbuf->b_p_key) |
| 2818 | cryptkey = curbuf->b_p_key; |
| 2819 | else |
| 2820 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2821 | /* When newfile is TRUE, store the typed key in the 'key' |
| 2822 | * option and don't free it. bf needs hash of the key saved. |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2823 | * Don't ask for the key again when first time Enter was hit. |
| 2824 | * Happens when retrying to detect encoding. */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2825 | smsg(_(need_key_msg), fname); |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2826 | msg_scroll = TRUE; |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 2827 | crypt_check_method(method); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2828 | cryptkey = crypt_get_key(newfile, FALSE); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2829 | *did_ask = TRUE; |
| 2830 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | /* check if empty key entered */ |
| 2832 | if (cryptkey != NULL && *cryptkey == NUL) |
| 2833 | { |
| 2834 | if (cryptkey != curbuf->b_p_key) |
| 2835 | vim_free(cryptkey); |
| 2836 | cryptkey = NULL; |
| 2837 | } |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | if (cryptkey != NULL) |
| 2842 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2843 | int header_len; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2844 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2845 | curbuf->b_cryptstate = crypt_create_from_header( |
| 2846 | method, cryptkey, ptr); |
| 2847 | crypt_set_cm_option(curbuf, method); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2848 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2849 | /* Remove cryptmethod specific header from the text. */ |
| 2850 | header_len = crypt_get_header_len(method); |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 2851 | if (*sizep <= header_len) |
| 2852 | /* invalid header, buffer can't be encrypted */ |
| 2853 | return NULL; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2854 | *filesizep += header_len; |
| 2855 | *sizep -= header_len; |
| 2856 | mch_memmove(ptr, ptr + header_len, (size_t)*sizep); |
| 2857 | |
Bram Moolenaar | cf81aef | 2013-08-25 17:46:08 +0200 | [diff] [blame] | 2858 | /* Restore the read-only flag. */ |
| 2859 | curbuf->b_p_ro = b_p_ro; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | } |
| 2861 | } |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2862 | /* When starting to edit a new file which does not have encryption, clear |
| 2863 | * the 'key' option, except when starting up (called with -x argument) */ |
Bram Moolenaar | fa0ff9a | 2010-07-25 16:05:19 +0200 | [diff] [blame] | 2864 | else if (newfile && *curbuf->b_p_key != NUL && !starting) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2865 | set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
| 2866 | |
| 2867 | return cryptkey; |
| 2868 | } |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2869 | #endif /* FEAT_CRYPT */ |
| 2870 | |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2871 | #if defined(VMS) && !defined(MIN) |
| 2872 | /* Older DECC compiler for VAX doesn't define MIN() */ |
| 2873 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 2874 | #endif |
| 2875 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2876 | /* |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 2877 | * Return TRUE if a file appears to be read-only from the file permissions. |
| 2878 | */ |
| 2879 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2880 | check_file_readonly( |
| 2881 | char_u *fname, /* full path to file */ |
Bram Moolenaar | bd67aac | 2019-09-21 23:09:04 +0200 | [diff] [blame] | 2882 | int perm UNUSED) /* known permissions on file */ |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 2883 | { |
| 2884 | #ifndef USE_MCH_ACCESS |
| 2885 | int fd = 0; |
| 2886 | #endif |
| 2887 | |
| 2888 | return ( |
| 2889 | #ifdef USE_MCH_ACCESS |
| 2890 | # ifdef UNIX |
| 2891 | (perm & 0222) == 0 || |
| 2892 | # endif |
| 2893 | mch_access((char *)fname, W_OK) |
| 2894 | #else |
| 2895 | (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 |
| 2896 | ? TRUE : (close(fd), FALSE) |
| 2897 | #endif |
| 2898 | ); |
| 2899 | } |
| 2900 | |
Bram Moolenaar | a787019 | 2019-02-14 12:56:36 +0100 | [diff] [blame] | 2901 | #if defined(HAVE_FSYNC) || defined(PROTO) |
| 2902 | /* |
| 2903 | * Call fsync() with Mac-specific exception. |
| 2904 | * Return fsync() result: zero for success. |
| 2905 | */ |
| 2906 | int |
| 2907 | vim_fsync(int fd) |
| 2908 | { |
| 2909 | int r; |
| 2910 | |
| 2911 | # ifdef MACOS_X |
| 2912 | r = fcntl(fd, F_FULLFSYNC); |
Bram Moolenaar | 9166838 | 2019-02-21 12:16:12 +0100 | [diff] [blame] | 2913 | if (r != 0) // F_FULLFSYNC not working or not supported |
Bram Moolenaar | a787019 | 2019-02-14 12:56:36 +0100 | [diff] [blame] | 2914 | # endif |
| 2915 | r = fsync(fd); |
| 2916 | return r; |
| 2917 | } |
| 2918 | #endif |
| 2919 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2920 | /* |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2921 | * Set the name of the current buffer. Use when the buffer doesn't have a |
| 2922 | * name and a ":r" or ":w" command with a file name is used. |
| 2923 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 2924 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2925 | set_rw_fname(char_u *fname, char_u *sfname) |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2926 | { |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 2927 | buf_T *buf = curbuf; |
| 2928 | |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2929 | /* It's like the unnamed buffer is deleted.... */ |
| 2930 | if (curbuf->b_p_bl) |
| 2931 | apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); |
| 2932 | apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2933 | #ifdef FEAT_EVAL |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2934 | if (aborting()) /* autocmds may abort script processing */ |
| 2935 | return FAIL; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2936 | #endif |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 2937 | if (curbuf != buf) |
| 2938 | { |
| 2939 | /* We are in another buffer now, don't do the renaming. */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2940 | emsg(_(e_auchangedbuf)); |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 2941 | return FAIL; |
| 2942 | } |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2943 | |
| 2944 | if (setfname(curbuf, fname, sfname, FALSE) == OK) |
| 2945 | curbuf->b_flags |= BF_NOTEDITED; |
| 2946 | |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2947 | /* ....and a new named one is created */ |
| 2948 | apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); |
| 2949 | if (curbuf->b_p_bl) |
| 2950 | apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2951 | #ifdef FEAT_EVAL |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2952 | if (aborting()) /* autocmds may abort script processing */ |
| 2953 | return FAIL; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2954 | #endif |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2955 | |
| 2956 | /* Do filetype detection now if 'filetype' is empty. */ |
| 2957 | if (*curbuf->b_p_ft == NUL) |
| 2958 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2959 | if (au_has_group((char_u *)"filetypedetect")) |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 2960 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 2961 | do_modelines(0); |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2962 | } |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2963 | |
| 2964 | return OK; |
| 2965 | } |
| 2966 | |
| 2967 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | * Put file name into IObuff with quotes. |
| 2969 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2970 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2971 | msg_add_fname(buf_T *buf, char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2972 | { |
| 2973 | if (fname == NULL) |
| 2974 | fname = (char_u *)"-stdin-"; |
| 2975 | home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); |
| 2976 | IObuff[0] = '"'; |
| 2977 | STRCAT(IObuff, "\" "); |
| 2978 | } |
| 2979 | |
| 2980 | /* |
| 2981 | * Append message for text mode to IObuff. |
| 2982 | * Return TRUE if something appended. |
| 2983 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 2984 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2985 | msg_add_fileformat(int eol_type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2986 | { |
| 2987 | #ifndef USE_CRNL |
| 2988 | if (eol_type == EOL_DOS) |
| 2989 | { |
| 2990 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); |
| 2991 | return TRUE; |
| 2992 | } |
| 2993 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | if (eol_type == EOL_MAC) |
| 2995 | { |
| 2996 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); |
| 2997 | return TRUE; |
| 2998 | } |
Bram Moolenaar | 0059074 | 2019-02-15 21:06:09 +0100 | [diff] [blame] | 2999 | #ifdef USE_CRNL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3000 | if (eol_type == EOL_UNIX) |
| 3001 | { |
| 3002 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); |
| 3003 | return TRUE; |
| 3004 | } |
| 3005 | #endif |
| 3006 | return FALSE; |
| 3007 | } |
| 3008 | |
| 3009 | /* |
| 3010 | * Append line and character count to IObuff. |
| 3011 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3012 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3013 | msg_add_lines( |
| 3014 | int insert_space, |
| 3015 | long lnum, |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3016 | off_T nchars) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3017 | { |
| 3018 | char_u *p; |
| 3019 | |
| 3020 | p = IObuff + STRLEN(IObuff); |
| 3021 | |
| 3022 | if (insert_space) |
| 3023 | *p++ = ' '; |
| 3024 | if (shortmess(SHM_LINES)) |
Bram Moolenaar | bde9810 | 2016-07-01 20:03:42 +0200 | [diff] [blame] | 3025 | vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3026 | "%ldL, %lldC", lnum, (long_long_T)nchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3027 | else |
| 3028 | { |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3029 | sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | p += STRLEN(p); |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3031 | vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
| 3032 | NGETTEXT("%lld character", "%lld characters", nchars), |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3033 | (long_long_T)nchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | /* |
| 3038 | * Append message for missing line separator to IObuff. |
| 3039 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3040 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3041 | msg_add_eol(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3042 | { |
| 3043 | STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); |
| 3044 | } |
| 3045 | |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3046 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3047 | time_differs(long t1, long t2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3049 | #if defined(__linux__) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3050 | /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
| 3051 | * the seconds. Since the roundoff is done when flushing the inode, the |
| 3052 | * time may change unexpectedly by one second!!! */ |
| 3053 | return (t1 - t2 > 1 || t2 - t1 > 1); |
| 3054 | #else |
| 3055 | return (t1 != t2); |
| 3056 | #endif |
| 3057 | } |
| 3058 | |
| 3059 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 3060 | * Return TRUE if file encoding "fenc" requires conversion from or to |
| 3061 | * 'encoding'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3062 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3063 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3064 | need_conversion(char_u *fenc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3065 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 3066 | int same_encoding; |
| 3067 | int enc_flags; |
| 3068 | int fenc_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3069 | |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 3070 | if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 3071 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 3072 | same_encoding = TRUE; |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 3073 | fenc_flags = 0; |
| 3074 | } |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 3075 | else |
| 3076 | { |
| 3077 | /* Ignore difference between "ansi" and "latin1", "ucs-4" and |
| 3078 | * "ucs-4be", etc. */ |
| 3079 | enc_flags = get_fio_flags(p_enc); |
| 3080 | fenc_flags = get_fio_flags(fenc); |
| 3081 | same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); |
| 3082 | } |
| 3083 | if (same_encoding) |
| 3084 | { |
| 3085 | /* Specified encoding matches with 'encoding'. This requires |
| 3086 | * conversion when 'encoding' is Unicode but not UTF-8. */ |
| 3087 | return enc_unicode != 0; |
| 3088 | } |
| 3089 | |
| 3090 | /* Encodings differ. However, conversion is not needed when 'enc' is any |
| 3091 | * Unicode encoding and the file is UTF-8. */ |
| 3092 | return !(enc_utf8 && fenc_flags == FIO_UTF8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | /* |
| 3096 | * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the |
| 3097 | * internal conversion. |
| 3098 | * if "ptr" is an empty string, use 'encoding'. |
| 3099 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3100 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3101 | get_fio_flags(char_u *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3102 | { |
| 3103 | int prop; |
| 3104 | |
| 3105 | if (*ptr == NUL) |
| 3106 | ptr = p_enc; |
| 3107 | |
| 3108 | prop = enc_canon_props(ptr); |
| 3109 | if (prop & ENC_UNICODE) |
| 3110 | { |
| 3111 | if (prop & ENC_2BYTE) |
| 3112 | { |
| 3113 | if (prop & ENC_ENDIAN_L) |
| 3114 | return FIO_UCS2 | FIO_ENDIAN_L; |
| 3115 | return FIO_UCS2; |
| 3116 | } |
| 3117 | if (prop & ENC_4BYTE) |
| 3118 | { |
| 3119 | if (prop & ENC_ENDIAN_L) |
| 3120 | return FIO_UCS4 | FIO_ENDIAN_L; |
| 3121 | return FIO_UCS4; |
| 3122 | } |
| 3123 | if (prop & ENC_2WORD) |
| 3124 | { |
| 3125 | if (prop & ENC_ENDIAN_L) |
| 3126 | return FIO_UTF16 | FIO_ENDIAN_L; |
| 3127 | return FIO_UTF16; |
| 3128 | } |
| 3129 | return FIO_UTF8; |
| 3130 | } |
| 3131 | if (prop & ENC_LATIN1) |
| 3132 | return FIO_LATIN1; |
| 3133 | /* must be ENC_DBCS, requires iconv() */ |
| 3134 | return 0; |
| 3135 | } |
| 3136 | |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3137 | #if defined(MSWIN) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3138 | /* |
| 3139 | * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed |
| 3140 | * for the conversion MS-Windows can do for us. Also accept "utf-8". |
| 3141 | * Used for conversion between 'encoding' and 'fileencoding'. |
| 3142 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3143 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3144 | get_win_fio_flags(char_u *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3145 | { |
| 3146 | int cp; |
| 3147 | |
| 3148 | /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ |
| 3149 | if (!enc_utf8 && enc_codepage <= 0) |
| 3150 | return 0; |
| 3151 | |
| 3152 | cp = encname2codepage(ptr); |
| 3153 | if (cp == 0) |
| 3154 | { |
| 3155 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 3156 | if (STRCMP(ptr, "utf-8") == 0) |
| 3157 | cp = CP_UTF8; |
| 3158 | else |
| 3159 | # endif |
| 3160 | return 0; |
| 3161 | } |
| 3162 | return FIO_PUT_CP(cp) | FIO_CODEPAGE; |
| 3163 | } |
| 3164 | #endif |
| 3165 | |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3166 | #if defined(MACOS_CONVERT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3167 | /* |
| 3168 | * Check "ptr" for a Carbon supported encoding and return the FIO_ flags |
| 3169 | * needed for the internal conversion to/from utf-8 or latin1. |
| 3170 | */ |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3171 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3172 | get_mac_fio_flags(char_u *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3173 | { |
| 3174 | if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 3175 | && (enc_canon_props(ptr) & ENC_MACROMAN)) |
| 3176 | return FIO_MACROMAN; |
| 3177 | return 0; |
| 3178 | } |
| 3179 | #endif |
| 3180 | |
| 3181 | /* |
| 3182 | * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. |
| 3183 | * "size" must be at least 2. |
| 3184 | * Return the name of the encoding and set "*lenp" to the length. |
| 3185 | * Returns NULL when no BOM found. |
| 3186 | */ |
| 3187 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3188 | check_for_bom( |
| 3189 | char_u *p, |
| 3190 | long size, |
| 3191 | int *lenp, |
| 3192 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3193 | { |
| 3194 | char *name = NULL; |
| 3195 | int len = 2; |
| 3196 | |
| 3197 | if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf |
Bram Moolenaar | ee0f5a6 | 2008-07-24 20:09:16 +0000 | [diff] [blame] | 3198 | && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3199 | { |
| 3200 | name = "utf-8"; /* EF BB BF */ |
| 3201 | len = 3; |
| 3202 | } |
| 3203 | else if (p[0] == 0xff && p[1] == 0xfe) |
| 3204 | { |
| 3205 | if (size >= 4 && p[2] == 0 && p[3] == 0 |
| 3206 | && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) |
| 3207 | { |
| 3208 | name = "ucs-4le"; /* FF FE 00 00 */ |
| 3209 | len = 4; |
| 3210 | } |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 3211 | else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3212 | name = "ucs-2le"; /* FF FE */ |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 3213 | else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
| 3214 | /* utf-16le is preferred, it also works for ucs-2le text */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3215 | name = "utf-16le"; /* FF FE */ |
| 3216 | } |
| 3217 | else if (p[0] == 0xfe && p[1] == 0xff |
| 3218 | && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) |
| 3219 | { |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 3220 | /* Default to utf-16, it works also for ucs-2 text. */ |
| 3221 | if (flags == FIO_UCS2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3222 | name = "ucs-2"; /* FE FF */ |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 3223 | else |
| 3224 | name = "utf-16"; /* FE FF */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3225 | } |
| 3226 | else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe |
| 3227 | && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) |
| 3228 | { |
| 3229 | name = "ucs-4"; /* 00 00 FE FF */ |
| 3230 | len = 4; |
| 3231 | } |
| 3232 | |
| 3233 | *lenp = len; |
| 3234 | return (char_u *)name; |
| 3235 | } |
| 3236 | |
| 3237 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3238 | * Try to find a shortname by comparing the fullname with the current |
| 3239 | * directory. |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 3240 | * Returns "full_path" or pointer into "full_path" if shortened. |
| 3241 | */ |
| 3242 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3243 | shorten_fname1(char_u *full_path) |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 3244 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3245 | char_u *dirname; |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 3246 | char_u *p = full_path; |
| 3247 | |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3248 | dirname = alloc(MAXPATHL); |
| 3249 | if (dirname == NULL) |
| 3250 | return full_path; |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 3251 | if (mch_dirname(dirname, MAXPATHL) == OK) |
| 3252 | { |
| 3253 | p = shorten_fname(full_path, dirname); |
| 3254 | if (p == NULL || *p == NUL) |
| 3255 | p = full_path; |
| 3256 | } |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3257 | vim_free(dirname); |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 3258 | return p; |
| 3259 | } |
| 3260 | |
| 3261 | /* |
| 3262 | * Try to find a shortname by comparing the fullname with the current |
| 3263 | * directory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3264 | * Returns NULL if not shorter name possible, pointer into "full_path" |
| 3265 | * otherwise. |
| 3266 | */ |
| 3267 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3268 | shorten_fname(char_u *full_path, char_u *dir_name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3269 | { |
| 3270 | int len; |
| 3271 | char_u *p; |
| 3272 | |
| 3273 | if (full_path == NULL) |
| 3274 | return NULL; |
| 3275 | len = (int)STRLEN(dir_name); |
| 3276 | if (fnamencmp(dir_name, full_path, len) == 0) |
| 3277 | { |
| 3278 | p = full_path + len; |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3279 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3280 | /* |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3281 | * MS-Windows: when a file is in the root directory, dir_name will end |
| 3282 | * in a slash, since C: by itself does not define a specific dir. In |
| 3283 | * this case p may already be correct. <negri> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3284 | */ |
| 3285 | if (!((len > 2) && (*(p - 2) == ':'))) |
| 3286 | #endif |
| 3287 | { |
| 3288 | if (vim_ispathsep(*p)) |
| 3289 | ++p; |
| 3290 | #ifndef VMS /* the path separator is always part of the path */ |
| 3291 | else |
| 3292 | p = NULL; |
| 3293 | #endif |
| 3294 | } |
| 3295 | } |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3296 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3297 | /* |
| 3298 | * When using a file in the current drive, remove the drive name: |
| 3299 | * "A:\dir\file" -> "\dir\file". This helps when moving a session file on |
| 3300 | * a floppy from "A:\dir" to "B:\dir". |
| 3301 | */ |
| 3302 | else if (len > 3 |
| 3303 | && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) |
| 3304 | && full_path[1] == ':' |
| 3305 | && vim_ispathsep(full_path[2])) |
| 3306 | p = full_path + 2; |
| 3307 | #endif |
| 3308 | else |
| 3309 | p = NULL; |
| 3310 | return p; |
| 3311 | } |
| 3312 | |
| 3313 | /* |
Bram Moolenaar | a796d46 | 2018-05-01 14:30:36 +0200 | [diff] [blame] | 3314 | * Shorten filename of a buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3315 | * When "force" is TRUE: Use full path from now on for files currently being |
| 3316 | * edited, both for file name and swap file name. Try to shorten the file |
| 3317 | * names a bit, if safe to do so. |
| 3318 | * When "force" is FALSE: Only try to shorten absolute file names. |
| 3319 | * For buffers that have buftype "nofile" or "scratch": never change the file |
| 3320 | * name. |
| 3321 | */ |
| 3322 | void |
Bram Moolenaar | a796d46 | 2018-05-01 14:30:36 +0200 | [diff] [blame] | 3323 | shorten_buf_fname(buf_T *buf, char_u *dirname, int force) |
| 3324 | { |
| 3325 | char_u *p; |
| 3326 | |
| 3327 | if (buf->b_fname != NULL |
| 3328 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 26910de | 2019-06-15 19:37:15 +0200 | [diff] [blame] | 3329 | && !bt_nofilename(buf) |
Bram Moolenaar | a796d46 | 2018-05-01 14:30:36 +0200 | [diff] [blame] | 3330 | #endif |
| 3331 | && !path_with_url(buf->b_fname) |
| 3332 | && (force |
| 3333 | || buf->b_sfname == NULL |
| 3334 | || mch_isFullName(buf->b_sfname))) |
| 3335 | { |
Bram Moolenaar | 3d6014f | 2018-10-11 19:27:47 +0200 | [diff] [blame] | 3336 | if (buf->b_sfname != buf->b_ffname) |
| 3337 | VIM_CLEAR(buf->b_sfname); |
Bram Moolenaar | a796d46 | 2018-05-01 14:30:36 +0200 | [diff] [blame] | 3338 | p = shorten_fname(buf->b_ffname, dirname); |
| 3339 | if (p != NULL) |
| 3340 | { |
| 3341 | buf->b_sfname = vim_strsave(p); |
| 3342 | buf->b_fname = buf->b_sfname; |
| 3343 | } |
| 3344 | if (p == NULL || buf->b_fname == NULL) |
| 3345 | buf->b_fname = buf->b_ffname; |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | /* |
| 3350 | * Shorten filenames for all buffers. |
| 3351 | */ |
| 3352 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3353 | shorten_fnames(int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3354 | { |
| 3355 | char_u dirname[MAXPATHL]; |
| 3356 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3357 | |
| 3358 | mch_dirname(dirname, MAXPATHL); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3359 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3360 | { |
Bram Moolenaar | a796d46 | 2018-05-01 14:30:36 +0200 | [diff] [blame] | 3361 | shorten_buf_fname(buf, dirname, force); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 3362 | |
| 3363 | /* Always make the swap file name a full path, a "nofile" buffer may |
| 3364 | * also have a swap file. */ |
| 3365 | mf_fullname(buf->b_ml.ml_mfp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3366 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3367 | status_redraw_all(); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 3368 | redraw_tabline = TRUE; |
Bram Moolenaar | 90f3e7a | 2019-08-01 22:40:44 +0200 | [diff] [blame] | 3369 | #ifdef FEAT_TEXT_PROP |
| 3370 | popup_update_preview_title(); |
| 3371 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ |
| 3375 | || defined(FEAT_GUI_MSWIN) \ |
| 3376 | || defined(FEAT_GUI_MAC) \ |
| 3377 | || defined(PROTO) |
| 3378 | /* |
| 3379 | * Shorten all filenames in "fnames[count]" by current directory. |
| 3380 | */ |
| 3381 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3382 | shorten_filenames(char_u **fnames, int count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | { |
| 3384 | int i; |
| 3385 | char_u dirname[MAXPATHL]; |
| 3386 | char_u *p; |
| 3387 | |
| 3388 | if (fnames == NULL || count < 1) |
| 3389 | return; |
| 3390 | mch_dirname(dirname, sizeof(dirname)); |
| 3391 | for (i = 0; i < count; ++i) |
| 3392 | { |
| 3393 | if ((p = shorten_fname(fnames[i], dirname)) != NULL) |
| 3394 | { |
| 3395 | /* shorten_fname() returns pointer in given "fnames[i]". If free |
| 3396 | * "fnames[i]" first, "p" becomes invalid. So we need to copy |
| 3397 | * "p" first then free fnames[i]. */ |
| 3398 | p = vim_strsave(p); |
| 3399 | vim_free(fnames[i]); |
| 3400 | fnames[i] = p; |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | #endif |
| 3405 | |
| 3406 | /* |
Bram Moolenaar | b782ba4 | 2018-08-07 21:39:28 +0200 | [diff] [blame] | 3407 | * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3408 | * fo_o_h.ext for MSDOS or when shortname option set. |
| 3409 | * |
| 3410 | * Assumed that fname is a valid name found in the filesystem we assure that |
| 3411 | * the return value is a different name and ends in 'ext'. |
| 3412 | * "ext" MUST be at most 4 characters long if it starts with a dot, 3 |
| 3413 | * characters otherwise. |
| 3414 | * Space for the returned name is allocated, must be freed later. |
| 3415 | * Returns NULL when out of memory. |
| 3416 | */ |
| 3417 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3418 | modname( |
| 3419 | char_u *fname, |
| 3420 | char_u *ext, |
| 3421 | int prepend_dot) /* may prepend a '.' to file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3422 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3423 | return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | fname, ext, prepend_dot); |
| 3425 | } |
| 3426 | |
| 3427 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3428 | buf_modname( |
| 3429 | int shortname, /* use 8.3 file name */ |
| 3430 | char_u *fname, |
| 3431 | char_u *ext, |
| 3432 | int prepend_dot) /* may prepend a '.' to file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3433 | { |
| 3434 | char_u *retval; |
| 3435 | char_u *s; |
| 3436 | char_u *e; |
| 3437 | char_u *ptr; |
| 3438 | int fnamelen, extlen; |
| 3439 | |
| 3440 | extlen = (int)STRLEN(ext); |
| 3441 | |
| 3442 | /* |
| 3443 | * If there is no file name we must get the name of the current directory |
| 3444 | * (we need the full path in case :cd is used). |
| 3445 | */ |
| 3446 | if (fname == NULL || *fname == NUL) |
| 3447 | { |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 3448 | retval = alloc(MAXPATHL + extlen + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | if (retval == NULL) |
| 3450 | return NULL; |
| 3451 | if (mch_dirname(retval, MAXPATHL) == FAIL || |
| 3452 | (fnamelen = (int)STRLEN(retval)) == 0) |
| 3453 | { |
| 3454 | vim_free(retval); |
| 3455 | return NULL; |
| 3456 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3457 | if (!after_pathsep(retval, retval + fnamelen)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3458 | { |
| 3459 | retval[fnamelen++] = PATHSEP; |
| 3460 | retval[fnamelen] = NUL; |
| 3461 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3462 | prepend_dot = FALSE; /* nothing to prepend a dot to */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3463 | } |
| 3464 | else |
| 3465 | { |
| 3466 | fnamelen = (int)STRLEN(fname); |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 3467 | retval = alloc(fnamelen + extlen + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3468 | if (retval == NULL) |
| 3469 | return NULL; |
| 3470 | STRCPY(retval, fname); |
| 3471 | #ifdef VMS |
| 3472 | vms_remove_version(retval); /* we do not need versions here */ |
| 3473 | #endif |
| 3474 | } |
| 3475 | |
| 3476 | /* |
| 3477 | * search backwards until we hit a '/', '\' or ':' replacing all '.' |
| 3478 | * by '_' for MSDOS or when shortname option set and ext starts with a dot. |
| 3479 | * Then truncate what is after the '/', '\' or ':' to 8 characters for |
| 3480 | * MSDOS and 26 characters for AMIGA, a lot more for UNIX. |
| 3481 | */ |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 3482 | for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3483 | { |
Bram Moolenaar | 00f148d | 2019-02-12 22:37:27 +0100 | [diff] [blame] | 3484 | if (*ext == '.' && shortname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3485 | if (*ptr == '.') /* replace '.' by '_' */ |
| 3486 | *ptr = '_'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3487 | if (vim_ispathsep(*ptr)) |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 3488 | { |
| 3489 | ++ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3490 | break; |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 3491 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3492 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3493 | |
| 3494 | /* the file name has at most BASENAMELEN characters. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3495 | if (STRLEN(ptr) > (unsigned)BASENAMELEN) |
| 3496 | ptr[BASENAMELEN] = '\0'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3497 | |
| 3498 | s = ptr + STRLEN(ptr); |
| 3499 | |
| 3500 | /* |
| 3501 | * For 8.3 file names we may have to reduce the length. |
| 3502 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3503 | if (shortname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3504 | { |
| 3505 | /* |
| 3506 | * If there is no file name, or the file name ends in '/', and the |
| 3507 | * extension starts with '.', put a '_' before the dot, because just |
| 3508 | * ".ext" is invalid. |
| 3509 | */ |
| 3510 | if (fname == NULL || *fname == NUL |
| 3511 | || vim_ispathsep(fname[STRLEN(fname) - 1])) |
| 3512 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3513 | if (*ext == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | *s++ = '_'; |
| 3515 | } |
| 3516 | /* |
| 3517 | * If the extension starts with '.', truncate the base name at 8 |
| 3518 | * characters |
| 3519 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3520 | else if (*ext == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3521 | { |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 3522 | if ((size_t)(s - ptr) > (size_t)8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | { |
| 3524 | s = ptr + 8; |
| 3525 | *s = '\0'; |
| 3526 | } |
| 3527 | } |
| 3528 | /* |
| 3529 | * If the extension doesn't start with '.', and the file name |
| 3530 | * doesn't have an extension yet, append a '.' |
| 3531 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3532 | else if ((e = vim_strchr(ptr, '.')) == NULL) |
| 3533 | *s++ = '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3534 | /* |
| 3535 | * If the extension doesn't start with '.', and there already is an |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 3536 | * extension, it may need to be truncated |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3537 | */ |
| 3538 | else if ((int)STRLEN(e) + extlen > 4) |
| 3539 | s = e + 4 - extlen; |
| 3540 | } |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3541 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3542 | /* |
| 3543 | * If there is no file name, and the extension starts with '.', put a |
| 3544 | * '_' before the dot, because just ".ext" may be invalid if it's on a |
| 3545 | * FAT partition, and on HPFS it doesn't matter. |
| 3546 | */ |
| 3547 | else if ((fname == NULL || *fname == NUL) && *ext == '.') |
| 3548 | *s++ = '_'; |
| 3549 | #endif |
| 3550 | |
| 3551 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 3552 | * Append the extension. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3553 | * ext can start with '.' and cannot exceed 3 more characters. |
| 3554 | */ |
| 3555 | STRCPY(s, ext); |
| 3556 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3557 | /* |
| 3558 | * Prepend the dot. |
| 3559 | */ |
Bram Moolenaar | 00f148d | 2019-02-12 22:37:27 +0100 | [diff] [blame] | 3560 | if (prepend_dot && !shortname && *(e = gettail(retval)) != '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3561 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 3562 | STRMOVE(e + 1, e); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3563 | *e = '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3564 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3565 | |
| 3566 | /* |
| 3567 | * Check that, after appending the extension, the file name is really |
| 3568 | * different. |
| 3569 | */ |
| 3570 | if (fname != NULL && STRCMP(fname, retval) == 0) |
| 3571 | { |
| 3572 | /* we search for a character that can be replaced by '_' */ |
| 3573 | while (--s >= ptr) |
| 3574 | { |
| 3575 | if (*s != '_') |
| 3576 | { |
| 3577 | *s = '_'; |
| 3578 | break; |
| 3579 | } |
| 3580 | } |
| 3581 | if (s < ptr) /* fname was "________.<ext>", how tricky! */ |
| 3582 | *ptr = 'v'; |
| 3583 | } |
| 3584 | return retval; |
| 3585 | } |
| 3586 | |
| 3587 | /* |
| 3588 | * Like fgets(), but if the file line is too long, it is truncated and the |
| 3589 | * rest of the line is thrown away. Returns TRUE for end-of-file. |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 3590 | * If the line is truncated then buf[size - 2] will not be NUL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3591 | */ |
| 3592 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3593 | vim_fgets(char_u *buf, int size, FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3594 | { |
| 3595 | char *eof; |
| 3596 | #define FGETS_SIZE 200 |
| 3597 | char tbuf[FGETS_SIZE]; |
| 3598 | |
| 3599 | buf[size - 2] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3600 | eof = fgets((char *)buf, size, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | if (buf[size - 2] != NUL && buf[size - 2] != '\n') |
| 3602 | { |
| 3603 | buf[size - 1] = NUL; /* Truncate the line */ |
| 3604 | |
| 3605 | /* Now throw away the rest of the line: */ |
| 3606 | do |
| 3607 | { |
| 3608 | tbuf[FGETS_SIZE - 2] = NUL; |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 3609 | vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3610 | } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); |
| 3611 | } |
| 3612 | return (eof == NULL); |
| 3613 | } |
| 3614 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3615 | /* |
| 3616 | * rename() only works if both files are on the same file system, this |
| 3617 | * function will (attempts to?) copy the file across if rename fails -- webb |
| 3618 | * Return -1 for failure, 0 for success. |
| 3619 | */ |
| 3620 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3621 | vim_rename(char_u *from, char_u *to) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3622 | { |
| 3623 | int fd_in; |
| 3624 | int fd_out; |
| 3625 | int n; |
| 3626 | char *errmsg = NULL; |
| 3627 | char *buffer; |
| 3628 | #ifdef AMIGA |
| 3629 | BPTR flock; |
| 3630 | #endif |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3631 | stat_T st; |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 3632 | long perm; |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 3633 | #ifdef HAVE_ACL |
| 3634 | vim_acl_T acl; /* ACL from original file */ |
| 3635 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3636 | int use_tmp_file = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3637 | |
| 3638 | /* |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3639 | * When the names are identical, there is nothing to do. When they refer |
| 3640 | * to the same file (ignoring case and slash/backslash differences) but |
| 3641 | * the file name differs we need to go through a temp file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3642 | */ |
| 3643 | if (fnamecmp(from, to) == 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3644 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3645 | if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3646 | use_tmp_file = TRUE; |
| 3647 | else |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3648 | return 0; |
| 3649 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3650 | |
| 3651 | /* |
| 3652 | * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. |
| 3653 | */ |
| 3654 | if (mch_stat((char *)from, &st) < 0) |
| 3655 | return -1; |
| 3656 | |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3657 | #ifdef UNIX |
| 3658 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3659 | stat_T st_to; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3660 | |
| 3661 | /* It's possible for the source and destination to be the same file. |
| 3662 | * This happens when "from" and "to" differ in case and are on a FAT32 |
| 3663 | * filesystem. In that case go through a temp file name. */ |
| 3664 | if (mch_stat((char *)to, &st_to) >= 0 |
| 3665 | && st.st_dev == st_to.st_dev |
| 3666 | && st.st_ino == st_to.st_ino) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3667 | use_tmp_file = TRUE; |
| 3668 | } |
| 3669 | #endif |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3670 | #ifdef MSWIN |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3671 | { |
| 3672 | BY_HANDLE_FILE_INFORMATION info1, info2; |
| 3673 | |
| 3674 | /* It's possible for the source and destination to be the same file. |
| 3675 | * In that case go through a temp file name. This makes rename("foo", |
| 3676 | * "./foo") a no-op (in a complicated way). */ |
| 3677 | if (win32_fileinfo(from, &info1) == FILEINFO_OK |
| 3678 | && win32_fileinfo(to, &info2) == FILEINFO_OK |
| 3679 | && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber |
| 3680 | && info1.nFileIndexHigh == info2.nFileIndexHigh |
| 3681 | && info1.nFileIndexLow == info2.nFileIndexLow) |
| 3682 | use_tmp_file = TRUE; |
| 3683 | } |
| 3684 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3685 | |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3686 | if (use_tmp_file) |
| 3687 | { |
| 3688 | char tempname[MAXPATHL + 1]; |
| 3689 | |
| 3690 | /* |
| 3691 | * Find a name that doesn't exist and is in the same directory. |
| 3692 | * Rename "from" to "tempname" and then rename "tempname" to "to". |
| 3693 | */ |
| 3694 | if (STRLEN(from) >= MAXPATHL - 5) |
| 3695 | return -1; |
| 3696 | STRCPY(tempname, from); |
| 3697 | for (n = 123; n < 99999; ++n) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3698 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3699 | sprintf((char *)gettail((char_u *)tempname), "%d", n); |
| 3700 | if (mch_stat(tempname, &st) < 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3701 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3702 | if (mch_rename((char *)from, tempname) == 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3703 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3704 | if (mch_rename(tempname, (char *)to) == 0) |
| 3705 | return 0; |
| 3706 | /* Strange, the second step failed. Try moving the |
| 3707 | * file back and return failure. */ |
| 3708 | mch_rename(tempname, (char *)from); |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3709 | return -1; |
| 3710 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3711 | /* If it fails for one temp name it will most likely fail |
| 3712 | * for any temp name, give up. */ |
| 3713 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3714 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3715 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 3716 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3717 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 3718 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3719 | /* |
| 3720 | * Delete the "to" file, this is required on some systems to make the |
| 3721 | * mch_rename() work, on other systems it makes sure that we don't have |
| 3722 | * two files when the mch_rename() fails. |
| 3723 | */ |
| 3724 | |
| 3725 | #ifdef AMIGA |
| 3726 | /* |
| 3727 | * With MSDOS-compatible filesystems (crossdos, messydos) it is possible |
| 3728 | * that the name of the "to" file is the same as the "from" file, even |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 3729 | * though the names are different. To avoid the chance of accidentally |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3730 | * deleting the "from" file (horror!) we lock it during the remove. |
| 3731 | * |
| 3732 | * When used for making a backup before writing the file: This should not |
| 3733 | * happen with ":w", because startscript() should detect this problem and |
| 3734 | * set buf->b_shortname, causing modname() to return a correct ".bak" file |
| 3735 | * name. This problem does exist with ":w filename", but then the |
| 3736 | * original file will be somewhere else so the backup isn't really |
| 3737 | * important. If autoscripting is off the rename may fail. |
| 3738 | */ |
| 3739 | flock = Lock((UBYTE *)from, (long)ACCESS_READ); |
| 3740 | #endif |
| 3741 | mch_remove(to); |
| 3742 | #ifdef AMIGA |
| 3743 | if (flock) |
| 3744 | UnLock(flock); |
| 3745 | #endif |
| 3746 | |
| 3747 | /* |
| 3748 | * First try a normal rename, return if it works. |
| 3749 | */ |
| 3750 | if (mch_rename((char *)from, (char *)to) == 0) |
| 3751 | return 0; |
| 3752 | |
| 3753 | /* |
| 3754 | * Rename() failed, try copying the file. |
| 3755 | */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 3756 | perm = mch_getperm(from); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 3757 | #ifdef HAVE_ACL |
| 3758 | /* For systems that support ACL: get the ACL from the original file. */ |
| 3759 | acl = mch_get_acl(from); |
| 3760 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3761 | fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
| 3762 | if (fd_in == -1) |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 3763 | { |
| 3764 | #ifdef HAVE_ACL |
| 3765 | mch_free_acl(acl); |
| 3766 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | return -1; |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 3768 | } |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 3769 | |
| 3770 | /* Create the new file with same permissions as the original. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3771 | fd_out = mch_open((char *)to, |
| 3772 | O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | if (fd_out == -1) |
| 3774 | { |
| 3775 | close(fd_in); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 3776 | #ifdef HAVE_ACL |
| 3777 | mch_free_acl(acl); |
| 3778 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3779 | return -1; |
| 3780 | } |
| 3781 | |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3782 | buffer = alloc(WRITEBUFSIZE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3783 | if (buffer == NULL) |
| 3784 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3785 | close(fd_out); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 3786 | close(fd_in); |
| 3787 | #ifdef HAVE_ACL |
| 3788 | mch_free_acl(acl); |
| 3789 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3790 | return -1; |
| 3791 | } |
| 3792 | |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 3793 | while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0) |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 3794 | if (write_eintr(fd_out, buffer, n) != n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3795 | { |
| 3796 | errmsg = _("E208: Error writing to \"%s\""); |
| 3797 | break; |
| 3798 | } |
| 3799 | |
| 3800 | vim_free(buffer); |
| 3801 | close(fd_in); |
| 3802 | if (close(fd_out) < 0) |
| 3803 | errmsg = _("E209: Error closing \"%s\""); |
| 3804 | if (n < 0) |
| 3805 | { |
| 3806 | errmsg = _("E210: Error reading \"%s\""); |
| 3807 | to = from; |
| 3808 | } |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 3809 | #ifndef UNIX /* for Unix mch_open() already set the permission */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 3810 | mch_setperm(to, perm); |
Bram Moolenaar | c6039d8 | 2005-12-02 00:44:04 +0000 | [diff] [blame] | 3811 | #endif |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 3812 | #ifdef HAVE_ACL |
| 3813 | mch_set_acl(to, acl); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 3814 | mch_free_acl(acl); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 3815 | #endif |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3816 | #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
Bram Moolenaar | e874744 | 2013-11-12 18:09:29 +0100 | [diff] [blame] | 3817 | mch_copy_sec(from, to); |
Bram Moolenaar | 0671de3 | 2013-11-12 05:12:03 +0100 | [diff] [blame] | 3818 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3819 | if (errmsg != NULL) |
| 3820 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3821 | semsg(errmsg, to); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3822 | return -1; |
| 3823 | } |
| 3824 | mch_remove(from); |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
| 3828 | static int already_warned = FALSE; |
| 3829 | |
| 3830 | /* |
| 3831 | * Check if any not hidden buffer has been changed. |
| 3832 | * Postpone the check if there are characters in the stuff buffer, a global |
| 3833 | * command is being executed, a mapping is being executed or an autocommand is |
| 3834 | * busy. |
| 3835 | * Returns TRUE if some message was written (screen should be redrawn and |
| 3836 | * cursor positioned). |
| 3837 | */ |
| 3838 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3839 | check_timestamps( |
| 3840 | int focus) /* called for GUI focus event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | { |
| 3842 | buf_T *buf; |
| 3843 | int didit = 0; |
| 3844 | int n; |
| 3845 | |
| 3846 | /* Don't check timestamps while system() or another low-level function may |
| 3847 | * cause us to lose and gain focus. */ |
| 3848 | if (no_check_timestamps > 0) |
| 3849 | return FALSE; |
| 3850 | |
| 3851 | /* Avoid doing a check twice. The OK/Reload dialog can cause a focus |
| 3852 | * event and we would keep on checking if the file is steadily growing. |
| 3853 | * Do check again after typing something. */ |
| 3854 | if (focus && did_check_timestamps) |
| 3855 | { |
| 3856 | need_check_timestamps = TRUE; |
| 3857 | return FALSE; |
| 3858 | } |
| 3859 | |
| 3860 | if (!stuff_empty() || global_busy || !typebuf_typed() |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 3861 | || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3862 | need_check_timestamps = TRUE; /* check later */ |
| 3863 | else |
| 3864 | { |
| 3865 | ++no_wait_return; |
| 3866 | did_check_timestamps = TRUE; |
| 3867 | already_warned = FALSE; |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3868 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | { |
| 3870 | /* Only check buffers in a window. */ |
| 3871 | if (buf->b_nwindows > 0) |
| 3872 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3873 | bufref_T bufref; |
| 3874 | |
| 3875 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3876 | n = buf_check_timestamp(buf, focus); |
| 3877 | if (didit < n) |
| 3878 | didit = n; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3879 | if (n > 0 && !bufref_valid(&bufref)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3880 | { |
| 3881 | /* Autocommands have removed the buffer, start at the |
| 3882 | * first one again. */ |
| 3883 | buf = firstbuf; |
| 3884 | continue; |
| 3885 | } |
| 3886 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3887 | } |
| 3888 | --no_wait_return; |
| 3889 | need_check_timestamps = FALSE; |
| 3890 | if (need_wait_return && didit == 2) |
| 3891 | { |
| 3892 | /* make sure msg isn't overwritten */ |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3893 | msg_puts("\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | out_flush(); |
| 3895 | } |
| 3896 | } |
| 3897 | return didit; |
| 3898 | } |
| 3899 | |
| 3900 | /* |
| 3901 | * Move all the lines from buffer "frombuf" to buffer "tobuf". |
| 3902 | * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not |
| 3903 | * empty. |
| 3904 | */ |
| 3905 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3906 | move_lines(buf_T *frombuf, buf_T *tobuf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3907 | { |
| 3908 | buf_T *tbuf = curbuf; |
| 3909 | int retval = OK; |
| 3910 | linenr_T lnum; |
| 3911 | char_u *p; |
| 3912 | |
| 3913 | /* Copy the lines in "frombuf" to "tobuf". */ |
| 3914 | curbuf = tobuf; |
| 3915 | for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) |
| 3916 | { |
| 3917 | p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); |
| 3918 | if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) |
| 3919 | { |
| 3920 | vim_free(p); |
| 3921 | retval = FAIL; |
| 3922 | break; |
| 3923 | } |
| 3924 | vim_free(p); |
| 3925 | } |
| 3926 | |
| 3927 | /* Delete all the lines in "frombuf". */ |
| 3928 | if (retval != FAIL) |
| 3929 | { |
| 3930 | curbuf = frombuf; |
Bram Moolenaar | 9460b9d | 2007-01-09 14:37:01 +0000 | [diff] [blame] | 3931 | for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
| 3932 | if (ml_delete(lnum, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3933 | { |
| 3934 | /* Oops! We could try putting back the saved lines, but that |
| 3935 | * might fail again... */ |
| 3936 | retval = FAIL; |
| 3937 | break; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | curbuf = tbuf; |
| 3942 | return retval; |
| 3943 | } |
| 3944 | |
| 3945 | /* |
| 3946 | * Check if buffer "buf" has been changed. |
| 3947 | * Also check if the file for a new buffer unexpectedly appeared. |
| 3948 | * return 1 if a changed buffer was found. |
| 3949 | * return 2 if a message has been displayed. |
| 3950 | * return 0 otherwise. |
| 3951 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3953 | buf_check_timestamp( |
| 3954 | buf_T *buf, |
| 3955 | int focus UNUSED) /* called for GUI focus event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3956 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3957 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3958 | int stat_res; |
| 3959 | int retval = 0; |
| 3960 | char_u *path; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3961 | char *tbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3962 | char *mesg = NULL; |
Bram Moolenaar | 44ecf65 | 2005-03-07 23:09:59 +0000 | [diff] [blame] | 3963 | char *mesg2 = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | int helpmesg = FALSE; |
| 3965 | int reload = FALSE; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3966 | char *reason; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3967 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 3968 | int can_reload = FALSE; |
| 3969 | #endif |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3970 | off_T orig_size = buf->b_orig_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3971 | int orig_mode = buf->b_orig_mode; |
| 3972 | #ifdef FEAT_GUI |
| 3973 | int save_mouse_correct = need_mouse_correct; |
| 3974 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3975 | static int busy = FALSE; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3976 | int n; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 3977 | #ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3978 | char_u *s; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 3979 | #endif |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3980 | bufref_T bufref; |
| 3981 | |
| 3982 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | |
| 3984 | /* If there is no file name, the buffer is not loaded, 'buftype' is |
| 3985 | * set, we are in the middle of a save or being called recursively: ignore |
| 3986 | * this buffer. */ |
| 3987 | if (buf->b_ffname == NULL |
| 3988 | || buf->b_ml.ml_mfp == NULL |
Bram Moolenaar | 91335e5 | 2018-08-01 17:53:12 +0200 | [diff] [blame] | 3989 | || !bt_normal(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3990 | || buf->b_saving |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3991 | || busy |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3992 | #ifdef FEAT_NETBEANS_INTG |
| 3993 | || isNetbeansBuffer(buf) |
| 3994 | #endif |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 3995 | #ifdef FEAT_TERMINAL |
| 3996 | || buf->b_term != NULL |
| 3997 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3998 | ) |
| 3999 | return 0; |
| 4000 | |
| 4001 | if ( !(buf->b_flags & BF_NOTEDITED) |
| 4002 | && buf->b_mtime != 0 |
| 4003 | && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 |
| 4004 | || time_differs((long)st.st_mtime, buf->b_mtime) |
Bram Moolenaar | a7611f6 | 2014-05-02 15:46:14 +0200 | [diff] [blame] | 4005 | || st.st_size != buf->b_orig_size |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4006 | #ifdef HAVE_ST_MODE |
| 4007 | || (int)st.st_mode != buf->b_orig_mode |
| 4008 | #else |
| 4009 | || mch_getperm(buf->b_ffname) != buf->b_orig_mode |
| 4010 | #endif |
| 4011 | )) |
| 4012 | { |
Bram Moolenaar | 674e2bd | 2019-07-31 20:21:01 +0200 | [diff] [blame] | 4013 | long prev_b_mtime = buf->b_mtime; |
| 4014 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4015 | retval = 1; |
| 4016 | |
Bram Moolenaar | 386bc82 | 2018-07-07 18:34:12 +0200 | [diff] [blame] | 4017 | // set b_mtime to stop further warnings (e.g., when executing |
| 4018 | // FileChangedShell autocmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4019 | if (stat_res < 0) |
| 4020 | { |
Bram Moolenaar | 8239c62 | 2019-05-24 16:46:01 +0200 | [diff] [blame] | 4021 | // Check the file again later to see if it re-appears. |
| 4022 | buf->b_mtime = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4023 | buf->b_orig_size = 0; |
| 4024 | buf->b_orig_mode = 0; |
| 4025 | } |
| 4026 | else |
| 4027 | buf_store_time(buf, &st, buf->b_ffname); |
| 4028 | |
| 4029 | /* Don't do anything for a directory. Might contain the file |
| 4030 | * explorer. */ |
| 4031 | if (mch_isdir(buf->b_fname)) |
| 4032 | ; |
| 4033 | |
| 4034 | /* |
| 4035 | * If 'autoread' is set, the buffer has no changes and the file still |
| 4036 | * exists, reload the buffer. Use the buffer-local option value if it |
| 4037 | * was set, the global option value otherwise. |
| 4038 | */ |
| 4039 | else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) |
| 4040 | && !bufIsChanged(buf) && stat_res >= 0) |
| 4041 | reload = TRUE; |
| 4042 | else |
| 4043 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4044 | if (stat_res < 0) |
| 4045 | reason = "deleted"; |
| 4046 | else if (bufIsChanged(buf)) |
| 4047 | reason = "conflict"; |
Bram Moolenaar | 3e460fd | 2019-01-26 16:21:07 +0100 | [diff] [blame] | 4048 | /* |
| 4049 | * Check if the file contents really changed to avoid giving a |
| 4050 | * warning when only the timestamp was set (e.g., checked out of |
| 4051 | * CVS). Always warn when the buffer was changed. |
| 4052 | */ |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4053 | else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) |
| 4054 | reason = "changed"; |
| 4055 | else if (orig_mode != buf->b_orig_mode) |
| 4056 | reason = "mode"; |
| 4057 | else |
| 4058 | reason = "time"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4059 | |
| 4060 | /* |
| 4061 | * Only give the warning if there are no FileChangedShell |
| 4062 | * autocommands. |
| 4063 | * Avoid being called recursively by setting "busy". |
| 4064 | */ |
| 4065 | busy = TRUE; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4066 | #ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4067 | set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
| 4068 | set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4069 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 4070 | ++allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4071 | n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
| 4072 | buf->b_fname, buf->b_fname, FALSE, buf); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 4073 | --allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4074 | busy = FALSE; |
| 4075 | if (n) |
| 4076 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 4077 | if (!bufref_valid(&bufref)) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4078 | emsg(_("E246: FileChangedShell autocommand deleted buffer")); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4079 | #ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4080 | s = get_vim_var_str(VV_FCS_CHOICE); |
| 4081 | if (STRCMP(s, "reload") == 0 && *reason != 'd') |
| 4082 | reload = TRUE; |
| 4083 | else if (STRCMP(s, "ask") == 0) |
| 4084 | n = FALSE; |
| 4085 | else |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4086 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4087 | return 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4088 | } |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4089 | if (!n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4090 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4091 | if (*reason == 'd') |
Bram Moolenaar | 674e2bd | 2019-07-31 20:21:01 +0200 | [diff] [blame] | 4092 | { |
| 4093 | // Only give the message once. |
| 4094 | if (prev_b_mtime != -1) |
| 4095 | mesg = _("E211: File \"%s\" no longer available"); |
| 4096 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4097 | else |
| 4098 | { |
| 4099 | helpmesg = TRUE; |
| 4100 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 4101 | can_reload = TRUE; |
| 4102 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4103 | if (reason[2] == 'n') |
| 4104 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4105 | mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4106 | mesg2 = _("See \":help W12\" for more info."); |
| 4107 | } |
| 4108 | else if (reason[1] == 'h') |
| 4109 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4110 | mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4111 | mesg2 = _("See \":help W11\" for more info."); |
| 4112 | } |
| 4113 | else if (*reason == 'm') |
| 4114 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4115 | mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4116 | mesg2 = _("See \":help W16\" for more info."); |
| 4117 | } |
Bram Moolenaar | 85388b5 | 2009-06-24 09:58:32 +0000 | [diff] [blame] | 4118 | else |
| 4119 | /* Only timestamp changed, store it to avoid a warning |
| 4120 | * in check_mtime() later. */ |
| 4121 | buf->b_mtime_read = buf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | |
| 4126 | } |
| 4127 | else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) |
| 4128 | && vim_fexists(buf->b_ffname)) |
| 4129 | { |
| 4130 | retval = 1; |
| 4131 | mesg = _("W13: Warning: File \"%s\" has been created after editing started"); |
| 4132 | buf->b_flags |= BF_NEW_W; |
| 4133 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 4134 | can_reload = TRUE; |
| 4135 | #endif |
| 4136 | } |
| 4137 | |
| 4138 | if (mesg != NULL) |
| 4139 | { |
| 4140 | path = home_replace_save(buf, buf->b_fname); |
| 4141 | if (path != NULL) |
| 4142 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4143 | if (!helpmesg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | mesg2 = ""; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 4145 | tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4146 | sprintf(tbuf, mesg, path); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 4147 | #ifdef FEAT_EVAL |
| 4148 | /* Set warningmsg here, before the unimportant and output-specific |
| 4149 | * mesg2 has been appended. */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4150 | set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 4151 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4152 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 4153 | if (can_reload) |
| 4154 | { |
| 4155 | if (*mesg2 != NUL) |
| 4156 | { |
| 4157 | STRCAT(tbuf, "\n"); |
| 4158 | STRCAT(tbuf, mesg2); |
| 4159 | } |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4160 | if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), |
| 4161 | (char_u *)tbuf, |
Bram Moolenaar | d2c340a | 2011-01-17 20:08:11 +0100 | [diff] [blame] | 4162 | (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4163 | reload = TRUE; |
| 4164 | } |
| 4165 | else |
| 4166 | #endif |
| 4167 | if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) |
| 4168 | { |
| 4169 | if (*mesg2 != NUL) |
| 4170 | { |
| 4171 | STRCAT(tbuf, "; "); |
| 4172 | STRCAT(tbuf, mesg2); |
| 4173 | } |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4174 | emsg(tbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4175 | retval = 2; |
| 4176 | } |
| 4177 | else |
| 4178 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | if (!autocmd_busy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4180 | { |
| 4181 | msg_start(); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4182 | msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4183 | if (*mesg2 != NUL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4184 | msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4185 | msg_clr_eos(); |
| 4186 | (void)msg_end(); |
| 4187 | if (emsg_silent == 0) |
| 4188 | { |
| 4189 | out_flush(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4190 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4191 | if (!focus) |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4192 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4193 | /* give the user some time to think about it */ |
| 4194 | ui_delay(1000L, TRUE); |
| 4195 | |
| 4196 | /* don't redraw and erase the message */ |
| 4197 | redraw_cmdline = FALSE; |
| 4198 | } |
| 4199 | } |
| 4200 | already_warned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | vim_free(path); |
| 4204 | vim_free(tbuf); |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | if (reload) |
Bram Moolenaar | 465748e | 2012-08-29 18:50:54 +0200 | [diff] [blame] | 4209 | { |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4210 | /* Reload the buffer. */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 4211 | buf_reload(buf, orig_mode); |
Bram Moolenaar | 465748e | 2012-08-29 18:50:54 +0200 | [diff] [blame] | 4212 | #ifdef FEAT_PERSISTENT_UNDO |
| 4213 | if (buf->b_p_udf && buf->b_ffname != NULL) |
| 4214 | { |
| 4215 | char_u hash[UNDO_HASH_SIZE]; |
| 4216 | buf_T *save_curbuf = curbuf; |
| 4217 | |
| 4218 | /* Any existing undo file is unusable, write it now. */ |
| 4219 | curbuf = buf; |
| 4220 | u_compute_hash(hash); |
| 4221 | u_write_undo(NULL, FALSE, buf, hash); |
| 4222 | curbuf = save_curbuf; |
| 4223 | } |
| 4224 | #endif |
| 4225 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4226 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4227 | /* Trigger FileChangedShell when the file was changed in any way. */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 4228 | if (bufref_valid(&bufref) && retval != 0) |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 4229 | (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
| 4230 | buf->b_fname, buf->b_fname, FALSE, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4231 | #ifdef FEAT_GUI |
| 4232 | /* restore this in case an autocommand has set it; it would break |
| 4233 | * 'mousefocus' */ |
| 4234 | need_mouse_correct = save_mouse_correct; |
| 4235 | #endif |
| 4236 | |
| 4237 | return retval; |
| 4238 | } |
| 4239 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4240 | /* |
| 4241 | * Reload a buffer that is already loaded. |
| 4242 | * Used when the file was changed outside of Vim. |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 4243 | * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
| 4244 | * buf->b_orig_mode may have been reset already. |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4245 | */ |
| 4246 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4247 | buf_reload(buf_T *buf, int orig_mode) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4248 | { |
| 4249 | exarg_T ea; |
| 4250 | pos_T old_cursor; |
| 4251 | linenr_T old_topline; |
| 4252 | int old_ro = buf->b_p_ro; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4253 | buf_T *savebuf; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 4254 | bufref_T bufref; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4255 | int saved = OK; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4256 | aco_save_T aco; |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 4257 | int flags = READ_NEW; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4258 | |
| 4259 | /* set curwin/curbuf for "buf" and save some things */ |
| 4260 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4261 | |
| 4262 | /* We only want to read the text from the file, not reset the syntax |
| 4263 | * highlighting, clear marks, diff status, etc. Force the fileformat |
| 4264 | * and encoding to be the same. */ |
| 4265 | if (prep_exarg(&ea, buf) == OK) |
| 4266 | { |
| 4267 | old_cursor = curwin->w_cursor; |
| 4268 | old_topline = curwin->w_topline; |
| 4269 | |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 4270 | if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 4271 | { |
| 4272 | /* Save all the text, so that the reload can be undone. |
| 4273 | * Sync first so that this is a separate undo-able action. */ |
| 4274 | u_sync(FALSE); |
| 4275 | saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); |
| 4276 | flags |= READ_KEEP_UNDO; |
| 4277 | } |
| 4278 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4279 | /* |
| 4280 | * To behave like when a new file is edited (matters for |
| 4281 | * BufReadPost autocommands) we first need to delete the current |
| 4282 | * buffer contents. But if reading the file fails we should keep |
| 4283 | * the old contents. Can't use memory only, the file might be |
| 4284 | * too big. Use a hidden buffer to move the buffer contents to. |
| 4285 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4286 | if (BUFEMPTY() || saved == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4287 | savebuf = NULL; |
| 4288 | else |
| 4289 | { |
| 4290 | /* Allocate a buffer without putting it in the buffer list. */ |
| 4291 | savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 4292 | set_bufref(&bufref, savebuf); |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4293 | if (savebuf != NULL && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4294 | { |
| 4295 | /* Open the memline. */ |
| 4296 | curbuf = savebuf; |
| 4297 | curwin->w_buffer = savebuf; |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4298 | saved = ml_open(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4299 | curbuf = buf; |
| 4300 | curwin->w_buffer = buf; |
| 4301 | } |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4302 | if (savebuf == NULL || saved == FAIL || buf != curbuf |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4303 | || move_lines(buf, savebuf) == FAIL) |
| 4304 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4305 | semsg(_("E462: Could not prepare for reloading \"%s\""), |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4306 | buf->b_fname); |
| 4307 | saved = FAIL; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | if (saved == OK) |
| 4312 | { |
| 4313 | curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4314 | keep_filetype = TRUE; /* don't detect 'filetype' */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4315 | if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
| 4316 | (linenr_T)0, |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 4317 | (linenr_T)MAXLNUM, &ea, flags) != OK) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4318 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4319 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4320 | if (!aborting()) |
| 4321 | #endif |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4322 | semsg(_("E321: Could not reload \"%s\""), buf->b_fname); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 4323 | if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4324 | { |
| 4325 | /* Put the text back from the save buffer. First |
| 4326 | * delete any lines that readfile() added. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4327 | while (!BUFEMPTY()) |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4328 | if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4329 | break; |
| 4330 | (void)move_lines(savebuf, buf); |
| 4331 | } |
| 4332 | } |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 4333 | else if (buf == curbuf) /* "buf" still valid */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4334 | { |
| 4335 | /* Mark the buffer as unmodified and free undo info. */ |
Bram Moolenaar | c024b46 | 2019-06-08 18:07:21 +0200 | [diff] [blame] | 4336 | unchanged(buf, TRUE, TRUE); |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 4337 | if ((flags & READ_KEEP_UNDO) == 0) |
| 4338 | { |
| 4339 | u_blockfree(buf); |
| 4340 | u_clearall(buf); |
| 4341 | } |
| 4342 | else |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 4343 | { |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 4344 | /* Mark all undo states as changed. */ |
| 4345 | u_unchanged(curbuf); |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 4346 | } |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4347 | } |
| 4348 | } |
| 4349 | vim_free(ea.cmd); |
| 4350 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 4351 | if (savebuf != NULL && bufref_valid(&bufref)) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4352 | wipe_buffer(savebuf, FALSE); |
| 4353 | |
| 4354 | #ifdef FEAT_DIFF |
| 4355 | /* Invalidate diff info if necessary. */ |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4356 | diff_invalidate(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4357 | #endif |
| 4358 | |
| 4359 | /* Restore the topline and cursor position and check it (lines may |
| 4360 | * have been removed). */ |
| 4361 | if (old_topline > curbuf->b_ml.ml_line_count) |
| 4362 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 4363 | else |
| 4364 | curwin->w_topline = old_topline; |
| 4365 | curwin->w_cursor = old_cursor; |
| 4366 | check_cursor(); |
| 4367 | update_topline(); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4368 | keep_filetype = FALSE; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4369 | #ifdef FEAT_FOLDING |
| 4370 | { |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 4371 | win_T *wp; |
| 4372 | tabpage_T *tp; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4373 | |
| 4374 | /* Update folds unless they are defined manually. */ |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 4375 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4376 | if (wp->w_buffer == curwin->w_buffer |
| 4377 | && !foldmethodIsManual(wp)) |
| 4378 | foldUpdateAll(wp); |
| 4379 | } |
| 4380 | #endif |
| 4381 | /* If the mode didn't change and 'readonly' was set, keep the old |
| 4382 | * value; the user probably used the ":view" command. But don't |
| 4383 | * reset it, might have had a read error. */ |
| 4384 | if (orig_mode == curbuf->b_orig_mode) |
| 4385 | curbuf->b_p_ro |= old_ro; |
Bram Moolenaar | 52f85b7 | 2013-01-30 14:13:56 +0100 | [diff] [blame] | 4386 | |
| 4387 | /* Modelines must override settings done by autocommands. */ |
| 4388 | do_modelines(0); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4391 | /* restore curwin/curbuf and a few other things */ |
| 4392 | aucmd_restbuf(&aco); |
| 4393 | /* Careful: autocommands may have made "buf" invalid! */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | void |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4397 | buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4398 | { |
| 4399 | buf->b_mtime = (long)st->st_mtime; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 4400 | buf->b_orig_size = st->st_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4401 | #ifdef HAVE_ST_MODE |
| 4402 | buf->b_orig_mode = (int)st->st_mode; |
| 4403 | #else |
| 4404 | buf->b_orig_mode = mch_getperm(fname); |
| 4405 | #endif |
| 4406 | } |
| 4407 | |
| 4408 | /* |
| 4409 | * Adjust the line with missing eol, used for the next write. |
| 4410 | * Used for do_filter(), when the input lines for the filter are deleted. |
| 4411 | */ |
| 4412 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4413 | write_lnum_adjust(linenr_T offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4414 | { |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4415 | if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
| 4416 | curbuf->b_no_eol_lnum += offset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4419 | #if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO) |
| 4420 | /* |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4421 | * Core part of "readdir()" function. |
| 4422 | * Retrieve the list of files/directories of "path" into "gap". |
| 4423 | * Return OK for success, FAIL for failure. |
| 4424 | */ |
| 4425 | int |
| 4426 | readdir_core( |
| 4427 | garray_T *gap, |
| 4428 | char_u *path, |
| 4429 | void *context, |
| 4430 | int (*checkitem)(void *context, char_u *name)) |
| 4431 | { |
| 4432 | int failed = FALSE; |
| 4433 | #ifdef MSWIN |
| 4434 | char_u *buf, *p; |
| 4435 | int ok; |
| 4436 | HANDLE hFind = INVALID_HANDLE_VALUE; |
| 4437 | WIN32_FIND_DATAW wfb; |
| 4438 | WCHAR *wn = NULL; // UTF-16 name, NULL when not used. |
| 4439 | #endif |
| 4440 | |
| 4441 | ga_init2(gap, (int)sizeof(char *), 20); |
| 4442 | |
| 4443 | #ifdef MSWIN |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 4444 | buf = alloc(MAXPATHL); |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4445 | if (buf == NULL) |
| 4446 | return FAIL; |
| 4447 | STRNCPY(buf, path, MAXPATHL-5); |
Bram Moolenaar | 71de720 | 2019-05-24 19:04:29 +0200 | [diff] [blame] | 4448 | p = buf + STRLEN(buf); |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4449 | MB_PTR_BACK(buf, p); |
| 4450 | if (*p == '\\' || *p == '/') |
| 4451 | *p = NUL; |
| 4452 | STRCAT(buf, "\\*"); |
| 4453 | |
| 4454 | wn = enc_to_utf16(buf, NULL); |
| 4455 | if (wn != NULL) |
| 4456 | hFind = FindFirstFileW(wn, &wfb); |
| 4457 | ok = (hFind != INVALID_HANDLE_VALUE); |
| 4458 | if (!ok) |
| 4459 | { |
| 4460 | failed = TRUE; |
| 4461 | smsg(_(e_notopen), path); |
| 4462 | } |
| 4463 | else |
| 4464 | { |
| 4465 | while (ok) |
| 4466 | { |
| 4467 | int ignore; |
| 4468 | |
| 4469 | p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here |
| 4470 | if (p == NULL) |
| 4471 | break; // out of memory |
| 4472 | |
| 4473 | ignore = p[0] == '.' && (p[1] == NUL |
| 4474 | || (p[1] == '.' && p[2] == NUL)); |
| 4475 | if (!ignore && checkitem != NULL) |
| 4476 | { |
| 4477 | int r = checkitem(context, p); |
| 4478 | |
| 4479 | if (r < 0) |
| 4480 | { |
| 4481 | vim_free(p); |
| 4482 | break; |
| 4483 | } |
| 4484 | if (r == 0) |
| 4485 | ignore = TRUE; |
| 4486 | } |
| 4487 | |
| 4488 | if (!ignore) |
| 4489 | { |
| 4490 | if (ga_grow(gap, 1) == OK) |
| 4491 | ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p); |
| 4492 | else |
| 4493 | { |
| 4494 | failed = TRUE; |
| 4495 | vim_free(p); |
| 4496 | break; |
| 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | vim_free(p); |
| 4501 | ok = FindNextFileW(hFind, &wfb); |
| 4502 | } |
| 4503 | FindClose(hFind); |
| 4504 | } |
| 4505 | |
| 4506 | vim_free(buf); |
| 4507 | vim_free(wn); |
| 4508 | #else |
| 4509 | DIR *dirp; |
| 4510 | struct dirent *dp; |
| 4511 | char_u *p; |
| 4512 | |
| 4513 | dirp = opendir((char *)path); |
| 4514 | if (dirp == NULL) |
| 4515 | { |
| 4516 | failed = TRUE; |
| 4517 | smsg(_(e_notopen), path); |
| 4518 | } |
| 4519 | else |
| 4520 | { |
| 4521 | for (;;) |
| 4522 | { |
| 4523 | int ignore; |
| 4524 | |
| 4525 | dp = readdir(dirp); |
| 4526 | if (dp == NULL) |
| 4527 | break; |
| 4528 | p = (char_u *)dp->d_name; |
| 4529 | |
| 4530 | ignore = p[0] == '.' && |
| 4531 | (p[1] == NUL || |
| 4532 | (p[1] == '.' && p[2] == NUL)); |
| 4533 | if (!ignore && checkitem != NULL) |
| 4534 | { |
| 4535 | int r = checkitem(context, p); |
| 4536 | |
| 4537 | if (r < 0) |
| 4538 | break; |
| 4539 | if (r == 0) |
| 4540 | ignore = TRUE; |
| 4541 | } |
| 4542 | |
| 4543 | if (!ignore) |
| 4544 | { |
| 4545 | if (ga_grow(gap, 1) == OK) |
| 4546 | ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p); |
| 4547 | else |
| 4548 | { |
| 4549 | failed = TRUE; |
| 4550 | break; |
| 4551 | } |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | closedir(dirp); |
| 4556 | } |
| 4557 | #endif |
| 4558 | |
| 4559 | if (!failed && gap->ga_len > 0) |
| 4560 | sort_strings((char_u **)gap->ga_data, gap->ga_len); |
| 4561 | |
| 4562 | return failed ? FAIL : OK; |
| 4563 | } |
| 4564 | |
| 4565 | /* |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4566 | * Delete "name" and everything in it, recursively. |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4567 | * return 0 for success, -1 if some file was not deleted. |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4568 | */ |
| 4569 | int |
| 4570 | delete_recursive(char_u *name) |
| 4571 | { |
| 4572 | int result = 0; |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4573 | int i; |
| 4574 | char_u *exp; |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4575 | garray_T ga; |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4576 | |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4577 | // A symbolic link to a directory itself is deleted, not the directory it |
| 4578 | // points to. |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 4579 | if ( |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4580 | # if defined(UNIX) || defined(MSWIN) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 4581 | mch_isrealdir(name) |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 4582 | # else |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 4583 | mch_isdir(name) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 4584 | # endif |
| 4585 | ) |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4586 | { |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4587 | exp = vim_strsave(name); |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4588 | if (exp == NULL) |
| 4589 | return -1; |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4590 | if (readdir_core(&ga, exp, NULL, NULL) == OK) |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4591 | { |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4592 | for (i = 0; i < ga.ga_len; ++i) |
| 4593 | { |
| 4594 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp, |
| 4595 | ((char_u **)ga.ga_data)[i]); |
| 4596 | if (delete_recursive(NameBuff) != 0) |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4597 | result = -1; |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4598 | } |
| 4599 | ga_clear_strings(&ga); |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4600 | } |
| 4601 | else |
| 4602 | result = -1; |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 4603 | (void)mch_rmdir(exp); |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4604 | vim_free(exp); |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4605 | } |
| 4606 | else |
| 4607 | result = mch_remove(name) == 0 ? 0 : -1; |
| 4608 | |
| 4609 | return result; |
| 4610 | } |
| 4611 | #endif |
| 4612 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4613 | #if defined(TEMPDIRNAMES) || defined(PROTO) |
| 4614 | static long temp_count = 0; /* Temp filename counter. */ |
| 4615 | |
| 4616 | /* |
| 4617 | * Delete the temp directory and all files it contains. |
| 4618 | */ |
| 4619 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4620 | vim_deltempdir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4621 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4622 | if (vim_tempdir != NULL) |
| 4623 | { |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 4624 | /* remove the trailing path separator */ |
| 4625 | gettail(vim_tempdir)[-1] = NUL; |
| 4626 | delete_recursive(vim_tempdir); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4627 | VIM_CLEAR(vim_tempdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4628 | } |
| 4629 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4630 | |
| 4631 | /* |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4632 | * Directory "tempdir" was created. Expand this name to a full path and put |
| 4633 | * it in "vim_tempdir". This avoids that using ":cd" would confuse us. |
| 4634 | * "tempdir" must be no longer than MAXPATHL. |
| 4635 | */ |
| 4636 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4637 | vim_settempdir(char_u *tempdir) |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4638 | { |
| 4639 | char_u *buf; |
| 4640 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 4641 | buf = alloc(MAXPATHL + 2); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4642 | if (buf != NULL) |
| 4643 | { |
| 4644 | if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) |
| 4645 | STRCPY(buf, tempdir); |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4646 | add_pathsep(buf); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4647 | vim_tempdir = vim_strsave(buf); |
| 4648 | vim_free(buf); |
| 4649 | } |
| 4650 | } |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 4651 | #endif |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4652 | |
| 4653 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4654 | * vim_tempname(): Return a unique name that can be used for a temp file. |
| 4655 | * |
Bram Moolenaar | 76ae22f | 2016-06-13 20:00:29 +0200 | [diff] [blame] | 4656 | * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is |
| 4657 | * guaranteed to NOT be created. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4658 | * |
| 4659 | * The returned pointer is to allocated memory. |
| 4660 | * The returned pointer is NULL if no valid name was found. |
| 4661 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4662 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4663 | vim_tempname( |
| 4664 | int extra_char UNUSED, /* char to use in the name instead of '?' */ |
| 4665 | int keep UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4666 | { |
| 4667 | #ifdef USE_TMPNAM |
| 4668 | char_u itmp[L_tmpnam]; /* use tmpnam() */ |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4669 | #elif defined(MSWIN) |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4670 | WCHAR itmp[TEMPNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4671 | #else |
| 4672 | char_u itmp[TEMPNAMELEN]; |
| 4673 | #endif |
| 4674 | |
| 4675 | #ifdef TEMPDIRNAMES |
| 4676 | static char *(tempdirs[]) = {TEMPDIRNAMES}; |
| 4677 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4678 | # ifndef EEXIST |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4679 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4680 | # endif |
| 4681 | |
| 4682 | /* |
| 4683 | * This will create a directory for private use by this instance of Vim. |
| 4684 | * This is done once, and the same directory is used for all temp files. |
| 4685 | * This method avoids security problems because of symlink attacks et al. |
| 4686 | * It's also a bit faster, because we only need to check for an existing |
| 4687 | * file when creating the directory and not for each temp file. |
| 4688 | */ |
| 4689 | if (vim_tempdir == NULL) |
| 4690 | { |
| 4691 | /* |
| 4692 | * Try the entries in TEMPDIRNAMES to create the temp directory. |
| 4693 | */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4694 | for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4695 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4696 | # ifndef HAVE_MKDTEMP |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4697 | size_t itmplen; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4698 | long nr; |
| 4699 | long off; |
| 4700 | # endif |
| 4701 | |
Bram Moolenaar | e1a6199 | 2015-12-03 21:02:27 +0100 | [diff] [blame] | 4702 | /* Expand $TMP, leave room for "/v1100000/999999999". |
| 4703 | * Skip the directory check if the expansion fails. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4704 | expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); |
Bram Moolenaar | e1a6199 | 2015-12-03 21:02:27 +0100 | [diff] [blame] | 4705 | if (itmp[0] != '$' && mch_isdir(itmp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4706 | { |
Bram Moolenaar | e1a6199 | 2015-12-03 21:02:27 +0100 | [diff] [blame] | 4707 | /* directory exists */ |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4708 | add_pathsep(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4709 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4710 | # ifdef HAVE_MKDTEMP |
Bram Moolenaar | 35d88f4 | 2016-06-04 14:52:00 +0200 | [diff] [blame] | 4711 | { |
| 4712 | # if defined(UNIX) || defined(VMS) |
| 4713 | /* Make sure the umask doesn't remove the executable bit. |
| 4714 | * "repl" has been reported to use "177". */ |
| 4715 | mode_t umask_save = umask(077); |
| 4716 | # endif |
| 4717 | /* Leave room for filename */ |
| 4718 | STRCAT(itmp, "vXXXXXX"); |
| 4719 | if (mkdtemp((char *)itmp) != NULL) |
| 4720 | vim_settempdir(itmp); |
| 4721 | # if defined(UNIX) || defined(VMS) |
| 4722 | (void)umask(umask_save); |
| 4723 | # endif |
| 4724 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4725 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4726 | /* Get an arbitrary number of up to 6 digits. When it's |
| 4727 | * unlikely that it already exists it will be faster, |
| 4728 | * otherwise it doesn't matter. The use of mkdir() avoids any |
| 4729 | * security problems because of the predictable number. */ |
| 4730 | nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4731 | itmplen = STRLEN(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4732 | |
| 4733 | /* Try up to 10000 different values until we find a name that |
| 4734 | * doesn't exist. */ |
| 4735 | for (off = 0; off < 10000L; ++off) |
| 4736 | { |
| 4737 | int r; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4738 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4739 | mode_t umask_save; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4740 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4741 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4742 | sprintf((char *)itmp + itmplen, "v%ld", nr + off); |
| 4743 | # ifndef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4744 | /* If mkdir() does not set errno to EEXIST, check for |
| 4745 | * existing file here. There is a race condition then, |
| 4746 | * although it's fail-safe. */ |
| 4747 | if (mch_stat((char *)itmp, &st) >= 0) |
| 4748 | continue; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4749 | # endif |
| 4750 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4751 | /* Make sure the umask doesn't remove the executable bit. |
| 4752 | * "repl" has been reported to use "177". */ |
| 4753 | umask_save = umask(077); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4754 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4755 | r = vim_mkdir(itmp, 0700); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4756 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | (void)umask(umask_save); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4758 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | if (r == 0) |
| 4760 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4761 | vim_settempdir(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4762 | break; |
| 4763 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4764 | # ifdef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4765 | /* If the mkdir() didn't fail because the file/dir exists, |
| 4766 | * we probably can't create any dir here, try another |
| 4767 | * place. */ |
| 4768 | if (errno != EEXIST) |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4769 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4770 | break; |
| 4771 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 4772 | # endif /* HAVE_MKDTEMP */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4773 | if (vim_tempdir != NULL) |
| 4774 | break; |
| 4775 | } |
| 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | if (vim_tempdir != NULL) |
| 4780 | { |
| 4781 | /* There is no need to check if the file exists, because we own the |
| 4782 | * directory and nobody else creates a file in it. */ |
| 4783 | sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); |
| 4784 | return vim_strsave(itmp); |
| 4785 | } |
| 4786 | |
| 4787 | return NULL; |
| 4788 | |
| 4789 | #else /* TEMPDIRNAMES */ |
| 4790 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4791 | # ifdef MSWIN |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4792 | WCHAR wszTempFile[_MAX_PATH + 1]; |
| 4793 | WCHAR buf4[4]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4794 | char_u *retval; |
| 4795 | char_u *p; |
| 4796 | |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4797 | wcscpy(itmp, L""); |
| 4798 | if (GetTempPathW(_MAX_PATH, wszTempFile) == 0) |
Bram Moolenaar | b189191 | 2011-02-09 14:47:03 +0100 | [diff] [blame] | 4799 | { |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4800 | wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir |
| 4801 | wszTempFile[1] = NUL; |
Bram Moolenaar | b189191 | 2011-02-09 14:47:03 +0100 | [diff] [blame] | 4802 | } |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4803 | wcscpy(buf4, L"VIM"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4804 | buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4805 | if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4806 | return NULL; |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 4807 | if (!keep) |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4808 | // GetTempFileName() will create the file, we don't want that |
| 4809 | (void)DeleteFileW(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4810 | |
Bram Moolenaar | ec0f50a | 2019-02-10 23:26:13 +0100 | [diff] [blame] | 4811 | // Backslashes in a temp file name cause problems when filtering with |
| 4812 | // "sh". NOTE: This also checks 'shellcmdflag' to help those people who |
| 4813 | // didn't set 'shellslash'. |
| 4814 | retval = utf16_to_enc(itmp, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4815 | if (*p_shcf == '-' || p_ssl) |
| 4816 | for (p = retval; *p; ++p) |
| 4817 | if (*p == '\\') |
| 4818 | *p = '/'; |
| 4819 | return retval; |
| 4820 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4821 | # else // MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | |
| 4823 | # ifdef USE_TMPNAM |
Bram Moolenaar | 95474ca | 2011-02-09 16:44:51 +0100 | [diff] [blame] | 4824 | char_u *p; |
| 4825 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | /* tmpnam() will make its own name */ |
Bram Moolenaar | 95474ca | 2011-02-09 16:44:51 +0100 | [diff] [blame] | 4827 | p = tmpnam((char *)itmp); |
| 4828 | if (p == NULL || *p == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4829 | return NULL; |
| 4830 | # else |
| 4831 | char_u *p; |
| 4832 | |
| 4833 | # ifdef VMS_TEMPNAM |
| 4834 | /* mktemp() is not working on VMS. It seems to be |
| 4835 | * a do-nothing function. Therefore we use tempnam(). |
| 4836 | */ |
| 4837 | sprintf((char *)itmp, "VIM%c", extra_char); |
| 4838 | p = (char_u *)tempnam("tmp:", (char *)itmp); |
| 4839 | if (p != NULL) |
| 4840 | { |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 4841 | /* VMS will use '.LIS' if we don't explicitly specify an extension, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4842 | * and VIM will then be unable to find the file later */ |
| 4843 | STRCPY(itmp, p); |
| 4844 | STRCAT(itmp, ".txt"); |
| 4845 | free(p); |
| 4846 | } |
| 4847 | else |
| 4848 | return NULL; |
| 4849 | # else |
| 4850 | STRCPY(itmp, TEMPNAME); |
| 4851 | if ((p = vim_strchr(itmp, '?')) != NULL) |
| 4852 | *p = extra_char; |
| 4853 | if (mktemp((char *)itmp) == NULL) |
| 4854 | return NULL; |
| 4855 | # endif |
| 4856 | # endif |
| 4857 | |
| 4858 | return vim_strsave(itmp); |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4859 | # endif // MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4860 | #endif /* TEMPDIRNAMES */ |
| 4861 | } |
| 4862 | |
| 4863 | #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 4864 | /* |
Bram Moolenaar | b4f6a46 | 2015-10-13 19:43:17 +0200 | [diff] [blame] | 4865 | * Convert all backslashes in fname to forward slashes in-place, unless when |
| 4866 | * it looks like a URL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4867 | */ |
| 4868 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4869 | forward_slash(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4870 | { |
| 4871 | char_u *p; |
| 4872 | |
Bram Moolenaar | b4f6a46 | 2015-10-13 19:43:17 +0200 | [diff] [blame] | 4873 | if (path_with_url(fname)) |
| 4874 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4875 | for (p = fname; *p != NUL; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4876 | /* The Big5 encoding can have '\' in the trail byte. */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4877 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | ++p; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 4879 | else if (*p == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4880 | *p = '/'; |
| 4881 | } |
| 4882 | #endif |
| 4883 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 4884 | /* |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4885 | * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
| 4886 | * precompiled regprog "prog" ("pattern" is NULL). That avoids calling |
| 4887 | * vim_regcomp() often. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4888 | * Used for autocommands and 'wildignore'. |
| 4889 | * Returns TRUE if there is a match, FALSE otherwise. |
| 4890 | */ |
Bram Moolenaar | 3e460fd | 2019-01-26 16:21:07 +0100 | [diff] [blame] | 4891 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4892 | match_file_pat( |
| 4893 | char_u *pattern, /* pattern to match with */ |
| 4894 | regprog_T **prog, /* pre-compiled regprog or NULL */ |
| 4895 | char_u *fname, /* full path of file name */ |
| 4896 | char_u *sfname, /* short file name or NULL */ |
| 4897 | char_u *tail, /* tail of path */ |
| 4898 | int allow_dirs) /* allow matching with dir */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4899 | { |
| 4900 | regmatch_T regmatch; |
| 4901 | int result = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4902 | |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4903 | regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 4904 | if (prog != NULL) |
| 4905 | regmatch.regprog = *prog; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4906 | else |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 4907 | regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4908 | |
| 4909 | /* |
| 4910 | * Try for a match with the pattern with: |
| 4911 | * 1. the full file name, when the pattern has a '/'. |
| 4912 | * 2. the short file name, when the pattern has a '/'. |
| 4913 | * 3. the tail of the file name, when the pattern has no '/'. |
| 4914 | */ |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 4915 | if (regmatch.regprog != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4916 | && ((allow_dirs |
| 4917 | && (vim_regexec(®match, fname, (colnr_T)0) |
| 4918 | || (sfname != NULL |
| 4919 | && vim_regexec(®match, sfname, (colnr_T)0)))) |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 4920 | || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4921 | result = TRUE; |
| 4922 | |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 4923 | if (prog != NULL) |
| 4924 | *prog = regmatch.regprog; |
| 4925 | else |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4926 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4927 | return result; |
| 4928 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4929 | |
| 4930 | #if defined(FEAT_WILDIGN) || defined(PROTO) |
| 4931 | /* |
| 4932 | * Return TRUE if a file matches with a pattern in "list". |
| 4933 | * "list" is a comma-separated list of patterns, like 'wildignore'. |
| 4934 | * "sfname" is the short file name or NULL, "ffname" the long file name. |
| 4935 | */ |
| 4936 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4937 | match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4938 | { |
| 4939 | char_u buf[100]; |
| 4940 | char_u *tail; |
| 4941 | char_u *regpat; |
| 4942 | char allow_dirs; |
| 4943 | int match; |
| 4944 | char_u *p; |
| 4945 | |
| 4946 | tail = gettail(sfname); |
| 4947 | |
| 4948 | /* try all patterns in 'wildignore' */ |
| 4949 | p = list; |
| 4950 | while (*p) |
| 4951 | { |
| 4952 | copy_option_part(&p, buf, 100, ","); |
| 4953 | regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); |
| 4954 | if (regpat == NULL) |
| 4955 | break; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4956 | match = match_file_pat(regpat, NULL, ffname, sfname, |
| 4957 | tail, (int)allow_dirs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | vim_free(regpat); |
| 4959 | if (match) |
| 4960 | return TRUE; |
| 4961 | } |
| 4962 | return FALSE; |
| 4963 | } |
| 4964 | #endif |
| 4965 | |
| 4966 | /* |
| 4967 | * Convert the given pattern "pat" which has shell style wildcards in it, into |
| 4968 | * a regular expression, and return the result in allocated memory. If there |
| 4969 | * is a directory path separator to be matched, then TRUE is put in |
| 4970 | * allow_dirs, otherwise FALSE is put there -- webb. |
| 4971 | * Handle backslashes before special characters, like "\*" and "\ ". |
| 4972 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | * Returns NULL when out of memory. |
| 4974 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4975 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4976 | file_pat_to_reg_pat( |
| 4977 | char_u *pat, |
| 4978 | char_u *pat_end, /* first char after pattern or NULL */ |
| 4979 | char *allow_dirs, /* Result passed back out in here */ |
| 4980 | int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4981 | { |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 4982 | int size = 2; /* '^' at start, '$' at end */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4983 | char_u *endp; |
| 4984 | char_u *reg_pat; |
| 4985 | char_u *p; |
| 4986 | int i; |
| 4987 | int nested = 0; |
| 4988 | int add_dollar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | |
| 4990 | if (allow_dirs != NULL) |
| 4991 | *allow_dirs = FALSE; |
| 4992 | if (pat_end == NULL) |
| 4993 | pat_end = pat + STRLEN(pat); |
| 4994 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4995 | for (p = pat; p < pat_end; p++) |
| 4996 | { |
| 4997 | switch (*p) |
| 4998 | { |
| 4999 | case '*': |
| 5000 | case '.': |
| 5001 | case ',': |
| 5002 | case '{': |
| 5003 | case '}': |
| 5004 | case '~': |
| 5005 | size += 2; /* extra backslash */ |
| 5006 | break; |
| 5007 | #ifdef BACKSLASH_IN_FILENAME |
| 5008 | case '\\': |
| 5009 | case '/': |
| 5010 | size += 4; /* could become "[\/]" */ |
| 5011 | break; |
| 5012 | #endif |
| 5013 | default: |
| 5014 | size++; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5015 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5016 | { |
| 5017 | ++p; |
| 5018 | ++size; |
| 5019 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5020 | break; |
| 5021 | } |
| 5022 | } |
| 5023 | reg_pat = alloc(size + 1); |
| 5024 | if (reg_pat == NULL) |
| 5025 | return NULL; |
| 5026 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5027 | i = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5028 | |
| 5029 | if (pat[0] == '*') |
| 5030 | while (pat[0] == '*' && pat < pat_end - 1) |
| 5031 | pat++; |
| 5032 | else |
| 5033 | reg_pat[i++] = '^'; |
| 5034 | endp = pat_end - 1; |
Bram Moolenaar | 8fee878 | 2015-08-11 18:45:48 +0200 | [diff] [blame] | 5035 | if (endp >= pat && *endp == '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5036 | { |
| 5037 | while (endp - pat > 0 && *endp == '*') |
| 5038 | endp--; |
| 5039 | add_dollar = FALSE; |
| 5040 | } |
| 5041 | for (p = pat; *p && nested >= 0 && p <= endp; p++) |
| 5042 | { |
| 5043 | switch (*p) |
| 5044 | { |
| 5045 | case '*': |
| 5046 | reg_pat[i++] = '.'; |
| 5047 | reg_pat[i++] = '*'; |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 5048 | while (p[1] == '*') /* "**" matches like "*" */ |
| 5049 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5050 | break; |
| 5051 | case '.': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5052 | case '~': |
| 5053 | reg_pat[i++] = '\\'; |
| 5054 | reg_pat[i++] = *p; |
| 5055 | break; |
| 5056 | case '?': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5057 | reg_pat[i++] = '.'; |
| 5058 | break; |
| 5059 | case '\\': |
| 5060 | if (p[1] == NUL) |
| 5061 | break; |
| 5062 | #ifdef BACKSLASH_IN_FILENAME |
| 5063 | if (!no_bslash) |
| 5064 | { |
| 5065 | /* translate: |
| 5066 | * "\x" to "\\x" e.g., "dir\file" |
| 5067 | * "\*" to "\\.*" e.g., "dir\*.c" |
| 5068 | * "\?" to "\\." e.g., "dir\??.c" |
| 5069 | * "\+" to "\+" e.g., "fileX\+.c" |
| 5070 | */ |
| 5071 | if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') |
| 5072 | && p[1] != '+') |
| 5073 | { |
| 5074 | reg_pat[i++] = '['; |
| 5075 | reg_pat[i++] = '\\'; |
| 5076 | reg_pat[i++] = '/'; |
| 5077 | reg_pat[i++] = ']'; |
| 5078 | if (allow_dirs != NULL) |
| 5079 | *allow_dirs = TRUE; |
| 5080 | break; |
| 5081 | } |
| 5082 | } |
| 5083 | #endif |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 5084 | /* Undo escaping from ExpandEscape(): |
| 5085 | * foo\?bar -> foo?bar |
| 5086 | * foo\%bar -> foo%bar |
| 5087 | * foo\,bar -> foo,bar |
| 5088 | * foo\ bar -> foo bar |
| 5089 | * Don't unescape \, * and others that are also special in a |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 5090 | * regexp. |
| 5091 | * An escaped { must be unescaped since we use magic not |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 5092 | * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 5093 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5094 | if (*++p == '?' |
| 5095 | #ifdef BACKSLASH_IN_FILENAME |
| 5096 | && no_bslash |
| 5097 | #endif |
| 5098 | ) |
| 5099 | reg_pat[i++] = '?'; |
| 5100 | else |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 5101 | if (*p == ',' || *p == '%' || *p == '#' |
Bram Moolenaar | 2288afe | 2015-08-11 16:20:05 +0200 | [diff] [blame] | 5102 | || vim_isspace(*p) || *p == '{' || *p == '}') |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 5103 | reg_pat[i++] = *p; |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 5104 | else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
| 5105 | { |
| 5106 | reg_pat[i++] = '\\'; |
| 5107 | reg_pat[i++] = '{'; |
| 5108 | p += 2; |
| 5109 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5110 | else |
| 5111 | { |
| 5112 | if (allow_dirs != NULL && vim_ispathsep(*p) |
| 5113 | #ifdef BACKSLASH_IN_FILENAME |
| 5114 | && (!no_bslash || *p != '\\') |
| 5115 | #endif |
| 5116 | ) |
| 5117 | *allow_dirs = TRUE; |
| 5118 | reg_pat[i++] = '\\'; |
| 5119 | reg_pat[i++] = *p; |
| 5120 | } |
| 5121 | break; |
| 5122 | #ifdef BACKSLASH_IN_FILENAME |
| 5123 | case '/': |
| 5124 | reg_pat[i++] = '['; |
| 5125 | reg_pat[i++] = '\\'; |
| 5126 | reg_pat[i++] = '/'; |
| 5127 | reg_pat[i++] = ']'; |
| 5128 | if (allow_dirs != NULL) |
| 5129 | *allow_dirs = TRUE; |
| 5130 | break; |
| 5131 | #endif |
| 5132 | case '{': |
| 5133 | reg_pat[i++] = '\\'; |
| 5134 | reg_pat[i++] = '('; |
| 5135 | nested++; |
| 5136 | break; |
| 5137 | case '}': |
| 5138 | reg_pat[i++] = '\\'; |
| 5139 | reg_pat[i++] = ')'; |
| 5140 | --nested; |
| 5141 | break; |
| 5142 | case ',': |
| 5143 | if (nested) |
| 5144 | { |
| 5145 | reg_pat[i++] = '\\'; |
| 5146 | reg_pat[i++] = '|'; |
| 5147 | } |
| 5148 | else |
| 5149 | reg_pat[i++] = ','; |
| 5150 | break; |
| 5151 | default: |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5152 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5153 | reg_pat[i++] = *p++; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 5154 | else if (allow_dirs != NULL && vim_ispathsep(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5155 | *allow_dirs = TRUE; |
| 5156 | reg_pat[i++] = *p; |
| 5157 | break; |
| 5158 | } |
| 5159 | } |
| 5160 | if (add_dollar) |
| 5161 | reg_pat[i++] = '$'; |
| 5162 | reg_pat[i] = NUL; |
| 5163 | if (nested != 0) |
| 5164 | { |
| 5165 | if (nested < 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5166 | emsg(_("E219: Missing {.")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5167 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5168 | emsg(_("E220: Missing }.")); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5169 | VIM_CLEAR(reg_pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5170 | } |
| 5171 | return reg_pat; |
| 5172 | } |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5173 | |
| 5174 | #if defined(EINTR) || defined(PROTO) |
| 5175 | /* |
| 5176 | * Version of read() that retries when interrupted by EINTR (possibly |
| 5177 | * by a SIGWINCH). |
| 5178 | */ |
| 5179 | long |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5180 | read_eintr(int fd, void *buf, size_t bufsize) |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5181 | { |
| 5182 | long ret; |
| 5183 | |
| 5184 | for (;;) |
| 5185 | { |
| 5186 | ret = vim_read(fd, buf, bufsize); |
| 5187 | if (ret >= 0 || errno != EINTR) |
| 5188 | break; |
| 5189 | } |
| 5190 | return ret; |
| 5191 | } |
| 5192 | |
| 5193 | /* |
| 5194 | * Version of write() that retries when interrupted by EINTR (possibly |
| 5195 | * by a SIGWINCH). |
| 5196 | */ |
| 5197 | long |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5198 | write_eintr(int fd, void *buf, size_t bufsize) |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5199 | { |
| 5200 | long ret = 0; |
| 5201 | long wlen; |
| 5202 | |
| 5203 | /* Repeat the write() so long it didn't fail, other than being interrupted |
| 5204 | * by a signal. */ |
| 5205 | while (ret < (long)bufsize) |
| 5206 | { |
Bram Moolenaar | 9c26303 | 2010-12-17 18:06:06 +0100 | [diff] [blame] | 5207 | wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5208 | if (wlen < 0) |
| 5209 | { |
| 5210 | if (errno != EINTR) |
| 5211 | break; |
| 5212 | } |
| 5213 | else |
| 5214 | ret += wlen; |
| 5215 | } |
| 5216 | return ret; |
| 5217 | } |
| 5218 | #endif |