blob: b3b7100d641b3dce1926ff5ee2ee7643d7026752 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaare3f915d2020-07-14 23:02:44 +020016#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020019#if defined(UNIX) && defined(FEAT_EVAL)
20# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar217e1b82019-12-01 21:41:28 +010024// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000025#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar9d8bfae2020-09-02 21:21:35 +020027#if defined(__hpux) && !defined(HAVE_DIRFD)
28# define dirfd(x) ((x)->__dd_fd)
29# define HAVE_DIRFD
30#endif
31
Bram Moolenaarf077db22019-08-13 00:18:24 +020032static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010033#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020037static 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 +000038#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010039static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010040static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000041static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000042
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020043#ifdef FEAT_EVAL
44static int readdirex_sort;
45#endif
46
Bram Moolenaar473952e2019-09-28 16:30:04 +020047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010048filemess(
49 buf_T *buf,
50 char_u *name,
51 char_u *s,
52 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
54 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020055 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57 if (msg_silent != 0)
58 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010059 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020060
Bram Moolenaar217e1b82019-12-01 21:41:28 +010061 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020062 if (STRLEN(IObuff) > IOSIZE - 100)
63 IObuff[IOSIZE - 100] = NUL;
64
65 // Avoid an over-long translation to cause trouble.
66 STRNCAT(IObuff, s, 99);
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 /*
69 * For the first message may have to start a new line.
70 * For further ones overwrite the previous one, reset msg_scroll before
71 * calling filemess().
72 */
73 msg_scroll_save = msg_scroll;
74 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
75 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010076 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 check_for_delay(FALSE);
78 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020079 if (prev_msg_col != 0 && msg_col == 0)
80 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 msg_scroll = msg_scroll_save;
82 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010083 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
85 msg_clr_eos();
86 out_flush();
87 msg_scrolled_ign = FALSE;
88}
89
90/*
91 * Read lines from file "fname" into the buffer after line "from".
92 *
93 * 1. We allocate blocks with lalloc, as big as possible.
94 * 2. Each block is filled with characters from the file with a single read().
95 * 3. The lines are inserted in the buffer with ml_append().
96 *
97 * (caller must check that fname != NULL, unless READ_STDIN is used)
98 *
99 * "lines_to_skip" is the number of lines that must be skipped
100 * "lines_to_read" is the number of lines that are appended
101 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
102 *
103 * flags:
104 * READ_NEW starting to edit a new buffer
105 * READ_FILTER reading filter output
106 * READ_STDIN read from stdin instead of a file
107 * READ_BUFFER read from curbuf instead of a file (converting after reading
108 * stdin)
109 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200110 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200111 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100113 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
115 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100116readfile(
117 char_u *fname,
118 char_u *sfname,
119 linenr_T from,
120 linenr_T lines_to_skip,
121 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100122 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100123 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 int fd = 0;
126 int newfile = (flags & READ_NEW);
127 int check_readonly;
128 int filtering = (flags & READ_FILTER);
129 int read_stdin = (flags & READ_STDIN);
130 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200131 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000132 int set_options = newfile || read_buffer
133 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100134 linenr_T read_buf_lnum = 1; // next line to read from curbuf
135 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 char_u c;
137 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100138 char_u *ptr = NULL; // pointer into read buffer
139 char_u *buffer = NULL; // read buffer
140 char_u *new_buffer = NULL; // init to shut up gcc
141 char_u *line_start = NULL; // init to shut up gcc
142 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 colnr_T len;
144 long size = 0;
145 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200146 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 int skip_read = FALSE;
148#ifdef FEAT_CRYPT
149 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200150 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200152#ifdef FEAT_PERSISTENT_UNDO
153 context_sha256_T sha_ctx;
154 int read_undo_file = FALSE;
155#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100156 int split = 0; // number of split lines
157#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100159 int error = FALSE; // errors encountered
160 int ff_error = EOL_UNKNOWN; // file format with errors
161 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef UNIX
163 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100164 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#else
166 int perm;
167#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100168 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200170 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 int file_readonly;
172 linenr_T skip_count = 0;
173 linenr_T read_count = 0;
174 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100175 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
176 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100177 int try_mac;
178 int try_dos;
179 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100182 linenr_T conv_error = 0; // line nr with conversion error
183 linenr_T illegal_byte = 0; // line nr with illegal byte
184 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
185 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000186 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100187 // BAD_KEEP, BAD_DROP or character to
188 // replace with
189 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100191 char_u *fenc; // fileencoding to use
192 int fenc_alloced; // fenc_next is in allocated memory
193 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 int advance_fenc = FALSE;
195 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100196#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100197 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100198# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100199 int did_iconv = FALSE; // TRUE when iconv() failed and trying
200 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100202#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100203 int converted = FALSE; // TRUE if conversion done
204 int notconverted = FALSE; // TRUE if conversion wanted but it
205 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100207 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100208 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200209 buf_T *old_curbuf;
210 char_u *old_b_ffname;
211 char_u *old_b_fname;
212 int using_b_ffname;
213 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200214 static char *msg_is_a_directory = N_("is a directory");
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200215
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100216 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200217
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100218 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220 /*
221 * If there is no file name yet, use the one for the read file.
222 * BF_NOTEDITED is set to reflect this.
223 * Don't do this for a read from a filter.
224 * Only do this when 'cpoptions' contains the 'f' flag.
225 */
226 if (curbuf->b_ffname == NULL
227 && !filtering
228 && fname != NULL
229 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
230 && !(flags & READ_DUMMY))
231 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000232 if (set_rw_fname(fname, sfname) == FAIL)
233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 }
235
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100236 // Remember the initial values of curbuf, curbuf->b_ffname and
237 // curbuf->b_fname to detect whether they are altered as a result of
238 // executing nasty autocommands. Also check if "fname" and "sfname"
239 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200240 old_curbuf = curbuf;
241 old_b_ffname = curbuf->b_ffname;
242 old_b_fname = curbuf->b_fname;
243 using_b_ffname = (fname == curbuf->b_ffname)
244 || (sfname == curbuf->b_ffname);
245 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200246
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100247 // After reading a file the cursor line changes but we don't want to
248 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000249 ex_no_reprint = TRUE;
250
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100251 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000252 need_fileinfo = FALSE;
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 /*
255 * For Unix: Use the short file name whenever possible.
256 * Avoids problems with networks and when directory names are changed.
257 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
258 * another directory, which we don't detect.
259 */
260 if (sfname == NULL)
261 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200262#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 fname = sfname;
264#endif
265
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 /*
267 * The BufReadCmd and FileReadCmd events intercept the reading process by
268 * executing the associated commands instead.
269 */
270 if (!filtering && !read_stdin && !read_buffer)
271 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100272 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100274 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
276 curbuf->b_op_start.col = 0;
277
278 if (newfile)
279 {
280 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
281 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200282 {
283 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200285 if (aborting())
286 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200288 // The BufReadCmd code usually uses ":read" to get the text and
289 // perhaps ":file" to change the buffer name. But we should
290 // consider this to work like ":edit", thus reset the
291 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
292 // same file.
293 if (status == OK)
294 curbuf->b_flags &= ~BF_NOTEDITED;
295 return status;
296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 }
298 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
299 FALSE, NULL, eap))
300#ifdef FEAT_EVAL
301 return aborting() ? FAIL : OK;
302#else
303 return OK;
304#endif
305
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100306 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308
309 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100310 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100312 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000314 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200316 size_t namelen = STRLEN(fname);
317
318 // If the name is too long we might crash further on, quit here.
319 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000320 {
321 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
322 msg_end();
323 msg_scroll = msg_save;
324 return FAIL;
325 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200326
327 // If the name ends in a path separator, we can't open it. Check here,
328 // because reading the file may actually work, but then creating the
329 // swap file may destroy it! Reported on MS-DOS and Win 95.
330 if (after_pathsep(fname, fname + namelen))
331 {
332 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
333 msg_end();
334 msg_scroll = msg_save;
335 return FAIL;
336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 }
338
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200339 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200341#ifdef UNIX
342 /*
343 * On Unix it is possible to read a directory, so we have to
344 * check for it before the mch_open().
345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100347 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
348 && !S_ISFIFO(perm) // ... or fifo
349 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000350# ifdef OPEN_CHR_FILES
351 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100352 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 )
355 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100356 int retval = FAIL;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100359 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200360 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100361 retval = NOTDONE;
362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 else
364 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
365 msg_end();
366 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100367 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200369#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100370#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000371 /*
372 * MS-Windows allows opening a device, but we will probably get stuck
373 * trying to read it.
374 */
375 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
376 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000377 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000378 msg_end();
379 msg_scroll = msg_save;
380 return FAIL;
381 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000382#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200383 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000384
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200386 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
388 /*
389 * When opening a new file we take the readonly flag from the file.
390 * Default is r/w, can be set to r/o below.
391 * Don't reset it when in readonly mode
392 * Only set/reset b_p_ro when BF_CHECK_RO is set.
393 */
394 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000395 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 curbuf->b_p_ro = FALSE;
397
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200398 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100400 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 if (mch_stat((char *)fname, &st) >= 0)
402 {
403 buf_store_time(curbuf, &st, fname);
404 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#ifdef UNIX
406 /*
407 * Use the protection bits of the original file for the swap file.
408 * This makes it possible for others to read the name of the
409 * edited file from the swapfile, but only if they can read the
410 * edited file.
411 * Remove the "write" and "execute" bits for group and others
412 * (they must not write the swapfile).
413 * Add the "read" and "write" bits for the user, otherwise we may
414 * not be able to write to the file ourselves.
415 * Setting the bits is done below, after creating the swap file.
416 */
417 swap_mode = (st.st_mode & 0644) | 0600;
418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef VMS
420 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000421 curbuf->b_fab_rat = st.st_fab_rat;
422 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#endif
424 }
425 else
426 {
427 curbuf->b_mtime = 0;
428 curbuf->b_mtime_read = 0;
429 curbuf->b_orig_size = 0;
430 curbuf->b_orig_mode = 0;
431 }
432
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100433 // Reset the "new file" flag. It will be set again below when the
434 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
436 }
437
438/*
439 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100440 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 */
442 file_readonly = FALSE;
443 if (read_stdin)
444 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100445#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100446 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 setmode(0, O_BINARY);
448#endif
449 }
450 else if (!read_buffer)
451 {
452#ifdef USE_MCH_ACCESS
453 if (
454# ifdef UNIX
455 !(perm & 0222) ||
456# endif
457 mch_access((char *)fname, W_OK))
458 file_readonly = TRUE;
459 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
460#else
461 if (!newfile
462 || readonlymode
463 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
464 {
465 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100466 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
468 }
469#endif
470 }
471
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {
474#ifndef UNIX
475 int isdir_f;
476#endif
477 msg_scroll = msg_save;
478#ifndef UNIX
479 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100480 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 */
482 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (isdir_f)
485 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200486 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100487 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 }
489 else
490#endif
491 if (newfile)
492 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200493 if (perm < 0
494#ifdef ENOENT
495 && errno == ENOENT
496#endif
497 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 {
499 /*
500 * Set the 'new-file' flag, so that when the file has
501 * been created by someone else, a ":w" will complain.
502 */
503 curbuf->b_flags |= BF_NEW;
504
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 // Create a swap file now, so that other Vims are warned
506 // that we are editing this file. Don't do this for a
507 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508#ifdef FEAT_QUICKFIX
509 if (!bt_dontwrite(curbuf))
510#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100513 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000514 if (curbuf != old_curbuf
515 || (using_b_ffname
516 && (old_b_ffname != curbuf->b_ffname))
517 || (using_b_fname
518 && (old_b_fname != curbuf->b_fname)))
519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100520 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000521 return FAIL;
522 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000523 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000524 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200525 filemess(curbuf, sfname,
526 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000527 else
528 filemess(curbuf, sfname,
529 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 // Even though this is a new file, it might have been
532 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 check_marks_read();
534#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200536 if (eap != NULL)
537 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
539 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 save_file_ff(curbuf);
542
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100543#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 return FAIL;
546#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549 else
550 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000551 filemess(curbuf, sfname, (char_u *)(
552# ifdef EFBIG
553 (errno == EFBIG) ? _("[File too big]") :
554# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200555# ifdef EOVERFLOW
556 (errno == EOVERFLOW) ? _("[File too big]") :
557# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000558 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100559 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
561 }
562
563 return FAIL;
564 }
565
566 /*
567 * Only set the 'ro' flag for readonly files the first time they are
568 * loaded. Help files always get readonly mode
569 */
570 if ((check_readonly && file_readonly) || curbuf->b_help)
571 curbuf->b_p_ro = TRUE;
572
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000573 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100575 // Don't change 'eol' if reading from buffer as it will already be
576 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000577 if (!read_buffer)
578 {
579 curbuf->b_p_eol = TRUE;
580 curbuf->b_start_eol = TRUE;
581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000583 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100586 // Create a swap file now, so that other Vims are warned that we are
587 // editing this file.
588 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589#ifdef FEAT_QUICKFIX
590 if (!bt_dontwrite(curbuf))
591#endif
592 {
593 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000594 if (!read_stdin && (curbuf != old_curbuf
595 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
596 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100598 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000599 if (!read_buffer)
600 close(fd);
601 return FAIL;
602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100604 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000605 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
606 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100607 {
608 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
609
610 /*
611 * If the group-read bit is set but not the world-read bit, then
612 * the group must be equal to the group of the original file. If
613 * we can't make that happen then reset the group-read bit. This
614 * avoids making the swap file readable to more users when the
615 * primary group of the user is too permissive.
616 */
617 if ((swap_mode & 044) == 040)
618 {
619 stat_T swap_st;
620
621 if (mch_stat((char *)swap_fname, &swap_st) >= 0
622 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200623# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100624 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200625 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200626# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200627 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100628 swap_mode &= 0600;
629 }
630
631 (void)mch_setperm(swap_fname, (long)swap_mode);
632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633#endif
634 }
635
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200636 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (swap_exists_action == SEA_QUIT)
638 {
639 if (!read_buffer && !read_stdin)
640 close(fd);
641 return FAIL;
642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100644 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 /*
647 * Set '[ mark to the line above where the lines go (line 1 if zero).
648 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100649 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
651 curbuf->b_op_start.col = 0;
652
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100653 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
654 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
655 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (!read_buffer)
658 {
659 int m = msg_scroll;
660 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 /*
663 * The file must be closed again, the autocommands may want to change
664 * the file before reading it.
665 */
666 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 /*
670 * The output from the autocommands should not overwrite anything and
671 * should not be overwritten: Set msg_scroll, restore its value if no
672 * output was done.
673 */
674 msg_scroll = TRUE;
675 if (filtering)
676 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
677 FALSE, curbuf, eap);
678 else if (read_stdin)
679 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
680 FALSE, curbuf, eap);
681 else if (newfile)
682 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
683 FALSE, curbuf, eap);
684 else
685 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
686 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100687 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100688 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
689 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
690 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100691 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (msg_scrolled == n)
694 msg_scroll = m;
695
696#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 --no_wait_return;
700 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100701 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 return FAIL;
703 }
704#endif
705 /*
706 * Don't allow the autocommands to change the current buffer.
707 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000708 *
709 * Don't allow the autocommands to change the buffer name either
710 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 */
712 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000713 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
714 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
716 {
717 --no_wait_return;
718 msg_scroll = msg_save;
719 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100720 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100722 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100723 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return FAIL;
725 }
726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100728 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
730
731 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
732 {
733 /*
734 * Show the user that we are busy reading the input. Sometimes this
735 * may take a while. When reading from stdin another program may
736 * still be running, don't move the cursor to the last line, unless
737 * always using the GUI.
738 */
739 if (read_stdin)
740 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100741 if (!is_not_a_term())
742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200744# ifdef VIMDLL
745 if (!gui.in_use)
746# endif
747 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748#endif
749#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100750 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100751 if (gui.in_use && !gui.dying && !gui.starting)
752 {
753 p = (char_u *)_("Reading from stdin...");
754 gui_write(p, (int)STRLEN(p));
755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759 else if (!read_buffer)
760 filemess(curbuf, sfname, (char_u *)"", 0);
761 }
762
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100763 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 /*
766 * Set linecnt now, before the "retry" caused by a wrong guess for
767 * fileformat, and after the autocommands, which may change them.
768 */
769 linecnt = curbuf->b_ml.ml_line_count;
770
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100771 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000772 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000773 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000774 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000775 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000776 curbuf->b_bad_char = eap->bad_char;
777 }
778 else
779 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000782 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
784 if (eap != NULL && eap->force_enc != 0)
785 {
786 fenc = enc_canonize(eap->cmd + eap->force_enc);
787 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000788 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790 else if (curbuf->b_p_bin)
791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 fenc_alloced = FALSE;
794 }
795 else if (curbuf->b_help)
796 {
797 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000798 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // Help files are either utf-8 or latin1. Try utf-8 first, if this
801 // fails it must be latin1.
802 // Always do this when 'encoding' is "utf-8". Otherwise only do
803 // this when needed to avoid [converted] remarks all the time.
804 // It is needed when the first line contains non-ASCII characters.
805 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 fenc = (char_u *)"latin1";
807 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000808 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000810 fc = fname[STRLEN(fname) - 1];
811 if (TOLOWER_ASC(fc) == 'x')
812 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Read the first line (and a bit more). Immediately rewind to
814 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100815 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200816 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000817 for (p = firstline; p < firstline + len; ++p)
818 if (*p >= 0x80)
819 {
820 c = TRUE;
821 break;
822 }
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825
826 if (c)
827 {
828 fenc_next = fenc;
829 fenc = (char_u *)"utf-8";
830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // When the file is utf-8 but a character doesn't fit in
832 // 'encoding' don't retry. In help text editing utf-8 bytes
833 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000834 if (!enc_utf8)
835 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 fenc_alloced = FALSE;
838 }
839 else if (*p_fencs == NUL)
840 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 fenc_alloced = FALSE;
843 }
844 else
845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200847 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 /*
851 * Jump back here to retry reading the file in different ways.
852 * Reasons to retry:
853 * - encoding conversion failed: try another one from "fenc_next"
854 * - BOM detected and fenc was set, need to setup conversion
855 * - "fileformat" check failed: try another
856 *
857 * Variables set for special retry actions:
858 * "file_rewind" Rewind the file to start reading it again.
859 * "advance_fenc" Advance "fenc" using "fenc_next".
860 * "skip_read" Re-use already read bytes (BOM detected).
861 * "did_iconv" iconv() conversion failed, try 'charconvert'.
862 * "keep_fileformat" Don't reset "fileformat".
863 *
864 * Other status indicators:
865 * "tmpname" When != NULL did conversion with 'charconvert'.
866 * Output file has to be deleted afterwards.
867 * "iconv_fd" When != -1 did conversion with iconv().
868 */
869retry:
870
871 if (file_rewind)
872 {
873 if (read_buffer)
874 {
875 read_buf_lnum = 1;
876 read_buf_col = 0;
877 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200878 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100880 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 error = TRUE;
882 goto failed;
883 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200886 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000888 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000891 curbuf->b_start_bomb = FALSE;
892 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000893 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895
896 /*
897 * When retrying with another "fenc" and the first time "fileformat"
898 * will be reset.
899 */
900 if (keep_fileformat)
901 keep_fileformat = FALSE;
902 else
903 {
904 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000907 try_unix = try_dos = try_mac = FALSE;
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100910 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100912 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100914 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916
Bram Moolenaar13505972019-01-24 15:04:48 +0100917#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (iconv_fd != (iconv_t)-1)
919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100920 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 iconv_close(iconv_fd);
922 iconv_fd = (iconv_t)-1;
923 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
926 if (advance_fenc)
927 {
928 /*
929 * Try the next entry in 'fileencodings'.
930 */
931 advance_fenc = FALSE;
932
933 if (eap != NULL && eap->force_enc != 0)
934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100935 // Conversion given with "++cc=" wasn't possible, read
936 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000938 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (fenc_alloced)
940 vim_free(fenc);
941 fenc = (char_u *)"";
942 fenc_alloced = FALSE;
943 }
944 else
945 {
946 if (fenc_alloced)
947 vim_free(fenc);
948 if (fenc_next != NULL)
949 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200950 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952 else
953 {
954 fenc = (char_u *)"";
955 fenc_alloced = FALSE;
956 }
957 }
958 if (tmpname != NULL)
959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100960 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100961 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963 }
964
965 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000966 * Conversion may be required when the encoding of the file is different
967 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 */
969 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000970 converted = need_conversion(fenc);
971 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 {
973
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 // "ucs-bom" means we need to check the first bytes of the file
975 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (STRCMP(fenc, ENC_UCSBOM) == 0)
977 fio_flags = FIO_UCSBOM;
978
979 /*
980 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
981 * done. This is handled below after read(). Prepare the
982 * fio_flags to avoid having to parse the string each time.
983 * Also check for Unicode to Latin1 conversion, because iconv()
984 * appears not to handle this correctly. This works just like
985 * conversion to UTF-8 except how the resulting character is put in
986 * the buffer.
987 */
988 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
989 fio_flags = get_fio_flags(fenc);
990
Bram Moolenaar4f974752019-02-17 17:44:42 +0100991#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 /*
993 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
994 * is handled with MultiByteToWideChar().
995 */
996 if (fio_flags == 0)
997 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar13505972019-01-24 15:04:48 +01001000#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001001 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (fio_flags == 0)
1003 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar13505972019-01-24 15:04:48 +01001006#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 /*
1008 * Try using iconv() if we can't convert internally.
1009 */
1010 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001011# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 )
1015 iconv_fd = (iconv_t)my_iconv_open(
1016 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
Bram Moolenaar13505972019-01-24 15:04:48 +01001019#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 /*
1021 * Use the 'charconvert' expression when conversion is required
1022 * and we can't do it internally or with iconv().
1023 */
1024 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001025 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001026# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 )
1030 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001031# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001033# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001034 // Skip conversion when it's already done (retry for wrong
1035 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (tmpname == NULL)
1037 {
1038 tmpname = readfile_charconvert(fname, fenc, &fd);
1039 if (tmpname == NULL)
1040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 advance_fenc = TRUE;
1043 if (fd < 0)
1044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001045 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001046 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 error = TRUE;
1048 goto failed;
1049 }
1050 goto retry;
1051 }
1052 }
1053 }
1054 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001058#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 )
1062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Conversion wanted but we can't.
1064 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 advance_fenc = TRUE;
1066 goto retry;
1067 }
1068 }
1069 }
1070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // Set "can_retry" when it's possible to rewind the file and try with
1072 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1073 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001074 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 if (!skip_read)
1077 {
1078 linerest = 0;
1079 filesize = 0;
1080 skip_count = lines_to_skip;
1081 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001083#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001084 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1085 && curbuf->b_ffname != NULL
1086 && curbuf->b_p_udf
1087 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001088 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001089 && !read_stdin
1090 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001091 if (read_undo_file)
1092 sha256_start(&sha_ctx);
1093#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001094#ifdef FEAT_CRYPT
1095 if (curbuf->b_cryptstate != NULL)
1096 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001097 // Need to free the state, but keep the key, don't want to ask for
1098 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001099 crypt_free_state(curbuf->b_cryptstate);
1100 curbuf->b_cryptstate = NULL;
1101 }
1102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
1104
1105 while (!error && !got_int)
1106 {
1107 /*
1108 * We allocate as much space for the file as we can get, plus
1109 * space for the old line plus room for one terminating NUL.
1110 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001111 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001113 if (!skip_read)
1114 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001115#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001116 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001117#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001118 // Use buffer >= 64K. Add linerest to double the size if the
1119 // line gets very long, to avoid a lot of copying. But don't
1120 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001121 size = 0x10000L + linerest;
1122 if (size > 0x100000L)
1123 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001124#endif
1125 }
1126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001127 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001128 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
1130 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001131 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 size = 1;
1133 }
1134 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
1136 if (!skip_read)
1137 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001138 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001140 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 FALSE)) != NULL)
1142 break;
1143 }
1144 if (new_buffer == NULL)
1145 {
1146 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1147 error = TRUE;
1148 break;
1149 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1152 vim_free(buffer);
1153 buffer = new_buffer;
1154 ptr = buffer + linerest;
1155 line_start = buffer;
1156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001157 // May need room to translate into.
1158 // For iconv() we don't really know the required space, use a
1159 // factor ICONV_MULT.
1160 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1161 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1162 // become up to 4 bytes, size must be multiple of 2
1163 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1164 // multiple of 2
1165 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1166 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001167 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001168#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (iconv_fd != (iconv_t)-1)
1170 size = size / ICONV_MULT;
1171 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (fio_flags & FIO_LATIN1)
1174 size = size / 2;
1175 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1176 size = (size * 2 / 3) & ~1;
1177 else if (fio_flags & FIO_UCS4)
1178 size = (size * 2 / 3) & ~3;
1179 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001180 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001181#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001183 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001184#endif
1185#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001187 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#endif
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (conv_restlen > 0)
1191 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001192 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 mch_memmove(ptr, conv_rest, conv_restlen);
1194 ptr += conv_restlen;
1195 size -= conv_restlen;
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198 if (read_buffer)
1199 {
1200 /*
1201 * Read bytes from curbuf. Used for converting text read
1202 * from stdin.
1203 */
1204 if (read_buf_lnum > from)
1205 size = 0;
1206 else
1207 {
1208 int n, ni;
1209 long tlen;
1210
1211 tlen = 0;
1212 for (;;)
1213 {
1214 p = ml_get(read_buf_lnum) + read_buf_col;
1215 n = (int)STRLEN(p);
1216 if ((int)tlen + n + 1 > size)
1217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 // Filled up to "size", append partial line.
1219 // Change NL to NUL to reverse the effect done
1220 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001221 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 for (ni = 0; ni < n; ++ni)
1223 {
1224 if (p[ni] == NL)
1225 ptr[tlen++] = NUL;
1226 else
1227 ptr[tlen++] = p[ni];
1228 }
1229 read_buf_col += n;
1230 break;
1231 }
1232 else
1233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001234 // Append whole line and new-line. Change NL
1235 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 for (ni = 0; ni < n; ++ni)
1237 {
1238 if (p[ni] == NL)
1239 ptr[tlen++] = NUL;
1240 else
1241 ptr[tlen++] = p[ni];
1242 }
1243 ptr[tlen++] = NL;
1244 read_buf_col = 0;
1245 if (++read_buf_lnum > from)
1246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001247 // When the last line didn't have an
1248 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (!curbuf->b_p_eol)
1250 --tlen;
1251 size = tlen;
1252 break;
1253 }
1254 }
1255 }
1256 }
1257 }
1258 else
1259 {
1260 /*
1261 * Read bytes from the file.
1262 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001263 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266#ifdef FEAT_CRYPT
1267 /*
1268 * At start of file: Check for magic number of encryption.
1269 */
1270 if (filesize == 0 && size > 0)
1271 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1272 &filesize, newfile, sfname,
1273 &did_ask_for_key);
1274 /*
1275 * Decrypt the read bytes. This is done before checking for
1276 * EOF because the crypt layer may be buffering.
1277 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001278 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1279 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001280 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001281# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001282 if (crypt_works_inplace(curbuf->b_cryptstate))
1283 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001284# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001285 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001286# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001287 }
1288 else
1289 {
1290 char_u *newptr = NULL;
1291 int decrypted_size;
1292
1293 decrypted_size = crypt_decode_alloc(
1294 curbuf->b_cryptstate, ptr, size, &newptr);
1295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001296 // If the crypt layer is buffering, not producing
1297 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001298 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001299 continue;
1300
1301 if (linerest == 0)
1302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001303 // Simple case: reuse returned buffer (may be
1304 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001305 new_buffer = newptr;
1306 }
1307 else
1308 {
1309 long_u new_size;
1310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001311 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001312 new_size = (long_u)(decrypted_size + linerest + 1);
1313 new_buffer = lalloc(new_size, FALSE);
1314 if (new_buffer == NULL)
1315 {
1316 do_outofmem_msg(new_size);
1317 error = TRUE;
1318 break;
1319 }
1320
1321 mch_memmove(new_buffer, buffer, linerest);
1322 if (newptr != NULL)
1323 mch_memmove(new_buffer + linerest, newptr,
1324 decrypted_size);
1325 }
1326
1327 if (new_buffer != NULL)
1328 {
1329 vim_free(buffer);
1330 buffer = new_buffer;
1331 new_buffer = NULL;
1332 line_start = buffer;
1333 ptr = buffer + linerest;
1334 }
1335 size = decrypted_size;
1336 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001337# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001338 }
1339#endif
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (size <= 0)
1342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001343 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001346 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001347 /*
1348 * Reached end-of-file but some trailing bytes could
1349 * not be converted. Truncated file?
1350 */
1351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001352 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001353 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001354#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001355 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001356#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001357 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001358 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001359 if (can_retry)
1360 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001361 if (conv_error == 0)
1362 conv_error = curbuf->b_ml.ml_line_count
1363 - linecnt + 1;
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001366 else if (illegal_byte == 0)
1367 illegal_byte = curbuf->b_ml.ml_line_count
1368 - linecnt + 1;
1369 if (bad_char_behavior == BAD_DROP)
1370 {
1371 *(ptr - conv_restlen) = NUL;
1372 conv_restlen = 0;
1373 }
1374 else
1375 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001376 // Replace the trailing bytes with the replacement
1377 // character if we were converting; if we weren't,
1378 // leave the UTF8 checking code to do it, as it
1379 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001380 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001381#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001382 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001383#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001384 ))
1385 {
1386 while (conv_restlen > 0)
1387 {
1388 *(--ptr) = bad_char_behavior;
1389 --conv_restlen;
1390 }
1391 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001393#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001394 if (iconv_fd != (iconv_t)-1)
1395 {
1396 iconv_close(iconv_fd);
1397 iconv_fd = (iconv_t)-1;
1398 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001399#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001400 }
1401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 skip_read = FALSE;
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * At start of file (or after crypt magic number): Check for BOM.
1408 * Also check for a BOM for other Unicode encodings, but not after
1409 * converting with 'charconvert' or when a BOM has already been
1410 * found.
1411 */
1412 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001413#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001414 || (cryptkey != NULL
1415 && filesize == crypt_get_header_len(
1416 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 )
1419 && (fio_flags == FIO_UCSBOM
1420 || (!curbuf->b_p_bomb
1421 && tmpname == NULL
1422 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1423 {
1424 char_u *ccname;
1425 int blen;
1426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001427 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (size < 2 || curbuf->b_p_bin)
1429 ccname = NULL;
1430 else
1431 ccname = check_for_bom(ptr, size, &blen,
1432 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1433 if (ccname != NULL)
1434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001435 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 filesize += blen;
1437 size -= blen;
1438 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001439 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001442 curbuf->b_start_bomb = TRUE;
1443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445
1446 if (fio_flags == FIO_UCSBOM)
1447 {
1448 if (ccname == NULL)
1449 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001450 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 advance_fenc = TRUE;
1452 }
1453 else
1454 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001455 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (fenc_alloced)
1457 vim_free(fenc);
1458 fenc = ccname;
1459 fenc_alloced = FALSE;
1460 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001461 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 skip_read = TRUE;
1463 goto retry;
1464 }
1465 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001468 ptr -= conv_restlen;
1469 size += conv_restlen;
1470 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 /*
1472 * Break here for a read error or end-of-file.
1473 */
1474 if (size <= 0)
1475 break;
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar13505972019-01-24 15:04:48 +01001478#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (iconv_fd != (iconv_t)-1)
1480 {
1481 /*
1482 * Attempt conversion of the read bytes to 'encoding' using
1483 * iconv().
1484 */
1485 const char *fromp;
1486 char *top;
1487 size_t from_size;
1488 size_t to_size;
1489
1490 fromp = (char *)ptr;
1491 from_size = size;
1492 ptr += size;
1493 top = (char *)ptr;
1494 to_size = real_size - size;
1495
1496 /*
1497 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001498 * another conversion. Except for when there is no
1499 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001501 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1502 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1504 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001505 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001506 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001507 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001508 if (conv_error == 0)
1509 conv_error = readfile_linenr(linecnt,
1510 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001511
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001513 ++fromp;
1514 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001515 if (bad_char_behavior == BAD_KEEP)
1516 {
1517 *top++ = *(fromp - 1);
1518 --to_size;
1519 }
1520 else if (bad_char_behavior != BAD_DROP)
1521 {
1522 *top++ = bad_char_behavior;
1523 --to_size;
1524 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 if (from_size > 0)
1528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001529 // Some remaining characters, keep them for the next
1530 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1532 conv_restlen = (int)from_size;
1533 }
1534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001535 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 line_start = ptr - linerest;
1537 mch_memmove(line_start, buffer, (size_t)linerest);
1538 size = (long)((char_u *)top - ptr);
1539 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar4f974752019-02-17 17:44:42 +01001542#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if (fio_flags & FIO_CODEPAGE)
1544 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001545 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001546 WCHAR ucs2buf[3];
1547 int ucs2len;
1548 int codepage = FIO_GET_CP(fio_flags);
1549 int bytelen;
1550 int found_bad;
1551 char replstr[2];
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 /*
1554 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001555 * a codepage, using standard MS-Windows functions. This
1556 * requires two steps:
1557 * 1. convert from 'fileencoding' to ucs-2
1558 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001560 * Because there may be illegal bytes AND an incomplete byte
1561 * sequence at the end, we may have to do the conversion one
1562 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001565 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001566 if (bad_char_behavior > 0)
1567 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001569 replstr[0] = '?';
1570 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001573 * Move the bytes to the end of the buffer, so that we have
1574 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001576 src = ptr + real_size - size;
1577 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001579 /*
1580 * Do the conversion.
1581 */
1582 dst = ptr;
1583 size = size;
1584 while (size > 0)
1585 {
1586 found_bad = FALSE;
1587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001588# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001589 if (codepage == CP_UTF8)
1590 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001591 // Handle CP_UTF8 input ourselves to be able to handle
1592 // trailing bytes properly.
1593 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001594 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001595 if (bytelen > size)
1596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // Only got some bytes of a character. Normally
1598 // it's put in "conv_rest", but if it's too long
1599 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001600 if (bytelen <= CONV_RESTLEN)
1601 break;
1602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001603 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001604 bytelen = size;
1605 found_bad = TRUE;
1606 }
1607 else
1608 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001609 int u8c = utf_ptr2char(src);
1610
Bram Moolenaar86e01082005-12-29 22:45:34 +00001611 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001612 found_bad = TRUE;
1613 ucs2buf[0] = u8c;
1614 ucs2len = 1;
1615 }
1616 }
1617 else
1618# endif
1619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001620 // We don't know how long the byte sequence is, try
1621 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001622 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1623 ++bytelen)
1624 {
1625 ucs2len = MultiByteToWideChar(codepage,
1626 MB_ERR_INVALID_CHARS,
1627 (LPCSTR)src, bytelen,
1628 ucs2buf, 3);
1629 if (ucs2len > 0)
1630 break;
1631 }
1632 if (ucs2len == 0)
1633 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001634 // If we have only one byte then it's probably an
1635 // incomplete byte sequence. Otherwise discard
1636 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001637 if (size == 1)
1638 break;
1639 found_bad = TRUE;
1640 bytelen = 1;
1641 }
1642 }
1643
1644 if (!found_bad)
1645 {
1646 int i;
1647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001648 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001649 if (enc_utf8)
1650 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001651 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001652 for (i = 0; i < ucs2len; ++i)
1653 dst += utf_char2bytes(ucs2buf[i], dst);
1654 }
1655 else
1656 {
1657 BOOL bad = FALSE;
1658 int dstlen;
1659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001660 // From UCS-2 to "enc_codepage". If the
1661 // conversion uses the default character "?",
1662 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001663 dstlen = WideCharToMultiByte(enc_codepage, 0,
1664 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001665 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001666 replstr, &bad);
1667 if (bad)
1668 found_bad = TRUE;
1669 else
1670 dst += dstlen;
1671 }
1672 }
1673
1674 if (found_bad)
1675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001677 if (can_retry)
1678 goto rewind_retry;
1679 if (conv_error == 0)
1680 conv_error = readfile_linenr(linecnt, ptr, dst);
1681 if (bad_char_behavior != BAD_DROP)
1682 {
1683 if (bad_char_behavior == BAD_KEEP)
1684 {
1685 mch_memmove(dst, src, bytelen);
1686 dst += bytelen;
1687 }
1688 else
1689 *dst++ = bad_char_behavior;
1690 }
1691 }
1692
1693 src += bytelen;
1694 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001696
1697 if (size > 0)
1698 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001699 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001700 mch_memmove(conv_rest, src, size);
1701 conv_restlen = size;
1702 }
1703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001704 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001705 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
1707 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001708#endif
1709#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (fio_flags & FIO_MACROMAN)
1711 {
1712 /*
1713 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001714 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001716 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (fio_flags != 0)
1722 {
1723 int u8c;
1724 char_u *dest;
1725 char_u *tail = NULL;
1726
1727 /*
1728 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1729 * "enc_utf8" not set: Convert Unicode to Latin1.
1730 * Go from end to start through the buffer, because the number
1731 * of bytes may increase.
1732 * "dest" points to after where the UTF-8 bytes go, "p" points
1733 * to after the next character to convert.
1734 */
1735 dest = ptr + real_size;
1736 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1737 {
1738 p = ptr + size;
1739 if (fio_flags == FIO_UTF8)
1740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001741 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 tail = ptr + size - 1;
1743 while (tail > ptr && (*tail & 0xc0) == 0x80)
1744 --tail;
1745 if (tail + utf_byte2len(*tail) <= ptr + size)
1746 tail = NULL;
1747 else
1748 p = tail;
1749 }
1750 }
1751 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1752 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 p = ptr + (size & ~1);
1755 if (size & 1)
1756 tail = p;
1757 if ((fio_flags & FIO_UTF16) && p > ptr)
1758 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (fio_flags & FIO_ENDIAN_L)
1761 {
1762 u8c = (*--p << 8);
1763 u8c += *--p;
1764 }
1765 else
1766 {
1767 u8c = *--p;
1768 u8c += (*--p << 8);
1769 }
1770 if (u8c >= 0xd800 && u8c <= 0xdbff)
1771 tail = p;
1772 else
1773 p += 2;
1774 }
1775 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001776 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001778 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 p = ptr + (size & ~3);
1780 if (size & 3)
1781 tail = p;
1782 }
1783
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001784 // If there is a trailing incomplete sequence move it to
1785 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 if (tail != NULL)
1787 {
1788 conv_restlen = (int)((ptr + size) - tail);
1789 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1790 size -= conv_restlen;
1791 }
1792
1793
1794 while (p > ptr)
1795 {
1796 if (fio_flags & FIO_LATIN1)
1797 u8c = *--p;
1798 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1799 {
1800 if (fio_flags & FIO_ENDIAN_L)
1801 {
1802 u8c = (*--p << 8);
1803 u8c += *--p;
1804 }
1805 else
1806 {
1807 u8c = *--p;
1808 u8c += (*--p << 8);
1809 }
1810 if ((fio_flags & FIO_UTF16)
1811 && u8c >= 0xdc00 && u8c <= 0xdfff)
1812 {
1813 int u16c;
1814
1815 if (p == ptr)
1816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001817 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (can_retry)
1819 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001820 if (conv_error == 0)
1821 conv_error = readfile_linenr(linecnt,
1822 ptr, p);
1823 if (bad_char_behavior == BAD_DROP)
1824 continue;
1825 if (bad_char_behavior != BAD_KEEP)
1826 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001829 // found second word of double-word, get the first
1830 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (fio_flags & FIO_ENDIAN_L)
1832 {
1833 u16c = (*--p << 8);
1834 u16c += *--p;
1835 }
1836 else
1837 {
1838 u16c = *--p;
1839 u16c += (*--p << 8);
1840 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001841 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1842 + (u8c & 0x3ff);
1843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001844 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (u16c < 0xd800 || u16c > 0xdbff)
1846 {
1847 if (can_retry)
1848 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001849 if (conv_error == 0)
1850 conv_error = readfile_linenr(linecnt,
1851 ptr, p);
1852 if (bad_char_behavior == BAD_DROP)
1853 continue;
1854 if (bad_char_behavior != BAD_KEEP)
1855 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 }
1859 else if (fio_flags & FIO_UCS4)
1860 {
1861 if (fio_flags & FIO_ENDIAN_L)
1862 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001863 u8c = (unsigned)*--p << 24;
1864 u8c += (unsigned)*--p << 16;
1865 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 u8c += *--p;
1867 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001871 u8c += (unsigned)*--p << 8;
1872 u8c += (unsigned)*--p << 16;
1873 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001876 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 if (*--p < 0x80)
1879 u8c = *p;
1880 else
1881 {
1882 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001883 p -= len;
1884 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (len == 0)
1886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001887 // Not a valid UTF-8 character, retry with
1888 // another fenc when possible, otherwise just
1889 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (can_retry)
1891 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001892 if (conv_error == 0)
1893 conv_error = readfile_linenr(linecnt,
1894 ptr, p);
1895 if (bad_char_behavior == BAD_DROP)
1896 continue;
1897 if (bad_char_behavior != BAD_KEEP)
1898 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001902 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
1904 dest -= utf_char2len(u8c);
1905 (void)utf_char2bytes(u8c, dest);
1906 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001907 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 --dest;
1910 if (u8c >= 0x100)
1911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 // character doesn't fit in latin1, retry with
1913 // another fenc when possible, otherwise just
1914 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001915 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001917 if (conv_error == 0)
1918 conv_error = readfile_linenr(linecnt, ptr, p);
1919 if (bad_char_behavior == BAD_DROP)
1920 ++dest;
1921 else if (bad_char_behavior == BAD_KEEP)
1922 *dest = u8c;
1923 else if (eap != NULL && eap->bad_char != 0)
1924 *dest = bad_char_behavior;
1925 else
1926 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928 else
1929 *dest = u8c;
1930 }
1931 }
1932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001933 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 line_start = dest - linerest;
1935 mch_memmove(line_start, buffer, (size_t)linerest);
1936 size = (long)((ptr + real_size) - dest);
1937 ptr = dest;
1938 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001939 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001941 int incomplete_tail = FALSE;
1942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001944 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001946 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001947 int l;
1948
1949 if (todo <= 0)
1950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (*p >= 0x80)
1952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 // A length of 1 means it's an illegal byte. Accept
1954 // an incomplete character at the end though, the next
1955 // read() will get the next bytes, we'll check it
1956 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001957 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001958 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001960 // Avoid retrying with a different encoding when
1961 // a truncated file is more likely, or attempting
1962 // to read the rest of an incomplete sequence when
1963 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001964 if (p > ptr || filesize > 0)
1965 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // Incomplete byte sequence, move it to conv_rest[]
1967 // and try to read the rest of it, unless we've
1968 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001969 if (p > ptr)
1970 {
1971 conv_restlen = todo;
1972 mch_memmove(conv_rest, p, conv_restlen);
1973 size -= conv_restlen;
1974 break;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001977 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001979 // Illegal byte. If we can try another encoding
1980 // do that, unless at EOF where a truncated
1981 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001982 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001983 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001984#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001986 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1987 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001988#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001989 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001990 if (conv_error == 0 && illegal_byte == 0)
1991 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001994 if (bad_char_behavior == BAD_DROP)
1995 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001996 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001997 --p;
1998 --size;
1999 }
2000 else if (bad_char_behavior != BAD_KEEP)
2001 *p = bad_char_behavior;
2002 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002003 else
2004 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002007 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002012#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002013 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002015 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002017#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002019 advance_fenc = TRUE;
2020 file_rewind = TRUE;
2021 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002025 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 filesize += size;
2027
2028 /*
2029 * when reading the first part of a file: guess EOL type
2030 */
2031 if (fileformat == EOL_UNKNOWN)
2032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (try_dos || try_unix)
2035 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002036 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002037 if (try_mac)
2038 try_mac = 1;
2039
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 for (p = ptr; p < ptr + size; ++p)
2041 {
2042 if (*p == NL)
2043 {
2044 if (!try_unix
2045 || (try_dos && p > ptr && p[-1] == CAR))
2046 fileformat = EOL_DOS;
2047 else
2048 fileformat = EOL_UNIX;
2049 break;
2050 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002051 else if (*p == CAR && try_mac)
2052 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002055 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (fileformat == EOL_UNIX && try_mac)
2057 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 try_mac = 1;
2060 try_unix = 1;
2061 for (; p >= ptr && *p != CAR; p--)
2062 ;
2063 if (p >= ptr)
2064 {
2065 for (p = ptr; p < ptr + size; ++p)
2066 {
2067 if (*p == NL)
2068 try_unix++;
2069 else if (*p == CAR)
2070 try_mac++;
2071 }
2072 if (try_mac > try_unix)
2073 fileformat = EOL_MAC;
2074 }
2075 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002076 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002077 // Looking for CR but found no end-of-line markers at
2078 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002079 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (fileformat == EOL_UNKNOWN && try_mac)
2084 fileformat = EOL_MAC;
2085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (fileformat == EOL_UNKNOWN)
2088 fileformat = default_fileformat();
2089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002090 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002091 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 set_fileformat(fileformat, OPT_LOCAL);
2093 }
2094 }
2095
2096 /*
2097 * This loop is executed once for every character read.
2098 * Keep it fast!
2099 */
2100 if (fileformat == EOL_MAC)
2101 {
2102 --ptr;
2103 while (++ptr, --size >= 0)
2104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if ((c = *ptr) != NUL && c != CAR && c != NL)
2107 continue;
2108 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002111 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 else
2113 {
2114 if (skip_count == 0)
2115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 len = (colnr_T) (ptr - line_start + 1);
2118 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2119 {
2120 error = TRUE;
2121 break;
2122 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002123#ifdef FEAT_PERSISTENT_UNDO
2124 if (read_undo_file)
2125 sha256_update(&sha_ctx, line_start, len);
2126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 ++lnum;
2128 if (--read_count == 0)
2129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002130 error = TRUE; // break loop
2131 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 break;
2133 }
2134 }
2135 else
2136 --skip_count;
2137 line_start = ptr + 1;
2138 }
2139 }
2140 }
2141 else
2142 {
2143 --ptr;
2144 while (++ptr, --size >= 0)
2145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 continue;
2148 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 else
2151 {
2152 if (skip_count == 0)
2153 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002154 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 len = (colnr_T)(ptr - line_start + 1);
2156 if (fileformat == EOL_DOS)
2157 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002158 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 ptr[-1] = NUL;
2162 --len;
2163 }
2164 /*
2165 * Reading in Dos format, but no CR-LF found!
2166 * When 'fileformats' includes "unix", delete all
2167 * the lines read so far and start all over again.
2168 * Otherwise give an error message later.
2169 */
2170 else if (ff_error != EOL_DOS)
2171 {
2172 if ( try_unix
2173 && !read_stdin
2174 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002175 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2176 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 set_fileformat(EOL_UNIX, OPT_LOCAL);
2181 file_rewind = TRUE;
2182 keep_fileformat = TRUE;
2183 goto retry;
2184 }
2185 ff_error = EOL_DOS;
2186 }
2187 }
2188 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2189 {
2190 error = TRUE;
2191 break;
2192 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002193#ifdef FEAT_PERSISTENT_UNDO
2194 if (read_undo_file)
2195 sha256_update(&sha_ctx, line_start, len);
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 ++lnum;
2198 if (--read_count == 0)
2199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 error = TRUE; // break loop
2201 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 break;
2203 }
2204 }
2205 else
2206 --skip_count;
2207 line_start = ptr + 1;
2208 }
2209 }
2210 }
2211 linerest = (long)(ptr - line_start);
2212 ui_breakcheck();
2213 }
2214
2215failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002216 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (error && read_count == 0)
2218 error = FALSE;
2219
2220 /*
2221 * If we get EOF in the middle of a line, note the fact and
2222 * complete the line ourselves.
2223 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2224 */
2225 if (!error
2226 && !got_int
2227 && linerest != 0
2228 && !(!curbuf->b_p_bin
2229 && fileformat == EOL_DOS
2230 && *line_start == Ctrl_Z
2231 && ptr == line_start + 1))
2232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002233 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 curbuf->b_p_eol = FALSE;
2236 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002237 len = (colnr_T)(ptr - line_start + 1);
2238 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 error = TRUE;
2240 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002241 {
2242#ifdef FEAT_PERSISTENT_UNDO
2243 if (read_undo_file)
2244 sha256_update(&sha_ctx, line_start, len);
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002254 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002255 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002256 crypt_free_state(curbuf->b_cryptstate);
2257 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002258 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002259 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2260 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 // Don't set cryptkey to NULL, it's used below as a flag that
2262 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#endif
2264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002265 // If editing a new file: set 'fenc' for the current buffer.
2266 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002269 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (fenc_alloced)
2271 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002272#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#endif
2276
2277 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002278 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002279#ifdef HAVE_FD_CLOEXEC
2280 else
2281 {
2282 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002283
Bram Moolenaarf05da212009-11-17 16:13:15 +00002284 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002285 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002286 }
2287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 vim_free(buffer);
2289
2290#ifdef HAVE_DUP
2291 if (read_stdin)
2292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002295 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297#endif
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (tmpname != NULL)
2300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002301 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 vim_free(tmpname);
2303 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002304 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 /*
2307 * In recovery mode everything but autocommands is skipped.
2308 */
2309 if (!recoverymode)
2310 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2313 {
2314#ifdef FEAT_NETBEANS_INTG
2315 netbeansFireChanges = 0;
2316#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002317 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#ifdef FEAT_NETBEANS_INTG
2319 netbeansFireChanges = 1;
2320#endif
2321 --linecnt;
2322 }
2323 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2324 if (filesize == 0)
2325 linecnt = 0;
2326 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002329#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // After reading the text into the buffer the diff info needs to
2331 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002332 diff_invalidate(curbuf);
2333#endif
2334#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002335 // All folds in the window are invalid now. Mark them for update
2336 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002337 foldUpdateAll(curwin);
2338#endif
2339 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002340 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 appended_lines_mark(from, linecnt);
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343#ifndef ALWAYS_USE_GUI
2344 /*
2345 * If we were reading from the same terminal as where messages go,
2346 * the screen will have been messed up.
2347 * Switch on raw mode now and clear the screen.
2348 */
2349 if (read_stdin)
2350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002351 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 starttermcap();
2353 screenclear();
2354 }
2355#endif
2356
2357 if (got_int)
2358 {
2359 if (!(flags & READ_DUMMY))
2360 {
2361 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2362 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
2365 msg_scroll = msg_save;
2366#ifdef FEAT_VIMINFO
2367 check_marks_read();
2368#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002369 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371
2372 if (!filtering && !(flags & READ_DUMMY))
2373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002374 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 c = FALSE;
2376
2377#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002378 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
2380 STRCAT(IObuff, _("[fifo]"));
2381 c = TRUE;
2382 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002383 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385 STRCAT(IObuff, _("[socket]"));
2386 c = TRUE;
2387 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002388# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002389 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002390 {
2391 STRCAT(IObuff, _("[character special]"));
2392 c = TRUE;
2393 }
2394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#endif
2396 if (curbuf->b_p_ro)
2397 {
2398 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2399 c = TRUE;
2400 }
2401 if (read_no_eol_lnum)
2402 {
2403 msg_add_eol();
2404 c = TRUE;
2405 }
2406 if (ff_error == EOL_DOS)
2407 {
2408 STRCAT(IObuff, _("[CR missing]"));
2409 c = TRUE;
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (split)
2412 {
2413 STRCAT(IObuff, _("[long lines split]"));
2414 c = TRUE;
2415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (notconverted)
2417 {
2418 STRCAT(IObuff, _("[NOT converted]"));
2419 c = TRUE;
2420 }
2421 else if (converted)
2422 {
2423 STRCAT(IObuff, _("[converted]"));
2424 c = TRUE;
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426#ifdef FEAT_CRYPT
2427 if (cryptkey != NULL)
2428 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002429 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 c = TRUE;
2431 }
2432#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002433 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002435 sprintf((char *)IObuff + STRLEN(IObuff),
2436 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 c = TRUE;
2438 }
2439 else if (illegal_byte > 0)
2440 {
2441 sprintf((char *)IObuff + STRLEN(IObuff),
2442 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2443 c = TRUE;
2444 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002445 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
2447 STRCAT(IObuff, _("[READ ERRORS]"));
2448 c = TRUE;
2449 }
2450 if (msg_add_fileformat(fileformat))
2451 c = TRUE;
2452#ifdef FEAT_CRYPT
2453 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002454 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002455 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 else
2457#endif
2458 msg_add_lines(c, (long)linecnt, filesize);
2459
Bram Moolenaard23a8232018-02-10 18:45:26 +01002460 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 msg_scrolled_ign = TRUE;
2462#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 // Don't show the message when reading stdin, it would end up in a
2464 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (read_stdin || read_buffer)
2466 p = msg_may_trunc(FALSE, IObuff);
2467 else
2468#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002469 {
2470 if (msg_col > 0)
2471 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002472 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002475 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002476 // Need to repeat the message after redrawing when:
2477 // - When reading from stdin (the screen will be cleared next).
2478 // - When restart_edit is set (otherwise there will be a delay
2479 // before redrawing).
2480 // - When the screen was scrolled but there is no wait-return
2481 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002482 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 msg_scrolled_ign = FALSE;
2484 }
2485
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002486 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002488 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002489 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 curbuf->b_p_ro = TRUE;
2491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002492 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 /*
2495 * In Ex mode: cursor at last new line.
2496 * Otherwise: cursor at first new line.
2497 */
2498 if (exmode_active)
2499 curwin->w_cursor.lnum = from + linecnt;
2500 else
2501 curwin->w_cursor.lnum = from + 1;
2502 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002503 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
Bram Moolenaare1004402020-10-24 20:49:43 +02002505 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002506 {
2507 // Set '[ and '] marks to the newly read lines.
2508 curbuf->b_op_start.lnum = from + 1;
2509 curbuf->b_op_start.col = 0;
2510 curbuf->b_op_end.lnum = from + linecnt;
2511 curbuf->b_op_end.col = 0;
2512 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002513
Bram Moolenaar4f974752019-02-17 17:44:42 +01002514#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002515 /*
2516 * Work around a weird problem: When a file has two links (only
2517 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002518 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002519 * It's correct again after reading the file, thus reset the timestamp
2520 * here.
2521 */
2522 if (newfile && !read_stdin && !read_buffer
2523 && mch_stat((char *)fname, &st) >= 0)
2524 {
2525 buf_store_time(curbuf, &st, fname);
2526 curbuf->b_mtime_read = curbuf->b_mtime;
2527 }
2528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530 msg_scroll = msg_save;
2531
2532#ifdef FEAT_VIMINFO
2533 /*
2534 * Get the marks before executing autocommands, so they can be used there.
2535 */
2536 check_marks_read();
2537#endif
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002540 * We remember if the last line of the read didn't have
2541 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2542 * or writing the read again with 'binary' on. The latter is required
2543 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002545 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002547 // When reloading a buffer put the cursor at the first line that is
2548 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002549 if (flags & READ_KEEP_UNDO)
2550 u_find_first_changed();
2551
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002552#ifdef FEAT_PERSISTENT_UNDO
2553 /*
2554 * When opening a new file locate undo info and read it.
2555 */
2556 if (read_undo_file)
2557 {
2558 char_u hash[UNDO_HASH_SIZE];
2559
2560 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002561 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002562 }
2563#endif
2564
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002565 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 int m = msg_scroll;
2568 int n = msg_scrolled;
2569
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002570 // Save the fileformat now, otherwise the buffer will be considered
2571 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002572 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 save_file_ff(curbuf);
2574
2575 /*
2576 * The output from the autocommands should not overwrite anything and
2577 * should not be overwritten: Set msg_scroll, restore its value if no
2578 * output was done.
2579 */
2580 msg_scroll = TRUE;
2581 if (filtering)
2582 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2583 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002584 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2587 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002588 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2589 /*
2590 * EVENT_FILETYPE was not triggered but the buffer already has a
2591 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2592 */
2593 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2594 TRUE, curbuf);
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 else
2597 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2598 FALSE, NULL, eap);
2599 if (msg_scrolled == n)
2600 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002601# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002602 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
2607 if (recoverymode && error)
2608 return FAIL;
2609 return OK;
2610}
2611
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002612#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002613/*
2614 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2615 * which is the name of files used for process substitution output by
2616 * some shells on some operating systems, e.g., bash on SunOS.
2617 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2618 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002620is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002621{
2622 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2623 && VIM_ISDIGIT(fname[8])
2624 && *skipdigits(fname + 9) == NUL
2625 && (fname[9] != NUL
2626 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2627}
2628#endif
2629
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002630/*
2631 * From the current line count and characters read after that, estimate the
2632 * line number where we are now.
2633 * Used for error messages that include a line number.
2634 */
2635 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002636readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002637 linenr_T linecnt, // line count before reading more bytes
2638 char_u *p, // start of more bytes read
2639 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002640{
2641 char_u *s;
2642 linenr_T lnum;
2643
2644 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2645 for (s = p; s < endp; ++s)
2646 if (*s == '\n')
2647 ++lnum;
2648 return lnum;
2649}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002652 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2653 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 * Returns OK or FAIL.
2655 */
2656 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002657prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar13505972019-01-24 15:04:48 +01002659 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (eap->cmd == NULL)
2661 return FAIL;
2662
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002663 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2664 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002665 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002666 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002667
2668 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002669 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002670 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return OK;
2672}
2673
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002674/*
2675 * Set default or forced 'fileformat' and 'binary'.
2676 */
2677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002678set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002679{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002680 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002681 if (set_options)
2682 {
2683 if (eap != NULL && eap->force_ff != 0)
2684 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2685 else if (*p_ffs != NUL)
2686 set_fileformat(default_fileformat(), OPT_LOCAL);
2687 }
2688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002690 if (eap != NULL && eap->force_bin != 0)
2691 {
2692 int oldval = curbuf->b_p_bin;
2693
2694 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2695 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2696 }
2697}
2698
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002699/*
2700 * Set forced 'fileencoding'.
2701 */
2702 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002703set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002704{
2705 if (eap->force_enc != 0)
2706 {
2707 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2708
2709 if (fenc != NULL)
2710 set_string_option_direct((char_u *)"fenc", -1,
2711 fenc, OPT_FREE|OPT_LOCAL, 0);
2712 vim_free(fenc);
2713 }
2714}
2715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716/*
2717 * Find next fileencoding to use from 'fileencodings'.
2718 * "pp" points to fenc_next. It's advanced to the next item.
2719 * When there are no more items, an empty string is returned and *pp is set to
2720 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002721 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2722 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002725next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 char_u *p;
2728 char_u *r;
2729
Bram Moolenaarf077db22019-08-13 00:18:24 +02002730 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (**pp == NUL)
2732 {
2733 *pp = NULL;
2734 return (char_u *)"";
2735 }
2736 p = vim_strchr(*pp, ',');
2737 if (p == NULL)
2738 {
2739 r = enc_canonize(*pp);
2740 *pp += STRLEN(*pp);
2741 }
2742 else
2743 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002744 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 *pp = p + 1;
2746 if (r != NULL)
2747 {
2748 p = enc_canonize(r);
2749 vim_free(r);
2750 r = p;
2751 }
2752 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002753 if (r != NULL)
2754 *alloced = TRUE;
2755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002757 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 r = (char_u *)"";
2759 *pp = NULL;
2760 }
2761 return r;
2762}
2763
Bram Moolenaar13505972019-01-24 15:04:48 +01002764#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765/*
2766 * Convert a file with the 'charconvert' expression.
2767 * This closes the file which is to be read, converts it and opens the
2768 * resulting file for reading.
2769 * Returns name of the resulting converted file (the caller should delete it
2770 * after reading it).
2771 * Returns NULL if the conversion failed ("*fdp" is not set) .
2772 */
2773 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002774readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002775 char_u *fname, // name of input file
2776 char_u *fenc, // converted from
2777 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002780 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002782 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002784 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 else
2786 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002787 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 *fdp = -1;
2789 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2790 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002791 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2793 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002794 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796
2797 if (errmsg != NULL)
2798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002799 // Don't use emsg(), it breaks mappings, the retry with
2800 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002801 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (tmpname != NULL)
2803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002804 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002805 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 }
2808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002809 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (*fdp < 0)
2811 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2812
2813 return tmpname;
2814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
2816
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002817#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002819 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2821 * *filesizep are updated.
2822 * Return the (new) encryption key, NULL for no encryption.
2823 */
2824 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002825check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002826 char_u *cryptkey, // previous encryption key or NULL
2827 char_u *ptr, // pointer to read bytes
2828 long *sizep, // length of read bytes
2829 off_T *filesizep, // nr of bytes used from file
2830 int newfile, // editing a new buffer
2831 char_u *fname, // file name to display
2832 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002834 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002835 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002836
2837 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 // Mark the buffer as read-only until the decryption has taken place.
2840 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002841 curbuf->b_p_ro = TRUE;
2842
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002843 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002844 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002845 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
2847 if (*curbuf->b_p_key)
2848 cryptkey = curbuf->b_p_key;
2849 else
2850 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002851 // When newfile is TRUE, store the typed key in the 'key'
2852 // option and don't free it. bf needs hash of the key saved.
2853 // Don't ask for the key again when first time Enter was hit.
2854 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002855 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002856 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002857 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002858 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002859 *did_ask = TRUE;
2860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002861 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (cryptkey != NULL && *cryptkey == NUL)
2863 {
2864 if (cryptkey != curbuf->b_p_key)
2865 vim_free(cryptkey);
2866 cryptkey = NULL;
2867 }
2868 }
2869 }
2870
2871 if (cryptkey != NULL)
2872 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002873 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002874
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002875 curbuf->b_cryptstate = crypt_create_from_header(
2876 method, cryptkey, ptr);
2877 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002880 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002881 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002882 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002883 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002884 *filesizep += header_len;
2885 *sizep -= header_len;
2886 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002888 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002889 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002892 // When starting to edit a new file which does not have encryption, clear
2893 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002894 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2896
2897 return cryptkey;
2898}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002899#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002900
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002902 * Return TRUE if a file appears to be read-only from the file permissions.
2903 */
2904 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002905check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002906 char_u *fname, // full path to file
2907 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002908{
2909#ifndef USE_MCH_ACCESS
2910 int fd = 0;
2911#endif
2912
2913 return (
2914#ifdef USE_MCH_ACCESS
2915# ifdef UNIX
2916 (perm & 0222) == 0 ||
2917# endif
2918 mch_access((char *)fname, W_OK)
2919#else
2920 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2921 ? TRUE : (close(fd), FALSE)
2922#endif
2923 );
2924}
2925
Bram Moolenaara7870192019-02-14 12:56:36 +01002926#if defined(HAVE_FSYNC) || defined(PROTO)
2927/*
2928 * Call fsync() with Mac-specific exception.
2929 * Return fsync() result: zero for success.
2930 */
2931 int
2932vim_fsync(int fd)
2933{
2934 int r;
2935
2936# ifdef MACOS_X
2937 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002938 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002939# endif
2940 r = fsync(fd);
2941 return r;
2942}
2943#endif
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002946 * Set the name of the current buffer. Use when the buffer doesn't have a
2947 * name and a ":r" or ":w" command with a file name is used.
2948 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002949 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002950set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002951{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002952 buf_T *buf = curbuf;
2953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002954 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002955 if (curbuf->b_p_bl)
2956 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2957 apply_autocmds(EVENT_BUFWIPEOUT, 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 Moolenaar8b38e242009-06-16 13:35:20 +00002962 if (curbuf != buf)
2963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002965 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002966 return FAIL;
2967 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002968
2969 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2970 curbuf->b_flags |= BF_NOTEDITED;
2971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002972 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002973 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2974 if (curbuf->b_p_bl)
2975 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002976#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002977 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002978 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002979#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002981 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002982 if (*curbuf->b_p_ft == NUL)
2983 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002984 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002985 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002986 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002987 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002988
2989 return OK;
2990}
2991
2992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 * Put file name into IObuff with quotes.
2994 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002996msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 if (fname == NULL)
2999 fname = (char_u *)"-stdin-";
3000 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3001 IObuff[0] = '"';
3002 STRCAT(IObuff, "\" ");
3003}
3004
3005/*
3006 * Append message for text mode to IObuff.
3007 * Return TRUE if something appended.
3008 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003009 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003010msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012#ifndef USE_CRNL
3013 if (eol_type == EOL_DOS)
3014 {
3015 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3016 return TRUE;
3017 }
3018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (eol_type == EOL_MAC)
3020 {
3021 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3022 return TRUE;
3023 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003024#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (eol_type == EOL_UNIX)
3026 {
3027 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3028 return TRUE;
3029 }
3030#endif
3031 return FALSE;
3032}
3033
3034/*
3035 * Append line and character count to IObuff.
3036 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003038msg_add_lines(
3039 int insert_space,
3040 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003041 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 char_u *p;
3044
3045 p = IObuff + STRLEN(IObuff);
3046
3047 if (insert_space)
3048 *p++ = ' ';
3049 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003050 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003051 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 else
3053 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003054 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003056 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003057 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003058 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 }
3060}
3061
3062/*
3063 * Append message for missing line separator to IObuff.
3064 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003065 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003066msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3069}
3070
Bram Moolenaar473952e2019-09-28 16:30:04 +02003071 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003072time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003074#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003075 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3076 // the seconds. Since the roundoff is done when flushing the inode, the
3077 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 return (t1 - t2 > 1 || t2 - t1 > 1);
3079#else
3080 return (t1 != t2);
3081#endif
3082}
3083
3084/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003085 * Return TRUE if file encoding "fenc" requires conversion from or to
3086 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003088 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003089need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003091 int same_encoding;
3092 int enc_flags;
3093 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003095 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003096 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003097 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003098 fenc_flags = 0;
3099 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003100 else
3101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003102 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3103 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003104 enc_flags = get_fio_flags(p_enc);
3105 fenc_flags = get_fio_flags(fenc);
3106 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3107 }
3108 if (same_encoding)
3109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003110 // Specified encoding matches with 'encoding'. This requires
3111 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003112 return enc_unicode != 0;
3113 }
3114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003115 // Encodings differ. However, conversion is not needed when 'enc' is any
3116 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003117 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118}
3119
3120/*
3121 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3122 * internal conversion.
3123 * if "ptr" is an empty string, use 'encoding'.
3124 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003125 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003126get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 int prop;
3129
3130 if (*ptr == NUL)
3131 ptr = p_enc;
3132
3133 prop = enc_canon_props(ptr);
3134 if (prop & ENC_UNICODE)
3135 {
3136 if (prop & ENC_2BYTE)
3137 {
3138 if (prop & ENC_ENDIAN_L)
3139 return FIO_UCS2 | FIO_ENDIAN_L;
3140 return FIO_UCS2;
3141 }
3142 if (prop & ENC_4BYTE)
3143 {
3144 if (prop & ENC_ENDIAN_L)
3145 return FIO_UCS4 | FIO_ENDIAN_L;
3146 return FIO_UCS4;
3147 }
3148 if (prop & ENC_2WORD)
3149 {
3150 if (prop & ENC_ENDIAN_L)
3151 return FIO_UTF16 | FIO_ENDIAN_L;
3152 return FIO_UTF16;
3153 }
3154 return FIO_UTF8;
3155 }
3156 if (prop & ENC_LATIN1)
3157 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003158 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return 0;
3160}
3161
Bram Moolenaar473952e2019-09-28 16:30:04 +02003162#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163/*
3164 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3165 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3166 * Used for conversion between 'encoding' and 'fileencoding'.
3167 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003168 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003169get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 int cp;
3172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003173 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (!enc_utf8 && enc_codepage <= 0)
3175 return 0;
3176
3177 cp = encname2codepage(ptr);
3178 if (cp == 0)
3179 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003180# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (STRCMP(ptr, "utf-8") == 0)
3182 cp = CP_UTF8;
3183 else
3184# endif
3185 return 0;
3186 }
3187 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3188}
3189#endif
3190
Bram Moolenaar473952e2019-09-28 16:30:04 +02003191#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192/*
3193 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3194 * needed for the internal conversion to/from utf-8 or latin1.
3195 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003197get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3200 && (enc_canon_props(ptr) & ENC_MACROMAN))
3201 return FIO_MACROMAN;
3202 return 0;
3203}
3204#endif
3205
3206/*
3207 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3208 * "size" must be at least 2.
3209 * Return the name of the encoding and set "*lenp" to the length.
3210 * Returns NULL when no BOM found.
3211 */
3212 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003213check_for_bom(
3214 char_u *p,
3215 long size,
3216 int *lenp,
3217 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 char *name = NULL;
3220 int len = 2;
3221
3222 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003223 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003225 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 len = 3;
3227 }
3228 else if (p[0] == 0xff && p[1] == 0xfe)
3229 {
3230 if (size >= 4 && p[2] == 0 && p[3] == 0
3231 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003233 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 len = 4;
3235 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003236 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003237 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003238 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003239 // utf-16le is preferred, it also works for ucs-2le text
3240 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242 else if (p[0] == 0xfe && p[1] == 0xff
3243 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003245 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003246 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003247 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003248 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003249 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3252 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003254 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 len = 4;
3256 }
3257
3258 *lenp = len;
3259 return (char_u *)name;
3260}
3261
3262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 * Try to find a shortname by comparing the fullname with the current
3264 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003265 * Returns "full_path" or pointer into "full_path" if shortened.
3266 */
3267 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003268shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003269{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003270 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003271 char_u *p = full_path;
3272
Bram Moolenaard9462e32011-04-11 21:35:11 +02003273 dirname = alloc(MAXPATHL);
3274 if (dirname == NULL)
3275 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003276 if (mch_dirname(dirname, MAXPATHL) == OK)
3277 {
3278 p = shorten_fname(full_path, dirname);
3279 if (p == NULL || *p == NUL)
3280 p = full_path;
3281 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003282 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003283 return p;
3284}
3285
3286/*
3287 * Try to find a shortname by comparing the fullname with the current
3288 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * Returns NULL if not shorter name possible, pointer into "full_path"
3290 * otherwise.
3291 */
3292 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003293shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 int len;
3296 char_u *p;
3297
3298 if (full_path == NULL)
3299 return NULL;
3300 len = (int)STRLEN(dir_name);
3301 if (fnamencmp(dir_name, full_path, len) == 0)
3302 {
3303 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003304#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003306 * MS-Windows: when a file is in the root directory, dir_name will end
3307 * in a slash, since C: by itself does not define a specific dir. In
3308 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 if (!((len > 2) && (*(p - 2) == ':')))
3311#endif
3312 {
3313 if (vim_ispathsep(*p))
3314 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003315#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 else
3317 p = NULL;
3318#endif
3319 }
3320 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003321#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 /*
3323 * When using a file in the current drive, remove the drive name:
3324 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3325 * a floppy from "A:\dir" to "B:\dir".
3326 */
3327 else if (len > 3
3328 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3329 && full_path[1] == ':'
3330 && vim_ispathsep(full_path[2]))
3331 p = full_path + 2;
3332#endif
3333 else
3334 p = NULL;
3335 return p;
3336}
3337
3338/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003339 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * When "force" is TRUE: Use full path from now on for files currently being
3341 * edited, both for file name and swap file name. Try to shorten the file
3342 * names a bit, if safe to do so.
3343 * When "force" is FALSE: Only try to shorten absolute file names.
3344 * For buffers that have buftype "nofile" or "scratch": never change the file
3345 * name.
3346 */
3347 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003348shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3349{
3350 char_u *p;
3351
3352 if (buf->b_fname != NULL
3353#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003354 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003355#endif
3356 && !path_with_url(buf->b_fname)
3357 && (force
3358 || buf->b_sfname == NULL
3359 || mch_isFullName(buf->b_sfname)))
3360 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003361 if (buf->b_sfname != buf->b_ffname)
3362 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003363 p = shorten_fname(buf->b_ffname, dirname);
3364 if (p != NULL)
3365 {
3366 buf->b_sfname = vim_strsave(p);
3367 buf->b_fname = buf->b_sfname;
3368 }
3369 if (p == NULL || buf->b_fname == NULL)
3370 buf->b_fname = buf->b_ffname;
3371 }
3372}
3373
3374/*
3375 * Shorten filenames for all buffers.
3376 */
3377 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003378shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 char_u dirname[MAXPATHL];
3381 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003384 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003386 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003387
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003388 // Always make the swap file name a full path, a "nofile" buffer may
3389 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003390 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003394#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003395 popup_update_preview_title();
3396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397}
3398
3399#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3400 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003401 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 || defined(PROTO)
3403/*
3404 * Shorten all filenames in "fnames[count]" by current directory.
3405 */
3406 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003407shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 int i;
3410 char_u dirname[MAXPATHL];
3411 char_u *p;
3412
3413 if (fnames == NULL || count < 1)
3414 return;
3415 mch_dirname(dirname, sizeof(dirname));
3416 for (i = 0; i < count; ++i)
3417 {
3418 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003420 // shorten_fname() returns pointer in given "fnames[i]". If free
3421 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3422 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 p = vim_strsave(p);
3424 vim_free(fnames[i]);
3425 fnames[i] = p;
3426 }
3427 }
3428}
3429#endif
3430
3431/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003432 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 * fo_o_h.ext for MSDOS or when shortname option set.
3434 *
3435 * Assumed that fname is a valid name found in the filesystem we assure that
3436 * the return value is a different name and ends in 'ext'.
3437 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3438 * characters otherwise.
3439 * Space for the returned name is allocated, must be freed later.
3440 * Returns NULL when out of memory.
3441 */
3442 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003443modname(
3444 char_u *fname,
3445 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003446 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003448 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 fname, ext, prepend_dot);
3450}
3451
3452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003454 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003455 char_u *fname,
3456 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003457 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 char_u *retval;
3460 char_u *s;
3461 char_u *e;
3462 char_u *ptr;
3463 int fnamelen, extlen;
3464
3465 extlen = (int)STRLEN(ext);
3466
3467 /*
3468 * If there is no file name we must get the name of the current directory
3469 * (we need the full path in case :cd is used).
3470 */
3471 if (fname == NULL || *fname == NUL)
3472 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003473 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (retval == NULL)
3475 return NULL;
3476 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3477 (fnamelen = (int)STRLEN(retval)) == 0)
3478 {
3479 vim_free(retval);
3480 return NULL;
3481 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003482 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
3484 retval[fnamelen++] = PATHSEP;
3485 retval[fnamelen] = NUL;
3486 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003487 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 else
3490 {
3491 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003492 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (retval == NULL)
3494 return NULL;
3495 STRCPY(retval, fname);
3496#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003497 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#endif
3499 }
3500
3501 /*
3502 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3503 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3504 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3505 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3506 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003507 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003509 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003510 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003513 {
3514 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003519 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3521 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 s = ptr + STRLEN(ptr);
3524
3525 /*
3526 * For 8.3 file names we may have to reduce the length.
3527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 /*
3531 * If there is no file name, or the file name ends in '/', and the
3532 * extension starts with '.', put a '_' before the dot, because just
3533 * ".ext" is invalid.
3534 */
3535 if (fname == NULL || *fname == NUL
3536 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 *s++ = '_';
3540 }
3541 /*
3542 * If the extension starts with '.', truncate the base name at 8
3543 * characters
3544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003547 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 s = ptr + 8;
3550 *s = '\0';
3551 }
3552 }
3553 /*
3554 * If the extension doesn't start with '.', and the file name
3555 * doesn't have an extension yet, append a '.'
3556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 else if ((e = vim_strchr(ptr, '.')) == NULL)
3558 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /*
3560 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003561 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 */
3563 else if ((int)STRLEN(e) + extlen > 4)
3564 s = e + 4 - extlen;
3565 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003566#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 /*
3568 * If there is no file name, and the extension starts with '.', put a
3569 * '_' before the dot, because just ".ext" may be invalid if it's on a
3570 * FAT partition, and on HPFS it doesn't matter.
3571 */
3572 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3573 *s++ = '_';
3574#endif
3575
3576 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003577 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 * ext can start with '.' and cannot exceed 3 more characters.
3579 */
3580 STRCPY(s, ext);
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 /*
3583 * Prepend the dot.
3584 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003585 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003587 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 /*
3592 * Check that, after appending the extension, the file name is really
3593 * different.
3594 */
3595 if (fname != NULL && STRCMP(fname, retval) == 0)
3596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003597 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 while (--s >= ptr)
3599 {
3600 if (*s != '_')
3601 {
3602 *s = '_';
3603 break;
3604 }
3605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003606 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 *ptr = 'v';
3608 }
3609 return retval;
3610}
3611
3612/*
3613 * Like fgets(), but if the file line is too long, it is truncated and the
3614 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003615 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 */
3617 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003618vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 char *eof;
3621#define FGETS_SIZE 200
3622 char tbuf[FGETS_SIZE];
3623
3624 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3627 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003628 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003630 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 do
3632 {
3633 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003634 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3636 }
3637 return (eof == NULL);
3638}
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640/*
3641 * rename() only works if both files are on the same file system, this
3642 * function will (attempts to?) copy the file across if rename fails -- webb
3643 * Return -1 for failure, 0 for success.
3644 */
3645 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003646vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 int fd_in;
3649 int fd_out;
3650 int n;
3651 char *errmsg = NULL;
3652 char *buffer;
3653#ifdef AMIGA
3654 BPTR flock;
3655#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003656 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003657 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003658#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003659 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003660#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003661 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003664 * When the names are identical, there is nothing to do. When they refer
3665 * to the same file (ignoring case and slash/backslash differences) but
3666 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003669 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003670 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003671 use_tmp_file = TRUE;
3672 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003673 return 0;
3674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676 /*
3677 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3678 */
3679 if (mch_stat((char *)from, &st) < 0)
3680 return -1;
3681
Bram Moolenaar3576da72008-12-30 15:15:57 +00003682#ifdef UNIX
3683 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003684 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003685
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003686 // It's possible for the source and destination to be the same file.
3687 // This happens when "from" and "to" differ in case and are on a FAT32
3688 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003689 if (mch_stat((char *)to, &st_to) >= 0
3690 && st.st_dev == st_to.st_dev
3691 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003692 use_tmp_file = TRUE;
3693 }
3694#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003695#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003696 {
3697 BY_HANDLE_FILE_INFORMATION info1, info2;
3698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003699 // It's possible for the source and destination to be the same file.
3700 // In that case go through a temp file name. This makes rename("foo",
3701 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003702 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3703 && win32_fileinfo(to, &info2) == FILEINFO_OK
3704 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3705 && info1.nFileIndexHigh == info2.nFileIndexHigh
3706 && info1.nFileIndexLow == info2.nFileIndexLow)
3707 use_tmp_file = TRUE;
3708 }
3709#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003710
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003711 if (use_tmp_file)
3712 {
3713 char tempname[MAXPATHL + 1];
3714
3715 /*
3716 * Find a name that doesn't exist and is in the same directory.
3717 * Rename "from" to "tempname" and then rename "tempname" to "to".
3718 */
3719 if (STRLEN(from) >= MAXPATHL - 5)
3720 return -1;
3721 STRCPY(tempname, from);
3722 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003723 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003724 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3725 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003726 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003727 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003728 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003729 if (mch_rename(tempname, (char *)to) == 0)
3730 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 // Strange, the second step failed. Try moving the
3732 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003733 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003734 return -1;
3735 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003736 // If it fails for one temp name it will most likely fail
3737 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003738 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003739 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003740 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003741 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003742 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 /*
3745 * Delete the "to" file, this is required on some systems to make the
3746 * mch_rename() work, on other systems it makes sure that we don't have
3747 * two files when the mch_rename() fails.
3748 */
3749
3750#ifdef AMIGA
3751 /*
3752 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3753 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003754 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 * deleting the "from" file (horror!) we lock it during the remove.
3756 *
3757 * When used for making a backup before writing the file: This should not
3758 * happen with ":w", because startscript() should detect this problem and
3759 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3760 * name. This problem does exist with ":w filename", but then the
3761 * original file will be somewhere else so the backup isn't really
3762 * important. If autoscripting is off the rename may fail.
3763 */
3764 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3765#endif
3766 mch_remove(to);
3767#ifdef AMIGA
3768 if (flock)
3769 UnLock(flock);
3770#endif
3771
3772 /*
3773 * First try a normal rename, return if it works.
3774 */
3775 if (mch_rename((char *)from, (char *)to) == 0)
3776 return 0;
3777
3778 /*
3779 * Rename() failed, try copying the file.
3780 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003781 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003782#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003783 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003784 acl = mch_get_acl(from);
3785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3787 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003788 {
3789#ifdef HAVE_ACL
3790 mch_free_acl(acl);
3791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003793 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003795 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003796 fd_out = mch_open((char *)to,
3797 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (fd_out == -1)
3799 {
3800 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003801#ifdef HAVE_ACL
3802 mch_free_acl(acl);
3803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return -1;
3805 }
3806
Bram Moolenaar473952e2019-09-28 16:30:04 +02003807 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (buffer == NULL)
3809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003811 close(fd_in);
3812#ifdef HAVE_ACL
3813 mch_free_acl(acl);
3814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return -1;
3816 }
3817
Bram Moolenaar473952e2019-09-28 16:30:04 +02003818 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003819 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
3821 errmsg = _("E208: Error writing to \"%s\"");
3822 break;
3823 }
3824
3825 vim_free(buffer);
3826 close(fd_in);
3827 if (close(fd_out) < 0)
3828 errmsg = _("E209: Error closing \"%s\"");
3829 if (n < 0)
3830 {
3831 errmsg = _("E210: Error reading \"%s\"");
3832 to = from;
3833 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003834#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003835 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003836#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003837#ifdef HAVE_ACL
3838 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003839 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003840#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003841#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003842 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (errmsg != NULL)
3845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003846 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return -1;
3848 }
3849 mch_remove(from);
3850 return 0;
3851}
3852
3853static int already_warned = FALSE;
3854
3855/*
3856 * Check if any not hidden buffer has been changed.
3857 * Postpone the check if there are characters in the stuff buffer, a global
3858 * command is being executed, a mapping is being executed or an autocommand is
3859 * busy.
3860 * Returns TRUE if some message was written (screen should be redrawn and
3861 * cursor positioned).
3862 */
3863 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003864check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003865 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 buf_T *buf;
3868 int didit = 0;
3869 int n;
3870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 // Don't check timestamps while system() or another low-level function may
3872 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 if (no_check_timestamps > 0)
3874 return FALSE;
3875
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003876 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3877 // event and we would keep on checking if the file is steadily growing.
3878 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (focus && did_check_timestamps)
3880 {
3881 need_check_timestamps = TRUE;
3882 return FALSE;
3883 }
3884
3885 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003886 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003887 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 else
3889 {
3890 ++no_wait_return;
3891 did_check_timestamps = TRUE;
3892 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003893 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003895 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (buf->b_nwindows > 0)
3897 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003898 bufref_T bufref;
3899
3900 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 n = buf_check_timestamp(buf, focus);
3902 if (didit < n)
3903 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003904 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 // Autocommands have removed the buffer, start at the
3907 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 buf = firstbuf;
3909 continue;
3910 }
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 --no_wait_return;
3914 need_check_timestamps = FALSE;
3915 if (need_wait_return && didit == 2)
3916 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003917 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003918 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 out_flush();
3920 }
3921 }
3922 return didit;
3923}
3924
3925/*
3926 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3927 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3928 * empty.
3929 */
3930 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003931move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
3933 buf_T *tbuf = curbuf;
3934 int retval = OK;
3935 linenr_T lnum;
3936 char_u *p;
3937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003938 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 curbuf = tobuf;
3940 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3941 {
3942 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3943 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3944 {
3945 vim_free(p);
3946 retval = FAIL;
3947 break;
3948 }
3949 vim_free(p);
3950 }
3951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003952 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (retval != FAIL)
3954 {
3955 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003956 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003957 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003959 // Oops! We could try putting back the saved lines, but that
3960 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 retval = FAIL;
3962 break;
3963 }
3964 }
3965
3966 curbuf = tbuf;
3967 return retval;
3968}
3969
3970/*
3971 * Check if buffer "buf" has been changed.
3972 * Also check if the file for a new buffer unexpectedly appeared.
3973 * return 1 if a changed buffer was found.
3974 * return 2 if a message has been displayed.
3975 * return 0 otherwise.
3976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003978buf_check_timestamp(
3979 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003980 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003982 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 int stat_res;
3984 int retval = 0;
3985 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003986 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003988 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int helpmesg = FALSE;
3990 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003991 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3993 int can_reload = FALSE;
3994#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003995 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 int orig_mode = buf->b_orig_mode;
3997#ifdef FEAT_GUI
3998 int save_mouse_correct = need_mouse_correct;
3999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004001 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004002#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004003 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004004#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004005 bufref_T bufref;
4006
4007 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004009 // If there is no file name, the buffer is not loaded, 'buftype' is
4010 // set, we are in the middle of a save or being called recursively: ignore
4011 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (buf->b_ffname == NULL
4013 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004014 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004017#ifdef FEAT_NETBEANS_INTG
4018 || isNetbeansBuffer(buf)
4019#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004020#ifdef FEAT_TERMINAL
4021 || buf->b_term != NULL
4022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 )
4024 return 0;
4025
4026 if ( !(buf->b_flags & BF_NOTEDITED)
4027 && buf->b_mtime != 0
4028 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4029 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004030 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#ifdef HAVE_ST_MODE
4032 || (int)st.st_mode != buf->b_orig_mode
4033#else
4034 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4035#endif
4036 ))
4037 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004038 long prev_b_mtime = buf->b_mtime;
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 retval = 1;
4041
Bram Moolenaar386bc822018-07-07 18:34:12 +02004042 // set b_mtime to stop further warnings (e.g., when executing
4043 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (stat_res < 0)
4045 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004046 // Check the file again later to see if it re-appears.
4047 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 buf->b_orig_size = 0;
4049 buf->b_orig_mode = 0;
4050 }
4051 else
4052 buf_store_time(buf, &st, buf->b_ffname);
4053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004054 // Don't do anything for a directory. Might contain the file
4055 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (mch_isdir(buf->b_fname))
4057 ;
4058
4059 /*
4060 * If 'autoread' is set, the buffer has no changes and the file still
4061 * exists, reload the buffer. Use the buffer-local option value if it
4062 * was set, the global option value otherwise.
4063 */
4064 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4065 && !bufIsChanged(buf) && stat_res >= 0)
4066 reload = TRUE;
4067 else
4068 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004069 if (stat_res < 0)
4070 reason = "deleted";
4071 else if (bufIsChanged(buf))
4072 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004073 /*
4074 * Check if the file contents really changed to avoid giving a
4075 * warning when only the timestamp was set (e.g., checked out of
4076 * CVS). Always warn when the buffer was changed.
4077 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004078 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4079 reason = "changed";
4080 else if (orig_mode != buf->b_orig_mode)
4081 reason = "mode";
4082 else
4083 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 /*
4086 * Only give the warning if there are no FileChangedShell
4087 * autocommands.
4088 * Avoid being called recursively by setting "busy".
4089 */
4090 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004091#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004092 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4093 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004094#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004095 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4097 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004098 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 busy = FALSE;
4100 if (n)
4101 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004102 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004103 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004104#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004105 s = get_vim_var_str(VV_FCS_CHOICE);
4106 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4107 reload = TRUE;
4108 else if (STRCMP(s, "ask") == 0)
4109 n = FALSE;
4110 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004111#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004112 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004114 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004116 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004117 {
4118 // Only give the message once.
4119 if (prev_b_mtime != -1)
4120 mesg = _("E211: File \"%s\" no longer available");
4121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 else
4123 {
4124 helpmesg = TRUE;
4125#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4126 can_reload = TRUE;
4127#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004128 if (reason[2] == 'n')
4129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004131 mesg2 = _("See \":help W12\" for more info.");
4132 }
4133 else if (reason[1] == 'h')
4134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004136 mesg2 = _("See \":help W11\" for more info.");
4137 }
4138 else if (*reason == 'm')
4139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004141 mesg2 = _("See \":help W16\" for more info.");
4142 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004143 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004144 // Only timestamp changed, store it to avoid a warning
4145 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004146 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148 }
4149 }
4150
4151 }
4152 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4153 && vim_fexists(buf->b_ffname))
4154 {
4155 retval = 1;
4156 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4157 buf->b_flags |= BF_NEW_W;
4158#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4159 can_reload = TRUE;
4160#endif
4161 }
4162
4163 if (mesg != NULL)
4164 {
4165 path = home_replace_save(buf, buf->b_fname);
4166 if (path != NULL)
4167 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004168 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004170 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004171 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004172#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004173 // Set warningmsg here, before the unimportant and output-specific
4174 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4178 if (can_reload)
4179 {
4180 if (*mesg2 != NUL)
4181 {
4182 STRCAT(tbuf, "\n");
4183 STRCAT(tbuf, mesg2);
4184 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4186 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004187 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 reload = TRUE;
4189 }
4190 else
4191#endif
4192 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4193 {
4194 if (*mesg2 != NUL)
4195 {
4196 STRCAT(tbuf, "; ");
4197 STRCAT(tbuf, mesg2);
4198 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004199 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 retval = 2;
4201 }
4202 else
4203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
4206 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004207 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004209 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 msg_clr_eos();
4211 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004212 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004215#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004217#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004218 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004219 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004221 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 redraw_cmdline = FALSE;
4223 }
4224 }
4225 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227
4228 vim_free(path);
4229 vim_free(tbuf);
4230 }
4231 }
4232
4233 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004234 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004235 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004236 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004237#ifdef FEAT_PERSISTENT_UNDO
4238 if (buf->b_p_udf && buf->b_ffname != NULL)
4239 {
4240 char_u hash[UNDO_HASH_SIZE];
4241 buf_T *save_curbuf = curbuf;
4242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004244 curbuf = buf;
4245 u_compute_hash(hash);
4246 u_write_undo(NULL, FALSE, buf, hash);
4247 curbuf = save_curbuf;
4248 }
4249#endif
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004252 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004253 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004254 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4255 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004257 // restore this in case an autocommand has set it; it would break
4258 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 need_mouse_correct = save_mouse_correct;
4260#endif
4261
4262 return retval;
4263}
4264
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004265/*
4266 * Reload a buffer that is already loaded.
4267 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004268 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4269 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004270 */
4271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004272buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004273{
4274 exarg_T ea;
4275 pos_T old_cursor;
4276 linenr_T old_topline;
4277 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004278 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004279 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004280 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004281 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004282 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004283
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004284 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004285 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004286
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004287 // We only want to read the text from the file, not reset the syntax
4288 // highlighting, clear marks, diff status, etc. Force the fileformat
4289 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004290 if (prep_exarg(&ea, buf) == OK)
4291 {
4292 old_cursor = curwin->w_cursor;
4293 old_topline = curwin->w_topline;
4294
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004295 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 // Save all the text, so that the reload can be undone.
4298 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004299 u_sync(FALSE);
4300 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4301 flags |= READ_KEEP_UNDO;
4302 }
4303
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004304 /*
4305 * To behave like when a new file is edited (matters for
4306 * BufReadPost autocommands) we first need to delete the current
4307 * buffer contents. But if reading the file fails we should keep
4308 * the old contents. Can't use memory only, the file might be
4309 * too big. Use a hidden buffer to move the buffer contents to.
4310 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004311 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004312 savebuf = NULL;
4313 else
4314 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004315 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004316 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004317 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004318 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004319 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004320 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004321 curbuf = savebuf;
4322 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004323 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004324 curbuf = buf;
4325 curwin->w_buffer = buf;
4326 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004327 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004328 || move_lines(buf, savebuf) == FAIL)
4329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004330 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004331 buf->b_fname);
4332 saved = FAIL;
4333 }
4334 }
4335
4336 if (saved == OK)
4337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4339 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004340 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4341 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004342 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004343 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004344#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004345 if (!aborting())
4346#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004347 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004348 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004350 // Put the text back from the save buffer. First
4351 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004352 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004353 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004354 break;
4355 (void)move_lines(savebuf, buf);
4356 }
4357 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004361 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004362 if ((flags & READ_KEEP_UNDO) == 0)
4363 {
4364 u_blockfree(buf);
4365 u_clearall(buf);
4366 }
4367 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004368 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004370 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004371 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004372 }
4373 }
4374 vim_free(ea.cmd);
4375
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004376 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004377 wipe_buffer(savebuf, FALSE);
4378
4379#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004381 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004382#endif
4383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 // Restore the topline and cursor position and check it (lines may
4385 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004386 if (old_topline > curbuf->b_ml.ml_line_count)
4387 curwin->w_topline = curbuf->b_ml.ml_line_count;
4388 else
4389 curwin->w_topline = old_topline;
4390 curwin->w_cursor = old_cursor;
4391 check_cursor();
4392 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004394#ifdef FEAT_FOLDING
4395 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004396 win_T *wp;
4397 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004400 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004401 if (wp->w_buffer == curwin->w_buffer
4402 && !foldmethodIsManual(wp))
4403 foldUpdateAll(wp);
4404 }
4405#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 // If the mode didn't change and 'readonly' was set, keep the old
4407 // value; the user probably used the ":view" command. But don't
4408 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004409 if (orig_mode == curbuf->b_orig_mode)
4410 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004412 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004413 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004414 }
4415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004416 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004417 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004418 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004419}
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004422buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004425 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426#ifdef HAVE_ST_MODE
4427 buf->b_orig_mode = (int)st->st_mode;
4428#else
4429 buf->b_orig_mode = mch_getperm(fname);
4430#endif
4431}
4432
4433/*
4434 * Adjust the line with missing eol, used for the next write.
4435 * Used for do_filter(), when the input lines for the filter are deleted.
4436 */
4437 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004438write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004441 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442}
4443
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004444// Subfuncions for readdirex()
4445#ifdef FEAT_EVAL
4446# ifdef MSWIN
4447 static char_u *
4448getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4449{
4450 stat_T st;
4451 unsigned short st_mode;
4452 DWORD flag = wfd->dwFileAttributes;
4453 WCHAR *wp;
4454
4455 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4456 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4457 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4458 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4459
4460 wp = wcsrchr(wfd->cFileName, L'.');
4461 if (wp != NULL)
4462 {
4463 if (_wcsicmp(wp, L".exe") == 0 ||
4464 _wcsicmp(wp, L".com") == 0 ||
4465 _wcsicmp(wp, L".cmd") == 0 ||
4466 _wcsicmp(wp, L".bat") == 0)
4467 st_mode |= _S_IEXEC;
4468 }
4469
4470 // Copy user bits to group/other.
4471 st_mode |= (st_mode & 0700) >> 3;
4472 st_mode |= (st_mode & 0700) >> 6;
4473
4474 st.st_mode = st_mode;
4475 return getfpermst(&st, perm);
4476}
4477
4478 static char_u *
4479getftypewfd(WIN32_FIND_DATAW *wfd)
4480{
4481 DWORD flag = wfd->dwFileAttributes;
4482 DWORD tag = wfd->dwReserved0;
4483
4484 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4485 {
4486 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4487 return (char_u*)"junction";
4488 else if (tag == IO_REPARSE_TAG_SYMLINK)
4489 {
4490 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4491 return (char_u*)"linkd";
4492 else
4493 return (char_u*)"link";
4494 }
4495 return (char_u*)"reparse"; // unknown reparse point type
4496 }
4497 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4498 return (char_u*)"dir";
4499 else
4500 return (char_u*)"file";
4501}
4502
4503 static dict_T *
4504create_readdirex_item(WIN32_FIND_DATAW *wfd)
4505{
4506 dict_T *item;
4507 char_u *p;
4508 varnumber_T size, time;
4509 char_u permbuf[] = "---------";
4510
4511 item = dict_alloc();
4512 if (item == NULL)
4513 return NULL;
4514 item->dv_refcount++;
4515
4516 p = utf16_to_enc(wfd->cFileName, NULL);
4517 if (p == NULL)
4518 goto theend;
4519 if (dict_add_string(item, "name", p) == FAIL)
4520 {
4521 vim_free(p);
4522 goto theend;
4523 }
4524 vim_free(p);
4525
4526 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4527 if (dict_add_number(item, "size", size) == FAIL)
4528 goto theend;
4529
4530 // Convert FILETIME to unix time.
4531 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4532 wfd->ftLastWriteTime.dwLowDateTime)
4533 - 116444736000000000) / 10000000;
4534 if (dict_add_number(item, "time", time) == FAIL)
4535 goto theend;
4536
4537 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4538 goto theend;
4539 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4540 goto theend;
4541
4542 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4543 goto theend;
4544 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4545 goto theend;
4546
4547 return item;
4548
4549theend:
4550 dict_unref(item);
4551 return NULL;
4552}
4553# else
4554 static dict_T *
4555create_readdirex_item(char_u *path, char_u *name)
4556{
4557 dict_T *item;
4558 char *p;
4559 size_t len;
4560 stat_T st;
4561 int ret, link = FALSE;
4562 varnumber_T size;
4563 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004564 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004565 struct passwd *pw;
4566 struct group *gr;
4567
4568 item = dict_alloc();
4569 if (item == NULL)
4570 return NULL;
4571 item->dv_refcount++;
4572
4573 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4574 p = alloc(len);
4575 if (p == NULL)
4576 goto theend;
4577 vim_snprintf(p, len, "%s/%s", path, name);
4578 ret = mch_lstat(p, &st);
4579 if (ret >= 0 && S_ISLNK(st.st_mode))
4580 {
4581 link = TRUE;
4582 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004583 if (ret < 0)
4584 q = (char_u*)"link";
4585
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004586 }
4587 vim_free(p);
4588
4589 if (dict_add_string(item, "name", name) == FAIL)
4590 goto theend;
4591
4592 if (ret >= 0)
4593 {
4594 size = (varnumber_T)st.st_size;
4595 if (S_ISDIR(st.st_mode))
4596 size = 0;
4597 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004598 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004599 size = -2;
4600 if (dict_add_number(item, "size", size) == FAIL)
4601 goto theend;
4602 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4603 goto theend;
4604
4605 if (link)
4606 {
4607 if (S_ISDIR(st.st_mode))
4608 q = (char_u*)"linkd";
4609 else
4610 q = (char_u*)"link";
4611 }
4612 else
4613 q = getftypest(&st);
4614 if (dict_add_string(item, "type", q) == FAIL)
4615 goto theend;
4616 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4617 goto theend;
4618
4619 pw = getpwuid(st.st_uid);
4620 if (pw == NULL)
4621 q = (char_u*)"";
4622 else
4623 q = (char_u*)pw->pw_name;
4624 if (dict_add_string(item, "user", q) == FAIL)
4625 goto theend;
4626 gr = getgrgid(st.st_gid);
4627 if (gr == NULL)
4628 q = (char_u*)"";
4629 else
4630 q = (char_u*)gr->gr_name;
4631 if (dict_add_string(item, "group", q) == FAIL)
4632 goto theend;
4633 }
4634 else
4635 {
4636 if (dict_add_number(item, "size", -1) == FAIL)
4637 goto theend;
4638 if (dict_add_number(item, "time", -1) == FAIL)
4639 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004640 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004641 goto theend;
4642 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4643 goto theend;
4644 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4645 goto theend;
4646 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4647 goto theend;
4648 }
4649 return item;
4650
4651theend:
4652 dict_unref(item);
4653 return NULL;
4654}
4655# endif
4656
4657 static int
4658compare_readdirex_item(const void *p1, const void *p2)
4659{
4660 char_u *name1, *name2;
4661
4662 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4663 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004664 if (readdirex_sort == READDIR_SORT_BYTE)
4665 return STRCMP(name1, name2);
4666 else if (readdirex_sort == READDIR_SORT_IC)
4667 return STRICMP(name1, name2);
4668 else
4669 return STRCOLL(name1, name2);
4670}
4671
4672 static int
4673compare_readdir_item(const void *s1, const void *s2)
4674{
4675 if (readdirex_sort == READDIR_SORT_BYTE)
4676 return STRCMP(*(char **)s1, *(char **)s2);
4677 else if (readdirex_sort == READDIR_SORT_IC)
4678 return STRICMP(*(char **)s1, *(char **)s2);
4679 else
4680 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004681}
4682#endif
4683
Bram Moolenaarda440d22016-01-16 21:27:23 +01004684#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4685/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004686 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004687 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004688 * If "withattr" is TRUE, retrieve the names and their attributes.
4689 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004690 * Return OK for success, FAIL for failure.
4691 */
4692 int
4693readdir_core(
4694 garray_T *gap,
4695 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004696 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004697 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004698 int (*checkitem)(void *context, void *item),
4699 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004700{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004701 int failed = FALSE;
4702 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004703# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004704 char_u *buf;
4705 int ok;
4706 HANDLE hFind = INVALID_HANDLE_VALUE;
4707 WIN32_FIND_DATAW wfd;
4708 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004709# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004710 DIR *dirp;
4711 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004712# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004713
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004714 ga_init2(gap, (int)sizeof(void *), 20);
4715
4716# ifdef FEAT_EVAL
4717# define FREE_ITEM(item) do { \
4718 if (withattr) \
4719 dict_unref((dict_T*)item); \
4720 else \
4721 vim_free(item); \
4722 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004723
4724 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004725# else
4726# define FREE_ITEM(item) vim_free(item)
4727# endif
4728
4729# ifdef MSWIN
4730 buf = alloc(MAXPATHL);
4731 if (buf == NULL)
4732 return FAIL;
4733 STRNCPY(buf, path, MAXPATHL-5);
4734 p = buf + STRLEN(buf);
4735 MB_PTR_BACK(buf, p);
4736 if (*p == '\\' || *p == '/')
4737 *p = NUL;
4738 STRCAT(p, "\\*");
4739
4740 wn = enc_to_utf16(buf, NULL);
4741 if (wn != NULL)
4742 hFind = FindFirstFileW(wn, &wfd);
4743 ok = (hFind != INVALID_HANDLE_VALUE);
4744 if (!ok)
4745 {
4746 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004747 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004748 }
4749 else
4750 {
4751 while (ok)
4752 {
4753 int ignore;
4754 void *item;
4755 WCHAR *wp;
4756
4757 wp = wfd.cFileName;
4758 ignore = wp[0] == L'.' &&
4759 (wp[1] == NUL ||
4760 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004761 if (ignore)
4762 {
4763 ok = FindNextFileW(hFind, &wfd);
4764 continue;
4765 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004766# ifdef FEAT_EVAL
4767 if (withattr)
4768 item = (void*)create_readdirex_item(&wfd);
4769 else
4770# endif
4771 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4772 if (item == NULL)
4773 {
4774 failed = TRUE;
4775 break;
4776 }
4777
4778 if (!ignore && checkitem != NULL)
4779 {
4780 int r = checkitem(context, item);
4781
4782 if (r < 0)
4783 {
4784 FREE_ITEM(item);
4785 break;
4786 }
4787 if (r == 0)
4788 ignore = TRUE;
4789 }
4790
4791 if (!ignore)
4792 {
4793 if (ga_grow(gap, 1) == OK)
4794 ((void**)gap->ga_data)[gap->ga_len++] = item;
4795 else
4796 {
4797 failed = TRUE;
4798 FREE_ITEM(item);
4799 break;
4800 }
4801 }
4802 else
4803 FREE_ITEM(item);
4804
4805 ok = FindNextFileW(hFind, &wfd);
4806 }
4807 FindClose(hFind);
4808 }
4809
4810 vim_free(buf);
4811 vim_free(wn);
4812# else // MSWIN
4813 dirp = opendir((char *)path);
4814 if (dirp == NULL)
4815 {
4816 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004817 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004818 }
4819 else
4820 {
4821 for (;;)
4822 {
4823 int ignore;
4824 void *item;
4825
4826 dp = readdir(dirp);
4827 if (dp == NULL)
4828 break;
4829 p = (char_u *)dp->d_name;
4830
4831 ignore = p[0] == '.' &&
4832 (p[1] == NUL ||
4833 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004834 if (ignore)
4835 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004836# ifdef FEAT_EVAL
4837 if (withattr)
4838 item = (void*)create_readdirex_item(path, p);
4839 else
4840# endif
4841 item = (void*)vim_strsave(p);
4842 if (item == NULL)
4843 {
4844 failed = TRUE;
4845 break;
4846 }
4847
4848 if (!ignore && checkitem != NULL)
4849 {
4850 int r = checkitem(context, item);
4851
4852 if (r < 0)
4853 {
4854 FREE_ITEM(item);
4855 break;
4856 }
4857 if (r == 0)
4858 ignore = TRUE;
4859 }
4860
4861 if (!ignore)
4862 {
4863 if (ga_grow(gap, 1) == OK)
4864 ((void**)gap->ga_data)[gap->ga_len++] = item;
4865 else
4866 {
4867 failed = TRUE;
4868 FREE_ITEM(item);
4869 break;
4870 }
4871 }
4872 else
4873 FREE_ITEM(item);
4874 }
4875
4876 closedir(dirp);
4877 }
4878# endif // MSWIN
4879
4880# undef FREE_ITEM
4881
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004882 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004883 {
4884# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004885 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004886 if (withattr)
4887 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4888 compare_readdirex_item);
4889 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004890 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4891 compare_readdir_item);
4892# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004893 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004894# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004895 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004896
4897 return failed ? FAIL : OK;
4898}
4899
4900/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004901 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004902 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004903 */
4904 int
4905delete_recursive(char_u *name)
4906{
4907 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004908 int i;
4909 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004910 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004911
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004912 // A symbolic link to a directory itself is deleted, not the directory it
4913 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004914 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004915# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004916 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004917# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004918 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004919# endif
4920 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004921 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004922 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004923 if (exp == NULL)
4924 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004925 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004926 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004927 for (i = 0; i < ga.ga_len; ++i)
4928 {
4929 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4930 ((char_u **)ga.ga_data)[i]);
4931 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004932 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004933 }
4934 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004935 }
4936 else
4937 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004938 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004939 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004940 }
4941 else
4942 result = mch_remove(name) == 0 ? 0 : -1;
4943
4944 return result;
4945}
4946#endif
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004949static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004951# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4952/*
4953 * Open temporary directory and take file lock to prevent
4954 * to be auto-cleaned.
4955 */
4956 static void
4957vim_opentempdir(void)
4958{
4959 DIR *dp = NULL;
4960
4961 if (vim_tempdir_dp != NULL)
4962 return;
4963
4964 dp = opendir((const char*)vim_tempdir);
4965
4966 if (dp != NULL)
4967 {
4968 vim_tempdir_dp = dp;
4969 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4970 }
4971}
4972
4973/*
4974 * Close temporary directory - it automatically release file lock.
4975 */
4976 static void
4977vim_closetempdir(void)
4978{
4979 if (vim_tempdir_dp != NULL)
4980 {
4981 closedir(vim_tempdir_dp);
4982 vim_tempdir_dp = NULL;
4983 }
4984}
4985# endif
4986
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987/*
4988 * Delete the temp directory and all files it contains.
4989 */
4990 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004991vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 if (vim_tempdir != NULL)
4994 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004995# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4996 vim_closetempdir();
4997# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004998 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004999 gettail(vim_tempdir)[-1] = NUL;
5000 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005001 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005006 * Directory "tempdir" was created. Expand this name to a full path and put
5007 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5008 * "tempdir" must be no longer than MAXPATHL.
5009 */
5010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005012{
5013 char_u *buf;
5014
Bram Moolenaar964b3742019-05-24 18:54:09 +02005015 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005016 if (buf != NULL)
5017 {
5018 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5019 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005020 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005021 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005022# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5023 vim_opentempdir();
5024# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005025 vim_free(buf);
5026 }
5027}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005028#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005029
5030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * vim_tempname(): Return a unique name that can be used for a temp file.
5032 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005033 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5034 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *
5036 * The returned pointer is to allocated memory.
5037 * The returned pointer is NULL if no valid name was found.
5038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005040vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005042 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005045 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005046#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005047 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#else
5049 char_u itmp[TEMPNAMELEN];
5050#endif
5051
5052#ifdef TEMPDIRNAMES
5053 static char *(tempdirs[]) = {TEMPDIRNAMES};
5054 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005056 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057# endif
5058
5059 /*
5060 * This will create a directory for private use by this instance of Vim.
5061 * This is done once, and the same directory is used for all temp files.
5062 * This method avoids security problems because of symlink attacks et al.
5063 * It's also a bit faster, because we only need to check for an existing
5064 * file when creating the directory and not for each temp file.
5065 */
5066 if (vim_tempdir == NULL)
5067 {
5068 /*
5069 * Try the entries in TEMPDIRNAMES to create the temp directory.
5070 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005071 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005073# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005074 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005075 long nr;
5076 long off;
5077# endif
5078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005079 // Expand $TMP, leave room for "/v1100000/999999999".
5080 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005082 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005085 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
Bram Moolenaareaf03392009-11-17 11:08:52 +00005087# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005088 {
5089# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005090 // Make sure the umask doesn't remove the executable bit.
5091 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005092 mode_t umask_save = umask(077);
5093# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005094 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005095 STRCAT(itmp, "vXXXXXX");
5096 if (mkdtemp((char *)itmp) != NULL)
5097 vim_settempdir(itmp);
5098# if defined(UNIX) || defined(VMS)
5099 (void)umask(umask_save);
5100# endif
5101 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005102# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 // Get an arbitrary number of up to 6 digits. When it's
5104 // unlikely that it already exists it will be faster,
5105 // otherwise it doesn't matter. The use of mkdir() avoids any
5106 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005108 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005110 // Try up to 10000 different values until we find a name that
5111 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 for (off = 0; off < 10000L; ++off)
5113 {
5114 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005115# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
Bram Moolenaareaf03392009-11-17 11:08:52 +00005119 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5120# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005121 // If mkdir() does not set errno to EEXIST, check for
5122 // existing file here. There is a race condition then,
5123 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (mch_stat((char *)itmp, &st) >= 0)
5125 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005126# endif
5127# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005128 // Make sure the umask doesn't remove the executable bit.
5129 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005133# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (r == 0)
5137 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005138 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005141# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005142 // If the mkdir() didn't fail because the file/dir exists,
5143 // we probably can't create any dir here, try another
5144 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (vim_tempdir != NULL)
5151 break;
5152 }
5153 }
5154 }
5155
5156 if (vim_tempdir != NULL)
5157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005158 // There is no need to check if the file exists, because we own the
5159 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5161 return vim_strsave(itmp);
5162 }
5163
5164 return NULL;
5165
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005166#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
Bram Moolenaar4f974752019-02-17 17:44:42 +01005168# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005169 WCHAR wszTempFile[_MAX_PATH + 1];
5170 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005171 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 char_u *retval;
5173 char_u *p;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005174 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005176 wcscpy(itmp, L"");
5177 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005178 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005179 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005180 wszTempFile[1] = L'\\';
5181 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005182 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005183 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005184
5185 // randomize the name to avoid collisions
5186 i = mch_get_pid() + extra_char;
5187 buf4[1] = chartab[i % 36];
5188 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005189 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005191 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005192 // GetTempFileName() will create the file, we don't want that
5193 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005195 // Backslashes in a temp file name cause problems when filtering with
5196 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5197 // didn't set 'shellslash'.
5198 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (*p_shcf == '-' || p_ssl)
5200 for (p = retval; *p; ++p)
5201 if (*p == '\\')
5202 *p = '/';
5203 return retval;
5204
Bram Moolenaar4f974752019-02-17 17:44:42 +01005205# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005208 char_u *p;
5209
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005210 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005211 p = tmpnam((char *)itmp);
5212 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 return NULL;
5214# else
5215 char_u *p;
5216
5217# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005218 // mktemp() is not working on VMS. It seems to be
5219 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 sprintf((char *)itmp, "VIM%c", extra_char);
5221 p = (char_u *)tempnam("tmp:", (char *)itmp);
5222 if (p != NULL)
5223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005224 // VMS will use '.LIS' if we don't explicitly specify an extension,
5225 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 STRCPY(itmp, p);
5227 STRCAT(itmp, ".txt");
5228 free(p);
5229 }
5230 else
5231 return NULL;
5232# else
5233 STRCPY(itmp, TEMPNAME);
5234 if ((p = vim_strchr(itmp, '?')) != NULL)
5235 *p = extra_char;
5236 if (mktemp((char *)itmp) == NULL)
5237 return NULL;
5238# endif
5239# endif
5240
5241 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005242# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005243#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244}
5245
5246#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5247/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005248 * Convert all backslashes in fname to forward slashes in-place, unless when
5249 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 */
5251 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005252forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 char_u *p;
5255
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005256 if (path_with_url(fname))
5257 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005260 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005262 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 *p = '/';
5264}
5265#endif
5266
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005267/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005268 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5269 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5270 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 * Used for autocommands and 'wildignore'.
5272 * Returns TRUE if there is a match, FALSE otherwise.
5273 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005274 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005276 char_u *pattern, // pattern to match with
5277 regprog_T **prog, // pre-compiled regprog or NULL
5278 char_u *fname, // full path of file name
5279 char_u *sfname, // short file name or NULL
5280 char_u *tail, // tail of path
5281 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
5283 regmatch_T regmatch;
5284 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005287 if (prog != NULL)
5288 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005290 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 /*
5293 * Try for a match with the pattern with:
5294 * 1. the full file name, when the pattern has a '/'.
5295 * 2. the short file name, when the pattern has a '/'.
5296 * 3. the tail of the file name, when the pattern has no '/'.
5297 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005298 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 && ((allow_dirs
5300 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5301 || (sfname != NULL
5302 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005303 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 result = TRUE;
5305
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005306 if (prog != NULL)
5307 *prog = regmatch.regprog;
5308 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005309 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return result;
5311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313#if defined(FEAT_WILDIGN) || defined(PROTO)
5314/*
5315 * Return TRUE if a file matches with a pattern in "list".
5316 * "list" is a comma-separated list of patterns, like 'wildignore'.
5317 * "sfname" is the short file name or NULL, "ffname" the long file name.
5318 */
5319 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005320match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 char_u buf[100];
5323 char_u *tail;
5324 char_u *regpat;
5325 char allow_dirs;
5326 int match;
5327 char_u *p;
5328
5329 tail = gettail(sfname);
5330
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005331 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 p = list;
5333 while (*p)
5334 {
5335 copy_option_part(&p, buf, 100, ",");
5336 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5337 if (regpat == NULL)
5338 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005339 match = match_file_pat(regpat, NULL, ffname, sfname,
5340 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 vim_free(regpat);
5342 if (match)
5343 return TRUE;
5344 }
5345 return FALSE;
5346}
5347#endif
5348
5349/*
5350 * Convert the given pattern "pat" which has shell style wildcards in it, into
5351 * a regular expression, and return the result in allocated memory. If there
5352 * is a directory path separator to be matched, then TRUE is put in
5353 * allow_dirs, otherwise FALSE is put there -- webb.
5354 * Handle backslashes before special characters, like "\*" and "\ ".
5355 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 * Returns NULL when out of memory.
5357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359file_pat_to_reg_pat(
5360 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005361 char_u *pat_end, // first char after pattern or NULL
5362 char *allow_dirs, // Result passed back out in here
5363 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005365 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 char_u *endp;
5367 char_u *reg_pat;
5368 char_u *p;
5369 int i;
5370 int nested = 0;
5371 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 if (allow_dirs != NULL)
5374 *allow_dirs = FALSE;
5375 if (pat_end == NULL)
5376 pat_end = pat + STRLEN(pat);
5377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 for (p = pat; p < pat_end; p++)
5379 {
5380 switch (*p)
5381 {
5382 case '*':
5383 case '.':
5384 case ',':
5385 case '{':
5386 case '}':
5387 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005388 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 break;
5390#ifdef BACKSLASH_IN_FILENAME
5391 case '\\':
5392 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005393 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 break;
5395#endif
5396 default:
5397 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005398 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
5400 ++p;
5401 ++size;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 break;
5404 }
5405 }
5406 reg_pat = alloc(size + 1);
5407 if (reg_pat == NULL)
5408 return NULL;
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 if (pat[0] == '*')
5413 while (pat[0] == '*' && pat < pat_end - 1)
5414 pat++;
5415 else
5416 reg_pat[i++] = '^';
5417 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005418 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 while (endp - pat > 0 && *endp == '*')
5421 endp--;
5422 add_dollar = FALSE;
5423 }
5424 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5425 {
5426 switch (*p)
5427 {
5428 case '*':
5429 reg_pat[i++] = '.';
5430 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005431 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005432 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 break;
5434 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 case '~':
5436 reg_pat[i++] = '\\';
5437 reg_pat[i++] = *p;
5438 break;
5439 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 reg_pat[i++] = '.';
5441 break;
5442 case '\\':
5443 if (p[1] == NUL)
5444 break;
5445#ifdef BACKSLASH_IN_FILENAME
5446 if (!no_bslash)
5447 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005448 // translate:
5449 // "\x" to "\\x" e.g., "dir\file"
5450 // "\*" to "\\.*" e.g., "dir\*.c"
5451 // "\?" to "\\." e.g., "dir\??.c"
5452 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5454 && p[1] != '+')
5455 {
5456 reg_pat[i++] = '[';
5457 reg_pat[i++] = '\\';
5458 reg_pat[i++] = '/';
5459 reg_pat[i++] = ']';
5460 if (allow_dirs != NULL)
5461 *allow_dirs = TRUE;
5462 break;
5463 }
5464 }
5465#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005466 // Undo escaping from ExpandEscape():
5467 // foo\?bar -> foo?bar
5468 // foo\%bar -> foo%bar
5469 // foo\,bar -> foo,bar
5470 // foo\ bar -> foo bar
5471 // Don't unescape \, * and others that are also special in a
5472 // regexp.
5473 // An escaped { must be unescaped since we use magic not
5474 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (*++p == '?'
5476#ifdef BACKSLASH_IN_FILENAME
5477 && no_bslash
5478#endif
5479 )
5480 reg_pat[i++] = '?';
5481 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005482 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005483 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005484 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005485 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5486 {
5487 reg_pat[i++] = '\\';
5488 reg_pat[i++] = '{';
5489 p += 2;
5490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
5492 {
5493 if (allow_dirs != NULL && vim_ispathsep(*p)
5494#ifdef BACKSLASH_IN_FILENAME
5495 && (!no_bslash || *p != '\\')
5496#endif
5497 )
5498 *allow_dirs = TRUE;
5499 reg_pat[i++] = '\\';
5500 reg_pat[i++] = *p;
5501 }
5502 break;
5503#ifdef BACKSLASH_IN_FILENAME
5504 case '/':
5505 reg_pat[i++] = '[';
5506 reg_pat[i++] = '\\';
5507 reg_pat[i++] = '/';
5508 reg_pat[i++] = ']';
5509 if (allow_dirs != NULL)
5510 *allow_dirs = TRUE;
5511 break;
5512#endif
5513 case '{':
5514 reg_pat[i++] = '\\';
5515 reg_pat[i++] = '(';
5516 nested++;
5517 break;
5518 case '}':
5519 reg_pat[i++] = '\\';
5520 reg_pat[i++] = ')';
5521 --nested;
5522 break;
5523 case ',':
5524 if (nested)
5525 {
5526 reg_pat[i++] = '\\';
5527 reg_pat[i++] = '|';
5528 }
5529 else
5530 reg_pat[i++] = ',';
5531 break;
5532 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005533 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005535 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 *allow_dirs = TRUE;
5537 reg_pat[i++] = *p;
5538 break;
5539 }
5540 }
5541 if (add_dollar)
5542 reg_pat[i++] = '$';
5543 reg_pat[i] = NUL;
5544 if (nested != 0)
5545 {
5546 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005547 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005549 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005550 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552 return reg_pat;
5553}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005554
5555#if defined(EINTR) || defined(PROTO)
5556/*
5557 * Version of read() that retries when interrupted by EINTR (possibly
5558 * by a SIGWINCH).
5559 */
5560 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005562{
5563 long ret;
5564
5565 for (;;)
5566 {
5567 ret = vim_read(fd, buf, bufsize);
5568 if (ret >= 0 || errno != EINTR)
5569 break;
5570 }
5571 return ret;
5572}
5573
5574/*
5575 * Version of write() that retries when interrupted by EINTR (possibly
5576 * by a SIGWINCH).
5577 */
5578 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005580{
5581 long ret = 0;
5582 long wlen;
5583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005584 // Repeat the write() so long it didn't fail, other than being interrupted
5585 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005586 while (ret < (long)bufsize)
5587 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005588 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005589 if (wlen < 0)
5590 {
5591 if (errno != EINTR)
5592 break;
5593 }
5594 else
5595 ret += wlen;
5596 }
5597 return ret;
5598}
5599#endif