blob: 4da3afc5404f83f34bd52ded230a79150023c61d [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 Moolenaarf4888d02009-12-02 12:31:27 +000016#if defined(__TANDEM) || defined(__MINT__)
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 Moolenaar473952e2019-09-28 16:30:04 +020038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010039filemess(
40 buf_T *buf,
41 char_u *name,
42 char_u *s,
43 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020046 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48 if (msg_silent != 0)
49 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010050 msg_add_fname(buf, name); // put file name in IObuff with quotes
51 // If it's extremely long, truncate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 if (STRLEN(IObuff) > IOSIZE - 80)
53 IObuff[IOSIZE - 80] = NUL;
54 STRCAT(IObuff, s);
55 /*
56 * For the first message may have to start a new line.
57 * For further ones overwrite the previous one, reset msg_scroll before
58 * calling filemess().
59 */
60 msg_scroll_save = msg_scroll;
61 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
62 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 check_for_delay(FALSE);
65 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020066 if (prev_msg_col != 0 && msg_col == 0)
67 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 msg_scroll = msg_scroll_save;
69 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010070 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
72 msg_clr_eos();
73 out_flush();
74 msg_scrolled_ign = FALSE;
75}
76
77/*
78 * Read lines from file "fname" into the buffer after line "from".
79 *
80 * 1. We allocate blocks with lalloc, as big as possible.
81 * 2. Each block is filled with characters from the file with a single read().
82 * 3. The lines are inserted in the buffer with ml_append().
83 *
84 * (caller must check that fname != NULL, unless READ_STDIN is used)
85 *
86 * "lines_to_skip" is the number of lines that must be skipped
87 * "lines_to_read" is the number of lines that are appended
88 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
89 *
90 * flags:
91 * READ_NEW starting to edit a new buffer
92 * READ_FILTER reading filter output
93 * READ_STDIN read from stdin instead of a file
94 * READ_BUFFER read from curbuf instead of a file (converting after reading
95 * stdin)
96 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +020097 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020098 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100100 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 */
102 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100103readfile(
104 char_u *fname,
105 char_u *sfname,
106 linenr_T from,
107 linenr_T lines_to_skip,
108 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100109 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100110 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111{
112 int fd = 0;
113 int newfile = (flags & READ_NEW);
114 int check_readonly;
115 int filtering = (flags & READ_FILTER);
116 int read_stdin = (flags & READ_STDIN);
117 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200118 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000119 int set_options = newfile || read_buffer
120 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100121 linenr_T read_buf_lnum = 1; // next line to read from curbuf
122 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 char_u c;
124 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100125 char_u *ptr = NULL; // pointer into read buffer
126 char_u *buffer = NULL; // read buffer
127 char_u *new_buffer = NULL; // init to shut up gcc
128 char_u *line_start = NULL; // init to shut up gcc
129 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 colnr_T len;
131 long size = 0;
132 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200133 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 int skip_read = FALSE;
135#ifdef FEAT_CRYPT
136 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200137 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200139#ifdef FEAT_PERSISTENT_UNDO
140 context_sha256_T sha_ctx;
141 int read_undo_file = FALSE;
142#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100143 int split = 0; // number of split lines
144#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100146 int error = FALSE; // errors encountered
147 int ff_error = EOL_UNKNOWN; // file format with errors
148 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#ifdef UNIX
150 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100151 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#else
153 int perm;
154#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100155 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200157 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 int file_readonly;
159 linenr_T skip_count = 0;
160 linenr_T read_count = 0;
161 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100162 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
163 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100164 int try_mac;
165 int try_dos;
166 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100169 linenr_T conv_error = 0; // line nr with conversion error
170 linenr_T illegal_byte = 0; // line nr with illegal byte
171 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
172 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000173 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100174 // BAD_KEEP, BAD_DROP or character to
175 // replace with
176 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100178 char_u *fenc; // fileencoding to use
179 int fenc_alloced; // fenc_next is in allocated memory
180 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 int advance_fenc = FALSE;
182 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100183#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100184 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100185# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 int did_iconv = FALSE; // TRUE when iconv() failed and trying
187 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100189#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100190 int converted = FALSE; // TRUE if conversion done
191 int notconverted = FALSE; // TRUE if conversion wanted but it
192 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100195 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200196 buf_T *old_curbuf;
197 char_u *old_b_ffname;
198 char_u *old_b_fname;
199 int using_b_ffname;
200 int using_b_fname;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200201
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100202 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200203
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100204 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206 /*
207 * If there is no file name yet, use the one for the read file.
208 * BF_NOTEDITED is set to reflect this.
209 * Don't do this for a read from a filter.
210 * Only do this when 'cpoptions' contains the 'f' flag.
211 */
212 if (curbuf->b_ffname == NULL
213 && !filtering
214 && fname != NULL
215 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
216 && !(flags & READ_DUMMY))
217 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000218 if (set_rw_fname(fname, sfname) == FAIL)
219 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 }
221
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100222 // Remember the initial values of curbuf, curbuf->b_ffname and
223 // curbuf->b_fname to detect whether they are altered as a result of
224 // executing nasty autocommands. Also check if "fname" and "sfname"
225 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200226 old_curbuf = curbuf;
227 old_b_ffname = curbuf->b_ffname;
228 old_b_fname = curbuf->b_fname;
229 using_b_ffname = (fname == curbuf->b_ffname)
230 || (sfname == curbuf->b_ffname);
231 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200232
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100233 // After reading a file the cursor line changes but we don't want to
234 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000235 ex_no_reprint = TRUE;
236
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100237 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000238 need_fileinfo = FALSE;
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 /*
241 * For Unix: Use the short file name whenever possible.
242 * Avoids problems with networks and when directory names are changed.
243 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
244 * another directory, which we don't detect.
245 */
246 if (sfname == NULL)
247 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200248#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 fname = sfname;
250#endif
251
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 /*
253 * The BufReadCmd and FileReadCmd events intercept the reading process by
254 * executing the associated commands instead.
255 */
256 if (!filtering && !read_stdin && !read_buffer)
257 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100258 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100260 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
262 curbuf->b_op_start.col = 0;
263
264 if (newfile)
265 {
266 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
267 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200268 {
269 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200271 if (aborting())
272 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200274 // The BufReadCmd code usually uses ":read" to get the text and
275 // perhaps ":file" to change the buffer name. But we should
276 // consider this to work like ":edit", thus reset the
277 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
278 // same file.
279 if (status == OK)
280 curbuf->b_flags &= ~BF_NOTEDITED;
281 return status;
282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 }
284 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
285 FALSE, NULL, eap))
286#ifdef FEAT_EVAL
287 return aborting() ? FAIL : OK;
288#else
289 return OK;
290#endif
291
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100292 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294
295 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100296 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100298 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300 /*
301 * If the name ends in a path separator, we can't open it. Check here,
302 * because reading the file may actually work, but then creating the swap
303 * file may destroy it! Reported on MS-DOS and Win 95.
304 * If the name is too long we might crash further on, quit here.
305 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000306 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000308 p = fname + STRLEN(fname);
309 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000310 {
311 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
312 msg_end();
313 msg_scroll = msg_save;
314 return FAIL;
315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 }
317
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200318 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200320#ifdef UNIX
321 /*
322 * On Unix it is possible to read a directory, so we have to
323 * check for it before the mch_open().
324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100326 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
327 && !S_ISFIFO(perm) // ... or fifo
328 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000329# ifdef OPEN_CHR_FILES
330 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100331 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 )
334 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100335 int retval = FAIL;
336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100340 retval = NOTDONE;
341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 else
343 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
344 msg_end();
345 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100346 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200348#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100349#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000350 /*
351 * MS-Windows allows opening a device, but we will probably get stuck
352 * trying to read it.
353 */
354 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
355 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000356 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000357 msg_end();
358 msg_scroll = msg_save;
359 return FAIL;
360 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000361#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200362 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000363
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100364 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200365 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 /*
368 * When opening a new file we take the readonly flag from the file.
369 * Default is r/w, can be set to r/o below.
370 * Don't reset it when in readonly mode
371 * Only set/reset b_p_ro when BF_CHECK_RO is set.
372 */
373 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000374 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 curbuf->b_p_ro = FALSE;
376
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200377 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100379 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (mch_stat((char *)fname, &st) >= 0)
381 {
382 buf_store_time(curbuf, &st, fname);
383 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#ifdef UNIX
385 /*
386 * Use the protection bits of the original file for the swap file.
387 * This makes it possible for others to read the name of the
388 * edited file from the swapfile, but only if they can read the
389 * edited file.
390 * Remove the "write" and "execute" bits for group and others
391 * (they must not write the swapfile).
392 * Add the "read" and "write" bits for the user, otherwise we may
393 * not be able to write to the file ourselves.
394 * Setting the bits is done below, after creating the swap file.
395 */
396 swap_mode = (st.st_mode & 0644) | 0600;
397#endif
398#ifdef FEAT_CW_EDITOR
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 // Get the FSSpec on MacOS
400 // TODO: Update it properly when the buffer name changes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
402#endif
403#ifdef VMS
404 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000405 curbuf->b_fab_rat = st.st_fab_rat;
406 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#endif
408 }
409 else
410 {
411 curbuf->b_mtime = 0;
412 curbuf->b_mtime_read = 0;
413 curbuf->b_orig_size = 0;
414 curbuf->b_orig_mode = 0;
415 }
416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417 // Reset the "new file" flag. It will be set again below when the
418 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
420 }
421
422/*
423 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100424 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 */
426 file_readonly = FALSE;
427 if (read_stdin)
428 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100429#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100430 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 setmode(0, O_BINARY);
432#endif
433 }
434 else if (!read_buffer)
435 {
436#ifdef USE_MCH_ACCESS
437 if (
438# ifdef UNIX
439 !(perm & 0222) ||
440# endif
441 mch_access((char *)fname, W_OK))
442 file_readonly = TRUE;
443 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
444#else
445 if (!newfile
446 || readonlymode
447 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
448 {
449 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100450 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
452 }
453#endif
454 }
455
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100456 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {
458#ifndef UNIX
459 int isdir_f;
460#endif
461 msg_scroll = msg_save;
462#ifndef UNIX
463 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100464 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
466 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100467 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 if (isdir_f)
469 {
470 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100471 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 }
473 else
474#endif
475 if (newfile)
476 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200477 if (perm < 0
478#ifdef ENOENT
479 && errno == ENOENT
480#endif
481 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
483 /*
484 * Set the 'new-file' flag, so that when the file has
485 * been created by someone else, a ":w" will complain.
486 */
487 curbuf->b_flags |= BF_NEW;
488
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // Create a swap file now, so that other Vims are warned
490 // that we are editing this file. Don't do this for a
491 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#ifdef FEAT_QUICKFIX
493 if (!bt_dontwrite(curbuf))
494#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000498 if (curbuf != old_curbuf
499 || (using_b_ffname
500 && (old_b_ffname != curbuf->b_ffname))
501 || (using_b_fname
502 && (old_b_fname != curbuf->b_fname)))
503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100504 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000505 return FAIL;
506 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000507 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000508 if (dir_of_file_exists(fname))
509 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
510 else
511 filemess(curbuf, sfname,
512 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100514 // Even though this is a new file, it might have been
515 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 check_marks_read();
517#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100518 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200519 if (eap != NULL)
520 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
522 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 save_file_ff(curbuf);
525
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100526#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100527 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 return FAIL;
529#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 }
532 else
533 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000534 filemess(curbuf, sfname, (char_u *)(
535# ifdef EFBIG
536 (errno == EFBIG) ? _("[File too big]") :
537# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200538# ifdef EOVERFLOW
539 (errno == EOVERFLOW) ? _("[File too big]") :
540# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000541 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100542 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
544 }
545
546 return FAIL;
547 }
548
549 /*
550 * Only set the 'ro' flag for readonly files the first time they are
551 * loaded. Help files always get readonly mode
552 */
553 if ((check_readonly && file_readonly) || curbuf->b_help)
554 curbuf->b_p_ro = TRUE;
555
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000556 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100558 // Don't change 'eol' if reading from buffer as it will already be
559 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000560 if (!read_buffer)
561 {
562 curbuf->b_p_eol = TRUE;
563 curbuf->b_start_eol = TRUE;
564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000566 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 }
568
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100569 // Create a swap file now, so that other Vims are warned that we are
570 // editing this file.
571 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572#ifdef FEAT_QUICKFIX
573 if (!bt_dontwrite(curbuf))
574#endif
575 {
576 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000577 if (!read_stdin && (curbuf != old_curbuf
578 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
579 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100581 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000582 if (!read_buffer)
583 close(fd);
584 return FAIL;
585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100587 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000588 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
589 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100590 {
591 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
592
593 /*
594 * If the group-read bit is set but not the world-read bit, then
595 * the group must be equal to the group of the original file. If
596 * we can't make that happen then reset the group-read bit. This
597 * avoids making the swap file readable to more users when the
598 * primary group of the user is too permissive.
599 */
600 if ((swap_mode & 044) == 040)
601 {
602 stat_T swap_st;
603
604 if (mch_stat((char *)swap_fname, &swap_st) >= 0
605 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200606# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100607 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200608 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200609# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200610 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100611 swap_mode &= 0600;
612 }
613
614 (void)mch_setperm(swap_fname, (long)swap_mode);
615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#endif
617 }
618
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200619 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (swap_exists_action == SEA_QUIT)
621 {
622 if (!read_buffer && !read_stdin)
623 close(fd);
624 return FAIL;
625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
629 /*
630 * Set '[ mark to the line above where the lines go (line 1 if zero).
631 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100632 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
634 curbuf->b_op_start.col = 0;
635
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100636 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
637 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
638 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (!read_buffer)
641 {
642 int m = msg_scroll;
643 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 /*
646 * The file must be closed again, the autocommands may want to change
647 * the file before reading it.
648 */
649 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652 /*
653 * The output from the autocommands should not overwrite anything and
654 * should not be overwritten: Set msg_scroll, restore its value if no
655 * output was done.
656 */
657 msg_scroll = TRUE;
658 if (filtering)
659 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
660 FALSE, curbuf, eap);
661 else if (read_stdin)
662 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
663 FALSE, curbuf, eap);
664 else if (newfile)
665 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
666 FALSE, curbuf, eap);
667 else
668 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
669 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100671 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
672 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
673 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100674 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (msg_scrolled == n)
677 msg_scroll = m;
678
679#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
682 --no_wait_return;
683 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 return FAIL;
686 }
687#endif
688 /*
689 * Don't allow the autocommands to change the current buffer.
690 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000691 *
692 * Don't allow the autocommands to change the buffer name either
693 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 */
695 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000696 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
697 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
699 {
700 --no_wait_return;
701 msg_scroll = msg_save;
702 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100703 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100705 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 return FAIL;
708 }
709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
713
714 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
715 {
716 /*
717 * Show the user that we are busy reading the input. Sometimes this
718 * may take a while. When reading from stdin another program may
719 * still be running, don't move the cursor to the last line, unless
720 * always using the GUI.
721 */
722 if (read_stdin)
723 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100724 if (!is_not_a_term())
725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200727# ifdef VIMDLL
728 if (!gui.in_use)
729# endif
730 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#endif
732#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100733 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100734 if (gui.in_use && !gui.dying && !gui.starting)
735 {
736 p = (char_u *)_("Reading from stdin...");
737 gui_write(p, (int)STRLEN(p));
738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 }
742 else if (!read_buffer)
743 filemess(curbuf, sfname, (char_u *)"", 0);
744 }
745
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100746 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
748 /*
749 * Set linecnt now, before the "retry" caused by a wrong guess for
750 * fileformat, and after the autocommands, which may change them.
751 */
752 linecnt = curbuf->b_ml.ml_line_count;
753
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100754 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000755 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000756 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000757 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000758 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000759 curbuf->b_bad_char = eap->bad_char;
760 }
761 else
762 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000765 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
767 if (eap != NULL && eap->force_enc != 0)
768 {
769 fenc = enc_canonize(eap->cmd + eap->force_enc);
770 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000771 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 }
773 else if (curbuf->b_p_bin)
774 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100775 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 fenc_alloced = FALSE;
777 }
778 else if (curbuf->b_help)
779 {
780 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000781 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100783 // Help files are either utf-8 or latin1. Try utf-8 first, if this
784 // fails it must be latin1.
785 // Always do this when 'encoding' is "utf-8". Otherwise only do
786 // this when needed to avoid [converted] remarks all the time.
787 // It is needed when the first line contains non-ASCII characters.
788 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 fenc = (char_u *)"latin1";
790 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000791 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000793 fc = fname[STRLEN(fname) - 1];
794 if (TOLOWER_ASC(fc) == 'x')
795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100796 // Read the first line (and a bit more). Immediately rewind to
797 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100798 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200799 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000800 for (p = firstline; p < firstline + len; ++p)
801 if (*p >= 0x80)
802 {
803 c = TRUE;
804 break;
805 }
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
808
809 if (c)
810 {
811 fenc_next = fenc;
812 fenc = (char_u *)"utf-8";
813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // When the file is utf-8 but a character doesn't fit in
815 // 'encoding' don't retry. In help text editing utf-8 bytes
816 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000817 if (!enc_utf8)
818 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820 fenc_alloced = FALSE;
821 }
822 else if (*p_fencs == NUL)
823 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 fenc_alloced = FALSE;
826 }
827 else
828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100829 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200830 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 /*
834 * Jump back here to retry reading the file in different ways.
835 * Reasons to retry:
836 * - encoding conversion failed: try another one from "fenc_next"
837 * - BOM detected and fenc was set, need to setup conversion
838 * - "fileformat" check failed: try another
839 *
840 * Variables set for special retry actions:
841 * "file_rewind" Rewind the file to start reading it again.
842 * "advance_fenc" Advance "fenc" using "fenc_next".
843 * "skip_read" Re-use already read bytes (BOM detected).
844 * "did_iconv" iconv() conversion failed, try 'charconvert'.
845 * "keep_fileformat" Don't reset "fileformat".
846 *
847 * Other status indicators:
848 * "tmpname" When != NULL did conversion with 'charconvert'.
849 * Output file has to be deleted afterwards.
850 * "iconv_fd" When != -1 did conversion with iconv().
851 */
852retry:
853
854 if (file_rewind)
855 {
856 if (read_buffer)
857 {
858 read_buf_lnum = 1;
859 read_buf_col = 0;
860 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200861 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100863 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 error = TRUE;
865 goto failed;
866 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200869 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000871 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000874 curbuf->b_start_bomb = FALSE;
875 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000876 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878
879 /*
880 * When retrying with another "fenc" and the first time "fileformat"
881 * will be reset.
882 */
883 if (keep_fileformat)
884 keep_fileformat = FALSE;
885 else
886 {
887 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000890 try_unix = try_dos = try_mac = FALSE;
891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100895 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100897 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899
Bram Moolenaar13505972019-01-24 15:04:48 +0100900#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (iconv_fd != (iconv_t)-1)
902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100903 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 iconv_close(iconv_fd);
905 iconv_fd = (iconv_t)-1;
906 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909 if (advance_fenc)
910 {
911 /*
912 * Try the next entry in 'fileencodings'.
913 */
914 advance_fenc = FALSE;
915
916 if (eap != NULL && eap->force_enc != 0)
917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100918 // Conversion given with "++cc=" wasn't possible, read
919 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000921 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (fenc_alloced)
923 vim_free(fenc);
924 fenc = (char_u *)"";
925 fenc_alloced = FALSE;
926 }
927 else
928 {
929 if (fenc_alloced)
930 vim_free(fenc);
931 if (fenc_next != NULL)
932 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200933 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935 else
936 {
937 fenc = (char_u *)"";
938 fenc_alloced = FALSE;
939 }
940 }
941 if (tmpname != NULL)
942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100944 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946 }
947
948 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000949 * Conversion may be required when the encoding of the file is different
950 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 */
952 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000953 converted = need_conversion(fenc);
954 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100957 // "ucs-bom" means we need to check the first bytes of the file
958 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (STRCMP(fenc, ENC_UCSBOM) == 0)
960 fio_flags = FIO_UCSBOM;
961
962 /*
963 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
964 * done. This is handled below after read(). Prepare the
965 * fio_flags to avoid having to parse the string each time.
966 * Also check for Unicode to Latin1 conversion, because iconv()
967 * appears not to handle this correctly. This works just like
968 * conversion to UTF-8 except how the resulting character is put in
969 * the buffer.
970 */
971 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
972 fio_flags = get_fio_flags(fenc);
973
Bram Moolenaar4f974752019-02-17 17:44:42 +0100974#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 /*
976 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
977 * is handled with MultiByteToWideChar().
978 */
979 if (fio_flags == 0)
980 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar13505972019-01-24 15:04:48 +0100983#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (fio_flags == 0)
986 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
Bram Moolenaar13505972019-01-24 15:04:48 +0100989#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 /*
991 * Try using iconv() if we can't convert internally.
992 */
993 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +0100994# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +0100996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 )
998 iconv_fd = (iconv_t)my_iconv_open(
999 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
Bram Moolenaar13505972019-01-24 15:04:48 +01001002#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * Use the 'charconvert' expression when conversion is required
1005 * and we can't do it internally or with iconv().
1006 */
1007 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001008 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001009# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 )
1013 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001014# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001016# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001017 // Skip conversion when it's already done (retry for wrong
1018 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (tmpname == NULL)
1020 {
1021 tmpname = readfile_charconvert(fname, fenc, &fd);
1022 if (tmpname == NULL)
1023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 advance_fenc = TRUE;
1026 if (fd < 0)
1027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001028 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001029 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 error = TRUE;
1031 goto failed;
1032 }
1033 goto retry;
1034 }
1035 }
1036 }
1037 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001041#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 )
1045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001046 // Conversion wanted but we can't.
1047 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 advance_fenc = TRUE;
1049 goto retry;
1050 }
1051 }
1052 }
1053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // Set "can_retry" when it's possible to rewind the file and try with
1055 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1056 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001057 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 if (!skip_read)
1060 {
1061 linerest = 0;
1062 filesize = 0;
1063 skip_count = lines_to_skip;
1064 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001066#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001067 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1068 && curbuf->b_ffname != NULL
1069 && curbuf->b_p_udf
1070 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001071 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001072 && !read_stdin
1073 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001074 if (read_undo_file)
1075 sha256_start(&sha_ctx);
1076#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001077#ifdef FEAT_CRYPT
1078 if (curbuf->b_cryptstate != NULL)
1079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Need to free the state, but keep the key, don't want to ask for
1081 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001082 crypt_free_state(curbuf->b_cryptstate);
1083 curbuf->b_cryptstate = NULL;
1084 }
1085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087
1088 while (!error && !got_int)
1089 {
1090 /*
1091 * We allocate as much space for the file as we can get, plus
1092 * space for the old line plus room for one terminating NUL.
1093 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001094 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001096 if (!skip_read)
1097 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001098#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001099 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001100#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001101 // Use buffer >= 64K. Add linerest to double the size if the
1102 // line gets very long, to avoid a lot of copying. But don't
1103 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001104 size = 0x10000L + linerest;
1105 if (size > 0x100000L)
1106 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001107#endif
1108 }
1109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001111 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001114 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 size = 1;
1116 }
1117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 if (!skip_read)
1120 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001121 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001123 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 FALSE)) != NULL)
1125 break;
1126 }
1127 if (new_buffer == NULL)
1128 {
1129 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1130 error = TRUE;
1131 break;
1132 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001133 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1135 vim_free(buffer);
1136 buffer = new_buffer;
1137 ptr = buffer + linerest;
1138 line_start = buffer;
1139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // May need room to translate into.
1141 // For iconv() we don't really know the required space, use a
1142 // factor ICONV_MULT.
1143 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1144 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1145 // become up to 4 bytes, size must be multiple of 2
1146 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1147 // multiple of 2
1148 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1149 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001150 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001151#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (iconv_fd != (iconv_t)-1)
1153 size = size / ICONV_MULT;
1154 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (fio_flags & FIO_LATIN1)
1157 size = size / 2;
1158 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1159 size = (size * 2 / 3) & ~1;
1160 else if (fio_flags & FIO_UCS4)
1161 size = (size * 2 / 3) & ~3;
1162 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001164#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001167#endif
1168#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001170 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (conv_restlen > 0)
1174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 mch_memmove(ptr, conv_rest, conv_restlen);
1177 ptr += conv_restlen;
1178 size -= conv_restlen;
1179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
1181 if (read_buffer)
1182 {
1183 /*
1184 * Read bytes from curbuf. Used for converting text read
1185 * from stdin.
1186 */
1187 if (read_buf_lnum > from)
1188 size = 0;
1189 else
1190 {
1191 int n, ni;
1192 long tlen;
1193
1194 tlen = 0;
1195 for (;;)
1196 {
1197 p = ml_get(read_buf_lnum) + read_buf_col;
1198 n = (int)STRLEN(p);
1199 if ((int)tlen + n + 1 > size)
1200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001201 // Filled up to "size", append partial line.
1202 // Change NL to NUL to reverse the effect done
1203 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001204 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 for (ni = 0; ni < n; ++ni)
1206 {
1207 if (p[ni] == NL)
1208 ptr[tlen++] = NUL;
1209 else
1210 ptr[tlen++] = p[ni];
1211 }
1212 read_buf_col += n;
1213 break;
1214 }
1215 else
1216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001217 // Append whole line and new-line. Change NL
1218 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 for (ni = 0; ni < n; ++ni)
1220 {
1221 if (p[ni] == NL)
1222 ptr[tlen++] = NUL;
1223 else
1224 ptr[tlen++] = p[ni];
1225 }
1226 ptr[tlen++] = NL;
1227 read_buf_col = 0;
1228 if (++read_buf_lnum > from)
1229 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001230 // When the last line didn't have an
1231 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (!curbuf->b_p_eol)
1233 --tlen;
1234 size = tlen;
1235 break;
1236 }
1237 }
1238 }
1239 }
1240 }
1241 else
1242 {
1243 /*
1244 * Read bytes from the file.
1245 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001246 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001249#ifdef FEAT_CRYPT
1250 /*
1251 * At start of file: Check for magic number of encryption.
1252 */
1253 if (filesize == 0 && size > 0)
1254 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1255 &filesize, newfile, sfname,
1256 &did_ask_for_key);
1257 /*
1258 * Decrypt the read bytes. This is done before checking for
1259 * EOF because the crypt layer may be buffering.
1260 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001261 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1262 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001263 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001264# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001265 if (crypt_works_inplace(curbuf->b_cryptstate))
1266 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001267# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001268 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001269# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001270 }
1271 else
1272 {
1273 char_u *newptr = NULL;
1274 int decrypted_size;
1275
1276 decrypted_size = crypt_decode_alloc(
1277 curbuf->b_cryptstate, ptr, size, &newptr);
1278
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001279 // If the crypt layer is buffering, not producing
1280 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001281 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001282 continue;
1283
1284 if (linerest == 0)
1285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001286 // Simple case: reuse returned buffer (may be
1287 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001288 new_buffer = newptr;
1289 }
1290 else
1291 {
1292 long_u new_size;
1293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001294 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001295 new_size = (long_u)(decrypted_size + linerest + 1);
1296 new_buffer = lalloc(new_size, FALSE);
1297 if (new_buffer == NULL)
1298 {
1299 do_outofmem_msg(new_size);
1300 error = TRUE;
1301 break;
1302 }
1303
1304 mch_memmove(new_buffer, buffer, linerest);
1305 if (newptr != NULL)
1306 mch_memmove(new_buffer + linerest, newptr,
1307 decrypted_size);
1308 }
1309
1310 if (new_buffer != NULL)
1311 {
1312 vim_free(buffer);
1313 buffer = new_buffer;
1314 new_buffer = NULL;
1315 line_start = buffer;
1316 ptr = buffer + linerest;
1317 }
1318 size = decrypted_size;
1319 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001320# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001321 }
1322#endif
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (size <= 0)
1325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001326 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001329 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001330 /*
1331 * Reached end-of-file but some trailing bytes could
1332 * not be converted. Truncated file?
1333 */
1334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001335 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001336 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001337#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001338 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001339#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001340 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001341 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001342 if (can_retry)
1343 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001344 if (conv_error == 0)
1345 conv_error = curbuf->b_ml.ml_line_count
1346 - linecnt + 1;
1347 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001348 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001349 else if (illegal_byte == 0)
1350 illegal_byte = curbuf->b_ml.ml_line_count
1351 - linecnt + 1;
1352 if (bad_char_behavior == BAD_DROP)
1353 {
1354 *(ptr - conv_restlen) = NUL;
1355 conv_restlen = 0;
1356 }
1357 else
1358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001359 // Replace the trailing bytes with the replacement
1360 // character if we were converting; if we weren't,
1361 // leave the UTF8 checking code to do it, as it
1362 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001363 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001364#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001365 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001366#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001367 ))
1368 {
1369 while (conv_restlen > 0)
1370 {
1371 *(--ptr) = bad_char_behavior;
1372 --conv_restlen;
1373 }
1374 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001375 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001376#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001377 if (iconv_fd != (iconv_t)-1)
1378 {
1379 iconv_close(iconv_fd);
1380 iconv_fd = (iconv_t)-1;
1381 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001382#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001383 }
1384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 skip_read = FALSE;
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 /*
1390 * At start of file (or after crypt magic number): Check for BOM.
1391 * Also check for a BOM for other Unicode encodings, but not after
1392 * converting with 'charconvert' or when a BOM has already been
1393 * found.
1394 */
1395 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001396#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001397 || (cryptkey != NULL
1398 && filesize == crypt_get_header_len(
1399 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 )
1402 && (fio_flags == FIO_UCSBOM
1403 || (!curbuf->b_p_bomb
1404 && tmpname == NULL
1405 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1406 {
1407 char_u *ccname;
1408 int blen;
1409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001410 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (size < 2 || curbuf->b_p_bin)
1412 ccname = NULL;
1413 else
1414 ccname = check_for_bom(ptr, size, &blen,
1415 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1416 if (ccname != NULL)
1417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001418 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 filesize += blen;
1420 size -= blen;
1421 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001422 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001425 curbuf->b_start_bomb = TRUE;
1426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428
1429 if (fio_flags == FIO_UCSBOM)
1430 {
1431 if (ccname == NULL)
1432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 advance_fenc = TRUE;
1435 }
1436 else
1437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (fenc_alloced)
1440 vim_free(fenc);
1441 fenc = ccname;
1442 fenc_alloced = FALSE;
1443 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001444 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 skip_read = TRUE;
1446 goto retry;
1447 }
1448 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001449
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001450 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001451 ptr -= conv_restlen;
1452 size += conv_restlen;
1453 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /*
1455 * Break here for a read error or end-of-file.
1456 */
1457 if (size <= 0)
1458 break;
1459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaar13505972019-01-24 15:04:48 +01001461#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (iconv_fd != (iconv_t)-1)
1463 {
1464 /*
1465 * Attempt conversion of the read bytes to 'encoding' using
1466 * iconv().
1467 */
1468 const char *fromp;
1469 char *top;
1470 size_t from_size;
1471 size_t to_size;
1472
1473 fromp = (char *)ptr;
1474 from_size = size;
1475 ptr += size;
1476 top = (char *)ptr;
1477 to_size = real_size - size;
1478
1479 /*
1480 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001481 * another conversion. Except for when there is no
1482 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001484 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1485 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1487 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001488 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001489 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001490 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001491 if (conv_error == 0)
1492 conv_error = readfile_linenr(linecnt,
1493 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001496 ++fromp;
1497 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001498 if (bad_char_behavior == BAD_KEEP)
1499 {
1500 *top++ = *(fromp - 1);
1501 --to_size;
1502 }
1503 else if (bad_char_behavior != BAD_DROP)
1504 {
1505 *top++ = bad_char_behavior;
1506 --to_size;
1507 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
1510 if (from_size > 0)
1511 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 // Some remaining characters, keep them for the next
1513 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1515 conv_restlen = (int)from_size;
1516 }
1517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 line_start = ptr - linerest;
1520 mch_memmove(line_start, buffer, (size_t)linerest);
1521 size = (long)((char_u *)top - ptr);
1522 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar4f974752019-02-17 17:44:42 +01001525#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (fio_flags & FIO_CODEPAGE)
1527 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001528 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001529 WCHAR ucs2buf[3];
1530 int ucs2len;
1531 int codepage = FIO_GET_CP(fio_flags);
1532 int bytelen;
1533 int found_bad;
1534 char replstr[2];
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /*
1537 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001538 * a codepage, using standard MS-Windows functions. This
1539 * requires two steps:
1540 * 1. convert from 'fileencoding' to ucs-2
1541 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001543 * Because there may be illegal bytes AND an incomplete byte
1544 * sequence at the end, we may have to do the conversion one
1545 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001549 if (bad_char_behavior > 0)
1550 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001552 replstr[0] = '?';
1553 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001556 * Move the bytes to the end of the buffer, so that we have
1557 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001559 src = ptr + real_size - size;
1560 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001562 /*
1563 * Do the conversion.
1564 */
1565 dst = ptr;
1566 size = size;
1567 while (size > 0)
1568 {
1569 found_bad = FALSE;
1570
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001571# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001572 if (codepage == CP_UTF8)
1573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001574 // Handle CP_UTF8 input ourselves to be able to handle
1575 // trailing bytes properly.
1576 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001577 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001578 if (bytelen > size)
1579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001580 // Only got some bytes of a character. Normally
1581 // it's put in "conv_rest", but if it's too long
1582 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001583 if (bytelen <= CONV_RESTLEN)
1584 break;
1585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001586 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001587 bytelen = size;
1588 found_bad = TRUE;
1589 }
1590 else
1591 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001592 int u8c = utf_ptr2char(src);
1593
Bram Moolenaar86e01082005-12-29 22:45:34 +00001594 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001595 found_bad = TRUE;
1596 ucs2buf[0] = u8c;
1597 ucs2len = 1;
1598 }
1599 }
1600 else
1601# endif
1602 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001603 // We don't know how long the byte sequence is, try
1604 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001605 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1606 ++bytelen)
1607 {
1608 ucs2len = MultiByteToWideChar(codepage,
1609 MB_ERR_INVALID_CHARS,
1610 (LPCSTR)src, bytelen,
1611 ucs2buf, 3);
1612 if (ucs2len > 0)
1613 break;
1614 }
1615 if (ucs2len == 0)
1616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001617 // If we have only one byte then it's probably an
1618 // incomplete byte sequence. Otherwise discard
1619 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001620 if (size == 1)
1621 break;
1622 found_bad = TRUE;
1623 bytelen = 1;
1624 }
1625 }
1626
1627 if (!found_bad)
1628 {
1629 int i;
1630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001632 if (enc_utf8)
1633 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001634 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001635 for (i = 0; i < ucs2len; ++i)
1636 dst += utf_char2bytes(ucs2buf[i], dst);
1637 }
1638 else
1639 {
1640 BOOL bad = FALSE;
1641 int dstlen;
1642
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001643 // From UCS-2 to "enc_codepage". If the
1644 // conversion uses the default character "?",
1645 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001646 dstlen = WideCharToMultiByte(enc_codepage, 0,
1647 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001648 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001649 replstr, &bad);
1650 if (bad)
1651 found_bad = TRUE;
1652 else
1653 dst += dstlen;
1654 }
1655 }
1656
1657 if (found_bad)
1658 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001659 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001660 if (can_retry)
1661 goto rewind_retry;
1662 if (conv_error == 0)
1663 conv_error = readfile_linenr(linecnt, ptr, dst);
1664 if (bad_char_behavior != BAD_DROP)
1665 {
1666 if (bad_char_behavior == BAD_KEEP)
1667 {
1668 mch_memmove(dst, src, bytelen);
1669 dst += bytelen;
1670 }
1671 else
1672 *dst++ = bad_char_behavior;
1673 }
1674 }
1675
1676 src += bytelen;
1677 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001679
1680 if (size > 0)
1681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001683 mch_memmove(conv_rest, src, size);
1684 conv_restlen = size;
1685 }
1686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001688 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001691#endif
1692#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (fio_flags & FIO_MACROMAN)
1694 {
1695 /*
1696 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001697 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001699 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (fio_flags != 0)
1705 {
1706 int u8c;
1707 char_u *dest;
1708 char_u *tail = NULL;
1709
1710 /*
1711 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1712 * "enc_utf8" not set: Convert Unicode to Latin1.
1713 * Go from end to start through the buffer, because the number
1714 * of bytes may increase.
1715 * "dest" points to after where the UTF-8 bytes go, "p" points
1716 * to after the next character to convert.
1717 */
1718 dest = ptr + real_size;
1719 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1720 {
1721 p = ptr + size;
1722 if (fio_flags == FIO_UTF8)
1723 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001724 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 tail = ptr + size - 1;
1726 while (tail > ptr && (*tail & 0xc0) == 0x80)
1727 --tail;
1728 if (tail + utf_byte2len(*tail) <= ptr + size)
1729 tail = NULL;
1730 else
1731 p = tail;
1732 }
1733 }
1734 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001736 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 p = ptr + (size & ~1);
1738 if (size & 1)
1739 tail = p;
1740 if ((fio_flags & FIO_UTF16) && p > ptr)
1741 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001742 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (fio_flags & FIO_ENDIAN_L)
1744 {
1745 u8c = (*--p << 8);
1746 u8c += *--p;
1747 }
1748 else
1749 {
1750 u8c = *--p;
1751 u8c += (*--p << 8);
1752 }
1753 if (u8c >= 0xd800 && u8c <= 0xdbff)
1754 tail = p;
1755 else
1756 p += 2;
1757 }
1758 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 p = ptr + (size & ~3);
1763 if (size & 3)
1764 tail = p;
1765 }
1766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001767 // If there is a trailing incomplete sequence move it to
1768 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (tail != NULL)
1770 {
1771 conv_restlen = (int)((ptr + size) - tail);
1772 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1773 size -= conv_restlen;
1774 }
1775
1776
1777 while (p > ptr)
1778 {
1779 if (fio_flags & FIO_LATIN1)
1780 u8c = *--p;
1781 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1782 {
1783 if (fio_flags & FIO_ENDIAN_L)
1784 {
1785 u8c = (*--p << 8);
1786 u8c += *--p;
1787 }
1788 else
1789 {
1790 u8c = *--p;
1791 u8c += (*--p << 8);
1792 }
1793 if ((fio_flags & FIO_UTF16)
1794 && u8c >= 0xdc00 && u8c <= 0xdfff)
1795 {
1796 int u16c;
1797
1798 if (p == ptr)
1799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001800 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (can_retry)
1802 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001803 if (conv_error == 0)
1804 conv_error = readfile_linenr(linecnt,
1805 ptr, p);
1806 if (bad_char_behavior == BAD_DROP)
1807 continue;
1808 if (bad_char_behavior != BAD_KEEP)
1809 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 }
1811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001812 // found second word of double-word, get the first
1813 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (fio_flags & FIO_ENDIAN_L)
1815 {
1816 u16c = (*--p << 8);
1817 u16c += *--p;
1818 }
1819 else
1820 {
1821 u16c = *--p;
1822 u16c += (*--p << 8);
1823 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001824 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1825 + (u8c & 0x3ff);
1826
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001827 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (u16c < 0xd800 || u16c > 0xdbff)
1829 {
1830 if (can_retry)
1831 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001832 if (conv_error == 0)
1833 conv_error = readfile_linenr(linecnt,
1834 ptr, p);
1835 if (bad_char_behavior == BAD_DROP)
1836 continue;
1837 if (bad_char_behavior != BAD_KEEP)
1838 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
1841 }
1842 else if (fio_flags & FIO_UCS4)
1843 {
1844 if (fio_flags & FIO_ENDIAN_L)
1845 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001846 u8c = (unsigned)*--p << 24;
1847 u8c += (unsigned)*--p << 16;
1848 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 u8c += *--p;
1850 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001851 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001854 u8c += (unsigned)*--p << 8;
1855 u8c += (unsigned)*--p << 16;
1856 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001859 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 if (*--p < 0x80)
1862 u8c = *p;
1863 else
1864 {
1865 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001866 p -= len;
1867 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (len == 0)
1869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001870 // Not a valid UTF-8 character, retry with
1871 // another fenc when possible, otherwise just
1872 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (can_retry)
1874 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001875 if (conv_error == 0)
1876 conv_error = readfile_linenr(linecnt,
1877 ptr, p);
1878 if (bad_char_behavior == BAD_DROP)
1879 continue;
1880 if (bad_char_behavior != BAD_KEEP)
1881 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 dest -= utf_char2len(u8c);
1888 (void)utf_char2bytes(u8c, dest);
1889 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001890 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 --dest;
1893 if (u8c >= 0x100)
1894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001895 // character doesn't fit in latin1, retry with
1896 // another fenc when possible, otherwise just
1897 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001898 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001900 if (conv_error == 0)
1901 conv_error = readfile_linenr(linecnt, ptr, p);
1902 if (bad_char_behavior == BAD_DROP)
1903 ++dest;
1904 else if (bad_char_behavior == BAD_KEEP)
1905 *dest = u8c;
1906 else if (eap != NULL && eap->bad_char != 0)
1907 *dest = bad_char_behavior;
1908 else
1909 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911 else
1912 *dest = u8c;
1913 }
1914 }
1915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001916 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 line_start = dest - linerest;
1918 mch_memmove(line_start, buffer, (size_t)linerest);
1919 size = (long)((ptr + real_size) - dest);
1920 ptr = dest;
1921 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001922 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001924 int incomplete_tail = FALSE;
1925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001926 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001927 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001929 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001930 int l;
1931
1932 if (todo <= 0)
1933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (*p >= 0x80)
1935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // A length of 1 means it's an illegal byte. Accept
1937 // an incomplete character at the end though, the next
1938 // read() will get the next bytes, we'll check it
1939 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001940 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001941 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 // Avoid retrying with a different encoding when
1944 // a truncated file is more likely, or attempting
1945 // to read the rest of an incomplete sequence when
1946 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001947 if (p > ptr || filesize > 0)
1948 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001949 // Incomplete byte sequence, move it to conv_rest[]
1950 // and try to read the rest of it, unless we've
1951 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001952 if (p > ptr)
1953 {
1954 conv_restlen = todo;
1955 mch_memmove(conv_rest, p, conv_restlen);
1956 size -= conv_restlen;
1957 break;
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001960 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001962 // Illegal byte. If we can try another encoding
1963 // do that, unless at EOF where a truncated
1964 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001965 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001966 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001967#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001969 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1970 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001971#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001973 if (conv_error == 0 && illegal_byte == 0)
1974 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001975
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001976 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001977 if (bad_char_behavior == BAD_DROP)
1978 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001979 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001980 --p;
1981 --size;
1982 }
1983 else if (bad_char_behavior != BAD_KEEP)
1984 *p = bad_char_behavior;
1985 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001986 else
1987 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001990 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001992 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01001995#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001996 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001998 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002000#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002002 advance_fenc = TRUE;
2003 file_rewind = TRUE;
2004 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002008 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 filesize += size;
2010
2011 /*
2012 * when reading the first part of a file: guess EOL type
2013 */
2014 if (fileformat == EOL_UNKNOWN)
2015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (try_dos || try_unix)
2018 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002020 if (try_mac)
2021 try_mac = 1;
2022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 for (p = ptr; p < ptr + size; ++p)
2024 {
2025 if (*p == NL)
2026 {
2027 if (!try_unix
2028 || (try_dos && p > ptr && p[-1] == CAR))
2029 fileformat = EOL_DOS;
2030 else
2031 fileformat = EOL_UNIX;
2032 break;
2033 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002034 else if (*p == CAR && try_mac)
2035 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (fileformat == EOL_UNIX && try_mac)
2040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002041 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 try_mac = 1;
2043 try_unix = 1;
2044 for (; p >= ptr && *p != CAR; p--)
2045 ;
2046 if (p >= ptr)
2047 {
2048 for (p = ptr; p < ptr + size; ++p)
2049 {
2050 if (*p == NL)
2051 try_unix++;
2052 else if (*p == CAR)
2053 try_mac++;
2054 }
2055 if (try_mac > try_unix)
2056 fileformat = EOL_MAC;
2057 }
2058 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002059 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002060 // Looking for CR but found no end-of-line markers at
2061 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002062 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (fileformat == EOL_UNKNOWN && try_mac)
2067 fileformat = EOL_MAC;
2068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (fileformat == EOL_UNKNOWN)
2071 fileformat = default_fileformat();
2072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002073 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002074 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 set_fileformat(fileformat, OPT_LOCAL);
2076 }
2077 }
2078
2079 /*
2080 * This loop is executed once for every character read.
2081 * Keep it fast!
2082 */
2083 if (fileformat == EOL_MAC)
2084 {
2085 --ptr;
2086 while (++ptr, --size >= 0)
2087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if ((c = *ptr) != NUL && c != CAR && c != NL)
2090 continue;
2091 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002092 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002094 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else
2096 {
2097 if (skip_count == 0)
2098 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 len = (colnr_T) (ptr - line_start + 1);
2101 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2102 {
2103 error = TRUE;
2104 break;
2105 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002106#ifdef FEAT_PERSISTENT_UNDO
2107 if (read_undo_file)
2108 sha256_update(&sha_ctx, line_start, len);
2109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 ++lnum;
2111 if (--read_count == 0)
2112 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002113 error = TRUE; // break loop
2114 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 break;
2116 }
2117 }
2118 else
2119 --skip_count;
2120 line_start = ptr + 1;
2121 }
2122 }
2123 }
2124 else
2125 {
2126 --ptr;
2127 while (++ptr, --size >= 0)
2128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 continue;
2131 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 else
2134 {
2135 if (skip_count == 0)
2136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 len = (colnr_T)(ptr - line_start + 1);
2139 if (fileformat == EOL_DOS)
2140 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002141 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002143 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 ptr[-1] = NUL;
2145 --len;
2146 }
2147 /*
2148 * Reading in Dos format, but no CR-LF found!
2149 * When 'fileformats' includes "unix", delete all
2150 * the lines read so far and start all over again.
2151 * Otherwise give an error message later.
2152 */
2153 else if (ff_error != EOL_DOS)
2154 {
2155 if ( try_unix
2156 && !read_stdin
2157 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002158 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2159 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002162 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 set_fileformat(EOL_UNIX, OPT_LOCAL);
2164 file_rewind = TRUE;
2165 keep_fileformat = TRUE;
2166 goto retry;
2167 }
2168 ff_error = EOL_DOS;
2169 }
2170 }
2171 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2172 {
2173 error = TRUE;
2174 break;
2175 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002176#ifdef FEAT_PERSISTENT_UNDO
2177 if (read_undo_file)
2178 sha256_update(&sha_ctx, line_start, len);
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 ++lnum;
2181 if (--read_count == 0)
2182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002183 error = TRUE; // break loop
2184 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 break;
2186 }
2187 }
2188 else
2189 --skip_count;
2190 line_start = ptr + 1;
2191 }
2192 }
2193 }
2194 linerest = (long)(ptr - line_start);
2195 ui_breakcheck();
2196 }
2197
2198failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002199 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (error && read_count == 0)
2201 error = FALSE;
2202
2203 /*
2204 * If we get EOF in the middle of a line, note the fact and
2205 * complete the line ourselves.
2206 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2207 */
2208 if (!error
2209 && !got_int
2210 && linerest != 0
2211 && !(!curbuf->b_p_bin
2212 && fileformat == EOL_DOS
2213 && *line_start == Ctrl_Z
2214 && ptr == line_start + 1))
2215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002216 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002217 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 curbuf->b_p_eol = FALSE;
2219 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002220 len = (colnr_T)(ptr - line_start + 1);
2221 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 error = TRUE;
2223 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002224 {
2225#ifdef FEAT_PERSISTENT_UNDO
2226 if (read_undo_file)
2227 sha256_update(&sha_ctx, line_start, len);
2228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002237 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002238 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002239 crypt_free_state(curbuf->b_cryptstate);
2240 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002241 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002242 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2243 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 // Don't set cryptkey to NULL, it's used below as a flag that
2245 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#endif
2247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 // If editing a new file: set 'fenc' for the current buffer.
2249 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002252 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (fenc_alloced)
2254 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002255#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258#endif
2259
2260 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002262#ifdef HAVE_FD_CLOEXEC
2263 else
2264 {
2265 int fdflags = fcntl(fd, F_GETFD);
2266 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002267 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002268 }
2269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 vim_free(buffer);
2271
2272#ifdef HAVE_DUP
2273 if (read_stdin)
2274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002275 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002277 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279#endif
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (tmpname != NULL)
2282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002283 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 vim_free(tmpname);
2285 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002286 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 /*
2289 * In recovery mode everything but autocommands is skipped.
2290 */
2291 if (!recoverymode)
2292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2295 {
2296#ifdef FEAT_NETBEANS_INTG
2297 netbeansFireChanges = 0;
2298#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002299 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#ifdef FEAT_NETBEANS_INTG
2301 netbeansFireChanges = 1;
2302#endif
2303 --linecnt;
2304 }
2305 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2306 if (filesize == 0)
2307 linecnt = 0;
2308 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002311#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // After reading the text into the buffer the diff info needs to
2313 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002314 diff_invalidate(curbuf);
2315#endif
2316#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002317 // All folds in the window are invalid now. Mark them for update
2318 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002319 foldUpdateAll(curwin);
2320#endif
2321 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 appended_lines_mark(from, linecnt);
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#ifndef ALWAYS_USE_GUI
2326 /*
2327 * If we were reading from the same terminal as where messages go,
2328 * the screen will have been messed up.
2329 * Switch on raw mode now and clear the screen.
2330 */
2331 if (read_stdin)
2332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 starttermcap();
2335 screenclear();
2336 }
2337#endif
2338
2339 if (got_int)
2340 {
2341 if (!(flags & READ_DUMMY))
2342 {
2343 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2344 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347 msg_scroll = msg_save;
2348#ifdef FEAT_VIMINFO
2349 check_marks_read();
2350#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002351 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353
2354 if (!filtering && !(flags & READ_DUMMY))
2355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 c = FALSE;
2358
2359#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362 STRCAT(IObuff, _("[fifo]"));
2363 c = TRUE;
2364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 STRCAT(IObuff, _("[socket]"));
2368 c = TRUE;
2369 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002370# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002371 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002372 {
2373 STRCAT(IObuff, _("[character special]"));
2374 c = TRUE;
2375 }
2376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#endif
2378 if (curbuf->b_p_ro)
2379 {
2380 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2381 c = TRUE;
2382 }
2383 if (read_no_eol_lnum)
2384 {
2385 msg_add_eol();
2386 c = TRUE;
2387 }
2388 if (ff_error == EOL_DOS)
2389 {
2390 STRCAT(IObuff, _("[CR missing]"));
2391 c = TRUE;
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (split)
2394 {
2395 STRCAT(IObuff, _("[long lines split]"));
2396 c = TRUE;
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (notconverted)
2399 {
2400 STRCAT(IObuff, _("[NOT converted]"));
2401 c = TRUE;
2402 }
2403 else if (converted)
2404 {
2405 STRCAT(IObuff, _("[converted]"));
2406 c = TRUE;
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#ifdef FEAT_CRYPT
2409 if (cryptkey != NULL)
2410 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002411 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 c = TRUE;
2413 }
2414#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002415 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002417 sprintf((char *)IObuff + STRLEN(IObuff),
2418 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 c = TRUE;
2420 }
2421 else if (illegal_byte > 0)
2422 {
2423 sprintf((char *)IObuff + STRLEN(IObuff),
2424 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2425 c = TRUE;
2426 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002427 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
2429 STRCAT(IObuff, _("[READ ERRORS]"));
2430 c = TRUE;
2431 }
2432 if (msg_add_fileformat(fileformat))
2433 c = TRUE;
2434#ifdef FEAT_CRYPT
2435 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002436 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002437 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 else
2439#endif
2440 msg_add_lines(c, (long)linecnt, filesize);
2441
Bram Moolenaard23a8232018-02-10 18:45:26 +01002442 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 msg_scrolled_ign = TRUE;
2444#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002445 // Don't show the message when reading stdin, it would end up in a
2446 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (read_stdin || read_buffer)
2448 p = msg_may_trunc(FALSE, IObuff);
2449 else
2450#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002451 {
2452 if (msg_col > 0)
2453 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002454 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002457 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002458 // Need to repeat the message after redrawing when:
2459 // - When reading from stdin (the screen will be cleared next).
2460 // - When restart_edit is set (otherwise there will be a delay
2461 // before redrawing).
2462 // - When the screen was scrolled but there is no wait-return
2463 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002464 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 msg_scrolled_ign = FALSE;
2466 }
2467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002468 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002470 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002471 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 curbuf->b_p_ro = TRUE;
2473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002474 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 /*
2477 * In Ex mode: cursor at last new line.
2478 * Otherwise: cursor at first new line.
2479 */
2480 if (exmode_active)
2481 curwin->w_cursor.lnum = from + linecnt;
2482 else
2483 curwin->w_cursor.lnum = from + 1;
2484 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002485 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002487 if (!cmdmod.lockmarks)
2488 {
2489 // Set '[ and '] marks to the newly read lines.
2490 curbuf->b_op_start.lnum = from + 1;
2491 curbuf->b_op_start.col = 0;
2492 curbuf->b_op_end.lnum = from + linecnt;
2493 curbuf->b_op_end.col = 0;
2494 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002495
Bram Moolenaar4f974752019-02-17 17:44:42 +01002496#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002497 /*
2498 * Work around a weird problem: When a file has two links (only
2499 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002500 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002501 * It's correct again after reading the file, thus reset the timestamp
2502 * here.
2503 */
2504 if (newfile && !read_stdin && !read_buffer
2505 && mch_stat((char *)fname, &st) >= 0)
2506 {
2507 buf_store_time(curbuf, &st, fname);
2508 curbuf->b_mtime_read = curbuf->b_mtime;
2509 }
2510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
2512 msg_scroll = msg_save;
2513
2514#ifdef FEAT_VIMINFO
2515 /*
2516 * Get the marks before executing autocommands, so they can be used there.
2517 */
2518 check_marks_read();
2519#endif
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002522 * We remember if the last line of the read didn't have
2523 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2524 * or writing the read again with 'binary' on. The latter is required
2525 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002527 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002529 // When reloading a buffer put the cursor at the first line that is
2530 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002531 if (flags & READ_KEEP_UNDO)
2532 u_find_first_changed();
2533
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002534#ifdef FEAT_PERSISTENT_UNDO
2535 /*
2536 * When opening a new file locate undo info and read it.
2537 */
2538 if (read_undo_file)
2539 {
2540 char_u hash[UNDO_HASH_SIZE];
2541
2542 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002543 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002544 }
2545#endif
2546
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002547 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 int m = msg_scroll;
2550 int n = msg_scrolled;
2551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002552 // Save the fileformat now, otherwise the buffer will be considered
2553 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002554 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 save_file_ff(curbuf);
2556
2557 /*
2558 * The output from the autocommands should not overwrite anything and
2559 * should not be overwritten: Set msg_scroll, restore its value if no
2560 * output was done.
2561 */
2562 msg_scroll = TRUE;
2563 if (filtering)
2564 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2565 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002566 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2569 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002570 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2571 /*
2572 * EVENT_FILETYPE was not triggered but the buffer already has a
2573 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2574 */
2575 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2576 TRUE, curbuf);
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 else
2579 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2580 FALSE, NULL, eap);
2581 if (msg_scrolled == n)
2582 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002583# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 if (recoverymode && error)
2590 return FAIL;
2591 return OK;
2592}
2593
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002594#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002595/*
2596 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2597 * which is the name of files used for process substitution output by
2598 * some shells on some operating systems, e.g., bash on SunOS.
2599 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2600 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002601 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002603{
2604 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2605 && VIM_ISDIGIT(fname[8])
2606 && *skipdigits(fname + 9) == NUL
2607 && (fname[9] != NUL
2608 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2609}
2610#endif
2611
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002612/*
2613 * From the current line count and characters read after that, estimate the
2614 * line number where we are now.
2615 * Used for error messages that include a line number.
2616 */
2617 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002618readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002619 linenr_T linecnt, // line count before reading more bytes
2620 char_u *p, // start of more bytes read
2621 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002622{
2623 char_u *s;
2624 linenr_T lnum;
2625
2626 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2627 for (s = p; s < endp; ++s)
2628 if (*s == '\n')
2629 ++lnum;
2630 return lnum;
2631}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002634 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2635 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * Returns OK or FAIL.
2637 */
2638 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002639prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaar13505972019-01-24 15:04:48 +01002641 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (eap->cmd == NULL)
2643 return FAIL;
2644
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002645 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2646 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002647 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002648 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002649
2650 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002651 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002652 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 return OK;
2654}
2655
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002656/*
2657 * Set default or forced 'fileformat' and 'binary'.
2658 */
2659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002660set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002661{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002662 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002663 if (set_options)
2664 {
2665 if (eap != NULL && eap->force_ff != 0)
2666 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2667 else if (*p_ffs != NUL)
2668 set_fileformat(default_fileformat(), OPT_LOCAL);
2669 }
2670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002671 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002672 if (eap != NULL && eap->force_bin != 0)
2673 {
2674 int oldval = curbuf->b_p_bin;
2675
2676 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2677 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2678 }
2679}
2680
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002681/*
2682 * Set forced 'fileencoding'.
2683 */
2684 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002685set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002686{
2687 if (eap->force_enc != 0)
2688 {
2689 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2690
2691 if (fenc != NULL)
2692 set_string_option_direct((char_u *)"fenc", -1,
2693 fenc, OPT_FREE|OPT_LOCAL, 0);
2694 vim_free(fenc);
2695 }
2696}
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698/*
2699 * Find next fileencoding to use from 'fileencodings'.
2700 * "pp" points to fenc_next. It's advanced to the next item.
2701 * When there are no more items, an empty string is returned and *pp is set to
2702 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002703 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2704 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 */
2706 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002707next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 char_u *p;
2710 char_u *r;
2711
Bram Moolenaarf077db22019-08-13 00:18:24 +02002712 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (**pp == NUL)
2714 {
2715 *pp = NULL;
2716 return (char_u *)"";
2717 }
2718 p = vim_strchr(*pp, ',');
2719 if (p == NULL)
2720 {
2721 r = enc_canonize(*pp);
2722 *pp += STRLEN(*pp);
2723 }
2724 else
2725 {
2726 r = vim_strnsave(*pp, (int)(p - *pp));
2727 *pp = p + 1;
2728 if (r != NULL)
2729 {
2730 p = enc_canonize(r);
2731 vim_free(r);
2732 r = p;
2733 }
2734 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002735 if (r != NULL)
2736 *alloced = TRUE;
2737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002739 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 r = (char_u *)"";
2741 *pp = NULL;
2742 }
2743 return r;
2744}
2745
Bram Moolenaar13505972019-01-24 15:04:48 +01002746#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747/*
2748 * Convert a file with the 'charconvert' expression.
2749 * This closes the file which is to be read, converts it and opens the
2750 * resulting file for reading.
2751 * Returns name of the resulting converted file (the caller should delete it
2752 * after reading it).
2753 * Returns NULL if the conversion failed ("*fdp" is not set) .
2754 */
2755 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002756readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002757 char_u *fname, // name of input file
2758 char_u *fenc, // converted from
2759 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
2761 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002762 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002764 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002766 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else
2768 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002769 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 *fdp = -1;
2771 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2772 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002773 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2775 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002776 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
2778
2779 if (errmsg != NULL)
2780 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002781 // Don't use emsg(), it breaks mappings, the retry with
2782 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002783 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (tmpname != NULL)
2785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002786 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002787 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 }
2790
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002791 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (*fdp < 0)
2793 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2794
2795 return tmpname;
2796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797#endif
2798
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002799#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002801 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2803 * *filesizep are updated.
2804 * Return the (new) encryption key, NULL for no encryption.
2805 */
2806 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002807check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002808 char_u *cryptkey, // previous encryption key or NULL
2809 char_u *ptr, // pointer to read bytes
2810 long *sizep, // length of read bytes
2811 off_T *filesizep, // nr of bytes used from file
2812 int newfile, // editing a new buffer
2813 char_u *fname, // file name to display
2814 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002816 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002817 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002818
2819 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002821 // Mark the buffer as read-only until the decryption has taken place.
2822 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002823 curbuf->b_p_ro = TRUE;
2824
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002825 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002826 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002827 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
2829 if (*curbuf->b_p_key)
2830 cryptkey = curbuf->b_p_key;
2831 else
2832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002833 // When newfile is TRUE, store the typed key in the 'key'
2834 // option and don't free it. bf needs hash of the key saved.
2835 // Don't ask for the key again when first time Enter was hit.
2836 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002837 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002838 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002839 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002840 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002841 *did_ask = TRUE;
2842
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002843 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (cryptkey != NULL && *cryptkey == NUL)
2845 {
2846 if (cryptkey != curbuf->b_p_key)
2847 vim_free(cryptkey);
2848 cryptkey = NULL;
2849 }
2850 }
2851 }
2852
2853 if (cryptkey != NULL)
2854 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002855 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002856
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002857 curbuf->b_cryptstate = crypt_create_from_header(
2858 method, cryptkey, ptr);
2859 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002861 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002862 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002863 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002864 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002865 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002866 *filesizep += header_len;
2867 *sizep -= header_len;
2868 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002870 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002871 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 // When starting to edit a new file which does not have encryption, clear
2875 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002876 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2878
2879 return cryptkey;
2880}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002881#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002884 * Return TRUE if a file appears to be read-only from the file permissions.
2885 */
2886 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002887check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002888 char_u *fname, // full path to file
2889 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002890{
2891#ifndef USE_MCH_ACCESS
2892 int fd = 0;
2893#endif
2894
2895 return (
2896#ifdef USE_MCH_ACCESS
2897# ifdef UNIX
2898 (perm & 0222) == 0 ||
2899# endif
2900 mch_access((char *)fname, W_OK)
2901#else
2902 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2903 ? TRUE : (close(fd), FALSE)
2904#endif
2905 );
2906}
2907
Bram Moolenaara7870192019-02-14 12:56:36 +01002908#if defined(HAVE_FSYNC) || defined(PROTO)
2909/*
2910 * Call fsync() with Mac-specific exception.
2911 * Return fsync() result: zero for success.
2912 */
2913 int
2914vim_fsync(int fd)
2915{
2916 int r;
2917
2918# ifdef MACOS_X
2919 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002920 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002921# endif
2922 r = fsync(fd);
2923 return r;
2924}
2925#endif
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002928 * Set the name of the current buffer. Use when the buffer doesn't have a
2929 * name and a ":r" or ":w" command with a file name is used.
2930 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002931 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002932set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002933{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002934 buf_T *buf = curbuf;
2935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002936 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002937 if (curbuf->b_p_bl)
2938 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2939 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002940#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002941 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002942 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002943#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002944 if (curbuf != buf)
2945 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002946 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002947 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002948 return FAIL;
2949 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002950
2951 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2952 curbuf->b_flags |= BF_NOTEDITED;
2953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002954 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002955 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2956 if (curbuf->b_p_bl)
2957 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002958#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002959 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002960 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002961#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002964 if (*curbuf->b_p_ft == NUL)
2965 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002966 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002967 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002968 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002969 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002970
2971 return OK;
2972}
2973
2974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 * Put file name into IObuff with quotes.
2976 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002977 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002978msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 if (fname == NULL)
2981 fname = (char_u *)"-stdin-";
2982 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2983 IObuff[0] = '"';
2984 STRCAT(IObuff, "\" ");
2985}
2986
2987/*
2988 * Append message for text mode to IObuff.
2989 * Return TRUE if something appended.
2990 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002991 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002992msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994#ifndef USE_CRNL
2995 if (eol_type == EOL_DOS)
2996 {
2997 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
2998 return TRUE;
2999 }
3000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (eol_type == EOL_MAC)
3002 {
3003 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3004 return TRUE;
3005 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003006#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (eol_type == EOL_UNIX)
3008 {
3009 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3010 return TRUE;
3011 }
3012#endif
3013 return FALSE;
3014}
3015
3016/*
3017 * Append line and character count to IObuff.
3018 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020msg_add_lines(
3021 int insert_space,
3022 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003023 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 char_u *p;
3026
3027 p = IObuff + STRLEN(IObuff);
3028
3029 if (insert_space)
3030 *p++ = ' ';
3031 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003032 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003033 "%ldL, %lldC", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 else
3035 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003036 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003038 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
3039 NGETTEXT("%lld character", "%lld characters", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003040 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042}
3043
3044/*
3045 * Append message for missing line separator to IObuff.
3046 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003048msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3051}
3052
Bram Moolenaar473952e2019-09-28 16:30:04 +02003053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003054time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003056#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003057 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3058 // the seconds. Since the roundoff is done when flushing the inode, the
3059 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return (t1 - t2 > 1 || t2 - t1 > 1);
3061#else
3062 return (t1 != t2);
3063#endif
3064}
3065
3066/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003067 * Return TRUE if file encoding "fenc" requires conversion from or to
3068 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003070 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003071need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003073 int same_encoding;
3074 int enc_flags;
3075 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003077 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003078 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003079 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003080 fenc_flags = 0;
3081 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003082 else
3083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003084 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3085 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003086 enc_flags = get_fio_flags(p_enc);
3087 fenc_flags = get_fio_flags(fenc);
3088 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3089 }
3090 if (same_encoding)
3091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003092 // Specified encoding matches with 'encoding'. This requires
3093 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003094 return enc_unicode != 0;
3095 }
3096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003097 // Encodings differ. However, conversion is not needed when 'enc' is any
3098 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003099 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100}
3101
3102/*
3103 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3104 * internal conversion.
3105 * if "ptr" is an empty string, use 'encoding'.
3106 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003107 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003108get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int prop;
3111
3112 if (*ptr == NUL)
3113 ptr = p_enc;
3114
3115 prop = enc_canon_props(ptr);
3116 if (prop & ENC_UNICODE)
3117 {
3118 if (prop & ENC_2BYTE)
3119 {
3120 if (prop & ENC_ENDIAN_L)
3121 return FIO_UCS2 | FIO_ENDIAN_L;
3122 return FIO_UCS2;
3123 }
3124 if (prop & ENC_4BYTE)
3125 {
3126 if (prop & ENC_ENDIAN_L)
3127 return FIO_UCS4 | FIO_ENDIAN_L;
3128 return FIO_UCS4;
3129 }
3130 if (prop & ENC_2WORD)
3131 {
3132 if (prop & ENC_ENDIAN_L)
3133 return FIO_UTF16 | FIO_ENDIAN_L;
3134 return FIO_UTF16;
3135 }
3136 return FIO_UTF8;
3137 }
3138 if (prop & ENC_LATIN1)
3139 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003140 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return 0;
3142}
3143
Bram Moolenaar473952e2019-09-28 16:30:04 +02003144#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3147 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3148 * Used for conversion between 'encoding' and 'fileencoding'.
3149 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003150 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 int cp;
3154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003155 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (!enc_utf8 && enc_codepage <= 0)
3157 return 0;
3158
3159 cp = encname2codepage(ptr);
3160 if (cp == 0)
3161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003162# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (STRCMP(ptr, "utf-8") == 0)
3164 cp = CP_UTF8;
3165 else
3166# endif
3167 return 0;
3168 }
3169 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3170}
3171#endif
3172
Bram Moolenaar473952e2019-09-28 16:30:04 +02003173#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174/*
3175 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3176 * needed for the internal conversion to/from utf-8 or latin1.
3177 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003178 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003179get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3182 && (enc_canon_props(ptr) & ENC_MACROMAN))
3183 return FIO_MACROMAN;
3184 return 0;
3185}
3186#endif
3187
3188/*
3189 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3190 * "size" must be at least 2.
3191 * Return the name of the encoding and set "*lenp" to the length.
3192 * Returns NULL when no BOM found.
3193 */
3194 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003195check_for_bom(
3196 char_u *p,
3197 long size,
3198 int *lenp,
3199 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 char *name = NULL;
3202 int len = 2;
3203
3204 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003205 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003207 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 len = 3;
3209 }
3210 else if (p[0] == 0xff && p[1] == 0xfe)
3211 {
3212 if (size >= 4 && p[2] == 0 && p[3] == 0
3213 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003215 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 len = 4;
3217 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003218 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003219 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003220 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003221 // utf-16le is preferred, it also works for ucs-2le text
3222 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224 else if (p[0] == 0xfe && p[1] == 0xff
3225 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003227 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003228 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003230 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003231 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3234 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003236 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 len = 4;
3238 }
3239
3240 *lenp = len;
3241 return (char_u *)name;
3242}
3243
3244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * Try to find a shortname by comparing the fullname with the current
3246 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003247 * Returns "full_path" or pointer into "full_path" if shortened.
3248 */
3249 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003250shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003251{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003252 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003253 char_u *p = full_path;
3254
Bram Moolenaard9462e32011-04-11 21:35:11 +02003255 dirname = alloc(MAXPATHL);
3256 if (dirname == NULL)
3257 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003258 if (mch_dirname(dirname, MAXPATHL) == OK)
3259 {
3260 p = shorten_fname(full_path, dirname);
3261 if (p == NULL || *p == NUL)
3262 p = full_path;
3263 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003264 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003265 return p;
3266}
3267
3268/*
3269 * Try to find a shortname by comparing the fullname with the current
3270 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 * Returns NULL if not shorter name possible, pointer into "full_path"
3272 * otherwise.
3273 */
3274 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003275shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 int len;
3278 char_u *p;
3279
3280 if (full_path == NULL)
3281 return NULL;
3282 len = (int)STRLEN(dir_name);
3283 if (fnamencmp(dir_name, full_path, len) == 0)
3284 {
3285 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003286#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003288 * MS-Windows: when a file is in the root directory, dir_name will end
3289 * in a slash, since C: by itself does not define a specific dir. In
3290 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 */
3292 if (!((len > 2) && (*(p - 2) == ':')))
3293#endif
3294 {
3295 if (vim_ispathsep(*p))
3296 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003297#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 else
3299 p = NULL;
3300#endif
3301 }
3302 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003303#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 /*
3305 * When using a file in the current drive, remove the drive name:
3306 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3307 * a floppy from "A:\dir" to "B:\dir".
3308 */
3309 else if (len > 3
3310 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3311 && full_path[1] == ':'
3312 && vim_ispathsep(full_path[2]))
3313 p = full_path + 2;
3314#endif
3315 else
3316 p = NULL;
3317 return p;
3318}
3319
3320/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003321 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 * When "force" is TRUE: Use full path from now on for files currently being
3323 * edited, both for file name and swap file name. Try to shorten the file
3324 * names a bit, if safe to do so.
3325 * When "force" is FALSE: Only try to shorten absolute file names.
3326 * For buffers that have buftype "nofile" or "scratch": never change the file
3327 * name.
3328 */
3329 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003330shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3331{
3332 char_u *p;
3333
3334 if (buf->b_fname != NULL
3335#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003336 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003337#endif
3338 && !path_with_url(buf->b_fname)
3339 && (force
3340 || buf->b_sfname == NULL
3341 || mch_isFullName(buf->b_sfname)))
3342 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003343 if (buf->b_sfname != buf->b_ffname)
3344 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003345 p = shorten_fname(buf->b_ffname, dirname);
3346 if (p != NULL)
3347 {
3348 buf->b_sfname = vim_strsave(p);
3349 buf->b_fname = buf->b_sfname;
3350 }
3351 if (p == NULL || buf->b_fname == NULL)
3352 buf->b_fname = buf->b_ffname;
3353 }
3354}
3355
3356/*
3357 * Shorten filenames for all buffers.
3358 */
3359 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003360shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 char_u dirname[MAXPATHL];
3363 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003366 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003368 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003370 // Always make the swap file name a full path, a "nofile" buffer may
3371 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003372 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003375 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003376#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003377 popup_update_preview_title();
3378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
3381#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3382 || defined(FEAT_GUI_MSWIN) \
3383 || defined(FEAT_GUI_MAC) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003384 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 || defined(PROTO)
3386/*
3387 * Shorten all filenames in "fnames[count]" by current directory.
3388 */
3389 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003390shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 int i;
3393 char_u dirname[MAXPATHL];
3394 char_u *p;
3395
3396 if (fnames == NULL || count < 1)
3397 return;
3398 mch_dirname(dirname, sizeof(dirname));
3399 for (i = 0; i < count; ++i)
3400 {
3401 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3402 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003403 // shorten_fname() returns pointer in given "fnames[i]". If free
3404 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3405 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 p = vim_strsave(p);
3407 vim_free(fnames[i]);
3408 fnames[i] = p;
3409 }
3410 }
3411}
3412#endif
3413
3414/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003415 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 * fo_o_h.ext for MSDOS or when shortname option set.
3417 *
3418 * Assumed that fname is a valid name found in the filesystem we assure that
3419 * the return value is a different name and ends in 'ext'.
3420 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3421 * characters otherwise.
3422 * Space for the returned name is allocated, must be freed later.
3423 * Returns NULL when out of memory.
3424 */
3425 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003426modname(
3427 char_u *fname,
3428 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003429 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003431 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 fname, ext, prepend_dot);
3433}
3434
3435 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003436buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003437 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003438 char_u *fname,
3439 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003440 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 char_u *retval;
3443 char_u *s;
3444 char_u *e;
3445 char_u *ptr;
3446 int fnamelen, extlen;
3447
3448 extlen = (int)STRLEN(ext);
3449
3450 /*
3451 * If there is no file name we must get the name of the current directory
3452 * (we need the full path in case :cd is used).
3453 */
3454 if (fname == NULL || *fname == NUL)
3455 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003456 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (retval == NULL)
3458 return NULL;
3459 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3460 (fnamelen = (int)STRLEN(retval)) == 0)
3461 {
3462 vim_free(retval);
3463 return NULL;
3464 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003465 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 retval[fnamelen++] = PATHSEP;
3468 retval[fnamelen] = NUL;
3469 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003470 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472 else
3473 {
3474 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003475 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (retval == NULL)
3477 return NULL;
3478 STRCPY(retval, fname);
3479#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003480 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
3482 }
3483
3484 /*
3485 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3486 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3487 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3488 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3489 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003490 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003492 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003493 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003496 {
3497 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003502 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3504 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 s = ptr + STRLEN(ptr);
3507
3508 /*
3509 * For 8.3 file names we may have to reduce the length.
3510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 /*
3514 * If there is no file name, or the file name ends in '/', and the
3515 * extension starts with '.', put a '_' before the dot, because just
3516 * ".ext" is invalid.
3517 */
3518 if (fname == NULL || *fname == NUL
3519 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 *s++ = '_';
3523 }
3524 /*
3525 * If the extension starts with '.', truncate the base name at 8
3526 * characters
3527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003530 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 s = ptr + 8;
3533 *s = '\0';
3534 }
3535 }
3536 /*
3537 * If the extension doesn't start with '.', and the file name
3538 * doesn't have an extension yet, append a '.'
3539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 else if ((e = vim_strchr(ptr, '.')) == NULL)
3541 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /*
3543 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003544 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 */
3546 else if ((int)STRLEN(e) + extlen > 4)
3547 s = e + 4 - extlen;
3548 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003549#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /*
3551 * If there is no file name, and the extension starts with '.', put a
3552 * '_' before the dot, because just ".ext" may be invalid if it's on a
3553 * FAT partition, and on HPFS it doesn't matter.
3554 */
3555 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3556 *s++ = '_';
3557#endif
3558
3559 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003560 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 * ext can start with '.' and cannot exceed 3 more characters.
3562 */
3563 STRCPY(s, ext);
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 /*
3566 * Prepend the dot.
3567 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003568 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003570 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
3574 /*
3575 * Check that, after appending the extension, the file name is really
3576 * different.
3577 */
3578 if (fname != NULL && STRCMP(fname, retval) == 0)
3579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003580 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 while (--s >= ptr)
3582 {
3583 if (*s != '_')
3584 {
3585 *s = '_';
3586 break;
3587 }
3588 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003589 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 *ptr = 'v';
3591 }
3592 return retval;
3593}
3594
3595/*
3596 * Like fgets(), but if the file line is too long, it is truncated and the
3597 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003598 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 */
3600 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003601vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 char *eof;
3604#define FGETS_SIZE 200
3605 char tbuf[FGETS_SIZE];
3606
3607 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3610 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003611 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003613 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 do
3615 {
3616 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003617 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3619 }
3620 return (eof == NULL);
3621}
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
3624 * rename() only works if both files are on the same file system, this
3625 * function will (attempts to?) copy the file across if rename fails -- webb
3626 * Return -1 for failure, 0 for success.
3627 */
3628 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003629vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 int fd_in;
3632 int fd_out;
3633 int n;
3634 char *errmsg = NULL;
3635 char *buffer;
3636#ifdef AMIGA
3637 BPTR flock;
3638#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003639 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003640 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003641#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003642 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003643#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003644 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003647 * When the names are identical, there is nothing to do. When they refer
3648 * to the same file (ignoring case and slash/backslash differences) but
3649 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
3651 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003652 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003653 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003654 use_tmp_file = TRUE;
3655 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003656 return 0;
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
3659 /*
3660 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3661 */
3662 if (mch_stat((char *)from, &st) < 0)
3663 return -1;
3664
Bram Moolenaar3576da72008-12-30 15:15:57 +00003665#ifdef UNIX
3666 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003667 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003669 // It's possible for the source and destination to be the same file.
3670 // This happens when "from" and "to" differ in case and are on a FAT32
3671 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003672 if (mch_stat((char *)to, &st_to) >= 0
3673 && st.st_dev == st_to.st_dev
3674 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003675 use_tmp_file = TRUE;
3676 }
3677#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003678#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003679 {
3680 BY_HANDLE_FILE_INFORMATION info1, info2;
3681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003682 // It's possible for the source and destination to be the same file.
3683 // In that case go through a temp file name. This makes rename("foo",
3684 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003685 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3686 && win32_fileinfo(to, &info2) == FILEINFO_OK
3687 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3688 && info1.nFileIndexHigh == info2.nFileIndexHigh
3689 && info1.nFileIndexLow == info2.nFileIndexLow)
3690 use_tmp_file = TRUE;
3691 }
3692#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003693
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003694 if (use_tmp_file)
3695 {
3696 char tempname[MAXPATHL + 1];
3697
3698 /*
3699 * Find a name that doesn't exist and is in the same directory.
3700 * Rename "from" to "tempname" and then rename "tempname" to "to".
3701 */
3702 if (STRLEN(from) >= MAXPATHL - 5)
3703 return -1;
3704 STRCPY(tempname, from);
3705 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003706 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003707 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3708 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003709 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003710 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003711 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003712 if (mch_rename(tempname, (char *)to) == 0)
3713 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003714 // Strange, the second step failed. Try moving the
3715 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003716 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003717 return -1;
3718 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 // If it fails for one temp name it will most likely fail
3720 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003721 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003722 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003723 }
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 Moolenaar071d4272004-06-13 20:20:40 +00003727 /*
3728 * Delete the "to" file, this is required on some systems to make the
3729 * mch_rename() work, on other systems it makes sure that we don't have
3730 * two files when the mch_rename() fails.
3731 */
3732
3733#ifdef AMIGA
3734 /*
3735 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3736 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003737 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * deleting the "from" file (horror!) we lock it during the remove.
3739 *
3740 * When used for making a backup before writing the file: This should not
3741 * happen with ":w", because startscript() should detect this problem and
3742 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3743 * name. This problem does exist with ":w filename", but then the
3744 * original file will be somewhere else so the backup isn't really
3745 * important. If autoscripting is off the rename may fail.
3746 */
3747 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3748#endif
3749 mch_remove(to);
3750#ifdef AMIGA
3751 if (flock)
3752 UnLock(flock);
3753#endif
3754
3755 /*
3756 * First try a normal rename, return if it works.
3757 */
3758 if (mch_rename((char *)from, (char *)to) == 0)
3759 return 0;
3760
3761 /*
3762 * Rename() failed, try copying the file.
3763 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003764 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003765#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003766 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003767 acl = mch_get_acl(from);
3768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3770 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003771 {
3772#ifdef HAVE_ACL
3773 mch_free_acl(acl);
3774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003776 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003778 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003779 fd_out = mch_open((char *)to,
3780 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (fd_out == -1)
3782 {
3783 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003784#ifdef HAVE_ACL
3785 mch_free_acl(acl);
3786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return -1;
3788 }
3789
Bram Moolenaar473952e2019-09-28 16:30:04 +02003790 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (buffer == NULL)
3792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003794 close(fd_in);
3795#ifdef HAVE_ACL
3796 mch_free_acl(acl);
3797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return -1;
3799 }
3800
Bram Moolenaar473952e2019-09-28 16:30:04 +02003801 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003802 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
3804 errmsg = _("E208: Error writing to \"%s\"");
3805 break;
3806 }
3807
3808 vim_free(buffer);
3809 close(fd_in);
3810 if (close(fd_out) < 0)
3811 errmsg = _("E209: Error closing \"%s\"");
3812 if (n < 0)
3813 {
3814 errmsg = _("E210: Error reading \"%s\"");
3815 to = from;
3816 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003817#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003818 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003819#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003820#ifdef HAVE_ACL
3821 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003822 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003823#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003824#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003825 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (errmsg != NULL)
3828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003829 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return -1;
3831 }
3832 mch_remove(from);
3833 return 0;
3834}
3835
3836static int already_warned = FALSE;
3837
3838/*
3839 * Check if any not hidden buffer has been changed.
3840 * Postpone the check if there are characters in the stuff buffer, a global
3841 * command is being executed, a mapping is being executed or an autocommand is
3842 * busy.
3843 * Returns TRUE if some message was written (screen should be redrawn and
3844 * cursor positioned).
3845 */
3846 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003847check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003848 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849{
3850 buf_T *buf;
3851 int didit = 0;
3852 int n;
3853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 // Don't check timestamps while system() or another low-level function may
3855 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (no_check_timestamps > 0)
3857 return FALSE;
3858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003859 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3860 // event and we would keep on checking if the file is steadily growing.
3861 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (focus && did_check_timestamps)
3863 {
3864 need_check_timestamps = TRUE;
3865 return FALSE;
3866 }
3867
3868 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003869 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003870 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
3872 {
3873 ++no_wait_return;
3874 did_check_timestamps = TRUE;
3875 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003876 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003878 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (buf->b_nwindows > 0)
3880 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003881 bufref_T bufref;
3882
3883 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 n = buf_check_timestamp(buf, focus);
3885 if (didit < n)
3886 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003887 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003889 // Autocommands have removed the buffer, start at the
3890 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 buf = firstbuf;
3892 continue;
3893 }
3894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 --no_wait_return;
3897 need_check_timestamps = FALSE;
3898 if (need_wait_return && didit == 2)
3899 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003900 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003901 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 out_flush();
3903 }
3904 }
3905 return didit;
3906}
3907
3908/*
3909 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3910 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3911 * empty.
3912 */
3913 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003914move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 buf_T *tbuf = curbuf;
3917 int retval = OK;
3918 linenr_T lnum;
3919 char_u *p;
3920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003921 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 curbuf = tobuf;
3923 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3924 {
3925 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3926 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3927 {
3928 vim_free(p);
3929 retval = FAIL;
3930 break;
3931 }
3932 vim_free(p);
3933 }
3934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003935 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if (retval != FAIL)
3937 {
3938 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003939 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003940 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003942 // Oops! We could try putting back the saved lines, but that
3943 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 retval = FAIL;
3945 break;
3946 }
3947 }
3948
3949 curbuf = tbuf;
3950 return retval;
3951}
3952
3953/*
3954 * Check if buffer "buf" has been changed.
3955 * Also check if the file for a new buffer unexpectedly appeared.
3956 * return 1 if a changed buffer was found.
3957 * return 2 if a message has been displayed.
3958 * return 0 otherwise.
3959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003961buf_check_timestamp(
3962 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003963 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003965 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int stat_res;
3967 int retval = 0;
3968 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003969 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003971 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 int helpmesg = FALSE;
3973 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003974 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3976 int can_reload = FALSE;
3977#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003978 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 int orig_mode = buf->b_orig_mode;
3980#ifdef FEAT_GUI
3981 int save_mouse_correct = need_mouse_correct;
3982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003984 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003985#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003986 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003987#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003988 bufref_T bufref;
3989
3990 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003992 // If there is no file name, the buffer is not loaded, 'buftype' is
3993 // set, we are in the middle of a save or being called recursively: ignore
3994 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (buf->b_ffname == NULL
3996 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02003997 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004000#ifdef FEAT_NETBEANS_INTG
4001 || isNetbeansBuffer(buf)
4002#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004003#ifdef FEAT_TERMINAL
4004 || buf->b_term != NULL
4005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 )
4007 return 0;
4008
4009 if ( !(buf->b_flags & BF_NOTEDITED)
4010 && buf->b_mtime != 0
4011 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4012 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004013 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#ifdef HAVE_ST_MODE
4015 || (int)st.st_mode != buf->b_orig_mode
4016#else
4017 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4018#endif
4019 ))
4020 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004021 long prev_b_mtime = buf->b_mtime;
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 retval = 1;
4024
Bram Moolenaar386bc822018-07-07 18:34:12 +02004025 // set b_mtime to stop further warnings (e.g., when executing
4026 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (stat_res < 0)
4028 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004029 // Check the file again later to see if it re-appears.
4030 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 buf->b_orig_size = 0;
4032 buf->b_orig_mode = 0;
4033 }
4034 else
4035 buf_store_time(buf, &st, buf->b_ffname);
4036
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004037 // Don't do anything for a directory. Might contain the file
4038 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (mch_isdir(buf->b_fname))
4040 ;
4041
4042 /*
4043 * If 'autoread' is set, the buffer has no changes and the file still
4044 * exists, reload the buffer. Use the buffer-local option value if it
4045 * was set, the global option value otherwise.
4046 */
4047 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4048 && !bufIsChanged(buf) && stat_res >= 0)
4049 reload = TRUE;
4050 else
4051 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004052 if (stat_res < 0)
4053 reason = "deleted";
4054 else if (bufIsChanged(buf))
4055 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004056 /*
4057 * Check if the file contents really changed to avoid giving a
4058 * warning when only the timestamp was set (e.g., checked out of
4059 * CVS). Always warn when the buffer was changed.
4060 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004061 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4062 reason = "changed";
4063 else if (orig_mode != buf->b_orig_mode)
4064 reason = "mode";
4065 else
4066 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 /*
4069 * Only give the warning if there are no FileChangedShell
4070 * autocommands.
4071 * Avoid being called recursively by setting "busy".
4072 */
4073 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004074#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004075 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4076 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004077#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004078 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4080 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004081 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 busy = FALSE;
4083 if (n)
4084 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004085 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004086 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004087#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004088 s = get_vim_var_str(VV_FCS_CHOICE);
4089 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4090 reload = TRUE;
4091 else if (STRCMP(s, "ask") == 0)
4092 n = FALSE;
4093 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004094#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004095 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004097 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004099 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004100 {
4101 // Only give the message once.
4102 if (prev_b_mtime != -1)
4103 mesg = _("E211: File \"%s\" no longer available");
4104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 else
4106 {
4107 helpmesg = TRUE;
4108#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4109 can_reload = TRUE;
4110#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004111 if (reason[2] == 'n')
4112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004114 mesg2 = _("See \":help W12\" for more info.");
4115 }
4116 else if (reason[1] == 'h')
4117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004119 mesg2 = _("See \":help W11\" for more info.");
4120 }
4121 else if (*reason == 'm')
4122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004124 mesg2 = _("See \":help W16\" for more info.");
4125 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004126 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004127 // Only timestamp changed, store it to avoid a warning
4128 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004129 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 }
4132 }
4133
4134 }
4135 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4136 && vim_fexists(buf->b_ffname))
4137 {
4138 retval = 1;
4139 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4140 buf->b_flags |= BF_NEW_W;
4141#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4142 can_reload = TRUE;
4143#endif
4144 }
4145
4146 if (mesg != NULL)
4147 {
4148 path = home_replace_save(buf, buf->b_fname);
4149 if (path != NULL)
4150 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004151 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004153 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004154 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004155#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004156 // Set warningmsg here, before the unimportant and output-specific
4157 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004158 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4161 if (can_reload)
4162 {
4163 if (*mesg2 != NUL)
4164 {
4165 STRCAT(tbuf, "\n");
4166 STRCAT(tbuf, mesg2);
4167 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004168 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4169 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004170 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 reload = TRUE;
4172 }
4173 else
4174#endif
4175 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4176 {
4177 if (*mesg2 != NUL)
4178 {
4179 STRCAT(tbuf, "; ");
4180 STRCAT(tbuf, mesg2);
4181 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 retval = 2;
4184 }
4185 else
4186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004190 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004192 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 msg_clr_eos();
4194 (void)msg_end();
4195 if (emsg_silent == 0)
4196 {
4197 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004198#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004200#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004201 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004202 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004204 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 redraw_cmdline = FALSE;
4206 }
4207 }
4208 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210
4211 vim_free(path);
4212 vim_free(tbuf);
4213 }
4214 }
4215
4216 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004218 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004219 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004220#ifdef FEAT_PERSISTENT_UNDO
4221 if (buf->b_p_udf && buf->b_ffname != NULL)
4222 {
4223 char_u hash[UNDO_HASH_SIZE];
4224 buf_T *save_curbuf = curbuf;
4225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004226 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004227 curbuf = buf;
4228 u_compute_hash(hash);
4229 u_write_undo(NULL, FALSE, buf, hash);
4230 curbuf = save_curbuf;
4231 }
4232#endif
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004235 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004236 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004237 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4238 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004240 // restore this in case an autocommand has set it; it would break
4241 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 need_mouse_correct = save_mouse_correct;
4243#endif
4244
4245 return retval;
4246}
4247
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004248/*
4249 * Reload a buffer that is already loaded.
4250 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004251 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4252 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004253 */
4254 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004255buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004256{
4257 exarg_T ea;
4258 pos_T old_cursor;
4259 linenr_T old_topline;
4260 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004261 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004262 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004263 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004264 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004265 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004267 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004268 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004270 // We only want to read the text from the file, not reset the syntax
4271 // highlighting, clear marks, diff status, etc. Force the fileformat
4272 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004273 if (prep_exarg(&ea, buf) == OK)
4274 {
4275 old_cursor = curwin->w_cursor;
4276 old_topline = curwin->w_topline;
4277
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004278 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004280 // Save all the text, so that the reload can be undone.
4281 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004282 u_sync(FALSE);
4283 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4284 flags |= READ_KEEP_UNDO;
4285 }
4286
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004287 /*
4288 * To behave like when a new file is edited (matters for
4289 * BufReadPost autocommands) we first need to delete the current
4290 * buffer contents. But if reading the file fails we should keep
4291 * the old contents. Can't use memory only, the file might be
4292 * too big. Use a hidden buffer to move the buffer contents to.
4293 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004294 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004295 savebuf = NULL;
4296 else
4297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004298 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004299 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004300 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004301 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004304 curbuf = savebuf;
4305 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004306 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004307 curbuf = buf;
4308 curwin->w_buffer = buf;
4309 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004310 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004311 || move_lines(buf, savebuf) == FAIL)
4312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004313 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004314 buf->b_fname);
4315 saved = FAIL;
4316 }
4317 }
4318
4319 if (saved == OK)
4320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4322 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004323 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4324 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004325 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004326 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004327#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004328 if (!aborting())
4329#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004330 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004331 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004333 // Put the text back from the save buffer. First
4334 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004335 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004336 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004337 break;
4338 (void)move_lines(savebuf, buf);
4339 }
4340 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004341 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004343 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004344 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004345 if ((flags & READ_KEEP_UNDO) == 0)
4346 {
4347 u_blockfree(buf);
4348 u_clearall(buf);
4349 }
4350 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004353 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004354 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004355 }
4356 }
4357 vim_free(ea.cmd);
4358
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004359 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004360 wipe_buffer(savebuf, FALSE);
4361
4362#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004364 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365#endif
4366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 // Restore the topline and cursor position and check it (lines may
4368 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004369 if (old_topline > curbuf->b_ml.ml_line_count)
4370 curwin->w_topline = curbuf->b_ml.ml_line_count;
4371 else
4372 curwin->w_topline = old_topline;
4373 curwin->w_cursor = old_cursor;
4374 check_cursor();
4375 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004376 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004377#ifdef FEAT_FOLDING
4378 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004379 win_T *wp;
4380 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004382 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004383 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004384 if (wp->w_buffer == curwin->w_buffer
4385 && !foldmethodIsManual(wp))
4386 foldUpdateAll(wp);
4387 }
4388#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004389 // If the mode didn't change and 'readonly' was set, keep the old
4390 // value; the user probably used the ":view" command. But don't
4391 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004392 if (orig_mode == curbuf->b_orig_mode)
4393 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004395 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004396 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397 }
4398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004402}
4403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004405buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406{
4407 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004408 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409#ifdef HAVE_ST_MODE
4410 buf->b_orig_mode = (int)st->st_mode;
4411#else
4412 buf->b_orig_mode = mch_getperm(fname);
4413#endif
4414}
4415
4416/*
4417 * Adjust the line with missing eol, used for the next write.
4418 * Used for do_filter(), when the input lines for the filter are deleted.
4419 */
4420 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004421write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004424 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425}
4426
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004427// Subfuncions for readdirex()
4428#ifdef FEAT_EVAL
4429# ifdef MSWIN
4430 static char_u *
4431getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4432{
4433 stat_T st;
4434 unsigned short st_mode;
4435 DWORD flag = wfd->dwFileAttributes;
4436 WCHAR *wp;
4437
4438 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4439 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4440 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4441 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4442
4443 wp = wcsrchr(wfd->cFileName, L'.');
4444 if (wp != NULL)
4445 {
4446 if (_wcsicmp(wp, L".exe") == 0 ||
4447 _wcsicmp(wp, L".com") == 0 ||
4448 _wcsicmp(wp, L".cmd") == 0 ||
4449 _wcsicmp(wp, L".bat") == 0)
4450 st_mode |= _S_IEXEC;
4451 }
4452
4453 // Copy user bits to group/other.
4454 st_mode |= (st_mode & 0700) >> 3;
4455 st_mode |= (st_mode & 0700) >> 6;
4456
4457 st.st_mode = st_mode;
4458 return getfpermst(&st, perm);
4459}
4460
4461 static char_u *
4462getftypewfd(WIN32_FIND_DATAW *wfd)
4463{
4464 DWORD flag = wfd->dwFileAttributes;
4465 DWORD tag = wfd->dwReserved0;
4466
4467 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4468 {
4469 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4470 return (char_u*)"junction";
4471 else if (tag == IO_REPARSE_TAG_SYMLINK)
4472 {
4473 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4474 return (char_u*)"linkd";
4475 else
4476 return (char_u*)"link";
4477 }
4478 return (char_u*)"reparse"; // unknown reparse point type
4479 }
4480 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4481 return (char_u*)"dir";
4482 else
4483 return (char_u*)"file";
4484}
4485
4486 static dict_T *
4487create_readdirex_item(WIN32_FIND_DATAW *wfd)
4488{
4489 dict_T *item;
4490 char_u *p;
4491 varnumber_T size, time;
4492 char_u permbuf[] = "---------";
4493
4494 item = dict_alloc();
4495 if (item == NULL)
4496 return NULL;
4497 item->dv_refcount++;
4498
4499 p = utf16_to_enc(wfd->cFileName, NULL);
4500 if (p == NULL)
4501 goto theend;
4502 if (dict_add_string(item, "name", p) == FAIL)
4503 {
4504 vim_free(p);
4505 goto theend;
4506 }
4507 vim_free(p);
4508
4509 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4510 if (dict_add_number(item, "size", size) == FAIL)
4511 goto theend;
4512
4513 // Convert FILETIME to unix time.
4514 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4515 wfd->ftLastWriteTime.dwLowDateTime)
4516 - 116444736000000000) / 10000000;
4517 if (dict_add_number(item, "time", time) == FAIL)
4518 goto theend;
4519
4520 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4521 goto theend;
4522 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4523 goto theend;
4524
4525 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4526 goto theend;
4527 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4528 goto theend;
4529
4530 return item;
4531
4532theend:
4533 dict_unref(item);
4534 return NULL;
4535}
4536# else
4537 static dict_T *
4538create_readdirex_item(char_u *path, char_u *name)
4539{
4540 dict_T *item;
4541 char *p;
4542 size_t len;
4543 stat_T st;
4544 int ret, link = FALSE;
4545 varnumber_T size;
4546 char_u permbuf[] = "---------";
4547 char_u *q;
4548 struct passwd *pw;
4549 struct group *gr;
4550
4551 item = dict_alloc();
4552 if (item == NULL)
4553 return NULL;
4554 item->dv_refcount++;
4555
4556 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4557 p = alloc(len);
4558 if (p == NULL)
4559 goto theend;
4560 vim_snprintf(p, len, "%s/%s", path, name);
4561 ret = mch_lstat(p, &st);
4562 if (ret >= 0 && S_ISLNK(st.st_mode))
4563 {
4564 link = TRUE;
4565 ret = mch_stat(p, &st);
4566 }
4567 vim_free(p);
4568
4569 if (dict_add_string(item, "name", name) == FAIL)
4570 goto theend;
4571
4572 if (ret >= 0)
4573 {
4574 size = (varnumber_T)st.st_size;
4575 if (S_ISDIR(st.st_mode))
4576 size = 0;
4577 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004578 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004579 size = -2;
4580 if (dict_add_number(item, "size", size) == FAIL)
4581 goto theend;
4582 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4583 goto theend;
4584
4585 if (link)
4586 {
4587 if (S_ISDIR(st.st_mode))
4588 q = (char_u*)"linkd";
4589 else
4590 q = (char_u*)"link";
4591 }
4592 else
4593 q = getftypest(&st);
4594 if (dict_add_string(item, "type", q) == FAIL)
4595 goto theend;
4596 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4597 goto theend;
4598
4599 pw = getpwuid(st.st_uid);
4600 if (pw == NULL)
4601 q = (char_u*)"";
4602 else
4603 q = (char_u*)pw->pw_name;
4604 if (dict_add_string(item, "user", q) == FAIL)
4605 goto theend;
4606 gr = getgrgid(st.st_gid);
4607 if (gr == NULL)
4608 q = (char_u*)"";
4609 else
4610 q = (char_u*)gr->gr_name;
4611 if (dict_add_string(item, "group", q) == FAIL)
4612 goto theend;
4613 }
4614 else
4615 {
4616 if (dict_add_number(item, "size", -1) == FAIL)
4617 goto theend;
4618 if (dict_add_number(item, "time", -1) == FAIL)
4619 goto theend;
4620 if (dict_add_string(item, "type", (char_u*)"") == FAIL)
4621 goto theend;
4622 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4623 goto theend;
4624 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4625 goto theend;
4626 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4627 goto theend;
4628 }
4629 return item;
4630
4631theend:
4632 dict_unref(item);
4633 return NULL;
4634}
4635# endif
4636
4637 static int
4638compare_readdirex_item(const void *p1, const void *p2)
4639{
4640 char_u *name1, *name2;
4641
4642 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4643 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
4644 return STRCMP(name1, name2);
4645}
4646#endif
4647
Bram Moolenaarda440d22016-01-16 21:27:23 +01004648#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4649/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004650 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004651 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004652 * If "withattr" is TRUE, retrieve the names and their attributes.
4653 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004654 * Return OK for success, FAIL for failure.
4655 */
4656 int
4657readdir_core(
4658 garray_T *gap,
4659 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004660 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004661 void *context,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004662 int (*checkitem)(void *context, void *item))
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004663{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004664 int failed = FALSE;
4665 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004666# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004667 char_u *buf;
4668 int ok;
4669 HANDLE hFind = INVALID_HANDLE_VALUE;
4670 WIN32_FIND_DATAW wfd;
4671 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004672# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004673 DIR *dirp;
4674 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004675# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004676
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004677 ga_init2(gap, (int)sizeof(void *), 20);
4678
4679# ifdef FEAT_EVAL
4680# define FREE_ITEM(item) do { \
4681 if (withattr) \
4682 dict_unref((dict_T*)item); \
4683 else \
4684 vim_free(item); \
4685 } while (0)
4686# else
4687# define FREE_ITEM(item) vim_free(item)
4688# endif
4689
4690# ifdef MSWIN
4691 buf = alloc(MAXPATHL);
4692 if (buf == NULL)
4693 return FAIL;
4694 STRNCPY(buf, path, MAXPATHL-5);
4695 p = buf + STRLEN(buf);
4696 MB_PTR_BACK(buf, p);
4697 if (*p == '\\' || *p == '/')
4698 *p = NUL;
4699 STRCAT(p, "\\*");
4700
4701 wn = enc_to_utf16(buf, NULL);
4702 if (wn != NULL)
4703 hFind = FindFirstFileW(wn, &wfd);
4704 ok = (hFind != INVALID_HANDLE_VALUE);
4705 if (!ok)
4706 {
4707 failed = TRUE;
4708 smsg(_(e_notopen), path);
4709 }
4710 else
4711 {
4712 while (ok)
4713 {
4714 int ignore;
4715 void *item;
4716 WCHAR *wp;
4717
4718 wp = wfd.cFileName;
4719 ignore = wp[0] == L'.' &&
4720 (wp[1] == NUL ||
4721 (wp[1] == L'.' && wp[2] == NUL));
4722# ifdef FEAT_EVAL
4723 if (withattr)
4724 item = (void*)create_readdirex_item(&wfd);
4725 else
4726# endif
4727 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4728 if (item == NULL)
4729 {
4730 failed = TRUE;
4731 break;
4732 }
4733
4734 if (!ignore && checkitem != NULL)
4735 {
4736 int r = checkitem(context, item);
4737
4738 if (r < 0)
4739 {
4740 FREE_ITEM(item);
4741 break;
4742 }
4743 if (r == 0)
4744 ignore = TRUE;
4745 }
4746
4747 if (!ignore)
4748 {
4749 if (ga_grow(gap, 1) == OK)
4750 ((void**)gap->ga_data)[gap->ga_len++] = item;
4751 else
4752 {
4753 failed = TRUE;
4754 FREE_ITEM(item);
4755 break;
4756 }
4757 }
4758 else
4759 FREE_ITEM(item);
4760
4761 ok = FindNextFileW(hFind, &wfd);
4762 }
4763 FindClose(hFind);
4764 }
4765
4766 vim_free(buf);
4767 vim_free(wn);
4768# else // MSWIN
4769 dirp = opendir((char *)path);
4770 if (dirp == NULL)
4771 {
4772 failed = TRUE;
4773 smsg(_(e_notopen), path);
4774 }
4775 else
4776 {
4777 for (;;)
4778 {
4779 int ignore;
4780 void *item;
4781
4782 dp = readdir(dirp);
4783 if (dp == NULL)
4784 break;
4785 p = (char_u *)dp->d_name;
4786
4787 ignore = p[0] == '.' &&
4788 (p[1] == NUL ||
4789 (p[1] == '.' && p[2] == NUL));
4790# ifdef FEAT_EVAL
4791 if (withattr)
4792 item = (void*)create_readdirex_item(path, p);
4793 else
4794# endif
4795 item = (void*)vim_strsave(p);
4796 if (item == NULL)
4797 {
4798 failed = TRUE;
4799 break;
4800 }
4801
4802 if (!ignore && checkitem != NULL)
4803 {
4804 int r = checkitem(context, item);
4805
4806 if (r < 0)
4807 {
4808 FREE_ITEM(item);
4809 break;
4810 }
4811 if (r == 0)
4812 ignore = TRUE;
4813 }
4814
4815 if (!ignore)
4816 {
4817 if (ga_grow(gap, 1) == OK)
4818 ((void**)gap->ga_data)[gap->ga_len++] = item;
4819 else
4820 {
4821 failed = TRUE;
4822 FREE_ITEM(item);
4823 break;
4824 }
4825 }
4826 else
4827 FREE_ITEM(item);
4828 }
4829
4830 closedir(dirp);
4831 }
4832# endif // MSWIN
4833
4834# undef FREE_ITEM
4835
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004836 if (!failed && gap->ga_len > 0)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004837 {
4838# ifdef FEAT_EVAL
4839 if (withattr)
4840 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4841 compare_readdirex_item);
4842 else
4843# endif
4844 sort_strings((char_u **)gap->ga_data, gap->ga_len);
4845 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004846
4847 return failed ? FAIL : OK;
4848}
4849
4850/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004851 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004852 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004853 */
4854 int
4855delete_recursive(char_u *name)
4856{
4857 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004858 int i;
4859 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004860 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004861
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004862 // A symbolic link to a directory itself is deleted, not the directory it
4863 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004864 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004865# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004866 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004867# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004868 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004869# endif
4870 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004871 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004872 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004873 if (exp == NULL)
4874 return -1;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004875 if (readdir_core(&ga, exp, FALSE, NULL, NULL) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004876 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004877 for (i = 0; i < ga.ga_len; ++i)
4878 {
4879 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4880 ((char_u **)ga.ga_data)[i]);
4881 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004882 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004883 }
4884 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004885 }
4886 else
4887 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004888 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004889 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004890 }
4891 else
4892 result = mch_remove(name) == 0 ? 0 : -1;
4893
4894 return result;
4895}
4896#endif
4897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004899static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004901# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4902/*
4903 * Open temporary directory and take file lock to prevent
4904 * to be auto-cleaned.
4905 */
4906 static void
4907vim_opentempdir(void)
4908{
4909 DIR *dp = NULL;
4910
4911 if (vim_tempdir_dp != NULL)
4912 return;
4913
4914 dp = opendir((const char*)vim_tempdir);
4915
4916 if (dp != NULL)
4917 {
4918 vim_tempdir_dp = dp;
4919 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4920 }
4921}
4922
4923/*
4924 * Close temporary directory - it automatically release file lock.
4925 */
4926 static void
4927vim_closetempdir(void)
4928{
4929 if (vim_tempdir_dp != NULL)
4930 {
4931 closedir(vim_tempdir_dp);
4932 vim_tempdir_dp = NULL;
4933 }
4934}
4935# endif
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937/*
4938 * Delete the temp directory and all files it contains.
4939 */
4940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004941vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (vim_tempdir != NULL)
4944 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004945# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4946 vim_closetempdir();
4947# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004948 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004949 gettail(vim_tempdir)[-1] = NUL;
4950 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004951 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004956 * Directory "tempdir" was created. Expand this name to a full path and put
4957 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
4958 * "tempdir" must be no longer than MAXPATHL.
4959 */
4960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004962{
4963 char_u *buf;
4964
Bram Moolenaar964b3742019-05-24 18:54:09 +02004965 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004966 if (buf != NULL)
4967 {
4968 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
4969 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004970 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004971 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004972# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4973 vim_opentempdir();
4974# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004975 vim_free(buf);
4976 }
4977}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00004978#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004979
4980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 * vim_tempname(): Return a unique name that can be used for a temp file.
4982 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02004983 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
4984 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 *
4986 * The returned pointer is to allocated memory.
4987 * The returned pointer is NULL if no valid name was found.
4988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004990vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004991 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004992 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
4994#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004995 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01004996#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004997 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#else
4999 char_u itmp[TEMPNAMELEN];
5000#endif
5001
5002#ifdef TEMPDIRNAMES
5003 static char *(tempdirs[]) = {TEMPDIRNAMES};
5004 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005006 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007# endif
5008
5009 /*
5010 * This will create a directory for private use by this instance of Vim.
5011 * This is done once, and the same directory is used for all temp files.
5012 * This method avoids security problems because of symlink attacks et al.
5013 * It's also a bit faster, because we only need to check for an existing
5014 * file when creating the directory and not for each temp file.
5015 */
5016 if (vim_tempdir == NULL)
5017 {
5018 /*
5019 * Try the entries in TEMPDIRNAMES to create the temp directory.
5020 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005021 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005023# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005024 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005025 long nr;
5026 long off;
5027# endif
5028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005029 // Expand $TMP, leave room for "/v1100000/999999999".
5030 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005032 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005034 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005035 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
Bram Moolenaareaf03392009-11-17 11:08:52 +00005037# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005038 {
5039# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 // Make sure the umask doesn't remove the executable bit.
5041 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005042 mode_t umask_save = umask(077);
5043# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005044 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005045 STRCAT(itmp, "vXXXXXX");
5046 if (mkdtemp((char *)itmp) != NULL)
5047 vim_settempdir(itmp);
5048# if defined(UNIX) || defined(VMS)
5049 (void)umask(umask_save);
5050# endif
5051 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005052# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005053 // Get an arbitrary number of up to 6 digits. When it's
5054 // unlikely that it already exists it will be faster,
5055 // otherwise it doesn't matter. The use of mkdir() avoids any
5056 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005058 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 // Try up to 10000 different values until we find a name that
5061 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 for (off = 0; off < 10000L; ++off)
5063 {
5064 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005065# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
Bram Moolenaareaf03392009-11-17 11:08:52 +00005069 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5070# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 // If mkdir() does not set errno to EEXIST, check for
5072 // existing file here. There is a race condition then,
5073 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (mch_stat((char *)itmp, &st) >= 0)
5075 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005076# endif
5077# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 // Make sure the umask doesn't remove the executable bit.
5079 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005083# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (r == 0)
5087 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005088 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005091# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005092 // If the mkdir() didn't fail because the file/dir exists,
5093 // we probably can't create any dir here, try another
5094 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005099# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (vim_tempdir != NULL)
5101 break;
5102 }
5103 }
5104 }
5105
5106 if (vim_tempdir != NULL)
5107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005108 // There is no need to check if the file exists, because we own the
5109 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5111 return vim_strsave(itmp);
5112 }
5113
5114 return NULL;
5115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005116#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117
Bram Moolenaar4f974752019-02-17 17:44:42 +01005118# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005119 WCHAR wszTempFile[_MAX_PATH + 1];
5120 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 char_u *retval;
5122 char_u *p;
5123
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005124 wcscpy(itmp, L"");
5125 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005126 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005127 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
5128 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005129 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005130 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005132 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005134 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005135 // GetTempFileName() will create the file, we don't want that
5136 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005138 // Backslashes in a temp file name cause problems when filtering with
5139 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5140 // didn't set 'shellslash'.
5141 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (*p_shcf == '-' || p_ssl)
5143 for (p = retval; *p; ++p)
5144 if (*p == '\\')
5145 *p = '/';
5146 return retval;
5147
Bram Moolenaar4f974752019-02-17 17:44:42 +01005148# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005151 char_u *p;
5152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005153 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005154 p = tmpnam((char *)itmp);
5155 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 return NULL;
5157# else
5158 char_u *p;
5159
5160# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005161 // mktemp() is not working on VMS. It seems to be
5162 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 sprintf((char *)itmp, "VIM%c", extra_char);
5164 p = (char_u *)tempnam("tmp:", (char *)itmp);
5165 if (p != NULL)
5166 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005167 // VMS will use '.LIS' if we don't explicitly specify an extension,
5168 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 STRCPY(itmp, p);
5170 STRCAT(itmp, ".txt");
5171 free(p);
5172 }
5173 else
5174 return NULL;
5175# else
5176 STRCPY(itmp, TEMPNAME);
5177 if ((p = vim_strchr(itmp, '?')) != NULL)
5178 *p = extra_char;
5179 if (mktemp((char *)itmp) == NULL)
5180 return NULL;
5181# endif
5182# endif
5183
5184 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005185# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005186#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187}
5188
5189#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5190/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005191 * Convert all backslashes in fname to forward slashes in-place, unless when
5192 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 */
5194 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005195forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 char_u *p;
5198
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005199 if (path_with_url(fname))
5200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005202 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005203 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005205 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 *p = '/';
5207}
5208#endif
5209
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005210/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005211 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5212 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5213 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 * Used for autocommands and 'wildignore'.
5215 * Returns TRUE if there is a match, FALSE otherwise.
5216 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005217 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005218match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005219 char_u *pattern, // pattern to match with
5220 regprog_T **prog, // pre-compiled regprog or NULL
5221 char_u *fname, // full path of file name
5222 char_u *sfname, // short file name or NULL
5223 char_u *tail, // tail of path
5224 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 regmatch_T regmatch;
5227 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005230 if (prog != NULL)
5231 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005233 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
5235 /*
5236 * Try for a match with the pattern with:
5237 * 1. the full file name, when the pattern has a '/'.
5238 * 2. the short file name, when the pattern has a '/'.
5239 * 3. the tail of the file name, when the pattern has no '/'.
5240 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005241 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 && ((allow_dirs
5243 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5244 || (sfname != NULL
5245 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005246 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 result = TRUE;
5248
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005249 if (prog != NULL)
5250 *prog = regmatch.regprog;
5251 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005252 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 return result;
5254}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256#if defined(FEAT_WILDIGN) || defined(PROTO)
5257/*
5258 * Return TRUE if a file matches with a pattern in "list".
5259 * "list" is a comma-separated list of patterns, like 'wildignore'.
5260 * "sfname" is the short file name or NULL, "ffname" the long file name.
5261 */
5262 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u buf[100];
5266 char_u *tail;
5267 char_u *regpat;
5268 char allow_dirs;
5269 int match;
5270 char_u *p;
5271
5272 tail = gettail(sfname);
5273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005274 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 p = list;
5276 while (*p)
5277 {
5278 copy_option_part(&p, buf, 100, ",");
5279 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5280 if (regpat == NULL)
5281 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005282 match = match_file_pat(regpat, NULL, ffname, sfname,
5283 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 vim_free(regpat);
5285 if (match)
5286 return TRUE;
5287 }
5288 return FALSE;
5289}
5290#endif
5291
5292/*
5293 * Convert the given pattern "pat" which has shell style wildcards in it, into
5294 * a regular expression, and return the result in allocated memory. If there
5295 * is a directory path separator to be matched, then TRUE is put in
5296 * allow_dirs, otherwise FALSE is put there -- webb.
5297 * Handle backslashes before special characters, like "\*" and "\ ".
5298 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * Returns NULL when out of memory.
5300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302file_pat_to_reg_pat(
5303 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304 char_u *pat_end, // first char after pattern or NULL
5305 char *allow_dirs, // Result passed back out in here
5306 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005308 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 char_u *endp;
5310 char_u *reg_pat;
5311 char_u *p;
5312 int i;
5313 int nested = 0;
5314 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 if (allow_dirs != NULL)
5317 *allow_dirs = FALSE;
5318 if (pat_end == NULL)
5319 pat_end = pat + STRLEN(pat);
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 for (p = pat; p < pat_end; p++)
5322 {
5323 switch (*p)
5324 {
5325 case '*':
5326 case '.':
5327 case ',':
5328 case '{':
5329 case '}':
5330 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005331 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 break;
5333#ifdef BACKSLASH_IN_FILENAME
5334 case '\\':
5335 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 break;
5338#endif
5339 default:
5340 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005341 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 ++p;
5344 ++size;
5345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 break;
5347 }
5348 }
5349 reg_pat = alloc(size + 1);
5350 if (reg_pat == NULL)
5351 return NULL;
5352
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 if (pat[0] == '*')
5356 while (pat[0] == '*' && pat < pat_end - 1)
5357 pat++;
5358 else
5359 reg_pat[i++] = '^';
5360 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005361 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 while (endp - pat > 0 && *endp == '*')
5364 endp--;
5365 add_dollar = FALSE;
5366 }
5367 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5368 {
5369 switch (*p)
5370 {
5371 case '*':
5372 reg_pat[i++] = '.';
5373 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005375 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 break;
5377 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 case '~':
5379 reg_pat[i++] = '\\';
5380 reg_pat[i++] = *p;
5381 break;
5382 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 reg_pat[i++] = '.';
5384 break;
5385 case '\\':
5386 if (p[1] == NUL)
5387 break;
5388#ifdef BACKSLASH_IN_FILENAME
5389 if (!no_bslash)
5390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005391 // translate:
5392 // "\x" to "\\x" e.g., "dir\file"
5393 // "\*" to "\\.*" e.g., "dir\*.c"
5394 // "\?" to "\\." e.g., "dir\??.c"
5395 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5397 && p[1] != '+')
5398 {
5399 reg_pat[i++] = '[';
5400 reg_pat[i++] = '\\';
5401 reg_pat[i++] = '/';
5402 reg_pat[i++] = ']';
5403 if (allow_dirs != NULL)
5404 *allow_dirs = TRUE;
5405 break;
5406 }
5407 }
5408#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005409 // Undo escaping from ExpandEscape():
5410 // foo\?bar -> foo?bar
5411 // foo\%bar -> foo%bar
5412 // foo\,bar -> foo,bar
5413 // foo\ bar -> foo bar
5414 // Don't unescape \, * and others that are also special in a
5415 // regexp.
5416 // An escaped { must be unescaped since we use magic not
5417 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 if (*++p == '?'
5419#ifdef BACKSLASH_IN_FILENAME
5420 && no_bslash
5421#endif
5422 )
5423 reg_pat[i++] = '?';
5424 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005425 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005426 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005427 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005428 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5429 {
5430 reg_pat[i++] = '\\';
5431 reg_pat[i++] = '{';
5432 p += 2;
5433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 else
5435 {
5436 if (allow_dirs != NULL && vim_ispathsep(*p)
5437#ifdef BACKSLASH_IN_FILENAME
5438 && (!no_bslash || *p != '\\')
5439#endif
5440 )
5441 *allow_dirs = TRUE;
5442 reg_pat[i++] = '\\';
5443 reg_pat[i++] = *p;
5444 }
5445 break;
5446#ifdef BACKSLASH_IN_FILENAME
5447 case '/':
5448 reg_pat[i++] = '[';
5449 reg_pat[i++] = '\\';
5450 reg_pat[i++] = '/';
5451 reg_pat[i++] = ']';
5452 if (allow_dirs != NULL)
5453 *allow_dirs = TRUE;
5454 break;
5455#endif
5456 case '{':
5457 reg_pat[i++] = '\\';
5458 reg_pat[i++] = '(';
5459 nested++;
5460 break;
5461 case '}':
5462 reg_pat[i++] = '\\';
5463 reg_pat[i++] = ')';
5464 --nested;
5465 break;
5466 case ',':
5467 if (nested)
5468 {
5469 reg_pat[i++] = '\\';
5470 reg_pat[i++] = '|';
5471 }
5472 else
5473 reg_pat[i++] = ',';
5474 break;
5475 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005476 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005478 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 *allow_dirs = TRUE;
5480 reg_pat[i++] = *p;
5481 break;
5482 }
5483 }
5484 if (add_dollar)
5485 reg_pat[i++] = '$';
5486 reg_pat[i] = NUL;
5487 if (nested != 0)
5488 {
5489 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005490 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005492 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005493 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 return reg_pat;
5496}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005497
5498#if defined(EINTR) || defined(PROTO)
5499/*
5500 * Version of read() that retries when interrupted by EINTR (possibly
5501 * by a SIGWINCH).
5502 */
5503 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005504read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005505{
5506 long ret;
5507
5508 for (;;)
5509 {
5510 ret = vim_read(fd, buf, bufsize);
5511 if (ret >= 0 || errno != EINTR)
5512 break;
5513 }
5514 return ret;
5515}
5516
5517/*
5518 * Version of write() that retries when interrupted by EINTR (possibly
5519 * by a SIGWINCH).
5520 */
5521 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005522write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005523{
5524 long ret = 0;
5525 long wlen;
5526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005527 // Repeat the write() so long it didn't fail, other than being interrupted
5528 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005529 while (ret < (long)bufsize)
5530 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005531 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005532 if (wlen < 0)
5533 {
5534 if (errno != EINTR)
5535 break;
5536 }
5537 else
5538 ret += wlen;
5539 }
5540 return ret;
5541}
5542#endif