blob: 9eecb0108d4316877ecad1276af1e7aaae1c02ec [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);
2283 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002284 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002285 }
2286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 vim_free(buffer);
2288
2289#ifdef HAVE_DUP
2290 if (read_stdin)
2291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002294 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296#endif
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (tmpname != NULL)
2299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 vim_free(tmpname);
2302 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002303 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 /*
2306 * In recovery mode everything but autocommands is skipped.
2307 */
2308 if (!recoverymode)
2309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2312 {
2313#ifdef FEAT_NETBEANS_INTG
2314 netbeansFireChanges = 0;
2315#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002316 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_NETBEANS_INTG
2318 netbeansFireChanges = 1;
2319#endif
2320 --linecnt;
2321 }
2322 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2323 if (filesize == 0)
2324 linecnt = 0;
2325 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002328#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 // After reading the text into the buffer the diff info needs to
2330 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002331 diff_invalidate(curbuf);
2332#endif
2333#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 // All folds in the window are invalid now. Mark them for update
2335 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002336 foldUpdateAll(curwin);
2337#endif
2338 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 appended_lines_mark(from, linecnt);
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342#ifndef ALWAYS_USE_GUI
2343 /*
2344 * If we were reading from the same terminal as where messages go,
2345 * the screen will have been messed up.
2346 * Switch on raw mode now and clear the screen.
2347 */
2348 if (read_stdin)
2349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 starttermcap();
2352 screenclear();
2353 }
2354#endif
2355
2356 if (got_int)
2357 {
2358 if (!(flags & READ_DUMMY))
2359 {
2360 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2361 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364 msg_scroll = msg_save;
2365#ifdef FEAT_VIMINFO
2366 check_marks_read();
2367#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002368 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370
2371 if (!filtering && !(flags & READ_DUMMY))
2372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002373 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 c = FALSE;
2375
2376#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002377 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 STRCAT(IObuff, _("[fifo]"));
2380 c = TRUE;
2381 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 STRCAT(IObuff, _("[socket]"));
2385 c = TRUE;
2386 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002387# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002388 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002389 {
2390 STRCAT(IObuff, _("[character special]"));
2391 c = TRUE;
2392 }
2393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#endif
2395 if (curbuf->b_p_ro)
2396 {
2397 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2398 c = TRUE;
2399 }
2400 if (read_no_eol_lnum)
2401 {
2402 msg_add_eol();
2403 c = TRUE;
2404 }
2405 if (ff_error == EOL_DOS)
2406 {
2407 STRCAT(IObuff, _("[CR missing]"));
2408 c = TRUE;
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (split)
2411 {
2412 STRCAT(IObuff, _("[long lines split]"));
2413 c = TRUE;
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (notconverted)
2416 {
2417 STRCAT(IObuff, _("[NOT converted]"));
2418 c = TRUE;
2419 }
2420 else if (converted)
2421 {
2422 STRCAT(IObuff, _("[converted]"));
2423 c = TRUE;
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#ifdef FEAT_CRYPT
2426 if (cryptkey != NULL)
2427 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002428 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 c = TRUE;
2430 }
2431#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002432 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002434 sprintf((char *)IObuff + STRLEN(IObuff),
2435 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 c = TRUE;
2437 }
2438 else if (illegal_byte > 0)
2439 {
2440 sprintf((char *)IObuff + STRLEN(IObuff),
2441 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2442 c = TRUE;
2443 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002444 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 STRCAT(IObuff, _("[READ ERRORS]"));
2447 c = TRUE;
2448 }
2449 if (msg_add_fileformat(fileformat))
2450 c = TRUE;
2451#ifdef FEAT_CRYPT
2452 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002453 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002454 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 else
2456#endif
2457 msg_add_lines(c, (long)linecnt, filesize);
2458
Bram Moolenaard23a8232018-02-10 18:45:26 +01002459 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 msg_scrolled_ign = TRUE;
2461#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002462 // Don't show the message when reading stdin, it would end up in a
2463 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (read_stdin || read_buffer)
2465 p = msg_may_trunc(FALSE, IObuff);
2466 else
2467#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002468 {
2469 if (msg_col > 0)
2470 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002471 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002474 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002475 // Need to repeat the message after redrawing when:
2476 // - When reading from stdin (the screen will be cleared next).
2477 // - When restart_edit is set (otherwise there will be a delay
2478 // before redrawing).
2479 // - When the screen was scrolled but there is no wait-return
2480 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002481 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 msg_scrolled_ign = FALSE;
2483 }
2484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002485 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002487 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002488 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 curbuf->b_p_ro = TRUE;
2490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002491 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 /*
2494 * In Ex mode: cursor at last new line.
2495 * Otherwise: cursor at first new line.
2496 */
2497 if (exmode_active)
2498 curwin->w_cursor.lnum = from + linecnt;
2499 else
2500 curwin->w_cursor.lnum = from + 1;
2501 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002502 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
Bram Moolenaare1004402020-10-24 20:49:43 +02002504 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002505 {
2506 // Set '[ and '] marks to the newly read lines.
2507 curbuf->b_op_start.lnum = from + 1;
2508 curbuf->b_op_start.col = 0;
2509 curbuf->b_op_end.lnum = from + linecnt;
2510 curbuf->b_op_end.col = 0;
2511 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002512
Bram Moolenaar4f974752019-02-17 17:44:42 +01002513#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002514 /*
2515 * Work around a weird problem: When a file has two links (only
2516 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002517 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002518 * It's correct again after reading the file, thus reset the timestamp
2519 * here.
2520 */
2521 if (newfile && !read_stdin && !read_buffer
2522 && mch_stat((char *)fname, &st) >= 0)
2523 {
2524 buf_store_time(curbuf, &st, fname);
2525 curbuf->b_mtime_read = curbuf->b_mtime;
2526 }
2527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529 msg_scroll = msg_save;
2530
2531#ifdef FEAT_VIMINFO
2532 /*
2533 * Get the marks before executing autocommands, so they can be used there.
2534 */
2535 check_marks_read();
2536#endif
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002539 * We remember if the last line of the read didn't have
2540 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2541 * or writing the read again with 'binary' on. The latter is required
2542 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002544 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002546 // When reloading a buffer put the cursor at the first line that is
2547 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002548 if (flags & READ_KEEP_UNDO)
2549 u_find_first_changed();
2550
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002551#ifdef FEAT_PERSISTENT_UNDO
2552 /*
2553 * When opening a new file locate undo info and read it.
2554 */
2555 if (read_undo_file)
2556 {
2557 char_u hash[UNDO_HASH_SIZE];
2558
2559 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002560 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002561 }
2562#endif
2563
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002564 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 int m = msg_scroll;
2567 int n = msg_scrolled;
2568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 // Save the fileformat now, otherwise the buffer will be considered
2570 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002571 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 save_file_ff(curbuf);
2573
2574 /*
2575 * The output from the autocommands should not overwrite anything and
2576 * should not be overwritten: Set msg_scroll, restore its value if no
2577 * output was done.
2578 */
2579 msg_scroll = TRUE;
2580 if (filtering)
2581 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2582 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002583 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2586 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002587 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2588 /*
2589 * EVENT_FILETYPE was not triggered but the buffer already has a
2590 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2591 */
2592 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2593 TRUE, curbuf);
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 else
2596 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2597 FALSE, NULL, eap);
2598 if (msg_scrolled == n)
2599 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002600# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002601 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
2606 if (recoverymode && error)
2607 return FAIL;
2608 return OK;
2609}
2610
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002611#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002612/*
2613 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2614 * which is the name of files used for process substitution output by
2615 * some shells on some operating systems, e.g., bash on SunOS.
2616 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2617 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002619is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002620{
2621 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2622 && VIM_ISDIGIT(fname[8])
2623 && *skipdigits(fname + 9) == NUL
2624 && (fname[9] != NUL
2625 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2626}
2627#endif
2628
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002629/*
2630 * From the current line count and characters read after that, estimate the
2631 * line number where we are now.
2632 * Used for error messages that include a line number.
2633 */
2634 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002635readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002636 linenr_T linecnt, // line count before reading more bytes
2637 char_u *p, // start of more bytes read
2638 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002639{
2640 char_u *s;
2641 linenr_T lnum;
2642
2643 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2644 for (s = p; s < endp; ++s)
2645 if (*s == '\n')
2646 ++lnum;
2647 return lnum;
2648}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002651 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2652 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 * Returns OK or FAIL.
2654 */
2655 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002656prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
Bram Moolenaar13505972019-01-24 15:04:48 +01002658 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (eap->cmd == NULL)
2660 return FAIL;
2661
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002662 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2663 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002664 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002665 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002666
2667 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002668 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002669 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return OK;
2671}
2672
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002673/*
2674 * Set default or forced 'fileformat' and 'binary'.
2675 */
2676 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002677set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002678{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002679 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002680 if (set_options)
2681 {
2682 if (eap != NULL && eap->force_ff != 0)
2683 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2684 else if (*p_ffs != NUL)
2685 set_fileformat(default_fileformat(), OPT_LOCAL);
2686 }
2687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002688 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002689 if (eap != NULL && eap->force_bin != 0)
2690 {
2691 int oldval = curbuf->b_p_bin;
2692
2693 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2694 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2695 }
2696}
2697
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002698/*
2699 * Set forced 'fileencoding'.
2700 */
2701 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002702set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002703{
2704 if (eap->force_enc != 0)
2705 {
2706 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2707
2708 if (fenc != NULL)
2709 set_string_option_direct((char_u *)"fenc", -1,
2710 fenc, OPT_FREE|OPT_LOCAL, 0);
2711 vim_free(fenc);
2712 }
2713}
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715/*
2716 * Find next fileencoding to use from 'fileencodings'.
2717 * "pp" points to fenc_next. It's advanced to the next item.
2718 * When there are no more items, an empty string is returned and *pp is set to
2719 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002720 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2721 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 */
2723 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002724next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 char_u *p;
2727 char_u *r;
2728
Bram Moolenaarf077db22019-08-13 00:18:24 +02002729 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (**pp == NUL)
2731 {
2732 *pp = NULL;
2733 return (char_u *)"";
2734 }
2735 p = vim_strchr(*pp, ',');
2736 if (p == NULL)
2737 {
2738 r = enc_canonize(*pp);
2739 *pp += STRLEN(*pp);
2740 }
2741 else
2742 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002743 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 *pp = p + 1;
2745 if (r != NULL)
2746 {
2747 p = enc_canonize(r);
2748 vim_free(r);
2749 r = p;
2750 }
2751 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002752 if (r != NULL)
2753 *alloced = TRUE;
2754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002756 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 r = (char_u *)"";
2758 *pp = NULL;
2759 }
2760 return r;
2761}
2762
Bram Moolenaar13505972019-01-24 15:04:48 +01002763#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764/*
2765 * Convert a file with the 'charconvert' expression.
2766 * This closes the file which is to be read, converts it and opens the
2767 * resulting file for reading.
2768 * Returns name of the resulting converted file (the caller should delete it
2769 * after reading it).
2770 * Returns NULL if the conversion failed ("*fdp" is not set) .
2771 */
2772 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002773readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002774 char_u *fname, // name of input file
2775 char_u *fenc, // converted from
2776 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002779 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002781 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002783 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
2785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002786 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 *fdp = -1;
2788 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2789 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002790 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2792 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002793 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
2795
2796 if (errmsg != NULL)
2797 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002798 // Don't use emsg(), it breaks mappings, the retry with
2799 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002800 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (tmpname != NULL)
2802 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002803 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002804 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 }
2807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002808 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 if (*fdp < 0)
2810 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2811
2812 return tmpname;
2813}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#endif
2815
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002816#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002818 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2820 * *filesizep are updated.
2821 * Return the (new) encryption key, NULL for no encryption.
2822 */
2823 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002824check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002825 char_u *cryptkey, // previous encryption key or NULL
2826 char_u *ptr, // pointer to read bytes
2827 long *sizep, // length of read bytes
2828 off_T *filesizep, // nr of bytes used from file
2829 int newfile, // editing a new buffer
2830 char_u *fname, // file name to display
2831 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002833 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002834 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002835
2836 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002838 // Mark the buffer as read-only until the decryption has taken place.
2839 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002840 curbuf->b_p_ro = TRUE;
2841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002842 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002843 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002844 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846 if (*curbuf->b_p_key)
2847 cryptkey = curbuf->b_p_key;
2848 else
2849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002850 // When newfile is TRUE, store the typed key in the 'key'
2851 // option and don't free it. bf needs hash of the key saved.
2852 // Don't ask for the key again when first time Enter was hit.
2853 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002854 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002855 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002856 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002857 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002858 *did_ask = TRUE;
2859
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002860 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (cryptkey != NULL && *cryptkey == NUL)
2862 {
2863 if (cryptkey != curbuf->b_p_key)
2864 vim_free(cryptkey);
2865 cryptkey = NULL;
2866 }
2867 }
2868 }
2869
2870 if (cryptkey != NULL)
2871 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002872 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002873
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002874 curbuf->b_cryptstate = crypt_create_from_header(
2875 method, cryptkey, ptr);
2876 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002878 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002879 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002880 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002881 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002882 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002883 *filesizep += header_len;
2884 *sizep -= header_len;
2885 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002887 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002888 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
2890 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 // When starting to edit a new file which does not have encryption, clear
2892 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002893 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2895
2896 return cryptkey;
2897}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002898#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002901 * Return TRUE if a file appears to be read-only from the file permissions.
2902 */
2903 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002904check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002905 char_u *fname, // full path to file
2906 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002907{
2908#ifndef USE_MCH_ACCESS
2909 int fd = 0;
2910#endif
2911
2912 return (
2913#ifdef USE_MCH_ACCESS
2914# ifdef UNIX
2915 (perm & 0222) == 0 ||
2916# endif
2917 mch_access((char *)fname, W_OK)
2918#else
2919 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2920 ? TRUE : (close(fd), FALSE)
2921#endif
2922 );
2923}
2924
Bram Moolenaara7870192019-02-14 12:56:36 +01002925#if defined(HAVE_FSYNC) || defined(PROTO)
2926/*
2927 * Call fsync() with Mac-specific exception.
2928 * Return fsync() result: zero for success.
2929 */
2930 int
2931vim_fsync(int fd)
2932{
2933 int r;
2934
2935# ifdef MACOS_X
2936 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002937 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002938# endif
2939 r = fsync(fd);
2940 return r;
2941}
2942#endif
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002945 * Set the name of the current buffer. Use when the buffer doesn't have a
2946 * name and a ":r" or ":w" command with a file name is used.
2947 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002948 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002949set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002950{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002951 buf_T *buf = curbuf;
2952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002953 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002954 if (curbuf->b_p_bl)
2955 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2956 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002957#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002958 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002959 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002960#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002961 if (curbuf != buf)
2962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002965 return FAIL;
2966 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002967
2968 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2969 curbuf->b_flags |= BF_NOTEDITED;
2970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002971 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002972 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2973 if (curbuf->b_p_bl)
2974 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002975#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002976 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002977 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002978#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002979
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002980 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002981 if (*curbuf->b_p_ft == NUL)
2982 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002983 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002984 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002985 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002986 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002987
2988 return OK;
2989}
2990
2991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 * Put file name into IObuff with quotes.
2993 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 if (fname == NULL)
2998 fname = (char_u *)"-stdin-";
2999 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3000 IObuff[0] = '"';
3001 STRCAT(IObuff, "\" ");
3002}
3003
3004/*
3005 * Append message for text mode to IObuff.
3006 * Return TRUE if something appended.
3007 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003008 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003009msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011#ifndef USE_CRNL
3012 if (eol_type == EOL_DOS)
3013 {
3014 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3015 return TRUE;
3016 }
3017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (eol_type == EOL_MAC)
3019 {
3020 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3021 return TRUE;
3022 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003023#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (eol_type == EOL_UNIX)
3025 {
3026 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3027 return TRUE;
3028 }
3029#endif
3030 return FALSE;
3031}
3032
3033/*
3034 * Append line and character count to IObuff.
3035 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003036 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003037msg_add_lines(
3038 int insert_space,
3039 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003040 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 char_u *p;
3043
3044 p = IObuff + STRLEN(IObuff);
3045
3046 if (insert_space)
3047 *p++ = ' ';
3048 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003049 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003050 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 else
3052 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003053 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003055 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003056 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003057 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059}
3060
3061/*
3062 * Append message for missing line separator to IObuff.
3063 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003065msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3068}
3069
Bram Moolenaar473952e2019-09-28 16:30:04 +02003070 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003071time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003073#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003074 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3075 // the seconds. Since the roundoff is done when flushing the inode, the
3076 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 return (t1 - t2 > 1 || t2 - t1 > 1);
3078#else
3079 return (t1 != t2);
3080#endif
3081}
3082
3083/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003084 * Return TRUE if file encoding "fenc" requires conversion from or to
3085 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003090 int same_encoding;
3091 int enc_flags;
3092 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003094 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003095 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003096 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003097 fenc_flags = 0;
3098 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003099 else
3100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003101 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3102 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003103 enc_flags = get_fio_flags(p_enc);
3104 fenc_flags = get_fio_flags(fenc);
3105 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3106 }
3107 if (same_encoding)
3108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003109 // Specified encoding matches with 'encoding'. This requires
3110 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003111 return enc_unicode != 0;
3112 }
3113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003114 // Encodings differ. However, conversion is not needed when 'enc' is any
3115 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003116 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117}
3118
3119/*
3120 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3121 * internal conversion.
3122 * if "ptr" is an empty string, use 'encoding'.
3123 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003124 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003125get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 int prop;
3128
3129 if (*ptr == NUL)
3130 ptr = p_enc;
3131
3132 prop = enc_canon_props(ptr);
3133 if (prop & ENC_UNICODE)
3134 {
3135 if (prop & ENC_2BYTE)
3136 {
3137 if (prop & ENC_ENDIAN_L)
3138 return FIO_UCS2 | FIO_ENDIAN_L;
3139 return FIO_UCS2;
3140 }
3141 if (prop & ENC_4BYTE)
3142 {
3143 if (prop & ENC_ENDIAN_L)
3144 return FIO_UCS4 | FIO_ENDIAN_L;
3145 return FIO_UCS4;
3146 }
3147 if (prop & ENC_2WORD)
3148 {
3149 if (prop & ENC_ENDIAN_L)
3150 return FIO_UTF16 | FIO_ENDIAN_L;
3151 return FIO_UTF16;
3152 }
3153 return FIO_UTF8;
3154 }
3155 if (prop & ENC_LATIN1)
3156 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003157 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return 0;
3159}
3160
Bram Moolenaar473952e2019-09-28 16:30:04 +02003161#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162/*
3163 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3164 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3165 * Used for conversion between 'encoding' and 'fileencoding'.
3166 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003167 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003168get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 int cp;
3171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003172 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (!enc_utf8 && enc_codepage <= 0)
3174 return 0;
3175
3176 cp = encname2codepage(ptr);
3177 if (cp == 0)
3178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003179# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (STRCMP(ptr, "utf-8") == 0)
3181 cp = CP_UTF8;
3182 else
3183# endif
3184 return 0;
3185 }
3186 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3187}
3188#endif
3189
Bram Moolenaar473952e2019-09-28 16:30:04 +02003190#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191/*
3192 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3193 * needed for the internal conversion to/from utf-8 or latin1.
3194 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003195 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3199 && (enc_canon_props(ptr) & ENC_MACROMAN))
3200 return FIO_MACROMAN;
3201 return 0;
3202}
3203#endif
3204
3205/*
3206 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3207 * "size" must be at least 2.
3208 * Return the name of the encoding and set "*lenp" to the length.
3209 * Returns NULL when no BOM found.
3210 */
3211 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003212check_for_bom(
3213 char_u *p,
3214 long size,
3215 int *lenp,
3216 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
3218 char *name = NULL;
3219 int len = 2;
3220
3221 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003222 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003224 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 len = 3;
3226 }
3227 else if (p[0] == 0xff && p[1] == 0xfe)
3228 {
3229 if (size >= 4 && p[2] == 0 && p[3] == 0
3230 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3231 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003232 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 len = 4;
3234 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003235 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003236 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003237 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003238 // utf-16le is preferred, it also works for ucs-2le text
3239 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
3241 else if (p[0] == 0xfe && p[1] == 0xff
3242 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003244 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003245 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003246 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003247 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003248 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3251 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003253 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 len = 4;
3255 }
3256
3257 *lenp = len;
3258 return (char_u *)name;
3259}
3260
3261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 * Try to find a shortname by comparing the fullname with the current
3263 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003264 * Returns "full_path" or pointer into "full_path" if shortened.
3265 */
3266 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003267shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003268{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003269 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003270 char_u *p = full_path;
3271
Bram Moolenaard9462e32011-04-11 21:35:11 +02003272 dirname = alloc(MAXPATHL);
3273 if (dirname == NULL)
3274 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003275 if (mch_dirname(dirname, MAXPATHL) == OK)
3276 {
3277 p = shorten_fname(full_path, dirname);
3278 if (p == NULL || *p == NUL)
3279 p = full_path;
3280 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003281 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003282 return p;
3283}
3284
3285/*
3286 * Try to find a shortname by comparing the fullname with the current
3287 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * Returns NULL if not shorter name possible, pointer into "full_path"
3289 * otherwise.
3290 */
3291 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003292shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 int len;
3295 char_u *p;
3296
3297 if (full_path == NULL)
3298 return NULL;
3299 len = (int)STRLEN(dir_name);
3300 if (fnamencmp(dir_name, full_path, len) == 0)
3301 {
3302 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003303#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003305 * MS-Windows: when a file is in the root directory, dir_name will end
3306 * in a slash, since C: by itself does not define a specific dir. In
3307 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 */
3309 if (!((len > 2) && (*(p - 2) == ':')))
3310#endif
3311 {
3312 if (vim_ispathsep(*p))
3313 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003314#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 else
3316 p = NULL;
3317#endif
3318 }
3319 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003320#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /*
3322 * When using a file in the current drive, remove the drive name:
3323 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3324 * a floppy from "A:\dir" to "B:\dir".
3325 */
3326 else if (len > 3
3327 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3328 && full_path[1] == ':'
3329 && vim_ispathsep(full_path[2]))
3330 p = full_path + 2;
3331#endif
3332 else
3333 p = NULL;
3334 return p;
3335}
3336
3337/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003338 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * When "force" is TRUE: Use full path from now on for files currently being
3340 * edited, both for file name and swap file name. Try to shorten the file
3341 * names a bit, if safe to do so.
3342 * When "force" is FALSE: Only try to shorten absolute file names.
3343 * For buffers that have buftype "nofile" or "scratch": never change the file
3344 * name.
3345 */
3346 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003347shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3348{
3349 char_u *p;
3350
3351 if (buf->b_fname != NULL
3352#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003353 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003354#endif
3355 && !path_with_url(buf->b_fname)
3356 && (force
3357 || buf->b_sfname == NULL
3358 || mch_isFullName(buf->b_sfname)))
3359 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003360 if (buf->b_sfname != buf->b_ffname)
3361 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003362 p = shorten_fname(buf->b_ffname, dirname);
3363 if (p != NULL)
3364 {
3365 buf->b_sfname = vim_strsave(p);
3366 buf->b_fname = buf->b_sfname;
3367 }
3368 if (p == NULL || buf->b_fname == NULL)
3369 buf->b_fname = buf->b_ffname;
3370 }
3371}
3372
3373/*
3374 * Shorten filenames for all buffers.
3375 */
3376 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003377shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 char_u dirname[MAXPATHL];
3380 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003383 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003385 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003387 // Always make the swap file name a full path, a "nofile" buffer may
3388 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003389 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003392 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003393#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003394 popup_update_preview_title();
3395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396}
3397
3398#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3399 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003400 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 || defined(PROTO)
3402/*
3403 * Shorten all filenames in "fnames[count]" by current directory.
3404 */
3405 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003406shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 int i;
3409 char_u dirname[MAXPATHL];
3410 char_u *p;
3411
3412 if (fnames == NULL || count < 1)
3413 return;
3414 mch_dirname(dirname, sizeof(dirname));
3415 for (i = 0; i < count; ++i)
3416 {
3417 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003419 // shorten_fname() returns pointer in given "fnames[i]". If free
3420 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3421 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 p = vim_strsave(p);
3423 vim_free(fnames[i]);
3424 fnames[i] = p;
3425 }
3426 }
3427}
3428#endif
3429
3430/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003431 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 * fo_o_h.ext for MSDOS or when shortname option set.
3433 *
3434 * Assumed that fname is a valid name found in the filesystem we assure that
3435 * the return value is a different name and ends in 'ext'.
3436 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3437 * characters otherwise.
3438 * Space for the returned name is allocated, must be freed later.
3439 * Returns NULL when out of memory.
3440 */
3441 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003442modname(
3443 char_u *fname,
3444 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003445 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003447 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 fname, ext, prepend_dot);
3449}
3450
3451 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003453 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003454 char_u *fname,
3455 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003456 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 char_u *retval;
3459 char_u *s;
3460 char_u *e;
3461 char_u *ptr;
3462 int fnamelen, extlen;
3463
3464 extlen = (int)STRLEN(ext);
3465
3466 /*
3467 * If there is no file name we must get the name of the current directory
3468 * (we need the full path in case :cd is used).
3469 */
3470 if (fname == NULL || *fname == NUL)
3471 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003472 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (retval == NULL)
3474 return NULL;
3475 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3476 (fnamelen = (int)STRLEN(retval)) == 0)
3477 {
3478 vim_free(retval);
3479 return NULL;
3480 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003481 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 retval[fnamelen++] = PATHSEP;
3484 retval[fnamelen] = NUL;
3485 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003486 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 else
3489 {
3490 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003491 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (retval == NULL)
3493 return NULL;
3494 STRCPY(retval, fname);
3495#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003496 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#endif
3498 }
3499
3500 /*
3501 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3502 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3503 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3504 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3505 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003506 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003508 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003509 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003512 {
3513 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003518 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3520 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522 s = ptr + STRLEN(ptr);
3523
3524 /*
3525 * For 8.3 file names we may have to reduce the length.
3526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 /*
3530 * If there is no file name, or the file name ends in '/', and the
3531 * extension starts with '.', put a '_' before the dot, because just
3532 * ".ext" is invalid.
3533 */
3534 if (fname == NULL || *fname == NUL
3535 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 *s++ = '_';
3539 }
3540 /*
3541 * If the extension starts with '.', truncate the base name at 8
3542 * characters
3543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003546 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
3548 s = ptr + 8;
3549 *s = '\0';
3550 }
3551 }
3552 /*
3553 * If the extension doesn't start with '.', and the file name
3554 * doesn't have an extension yet, append a '.'
3555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else if ((e = vim_strchr(ptr, '.')) == NULL)
3557 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 /*
3559 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003560 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 */
3562 else if ((int)STRLEN(e) + extlen > 4)
3563 s = e + 4 - extlen;
3564 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003565#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 /*
3567 * If there is no file name, and the extension starts with '.', put a
3568 * '_' before the dot, because just ".ext" may be invalid if it's on a
3569 * FAT partition, and on HPFS it doesn't matter.
3570 */
3571 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3572 *s++ = '_';
3573#endif
3574
3575 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003576 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 * ext can start with '.' and cannot exceed 3 more characters.
3578 */
3579 STRCPY(s, ext);
3580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 /*
3582 * Prepend the dot.
3583 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003584 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003586 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 /*
3591 * Check that, after appending the extension, the file name is really
3592 * different.
3593 */
3594 if (fname != NULL && STRCMP(fname, retval) == 0)
3595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003596 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 while (--s >= ptr)
3598 {
3599 if (*s != '_')
3600 {
3601 *s = '_';
3602 break;
3603 }
3604 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003605 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 *ptr = 'v';
3607 }
3608 return retval;
3609}
3610
3611/*
3612 * Like fgets(), but if the file line is too long, it is truncated and the
3613 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003614 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 */
3616 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003617vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
3619 char *eof;
3620#define FGETS_SIZE 200
3621 char tbuf[FGETS_SIZE];
3622
3623 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3626 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003627 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003629 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 do
3631 {
3632 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003633 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3635 }
3636 return (eof == NULL);
3637}
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639/*
3640 * rename() only works if both files are on the same file system, this
3641 * function will (attempts to?) copy the file across if rename fails -- webb
3642 * Return -1 for failure, 0 for success.
3643 */
3644 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003645vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 int fd_in;
3648 int fd_out;
3649 int n;
3650 char *errmsg = NULL;
3651 char *buffer;
3652#ifdef AMIGA
3653 BPTR flock;
3654#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003655 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003656 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003657#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003658 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003659#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003660 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003663 * When the names are identical, there is nothing to do. When they refer
3664 * to the same file (ignoring case and slash/backslash differences) but
3665 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003668 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003669 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003670 use_tmp_file = TRUE;
3671 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003672 return 0;
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 /*
3676 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3677 */
3678 if (mch_stat((char *)from, &st) < 0)
3679 return -1;
3680
Bram Moolenaar3576da72008-12-30 15:15:57 +00003681#ifdef UNIX
3682 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003683 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003685 // It's possible for the source and destination to be the same file.
3686 // This happens when "from" and "to" differ in case and are on a FAT32
3687 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003688 if (mch_stat((char *)to, &st_to) >= 0
3689 && st.st_dev == st_to.st_dev
3690 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003691 use_tmp_file = TRUE;
3692 }
3693#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003694#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003695 {
3696 BY_HANDLE_FILE_INFORMATION info1, info2;
3697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003698 // It's possible for the source and destination to be the same file.
3699 // In that case go through a temp file name. This makes rename("foo",
3700 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003701 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3702 && win32_fileinfo(to, &info2) == FILEINFO_OK
3703 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3704 && info1.nFileIndexHigh == info2.nFileIndexHigh
3705 && info1.nFileIndexLow == info2.nFileIndexLow)
3706 use_tmp_file = TRUE;
3707 }
3708#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003709
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003710 if (use_tmp_file)
3711 {
3712 char tempname[MAXPATHL + 1];
3713
3714 /*
3715 * Find a name that doesn't exist and is in the same directory.
3716 * Rename "from" to "tempname" and then rename "tempname" to "to".
3717 */
3718 if (STRLEN(from) >= MAXPATHL - 5)
3719 return -1;
3720 STRCPY(tempname, from);
3721 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003722 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003723 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3724 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003725 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003726 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003727 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003728 if (mch_rename(tempname, (char *)to) == 0)
3729 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003730 // Strange, the second step failed. Try moving the
3731 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003732 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003733 return -1;
3734 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003735 // If it fails for one temp name it will most likely fail
3736 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003737 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003738 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003739 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003740 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003741 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 /*
3744 * Delete the "to" file, this is required on some systems to make the
3745 * mch_rename() work, on other systems it makes sure that we don't have
3746 * two files when the mch_rename() fails.
3747 */
3748
3749#ifdef AMIGA
3750 /*
3751 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3752 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003753 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 * deleting the "from" file (horror!) we lock it during the remove.
3755 *
3756 * When used for making a backup before writing the file: This should not
3757 * happen with ":w", because startscript() should detect this problem and
3758 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3759 * name. This problem does exist with ":w filename", but then the
3760 * original file will be somewhere else so the backup isn't really
3761 * important. If autoscripting is off the rename may fail.
3762 */
3763 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3764#endif
3765 mch_remove(to);
3766#ifdef AMIGA
3767 if (flock)
3768 UnLock(flock);
3769#endif
3770
3771 /*
3772 * First try a normal rename, return if it works.
3773 */
3774 if (mch_rename((char *)from, (char *)to) == 0)
3775 return 0;
3776
3777 /*
3778 * Rename() failed, try copying the file.
3779 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003780 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003781#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003782 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003783 acl = mch_get_acl(from);
3784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3786 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003787 {
3788#ifdef HAVE_ACL
3789 mch_free_acl(acl);
3790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003792 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003794 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003795 fd_out = mch_open((char *)to,
3796 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (fd_out == -1)
3798 {
3799 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003800#ifdef HAVE_ACL
3801 mch_free_acl(acl);
3802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return -1;
3804 }
3805
Bram Moolenaar473952e2019-09-28 16:30:04 +02003806 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (buffer == NULL)
3808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003810 close(fd_in);
3811#ifdef HAVE_ACL
3812 mch_free_acl(acl);
3813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return -1;
3815 }
3816
Bram Moolenaar473952e2019-09-28 16:30:04 +02003817 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003818 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
3820 errmsg = _("E208: Error writing to \"%s\"");
3821 break;
3822 }
3823
3824 vim_free(buffer);
3825 close(fd_in);
3826 if (close(fd_out) < 0)
3827 errmsg = _("E209: Error closing \"%s\"");
3828 if (n < 0)
3829 {
3830 errmsg = _("E210: Error reading \"%s\"");
3831 to = from;
3832 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003833#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003834 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003835#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003836#ifdef HAVE_ACL
3837 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003838 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003839#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003840#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003841 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (errmsg != NULL)
3844 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003845 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return -1;
3847 }
3848 mch_remove(from);
3849 return 0;
3850}
3851
3852static int already_warned = FALSE;
3853
3854/*
3855 * Check if any not hidden buffer has been changed.
3856 * Postpone the check if there are characters in the stuff buffer, a global
3857 * command is being executed, a mapping is being executed or an autocommand is
3858 * busy.
3859 * Returns TRUE if some message was written (screen should be redrawn and
3860 * cursor positioned).
3861 */
3862 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003863check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003864 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 buf_T *buf;
3867 int didit = 0;
3868 int n;
3869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003870 // Don't check timestamps while system() or another low-level function may
3871 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (no_check_timestamps > 0)
3873 return FALSE;
3874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003875 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3876 // event and we would keep on checking if the file is steadily growing.
3877 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (focus && did_check_timestamps)
3879 {
3880 need_check_timestamps = TRUE;
3881 return FALSE;
3882 }
3883
3884 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003885 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003886 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 else
3888 {
3889 ++no_wait_return;
3890 did_check_timestamps = TRUE;
3891 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003892 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (buf->b_nwindows > 0)
3896 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003897 bufref_T bufref;
3898
3899 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 n = buf_check_timestamp(buf, focus);
3901 if (didit < n)
3902 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003903 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // Autocommands have removed the buffer, start at the
3906 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 buf = firstbuf;
3908 continue;
3909 }
3910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912 --no_wait_return;
3913 need_check_timestamps = FALSE;
3914 if (need_wait_return && didit == 2)
3915 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003916 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003917 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 out_flush();
3919 }
3920 }
3921 return didit;
3922}
3923
3924/*
3925 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3926 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3927 * empty.
3928 */
3929 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003930move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
3932 buf_T *tbuf = curbuf;
3933 int retval = OK;
3934 linenr_T lnum;
3935 char_u *p;
3936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003937 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 curbuf = tobuf;
3939 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3940 {
3941 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3942 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3943 {
3944 vim_free(p);
3945 retval = FAIL;
3946 break;
3947 }
3948 vim_free(p);
3949 }
3950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003951 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (retval != FAIL)
3953 {
3954 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003955 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003956 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003958 // Oops! We could try putting back the saved lines, but that
3959 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 retval = FAIL;
3961 break;
3962 }
3963 }
3964
3965 curbuf = tbuf;
3966 return retval;
3967}
3968
3969/*
3970 * Check if buffer "buf" has been changed.
3971 * Also check if the file for a new buffer unexpectedly appeared.
3972 * return 1 if a changed buffer was found.
3973 * return 2 if a message has been displayed.
3974 * return 0 otherwise.
3975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003977buf_check_timestamp(
3978 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003979 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003981 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 int stat_res;
3983 int retval = 0;
3984 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003985 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003987 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int helpmesg = FALSE;
3989 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003990 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3992 int can_reload = FALSE;
3993#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003994 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 int orig_mode = buf->b_orig_mode;
3996#ifdef FEAT_GUI
3997 int save_mouse_correct = need_mouse_correct;
3998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004000 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004001#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004002 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004003#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004004 bufref_T bufref;
4005
4006 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004008 // If there is no file name, the buffer is not loaded, 'buftype' is
4009 // set, we are in the middle of a save or being called recursively: ignore
4010 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (buf->b_ffname == NULL
4012 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004013 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004016#ifdef FEAT_NETBEANS_INTG
4017 || isNetbeansBuffer(buf)
4018#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004019#ifdef FEAT_TERMINAL
4020 || buf->b_term != NULL
4021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 )
4023 return 0;
4024
4025 if ( !(buf->b_flags & BF_NOTEDITED)
4026 && buf->b_mtime != 0
4027 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4028 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004029 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#ifdef HAVE_ST_MODE
4031 || (int)st.st_mode != buf->b_orig_mode
4032#else
4033 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4034#endif
4035 ))
4036 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004037 long prev_b_mtime = buf->b_mtime;
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 retval = 1;
4040
Bram Moolenaar386bc822018-07-07 18:34:12 +02004041 // set b_mtime to stop further warnings (e.g., when executing
4042 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (stat_res < 0)
4044 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004045 // Check the file again later to see if it re-appears.
4046 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 buf->b_orig_size = 0;
4048 buf->b_orig_mode = 0;
4049 }
4050 else
4051 buf_store_time(buf, &st, buf->b_ffname);
4052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004053 // Don't do anything for a directory. Might contain the file
4054 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (mch_isdir(buf->b_fname))
4056 ;
4057
4058 /*
4059 * If 'autoread' is set, the buffer has no changes and the file still
4060 * exists, reload the buffer. Use the buffer-local option value if it
4061 * was set, the global option value otherwise.
4062 */
4063 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4064 && !bufIsChanged(buf) && stat_res >= 0)
4065 reload = TRUE;
4066 else
4067 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004068 if (stat_res < 0)
4069 reason = "deleted";
4070 else if (bufIsChanged(buf))
4071 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004072 /*
4073 * Check if the file contents really changed to avoid giving a
4074 * warning when only the timestamp was set (e.g., checked out of
4075 * CVS). Always warn when the buffer was changed.
4076 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004077 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4078 reason = "changed";
4079 else if (orig_mode != buf->b_orig_mode)
4080 reason = "mode";
4081 else
4082 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Only give the warning if there are no FileChangedShell
4086 * autocommands.
4087 * Avoid being called recursively by setting "busy".
4088 */
4089 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004090#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004091 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4092 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004093#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004094 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4096 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004097 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 busy = FALSE;
4099 if (n)
4100 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004101 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004102 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004103#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004104 s = get_vim_var_str(VV_FCS_CHOICE);
4105 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4106 reload = TRUE;
4107 else if (STRCMP(s, "ask") == 0)
4108 n = FALSE;
4109 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004110#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004111 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004113 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004116 {
4117 // Only give the message once.
4118 if (prev_b_mtime != -1)
4119 mesg = _("E211: File \"%s\" no longer available");
4120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else
4122 {
4123 helpmesg = TRUE;
4124#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4125 can_reload = TRUE;
4126#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004127 if (reason[2] == 'n')
4128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004130 mesg2 = _("See \":help W12\" for more info.");
4131 }
4132 else if (reason[1] == 'h')
4133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004135 mesg2 = _("See \":help W11\" for more info.");
4136 }
4137 else if (*reason == 'm')
4138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004140 mesg2 = _("See \":help W16\" for more info.");
4141 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004142 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004143 // Only timestamp changed, store it to avoid a warning
4144 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004145 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 }
4148 }
4149
4150 }
4151 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4152 && vim_fexists(buf->b_ffname))
4153 {
4154 retval = 1;
4155 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4156 buf->b_flags |= BF_NEW_W;
4157#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4158 can_reload = TRUE;
4159#endif
4160 }
4161
4162 if (mesg != NULL)
4163 {
4164 path = home_replace_save(buf, buf->b_fname);
4165 if (path != NULL)
4166 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004167 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004169 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004170 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004171#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004172 // Set warningmsg here, before the unimportant and output-specific
4173 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004174 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4177 if (can_reload)
4178 {
4179 if (*mesg2 != NUL)
4180 {
4181 STRCAT(tbuf, "\n");
4182 STRCAT(tbuf, mesg2);
4183 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004184 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4185 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004186 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 reload = TRUE;
4188 }
4189 else
4190#endif
4191 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4192 {
4193 if (*mesg2 != NUL)
4194 {
4195 STRCAT(tbuf, "; ");
4196 STRCAT(tbuf, mesg2);
4197 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004198 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 retval = 2;
4200 }
4201 else
4202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004206 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004208 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 msg_clr_eos();
4210 (void)msg_end();
4211 if (emsg_silent == 0)
4212 {
4213 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004214#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004216#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004217 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004218 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004220 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 redraw_cmdline = FALSE;
4222 }
4223 }
4224 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226
4227 vim_free(path);
4228 vim_free(tbuf);
4229 }
4230 }
4231
4232 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004234 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004235 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004236#ifdef FEAT_PERSISTENT_UNDO
4237 if (buf->b_p_udf && buf->b_ffname != NULL)
4238 {
4239 char_u hash[UNDO_HASH_SIZE];
4240 buf_T *save_curbuf = curbuf;
4241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004242 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004243 curbuf = buf;
4244 u_compute_hash(hash);
4245 u_write_undo(NULL, FALSE, buf, hash);
4246 curbuf = save_curbuf;
4247 }
4248#endif
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004251 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004252 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004253 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4254 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004256 // restore this in case an autocommand has set it; it would break
4257 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 need_mouse_correct = save_mouse_correct;
4259#endif
4260
4261 return retval;
4262}
4263
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004264/*
4265 * Reload a buffer that is already loaded.
4266 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004267 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4268 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004269 */
4270 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004271buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004272{
4273 exarg_T ea;
4274 pos_T old_cursor;
4275 linenr_T old_topline;
4276 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004277 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004278 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004279 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004280 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004281 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004283 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004284 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004286 // We only want to read the text from the file, not reset the syntax
4287 // highlighting, clear marks, diff status, etc. Force the fileformat
4288 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004289 if (prep_exarg(&ea, buf) == OK)
4290 {
4291 old_cursor = curwin->w_cursor;
4292 old_topline = curwin->w_topline;
4293
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004294 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004296 // Save all the text, so that the reload can be undone.
4297 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004298 u_sync(FALSE);
4299 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4300 flags |= READ_KEEP_UNDO;
4301 }
4302
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004303 /*
4304 * To behave like when a new file is edited (matters for
4305 * BufReadPost autocommands) we first need to delete the current
4306 * buffer contents. But if reading the file fails we should keep
4307 * the old contents. Can't use memory only, the file might be
4308 * too big. Use a hidden buffer to move the buffer contents to.
4309 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004310 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004311 savebuf = NULL;
4312 else
4313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004314 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004315 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004316 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004317 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004320 curbuf = savebuf;
4321 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004322 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004323 curbuf = buf;
4324 curwin->w_buffer = buf;
4325 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004326 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327 || move_lines(buf, savebuf) == FAIL)
4328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004329 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004330 buf->b_fname);
4331 saved = FAIL;
4332 }
4333 }
4334
4335 if (saved == OK)
4336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4338 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004339 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4340 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004341 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004342 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004343#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004344 if (!aborting())
4345#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004346 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004347 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004349 // Put the text back from the save buffer. First
4350 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004351 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004352 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004353 break;
4354 (void)move_lines(savebuf, buf);
4355 }
4356 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004359 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004360 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004361 if ((flags & READ_KEEP_UNDO) == 0)
4362 {
4363 u_blockfree(buf);
4364 u_clearall(buf);
4365 }
4366 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004368 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004369 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004370 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004371 }
4372 }
4373 vim_free(ea.cmd);
4374
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004375 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004376 wipe_buffer(savebuf, FALSE);
4377
4378#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004380 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004381#endif
4382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 // Restore the topline and cursor position and check it (lines may
4384 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 if (old_topline > curbuf->b_ml.ml_line_count)
4386 curwin->w_topline = curbuf->b_ml.ml_line_count;
4387 else
4388 curwin->w_topline = old_topline;
4389 curwin->w_cursor = old_cursor;
4390 check_cursor();
4391 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004392 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393#ifdef FEAT_FOLDING
4394 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004395 win_T *wp;
4396 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004399 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 if (wp->w_buffer == curwin->w_buffer
4401 && !foldmethodIsManual(wp))
4402 foldUpdateAll(wp);
4403 }
4404#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004405 // If the mode didn't change and 'readonly' was set, keep the old
4406 // value; the user probably used the ":view" command. But don't
4407 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004408 if (orig_mode == curbuf->b_orig_mode)
4409 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004412 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004413 }
4414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004416 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004417 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004418}
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004421buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004424 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425#ifdef HAVE_ST_MODE
4426 buf->b_orig_mode = (int)st->st_mode;
4427#else
4428 buf->b_orig_mode = mch_getperm(fname);
4429#endif
4430}
4431
4432/*
4433 * Adjust the line with missing eol, used for the next write.
4434 * Used for do_filter(), when the input lines for the filter are deleted.
4435 */
4436 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004437write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004439 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004440 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441}
4442
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004443// Subfuncions for readdirex()
4444#ifdef FEAT_EVAL
4445# ifdef MSWIN
4446 static char_u *
4447getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4448{
4449 stat_T st;
4450 unsigned short st_mode;
4451 DWORD flag = wfd->dwFileAttributes;
4452 WCHAR *wp;
4453
4454 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4455 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4456 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4457 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4458
4459 wp = wcsrchr(wfd->cFileName, L'.');
4460 if (wp != NULL)
4461 {
4462 if (_wcsicmp(wp, L".exe") == 0 ||
4463 _wcsicmp(wp, L".com") == 0 ||
4464 _wcsicmp(wp, L".cmd") == 0 ||
4465 _wcsicmp(wp, L".bat") == 0)
4466 st_mode |= _S_IEXEC;
4467 }
4468
4469 // Copy user bits to group/other.
4470 st_mode |= (st_mode & 0700) >> 3;
4471 st_mode |= (st_mode & 0700) >> 6;
4472
4473 st.st_mode = st_mode;
4474 return getfpermst(&st, perm);
4475}
4476
4477 static char_u *
4478getftypewfd(WIN32_FIND_DATAW *wfd)
4479{
4480 DWORD flag = wfd->dwFileAttributes;
4481 DWORD tag = wfd->dwReserved0;
4482
4483 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4484 {
4485 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4486 return (char_u*)"junction";
4487 else if (tag == IO_REPARSE_TAG_SYMLINK)
4488 {
4489 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4490 return (char_u*)"linkd";
4491 else
4492 return (char_u*)"link";
4493 }
4494 return (char_u*)"reparse"; // unknown reparse point type
4495 }
4496 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4497 return (char_u*)"dir";
4498 else
4499 return (char_u*)"file";
4500}
4501
4502 static dict_T *
4503create_readdirex_item(WIN32_FIND_DATAW *wfd)
4504{
4505 dict_T *item;
4506 char_u *p;
4507 varnumber_T size, time;
4508 char_u permbuf[] = "---------";
4509
4510 item = dict_alloc();
4511 if (item == NULL)
4512 return NULL;
4513 item->dv_refcount++;
4514
4515 p = utf16_to_enc(wfd->cFileName, NULL);
4516 if (p == NULL)
4517 goto theend;
4518 if (dict_add_string(item, "name", p) == FAIL)
4519 {
4520 vim_free(p);
4521 goto theend;
4522 }
4523 vim_free(p);
4524
4525 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4526 if (dict_add_number(item, "size", size) == FAIL)
4527 goto theend;
4528
4529 // Convert FILETIME to unix time.
4530 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4531 wfd->ftLastWriteTime.dwLowDateTime)
4532 - 116444736000000000) / 10000000;
4533 if (dict_add_number(item, "time", time) == FAIL)
4534 goto theend;
4535
4536 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4537 goto theend;
4538 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4539 goto theend;
4540
4541 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4542 goto theend;
4543 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4544 goto theend;
4545
4546 return item;
4547
4548theend:
4549 dict_unref(item);
4550 return NULL;
4551}
4552# else
4553 static dict_T *
4554create_readdirex_item(char_u *path, char_u *name)
4555{
4556 dict_T *item;
4557 char *p;
4558 size_t len;
4559 stat_T st;
4560 int ret, link = FALSE;
4561 varnumber_T size;
4562 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004563 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004564 struct passwd *pw;
4565 struct group *gr;
4566
4567 item = dict_alloc();
4568 if (item == NULL)
4569 return NULL;
4570 item->dv_refcount++;
4571
4572 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4573 p = alloc(len);
4574 if (p == NULL)
4575 goto theend;
4576 vim_snprintf(p, len, "%s/%s", path, name);
4577 ret = mch_lstat(p, &st);
4578 if (ret >= 0 && S_ISLNK(st.st_mode))
4579 {
4580 link = TRUE;
4581 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004582 if (ret < 0)
4583 q = (char_u*)"link";
4584
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004585 }
4586 vim_free(p);
4587
4588 if (dict_add_string(item, "name", name) == FAIL)
4589 goto theend;
4590
4591 if (ret >= 0)
4592 {
4593 size = (varnumber_T)st.st_size;
4594 if (S_ISDIR(st.st_mode))
4595 size = 0;
4596 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004597 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004598 size = -2;
4599 if (dict_add_number(item, "size", size) == FAIL)
4600 goto theend;
4601 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4602 goto theend;
4603
4604 if (link)
4605 {
4606 if (S_ISDIR(st.st_mode))
4607 q = (char_u*)"linkd";
4608 else
4609 q = (char_u*)"link";
4610 }
4611 else
4612 q = getftypest(&st);
4613 if (dict_add_string(item, "type", q) == FAIL)
4614 goto theend;
4615 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4616 goto theend;
4617
4618 pw = getpwuid(st.st_uid);
4619 if (pw == NULL)
4620 q = (char_u*)"";
4621 else
4622 q = (char_u*)pw->pw_name;
4623 if (dict_add_string(item, "user", q) == FAIL)
4624 goto theend;
4625 gr = getgrgid(st.st_gid);
4626 if (gr == NULL)
4627 q = (char_u*)"";
4628 else
4629 q = (char_u*)gr->gr_name;
4630 if (dict_add_string(item, "group", q) == FAIL)
4631 goto theend;
4632 }
4633 else
4634 {
4635 if (dict_add_number(item, "size", -1) == FAIL)
4636 goto theend;
4637 if (dict_add_number(item, "time", -1) == FAIL)
4638 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004639 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004640 goto theend;
4641 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4642 goto theend;
4643 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4644 goto theend;
4645 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4646 goto theend;
4647 }
4648 return item;
4649
4650theend:
4651 dict_unref(item);
4652 return NULL;
4653}
4654# endif
4655
4656 static int
4657compare_readdirex_item(const void *p1, const void *p2)
4658{
4659 char_u *name1, *name2;
4660
4661 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4662 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004663 if (readdirex_sort == READDIR_SORT_BYTE)
4664 return STRCMP(name1, name2);
4665 else if (readdirex_sort == READDIR_SORT_IC)
4666 return STRICMP(name1, name2);
4667 else
4668 return STRCOLL(name1, name2);
4669}
4670
4671 static int
4672compare_readdir_item(const void *s1, const void *s2)
4673{
4674 if (readdirex_sort == READDIR_SORT_BYTE)
4675 return STRCMP(*(char **)s1, *(char **)s2);
4676 else if (readdirex_sort == READDIR_SORT_IC)
4677 return STRICMP(*(char **)s1, *(char **)s2);
4678 else
4679 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004680}
4681#endif
4682
Bram Moolenaarda440d22016-01-16 21:27:23 +01004683#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4684/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004685 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004686 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004687 * If "withattr" is TRUE, retrieve the names and their attributes.
4688 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004689 * Return OK for success, FAIL for failure.
4690 */
4691 int
4692readdir_core(
4693 garray_T *gap,
4694 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004695 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004696 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004697 int (*checkitem)(void *context, void *item),
4698 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004699{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004700 int failed = FALSE;
4701 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004702# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004703 char_u *buf;
4704 int ok;
4705 HANDLE hFind = INVALID_HANDLE_VALUE;
4706 WIN32_FIND_DATAW wfd;
4707 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004708# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004709 DIR *dirp;
4710 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004711# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004712
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004713 ga_init2(gap, (int)sizeof(void *), 20);
4714
4715# ifdef FEAT_EVAL
4716# define FREE_ITEM(item) do { \
4717 if (withattr) \
4718 dict_unref((dict_T*)item); \
4719 else \
4720 vim_free(item); \
4721 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004722
4723 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004724# else
4725# define FREE_ITEM(item) vim_free(item)
4726# endif
4727
4728# ifdef MSWIN
4729 buf = alloc(MAXPATHL);
4730 if (buf == NULL)
4731 return FAIL;
4732 STRNCPY(buf, path, MAXPATHL-5);
4733 p = buf + STRLEN(buf);
4734 MB_PTR_BACK(buf, p);
4735 if (*p == '\\' || *p == '/')
4736 *p = NUL;
4737 STRCAT(p, "\\*");
4738
4739 wn = enc_to_utf16(buf, NULL);
4740 if (wn != NULL)
4741 hFind = FindFirstFileW(wn, &wfd);
4742 ok = (hFind != INVALID_HANDLE_VALUE);
4743 if (!ok)
4744 {
4745 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004746 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004747 }
4748 else
4749 {
4750 while (ok)
4751 {
4752 int ignore;
4753 void *item;
4754 WCHAR *wp;
4755
4756 wp = wfd.cFileName;
4757 ignore = wp[0] == L'.' &&
4758 (wp[1] == NUL ||
4759 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004760 if (ignore)
4761 {
4762 ok = FindNextFileW(hFind, &wfd);
4763 continue;
4764 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004765# ifdef FEAT_EVAL
4766 if (withattr)
4767 item = (void*)create_readdirex_item(&wfd);
4768 else
4769# endif
4770 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4771 if (item == NULL)
4772 {
4773 failed = TRUE;
4774 break;
4775 }
4776
4777 if (!ignore && checkitem != NULL)
4778 {
4779 int r = checkitem(context, item);
4780
4781 if (r < 0)
4782 {
4783 FREE_ITEM(item);
4784 break;
4785 }
4786 if (r == 0)
4787 ignore = TRUE;
4788 }
4789
4790 if (!ignore)
4791 {
4792 if (ga_grow(gap, 1) == OK)
4793 ((void**)gap->ga_data)[gap->ga_len++] = item;
4794 else
4795 {
4796 failed = TRUE;
4797 FREE_ITEM(item);
4798 break;
4799 }
4800 }
4801 else
4802 FREE_ITEM(item);
4803
4804 ok = FindNextFileW(hFind, &wfd);
4805 }
4806 FindClose(hFind);
4807 }
4808
4809 vim_free(buf);
4810 vim_free(wn);
4811# else // MSWIN
4812 dirp = opendir((char *)path);
4813 if (dirp == NULL)
4814 {
4815 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004816 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004817 }
4818 else
4819 {
4820 for (;;)
4821 {
4822 int ignore;
4823 void *item;
4824
4825 dp = readdir(dirp);
4826 if (dp == NULL)
4827 break;
4828 p = (char_u *)dp->d_name;
4829
4830 ignore = p[0] == '.' &&
4831 (p[1] == NUL ||
4832 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004833 if (ignore)
4834 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004835# ifdef FEAT_EVAL
4836 if (withattr)
4837 item = (void*)create_readdirex_item(path, p);
4838 else
4839# endif
4840 item = (void*)vim_strsave(p);
4841 if (item == NULL)
4842 {
4843 failed = TRUE;
4844 break;
4845 }
4846
4847 if (!ignore && checkitem != NULL)
4848 {
4849 int r = checkitem(context, item);
4850
4851 if (r < 0)
4852 {
4853 FREE_ITEM(item);
4854 break;
4855 }
4856 if (r == 0)
4857 ignore = TRUE;
4858 }
4859
4860 if (!ignore)
4861 {
4862 if (ga_grow(gap, 1) == OK)
4863 ((void**)gap->ga_data)[gap->ga_len++] = item;
4864 else
4865 {
4866 failed = TRUE;
4867 FREE_ITEM(item);
4868 break;
4869 }
4870 }
4871 else
4872 FREE_ITEM(item);
4873 }
4874
4875 closedir(dirp);
4876 }
4877# endif // MSWIN
4878
4879# undef FREE_ITEM
4880
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004881 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004882 {
4883# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004884 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004885 if (withattr)
4886 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4887 compare_readdirex_item);
4888 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004889 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4890 compare_readdir_item);
4891# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004892 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004893# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004894 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004895
4896 return failed ? FAIL : OK;
4897}
4898
4899/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004900 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004901 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004902 */
4903 int
4904delete_recursive(char_u *name)
4905{
4906 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004907 int i;
4908 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004909 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004910
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004911 // A symbolic link to a directory itself is deleted, not the directory it
4912 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004913 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004914# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004915 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004916# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004917 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004918# endif
4919 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004920 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004921 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004922 if (exp == NULL)
4923 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004924 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004925 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004926 for (i = 0; i < ga.ga_len; ++i)
4927 {
4928 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4929 ((char_u **)ga.ga_data)[i]);
4930 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004931 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004932 }
4933 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004934 }
4935 else
4936 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004937 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004938 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004939 }
4940 else
4941 result = mch_remove(name) == 0 ? 0 : -1;
4942
4943 return result;
4944}
4945#endif
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004948static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004950# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4951/*
4952 * Open temporary directory and take file lock to prevent
4953 * to be auto-cleaned.
4954 */
4955 static void
4956vim_opentempdir(void)
4957{
4958 DIR *dp = NULL;
4959
4960 if (vim_tempdir_dp != NULL)
4961 return;
4962
4963 dp = opendir((const char*)vim_tempdir);
4964
4965 if (dp != NULL)
4966 {
4967 vim_tempdir_dp = dp;
4968 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4969 }
4970}
4971
4972/*
4973 * Close temporary directory - it automatically release file lock.
4974 */
4975 static void
4976vim_closetempdir(void)
4977{
4978 if (vim_tempdir_dp != NULL)
4979 {
4980 closedir(vim_tempdir_dp);
4981 vim_tempdir_dp = NULL;
4982 }
4983}
4984# endif
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986/*
4987 * Delete the temp directory and all files it contains.
4988 */
4989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004990vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (vim_tempdir != NULL)
4993 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004994# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4995 vim_closetempdir();
4996# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004997 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004998 gettail(vim_tempdir)[-1] = NUL;
4999 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005000 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005005 * Directory "tempdir" was created. Expand this name to a full path and put
5006 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5007 * "tempdir" must be no longer than MAXPATHL.
5008 */
5009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005010vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005011{
5012 char_u *buf;
5013
Bram Moolenaar964b3742019-05-24 18:54:09 +02005014 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005015 if (buf != NULL)
5016 {
5017 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5018 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005019 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005020 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005021# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5022 vim_opentempdir();
5023# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005024 vim_free(buf);
5025 }
5026}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005027#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005028
5029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 * vim_tempname(): Return a unique name that can be used for a temp file.
5031 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005032 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5033 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 *
5035 * The returned pointer is to allocated memory.
5036 * The returned pointer is NULL if no valid name was found.
5037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005039vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005041 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005044 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005045#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005046 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#else
5048 char_u itmp[TEMPNAMELEN];
5049#endif
5050
5051#ifdef TEMPDIRNAMES
5052 static char *(tempdirs[]) = {TEMPDIRNAMES};
5053 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005055 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056# endif
5057
5058 /*
5059 * This will create a directory for private use by this instance of Vim.
5060 * This is done once, and the same directory is used for all temp files.
5061 * This method avoids security problems because of symlink attacks et al.
5062 * It's also a bit faster, because we only need to check for an existing
5063 * file when creating the directory and not for each temp file.
5064 */
5065 if (vim_tempdir == NULL)
5066 {
5067 /*
5068 * Try the entries in TEMPDIRNAMES to create the temp directory.
5069 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005070 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005072# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005073 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005074 long nr;
5075 long off;
5076# endif
5077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 // Expand $TMP, leave room for "/v1100000/999999999".
5079 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005081 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005083 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005084 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaareaf03392009-11-17 11:08:52 +00005086# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005087 {
5088# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 // Make sure the umask doesn't remove the executable bit.
5090 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005091 mode_t umask_save = umask(077);
5092# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005093 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005094 STRCAT(itmp, "vXXXXXX");
5095 if (mkdtemp((char *)itmp) != NULL)
5096 vim_settempdir(itmp);
5097# if defined(UNIX) || defined(VMS)
5098 (void)umask(umask_save);
5099# endif
5100 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005101# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005102 // Get an arbitrary number of up to 6 digits. When it's
5103 // unlikely that it already exists it will be faster,
5104 // otherwise it doesn't matter. The use of mkdir() avoids any
5105 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005107 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 // Try up to 10000 different values until we find a name that
5110 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 for (off = 0; off < 10000L; ++off)
5112 {
5113 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005114# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117
Bram Moolenaareaf03392009-11-17 11:08:52 +00005118 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5119# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005120 // If mkdir() does not set errno to EEXIST, check for
5121 // existing file here. There is a race condition then,
5122 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (mch_stat((char *)itmp, &st) >= 0)
5124 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005125# endif
5126# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127 // Make sure the umask doesn't remove the executable bit.
5128 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005132# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (r == 0)
5136 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005137 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005140# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005141 // If the mkdir() didn't fail because the file/dir exists,
5142 // we probably can't create any dir here, try another
5143 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005148# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 if (vim_tempdir != NULL)
5150 break;
5151 }
5152 }
5153 }
5154
5155 if (vim_tempdir != NULL)
5156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 // There is no need to check if the file exists, because we own the
5158 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5160 return vim_strsave(itmp);
5161 }
5162
5163 return NULL;
5164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005165#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
Bram Moolenaar4f974752019-02-17 17:44:42 +01005167# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005168 WCHAR wszTempFile[_MAX_PATH + 1];
5169 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 char_u *retval;
5171 char_u *p;
5172
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005173 wcscpy(itmp, L"");
5174 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005175 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005176 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
5177 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005178 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005179 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005180 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005181 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005183 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005184 // GetTempFileName() will create the file, we don't want that
5185 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005187 // Backslashes in a temp file name cause problems when filtering with
5188 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5189 // didn't set 'shellslash'.
5190 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (*p_shcf == '-' || p_ssl)
5192 for (p = retval; *p; ++p)
5193 if (*p == '\\')
5194 *p = '/';
5195 return retval;
5196
Bram Moolenaar4f974752019-02-17 17:44:42 +01005197# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005200 char_u *p;
5201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005202 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005203 p = tmpnam((char *)itmp);
5204 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 return NULL;
5206# else
5207 char_u *p;
5208
5209# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005210 // mktemp() is not working on VMS. It seems to be
5211 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 sprintf((char *)itmp, "VIM%c", extra_char);
5213 p = (char_u *)tempnam("tmp:", (char *)itmp);
5214 if (p != NULL)
5215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005216 // VMS will use '.LIS' if we don't explicitly specify an extension,
5217 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 STRCPY(itmp, p);
5219 STRCAT(itmp, ".txt");
5220 free(p);
5221 }
5222 else
5223 return NULL;
5224# else
5225 STRCPY(itmp, TEMPNAME);
5226 if ((p = vim_strchr(itmp, '?')) != NULL)
5227 *p = extra_char;
5228 if (mktemp((char *)itmp) == NULL)
5229 return NULL;
5230# endif
5231# endif
5232
5233 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005234# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236}
5237
5238#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5239/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005240 * Convert all backslashes in fname to forward slashes in-place, unless when
5241 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 */
5243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005244forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 char_u *p;
5247
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005248 if (path_with_url(fname))
5249 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005251 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005252 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005254 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 *p = '/';
5256}
5257#endif
5258
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005259/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005260 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5261 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5262 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 * Used for autocommands and 'wildignore'.
5264 * Returns TRUE if there is a match, FALSE otherwise.
5265 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005266 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005268 char_u *pattern, // pattern to match with
5269 regprog_T **prog, // pre-compiled regprog or NULL
5270 char_u *fname, // full path of file name
5271 char_u *sfname, // short file name or NULL
5272 char_u *tail, // tail of path
5273 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 regmatch_T regmatch;
5276 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005278 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005279 if (prog != NULL)
5280 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005282 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 /*
5285 * Try for a match with the pattern with:
5286 * 1. the full file name, when the pattern has a '/'.
5287 * 2. the short file name, when the pattern has a '/'.
5288 * 3. the tail of the file name, when the pattern has no '/'.
5289 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005290 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 && ((allow_dirs
5292 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5293 || (sfname != NULL
5294 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005295 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 result = TRUE;
5297
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005298 if (prog != NULL)
5299 *prog = regmatch.regprog;
5300 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005301 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 return result;
5303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305#if defined(FEAT_WILDIGN) || defined(PROTO)
5306/*
5307 * Return TRUE if a file matches with a pattern in "list".
5308 * "list" is a comma-separated list of patterns, like 'wildignore'.
5309 * "sfname" is the short file name or NULL, "ffname" the long file name.
5310 */
5311 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 char_u buf[100];
5315 char_u *tail;
5316 char_u *regpat;
5317 char allow_dirs;
5318 int match;
5319 char_u *p;
5320
5321 tail = gettail(sfname);
5322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005323 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 p = list;
5325 while (*p)
5326 {
5327 copy_option_part(&p, buf, 100, ",");
5328 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5329 if (regpat == NULL)
5330 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005331 match = match_file_pat(regpat, NULL, ffname, sfname,
5332 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 vim_free(regpat);
5334 if (match)
5335 return TRUE;
5336 }
5337 return FALSE;
5338}
5339#endif
5340
5341/*
5342 * Convert the given pattern "pat" which has shell style wildcards in it, into
5343 * a regular expression, and return the result in allocated memory. If there
5344 * is a directory path separator to be matched, then TRUE is put in
5345 * allow_dirs, otherwise FALSE is put there -- webb.
5346 * Handle backslashes before special characters, like "\*" and "\ ".
5347 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 * Returns NULL when out of memory.
5349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005351file_pat_to_reg_pat(
5352 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 char_u *pat_end, // first char after pattern or NULL
5354 char *allow_dirs, // Result passed back out in here
5355 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005357 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 char_u *endp;
5359 char_u *reg_pat;
5360 char_u *p;
5361 int i;
5362 int nested = 0;
5363 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 if (allow_dirs != NULL)
5366 *allow_dirs = FALSE;
5367 if (pat_end == NULL)
5368 pat_end = pat + STRLEN(pat);
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 for (p = pat; p < pat_end; p++)
5371 {
5372 switch (*p)
5373 {
5374 case '*':
5375 case '.':
5376 case ',':
5377 case '{':
5378 case '}':
5379 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 break;
5382#ifdef BACKSLASH_IN_FILENAME
5383 case '\\':
5384 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 break;
5387#endif
5388 default:
5389 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005390 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
5392 ++p;
5393 ++size;
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 break;
5396 }
5397 }
5398 reg_pat = alloc(size + 1);
5399 if (reg_pat == NULL)
5400 return NULL;
5401
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
5404 if (pat[0] == '*')
5405 while (pat[0] == '*' && pat < pat_end - 1)
5406 pat++;
5407 else
5408 reg_pat[i++] = '^';
5409 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005410 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
5412 while (endp - pat > 0 && *endp == '*')
5413 endp--;
5414 add_dollar = FALSE;
5415 }
5416 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5417 {
5418 switch (*p)
5419 {
5420 case '*':
5421 reg_pat[i++] = '.';
5422 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005424 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 break;
5426 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 case '~':
5428 reg_pat[i++] = '\\';
5429 reg_pat[i++] = *p;
5430 break;
5431 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 reg_pat[i++] = '.';
5433 break;
5434 case '\\':
5435 if (p[1] == NUL)
5436 break;
5437#ifdef BACKSLASH_IN_FILENAME
5438 if (!no_bslash)
5439 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 // translate:
5441 // "\x" to "\\x" e.g., "dir\file"
5442 // "\*" to "\\.*" e.g., "dir\*.c"
5443 // "\?" to "\\." e.g., "dir\??.c"
5444 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5446 && p[1] != '+')
5447 {
5448 reg_pat[i++] = '[';
5449 reg_pat[i++] = '\\';
5450 reg_pat[i++] = '/';
5451 reg_pat[i++] = ']';
5452 if (allow_dirs != NULL)
5453 *allow_dirs = TRUE;
5454 break;
5455 }
5456 }
5457#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005458 // Undo escaping from ExpandEscape():
5459 // foo\?bar -> foo?bar
5460 // foo\%bar -> foo%bar
5461 // foo\,bar -> foo,bar
5462 // foo\ bar -> foo bar
5463 // Don't unescape \, * and others that are also special in a
5464 // regexp.
5465 // An escaped { must be unescaped since we use magic not
5466 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 if (*++p == '?'
5468#ifdef BACKSLASH_IN_FILENAME
5469 && no_bslash
5470#endif
5471 )
5472 reg_pat[i++] = '?';
5473 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005474 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005475 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005476 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005477 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5478 {
5479 reg_pat[i++] = '\\';
5480 reg_pat[i++] = '{';
5481 p += 2;
5482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 else
5484 {
5485 if (allow_dirs != NULL && vim_ispathsep(*p)
5486#ifdef BACKSLASH_IN_FILENAME
5487 && (!no_bslash || *p != '\\')
5488#endif
5489 )
5490 *allow_dirs = TRUE;
5491 reg_pat[i++] = '\\';
5492 reg_pat[i++] = *p;
5493 }
5494 break;
5495#ifdef BACKSLASH_IN_FILENAME
5496 case '/':
5497 reg_pat[i++] = '[';
5498 reg_pat[i++] = '\\';
5499 reg_pat[i++] = '/';
5500 reg_pat[i++] = ']';
5501 if (allow_dirs != NULL)
5502 *allow_dirs = TRUE;
5503 break;
5504#endif
5505 case '{':
5506 reg_pat[i++] = '\\';
5507 reg_pat[i++] = '(';
5508 nested++;
5509 break;
5510 case '}':
5511 reg_pat[i++] = '\\';
5512 reg_pat[i++] = ')';
5513 --nested;
5514 break;
5515 case ',':
5516 if (nested)
5517 {
5518 reg_pat[i++] = '\\';
5519 reg_pat[i++] = '|';
5520 }
5521 else
5522 reg_pat[i++] = ',';
5523 break;
5524 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005525 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005527 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 *allow_dirs = TRUE;
5529 reg_pat[i++] = *p;
5530 break;
5531 }
5532 }
5533 if (add_dollar)
5534 reg_pat[i++] = '$';
5535 reg_pat[i] = NUL;
5536 if (nested != 0)
5537 {
5538 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005539 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005541 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005542 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 }
5544 return reg_pat;
5545}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005546
5547#if defined(EINTR) || defined(PROTO)
5548/*
5549 * Version of read() that retries when interrupted by EINTR (possibly
5550 * by a SIGWINCH).
5551 */
5552 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005554{
5555 long ret;
5556
5557 for (;;)
5558 {
5559 ret = vim_read(fd, buf, bufsize);
5560 if (ret >= 0 || errno != EINTR)
5561 break;
5562 }
5563 return ret;
5564}
5565
5566/*
5567 * Version of write() that retries when interrupted by EINTR (possibly
5568 * by a SIGWINCH).
5569 */
5570 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005571write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005572{
5573 long ret = 0;
5574 long wlen;
5575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005576 // Repeat the write() so long it didn't fail, other than being interrupted
5577 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005578 while (ret < (long)bufsize)
5579 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005580 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005581 if (wlen < 0)
5582 {
5583 if (errno != EINTR)
5584 break;
5585 }
5586 else
5587 ret += wlen;
5588 }
5589 return ret;
5590}
5591#endif