blob: 361109313b2a7075837c82c6a277a45064f34eb9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaare3f915d2020-07-14 23:02:44 +020016#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020019#if defined(UNIX) && defined(FEAT_EVAL)
20# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar217e1b82019-12-01 21:41:28 +010024// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000025#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf077db22019-08-13 00:18:24 +020027static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010028#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010029static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020032static 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 Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010035static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000036static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000037
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020038#ifdef FEAT_EVAL
39static int readdirex_sort;
40#endif
41
Bram Moolenaar473952e2019-09-28 16:30:04 +020042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010043filemess(
44 buf_T *buf,
45 char_u *name,
46 char_u *s,
47 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
49 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020050 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52 if (msg_silent != 0)
53 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010054 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020055
Bram Moolenaar217e1b82019-12-01 21:41:28 +010056 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020057 if (STRLEN(IObuff) > IOSIZE - 100)
58 IObuff[IOSIZE - 100] = NUL;
59
60 // Avoid an over-long translation to cause trouble.
61 STRNCAT(IObuff, s, 99);
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 /*
64 * For the first message may have to start a new line.
65 * For further ones overwrite the previous one, reset msg_scroll before
66 * calling filemess().
67 */
68 msg_scroll_save = msg_scroll;
69 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
70 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010071 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 check_for_delay(FALSE);
73 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020074 if (prev_msg_col != 0 && msg_col == 0)
75 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 msg_scroll = msg_scroll_save;
77 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010078 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
80 msg_clr_eos();
81 out_flush();
82 msg_scrolled_ign = FALSE;
83}
84
85/*
86 * Read lines from file "fname" into the buffer after line "from".
87 *
88 * 1. We allocate blocks with lalloc, as big as possible.
89 * 2. Each block is filled with characters from the file with a single read().
90 * 3. The lines are inserted in the buffer with ml_append().
91 *
92 * (caller must check that fname != NULL, unless READ_STDIN is used)
93 *
94 * "lines_to_skip" is the number of lines that must be skipped
95 * "lines_to_read" is the number of lines that are appended
96 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
97 *
98 * flags:
99 * READ_NEW starting to edit a new buffer
100 * READ_FILTER reading filter output
101 * READ_STDIN read from stdin instead of a file
102 * READ_BUFFER read from curbuf instead of a file (converting after reading
103 * stdin)
104 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200105 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200106 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100108 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 */
110 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100111readfile(
112 char_u *fname,
113 char_u *sfname,
114 linenr_T from,
115 linenr_T lines_to_skip,
116 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100117 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100118 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 int fd = 0;
121 int newfile = (flags & READ_NEW);
122 int check_readonly;
123 int filtering = (flags & READ_FILTER);
124 int read_stdin = (flags & READ_STDIN);
125 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000127 int set_options = newfile || read_buffer
128 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100129 linenr_T read_buf_lnum = 1; // next line to read from curbuf
130 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 char_u c;
132 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100133 char_u *ptr = NULL; // pointer into read buffer
134 char_u *buffer = NULL; // read buffer
135 char_u *new_buffer = NULL; // init to shut up gcc
136 char_u *line_start = NULL; // init to shut up gcc
137 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 colnr_T len;
139 long size = 0;
140 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200141 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 int skip_read = FALSE;
143#ifdef FEAT_CRYPT
144 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200145 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200147#ifdef FEAT_PERSISTENT_UNDO
148 context_sha256_T sha_ctx;
149 int read_undo_file = FALSE;
150#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100151 int split = 0; // number of split lines
152#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100154 int error = FALSE; // errors encountered
155 int ff_error = EOL_UNKNOWN; // file format with errors
156 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef UNIX
158 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100159 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#else
161 int perm;
162#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100163 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200165 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 int file_readonly;
167 linenr_T skip_count = 0;
168 linenr_T read_count = 0;
169 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100170 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
171 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100172 int try_mac;
173 int try_dos;
174 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100177 linenr_T conv_error = 0; // line nr with conversion error
178 linenr_T illegal_byte = 0; // line nr with illegal byte
179 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
180 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000181 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100182 // BAD_KEEP, BAD_DROP or character to
183 // replace with
184 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 char_u *fenc; // fileencoding to use
187 int fenc_alloced; // fenc_next is in allocated memory
188 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 int advance_fenc = FALSE;
190 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100191#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100192 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100193# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 int did_iconv = FALSE; // TRUE when iconv() failed and trying
195 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100197#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100198 int converted = FALSE; // TRUE if conversion done
199 int notconverted = FALSE; // TRUE if conversion wanted but it
200 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100202 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100203 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200204 buf_T *old_curbuf;
205 char_u *old_b_ffname;
206 char_u *old_b_fname;
207 int using_b_ffname;
208 int using_b_fname;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200209
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100210 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200211
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100212 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214 /*
215 * If there is no file name yet, use the one for the read file.
216 * BF_NOTEDITED is set to reflect this.
217 * Don't do this for a read from a filter.
218 * Only do this when 'cpoptions' contains the 'f' flag.
219 */
220 if (curbuf->b_ffname == NULL
221 && !filtering
222 && fname != NULL
223 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
224 && !(flags & READ_DUMMY))
225 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000226 if (set_rw_fname(fname, sfname) == FAIL)
227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100230 // Remember the initial values of curbuf, curbuf->b_ffname and
231 // curbuf->b_fname to detect whether they are altered as a result of
232 // executing nasty autocommands. Also check if "fname" and "sfname"
233 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200234 old_curbuf = curbuf;
235 old_b_ffname = curbuf->b_ffname;
236 old_b_fname = curbuf->b_fname;
237 using_b_ffname = (fname == curbuf->b_ffname)
238 || (sfname == curbuf->b_ffname);
239 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200240
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100241 // After reading a file the cursor line changes but we don't want to
242 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000243 ex_no_reprint = TRUE;
244
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100245 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000246 need_fileinfo = FALSE;
247
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 /*
249 * For Unix: Use the short file name whenever possible.
250 * Avoids problems with networks and when directory names are changed.
251 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
252 * another directory, which we don't detect.
253 */
254 if (sfname == NULL)
255 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200256#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 fname = sfname;
258#endif
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 /*
261 * The BufReadCmd and FileReadCmd events intercept the reading process by
262 * executing the associated commands instead.
263 */
264 if (!filtering && !read_stdin && !read_buffer)
265 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100266 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100268 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
270 curbuf->b_op_start.col = 0;
271
272 if (newfile)
273 {
274 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
275 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200276 {
277 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200279 if (aborting())
280 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200282 // The BufReadCmd code usually uses ":read" to get the text and
283 // perhaps ":file" to change the buffer name. But we should
284 // consider this to work like ":edit", thus reset the
285 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
286 // same file.
287 if (status == OK)
288 curbuf->b_flags &= ~BF_NOTEDITED;
289 return status;
290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
293 FALSE, NULL, eap))
294#ifdef FEAT_EVAL
295 return aborting() ? FAIL : OK;
296#else
297 return OK;
298#endif
299
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100300 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
303 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100304 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100306 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
308 /*
309 * If the name ends in a path separator, we can't open it. Check here,
310 * because reading the file may actually work, but then creating the swap
311 * file may destroy it! Reported on MS-DOS and Win 95.
312 * If the name is too long we might crash further on, quit here.
313 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000314 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000316 p = fname + STRLEN(fname);
317 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000318 {
319 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
320 msg_end();
321 msg_scroll = msg_save;
322 return FAIL;
323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200326 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200328#ifdef UNIX
329 /*
330 * On Unix it is possible to read a directory, so we have to
331 * check for it before the mch_open().
332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100334 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
335 && !S_ISFIFO(perm) // ... or fifo
336 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000337# ifdef OPEN_CHR_FILES
338 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100339 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 )
342 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100343 int retval = FAIL;
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100348 retval = NOTDONE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 else
351 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
352 msg_end();
353 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100354 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200356#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100357#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000358 /*
359 * MS-Windows allows opening a device, but we will probably get stuck
360 * trying to read it.
361 */
362 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
363 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000364 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000365 msg_end();
366 msg_scroll = msg_save;
367 return FAIL;
368 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000369#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200370 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000371
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100372 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200373 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 /*
376 * When opening a new file we take the readonly flag from the file.
377 * Default is r/w, can be set to r/o below.
378 * Don't reset it when in readonly mode
379 * Only set/reset b_p_ro when BF_CHECK_RO is set.
380 */
381 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000382 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 curbuf->b_p_ro = FALSE;
384
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200385 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100387 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (mch_stat((char *)fname, &st) >= 0)
389 {
390 buf_store_time(curbuf, &st, fname);
391 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef UNIX
393 /*
394 * Use the protection bits of the original file for the swap file.
395 * This makes it possible for others to read the name of the
396 * edited file from the swapfile, but only if they can read the
397 * edited file.
398 * Remove the "write" and "execute" bits for group and others
399 * (they must not write the swapfile).
400 * Add the "read" and "write" bits for the user, otherwise we may
401 * not be able to write to the file ourselves.
402 * Setting the bits is done below, after creating the swap file.
403 */
404 swap_mode = (st.st_mode & 0644) | 0600;
405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#ifdef VMS
407 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000408 curbuf->b_fab_rat = st.st_fab_rat;
409 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#endif
411 }
412 else
413 {
414 curbuf->b_mtime = 0;
415 curbuf->b_mtime_read = 0;
416 curbuf->b_orig_size = 0;
417 curbuf->b_orig_mode = 0;
418 }
419
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100420 // Reset the "new file" flag. It will be set again below when the
421 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
423 }
424
425/*
426 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100427 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 */
429 file_readonly = FALSE;
430 if (read_stdin)
431 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100432#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100433 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 setmode(0, O_BINARY);
435#endif
436 }
437 else if (!read_buffer)
438 {
439#ifdef USE_MCH_ACCESS
440 if (
441# ifdef UNIX
442 !(perm & 0222) ||
443# endif
444 mch_access((char *)fname, W_OK))
445 file_readonly = TRUE;
446 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
447#else
448 if (!newfile
449 || readonlymode
450 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
451 {
452 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100453 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
455 }
456#endif
457 }
458
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100459 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {
461#ifndef UNIX
462 int isdir_f;
463#endif
464 msg_scroll = msg_save;
465#ifndef UNIX
466 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100467 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 */
469 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100470 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (isdir_f)
472 {
473 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100474 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 else
477#endif
478 if (newfile)
479 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200480 if (perm < 0
481#ifdef ENOENT
482 && errno == ENOENT
483#endif
484 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {
486 /*
487 * Set the 'new-file' flag, so that when the file has
488 * been created by someone else, a ":w" will complain.
489 */
490 curbuf->b_flags |= BF_NEW;
491
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 // Create a swap file now, so that other Vims are warned
493 // that we are editing this file. Don't do this for a
494 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_QUICKFIX
496 if (!bt_dontwrite(curbuf))
497#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100500 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000501 if (curbuf != old_curbuf
502 || (using_b_ffname
503 && (old_b_ffname != curbuf->b_ffname))
504 || (using_b_fname
505 && (old_b_fname != curbuf->b_fname)))
506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100507 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000508 return FAIL;
509 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000510 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000511 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200512 filemess(curbuf, sfname,
513 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000514 else
515 filemess(curbuf, sfname,
516 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100518 // Even though this is a new file, it might have been
519 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 check_marks_read();
521#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100522 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200523 if (eap != NULL)
524 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
526 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100527 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 save_file_ff(curbuf);
529
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100530#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 return FAIL;
533#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 }
536 else
537 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000538 filemess(curbuf, sfname, (char_u *)(
539# ifdef EFBIG
540 (errno == EFBIG) ? _("[File too big]") :
541# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200542# ifdef EOVERFLOW
543 (errno == EOVERFLOW) ? _("[File too big]") :
544# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000545 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548 }
549
550 return FAIL;
551 }
552
553 /*
554 * Only set the 'ro' flag for readonly files the first time they are
555 * loaded. Help files always get readonly mode
556 */
557 if ((check_readonly && file_readonly) || curbuf->b_help)
558 curbuf->b_p_ro = TRUE;
559
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000560 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100562 // Don't change 'eol' if reading from buffer as it will already be
563 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000564 if (!read_buffer)
565 {
566 curbuf->b_p_eol = TRUE;
567 curbuf->b_start_eol = TRUE;
568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000570 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
572
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100573 // Create a swap file now, so that other Vims are warned that we are
574 // editing this file.
575 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576#ifdef FEAT_QUICKFIX
577 if (!bt_dontwrite(curbuf))
578#endif
579 {
580 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000581 if (!read_stdin && (curbuf != old_curbuf
582 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
583 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100585 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000586 if (!read_buffer)
587 close(fd);
588 return FAIL;
589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100591 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000592 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
593 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100594 {
595 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
596
597 /*
598 * If the group-read bit is set but not the world-read bit, then
599 * the group must be equal to the group of the original file. If
600 * we can't make that happen then reset the group-read bit. This
601 * avoids making the swap file readable to more users when the
602 * primary group of the user is too permissive.
603 */
604 if ((swap_mode & 044) == 040)
605 {
606 stat_T swap_st;
607
608 if (mch_stat((char *)swap_fname, &swap_st) >= 0
609 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200610# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100611 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200612 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200613# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200614 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100615 swap_mode &= 0600;
616 }
617
618 (void)mch_setperm(swap_fname, (long)swap_mode);
619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#endif
621 }
622
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200623 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (swap_exists_action == SEA_QUIT)
625 {
626 if (!read_buffer && !read_stdin)
627 close(fd);
628 return FAIL;
629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633 /*
634 * Set '[ mark to the line above where the lines go (line 1 if zero).
635 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100636 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
638 curbuf->b_op_start.col = 0;
639
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100640 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
641 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
642 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (!read_buffer)
645 {
646 int m = msg_scroll;
647 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 /*
650 * The file must be closed again, the autocommands may want to change
651 * the file before reading it.
652 */
653 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656 /*
657 * The output from the autocommands should not overwrite anything and
658 * should not be overwritten: Set msg_scroll, restore its value if no
659 * output was done.
660 */
661 msg_scroll = TRUE;
662 if (filtering)
663 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
664 FALSE, curbuf, eap);
665 else if (read_stdin)
666 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
667 FALSE, curbuf, eap);
668 else if (newfile)
669 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
670 FALSE, curbuf, eap);
671 else
672 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
673 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100674 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100675 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
676 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
677 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100678 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (msg_scrolled == n)
681 msg_scroll = m;
682
683#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 --no_wait_return;
687 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100688 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 return FAIL;
690 }
691#endif
692 /*
693 * Don't allow the autocommands to change the current buffer.
694 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000695 *
696 * Don't allow the autocommands to change the buffer name either
697 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 */
699 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000700 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
701 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
703 {
704 --no_wait_return;
705 msg_scroll = msg_save;
706 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100707 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100709 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return FAIL;
712 }
713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100715 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
717
718 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
719 {
720 /*
721 * Show the user that we are busy reading the input. Sometimes this
722 * may take a while. When reading from stdin another program may
723 * still be running, don't move the cursor to the last line, unless
724 * always using the GUI.
725 */
726 if (read_stdin)
727 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100728 if (!is_not_a_term())
729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200731# ifdef VIMDLL
732 if (!gui.in_use)
733# endif
734 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#endif
736#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100737 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100738 if (gui.in_use && !gui.dying && !gui.starting)
739 {
740 p = (char_u *)_("Reading from stdin...");
741 gui_write(p, (int)STRLEN(p));
742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746 else if (!read_buffer)
747 filemess(curbuf, sfname, (char_u *)"", 0);
748 }
749
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100750 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
752 /*
753 * Set linecnt now, before the "retry" caused by a wrong guess for
754 * fileformat, and after the autocommands, which may change them.
755 */
756 linecnt = curbuf->b_ml.ml_line_count;
757
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100758 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000759 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000760 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000761 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000762 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000763 curbuf->b_bad_char = eap->bad_char;
764 }
765 else
766 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000769 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 */
771 if (eap != NULL && eap->force_enc != 0)
772 {
773 fenc = enc_canonize(eap->cmd + eap->force_enc);
774 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000775 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 }
777 else if (curbuf->b_p_bin)
778 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100779 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 fenc_alloced = FALSE;
781 }
782 else if (curbuf->b_help)
783 {
784 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000785 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100787 // Help files are either utf-8 or latin1. Try utf-8 first, if this
788 // fails it must be latin1.
789 // Always do this when 'encoding' is "utf-8". Otherwise only do
790 // this when needed to avoid [converted] remarks all the time.
791 // It is needed when the first line contains non-ASCII characters.
792 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 fenc = (char_u *)"latin1";
794 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000795 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000797 fc = fname[STRLEN(fname) - 1];
798 if (TOLOWER_ASC(fc) == 'x')
799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // Read the first line (and a bit more). Immediately rewind to
801 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100802 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200803 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000804 for (p = firstline; p < firstline + len; ++p)
805 if (*p >= 0x80)
806 {
807 c = TRUE;
808 break;
809 }
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812
813 if (c)
814 {
815 fenc_next = fenc;
816 fenc = (char_u *)"utf-8";
817
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100818 // When the file is utf-8 but a character doesn't fit in
819 // 'encoding' don't retry. In help text editing utf-8 bytes
820 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000821 if (!enc_utf8)
822 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824 fenc_alloced = FALSE;
825 }
826 else if (*p_fencs == NUL)
827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100828 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 fenc_alloced = FALSE;
830 }
831 else
832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200834 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 /*
838 * Jump back here to retry reading the file in different ways.
839 * Reasons to retry:
840 * - encoding conversion failed: try another one from "fenc_next"
841 * - BOM detected and fenc was set, need to setup conversion
842 * - "fileformat" check failed: try another
843 *
844 * Variables set for special retry actions:
845 * "file_rewind" Rewind the file to start reading it again.
846 * "advance_fenc" Advance "fenc" using "fenc_next".
847 * "skip_read" Re-use already read bytes (BOM detected).
848 * "did_iconv" iconv() conversion failed, try 'charconvert'.
849 * "keep_fileformat" Don't reset "fileformat".
850 *
851 * Other status indicators:
852 * "tmpname" When != NULL did conversion with 'charconvert'.
853 * Output file has to be deleted afterwards.
854 * "iconv_fd" When != -1 did conversion with iconv().
855 */
856retry:
857
858 if (file_rewind)
859 {
860 if (read_buffer)
861 {
862 read_buf_lnum = 1;
863 read_buf_col = 0;
864 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200865 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 error = TRUE;
869 goto failed;
870 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100871 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200873 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000875 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000878 curbuf->b_start_bomb = FALSE;
879 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000880 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
882
883 /*
884 * When retrying with another "fenc" and the first time "fileformat"
885 * will be reset.
886 */
887 if (keep_fileformat)
888 keep_fileformat = FALSE;
889 else
890 {
891 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000894 try_unix = try_dos = try_mac = FALSE;
895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100897 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100899 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100901 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903
Bram Moolenaar13505972019-01-24 15:04:48 +0100904#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (iconv_fd != (iconv_t)-1)
906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 iconv_close(iconv_fd);
909 iconv_fd = (iconv_t)-1;
910 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 if (advance_fenc)
914 {
915 /*
916 * Try the next entry in 'fileencodings'.
917 */
918 advance_fenc = FALSE;
919
920 if (eap != NULL && eap->force_enc != 0)
921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100922 // Conversion given with "++cc=" wasn't possible, read
923 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000925 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (fenc_alloced)
927 vim_free(fenc);
928 fenc = (char_u *)"";
929 fenc_alloced = FALSE;
930 }
931 else
932 {
933 if (fenc_alloced)
934 vim_free(fenc);
935 if (fenc_next != NULL)
936 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200937 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
939 else
940 {
941 fenc = (char_u *)"";
942 fenc_alloced = FALSE;
943 }
944 }
945 if (tmpname != NULL)
946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100947 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100948 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950 }
951
952 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000953 * Conversion may be required when the encoding of the file is different
954 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 */
956 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000957 converted = need_conversion(fenc);
958 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
960
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100961 // "ucs-bom" means we need to check the first bytes of the file
962 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (STRCMP(fenc, ENC_UCSBOM) == 0)
964 fio_flags = FIO_UCSBOM;
965
966 /*
967 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
968 * done. This is handled below after read(). Prepare the
969 * fio_flags to avoid having to parse the string each time.
970 * Also check for Unicode to Latin1 conversion, because iconv()
971 * appears not to handle this correctly. This works just like
972 * conversion to UTF-8 except how the resulting character is put in
973 * the buffer.
974 */
975 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
976 fio_flags = get_fio_flags(fenc);
977
Bram Moolenaar4f974752019-02-17 17:44:42 +0100978#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 /*
980 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
981 * is handled with MultiByteToWideChar().
982 */
983 if (fio_flags == 0)
984 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaar13505972019-01-24 15:04:48 +0100987#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100988 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (fio_flags == 0)
990 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar13505972019-01-24 15:04:48 +0100993#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 /*
995 * Try using iconv() if we can't convert internally.
996 */
997 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +0100998# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 )
1002 iconv_fd = (iconv_t)my_iconv_open(
1003 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar13505972019-01-24 15:04:48 +01001006#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 /*
1008 * Use the 'charconvert' expression when conversion is required
1009 * and we can't do it internally or with iconv().
1010 */
1011 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001012 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001013# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 )
1017 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001018# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001020# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // Skip conversion when it's already done (retry for wrong
1022 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (tmpname == NULL)
1024 {
1025 tmpname = readfile_charconvert(fname, fenc, &fd);
1026 if (tmpname == NULL)
1027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001028 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 advance_fenc = TRUE;
1030 if (fd < 0)
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001033 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 error = TRUE;
1035 goto failed;
1036 }
1037 goto retry;
1038 }
1039 }
1040 }
1041 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001045#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 )
1049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // Conversion wanted but we can't.
1051 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 advance_fenc = TRUE;
1053 goto retry;
1054 }
1055 }
1056 }
1057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 // Set "can_retry" when it's possible to rewind the file and try with
1059 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1060 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001061 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063 if (!skip_read)
1064 {
1065 linerest = 0;
1066 filesize = 0;
1067 skip_count = lines_to_skip;
1068 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001070#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001071 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1072 && curbuf->b_ffname != NULL
1073 && curbuf->b_p_udf
1074 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001075 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001076 && !read_stdin
1077 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001078 if (read_undo_file)
1079 sha256_start(&sha_ctx);
1080#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001081#ifdef FEAT_CRYPT
1082 if (curbuf->b_cryptstate != NULL)
1083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // Need to free the state, but keep the key, don't want to ask for
1085 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001086 crypt_free_state(curbuf->b_cryptstate);
1087 curbuf->b_cryptstate = NULL;
1088 }
1089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091
1092 while (!error && !got_int)
1093 {
1094 /*
1095 * We allocate as much space for the file as we can get, plus
1096 * space for the old line plus room for one terminating NUL.
1097 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001098 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001100 if (!skip_read)
1101 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001102#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001104#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 // Use buffer >= 64K. Add linerest to double the size if the
1106 // line gets very long, to avoid a lot of copying. But don't
1107 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001108 size = 0x10000L + linerest;
1109 if (size > 0x100000L)
1110 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001111#endif
1112 }
1113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001114 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001115 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001118 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 size = 1;
1120 }
1121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (!skip_read)
1124 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001125 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001127 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 FALSE)) != NULL)
1129 break;
1130 }
1131 if (new_buffer == NULL)
1132 {
1133 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1134 error = TRUE;
1135 break;
1136 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1139 vim_free(buffer);
1140 buffer = new_buffer;
1141 ptr = buffer + linerest;
1142 line_start = buffer;
1143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144 // May need room to translate into.
1145 // For iconv() we don't really know the required space, use a
1146 // factor ICONV_MULT.
1147 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1148 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1149 // become up to 4 bytes, size must be multiple of 2
1150 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1151 // multiple of 2
1152 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1153 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001154 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001155#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (iconv_fd != (iconv_t)-1)
1157 size = size / ICONV_MULT;
1158 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (fio_flags & FIO_LATIN1)
1161 size = size / 2;
1162 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1163 size = (size * 2 / 3) & ~1;
1164 else if (fio_flags & FIO_UCS4)
1165 size = (size * 2 / 3) & ~3;
1166 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001167 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001168#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001170 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001171#endif
1172#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001174 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (conv_restlen > 0)
1178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 mch_memmove(ptr, conv_rest, conv_restlen);
1181 ptr += conv_restlen;
1182 size -= conv_restlen;
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 if (read_buffer)
1186 {
1187 /*
1188 * Read bytes from curbuf. Used for converting text read
1189 * from stdin.
1190 */
1191 if (read_buf_lnum > from)
1192 size = 0;
1193 else
1194 {
1195 int n, ni;
1196 long tlen;
1197
1198 tlen = 0;
1199 for (;;)
1200 {
1201 p = ml_get(read_buf_lnum) + read_buf_col;
1202 n = (int)STRLEN(p);
1203 if ((int)tlen + n + 1 > size)
1204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001205 // Filled up to "size", append partial line.
1206 // Change NL to NUL to reverse the effect done
1207 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001208 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 for (ni = 0; ni < n; ++ni)
1210 {
1211 if (p[ni] == NL)
1212 ptr[tlen++] = NUL;
1213 else
1214 ptr[tlen++] = p[ni];
1215 }
1216 read_buf_col += n;
1217 break;
1218 }
1219 else
1220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 // Append whole line and new-line. Change NL
1222 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 for (ni = 0; ni < n; ++ni)
1224 {
1225 if (p[ni] == NL)
1226 ptr[tlen++] = NUL;
1227 else
1228 ptr[tlen++] = p[ni];
1229 }
1230 ptr[tlen++] = NL;
1231 read_buf_col = 0;
1232 if (++read_buf_lnum > from)
1233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001234 // When the last line didn't have an
1235 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (!curbuf->b_p_eol)
1237 --tlen;
1238 size = tlen;
1239 break;
1240 }
1241 }
1242 }
1243 }
1244 }
1245 else
1246 {
1247 /*
1248 * Read bytes from the file.
1249 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001250 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253#ifdef FEAT_CRYPT
1254 /*
1255 * At start of file: Check for magic number of encryption.
1256 */
1257 if (filesize == 0 && size > 0)
1258 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1259 &filesize, newfile, sfname,
1260 &did_ask_for_key);
1261 /*
1262 * Decrypt the read bytes. This is done before checking for
1263 * EOF because the crypt layer may be buffering.
1264 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001265 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1266 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001267 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001268# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001269 if (crypt_works_inplace(curbuf->b_cryptstate))
1270 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001271# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001272 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001273# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001274 }
1275 else
1276 {
1277 char_u *newptr = NULL;
1278 int decrypted_size;
1279
1280 decrypted_size = crypt_decode_alloc(
1281 curbuf->b_cryptstate, ptr, size, &newptr);
1282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001283 // If the crypt layer is buffering, not producing
1284 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001285 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001286 continue;
1287
1288 if (linerest == 0)
1289 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001290 // Simple case: reuse returned buffer (may be
1291 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001292 new_buffer = newptr;
1293 }
1294 else
1295 {
1296 long_u new_size;
1297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001298 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001299 new_size = (long_u)(decrypted_size + linerest + 1);
1300 new_buffer = lalloc(new_size, FALSE);
1301 if (new_buffer == NULL)
1302 {
1303 do_outofmem_msg(new_size);
1304 error = TRUE;
1305 break;
1306 }
1307
1308 mch_memmove(new_buffer, buffer, linerest);
1309 if (newptr != NULL)
1310 mch_memmove(new_buffer + linerest, newptr,
1311 decrypted_size);
1312 }
1313
1314 if (new_buffer != NULL)
1315 {
1316 vim_free(buffer);
1317 buffer = new_buffer;
1318 new_buffer = NULL;
1319 line_start = buffer;
1320 ptr = buffer + linerest;
1321 }
1322 size = decrypted_size;
1323 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001324# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001325 }
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (size <= 0)
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001333 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001334 /*
1335 * Reached end-of-file but some trailing bytes could
1336 * not be converted. Truncated file?
1337 */
1338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001339 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001340 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001341#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001342 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001343#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001344 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001345 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001346 if (can_retry)
1347 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001348 if (conv_error == 0)
1349 conv_error = curbuf->b_ml.ml_line_count
1350 - linecnt + 1;
1351 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001352 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001353 else if (illegal_byte == 0)
1354 illegal_byte = curbuf->b_ml.ml_line_count
1355 - linecnt + 1;
1356 if (bad_char_behavior == BAD_DROP)
1357 {
1358 *(ptr - conv_restlen) = NUL;
1359 conv_restlen = 0;
1360 }
1361 else
1362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001363 // Replace the trailing bytes with the replacement
1364 // character if we were converting; if we weren't,
1365 // leave the UTF8 checking code to do it, as it
1366 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001367 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001368#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001369 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001370#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001371 ))
1372 {
1373 while (conv_restlen > 0)
1374 {
1375 *(--ptr) = bad_char_behavior;
1376 --conv_restlen;
1377 }
1378 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001379 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001380#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001381 if (iconv_fd != (iconv_t)-1)
1382 {
1383 iconv_close(iconv_fd);
1384 iconv_fd = (iconv_t)-1;
1385 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001386#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001387 }
1388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 skip_read = FALSE;
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 /*
1394 * At start of file (or after crypt magic number): Check for BOM.
1395 * Also check for a BOM for other Unicode encodings, but not after
1396 * converting with 'charconvert' or when a BOM has already been
1397 * found.
1398 */
1399 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001400#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001401 || (cryptkey != NULL
1402 && filesize == crypt_get_header_len(
1403 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 )
1406 && (fio_flags == FIO_UCSBOM
1407 || (!curbuf->b_p_bomb
1408 && tmpname == NULL
1409 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1410 {
1411 char_u *ccname;
1412 int blen;
1413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001414 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (size < 2 || curbuf->b_p_bin)
1416 ccname = NULL;
1417 else
1418 ccname = check_for_bom(ptr, size, &blen,
1419 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1420 if (ccname != NULL)
1421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001422 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 filesize += blen;
1424 size -= blen;
1425 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001426 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001429 curbuf->b_start_bomb = TRUE;
1430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432
1433 if (fio_flags == FIO_UCSBOM)
1434 {
1435 if (ccname == NULL)
1436 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001437 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 advance_fenc = TRUE;
1439 }
1440 else
1441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001442 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (fenc_alloced)
1444 vim_free(fenc);
1445 fenc = ccname;
1446 fenc_alloced = FALSE;
1447 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001448 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 skip_read = TRUE;
1450 goto retry;
1451 }
1452 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001454 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001455 ptr -= conv_restlen;
1456 size += conv_restlen;
1457 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 /*
1459 * Break here for a read error or end-of-file.
1460 */
1461 if (size <= 0)
1462 break;
1463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar13505972019-01-24 15:04:48 +01001465#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (iconv_fd != (iconv_t)-1)
1467 {
1468 /*
1469 * Attempt conversion of the read bytes to 'encoding' using
1470 * iconv().
1471 */
1472 const char *fromp;
1473 char *top;
1474 size_t from_size;
1475 size_t to_size;
1476
1477 fromp = (char *)ptr;
1478 from_size = size;
1479 ptr += size;
1480 top = (char *)ptr;
1481 to_size = real_size - size;
1482
1483 /*
1484 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001485 * another conversion. Except for when there is no
1486 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001488 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1489 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1491 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001492 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001493 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001494 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001495 if (conv_error == 0)
1496 conv_error = readfile_linenr(linecnt,
1497 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001499 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001500 ++fromp;
1501 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001502 if (bad_char_behavior == BAD_KEEP)
1503 {
1504 *top++ = *(fromp - 1);
1505 --to_size;
1506 }
1507 else if (bad_char_behavior != BAD_DROP)
1508 {
1509 *top++ = bad_char_behavior;
1510 --to_size;
1511 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
1514 if (from_size > 0)
1515 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 // Some remaining characters, keep them for the next
1517 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1519 conv_restlen = (int)from_size;
1520 }
1521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 line_start = ptr - linerest;
1524 mch_memmove(line_start, buffer, (size_t)linerest);
1525 size = (long)((char_u *)top - ptr);
1526 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar4f974752019-02-17 17:44:42 +01001529#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (fio_flags & FIO_CODEPAGE)
1531 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001532 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001533 WCHAR ucs2buf[3];
1534 int ucs2len;
1535 int codepage = FIO_GET_CP(fio_flags);
1536 int bytelen;
1537 int found_bad;
1538 char replstr[2];
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 /*
1541 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001542 * a codepage, using standard MS-Windows functions. This
1543 * requires two steps:
1544 * 1. convert from 'fileencoding' to ucs-2
1545 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001547 * Because there may be illegal bytes AND an incomplete byte
1548 * sequence at the end, we may have to do the conversion one
1549 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001553 if (bad_char_behavior > 0)
1554 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001556 replstr[0] = '?';
1557 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
1559 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001560 * Move the bytes to the end of the buffer, so that we have
1561 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001563 src = ptr + real_size - size;
1564 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001566 /*
1567 * Do the conversion.
1568 */
1569 dst = ptr;
1570 size = size;
1571 while (size > 0)
1572 {
1573 found_bad = FALSE;
1574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001575# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001576 if (codepage == CP_UTF8)
1577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001578 // Handle CP_UTF8 input ourselves to be able to handle
1579 // trailing bytes properly.
1580 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001581 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001582 if (bytelen > size)
1583 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001584 // Only got some bytes of a character. Normally
1585 // it's put in "conv_rest", but if it's too long
1586 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001587 if (bytelen <= CONV_RESTLEN)
1588 break;
1589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001590 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001591 bytelen = size;
1592 found_bad = TRUE;
1593 }
1594 else
1595 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001596 int u8c = utf_ptr2char(src);
1597
Bram Moolenaar86e01082005-12-29 22:45:34 +00001598 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001599 found_bad = TRUE;
1600 ucs2buf[0] = u8c;
1601 ucs2len = 1;
1602 }
1603 }
1604 else
1605# endif
1606 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 // We don't know how long the byte sequence is, try
1608 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001609 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1610 ++bytelen)
1611 {
1612 ucs2len = MultiByteToWideChar(codepage,
1613 MB_ERR_INVALID_CHARS,
1614 (LPCSTR)src, bytelen,
1615 ucs2buf, 3);
1616 if (ucs2len > 0)
1617 break;
1618 }
1619 if (ucs2len == 0)
1620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001621 // If we have only one byte then it's probably an
1622 // incomplete byte sequence. Otherwise discard
1623 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001624 if (size == 1)
1625 break;
1626 found_bad = TRUE;
1627 bytelen = 1;
1628 }
1629 }
1630
1631 if (!found_bad)
1632 {
1633 int i;
1634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001635 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001636 if (enc_utf8)
1637 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001639 for (i = 0; i < ucs2len; ++i)
1640 dst += utf_char2bytes(ucs2buf[i], dst);
1641 }
1642 else
1643 {
1644 BOOL bad = FALSE;
1645 int dstlen;
1646
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001647 // From UCS-2 to "enc_codepage". If the
1648 // conversion uses the default character "?",
1649 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001650 dstlen = WideCharToMultiByte(enc_codepage, 0,
1651 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001652 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001653 replstr, &bad);
1654 if (bad)
1655 found_bad = TRUE;
1656 else
1657 dst += dstlen;
1658 }
1659 }
1660
1661 if (found_bad)
1662 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001663 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001664 if (can_retry)
1665 goto rewind_retry;
1666 if (conv_error == 0)
1667 conv_error = readfile_linenr(linecnt, ptr, dst);
1668 if (bad_char_behavior != BAD_DROP)
1669 {
1670 if (bad_char_behavior == BAD_KEEP)
1671 {
1672 mch_memmove(dst, src, bytelen);
1673 dst += bytelen;
1674 }
1675 else
1676 *dst++ = bad_char_behavior;
1677 }
1678 }
1679
1680 src += bytelen;
1681 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001683
1684 if (size > 0)
1685 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001686 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001687 mch_memmove(conv_rest, src, size);
1688 conv_restlen = size;
1689 }
1690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001692 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001695#endif
1696#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (fio_flags & FIO_MACROMAN)
1698 {
1699 /*
1700 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001701 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001703 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (fio_flags != 0)
1709 {
1710 int u8c;
1711 char_u *dest;
1712 char_u *tail = NULL;
1713
1714 /*
1715 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1716 * "enc_utf8" not set: Convert Unicode to Latin1.
1717 * Go from end to start through the buffer, because the number
1718 * of bytes may increase.
1719 * "dest" points to after where the UTF-8 bytes go, "p" points
1720 * to after the next character to convert.
1721 */
1722 dest = ptr + real_size;
1723 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1724 {
1725 p = ptr + size;
1726 if (fio_flags == FIO_UTF8)
1727 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001728 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 tail = ptr + size - 1;
1730 while (tail > ptr && (*tail & 0xc0) == 0x80)
1731 --tail;
1732 if (tail + utf_byte2len(*tail) <= ptr + size)
1733 tail = NULL;
1734 else
1735 p = tail;
1736 }
1737 }
1738 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1739 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001740 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 p = ptr + (size & ~1);
1742 if (size & 1)
1743 tail = p;
1744 if ((fio_flags & FIO_UTF16) && p > ptr)
1745 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001746 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (fio_flags & FIO_ENDIAN_L)
1748 {
1749 u8c = (*--p << 8);
1750 u8c += *--p;
1751 }
1752 else
1753 {
1754 u8c = *--p;
1755 u8c += (*--p << 8);
1756 }
1757 if (u8c >= 0xd800 && u8c <= 0xdbff)
1758 tail = p;
1759 else
1760 p += 2;
1761 }
1762 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001763 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001765 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 p = ptr + (size & ~3);
1767 if (size & 3)
1768 tail = p;
1769 }
1770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // If there is a trailing incomplete sequence move it to
1772 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (tail != NULL)
1774 {
1775 conv_restlen = (int)((ptr + size) - tail);
1776 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1777 size -= conv_restlen;
1778 }
1779
1780
1781 while (p > ptr)
1782 {
1783 if (fio_flags & FIO_LATIN1)
1784 u8c = *--p;
1785 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1786 {
1787 if (fio_flags & FIO_ENDIAN_L)
1788 {
1789 u8c = (*--p << 8);
1790 u8c += *--p;
1791 }
1792 else
1793 {
1794 u8c = *--p;
1795 u8c += (*--p << 8);
1796 }
1797 if ((fio_flags & FIO_UTF16)
1798 && u8c >= 0xdc00 && u8c <= 0xdfff)
1799 {
1800 int u16c;
1801
1802 if (p == ptr)
1803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001804 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (can_retry)
1806 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001807 if (conv_error == 0)
1808 conv_error = readfile_linenr(linecnt,
1809 ptr, p);
1810 if (bad_char_behavior == BAD_DROP)
1811 continue;
1812 if (bad_char_behavior != BAD_KEEP)
1813 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001816 // found second word of double-word, get the first
1817 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (fio_flags & FIO_ENDIAN_L)
1819 {
1820 u16c = (*--p << 8);
1821 u16c += *--p;
1822 }
1823 else
1824 {
1825 u16c = *--p;
1826 u16c += (*--p << 8);
1827 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001828 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1829 + (u8c & 0x3ff);
1830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001831 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (u16c < 0xd800 || u16c > 0xdbff)
1833 {
1834 if (can_retry)
1835 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001836 if (conv_error == 0)
1837 conv_error = readfile_linenr(linecnt,
1838 ptr, p);
1839 if (bad_char_behavior == BAD_DROP)
1840 continue;
1841 if (bad_char_behavior != BAD_KEEP)
1842 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845 }
1846 else if (fio_flags & FIO_UCS4)
1847 {
1848 if (fio_flags & FIO_ENDIAN_L)
1849 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001850 u8c = (unsigned)*--p << 24;
1851 u8c += (unsigned)*--p << 16;
1852 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 u8c += *--p;
1854 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001855 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001858 u8c += (unsigned)*--p << 8;
1859 u8c += (unsigned)*--p << 16;
1860 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001863 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 if (*--p < 0x80)
1866 u8c = *p;
1867 else
1868 {
1869 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001870 p -= len;
1871 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (len == 0)
1873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001874 // Not a valid UTF-8 character, retry with
1875 // another fenc when possible, otherwise just
1876 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (can_retry)
1878 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001879 if (conv_error == 0)
1880 conv_error = readfile_linenr(linecnt,
1881 ptr, p);
1882 if (bad_char_behavior == BAD_DROP)
1883 continue;
1884 if (bad_char_behavior != BAD_KEEP)
1885 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001889 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
1891 dest -= utf_char2len(u8c);
1892 (void)utf_char2bytes(u8c, dest);
1893 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 --dest;
1897 if (u8c >= 0x100)
1898 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001899 // character doesn't fit in latin1, retry with
1900 // another fenc when possible, otherwise just
1901 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001902 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001904 if (conv_error == 0)
1905 conv_error = readfile_linenr(linecnt, ptr, p);
1906 if (bad_char_behavior == BAD_DROP)
1907 ++dest;
1908 else if (bad_char_behavior == BAD_KEEP)
1909 *dest = u8c;
1910 else if (eap != NULL && eap->bad_char != 0)
1911 *dest = bad_char_behavior;
1912 else
1913 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 else
1916 *dest = u8c;
1917 }
1918 }
1919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001920 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 line_start = dest - linerest;
1922 mch_memmove(line_start, buffer, (size_t)linerest);
1923 size = (long)((ptr + real_size) - dest);
1924 ptr = dest;
1925 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001926 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001928 int incomplete_tail = FALSE;
1929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001931 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001933 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001934 int l;
1935
1936 if (todo <= 0)
1937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (*p >= 0x80)
1939 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // A length of 1 means it's an illegal byte. Accept
1941 // an incomplete character at the end though, the next
1942 // read() will get the next bytes, we'll check it
1943 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001944 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001945 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001947 // Avoid retrying with a different encoding when
1948 // a truncated file is more likely, or attempting
1949 // to read the rest of an incomplete sequence when
1950 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001951 if (p > ptr || filesize > 0)
1952 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 // Incomplete byte sequence, move it to conv_rest[]
1954 // and try to read the rest of it, unless we've
1955 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001956 if (p > ptr)
1957 {
1958 conv_restlen = todo;
1959 mch_memmove(conv_rest, p, conv_restlen);
1960 size -= conv_restlen;
1961 break;
1962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001964 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // Illegal byte. If we can try another encoding
1967 // do that, unless at EOF where a truncated
1968 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001969 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001970 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001971#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001973 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1974 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001975#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001976 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001977 if (conv_error == 0 && illegal_byte == 0)
1978 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001979
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001981 if (bad_char_behavior == BAD_DROP)
1982 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001983 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001984 --p;
1985 --size;
1986 }
1987 else if (bad_char_behavior != BAD_KEEP)
1988 *p = bad_char_behavior;
1989 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001990 else
1991 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001994 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001996 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01001999#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002000 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002002 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002004#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002005 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002006 advance_fenc = TRUE;
2007 file_rewind = TRUE;
2008 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 filesize += size;
2014
2015 /*
2016 * when reading the first part of a file: guess EOL type
2017 */
2018 if (fileformat == EOL_UNKNOWN)
2019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (try_dos || try_unix)
2022 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002023 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002024 if (try_mac)
2025 try_mac = 1;
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 for (p = ptr; p < ptr + size; ++p)
2028 {
2029 if (*p == NL)
2030 {
2031 if (!try_unix
2032 || (try_dos && p > ptr && p[-1] == CAR))
2033 fileformat = EOL_DOS;
2034 else
2035 fileformat = EOL_UNIX;
2036 break;
2037 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002038 else if (*p == CAR && try_mac)
2039 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
2041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (fileformat == EOL_UNIX && try_mac)
2044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002045 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 try_mac = 1;
2047 try_unix = 1;
2048 for (; p >= ptr && *p != CAR; p--)
2049 ;
2050 if (p >= ptr)
2051 {
2052 for (p = ptr; p < ptr + size; ++p)
2053 {
2054 if (*p == NL)
2055 try_unix++;
2056 else if (*p == CAR)
2057 try_mac++;
2058 }
2059 if (try_mac > try_unix)
2060 fileformat = EOL_MAC;
2061 }
2062 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002063 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // Looking for CR but found no end-of-line markers at
2065 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002066 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (fileformat == EOL_UNKNOWN && try_mac)
2071 fileformat = EOL_MAC;
2072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002073 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (fileformat == EOL_UNKNOWN)
2075 fileformat = default_fileformat();
2076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002077 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002078 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 set_fileformat(fileformat, OPT_LOCAL);
2080 }
2081 }
2082
2083 /*
2084 * This loop is executed once for every character read.
2085 * Keep it fast!
2086 */
2087 if (fileformat == EOL_MAC)
2088 {
2089 --ptr;
2090 while (++ptr, --size >= 0)
2091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002092 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if ((c = *ptr) != NUL && c != CAR && c != NL)
2094 continue;
2095 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002096 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002098 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 else
2100 {
2101 if (skip_count == 0)
2102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 len = (colnr_T) (ptr - line_start + 1);
2105 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2106 {
2107 error = TRUE;
2108 break;
2109 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002110#ifdef FEAT_PERSISTENT_UNDO
2111 if (read_undo_file)
2112 sha256_update(&sha_ctx, line_start, len);
2113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 ++lnum;
2115 if (--read_count == 0)
2116 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002117 error = TRUE; // break loop
2118 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 break;
2120 }
2121 }
2122 else
2123 --skip_count;
2124 line_start = ptr + 1;
2125 }
2126 }
2127 }
2128 else
2129 {
2130 --ptr;
2131 while (++ptr, --size >= 0)
2132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 continue;
2135 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002136 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else
2138 {
2139 if (skip_count == 0)
2140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002141 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 len = (colnr_T)(ptr - line_start + 1);
2143 if (fileformat == EOL_DOS)
2144 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002145 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002147 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ptr[-1] = NUL;
2149 --len;
2150 }
2151 /*
2152 * Reading in Dos format, but no CR-LF found!
2153 * When 'fileformats' includes "unix", delete all
2154 * the lines read so far and start all over again.
2155 * Otherwise give an error message later.
2156 */
2157 else if (ff_error != EOL_DOS)
2158 {
2159 if ( try_unix
2160 && !read_stdin
2161 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002162 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2163 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 set_fileformat(EOL_UNIX, OPT_LOCAL);
2168 file_rewind = TRUE;
2169 keep_fileformat = TRUE;
2170 goto retry;
2171 }
2172 ff_error = EOL_DOS;
2173 }
2174 }
2175 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2176 {
2177 error = TRUE;
2178 break;
2179 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002180#ifdef FEAT_PERSISTENT_UNDO
2181 if (read_undo_file)
2182 sha256_update(&sha_ctx, line_start, len);
2183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 ++lnum;
2185 if (--read_count == 0)
2186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002187 error = TRUE; // break loop
2188 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 break;
2190 }
2191 }
2192 else
2193 --skip_count;
2194 line_start = ptr + 1;
2195 }
2196 }
2197 }
2198 linerest = (long)(ptr - line_start);
2199 ui_breakcheck();
2200 }
2201
2202failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (error && read_count == 0)
2205 error = FALSE;
2206
2207 /*
2208 * If we get EOF in the middle of a line, note the fact and
2209 * complete the line ourselves.
2210 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2211 */
2212 if (!error
2213 && !got_int
2214 && linerest != 0
2215 && !(!curbuf->b_p_bin
2216 && fileformat == EOL_DOS
2217 && *line_start == Ctrl_Z
2218 && ptr == line_start + 1))
2219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002221 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 curbuf->b_p_eol = FALSE;
2223 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002224 len = (colnr_T)(ptr - line_start + 1);
2225 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 error = TRUE;
2227 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002228 {
2229#ifdef FEAT_PERSISTENT_UNDO
2230 if (read_undo_file)
2231 sha256_update(&sha_ctx, line_start, len);
2232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002241 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002242 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002243 crypt_free_state(curbuf->b_cryptstate);
2244 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002245 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002246 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2247 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 // Don't set cryptkey to NULL, it's used below as a flag that
2249 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#endif
2251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 // If editing a new file: set 'fenc' for the current buffer.
2253 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002256 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if (fenc_alloced)
2258 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002259#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#endif
2263
2264 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002265 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002266#ifdef HAVE_FD_CLOEXEC
2267 else
2268 {
2269 int fdflags = fcntl(fd, F_GETFD);
2270 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002271 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002272 }
2273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 vim_free(buffer);
2275
2276#ifdef HAVE_DUP
2277 if (read_stdin)
2278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002279 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002281 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283#endif
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (tmpname != NULL)
2286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 vim_free(tmpname);
2289 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
2292 /*
2293 * In recovery mode everything but autocommands is skipped.
2294 */
2295 if (!recoverymode)
2296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2299 {
2300#ifdef FEAT_NETBEANS_INTG
2301 netbeansFireChanges = 0;
2302#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002303 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#ifdef FEAT_NETBEANS_INTG
2305 netbeansFireChanges = 1;
2306#endif
2307 --linecnt;
2308 }
2309 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2310 if (filesize == 0)
2311 linecnt = 0;
2312 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002315#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // After reading the text into the buffer the diff info needs to
2317 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002318 diff_invalidate(curbuf);
2319#endif
2320#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002321 // All folds in the window are invalid now. Mark them for update
2322 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002323 foldUpdateAll(curwin);
2324#endif
2325 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 appended_lines_mark(from, linecnt);
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifndef ALWAYS_USE_GUI
2330 /*
2331 * If we were reading from the same terminal as where messages go,
2332 * the screen will have been messed up.
2333 * Switch on raw mode now and clear the screen.
2334 */
2335 if (read_stdin)
2336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002337 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 starttermcap();
2339 screenclear();
2340 }
2341#endif
2342
2343 if (got_int)
2344 {
2345 if (!(flags & READ_DUMMY))
2346 {
2347 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2348 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002349 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 msg_scroll = msg_save;
2352#ifdef FEAT_VIMINFO
2353 check_marks_read();
2354#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357
2358 if (!filtering && !(flags & READ_DUMMY))
2359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 c = FALSE;
2362
2363#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002364 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 STRCAT(IObuff, _("[fifo]"));
2367 c = TRUE;
2368 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002369 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 STRCAT(IObuff, _("[socket]"));
2372 c = TRUE;
2373 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002374# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002376 {
2377 STRCAT(IObuff, _("[character special]"));
2378 c = TRUE;
2379 }
2380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381#endif
2382 if (curbuf->b_p_ro)
2383 {
2384 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2385 c = TRUE;
2386 }
2387 if (read_no_eol_lnum)
2388 {
2389 msg_add_eol();
2390 c = TRUE;
2391 }
2392 if (ff_error == EOL_DOS)
2393 {
2394 STRCAT(IObuff, _("[CR missing]"));
2395 c = TRUE;
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (split)
2398 {
2399 STRCAT(IObuff, _("[long lines split]"));
2400 c = TRUE;
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (notconverted)
2403 {
2404 STRCAT(IObuff, _("[NOT converted]"));
2405 c = TRUE;
2406 }
2407 else if (converted)
2408 {
2409 STRCAT(IObuff, _("[converted]"));
2410 c = TRUE;
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_CRYPT
2413 if (cryptkey != NULL)
2414 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002415 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 c = TRUE;
2417 }
2418#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002419 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002421 sprintf((char *)IObuff + STRLEN(IObuff),
2422 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 c = TRUE;
2424 }
2425 else if (illegal_byte > 0)
2426 {
2427 sprintf((char *)IObuff + STRLEN(IObuff),
2428 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2429 c = TRUE;
2430 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002431 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433 STRCAT(IObuff, _("[READ ERRORS]"));
2434 c = TRUE;
2435 }
2436 if (msg_add_fileformat(fileformat))
2437 c = TRUE;
2438#ifdef FEAT_CRYPT
2439 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002440 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002441 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 else
2443#endif
2444 msg_add_lines(c, (long)linecnt, filesize);
2445
Bram Moolenaard23a8232018-02-10 18:45:26 +01002446 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 msg_scrolled_ign = TRUE;
2448#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002449 // Don't show the message when reading stdin, it would end up in a
2450 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (read_stdin || read_buffer)
2452 p = msg_may_trunc(FALSE, IObuff);
2453 else
2454#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002455 {
2456 if (msg_col > 0)
2457 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002458 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002461 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002462 // Need to repeat the message after redrawing when:
2463 // - When reading from stdin (the screen will be cleared next).
2464 // - When restart_edit is set (otherwise there will be a delay
2465 // before redrawing).
2466 // - When the screen was scrolled but there is no wait-return
2467 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002468 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 msg_scrolled_ign = FALSE;
2470 }
2471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002472 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002474 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002475 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 curbuf->b_p_ro = TRUE;
2477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /*
2481 * In Ex mode: cursor at last new line.
2482 * Otherwise: cursor at first new line.
2483 */
2484 if (exmode_active)
2485 curwin->w_cursor.lnum = from + linecnt;
2486 else
2487 curwin->w_cursor.lnum = from + 1;
2488 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002489 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002491 if (!cmdmod.lockmarks)
2492 {
2493 // Set '[ and '] marks to the newly read lines.
2494 curbuf->b_op_start.lnum = from + 1;
2495 curbuf->b_op_start.col = 0;
2496 curbuf->b_op_end.lnum = from + linecnt;
2497 curbuf->b_op_end.col = 0;
2498 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002499
Bram Moolenaar4f974752019-02-17 17:44:42 +01002500#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002501 /*
2502 * Work around a weird problem: When a file has two links (only
2503 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002504 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002505 * It's correct again after reading the file, thus reset the timestamp
2506 * here.
2507 */
2508 if (newfile && !read_stdin && !read_buffer
2509 && mch_stat((char *)fname, &st) >= 0)
2510 {
2511 buf_store_time(curbuf, &st, fname);
2512 curbuf->b_mtime_read = curbuf->b_mtime;
2513 }
2514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516 msg_scroll = msg_save;
2517
2518#ifdef FEAT_VIMINFO
2519 /*
2520 * Get the marks before executing autocommands, so they can be used there.
2521 */
2522 check_marks_read();
2523#endif
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002526 * We remember if the last line of the read didn't have
2527 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2528 * or writing the read again with 'binary' on. The latter is required
2529 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002531 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002533 // When reloading a buffer put the cursor at the first line that is
2534 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002535 if (flags & READ_KEEP_UNDO)
2536 u_find_first_changed();
2537
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002538#ifdef FEAT_PERSISTENT_UNDO
2539 /*
2540 * When opening a new file locate undo info and read it.
2541 */
2542 if (read_undo_file)
2543 {
2544 char_u hash[UNDO_HASH_SIZE];
2545
2546 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002547 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002548 }
2549#endif
2550
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002551 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 int m = msg_scroll;
2554 int n = msg_scrolled;
2555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002556 // Save the fileformat now, otherwise the buffer will be considered
2557 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002558 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 save_file_ff(curbuf);
2560
2561 /*
2562 * The output from the autocommands should not overwrite anything and
2563 * should not be overwritten: Set msg_scroll, restore its value if no
2564 * output was done.
2565 */
2566 msg_scroll = TRUE;
2567 if (filtering)
2568 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2569 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002570 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2573 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002574 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2575 /*
2576 * EVENT_FILETYPE was not triggered but the buffer already has a
2577 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2578 */
2579 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2580 TRUE, curbuf);
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 else
2583 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2584 FALSE, NULL, eap);
2585 if (msg_scrolled == n)
2586 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002587# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593 if (recoverymode && error)
2594 return FAIL;
2595 return OK;
2596}
2597
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002598#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002599/*
2600 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2601 * which is the name of files used for process substitution output by
2602 * some shells on some operating systems, e.g., bash on SunOS.
2603 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2604 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002606is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002607{
2608 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2609 && VIM_ISDIGIT(fname[8])
2610 && *skipdigits(fname + 9) == NUL
2611 && (fname[9] != NUL
2612 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2613}
2614#endif
2615
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002616/*
2617 * From the current line count and characters read after that, estimate the
2618 * line number where we are now.
2619 * Used for error messages that include a line number.
2620 */
2621 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002622readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002623 linenr_T linecnt, // line count before reading more bytes
2624 char_u *p, // start of more bytes read
2625 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002626{
2627 char_u *s;
2628 linenr_T lnum;
2629
2630 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2631 for (s = p; s < endp; ++s)
2632 if (*s == '\n')
2633 ++lnum;
2634 return lnum;
2635}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002638 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2639 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * Returns OK or FAIL.
2641 */
2642 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002643prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
Bram Moolenaar13505972019-01-24 15:04:48 +01002645 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (eap->cmd == NULL)
2647 return FAIL;
2648
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002649 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2650 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002651 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002652 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002653
2654 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002655 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002656 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return OK;
2658}
2659
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002660/*
2661 * Set default or forced 'fileformat' and 'binary'.
2662 */
2663 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002664set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002665{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002666 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002667 if (set_options)
2668 {
2669 if (eap != NULL && eap->force_ff != 0)
2670 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2671 else if (*p_ffs != NUL)
2672 set_fileformat(default_fileformat(), OPT_LOCAL);
2673 }
2674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002675 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002676 if (eap != NULL && eap->force_bin != 0)
2677 {
2678 int oldval = curbuf->b_p_bin;
2679
2680 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2681 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2682 }
2683}
2684
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002685/*
2686 * Set forced 'fileencoding'.
2687 */
2688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002689set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002690{
2691 if (eap->force_enc != 0)
2692 {
2693 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2694
2695 if (fenc != NULL)
2696 set_string_option_direct((char_u *)"fenc", -1,
2697 fenc, OPT_FREE|OPT_LOCAL, 0);
2698 vim_free(fenc);
2699 }
2700}
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702/*
2703 * Find next fileencoding to use from 'fileencodings'.
2704 * "pp" points to fenc_next. It's advanced to the next item.
2705 * When there are no more items, an empty string is returned and *pp is set to
2706 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002707 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2708 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
2710 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002711next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 char_u *p;
2714 char_u *r;
2715
Bram Moolenaarf077db22019-08-13 00:18:24 +02002716 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (**pp == NUL)
2718 {
2719 *pp = NULL;
2720 return (char_u *)"";
2721 }
2722 p = vim_strchr(*pp, ',');
2723 if (p == NULL)
2724 {
2725 r = enc_canonize(*pp);
2726 *pp += STRLEN(*pp);
2727 }
2728 else
2729 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002730 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 *pp = p + 1;
2732 if (r != NULL)
2733 {
2734 p = enc_canonize(r);
2735 vim_free(r);
2736 r = p;
2737 }
2738 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002739 if (r != NULL)
2740 *alloced = TRUE;
2741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002743 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 r = (char_u *)"";
2745 *pp = NULL;
2746 }
2747 return r;
2748}
2749
Bram Moolenaar13505972019-01-24 15:04:48 +01002750#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751/*
2752 * Convert a file with the 'charconvert' expression.
2753 * This closes the file which is to be read, converts it and opens the
2754 * resulting file for reading.
2755 * Returns name of the resulting converted file (the caller should delete it
2756 * after reading it).
2757 * Returns NULL if the conversion failed ("*fdp" is not set) .
2758 */
2759 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002760readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002761 char_u *fname, // name of input file
2762 char_u *fenc, // converted from
2763 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002766 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002768 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002770 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 else
2772 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002773 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 *fdp = -1;
2775 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2776 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002777 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2779 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002780 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
2782
2783 if (errmsg != NULL)
2784 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002785 // Don't use emsg(), it breaks mappings, the retry with
2786 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002787 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (tmpname != NULL)
2789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002790 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002791 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793 }
2794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002795 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (*fdp < 0)
2797 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2798
2799 return tmpname;
2800}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#endif
2802
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002803#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002805 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2807 * *filesizep are updated.
2808 * Return the (new) encryption key, NULL for no encryption.
2809 */
2810 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002811check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002812 char_u *cryptkey, // previous encryption key or NULL
2813 char_u *ptr, // pointer to read bytes
2814 long *sizep, // length of read bytes
2815 off_T *filesizep, // nr of bytes used from file
2816 int newfile, // editing a new buffer
2817 char_u *fname, // file name to display
2818 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002820 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002821 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002822
2823 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002825 // Mark the buffer as read-only until the decryption has taken place.
2826 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002827 curbuf->b_p_ro = TRUE;
2828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002830 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002831 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
2833 if (*curbuf->b_p_key)
2834 cryptkey = curbuf->b_p_key;
2835 else
2836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002837 // When newfile is TRUE, store the typed key in the 'key'
2838 // option and don't free it. bf needs hash of the key saved.
2839 // Don't ask for the key again when first time Enter was hit.
2840 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002841 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002842 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002843 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002844 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002845 *did_ask = TRUE;
2846
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002847 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (cryptkey != NULL && *cryptkey == NUL)
2849 {
2850 if (cryptkey != curbuf->b_p_key)
2851 vim_free(cryptkey);
2852 cryptkey = NULL;
2853 }
2854 }
2855 }
2856
2857 if (cryptkey != NULL)
2858 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002859 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002860
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002861 curbuf->b_cryptstate = crypt_create_from_header(
2862 method, cryptkey, ptr);
2863 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002865 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002866 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002867 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002868 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002869 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002870 *filesizep += header_len;
2871 *sizep -= header_len;
2872 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002875 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002878 // When starting to edit a new file which does not have encryption, clear
2879 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002880 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2882
2883 return cryptkey;
2884}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002885#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002888 * Return TRUE if a file appears to be read-only from the file permissions.
2889 */
2890 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002891check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002892 char_u *fname, // full path to file
2893 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002894{
2895#ifndef USE_MCH_ACCESS
2896 int fd = 0;
2897#endif
2898
2899 return (
2900#ifdef USE_MCH_ACCESS
2901# ifdef UNIX
2902 (perm & 0222) == 0 ||
2903# endif
2904 mch_access((char *)fname, W_OK)
2905#else
2906 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2907 ? TRUE : (close(fd), FALSE)
2908#endif
2909 );
2910}
2911
Bram Moolenaara7870192019-02-14 12:56:36 +01002912#if defined(HAVE_FSYNC) || defined(PROTO)
2913/*
2914 * Call fsync() with Mac-specific exception.
2915 * Return fsync() result: zero for success.
2916 */
2917 int
2918vim_fsync(int fd)
2919{
2920 int r;
2921
2922# ifdef MACOS_X
2923 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002924 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002925# endif
2926 r = fsync(fd);
2927 return r;
2928}
2929#endif
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002932 * Set the name of the current buffer. Use when the buffer doesn't have a
2933 * name and a ":r" or ":w" command with a file name is used.
2934 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002935 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002936set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002937{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002938 buf_T *buf = curbuf;
2939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002940 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002941 if (curbuf->b_p_bl)
2942 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2943 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002944#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002946 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002947#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002948 if (curbuf != buf)
2949 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002950 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002951 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002952 return FAIL;
2953 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002954
2955 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2956 curbuf->b_flags |= BF_NOTEDITED;
2957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002958 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002959 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2960 if (curbuf->b_p_bl)
2961 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002962#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002964 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002965#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002967 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002968 if (*curbuf->b_p_ft == NUL)
2969 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002970 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002971 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002972 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002973 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002974
2975 return OK;
2976}
2977
2978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 * Put file name into IObuff with quotes.
2980 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002982msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 if (fname == NULL)
2985 fname = (char_u *)"-stdin-";
2986 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2987 IObuff[0] = '"';
2988 STRCAT(IObuff, "\" ");
2989}
2990
2991/*
2992 * Append message for text mode to IObuff.
2993 * Return TRUE if something appended.
2994 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002995 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002996msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998#ifndef USE_CRNL
2999 if (eol_type == EOL_DOS)
3000 {
3001 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3002 return TRUE;
3003 }
3004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (eol_type == EOL_MAC)
3006 {
3007 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3008 return TRUE;
3009 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003010#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (eol_type == EOL_UNIX)
3012 {
3013 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3014 return TRUE;
3015 }
3016#endif
3017 return FALSE;
3018}
3019
3020/*
3021 * Append line and character count to IObuff.
3022 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003023 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003024msg_add_lines(
3025 int insert_space,
3026 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003027 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 char_u *p;
3030
3031 p = IObuff + STRLEN(IObuff);
3032
3033 if (insert_space)
3034 *p++ = ' ';
3035 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003036 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003037 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else
3039 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003040 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003042 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003043 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003044 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046}
3047
3048/*
3049 * Append message for missing line separator to IObuff.
3050 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003052msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3055}
3056
Bram Moolenaar473952e2019-09-28 16:30:04 +02003057 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003058time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003060#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003061 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3062 // the seconds. Since the roundoff is done when flushing the inode, the
3063 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 return (t1 - t2 > 1 || t2 - t1 > 1);
3065#else
3066 return (t1 != t2);
3067#endif
3068}
3069
3070/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003071 * Return TRUE if file encoding "fenc" requires conversion from or to
3072 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003075need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003077 int same_encoding;
3078 int enc_flags;
3079 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003081 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003082 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003083 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003084 fenc_flags = 0;
3085 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003086 else
3087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003088 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3089 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003090 enc_flags = get_fio_flags(p_enc);
3091 fenc_flags = get_fio_flags(fenc);
3092 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3093 }
3094 if (same_encoding)
3095 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003096 // Specified encoding matches with 'encoding'. This requires
3097 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003098 return enc_unicode != 0;
3099 }
3100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003101 // Encodings differ. However, conversion is not needed when 'enc' is any
3102 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003103 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104}
3105
3106/*
3107 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3108 * internal conversion.
3109 * if "ptr" is an empty string, use 'encoding'.
3110 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003111 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int prop;
3115
3116 if (*ptr == NUL)
3117 ptr = p_enc;
3118
3119 prop = enc_canon_props(ptr);
3120 if (prop & ENC_UNICODE)
3121 {
3122 if (prop & ENC_2BYTE)
3123 {
3124 if (prop & ENC_ENDIAN_L)
3125 return FIO_UCS2 | FIO_ENDIAN_L;
3126 return FIO_UCS2;
3127 }
3128 if (prop & ENC_4BYTE)
3129 {
3130 if (prop & ENC_ENDIAN_L)
3131 return FIO_UCS4 | FIO_ENDIAN_L;
3132 return FIO_UCS4;
3133 }
3134 if (prop & ENC_2WORD)
3135 {
3136 if (prop & ENC_ENDIAN_L)
3137 return FIO_UTF16 | FIO_ENDIAN_L;
3138 return FIO_UTF16;
3139 }
3140 return FIO_UTF8;
3141 }
3142 if (prop & ENC_LATIN1)
3143 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003144 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return 0;
3146}
3147
Bram Moolenaar473952e2019-09-28 16:30:04 +02003148#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149/*
3150 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3151 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3152 * Used for conversion between 'encoding' and 'fileencoding'.
3153 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003154 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003155get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 int cp;
3158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003159 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (!enc_utf8 && enc_codepage <= 0)
3161 return 0;
3162
3163 cp = encname2codepage(ptr);
3164 if (cp == 0)
3165 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003166# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (STRCMP(ptr, "utf-8") == 0)
3168 cp = CP_UTF8;
3169 else
3170# endif
3171 return 0;
3172 }
3173 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3174}
3175#endif
3176
Bram Moolenaar473952e2019-09-28 16:30:04 +02003177#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178/*
3179 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3180 * needed for the internal conversion to/from utf-8 or latin1.
3181 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003182 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003183get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3186 && (enc_canon_props(ptr) & ENC_MACROMAN))
3187 return FIO_MACROMAN;
3188 return 0;
3189}
3190#endif
3191
3192/*
3193 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3194 * "size" must be at least 2.
3195 * Return the name of the encoding and set "*lenp" to the length.
3196 * Returns NULL when no BOM found.
3197 */
3198 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003199check_for_bom(
3200 char_u *p,
3201 long size,
3202 int *lenp,
3203 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
3205 char *name = NULL;
3206 int len = 2;
3207
3208 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003209 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003211 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 len = 3;
3213 }
3214 else if (p[0] == 0xff && p[1] == 0xfe)
3215 {
3216 if (size >= 4 && p[2] == 0 && p[3] == 0
3217 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003219 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 len = 4;
3221 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003222 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003223 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003224 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003225 // utf-16le is preferred, it also works for ucs-2le text
3226 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228 else if (p[0] == 0xfe && p[1] == 0xff
3229 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003231 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003232 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003233 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003234 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003235 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3238 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 len = 4;
3242 }
3243
3244 *lenp = len;
3245 return (char_u *)name;
3246}
3247
3248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 * Try to find a shortname by comparing the fullname with the current
3250 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003251 * Returns "full_path" or pointer into "full_path" if shortened.
3252 */
3253 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003254shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003255{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003256 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003257 char_u *p = full_path;
3258
Bram Moolenaard9462e32011-04-11 21:35:11 +02003259 dirname = alloc(MAXPATHL);
3260 if (dirname == NULL)
3261 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003262 if (mch_dirname(dirname, MAXPATHL) == OK)
3263 {
3264 p = shorten_fname(full_path, dirname);
3265 if (p == NULL || *p == NUL)
3266 p = full_path;
3267 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003268 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003269 return p;
3270}
3271
3272/*
3273 * Try to find a shortname by comparing the fullname with the current
3274 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 * Returns NULL if not shorter name possible, pointer into "full_path"
3276 * otherwise.
3277 */
3278 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003279shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 int len;
3282 char_u *p;
3283
3284 if (full_path == NULL)
3285 return NULL;
3286 len = (int)STRLEN(dir_name);
3287 if (fnamencmp(dir_name, full_path, len) == 0)
3288 {
3289 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003290#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003292 * MS-Windows: when a file is in the root directory, dir_name will end
3293 * in a slash, since C: by itself does not define a specific dir. In
3294 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 */
3296 if (!((len > 2) && (*(p - 2) == ':')))
3297#endif
3298 {
3299 if (vim_ispathsep(*p))
3300 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003301#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 else
3303 p = NULL;
3304#endif
3305 }
3306 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003307#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 /*
3309 * When using a file in the current drive, remove the drive name:
3310 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3311 * a floppy from "A:\dir" to "B:\dir".
3312 */
3313 else if (len > 3
3314 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3315 && full_path[1] == ':'
3316 && vim_ispathsep(full_path[2]))
3317 p = full_path + 2;
3318#endif
3319 else
3320 p = NULL;
3321 return p;
3322}
3323
3324/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003325 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 * When "force" is TRUE: Use full path from now on for files currently being
3327 * edited, both for file name and swap file name. Try to shorten the file
3328 * names a bit, if safe to do so.
3329 * When "force" is FALSE: Only try to shorten absolute file names.
3330 * For buffers that have buftype "nofile" or "scratch": never change the file
3331 * name.
3332 */
3333 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003334shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3335{
3336 char_u *p;
3337
3338 if (buf->b_fname != NULL
3339#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003340 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003341#endif
3342 && !path_with_url(buf->b_fname)
3343 && (force
3344 || buf->b_sfname == NULL
3345 || mch_isFullName(buf->b_sfname)))
3346 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003347 if (buf->b_sfname != buf->b_ffname)
3348 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003349 p = shorten_fname(buf->b_ffname, dirname);
3350 if (p != NULL)
3351 {
3352 buf->b_sfname = vim_strsave(p);
3353 buf->b_fname = buf->b_sfname;
3354 }
3355 if (p == NULL || buf->b_fname == NULL)
3356 buf->b_fname = buf->b_ffname;
3357 }
3358}
3359
3360/*
3361 * Shorten filenames for all buffers.
3362 */
3363 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003364shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 char_u dirname[MAXPATHL];
3367 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003370 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003372 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003374 // Always make the swap file name a full path, a "nofile" buffer may
3375 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003376 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003379 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003380#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003381 popup_update_preview_title();
3382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
3385#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3386 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003387 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 || defined(PROTO)
3389/*
3390 * Shorten all filenames in "fnames[count]" by current directory.
3391 */
3392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003393shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 int i;
3396 char_u dirname[MAXPATHL];
3397 char_u *p;
3398
3399 if (fnames == NULL || count < 1)
3400 return;
3401 mch_dirname(dirname, sizeof(dirname));
3402 for (i = 0; i < count; ++i)
3403 {
3404 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003406 // shorten_fname() returns pointer in given "fnames[i]". If free
3407 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3408 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 p = vim_strsave(p);
3410 vim_free(fnames[i]);
3411 fnames[i] = p;
3412 }
3413 }
3414}
3415#endif
3416
3417/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003418 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 * fo_o_h.ext for MSDOS or when shortname option set.
3420 *
3421 * Assumed that fname is a valid name found in the filesystem we assure that
3422 * the return value is a different name and ends in 'ext'.
3423 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3424 * characters otherwise.
3425 * Space for the returned name is allocated, must be freed later.
3426 * Returns NULL when out of memory.
3427 */
3428 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003429modname(
3430 char_u *fname,
3431 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003432 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003434 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 fname, ext, prepend_dot);
3436}
3437
3438 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003439buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003440 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003441 char_u *fname,
3442 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003443 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 char_u *retval;
3446 char_u *s;
3447 char_u *e;
3448 char_u *ptr;
3449 int fnamelen, extlen;
3450
3451 extlen = (int)STRLEN(ext);
3452
3453 /*
3454 * If there is no file name we must get the name of the current directory
3455 * (we need the full path in case :cd is used).
3456 */
3457 if (fname == NULL || *fname == NUL)
3458 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003459 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (retval == NULL)
3461 return NULL;
3462 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3463 (fnamelen = (int)STRLEN(retval)) == 0)
3464 {
3465 vim_free(retval);
3466 return NULL;
3467 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003468 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 retval[fnamelen++] = PATHSEP;
3471 retval[fnamelen] = NUL;
3472 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003473 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 else
3476 {
3477 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003478 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (retval == NULL)
3480 return NULL;
3481 STRCPY(retval, fname);
3482#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003483 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484#endif
3485 }
3486
3487 /*
3488 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3489 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3490 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3491 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3492 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003493 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003495 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003496 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003499 {
3500 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003505 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3507 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509 s = ptr + STRLEN(ptr);
3510
3511 /*
3512 * For 8.3 file names we may have to reduce the length.
3513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 /*
3517 * If there is no file name, or the file name ends in '/', and the
3518 * extension starts with '.', put a '_' before the dot, because just
3519 * ".ext" is invalid.
3520 */
3521 if (fname == NULL || *fname == NUL
3522 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 *s++ = '_';
3526 }
3527 /*
3528 * If the extension starts with '.', truncate the base name at 8
3529 * characters
3530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003533 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 s = ptr + 8;
3536 *s = '\0';
3537 }
3538 }
3539 /*
3540 * If the extension doesn't start with '.', and the file name
3541 * doesn't have an extension yet, append a '.'
3542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else if ((e = vim_strchr(ptr, '.')) == NULL)
3544 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /*
3546 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003547 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
3549 else if ((int)STRLEN(e) + extlen > 4)
3550 s = e + 4 - extlen;
3551 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003552#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 /*
3554 * If there is no file name, and the extension starts with '.', put a
3555 * '_' before the dot, because just ".ext" may be invalid if it's on a
3556 * FAT partition, and on HPFS it doesn't matter.
3557 */
3558 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3559 *s++ = '_';
3560#endif
3561
3562 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003563 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 * ext can start with '.' and cannot exceed 3 more characters.
3565 */
3566 STRCPY(s, ext);
3567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 /*
3569 * Prepend the dot.
3570 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003571 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003573 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 /*
3578 * Check that, after appending the extension, the file name is really
3579 * different.
3580 */
3581 if (fname != NULL && STRCMP(fname, retval) == 0)
3582 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003583 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 while (--s >= ptr)
3585 {
3586 if (*s != '_')
3587 {
3588 *s = '_';
3589 break;
3590 }
3591 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003592 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 *ptr = 'v';
3594 }
3595 return retval;
3596}
3597
3598/*
3599 * Like fgets(), but if the file line is too long, it is truncated and the
3600 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003601 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
3603 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003604vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
3606 char *eof;
3607#define FGETS_SIZE 200
3608 char tbuf[FGETS_SIZE];
3609
3610 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3613 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003614 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003616 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 do
3618 {
3619 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003620 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3622 }
3623 return (eof == NULL);
3624}
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626/*
3627 * rename() only works if both files are on the same file system, this
3628 * function will (attempts to?) copy the file across if rename fails -- webb
3629 * Return -1 for failure, 0 for success.
3630 */
3631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003632vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 int fd_in;
3635 int fd_out;
3636 int n;
3637 char *errmsg = NULL;
3638 char *buffer;
3639#ifdef AMIGA
3640 BPTR flock;
3641#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003642 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003643 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003644#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003645 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003646#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003647 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003650 * When the names are identical, there is nothing to do. When they refer
3651 * to the same file (ignoring case and slash/backslash differences) but
3652 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
3654 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003655 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003656 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003657 use_tmp_file = TRUE;
3658 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003659 return 0;
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 /*
3663 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3664 */
3665 if (mch_stat((char *)from, &st) < 0)
3666 return -1;
3667
Bram Moolenaar3576da72008-12-30 15:15:57 +00003668#ifdef UNIX
3669 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003670 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003672 // It's possible for the source and destination to be the same file.
3673 // This happens when "from" and "to" differ in case and are on a FAT32
3674 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003675 if (mch_stat((char *)to, &st_to) >= 0
3676 && st.st_dev == st_to.st_dev
3677 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003678 use_tmp_file = TRUE;
3679 }
3680#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003681#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003682 {
3683 BY_HANDLE_FILE_INFORMATION info1, info2;
3684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003685 // It's possible for the source and destination to be the same file.
3686 // In that case go through a temp file name. This makes rename("foo",
3687 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003688 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3689 && win32_fileinfo(to, &info2) == FILEINFO_OK
3690 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3691 && info1.nFileIndexHigh == info2.nFileIndexHigh
3692 && info1.nFileIndexLow == info2.nFileIndexLow)
3693 use_tmp_file = TRUE;
3694 }
3695#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003696
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003697 if (use_tmp_file)
3698 {
3699 char tempname[MAXPATHL + 1];
3700
3701 /*
3702 * Find a name that doesn't exist and is in the same directory.
3703 * Rename "from" to "tempname" and then rename "tempname" to "to".
3704 */
3705 if (STRLEN(from) >= MAXPATHL - 5)
3706 return -1;
3707 STRCPY(tempname, from);
3708 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003709 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003710 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3711 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003712 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003713 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003714 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003715 if (mch_rename(tempname, (char *)to) == 0)
3716 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003717 // Strange, the second step failed. Try moving the
3718 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003719 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003720 return -1;
3721 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003722 // If it fails for one temp name it will most likely fail
3723 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003724 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003725 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003726 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003727 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003728 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 /*
3731 * Delete the "to" file, this is required on some systems to make the
3732 * mch_rename() work, on other systems it makes sure that we don't have
3733 * two files when the mch_rename() fails.
3734 */
3735
3736#ifdef AMIGA
3737 /*
3738 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3739 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003740 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 * deleting the "from" file (horror!) we lock it during the remove.
3742 *
3743 * When used for making a backup before writing the file: This should not
3744 * happen with ":w", because startscript() should detect this problem and
3745 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3746 * name. This problem does exist with ":w filename", but then the
3747 * original file will be somewhere else so the backup isn't really
3748 * important. If autoscripting is off the rename may fail.
3749 */
3750 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3751#endif
3752 mch_remove(to);
3753#ifdef AMIGA
3754 if (flock)
3755 UnLock(flock);
3756#endif
3757
3758 /*
3759 * First try a normal rename, return if it works.
3760 */
3761 if (mch_rename((char *)from, (char *)to) == 0)
3762 return 0;
3763
3764 /*
3765 * Rename() failed, try copying the file.
3766 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003767 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003768#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003769 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003770 acl = mch_get_acl(from);
3771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3773 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003774 {
3775#ifdef HAVE_ACL
3776 mch_free_acl(acl);
3777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003779 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003780
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003781 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003782 fd_out = mch_open((char *)to,
3783 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 if (fd_out == -1)
3785 {
3786 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003787#ifdef HAVE_ACL
3788 mch_free_acl(acl);
3789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return -1;
3791 }
3792
Bram Moolenaar473952e2019-09-28 16:30:04 +02003793 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (buffer == NULL)
3795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003797 close(fd_in);
3798#ifdef HAVE_ACL
3799 mch_free_acl(acl);
3800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return -1;
3802 }
3803
Bram Moolenaar473952e2019-09-28 16:30:04 +02003804 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003805 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
3807 errmsg = _("E208: Error writing to \"%s\"");
3808 break;
3809 }
3810
3811 vim_free(buffer);
3812 close(fd_in);
3813 if (close(fd_out) < 0)
3814 errmsg = _("E209: Error closing \"%s\"");
3815 if (n < 0)
3816 {
3817 errmsg = _("E210: Error reading \"%s\"");
3818 to = from;
3819 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003820#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003821 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003822#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003823#ifdef HAVE_ACL
3824 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003825 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003826#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003827#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003828 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (errmsg != NULL)
3831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003832 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 return -1;
3834 }
3835 mch_remove(from);
3836 return 0;
3837}
3838
3839static int already_warned = FALSE;
3840
3841/*
3842 * Check if any not hidden buffer has been changed.
3843 * Postpone the check if there are characters in the stuff buffer, a global
3844 * command is being executed, a mapping is being executed or an autocommand is
3845 * busy.
3846 * Returns TRUE if some message was written (screen should be redrawn and
3847 * cursor positioned).
3848 */
3849 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003850check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003851 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853 buf_T *buf;
3854 int didit = 0;
3855 int n;
3856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 // Don't check timestamps while system() or another low-level function may
3858 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (no_check_timestamps > 0)
3860 return FALSE;
3861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003862 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3863 // event and we would keep on checking if the file is steadily growing.
3864 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (focus && did_check_timestamps)
3866 {
3867 need_check_timestamps = TRUE;
3868 return FALSE;
3869 }
3870
3871 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003872 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003873 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 else
3875 {
3876 ++no_wait_return;
3877 did_check_timestamps = TRUE;
3878 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003879 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003881 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (buf->b_nwindows > 0)
3883 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003884 bufref_T bufref;
3885
3886 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 n = buf_check_timestamp(buf, focus);
3888 if (didit < n)
3889 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003890 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003892 // Autocommands have removed the buffer, start at the
3893 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 buf = firstbuf;
3895 continue;
3896 }
3897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 --no_wait_return;
3900 need_check_timestamps = FALSE;
3901 if (need_wait_return && didit == 2)
3902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003903 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003904 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 out_flush();
3906 }
3907 }
3908 return didit;
3909}
3910
3911/*
3912 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3913 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3914 * empty.
3915 */
3916 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003917move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 buf_T *tbuf = curbuf;
3920 int retval = OK;
3921 linenr_T lnum;
3922 char_u *p;
3923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003924 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 curbuf = tobuf;
3926 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3927 {
3928 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3929 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3930 {
3931 vim_free(p);
3932 retval = FAIL;
3933 break;
3934 }
3935 vim_free(p);
3936 }
3937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003938 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (retval != FAIL)
3940 {
3941 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003942 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003943 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003945 // Oops! We could try putting back the saved lines, but that
3946 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 retval = FAIL;
3948 break;
3949 }
3950 }
3951
3952 curbuf = tbuf;
3953 return retval;
3954}
3955
3956/*
3957 * Check if buffer "buf" has been changed.
3958 * Also check if the file for a new buffer unexpectedly appeared.
3959 * return 1 if a changed buffer was found.
3960 * return 2 if a message has been displayed.
3961 * return 0 otherwise.
3962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003964buf_check_timestamp(
3965 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003966 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003968 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int stat_res;
3970 int retval = 0;
3971 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003974 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int helpmesg = FALSE;
3976 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003977 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3979 int can_reload = FALSE;
3980#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003981 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 int orig_mode = buf->b_orig_mode;
3983#ifdef FEAT_GUI
3984 int save_mouse_correct = need_mouse_correct;
3985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003987 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003988#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003989 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003990#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003991 bufref_T bufref;
3992
3993 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003995 // If there is no file name, the buffer is not loaded, 'buftype' is
3996 // set, we are in the middle of a save or being called recursively: ignore
3997 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (buf->b_ffname == NULL
3999 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004000 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004003#ifdef FEAT_NETBEANS_INTG
4004 || isNetbeansBuffer(buf)
4005#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004006#ifdef FEAT_TERMINAL
4007 || buf->b_term != NULL
4008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 )
4010 return 0;
4011
4012 if ( !(buf->b_flags & BF_NOTEDITED)
4013 && buf->b_mtime != 0
4014 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4015 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004016 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifdef HAVE_ST_MODE
4018 || (int)st.st_mode != buf->b_orig_mode
4019#else
4020 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4021#endif
4022 ))
4023 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004024 long prev_b_mtime = buf->b_mtime;
4025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 retval = 1;
4027
Bram Moolenaar386bc822018-07-07 18:34:12 +02004028 // set b_mtime to stop further warnings (e.g., when executing
4029 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (stat_res < 0)
4031 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004032 // Check the file again later to see if it re-appears.
4033 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 buf->b_orig_size = 0;
4035 buf->b_orig_mode = 0;
4036 }
4037 else
4038 buf_store_time(buf, &st, buf->b_ffname);
4039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004040 // Don't do anything for a directory. Might contain the file
4041 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (mch_isdir(buf->b_fname))
4043 ;
4044
4045 /*
4046 * If 'autoread' is set, the buffer has no changes and the file still
4047 * exists, reload the buffer. Use the buffer-local option value if it
4048 * was set, the global option value otherwise.
4049 */
4050 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4051 && !bufIsChanged(buf) && stat_res >= 0)
4052 reload = TRUE;
4053 else
4054 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004055 if (stat_res < 0)
4056 reason = "deleted";
4057 else if (bufIsChanged(buf))
4058 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004059 /*
4060 * Check if the file contents really changed to avoid giving a
4061 * warning when only the timestamp was set (e.g., checked out of
4062 * CVS). Always warn when the buffer was changed.
4063 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004064 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4065 reason = "changed";
4066 else if (orig_mode != buf->b_orig_mode)
4067 reason = "mode";
4068 else
4069 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 /*
4072 * Only give the warning if there are no FileChangedShell
4073 * autocommands.
4074 * Avoid being called recursively by setting "busy".
4075 */
4076 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004077#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004078 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4079 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004080#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004081 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4083 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004084 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 busy = FALSE;
4086 if (n)
4087 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004088 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004089 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004090#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004091 s = get_vim_var_str(VV_FCS_CHOICE);
4092 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4093 reload = TRUE;
4094 else if (STRCMP(s, "ask") == 0)
4095 n = FALSE;
4096 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004097#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004098 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004100 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004102 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004103 {
4104 // Only give the message once.
4105 if (prev_b_mtime != -1)
4106 mesg = _("E211: File \"%s\" no longer available");
4107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 else
4109 {
4110 helpmesg = TRUE;
4111#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4112 can_reload = TRUE;
4113#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004114 if (reason[2] == 'n')
4115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004117 mesg2 = _("See \":help W12\" for more info.");
4118 }
4119 else if (reason[1] == 'h')
4120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004122 mesg2 = _("See \":help W11\" for more info.");
4123 }
4124 else if (*reason == 'm')
4125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004127 mesg2 = _("See \":help W16\" for more info.");
4128 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004129 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004130 // Only timestamp changed, store it to avoid a warning
4131 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004132 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134 }
4135 }
4136
4137 }
4138 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4139 && vim_fexists(buf->b_ffname))
4140 {
4141 retval = 1;
4142 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4143 buf->b_flags |= BF_NEW_W;
4144#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4145 can_reload = TRUE;
4146#endif
4147 }
4148
4149 if (mesg != NULL)
4150 {
4151 path = home_replace_save(buf, buf->b_fname);
4152 if (path != NULL)
4153 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004154 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004156 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004157 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004158#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004159 // Set warningmsg here, before the unimportant and output-specific
4160 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004161 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4164 if (can_reload)
4165 {
4166 if (*mesg2 != NUL)
4167 {
4168 STRCAT(tbuf, "\n");
4169 STRCAT(tbuf, mesg2);
4170 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004171 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4172 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004173 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 reload = TRUE;
4175 }
4176 else
4177#endif
4178 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4179 {
4180 if (*mesg2 != NUL)
4181 {
4182 STRCAT(tbuf, "; ");
4183 STRCAT(tbuf, mesg2);
4184 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 retval = 2;
4187 }
4188 else
4189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004193 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004195 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 msg_clr_eos();
4197 (void)msg_end();
4198 if (emsg_silent == 0)
4199 {
4200 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004201#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004203#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004204 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004205 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004207 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 redraw_cmdline = FALSE;
4209 }
4210 }
4211 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213
4214 vim_free(path);
4215 vim_free(tbuf);
4216 }
4217 }
4218
4219 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004221 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004222 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004223#ifdef FEAT_PERSISTENT_UNDO
4224 if (buf->b_p_udf && buf->b_ffname != NULL)
4225 {
4226 char_u hash[UNDO_HASH_SIZE];
4227 buf_T *save_curbuf = curbuf;
4228
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004229 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004230 curbuf = buf;
4231 u_compute_hash(hash);
4232 u_write_undo(NULL, FALSE, buf, hash);
4233 curbuf = save_curbuf;
4234 }
4235#endif
4236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004238 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004239 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004240 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4241 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 // restore this in case an autocommand has set it; it would break
4244 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 need_mouse_correct = save_mouse_correct;
4246#endif
4247
4248 return retval;
4249}
4250
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004251/*
4252 * Reload a buffer that is already loaded.
4253 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004254 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4255 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004256 */
4257 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004258buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004259{
4260 exarg_T ea;
4261 pos_T old_cursor;
4262 linenr_T old_topline;
4263 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004264 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004265 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004266 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004267 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004268 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004270 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004271 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004273 // We only want to read the text from the file, not reset the syntax
4274 // highlighting, clear marks, diff status, etc. Force the fileformat
4275 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004276 if (prep_exarg(&ea, buf) == OK)
4277 {
4278 old_cursor = curwin->w_cursor;
4279 old_topline = curwin->w_topline;
4280
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004281 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004283 // Save all the text, so that the reload can be undone.
4284 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004285 u_sync(FALSE);
4286 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4287 flags |= READ_KEEP_UNDO;
4288 }
4289
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004290 /*
4291 * To behave like when a new file is edited (matters for
4292 * BufReadPost autocommands) we first need to delete the current
4293 * buffer contents. But if reading the file fails we should keep
4294 * the old contents. Can't use memory only, the file might be
4295 * too big. Use a hidden buffer to move the buffer contents to.
4296 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004297 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004298 savebuf = NULL;
4299 else
4300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004301 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004302 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004303 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004304 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004307 curbuf = savebuf;
4308 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004309 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004310 curbuf = buf;
4311 curwin->w_buffer = buf;
4312 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004313 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004314 || move_lines(buf, savebuf) == FAIL)
4315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004316 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004317 buf->b_fname);
4318 saved = FAIL;
4319 }
4320 }
4321
4322 if (saved == OK)
4323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4325 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004326 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4327 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004328 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004329 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004330#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004331 if (!aborting())
4332#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004333 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004334 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004336 // Put the text back from the save buffer. First
4337 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004338 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004339 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004340 break;
4341 (void)move_lines(savebuf, buf);
4342 }
4343 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004347 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004348 if ((flags & READ_KEEP_UNDO) == 0)
4349 {
4350 u_blockfree(buf);
4351 u_clearall(buf);
4352 }
4353 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004354 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004355 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004356 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004357 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004358 }
4359 }
4360 vim_free(ea.cmd);
4361
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004362 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004363 wipe_buffer(savebuf, FALSE);
4364
4365#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004366 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004367 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004368#endif
4369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004370 // Restore the topline and cursor position and check it (lines may
4371 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004372 if (old_topline > curbuf->b_ml.ml_line_count)
4373 curwin->w_topline = curbuf->b_ml.ml_line_count;
4374 else
4375 curwin->w_topline = old_topline;
4376 curwin->w_cursor = old_cursor;
4377 check_cursor();
4378 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004379 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004380#ifdef FEAT_FOLDING
4381 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004382 win_T *wp;
4383 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004385 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004386 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004387 if (wp->w_buffer == curwin->w_buffer
4388 && !foldmethodIsManual(wp))
4389 foldUpdateAll(wp);
4390 }
4391#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004392 // If the mode didn't change and 'readonly' was set, keep the old
4393 // value; the user probably used the ":view" command. But don't
4394 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004395 if (orig_mode == curbuf->b_orig_mode)
4396 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004399 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 }
4401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004403 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405}
4406
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004408buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004411 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412#ifdef HAVE_ST_MODE
4413 buf->b_orig_mode = (int)st->st_mode;
4414#else
4415 buf->b_orig_mode = mch_getperm(fname);
4416#endif
4417}
4418
4419/*
4420 * Adjust the line with missing eol, used for the next write.
4421 * Used for do_filter(), when the input lines for the filter are deleted.
4422 */
4423 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004424write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004426 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004427 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428}
4429
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004430// Subfuncions for readdirex()
4431#ifdef FEAT_EVAL
4432# ifdef MSWIN
4433 static char_u *
4434getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4435{
4436 stat_T st;
4437 unsigned short st_mode;
4438 DWORD flag = wfd->dwFileAttributes;
4439 WCHAR *wp;
4440
4441 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4442 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4443 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4444 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4445
4446 wp = wcsrchr(wfd->cFileName, L'.');
4447 if (wp != NULL)
4448 {
4449 if (_wcsicmp(wp, L".exe") == 0 ||
4450 _wcsicmp(wp, L".com") == 0 ||
4451 _wcsicmp(wp, L".cmd") == 0 ||
4452 _wcsicmp(wp, L".bat") == 0)
4453 st_mode |= _S_IEXEC;
4454 }
4455
4456 // Copy user bits to group/other.
4457 st_mode |= (st_mode & 0700) >> 3;
4458 st_mode |= (st_mode & 0700) >> 6;
4459
4460 st.st_mode = st_mode;
4461 return getfpermst(&st, perm);
4462}
4463
4464 static char_u *
4465getftypewfd(WIN32_FIND_DATAW *wfd)
4466{
4467 DWORD flag = wfd->dwFileAttributes;
4468 DWORD tag = wfd->dwReserved0;
4469
4470 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4471 {
4472 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4473 return (char_u*)"junction";
4474 else if (tag == IO_REPARSE_TAG_SYMLINK)
4475 {
4476 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4477 return (char_u*)"linkd";
4478 else
4479 return (char_u*)"link";
4480 }
4481 return (char_u*)"reparse"; // unknown reparse point type
4482 }
4483 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4484 return (char_u*)"dir";
4485 else
4486 return (char_u*)"file";
4487}
4488
4489 static dict_T *
4490create_readdirex_item(WIN32_FIND_DATAW *wfd)
4491{
4492 dict_T *item;
4493 char_u *p;
4494 varnumber_T size, time;
4495 char_u permbuf[] = "---------";
4496
4497 item = dict_alloc();
4498 if (item == NULL)
4499 return NULL;
4500 item->dv_refcount++;
4501
4502 p = utf16_to_enc(wfd->cFileName, NULL);
4503 if (p == NULL)
4504 goto theend;
4505 if (dict_add_string(item, "name", p) == FAIL)
4506 {
4507 vim_free(p);
4508 goto theend;
4509 }
4510 vim_free(p);
4511
4512 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4513 if (dict_add_number(item, "size", size) == FAIL)
4514 goto theend;
4515
4516 // Convert FILETIME to unix time.
4517 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4518 wfd->ftLastWriteTime.dwLowDateTime)
4519 - 116444736000000000) / 10000000;
4520 if (dict_add_number(item, "time", time) == FAIL)
4521 goto theend;
4522
4523 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4524 goto theend;
4525 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4526 goto theend;
4527
4528 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4529 goto theend;
4530 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4531 goto theend;
4532
4533 return item;
4534
4535theend:
4536 dict_unref(item);
4537 return NULL;
4538}
4539# else
4540 static dict_T *
4541create_readdirex_item(char_u *path, char_u *name)
4542{
4543 dict_T *item;
4544 char *p;
4545 size_t len;
4546 stat_T st;
4547 int ret, link = FALSE;
4548 varnumber_T size;
4549 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004550 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004551 struct passwd *pw;
4552 struct group *gr;
4553
4554 item = dict_alloc();
4555 if (item == NULL)
4556 return NULL;
4557 item->dv_refcount++;
4558
4559 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4560 p = alloc(len);
4561 if (p == NULL)
4562 goto theend;
4563 vim_snprintf(p, len, "%s/%s", path, name);
4564 ret = mch_lstat(p, &st);
4565 if (ret >= 0 && S_ISLNK(st.st_mode))
4566 {
4567 link = TRUE;
4568 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004569 if (ret < 0)
4570 q = (char_u*)"link";
4571
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004572 }
4573 vim_free(p);
4574
4575 if (dict_add_string(item, "name", name) == FAIL)
4576 goto theend;
4577
4578 if (ret >= 0)
4579 {
4580 size = (varnumber_T)st.st_size;
4581 if (S_ISDIR(st.st_mode))
4582 size = 0;
4583 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004584 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004585 size = -2;
4586 if (dict_add_number(item, "size", size) == FAIL)
4587 goto theend;
4588 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4589 goto theend;
4590
4591 if (link)
4592 {
4593 if (S_ISDIR(st.st_mode))
4594 q = (char_u*)"linkd";
4595 else
4596 q = (char_u*)"link";
4597 }
4598 else
4599 q = getftypest(&st);
4600 if (dict_add_string(item, "type", q) == FAIL)
4601 goto theend;
4602 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4603 goto theend;
4604
4605 pw = getpwuid(st.st_uid);
4606 if (pw == NULL)
4607 q = (char_u*)"";
4608 else
4609 q = (char_u*)pw->pw_name;
4610 if (dict_add_string(item, "user", q) == FAIL)
4611 goto theend;
4612 gr = getgrgid(st.st_gid);
4613 if (gr == NULL)
4614 q = (char_u*)"";
4615 else
4616 q = (char_u*)gr->gr_name;
4617 if (dict_add_string(item, "group", q) == FAIL)
4618 goto theend;
4619 }
4620 else
4621 {
4622 if (dict_add_number(item, "size", -1) == FAIL)
4623 goto theend;
4624 if (dict_add_number(item, "time", -1) == FAIL)
4625 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004626 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004627 goto theend;
4628 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4629 goto theend;
4630 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4631 goto theend;
4632 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4633 goto theend;
4634 }
4635 return item;
4636
4637theend:
4638 dict_unref(item);
4639 return NULL;
4640}
4641# endif
4642
4643 static int
4644compare_readdirex_item(const void *p1, const void *p2)
4645{
4646 char_u *name1, *name2;
4647
4648 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4649 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004650 if (readdirex_sort == READDIR_SORT_BYTE)
4651 return STRCMP(name1, name2);
4652 else if (readdirex_sort == READDIR_SORT_IC)
4653 return STRICMP(name1, name2);
4654 else
4655 return STRCOLL(name1, name2);
4656}
4657
4658 static int
4659compare_readdir_item(const void *s1, const void *s2)
4660{
4661 if (readdirex_sort == READDIR_SORT_BYTE)
4662 return STRCMP(*(char **)s1, *(char **)s2);
4663 else if (readdirex_sort == READDIR_SORT_IC)
4664 return STRICMP(*(char **)s1, *(char **)s2);
4665 else
4666 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004667}
4668#endif
4669
Bram Moolenaarda440d22016-01-16 21:27:23 +01004670#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4671/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004672 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004673 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004674 * If "withattr" is TRUE, retrieve the names and their attributes.
4675 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004676 * Return OK for success, FAIL for failure.
4677 */
4678 int
4679readdir_core(
4680 garray_T *gap,
4681 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004682 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004683 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004684 int (*checkitem)(void *context, void *item),
4685 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004686{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004687 int failed = FALSE;
4688 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004689# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004690 char_u *buf;
4691 int ok;
4692 HANDLE hFind = INVALID_HANDLE_VALUE;
4693 WIN32_FIND_DATAW wfd;
4694 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004695# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004696 DIR *dirp;
4697 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004698# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004699
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004700 ga_init2(gap, (int)sizeof(void *), 20);
4701
4702# ifdef FEAT_EVAL
4703# define FREE_ITEM(item) do { \
4704 if (withattr) \
4705 dict_unref((dict_T*)item); \
4706 else \
4707 vim_free(item); \
4708 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004709
4710 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004711# else
4712# define FREE_ITEM(item) vim_free(item)
4713# endif
4714
4715# ifdef MSWIN
4716 buf = alloc(MAXPATHL);
4717 if (buf == NULL)
4718 return FAIL;
4719 STRNCPY(buf, path, MAXPATHL-5);
4720 p = buf + STRLEN(buf);
4721 MB_PTR_BACK(buf, p);
4722 if (*p == '\\' || *p == '/')
4723 *p = NUL;
4724 STRCAT(p, "\\*");
4725
4726 wn = enc_to_utf16(buf, NULL);
4727 if (wn != NULL)
4728 hFind = FindFirstFileW(wn, &wfd);
4729 ok = (hFind != INVALID_HANDLE_VALUE);
4730 if (!ok)
4731 {
4732 failed = TRUE;
4733 smsg(_(e_notopen), path);
4734 }
4735 else
4736 {
4737 while (ok)
4738 {
4739 int ignore;
4740 void *item;
4741 WCHAR *wp;
4742
4743 wp = wfd.cFileName;
4744 ignore = wp[0] == L'.' &&
4745 (wp[1] == NUL ||
4746 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004747 if (ignore)
4748 {
4749 ok = FindNextFileW(hFind, &wfd);
4750 continue;
4751 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004752# ifdef FEAT_EVAL
4753 if (withattr)
4754 item = (void*)create_readdirex_item(&wfd);
4755 else
4756# endif
4757 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4758 if (item == NULL)
4759 {
4760 failed = TRUE;
4761 break;
4762 }
4763
4764 if (!ignore && checkitem != NULL)
4765 {
4766 int r = checkitem(context, item);
4767
4768 if (r < 0)
4769 {
4770 FREE_ITEM(item);
4771 break;
4772 }
4773 if (r == 0)
4774 ignore = TRUE;
4775 }
4776
4777 if (!ignore)
4778 {
4779 if (ga_grow(gap, 1) == OK)
4780 ((void**)gap->ga_data)[gap->ga_len++] = item;
4781 else
4782 {
4783 failed = TRUE;
4784 FREE_ITEM(item);
4785 break;
4786 }
4787 }
4788 else
4789 FREE_ITEM(item);
4790
4791 ok = FindNextFileW(hFind, &wfd);
4792 }
4793 FindClose(hFind);
4794 }
4795
4796 vim_free(buf);
4797 vim_free(wn);
4798# else // MSWIN
4799 dirp = opendir((char *)path);
4800 if (dirp == NULL)
4801 {
4802 failed = TRUE;
4803 smsg(_(e_notopen), path);
4804 }
4805 else
4806 {
4807 for (;;)
4808 {
4809 int ignore;
4810 void *item;
4811
4812 dp = readdir(dirp);
4813 if (dp == NULL)
4814 break;
4815 p = (char_u *)dp->d_name;
4816
4817 ignore = p[0] == '.' &&
4818 (p[1] == NUL ||
4819 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004820 if (ignore)
4821 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004822# ifdef FEAT_EVAL
4823 if (withattr)
4824 item = (void*)create_readdirex_item(path, p);
4825 else
4826# endif
4827 item = (void*)vim_strsave(p);
4828 if (item == NULL)
4829 {
4830 failed = TRUE;
4831 break;
4832 }
4833
4834 if (!ignore && checkitem != NULL)
4835 {
4836 int r = checkitem(context, item);
4837
4838 if (r < 0)
4839 {
4840 FREE_ITEM(item);
4841 break;
4842 }
4843 if (r == 0)
4844 ignore = TRUE;
4845 }
4846
4847 if (!ignore)
4848 {
4849 if (ga_grow(gap, 1) == OK)
4850 ((void**)gap->ga_data)[gap->ga_len++] = item;
4851 else
4852 {
4853 failed = TRUE;
4854 FREE_ITEM(item);
4855 break;
4856 }
4857 }
4858 else
4859 FREE_ITEM(item);
4860 }
4861
4862 closedir(dirp);
4863 }
4864# endif // MSWIN
4865
4866# undef FREE_ITEM
4867
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004868 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004869 {
4870# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004871 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004872 if (withattr)
4873 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4874 compare_readdirex_item);
4875 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004876 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4877 compare_readdir_item);
4878# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004879 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004880# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004881 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004882
4883 return failed ? FAIL : OK;
4884}
4885
4886/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004887 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004888 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004889 */
4890 int
4891delete_recursive(char_u *name)
4892{
4893 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004894 int i;
4895 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004896 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004897
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004898 // A symbolic link to a directory itself is deleted, not the directory it
4899 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004900 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004901# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004902 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004903# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004904 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004905# endif
4906 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004907 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004908 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004909 if (exp == NULL)
4910 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004911 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004912 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004913 for (i = 0; i < ga.ga_len; ++i)
4914 {
4915 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4916 ((char_u **)ga.ga_data)[i]);
4917 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004918 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004919 }
4920 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004921 }
4922 else
4923 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004924 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004925 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004926 }
4927 else
4928 result = mch_remove(name) == 0 ? 0 : -1;
4929
4930 return result;
4931}
4932#endif
4933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004935static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004937# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4938/*
4939 * Open temporary directory and take file lock to prevent
4940 * to be auto-cleaned.
4941 */
4942 static void
4943vim_opentempdir(void)
4944{
4945 DIR *dp = NULL;
4946
4947 if (vim_tempdir_dp != NULL)
4948 return;
4949
4950 dp = opendir((const char*)vim_tempdir);
4951
4952 if (dp != NULL)
4953 {
4954 vim_tempdir_dp = dp;
4955 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4956 }
4957}
4958
4959/*
4960 * Close temporary directory - it automatically release file lock.
4961 */
4962 static void
4963vim_closetempdir(void)
4964{
4965 if (vim_tempdir_dp != NULL)
4966 {
4967 closedir(vim_tempdir_dp);
4968 vim_tempdir_dp = NULL;
4969 }
4970}
4971# endif
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973/*
4974 * Delete the temp directory and all files it contains.
4975 */
4976 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004977vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (vim_tempdir != NULL)
4980 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004981# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4982 vim_closetempdir();
4983# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004984 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004985 gettail(vim_tempdir)[-1] = NUL;
4986 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004987 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
4991/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004992 * Directory "tempdir" was created. Expand this name to a full path and put
4993 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
4994 * "tempdir" must be no longer than MAXPATHL.
4995 */
4996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004998{
4999 char_u *buf;
5000
Bram Moolenaar964b3742019-05-24 18:54:09 +02005001 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005002 if (buf != NULL)
5003 {
5004 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5005 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005006 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005007 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005008# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5009 vim_opentempdir();
5010# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005011 vim_free(buf);
5012 }
5013}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005014#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005015
5016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 * vim_tempname(): Return a unique name that can be used for a temp file.
5018 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005019 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5020 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 *
5022 * The returned pointer is to allocated memory.
5023 * The returned pointer is NULL if no valid name was found.
5024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005026vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005027 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005031 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005032#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005033 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#else
5035 char_u itmp[TEMPNAMELEN];
5036#endif
5037
5038#ifdef TEMPDIRNAMES
5039 static char *(tempdirs[]) = {TEMPDIRNAMES};
5040 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005042 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043# endif
5044
5045 /*
5046 * This will create a directory for private use by this instance of Vim.
5047 * This is done once, and the same directory is used for all temp files.
5048 * This method avoids security problems because of symlink attacks et al.
5049 * It's also a bit faster, because we only need to check for an existing
5050 * file when creating the directory and not for each temp file.
5051 */
5052 if (vim_tempdir == NULL)
5053 {
5054 /*
5055 * Try the entries in TEMPDIRNAMES to create the temp directory.
5056 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005057 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005059# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005060 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005061 long nr;
5062 long off;
5063# endif
5064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005065 // Expand $TMP, leave room for "/v1100000/999999999".
5066 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005068 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005070 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005071 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
Bram Moolenaareaf03392009-11-17 11:08:52 +00005073# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005074 {
5075# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005076 // Make sure the umask doesn't remove the executable bit.
5077 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005078 mode_t umask_save = umask(077);
5079# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005080 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005081 STRCAT(itmp, "vXXXXXX");
5082 if (mkdtemp((char *)itmp) != NULL)
5083 vim_settempdir(itmp);
5084# if defined(UNIX) || defined(VMS)
5085 (void)umask(umask_save);
5086# endif
5087 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005088# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 // Get an arbitrary number of up to 6 digits. When it's
5090 // unlikely that it already exists it will be faster,
5091 // otherwise it doesn't matter. The use of mkdir() avoids any
5092 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005094 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 // Try up to 10000 different values until we find a name that
5097 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 for (off = 0; off < 10000L; ++off)
5099 {
5100 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005101# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
Bram Moolenaareaf03392009-11-17 11:08:52 +00005105 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5106# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 // If mkdir() does not set errno to EEXIST, check for
5108 // existing file here. There is a race condition then,
5109 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if (mch_stat((char *)itmp, &st) >= 0)
5111 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005112# endif
5113# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005114 // Make sure the umask doesn't remove the executable bit.
5115 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005119# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (r == 0)
5123 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005124 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005127# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005128 // If the mkdir() didn't fail because the file/dir exists,
5129 // we probably can't create any dir here, try another
5130 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 break;
5134 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (vim_tempdir != NULL)
5137 break;
5138 }
5139 }
5140 }
5141
5142 if (vim_tempdir != NULL)
5143 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005144 // There is no need to check if the file exists, because we own the
5145 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5147 return vim_strsave(itmp);
5148 }
5149
5150 return NULL;
5151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005152#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
Bram Moolenaar4f974752019-02-17 17:44:42 +01005154# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005155 WCHAR wszTempFile[_MAX_PATH + 1];
5156 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 char_u *retval;
5158 char_u *p;
5159
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005160 wcscpy(itmp, L"");
5161 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005162 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005163 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
5164 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005165 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005166 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005167 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005168 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005170 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005171 // GetTempFileName() will create the file, we don't want that
5172 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005174 // Backslashes in a temp file name cause problems when filtering with
5175 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5176 // didn't set 'shellslash'.
5177 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (*p_shcf == '-' || p_ssl)
5179 for (p = retval; *p; ++p)
5180 if (*p == '\\')
5181 *p = '/';
5182 return retval;
5183
Bram Moolenaar4f974752019-02-17 17:44:42 +01005184# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005187 char_u *p;
5188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005189 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005190 p = tmpnam((char *)itmp);
5191 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 return NULL;
5193# else
5194 char_u *p;
5195
5196# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005197 // mktemp() is not working on VMS. It seems to be
5198 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 sprintf((char *)itmp, "VIM%c", extra_char);
5200 p = (char_u *)tempnam("tmp:", (char *)itmp);
5201 if (p != NULL)
5202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005203 // VMS will use '.LIS' if we don't explicitly specify an extension,
5204 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 STRCPY(itmp, p);
5206 STRCAT(itmp, ".txt");
5207 free(p);
5208 }
5209 else
5210 return NULL;
5211# else
5212 STRCPY(itmp, TEMPNAME);
5213 if ((p = vim_strchr(itmp, '?')) != NULL)
5214 *p = extra_char;
5215 if (mktemp((char *)itmp) == NULL)
5216 return NULL;
5217# endif
5218# endif
5219
5220 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005221# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005222#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223}
5224
5225#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5226/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005227 * Convert all backslashes in fname to forward slashes in-place, unless when
5228 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 */
5230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005231forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
5233 char_u *p;
5234
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005235 if (path_with_url(fname))
5236 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005239 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005241 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 *p = '/';
5243}
5244#endif
5245
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005246/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005247 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5248 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5249 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 * Used for autocommands and 'wildignore'.
5251 * Returns TRUE if there is a match, FALSE otherwise.
5252 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005253 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005254match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005255 char_u *pattern, // pattern to match with
5256 regprog_T **prog, // pre-compiled regprog or NULL
5257 char_u *fname, // full path of file name
5258 char_u *sfname, // short file name or NULL
5259 char_u *tail, // tail of path
5260 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 regmatch_T regmatch;
5263 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005265 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005266 if (prog != NULL)
5267 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005269 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /*
5272 * Try for a match with the pattern with:
5273 * 1. the full file name, when the pattern has a '/'.
5274 * 2. the short file name, when the pattern has a '/'.
5275 * 3. the tail of the file name, when the pattern has no '/'.
5276 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005277 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 && ((allow_dirs
5279 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5280 || (sfname != NULL
5281 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005282 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 result = TRUE;
5284
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005285 if (prog != NULL)
5286 *prog = regmatch.regprog;
5287 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005288 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return result;
5290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292#if defined(FEAT_WILDIGN) || defined(PROTO)
5293/*
5294 * Return TRUE if a file matches with a pattern in "list".
5295 * "list" is a comma-separated list of patterns, like 'wildignore'.
5296 * "sfname" is the short file name or NULL, "ffname" the long file name.
5297 */
5298 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005299match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
5301 char_u buf[100];
5302 char_u *tail;
5303 char_u *regpat;
5304 char allow_dirs;
5305 int match;
5306 char_u *p;
5307
5308 tail = gettail(sfname);
5309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 p = list;
5312 while (*p)
5313 {
5314 copy_option_part(&p, buf, 100, ",");
5315 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5316 if (regpat == NULL)
5317 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005318 match = match_file_pat(regpat, NULL, ffname, sfname,
5319 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 vim_free(regpat);
5321 if (match)
5322 return TRUE;
5323 }
5324 return FALSE;
5325}
5326#endif
5327
5328/*
5329 * Convert the given pattern "pat" which has shell style wildcards in it, into
5330 * a regular expression, and return the result in allocated memory. If there
5331 * is a directory path separator to be matched, then TRUE is put in
5332 * allow_dirs, otherwise FALSE is put there -- webb.
5333 * Handle backslashes before special characters, like "\*" and "\ ".
5334 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 * Returns NULL when out of memory.
5336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005338file_pat_to_reg_pat(
5339 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005340 char_u *pat_end, // first char after pattern or NULL
5341 char *allow_dirs, // Result passed back out in here
5342 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005344 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 char_u *endp;
5346 char_u *reg_pat;
5347 char_u *p;
5348 int i;
5349 int nested = 0;
5350 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 if (allow_dirs != NULL)
5353 *allow_dirs = FALSE;
5354 if (pat_end == NULL)
5355 pat_end = pat + STRLEN(pat);
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 for (p = pat; p < pat_end; p++)
5358 {
5359 switch (*p)
5360 {
5361 case '*':
5362 case '.':
5363 case ',':
5364 case '{':
5365 case '}':
5366 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005367 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 break;
5369#ifdef BACKSLASH_IN_FILENAME
5370 case '\\':
5371 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 break;
5374#endif
5375 default:
5376 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005377 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 ++p;
5380 ++size;
5381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 break;
5383 }
5384 }
5385 reg_pat = alloc(size + 1);
5386 if (reg_pat == NULL)
5387 return NULL;
5388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 if (pat[0] == '*')
5392 while (pat[0] == '*' && pat < pat_end - 1)
5393 pat++;
5394 else
5395 reg_pat[i++] = '^';
5396 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005397 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
5399 while (endp - pat > 0 && *endp == '*')
5400 endp--;
5401 add_dollar = FALSE;
5402 }
5403 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5404 {
5405 switch (*p)
5406 {
5407 case '*':
5408 reg_pat[i++] = '.';
5409 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005411 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 break;
5413 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 case '~':
5415 reg_pat[i++] = '\\';
5416 reg_pat[i++] = *p;
5417 break;
5418 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 reg_pat[i++] = '.';
5420 break;
5421 case '\\':
5422 if (p[1] == NUL)
5423 break;
5424#ifdef BACKSLASH_IN_FILENAME
5425 if (!no_bslash)
5426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005427 // translate:
5428 // "\x" to "\\x" e.g., "dir\file"
5429 // "\*" to "\\.*" e.g., "dir\*.c"
5430 // "\?" to "\\." e.g., "dir\??.c"
5431 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5433 && p[1] != '+')
5434 {
5435 reg_pat[i++] = '[';
5436 reg_pat[i++] = '\\';
5437 reg_pat[i++] = '/';
5438 reg_pat[i++] = ']';
5439 if (allow_dirs != NULL)
5440 *allow_dirs = TRUE;
5441 break;
5442 }
5443 }
5444#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005445 // Undo escaping from ExpandEscape():
5446 // foo\?bar -> foo?bar
5447 // foo\%bar -> foo%bar
5448 // foo\,bar -> foo,bar
5449 // foo\ bar -> foo bar
5450 // Don't unescape \, * and others that are also special in a
5451 // regexp.
5452 // An escaped { must be unescaped since we use magic not
5453 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 if (*++p == '?'
5455#ifdef BACKSLASH_IN_FILENAME
5456 && no_bslash
5457#endif
5458 )
5459 reg_pat[i++] = '?';
5460 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005461 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005462 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005463 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005464 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5465 {
5466 reg_pat[i++] = '\\';
5467 reg_pat[i++] = '{';
5468 p += 2;
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 else
5471 {
5472 if (allow_dirs != NULL && vim_ispathsep(*p)
5473#ifdef BACKSLASH_IN_FILENAME
5474 && (!no_bslash || *p != '\\')
5475#endif
5476 )
5477 *allow_dirs = TRUE;
5478 reg_pat[i++] = '\\';
5479 reg_pat[i++] = *p;
5480 }
5481 break;
5482#ifdef BACKSLASH_IN_FILENAME
5483 case '/':
5484 reg_pat[i++] = '[';
5485 reg_pat[i++] = '\\';
5486 reg_pat[i++] = '/';
5487 reg_pat[i++] = ']';
5488 if (allow_dirs != NULL)
5489 *allow_dirs = TRUE;
5490 break;
5491#endif
5492 case '{':
5493 reg_pat[i++] = '\\';
5494 reg_pat[i++] = '(';
5495 nested++;
5496 break;
5497 case '}':
5498 reg_pat[i++] = '\\';
5499 reg_pat[i++] = ')';
5500 --nested;
5501 break;
5502 case ',':
5503 if (nested)
5504 {
5505 reg_pat[i++] = '\\';
5506 reg_pat[i++] = '|';
5507 }
5508 else
5509 reg_pat[i++] = ',';
5510 break;
5511 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005512 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005514 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 *allow_dirs = TRUE;
5516 reg_pat[i++] = *p;
5517 break;
5518 }
5519 }
5520 if (add_dollar)
5521 reg_pat[i++] = '$';
5522 reg_pat[i] = NUL;
5523 if (nested != 0)
5524 {
5525 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005526 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005528 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005529 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531 return reg_pat;
5532}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005533
5534#if defined(EINTR) || defined(PROTO)
5535/*
5536 * Version of read() that retries when interrupted by EINTR (possibly
5537 * by a SIGWINCH).
5538 */
5539 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005541{
5542 long ret;
5543
5544 for (;;)
5545 {
5546 ret = vim_read(fd, buf, bufsize);
5547 if (ret >= 0 || errno != EINTR)
5548 break;
5549 }
5550 return ret;
5551}
5552
5553/*
5554 * Version of write() that retries when interrupted by EINTR (possibly
5555 * by a SIGWINCH).
5556 */
5557 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005559{
5560 long ret = 0;
5561 long wlen;
5562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005563 // Repeat the write() so long it didn't fail, other than being interrupted
5564 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005565 while (ret < (long)bufsize)
5566 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005567 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005568 if (wlen < 0)
5569 {
5570 if (errno != EINTR)
5571 break;
5572 }
5573 else
5574 ret += wlen;
5575 }
5576 return ret;
5577}
5578#endif