blob: 395e54b6a44dc3b097b545481115ff0410fd9418 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaarf4888d02009-12-02 12:31:27 +000016#if defined(__TANDEM) || defined(__MINT__)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020019#if defined(UNIX) && defined(FEAT_EVAL)
20# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar217e1b82019-12-01 21:41:28 +010024// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000025#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf077db22019-08-13 00:18:24 +020027static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010028#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010029static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020032static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010035static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000036static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000037
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020038#ifdef FEAT_EVAL
39static int readdirex_sort;
40#endif
41
Bram Moolenaar473952e2019-09-28 16:30:04 +020042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010043filemess(
44 buf_T *buf,
45 char_u *name,
46 char_u *s,
47 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
49 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020050 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52 if (msg_silent != 0)
53 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010054 msg_add_fname(buf, name); // put file name in IObuff with quotes
55 // If it's extremely long, truncate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 if (STRLEN(IObuff) > IOSIZE - 80)
57 IObuff[IOSIZE - 80] = NUL;
58 STRCAT(IObuff, s);
59 /*
60 * For the first message may have to start a new line.
61 * For further ones overwrite the previous one, reset msg_scroll before
62 * calling filemess().
63 */
64 msg_scroll_save = msg_scroll;
65 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
66 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010067 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 check_for_delay(FALSE);
69 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020070 if (prev_msg_col != 0 && msg_col == 0)
71 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 msg_scroll = msg_scroll_save;
73 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010074 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
76 msg_clr_eos();
77 out_flush();
78 msg_scrolled_ign = FALSE;
79}
80
81/*
82 * Read lines from file "fname" into the buffer after line "from".
83 *
84 * 1. We allocate blocks with lalloc, as big as possible.
85 * 2. Each block is filled with characters from the file with a single read().
86 * 3. The lines are inserted in the buffer with ml_append().
87 *
88 * (caller must check that fname != NULL, unless READ_STDIN is used)
89 *
90 * "lines_to_skip" is the number of lines that must be skipped
91 * "lines_to_read" is the number of lines that are appended
92 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
93 *
94 * flags:
95 * READ_NEW starting to edit a new buffer
96 * READ_FILTER reading filter output
97 * READ_STDIN read from stdin instead of a file
98 * READ_BUFFER read from curbuf instead of a file (converting after reading
99 * stdin)
100 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200101 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200102 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100104 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 */
106 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100107readfile(
108 char_u *fname,
109 char_u *sfname,
110 linenr_T from,
111 linenr_T lines_to_skip,
112 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100113 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100114 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115{
116 int fd = 0;
117 int newfile = (flags & READ_NEW);
118 int check_readonly;
119 int filtering = (flags & READ_FILTER);
120 int read_stdin = (flags & READ_STDIN);
121 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000123 int set_options = newfile || read_buffer
124 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100125 linenr_T read_buf_lnum = 1; // next line to read from curbuf
126 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 char_u c;
128 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100129 char_u *ptr = NULL; // pointer into read buffer
130 char_u *buffer = NULL; // read buffer
131 char_u *new_buffer = NULL; // init to shut up gcc
132 char_u *line_start = NULL; // init to shut up gcc
133 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 colnr_T len;
135 long size = 0;
136 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200137 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 int skip_read = FALSE;
139#ifdef FEAT_CRYPT
140 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200141 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200143#ifdef FEAT_PERSISTENT_UNDO
144 context_sha256_T sha_ctx;
145 int read_undo_file = FALSE;
146#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100147 int split = 0; // number of split lines
148#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100150 int error = FALSE; // errors encountered
151 int ff_error = EOL_UNKNOWN; // file format with errors
152 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef UNIX
154 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100155 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#else
157 int perm;
158#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100159 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200161 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 int file_readonly;
163 linenr_T skip_count = 0;
164 linenr_T read_count = 0;
165 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100166 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
167 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100168 int try_mac;
169 int try_dos;
170 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100173 linenr_T conv_error = 0; // line nr with conversion error
174 linenr_T illegal_byte = 0; // line nr with illegal byte
175 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
176 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000177 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100178 // BAD_KEEP, BAD_DROP or character to
179 // replace with
180 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100182 char_u *fenc; // fileencoding to use
183 int fenc_alloced; // fenc_next is in allocated memory
184 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int advance_fenc = FALSE;
186 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100187#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100188 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100189# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100190 int did_iconv = FALSE; // TRUE when iconv() failed and trying
191 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100193#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 int converted = FALSE; // TRUE if conversion done
195 int notconverted = FALSE; // TRUE if conversion wanted but it
196 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100198 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100199 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200200 buf_T *old_curbuf;
201 char_u *old_b_ffname;
202 char_u *old_b_fname;
203 int using_b_ffname;
204 int using_b_fname;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200205
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100206 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200207
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100208 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
210 /*
211 * If there is no file name yet, use the one for the read file.
212 * BF_NOTEDITED is set to reflect this.
213 * Don't do this for a read from a filter.
214 * Only do this when 'cpoptions' contains the 'f' flag.
215 */
216 if (curbuf->b_ffname == NULL
217 && !filtering
218 && fname != NULL
219 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
220 && !(flags & READ_DUMMY))
221 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000222 if (set_rw_fname(fname, sfname) == FAIL)
223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 }
225
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100226 // Remember the initial values of curbuf, curbuf->b_ffname and
227 // curbuf->b_fname to detect whether they are altered as a result of
228 // executing nasty autocommands. Also check if "fname" and "sfname"
229 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200230 old_curbuf = curbuf;
231 old_b_ffname = curbuf->b_ffname;
232 old_b_fname = curbuf->b_fname;
233 using_b_ffname = (fname == curbuf->b_ffname)
234 || (sfname == curbuf->b_ffname);
235 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200236
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100237 // After reading a file the cursor line changes but we don't want to
238 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000239 ex_no_reprint = TRUE;
240
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100241 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000242 need_fileinfo = FALSE;
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 /*
245 * For Unix: Use the short file name whenever possible.
246 * Avoids problems with networks and when directory names are changed.
247 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
248 * another directory, which we don't detect.
249 */
250 if (sfname == NULL)
251 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200252#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 fname = sfname;
254#endif
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 /*
257 * The BufReadCmd and FileReadCmd events intercept the reading process by
258 * executing the associated commands instead.
259 */
260 if (!filtering && !read_stdin && !read_buffer)
261 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100262 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100264 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
266 curbuf->b_op_start.col = 0;
267
268 if (newfile)
269 {
270 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
271 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200272 {
273 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200275 if (aborting())
276 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200278 // The BufReadCmd code usually uses ":read" to get the text and
279 // perhaps ":file" to change the buffer name. But we should
280 // consider this to work like ":edit", thus reset the
281 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
282 // same file.
283 if (status == OK)
284 curbuf->b_flags &= ~BF_NOTEDITED;
285 return status;
286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 }
288 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
289 FALSE, NULL, eap))
290#ifdef FEAT_EVAL
291 return aborting() ? FAIL : OK;
292#else
293 return OK;
294#endif
295
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100296 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
299 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100300 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100302 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304 /*
305 * If the name ends in a path separator, we can't open it. Check here,
306 * because reading the file may actually work, but then creating the swap
307 * file may destroy it! Reported on MS-DOS and Win 95.
308 * If the name is too long we might crash further on, quit here.
309 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000310 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000312 p = fname + STRLEN(fname);
313 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000314 {
315 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
316 msg_end();
317 msg_scroll = msg_save;
318 return FAIL;
319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200322 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200324#ifdef UNIX
325 /*
326 * On Unix it is possible to read a directory, so we have to
327 * check for it before the mch_open().
328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100330 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
331 && !S_ISFIFO(perm) // ... or fifo
332 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000333# ifdef OPEN_CHR_FILES
334 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100335 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 )
338 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100339 int retval = FAIL;
340
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100344 retval = NOTDONE;
345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 else
347 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
348 msg_end();
349 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100350 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200352#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100353#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000354 /*
355 * MS-Windows allows opening a device, but we will probably get stuck
356 * trying to read it.
357 */
358 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
359 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000360 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000361 msg_end();
362 msg_scroll = msg_save;
363 return FAIL;
364 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000365#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200366 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000367
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100368 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200369 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
371 /*
372 * When opening a new file we take the readonly flag from the file.
373 * Default is r/w, can be set to r/o below.
374 * Don't reset it when in readonly mode
375 * Only set/reset b_p_ro when BF_CHECK_RO is set.
376 */
377 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000378 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 curbuf->b_p_ro = FALSE;
380
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200381 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100383 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 if (mch_stat((char *)fname, &st) >= 0)
385 {
386 buf_store_time(curbuf, &st, fname);
387 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#ifdef UNIX
389 /*
390 * Use the protection bits of the original file for the swap file.
391 * This makes it possible for others to read the name of the
392 * edited file from the swapfile, but only if they can read the
393 * edited file.
394 * Remove the "write" and "execute" bits for group and others
395 * (they must not write the swapfile).
396 * Add the "read" and "write" bits for the user, otherwise we may
397 * not be able to write to the file ourselves.
398 * Setting the bits is done below, after creating the swap file.
399 */
400 swap_mode = (st.st_mode & 0644) | 0600;
401#endif
402#ifdef FEAT_CW_EDITOR
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 // Get the FSSpec on MacOS
404 // TODO: Update it properly when the buffer name changes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
406#endif
407#ifdef VMS
408 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000409 curbuf->b_fab_rat = st.st_fab_rat;
410 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411#endif
412 }
413 else
414 {
415 curbuf->b_mtime = 0;
416 curbuf->b_mtime_read = 0;
417 curbuf->b_orig_size = 0;
418 curbuf->b_orig_mode = 0;
419 }
420
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100421 // Reset the "new file" flag. It will be set again below when the
422 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
424 }
425
426/*
427 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100428 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 */
430 file_readonly = FALSE;
431 if (read_stdin)
432 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100433#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100434 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 setmode(0, O_BINARY);
436#endif
437 }
438 else if (!read_buffer)
439 {
440#ifdef USE_MCH_ACCESS
441 if (
442# ifdef UNIX
443 !(perm & 0222) ||
444# endif
445 mch_access((char *)fname, W_OK))
446 file_readonly = TRUE;
447 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
448#else
449 if (!newfile
450 || readonlymode
451 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
452 {
453 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100454 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
456 }
457#endif
458 }
459
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100460 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {
462#ifndef UNIX
463 int isdir_f;
464#endif
465 msg_scroll = msg_save;
466#ifndef UNIX
467 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100468 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 */
470 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100471 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (isdir_f)
473 {
474 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 }
477 else
478#endif
479 if (newfile)
480 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200481 if (perm < 0
482#ifdef ENOENT
483 && errno == ENOENT
484#endif
485 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 /*
488 * Set the 'new-file' flag, so that when the file has
489 * been created by someone else, a ":w" will complain.
490 */
491 curbuf->b_flags |= BF_NEW;
492
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 // Create a swap file now, so that other Vims are warned
494 // that we are editing this file. Don't do this for a
495 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_QUICKFIX
497 if (!bt_dontwrite(curbuf))
498#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000502 if (curbuf != old_curbuf
503 || (using_b_ffname
504 && (old_b_ffname != curbuf->b_ffname))
505 || (using_b_fname
506 && (old_b_fname != curbuf->b_fname)))
507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100508 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000509 return FAIL;
510 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000511 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000512 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200513 filemess(curbuf, sfname,
514 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000515 else
516 filemess(curbuf, sfname,
517 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100519 // Even though this is a new file, it might have been
520 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 check_marks_read();
522#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200524 if (eap != NULL)
525 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
527 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100528 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 save_file_ff(curbuf);
530
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100531#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return FAIL;
534#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
537 else
538 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000539 filemess(curbuf, sfname, (char_u *)(
540# ifdef EFBIG
541 (errno == EFBIG) ? _("[File too big]") :
542# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200543# ifdef EOVERFLOW
544 (errno == EOVERFLOW) ? _("[File too big]") :
545# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000546 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549 }
550
551 return FAIL;
552 }
553
554 /*
555 * Only set the 'ro' flag for readonly files the first time they are
556 * loaded. Help files always get readonly mode
557 */
558 if ((check_readonly && file_readonly) || curbuf->b_help)
559 curbuf->b_p_ro = TRUE;
560
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000561 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100563 // Don't change 'eol' if reading from buffer as it will already be
564 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000565 if (!read_buffer)
566 {
567 curbuf->b_p_eol = TRUE;
568 curbuf->b_start_eol = TRUE;
569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000571 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100574 // Create a swap file now, so that other Vims are warned that we are
575 // editing this file.
576 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#ifdef FEAT_QUICKFIX
578 if (!bt_dontwrite(curbuf))
579#endif
580 {
581 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000582 if (!read_stdin && (curbuf != old_curbuf
583 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
584 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100586 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000587 if (!read_buffer)
588 close(fd);
589 return FAIL;
590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100592 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000593 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
594 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100595 {
596 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
597
598 /*
599 * If the group-read bit is set but not the world-read bit, then
600 * the group must be equal to the group of the original file. If
601 * we can't make that happen then reset the group-read bit. This
602 * avoids making the swap file readable to more users when the
603 * primary group of the user is too permissive.
604 */
605 if ((swap_mode & 044) == 040)
606 {
607 stat_T swap_st;
608
609 if (mch_stat((char *)swap_fname, &swap_st) >= 0
610 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200611# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100612 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200613 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200614# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200615 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100616 swap_mode &= 0600;
617 }
618
619 (void)mch_setperm(swap_fname, (long)swap_mode);
620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622 }
623
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200624 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (swap_exists_action == SEA_QUIT)
626 {
627 if (!read_buffer && !read_stdin)
628 close(fd);
629 return FAIL;
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634 /*
635 * Set '[ mark to the line above where the lines go (line 1 if zero).
636 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100637 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
639 curbuf->b_op_start.col = 0;
640
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100641 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
642 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
643 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (!read_buffer)
646 {
647 int m = msg_scroll;
648 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 /*
651 * The file must be closed again, the autocommands may want to change
652 * the file before reading it.
653 */
654 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 /*
658 * The output from the autocommands should not overwrite anything and
659 * should not be overwritten: Set msg_scroll, restore its value if no
660 * output was done.
661 */
662 msg_scroll = TRUE;
663 if (filtering)
664 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
665 FALSE, curbuf, eap);
666 else if (read_stdin)
667 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
668 FALSE, curbuf, eap);
669 else if (newfile)
670 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
671 FALSE, curbuf, eap);
672 else
673 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
674 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100675 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100676 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
677 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
678 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100679 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (msg_scrolled == n)
682 msg_scroll = m;
683
684#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100685 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
687 --no_wait_return;
688 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 return FAIL;
691 }
692#endif
693 /*
694 * Don't allow the autocommands to change the current buffer.
695 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000696 *
697 * Don't allow the autocommands to change the buffer name either
698 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
700 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000701 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
702 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
704 {
705 --no_wait_return;
706 msg_scroll = msg_save;
707 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100708 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100710 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 return FAIL;
713 }
714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100716 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
718
719 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
720 {
721 /*
722 * Show the user that we are busy reading the input. Sometimes this
723 * may take a while. When reading from stdin another program may
724 * still be running, don't move the cursor to the last line, unless
725 * always using the GUI.
726 */
727 if (read_stdin)
728 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100729 if (!is_not_a_term())
730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200732# ifdef VIMDLL
733 if (!gui.in_use)
734# endif
735 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
737#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100738 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100739 if (gui.in_use && !gui.dying && !gui.starting)
740 {
741 p = (char_u *)_("Reading from stdin...");
742 gui_write(p, (int)STRLEN(p));
743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747 else if (!read_buffer)
748 filemess(curbuf, sfname, (char_u *)"", 0);
749 }
750
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100751 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
753 /*
754 * Set linecnt now, before the "retry" caused by a wrong guess for
755 * fileformat, and after the autocommands, which may change them.
756 */
757 linecnt = curbuf->b_ml.ml_line_count;
758
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100759 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000760 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000761 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000762 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000763 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000764 curbuf->b_bad_char = eap->bad_char;
765 }
766 else
767 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000770 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
772 if (eap != NULL && eap->force_enc != 0)
773 {
774 fenc = enc_canonize(eap->cmd + eap->force_enc);
775 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000776 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 else if (curbuf->b_p_bin)
779 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100780 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 fenc_alloced = FALSE;
782 }
783 else if (curbuf->b_help)
784 {
785 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000786 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // Help files are either utf-8 or latin1. Try utf-8 first, if this
789 // fails it must be latin1.
790 // Always do this when 'encoding' is "utf-8". Otherwise only do
791 // this when needed to avoid [converted] remarks all the time.
792 // It is needed when the first line contains non-ASCII characters.
793 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 fenc = (char_u *)"latin1";
795 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000796 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000798 fc = fname[STRLEN(fname) - 1];
799 if (TOLOWER_ASC(fc) == 'x')
800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Read the first line (and a bit more). Immediately rewind to
802 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100803 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200804 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000805 for (p = firstline; p < firstline + len; ++p)
806 if (*p >= 0x80)
807 {
808 c = TRUE;
809 break;
810 }
811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 }
813
814 if (c)
815 {
816 fenc_next = fenc;
817 fenc = (char_u *)"utf-8";
818
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100819 // When the file is utf-8 but a character doesn't fit in
820 // 'encoding' don't retry. In help text editing utf-8 bytes
821 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000822 if (!enc_utf8)
823 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825 fenc_alloced = FALSE;
826 }
827 else if (*p_fencs == NUL)
828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100829 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 fenc_alloced = FALSE;
831 }
832 else
833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200835 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838 /*
839 * Jump back here to retry reading the file in different ways.
840 * Reasons to retry:
841 * - encoding conversion failed: try another one from "fenc_next"
842 * - BOM detected and fenc was set, need to setup conversion
843 * - "fileformat" check failed: try another
844 *
845 * Variables set for special retry actions:
846 * "file_rewind" Rewind the file to start reading it again.
847 * "advance_fenc" Advance "fenc" using "fenc_next".
848 * "skip_read" Re-use already read bytes (BOM detected).
849 * "did_iconv" iconv() conversion failed, try 'charconvert'.
850 * "keep_fileformat" Don't reset "fileformat".
851 *
852 * Other status indicators:
853 * "tmpname" When != NULL did conversion with 'charconvert'.
854 * Output file has to be deleted afterwards.
855 * "iconv_fd" When != -1 did conversion with iconv().
856 */
857retry:
858
859 if (file_rewind)
860 {
861 if (read_buffer)
862 {
863 read_buf_lnum = 1;
864 read_buf_col = 0;
865 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200866 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 error = TRUE;
870 goto failed;
871 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100872 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200874 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000876 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000879 curbuf->b_start_bomb = FALSE;
880 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000881 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
883
884 /*
885 * When retrying with another "fenc" and the first time "fileformat"
886 * will be reset.
887 */
888 if (keep_fileformat)
889 keep_fileformat = FALSE;
890 else
891 {
892 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000895 try_unix = try_dos = try_mac = FALSE;
896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100900 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100902 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904
Bram Moolenaar13505972019-01-24 15:04:48 +0100905#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (iconv_fd != (iconv_t)-1)
907 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100908 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 iconv_close(iconv_fd);
910 iconv_fd = (iconv_t)-1;
911 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
914 if (advance_fenc)
915 {
916 /*
917 * Try the next entry in 'fileencodings'.
918 */
919 advance_fenc = FALSE;
920
921 if (eap != NULL && eap->force_enc != 0)
922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100923 // Conversion given with "++cc=" wasn't possible, read
924 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000926 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (fenc_alloced)
928 vim_free(fenc);
929 fenc = (char_u *)"";
930 fenc_alloced = FALSE;
931 }
932 else
933 {
934 if (fenc_alloced)
935 vim_free(fenc);
936 if (fenc_next != NULL)
937 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200938 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940 else
941 {
942 fenc = (char_u *)"";
943 fenc_alloced = FALSE;
944 }
945 }
946 if (tmpname != NULL)
947 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100948 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100949 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951 }
952
953 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000954 * Conversion may be required when the encoding of the file is different
955 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 */
957 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000958 converted = need_conversion(fenc);
959 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 {
961
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 // "ucs-bom" means we need to check the first bytes of the file
963 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (STRCMP(fenc, ENC_UCSBOM) == 0)
965 fio_flags = FIO_UCSBOM;
966
967 /*
968 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
969 * done. This is handled below after read(). Prepare the
970 * fio_flags to avoid having to parse the string each time.
971 * Also check for Unicode to Latin1 conversion, because iconv()
972 * appears not to handle this correctly. This works just like
973 * conversion to UTF-8 except how the resulting character is put in
974 * the buffer.
975 */
976 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
977 fio_flags = get_fio_flags(fenc);
978
Bram Moolenaar4f974752019-02-17 17:44:42 +0100979#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /*
981 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
982 * is handled with MultiByteToWideChar().
983 */
984 if (fio_flags == 0)
985 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar13505972019-01-24 15:04:48 +0100988#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100989 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (fio_flags == 0)
991 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar13505972019-01-24 15:04:48 +0100994#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 /*
996 * Try using iconv() if we can't convert internally.
997 */
998 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +0100999# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 )
1003 iconv_fd = (iconv_t)my_iconv_open(
1004 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar13505972019-01-24 15:04:48 +01001007#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 /*
1009 * Use the 'charconvert' expression when conversion is required
1010 * and we can't do it internally or with iconv().
1011 */
1012 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001013 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001014# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 )
1018 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001019# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001021# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001022 // Skip conversion when it's already done (retry for wrong
1023 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (tmpname == NULL)
1025 {
1026 tmpname = readfile_charconvert(fname, fenc, &fd);
1027 if (tmpname == NULL)
1028 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001029 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 advance_fenc = TRUE;
1031 if (fd < 0)
1032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 error = TRUE;
1036 goto failed;
1037 }
1038 goto retry;
1039 }
1040 }
1041 }
1042 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001046#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 )
1050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001051 // Conversion wanted but we can't.
1052 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 advance_fenc = TRUE;
1054 goto retry;
1055 }
1056 }
1057 }
1058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 // Set "can_retry" when it's possible to rewind the file and try with
1060 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1061 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001062 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064 if (!skip_read)
1065 {
1066 linerest = 0;
1067 filesize = 0;
1068 skip_count = lines_to_skip;
1069 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001071#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001072 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1073 && curbuf->b_ffname != NULL
1074 && curbuf->b_p_udf
1075 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001076 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001077 && !read_stdin
1078 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001079 if (read_undo_file)
1080 sha256_start(&sha_ctx);
1081#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001082#ifdef FEAT_CRYPT
1083 if (curbuf->b_cryptstate != NULL)
1084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 // Need to free the state, but keep the key, don't want to ask for
1086 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001087 crypt_free_state(curbuf->b_cryptstate);
1088 curbuf->b_cryptstate = NULL;
1089 }
1090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092
1093 while (!error && !got_int)
1094 {
1095 /*
1096 * We allocate as much space for the file as we can get, plus
1097 * space for the old line plus room for one terminating NUL.
1098 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001099 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001101 if (!skip_read)
1102 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001103#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001104 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001105#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001106 // Use buffer >= 64K. Add linerest to double the size if the
1107 // line gets very long, to avoid a lot of copying. But don't
1108 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001109 size = 0x10000L + linerest;
1110 if (size > 0x100000L)
1111 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001112#endif
1113 }
1114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001115 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001116 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
1118 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 size = 1;
1121 }
1122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 if (!skip_read)
1125 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001126 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001128 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 FALSE)) != NULL)
1130 break;
1131 }
1132 if (new_buffer == NULL)
1133 {
1134 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1135 error = TRUE;
1136 break;
1137 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001138 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1140 vim_free(buffer);
1141 buffer = new_buffer;
1142 ptr = buffer + linerest;
1143 line_start = buffer;
1144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145 // May need room to translate into.
1146 // For iconv() we don't really know the required space, use a
1147 // factor ICONV_MULT.
1148 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1149 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1150 // become up to 4 bytes, size must be multiple of 2
1151 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1152 // multiple of 2
1153 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1154 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001155 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001156#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (iconv_fd != (iconv_t)-1)
1158 size = size / ICONV_MULT;
1159 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (fio_flags & FIO_LATIN1)
1162 size = size / 2;
1163 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1164 size = (size * 2 / 3) & ~1;
1165 else if (fio_flags & FIO_UCS4)
1166 size = (size * 2 / 3) & ~3;
1167 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001168 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001169#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001171 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001172#endif
1173#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#endif
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (conv_restlen > 0)
1179 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001180 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 mch_memmove(ptr, conv_rest, conv_restlen);
1182 ptr += conv_restlen;
1183 size -= conv_restlen;
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
1186 if (read_buffer)
1187 {
1188 /*
1189 * Read bytes from curbuf. Used for converting text read
1190 * from stdin.
1191 */
1192 if (read_buf_lnum > from)
1193 size = 0;
1194 else
1195 {
1196 int n, ni;
1197 long tlen;
1198
1199 tlen = 0;
1200 for (;;)
1201 {
1202 p = ml_get(read_buf_lnum) + read_buf_col;
1203 n = (int)STRLEN(p);
1204 if ((int)tlen + n + 1 > size)
1205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001206 // Filled up to "size", append partial line.
1207 // Change NL to NUL to reverse the effect done
1208 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001209 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 for (ni = 0; ni < n; ++ni)
1211 {
1212 if (p[ni] == NL)
1213 ptr[tlen++] = NUL;
1214 else
1215 ptr[tlen++] = p[ni];
1216 }
1217 read_buf_col += n;
1218 break;
1219 }
1220 else
1221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001222 // Append whole line and new-line. Change NL
1223 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 for (ni = 0; ni < n; ++ni)
1225 {
1226 if (p[ni] == NL)
1227 ptr[tlen++] = NUL;
1228 else
1229 ptr[tlen++] = p[ni];
1230 }
1231 ptr[tlen++] = NL;
1232 read_buf_col = 0;
1233 if (++read_buf_lnum > from)
1234 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001235 // When the last line didn't have an
1236 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (!curbuf->b_p_eol)
1238 --tlen;
1239 size = tlen;
1240 break;
1241 }
1242 }
1243 }
1244 }
1245 }
1246 else
1247 {
1248 /*
1249 * Read bytes from the file.
1250 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001251 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001254#ifdef FEAT_CRYPT
1255 /*
1256 * At start of file: Check for magic number of encryption.
1257 */
1258 if (filesize == 0 && size > 0)
1259 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1260 &filesize, newfile, sfname,
1261 &did_ask_for_key);
1262 /*
1263 * Decrypt the read bytes. This is done before checking for
1264 * EOF because the crypt layer may be buffering.
1265 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001266 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1267 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001268 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001269# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001270 if (crypt_works_inplace(curbuf->b_cryptstate))
1271 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001272# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001273 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001274# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001275 }
1276 else
1277 {
1278 char_u *newptr = NULL;
1279 int decrypted_size;
1280
1281 decrypted_size = crypt_decode_alloc(
1282 curbuf->b_cryptstate, ptr, size, &newptr);
1283
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001284 // If the crypt layer is buffering, not producing
1285 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001286 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001287 continue;
1288
1289 if (linerest == 0)
1290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001291 // Simple case: reuse returned buffer (may be
1292 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001293 new_buffer = newptr;
1294 }
1295 else
1296 {
1297 long_u new_size;
1298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001299 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001300 new_size = (long_u)(decrypted_size + linerest + 1);
1301 new_buffer = lalloc(new_size, FALSE);
1302 if (new_buffer == NULL)
1303 {
1304 do_outofmem_msg(new_size);
1305 error = TRUE;
1306 break;
1307 }
1308
1309 mch_memmove(new_buffer, buffer, linerest);
1310 if (newptr != NULL)
1311 mch_memmove(new_buffer + linerest, newptr,
1312 decrypted_size);
1313 }
1314
1315 if (new_buffer != NULL)
1316 {
1317 vim_free(buffer);
1318 buffer = new_buffer;
1319 new_buffer = NULL;
1320 line_start = buffer;
1321 ptr = buffer + linerest;
1322 }
1323 size = decrypted_size;
1324 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001325# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 }
1327#endif
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (size <= 0)
1330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001334 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001335 /*
1336 * Reached end-of-file but some trailing bytes could
1337 * not be converted. Truncated file?
1338 */
1339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001340 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001341 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001342#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001343 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001344#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001345 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001346 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001347 if (can_retry)
1348 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001349 if (conv_error == 0)
1350 conv_error = curbuf->b_ml.ml_line_count
1351 - linecnt + 1;
1352 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001353 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001354 else if (illegal_byte == 0)
1355 illegal_byte = curbuf->b_ml.ml_line_count
1356 - linecnt + 1;
1357 if (bad_char_behavior == BAD_DROP)
1358 {
1359 *(ptr - conv_restlen) = NUL;
1360 conv_restlen = 0;
1361 }
1362 else
1363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001364 // Replace the trailing bytes with the replacement
1365 // character if we were converting; if we weren't,
1366 // leave the UTF8 checking code to do it, as it
1367 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001368 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001369#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001370 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001371#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001372 ))
1373 {
1374 while (conv_restlen > 0)
1375 {
1376 *(--ptr) = bad_char_behavior;
1377 --conv_restlen;
1378 }
1379 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001380 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001381#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001382 if (iconv_fd != (iconv_t)-1)
1383 {
1384 iconv_close(iconv_fd);
1385 iconv_fd = (iconv_t)-1;
1386 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001387#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001388 }
1389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 skip_read = FALSE;
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /*
1395 * At start of file (or after crypt magic number): Check for BOM.
1396 * Also check for a BOM for other Unicode encodings, but not after
1397 * converting with 'charconvert' or when a BOM has already been
1398 * found.
1399 */
1400 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001401#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001402 || (cryptkey != NULL
1403 && filesize == crypt_get_header_len(
1404 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 )
1407 && (fio_flags == FIO_UCSBOM
1408 || (!curbuf->b_p_bomb
1409 && tmpname == NULL
1410 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1411 {
1412 char_u *ccname;
1413 int blen;
1414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (size < 2 || curbuf->b_p_bin)
1417 ccname = NULL;
1418 else
1419 ccname = check_for_bom(ptr, size, &blen,
1420 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1421 if (ccname != NULL)
1422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001423 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 filesize += blen;
1425 size -= blen;
1426 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001427 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001430 curbuf->b_start_bomb = TRUE;
1431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433
1434 if (fio_flags == FIO_UCSBOM)
1435 {
1436 if (ccname == NULL)
1437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 advance_fenc = TRUE;
1440 }
1441 else
1442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001443 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (fenc_alloced)
1445 vim_free(fenc);
1446 fenc = ccname;
1447 fenc_alloced = FALSE;
1448 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001449 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 skip_read = TRUE;
1451 goto retry;
1452 }
1453 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001455 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001456 ptr -= conv_restlen;
1457 size += conv_restlen;
1458 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 /*
1460 * Break here for a read error or end-of-file.
1461 */
1462 if (size <= 0)
1463 break;
1464
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar13505972019-01-24 15:04:48 +01001466#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (iconv_fd != (iconv_t)-1)
1468 {
1469 /*
1470 * Attempt conversion of the read bytes to 'encoding' using
1471 * iconv().
1472 */
1473 const char *fromp;
1474 char *top;
1475 size_t from_size;
1476 size_t to_size;
1477
1478 fromp = (char *)ptr;
1479 from_size = size;
1480 ptr += size;
1481 top = (char *)ptr;
1482 to_size = real_size - size;
1483
1484 /*
1485 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001486 * another conversion. Except for when there is no
1487 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001489 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1490 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1492 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001493 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001494 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001495 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001496 if (conv_error == 0)
1497 conv_error = readfile_linenr(linecnt,
1498 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001499
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001501 ++fromp;
1502 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001503 if (bad_char_behavior == BAD_KEEP)
1504 {
1505 *top++ = *(fromp - 1);
1506 --to_size;
1507 }
1508 else if (bad_char_behavior != BAD_DROP)
1509 {
1510 *top++ = bad_char_behavior;
1511 --to_size;
1512 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515 if (from_size > 0)
1516 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001517 // Some remaining characters, keep them for the next
1518 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1520 conv_restlen = (int)from_size;
1521 }
1522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 line_start = ptr - linerest;
1525 mch_memmove(line_start, buffer, (size_t)linerest);
1526 size = (long)((char_u *)top - ptr);
1527 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar4f974752019-02-17 17:44:42 +01001530#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (fio_flags & FIO_CODEPAGE)
1532 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001533 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001534 WCHAR ucs2buf[3];
1535 int ucs2len;
1536 int codepage = FIO_GET_CP(fio_flags);
1537 int bytelen;
1538 int found_bad;
1539 char replstr[2];
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 /*
1542 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001543 * a codepage, using standard MS-Windows functions. This
1544 * requires two steps:
1545 * 1. convert from 'fileencoding' to ucs-2
1546 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001548 * Because there may be illegal bytes AND an incomplete byte
1549 * sequence at the end, we may have to do the conversion one
1550 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001554 if (bad_char_behavior > 0)
1555 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001557 replstr[0] = '?';
1558 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
1560 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001561 * Move the bytes to the end of the buffer, so that we have
1562 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001564 src = ptr + real_size - size;
1565 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001567 /*
1568 * Do the conversion.
1569 */
1570 dst = ptr;
1571 size = size;
1572 while (size > 0)
1573 {
1574 found_bad = FALSE;
1575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001576# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001577 if (codepage == CP_UTF8)
1578 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001579 // Handle CP_UTF8 input ourselves to be able to handle
1580 // trailing bytes properly.
1581 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001582 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001583 if (bytelen > size)
1584 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001585 // Only got some bytes of a character. Normally
1586 // it's put in "conv_rest", but if it's too long
1587 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001588 if (bytelen <= CONV_RESTLEN)
1589 break;
1590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001591 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001592 bytelen = size;
1593 found_bad = TRUE;
1594 }
1595 else
1596 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001597 int u8c = utf_ptr2char(src);
1598
Bram Moolenaar86e01082005-12-29 22:45:34 +00001599 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001600 found_bad = TRUE;
1601 ucs2buf[0] = u8c;
1602 ucs2len = 1;
1603 }
1604 }
1605 else
1606# endif
1607 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001608 // We don't know how long the byte sequence is, try
1609 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001610 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1611 ++bytelen)
1612 {
1613 ucs2len = MultiByteToWideChar(codepage,
1614 MB_ERR_INVALID_CHARS,
1615 (LPCSTR)src, bytelen,
1616 ucs2buf, 3);
1617 if (ucs2len > 0)
1618 break;
1619 }
1620 if (ucs2len == 0)
1621 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001622 // If we have only one byte then it's probably an
1623 // incomplete byte sequence. Otherwise discard
1624 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001625 if (size == 1)
1626 break;
1627 found_bad = TRUE;
1628 bytelen = 1;
1629 }
1630 }
1631
1632 if (!found_bad)
1633 {
1634 int i;
1635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001636 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001637 if (enc_utf8)
1638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001640 for (i = 0; i < ucs2len; ++i)
1641 dst += utf_char2bytes(ucs2buf[i], dst);
1642 }
1643 else
1644 {
1645 BOOL bad = FALSE;
1646 int dstlen;
1647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001648 // From UCS-2 to "enc_codepage". If the
1649 // conversion uses the default character "?",
1650 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001651 dstlen = WideCharToMultiByte(enc_codepage, 0,
1652 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001653 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001654 replstr, &bad);
1655 if (bad)
1656 found_bad = TRUE;
1657 else
1658 dst += dstlen;
1659 }
1660 }
1661
1662 if (found_bad)
1663 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001664 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001665 if (can_retry)
1666 goto rewind_retry;
1667 if (conv_error == 0)
1668 conv_error = readfile_linenr(linecnt, ptr, dst);
1669 if (bad_char_behavior != BAD_DROP)
1670 {
1671 if (bad_char_behavior == BAD_KEEP)
1672 {
1673 mch_memmove(dst, src, bytelen);
1674 dst += bytelen;
1675 }
1676 else
1677 *dst++ = bad_char_behavior;
1678 }
1679 }
1680
1681 src += bytelen;
1682 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001684
1685 if (size > 0)
1686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001688 mch_memmove(conv_rest, src, size);
1689 conv_restlen = size;
1690 }
1691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001692 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001693 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001696#endif
1697#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (fio_flags & FIO_MACROMAN)
1699 {
1700 /*
1701 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001702 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001704 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
1707 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (fio_flags != 0)
1710 {
1711 int u8c;
1712 char_u *dest;
1713 char_u *tail = NULL;
1714
1715 /*
1716 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1717 * "enc_utf8" not set: Convert Unicode to Latin1.
1718 * Go from end to start through the buffer, because the number
1719 * of bytes may increase.
1720 * "dest" points to after where the UTF-8 bytes go, "p" points
1721 * to after the next character to convert.
1722 */
1723 dest = ptr + real_size;
1724 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1725 {
1726 p = ptr + size;
1727 if (fio_flags == FIO_UTF8)
1728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 tail = ptr + size - 1;
1731 while (tail > ptr && (*tail & 0xc0) == 0x80)
1732 --tail;
1733 if (tail + utf_byte2len(*tail) <= ptr + size)
1734 tail = NULL;
1735 else
1736 p = tail;
1737 }
1738 }
1739 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001741 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 p = ptr + (size & ~1);
1743 if (size & 1)
1744 tail = p;
1745 if ((fio_flags & FIO_UTF16) && p > ptr)
1746 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (fio_flags & FIO_ENDIAN_L)
1749 {
1750 u8c = (*--p << 8);
1751 u8c += *--p;
1752 }
1753 else
1754 {
1755 u8c = *--p;
1756 u8c += (*--p << 8);
1757 }
1758 if (u8c >= 0xd800 && u8c <= 0xdbff)
1759 tail = p;
1760 else
1761 p += 2;
1762 }
1763 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001764 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001766 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 p = ptr + (size & ~3);
1768 if (size & 3)
1769 tail = p;
1770 }
1771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // If there is a trailing incomplete sequence move it to
1773 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (tail != NULL)
1775 {
1776 conv_restlen = (int)((ptr + size) - tail);
1777 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1778 size -= conv_restlen;
1779 }
1780
1781
1782 while (p > ptr)
1783 {
1784 if (fio_flags & FIO_LATIN1)
1785 u8c = *--p;
1786 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1787 {
1788 if (fio_flags & FIO_ENDIAN_L)
1789 {
1790 u8c = (*--p << 8);
1791 u8c += *--p;
1792 }
1793 else
1794 {
1795 u8c = *--p;
1796 u8c += (*--p << 8);
1797 }
1798 if ((fio_flags & FIO_UTF16)
1799 && u8c >= 0xdc00 && u8c <= 0xdfff)
1800 {
1801 int u16c;
1802
1803 if (p == ptr)
1804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001805 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (can_retry)
1807 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001808 if (conv_error == 0)
1809 conv_error = readfile_linenr(linecnt,
1810 ptr, p);
1811 if (bad_char_behavior == BAD_DROP)
1812 continue;
1813 if (bad_char_behavior != BAD_KEEP)
1814 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001817 // found second word of double-word, get the first
1818 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (fio_flags & FIO_ENDIAN_L)
1820 {
1821 u16c = (*--p << 8);
1822 u16c += *--p;
1823 }
1824 else
1825 {
1826 u16c = *--p;
1827 u16c += (*--p << 8);
1828 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001829 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1830 + (u8c & 0x3ff);
1831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001832 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 if (u16c < 0xd800 || u16c > 0xdbff)
1834 {
1835 if (can_retry)
1836 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001837 if (conv_error == 0)
1838 conv_error = readfile_linenr(linecnt,
1839 ptr, p);
1840 if (bad_char_behavior == BAD_DROP)
1841 continue;
1842 if (bad_char_behavior != BAD_KEEP)
1843 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 }
1847 else if (fio_flags & FIO_UCS4)
1848 {
1849 if (fio_flags & FIO_ENDIAN_L)
1850 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001851 u8c = (unsigned)*--p << 24;
1852 u8c += (unsigned)*--p << 16;
1853 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 u8c += *--p;
1855 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001856 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001859 u8c += (unsigned)*--p << 8;
1860 u8c += (unsigned)*--p << 16;
1861 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001864 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
1866 if (*--p < 0x80)
1867 u8c = *p;
1868 else
1869 {
1870 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001871 p -= len;
1872 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (len == 0)
1874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001875 // Not a valid UTF-8 character, retry with
1876 // another fenc when possible, otherwise just
1877 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (can_retry)
1879 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001880 if (conv_error == 0)
1881 conv_error = readfile_linenr(linecnt,
1882 ptr, p);
1883 if (bad_char_behavior == BAD_DROP)
1884 continue;
1885 if (bad_char_behavior != BAD_KEEP)
1886 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001890 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 dest -= utf_char2len(u8c);
1893 (void)utf_char2bytes(u8c, dest);
1894 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001895 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 --dest;
1898 if (u8c >= 0x100)
1899 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001900 // character doesn't fit in latin1, retry with
1901 // another fenc when possible, otherwise just
1902 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001903 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001905 if (conv_error == 0)
1906 conv_error = readfile_linenr(linecnt, ptr, p);
1907 if (bad_char_behavior == BAD_DROP)
1908 ++dest;
1909 else if (bad_char_behavior == BAD_KEEP)
1910 *dest = u8c;
1911 else if (eap != NULL && eap->bad_char != 0)
1912 *dest = bad_char_behavior;
1913 else
1914 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 else
1917 *dest = u8c;
1918 }
1919 }
1920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001921 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 line_start = dest - linerest;
1923 mch_memmove(line_start, buffer, (size_t)linerest);
1924 size = (long)((ptr + real_size) - dest);
1925 ptr = dest;
1926 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001927 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001929 int incomplete_tail = FALSE;
1930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001931 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001932 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001934 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001935 int l;
1936
1937 if (todo <= 0)
1938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (*p >= 0x80)
1940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001941 // A length of 1 means it's an illegal byte. Accept
1942 // an incomplete character at the end though, the next
1943 // read() will get the next bytes, we'll check it
1944 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001945 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001946 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 // Avoid retrying with a different encoding when
1949 // a truncated file is more likely, or attempting
1950 // to read the rest of an incomplete sequence when
1951 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001952 if (p > ptr || filesize > 0)
1953 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 // Incomplete byte sequence, move it to conv_rest[]
1955 // and try to read the rest of it, unless we've
1956 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001957 if (p > ptr)
1958 {
1959 conv_restlen = todo;
1960 mch_memmove(conv_rest, p, conv_restlen);
1961 size -= conv_restlen;
1962 break;
1963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001965 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001966 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 // Illegal byte. If we can try another encoding
1968 // do that, unless at EOF where a truncated
1969 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001970 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001971 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001972#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001973 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001974 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1975 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001976#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001978 if (conv_error == 0 && illegal_byte == 0)
1979 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001982 if (bad_char_behavior == BAD_DROP)
1983 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001984 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001985 --p;
1986 --size;
1987 }
1988 else if (bad_char_behavior != BAD_KEEP)
1989 *p = bad_char_behavior;
1990 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001991 else
1992 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001995 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001999 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002000#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002001 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002003 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002005#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002006 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002007 advance_fenc = TRUE;
2008 file_rewind = TRUE;
2009 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 filesize += size;
2015
2016 /*
2017 * when reading the first part of a file: guess EOL type
2018 */
2019 if (fileformat == EOL_UNKNOWN)
2020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002021 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (try_dos || try_unix)
2023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002025 if (try_mac)
2026 try_mac = 1;
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 for (p = ptr; p < ptr + size; ++p)
2029 {
2030 if (*p == NL)
2031 {
2032 if (!try_unix
2033 || (try_dos && p > ptr && p[-1] == CAR))
2034 fileformat = EOL_DOS;
2035 else
2036 fileformat = EOL_UNIX;
2037 break;
2038 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002039 else if (*p == CAR && try_mac)
2040 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (fileformat == EOL_UNIX && try_mac)
2045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002046 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 try_mac = 1;
2048 try_unix = 1;
2049 for (; p >= ptr && *p != CAR; p--)
2050 ;
2051 if (p >= ptr)
2052 {
2053 for (p = ptr; p < ptr + size; ++p)
2054 {
2055 if (*p == NL)
2056 try_unix++;
2057 else if (*p == CAR)
2058 try_mac++;
2059 }
2060 if (try_mac > try_unix)
2061 fileformat = EOL_MAC;
2062 }
2063 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002064 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Looking for CR but found no end-of-line markers at
2066 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002067 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (fileformat == EOL_UNKNOWN && try_mac)
2072 fileformat = EOL_MAC;
2073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (fileformat == EOL_UNKNOWN)
2076 fileformat = default_fileformat();
2077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002079 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 set_fileformat(fileformat, OPT_LOCAL);
2081 }
2082 }
2083
2084 /*
2085 * This loop is executed once for every character read.
2086 * Keep it fast!
2087 */
2088 if (fileformat == EOL_MAC)
2089 {
2090 --ptr;
2091 while (++ptr, --size >= 0)
2092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 if ((c = *ptr) != NUL && c != CAR && c != NL)
2095 continue;
2096 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002097 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 else
2101 {
2102 if (skip_count == 0)
2103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002104 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 len = (colnr_T) (ptr - line_start + 1);
2106 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2107 {
2108 error = TRUE;
2109 break;
2110 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002111#ifdef FEAT_PERSISTENT_UNDO
2112 if (read_undo_file)
2113 sha256_update(&sha_ctx, line_start, len);
2114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ++lnum;
2116 if (--read_count == 0)
2117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 error = TRUE; // break loop
2119 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 break;
2121 }
2122 }
2123 else
2124 --skip_count;
2125 line_start = ptr + 1;
2126 }
2127 }
2128 }
2129 else
2130 {
2131 --ptr;
2132 while (++ptr, --size >= 0)
2133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 continue;
2136 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
2139 {
2140 if (skip_count == 0)
2141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002142 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 len = (colnr_T)(ptr - line_start + 1);
2144 if (fileformat == EOL_DOS)
2145 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002146 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002148 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 ptr[-1] = NUL;
2150 --len;
2151 }
2152 /*
2153 * Reading in Dos format, but no CR-LF found!
2154 * When 'fileformats' includes "unix", delete all
2155 * the lines read so far and start all over again.
2156 * Otherwise give an error message later.
2157 */
2158 else if (ff_error != EOL_DOS)
2159 {
2160 if ( try_unix
2161 && !read_stdin
2162 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002163 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2164 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002167 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 set_fileformat(EOL_UNIX, OPT_LOCAL);
2169 file_rewind = TRUE;
2170 keep_fileformat = TRUE;
2171 goto retry;
2172 }
2173 ff_error = EOL_DOS;
2174 }
2175 }
2176 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2177 {
2178 error = TRUE;
2179 break;
2180 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002181#ifdef FEAT_PERSISTENT_UNDO
2182 if (read_undo_file)
2183 sha256_update(&sha_ctx, line_start, len);
2184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 ++lnum;
2186 if (--read_count == 0)
2187 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002188 error = TRUE; // break loop
2189 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 break;
2191 }
2192 }
2193 else
2194 --skip_count;
2195 line_start = ptr + 1;
2196 }
2197 }
2198 }
2199 linerest = (long)(ptr - line_start);
2200 ui_breakcheck();
2201 }
2202
2203failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (error && read_count == 0)
2206 error = FALSE;
2207
2208 /*
2209 * If we get EOF in the middle of a line, note the fact and
2210 * complete the line ourselves.
2211 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2212 */
2213 if (!error
2214 && !got_int
2215 && linerest != 0
2216 && !(!curbuf->b_p_bin
2217 && fileformat == EOL_DOS
2218 && *line_start == Ctrl_Z
2219 && ptr == line_start + 1))
2220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002222 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 curbuf->b_p_eol = FALSE;
2224 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002225 len = (colnr_T)(ptr - line_start + 1);
2226 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 error = TRUE;
2228 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002229 {
2230#ifdef FEAT_PERSISTENT_UNDO
2231 if (read_undo_file)
2232 sha256_update(&sha_ctx, line_start, len);
2233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002242 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002243 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002244 crypt_free_state(curbuf->b_cryptstate);
2245 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002246 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002247 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2248 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 // Don't set cryptkey to NULL, it's used below as a flag that
2250 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 // If editing a new file: set 'fenc' for the current buffer.
2254 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002257 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (fenc_alloced)
2259 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002260#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#endif
2264
2265 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002266 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002267#ifdef HAVE_FD_CLOEXEC
2268 else
2269 {
2270 int fdflags = fcntl(fd, F_GETFD);
2271 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002272 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002273 }
2274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 vim_free(buffer);
2276
2277#ifdef HAVE_DUP
2278 if (read_stdin)
2279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002280 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002282 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284#endif
2285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (tmpname != NULL)
2287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002288 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 vim_free(tmpname);
2290 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
2293 /*
2294 * In recovery mode everything but autocommands is skipped.
2295 */
2296 if (!recoverymode)
2297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2300 {
2301#ifdef FEAT_NETBEANS_INTG
2302 netbeansFireChanges = 0;
2303#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002304 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#ifdef FEAT_NETBEANS_INTG
2306 netbeansFireChanges = 1;
2307#endif
2308 --linecnt;
2309 }
2310 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2311 if (filesize == 0)
2312 linecnt = 0;
2313 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002316#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002317 // After reading the text into the buffer the diff info needs to
2318 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002319 diff_invalidate(curbuf);
2320#endif
2321#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 // All folds in the window are invalid now. Mark them for update
2323 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002324 foldUpdateAll(curwin);
2325#endif
2326 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 appended_lines_mark(from, linecnt);
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#ifndef ALWAYS_USE_GUI
2331 /*
2332 * If we were reading from the same terminal as where messages go,
2333 * the screen will have been messed up.
2334 * Switch on raw mode now and clear the screen.
2335 */
2336 if (read_stdin)
2337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 starttermcap();
2340 screenclear();
2341 }
2342#endif
2343
2344 if (got_int)
2345 {
2346 if (!(flags & READ_DUMMY))
2347 {
2348 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2349 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 msg_scroll = msg_save;
2353#ifdef FEAT_VIMINFO
2354 check_marks_read();
2355#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
2359 if (!filtering && !(flags & READ_DUMMY))
2360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 c = FALSE;
2363
2364#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 STRCAT(IObuff, _("[fifo]"));
2368 c = TRUE;
2369 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 STRCAT(IObuff, _("[socket]"));
2373 c = TRUE;
2374 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002375# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002376 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002377 {
2378 STRCAT(IObuff, _("[character special]"));
2379 c = TRUE;
2380 }
2381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#endif
2383 if (curbuf->b_p_ro)
2384 {
2385 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2386 c = TRUE;
2387 }
2388 if (read_no_eol_lnum)
2389 {
2390 msg_add_eol();
2391 c = TRUE;
2392 }
2393 if (ff_error == EOL_DOS)
2394 {
2395 STRCAT(IObuff, _("[CR missing]"));
2396 c = TRUE;
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (split)
2399 {
2400 STRCAT(IObuff, _("[long lines split]"));
2401 c = TRUE;
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (notconverted)
2404 {
2405 STRCAT(IObuff, _("[NOT converted]"));
2406 c = TRUE;
2407 }
2408 else if (converted)
2409 {
2410 STRCAT(IObuff, _("[converted]"));
2411 c = TRUE;
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#ifdef FEAT_CRYPT
2414 if (cryptkey != NULL)
2415 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002416 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 c = TRUE;
2418 }
2419#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002420 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002422 sprintf((char *)IObuff + STRLEN(IObuff),
2423 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 c = TRUE;
2425 }
2426 else if (illegal_byte > 0)
2427 {
2428 sprintf((char *)IObuff + STRLEN(IObuff),
2429 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2430 c = TRUE;
2431 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002432 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 STRCAT(IObuff, _("[READ ERRORS]"));
2435 c = TRUE;
2436 }
2437 if (msg_add_fileformat(fileformat))
2438 c = TRUE;
2439#ifdef FEAT_CRYPT
2440 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002441 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002442 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 else
2444#endif
2445 msg_add_lines(c, (long)linecnt, filesize);
2446
Bram Moolenaard23a8232018-02-10 18:45:26 +01002447 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 msg_scrolled_ign = TRUE;
2449#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002450 // Don't show the message when reading stdin, it would end up in a
2451 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (read_stdin || read_buffer)
2453 p = msg_may_trunc(FALSE, IObuff);
2454 else
2455#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002456 {
2457 if (msg_col > 0)
2458 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002459 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002462 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 // Need to repeat the message after redrawing when:
2464 // - When reading from stdin (the screen will be cleared next).
2465 // - When restart_edit is set (otherwise there will be a delay
2466 // before redrawing).
2467 // - When the screen was scrolled but there is no wait-return
2468 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002469 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 msg_scrolled_ign = FALSE;
2471 }
2472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002473 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002475 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002476 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 curbuf->b_p_ro = TRUE;
2478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002479 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
2482 * In Ex mode: cursor at last new line.
2483 * Otherwise: cursor at first new line.
2484 */
2485 if (exmode_active)
2486 curwin->w_cursor.lnum = from + linecnt;
2487 else
2488 curwin->w_cursor.lnum = from + 1;
2489 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002490 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002492 if (!cmdmod.lockmarks)
2493 {
2494 // Set '[ and '] marks to the newly read lines.
2495 curbuf->b_op_start.lnum = from + 1;
2496 curbuf->b_op_start.col = 0;
2497 curbuf->b_op_end.lnum = from + linecnt;
2498 curbuf->b_op_end.col = 0;
2499 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002500
Bram Moolenaar4f974752019-02-17 17:44:42 +01002501#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002502 /*
2503 * Work around a weird problem: When a file has two links (only
2504 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002505 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002506 * It's correct again after reading the file, thus reset the timestamp
2507 * here.
2508 */
2509 if (newfile && !read_stdin && !read_buffer
2510 && mch_stat((char *)fname, &st) >= 0)
2511 {
2512 buf_store_time(curbuf, &st, fname);
2513 curbuf->b_mtime_read = curbuf->b_mtime;
2514 }
2515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 msg_scroll = msg_save;
2518
2519#ifdef FEAT_VIMINFO
2520 /*
2521 * Get the marks before executing autocommands, so they can be used there.
2522 */
2523 check_marks_read();
2524#endif
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002527 * We remember if the last line of the read didn't have
2528 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2529 * or writing the read again with 'binary' on. The latter is required
2530 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002532 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002534 // When reloading a buffer put the cursor at the first line that is
2535 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002536 if (flags & READ_KEEP_UNDO)
2537 u_find_first_changed();
2538
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002539#ifdef FEAT_PERSISTENT_UNDO
2540 /*
2541 * When opening a new file locate undo info and read it.
2542 */
2543 if (read_undo_file)
2544 {
2545 char_u hash[UNDO_HASH_SIZE];
2546
2547 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002548 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002549 }
2550#endif
2551
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002552 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 int m = msg_scroll;
2555 int n = msg_scrolled;
2556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002557 // Save the fileformat now, otherwise the buffer will be considered
2558 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002559 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 save_file_ff(curbuf);
2561
2562 /*
2563 * The output from the autocommands should not overwrite anything and
2564 * should not be overwritten: Set msg_scroll, restore its value if no
2565 * output was done.
2566 */
2567 msg_scroll = TRUE;
2568 if (filtering)
2569 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2570 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002571 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2574 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002575 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2576 /*
2577 * EVENT_FILETYPE was not triggered but the buffer already has a
2578 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2579 */
2580 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2581 TRUE, curbuf);
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 else
2584 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2585 FALSE, NULL, eap);
2586 if (msg_scrolled == n)
2587 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002588# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002589 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 if (recoverymode && error)
2595 return FAIL;
2596 return OK;
2597}
2598
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002599#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002600/*
2601 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2602 * which is the name of files used for process substitution output by
2603 * some shells on some operating systems, e.g., bash on SunOS.
2604 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2605 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002606 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002607is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002608{
2609 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2610 && VIM_ISDIGIT(fname[8])
2611 && *skipdigits(fname + 9) == NUL
2612 && (fname[9] != NUL
2613 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2614}
2615#endif
2616
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002617/*
2618 * From the current line count and characters read after that, estimate the
2619 * line number where we are now.
2620 * Used for error messages that include a line number.
2621 */
2622 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002624 linenr_T linecnt, // line count before reading more bytes
2625 char_u *p, // start of more bytes read
2626 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002627{
2628 char_u *s;
2629 linenr_T lnum;
2630
2631 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2632 for (s = p; s < endp; ++s)
2633 if (*s == '\n')
2634 ++lnum;
2635 return lnum;
2636}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002639 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2640 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * Returns OK or FAIL.
2642 */
2643 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002644prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
Bram Moolenaar13505972019-01-24 15:04:48 +01002646 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (eap->cmd == NULL)
2648 return FAIL;
2649
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002650 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2651 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002652 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002653 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002654
2655 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002656 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002657 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return OK;
2659}
2660
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002661/*
2662 * Set default or forced 'fileformat' and 'binary'.
2663 */
2664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002665set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002666{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002668 if (set_options)
2669 {
2670 if (eap != NULL && eap->force_ff != 0)
2671 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2672 else if (*p_ffs != NUL)
2673 set_fileformat(default_fileformat(), OPT_LOCAL);
2674 }
2675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002677 if (eap != NULL && eap->force_bin != 0)
2678 {
2679 int oldval = curbuf->b_p_bin;
2680
2681 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2682 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2683 }
2684}
2685
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002686/*
2687 * Set forced 'fileencoding'.
2688 */
2689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002690set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002691{
2692 if (eap->force_enc != 0)
2693 {
2694 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2695
2696 if (fenc != NULL)
2697 set_string_option_direct((char_u *)"fenc", -1,
2698 fenc, OPT_FREE|OPT_LOCAL, 0);
2699 vim_free(fenc);
2700 }
2701}
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703/*
2704 * Find next fileencoding to use from 'fileencodings'.
2705 * "pp" points to fenc_next. It's advanced to the next item.
2706 * When there are no more items, an empty string is returned and *pp is set to
2707 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002708 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2709 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 */
2711 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002712next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 char_u *p;
2715 char_u *r;
2716
Bram Moolenaarf077db22019-08-13 00:18:24 +02002717 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (**pp == NUL)
2719 {
2720 *pp = NULL;
2721 return (char_u *)"";
2722 }
2723 p = vim_strchr(*pp, ',');
2724 if (p == NULL)
2725 {
2726 r = enc_canonize(*pp);
2727 *pp += STRLEN(*pp);
2728 }
2729 else
2730 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002731 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 *pp = p + 1;
2733 if (r != NULL)
2734 {
2735 p = enc_canonize(r);
2736 vim_free(r);
2737 r = p;
2738 }
2739 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002740 if (r != NULL)
2741 *alloced = TRUE;
2742 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002744 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 r = (char_u *)"";
2746 *pp = NULL;
2747 }
2748 return r;
2749}
2750
Bram Moolenaar13505972019-01-24 15:04:48 +01002751#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752/*
2753 * Convert a file with the 'charconvert' expression.
2754 * This closes the file which is to be read, converts it and opens the
2755 * resulting file for reading.
2756 * Returns name of the resulting converted file (the caller should delete it
2757 * after reading it).
2758 * Returns NULL if the conversion failed ("*fdp" is not set) .
2759 */
2760 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002761readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002762 char_u *fname, // name of input file
2763 char_u *fenc, // converted from
2764 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002767 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002769 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002771 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002774 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 *fdp = -1;
2776 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2777 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002778 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2780 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002781 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
2783
2784 if (errmsg != NULL)
2785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002786 // Don't use emsg(), it breaks mappings, the retry with
2787 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002788 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if (tmpname != NULL)
2790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002791 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002792 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 }
2795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002796 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (*fdp < 0)
2798 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2799
2800 return tmpname;
2801}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802#endif
2803
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002804#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002806 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2808 * *filesizep are updated.
2809 * Return the (new) encryption key, NULL for no encryption.
2810 */
2811 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002812check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002813 char_u *cryptkey, // previous encryption key or NULL
2814 char_u *ptr, // pointer to read bytes
2815 long *sizep, // length of read bytes
2816 off_T *filesizep, // nr of bytes used from file
2817 int newfile, // editing a new buffer
2818 char_u *fname, // file name to display
2819 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002821 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002822 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002823
2824 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002826 // Mark the buffer as read-only until the decryption has taken place.
2827 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002828 curbuf->b_p_ro = TRUE;
2829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002830 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002831 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002832 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 if (*curbuf->b_p_key)
2835 cryptkey = curbuf->b_p_key;
2836 else
2837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002838 // When newfile is TRUE, store the typed key in the 'key'
2839 // option and don't free it. bf needs hash of the key saved.
2840 // Don't ask for the key again when first time Enter was hit.
2841 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002843 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002844 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002845 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002846 *did_ask = TRUE;
2847
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002848 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (cryptkey != NULL && *cryptkey == NUL)
2850 {
2851 if (cryptkey != curbuf->b_p_key)
2852 vim_free(cryptkey);
2853 cryptkey = NULL;
2854 }
2855 }
2856 }
2857
2858 if (cryptkey != NULL)
2859 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002860 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002861
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002862 curbuf->b_cryptstate = crypt_create_from_header(
2863 method, cryptkey, ptr);
2864 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002866 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002867 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002868 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002869 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002870 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002871 *filesizep += header_len;
2872 *sizep -= header_len;
2873 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002876 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
2878 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 // When starting to edit a new file which does not have encryption, clear
2880 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002881 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2883
2884 return cryptkey;
2885}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002886#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002889 * Return TRUE if a file appears to be read-only from the file permissions.
2890 */
2891 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002892check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002893 char_u *fname, // full path to file
2894 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002895{
2896#ifndef USE_MCH_ACCESS
2897 int fd = 0;
2898#endif
2899
2900 return (
2901#ifdef USE_MCH_ACCESS
2902# ifdef UNIX
2903 (perm & 0222) == 0 ||
2904# endif
2905 mch_access((char *)fname, W_OK)
2906#else
2907 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2908 ? TRUE : (close(fd), FALSE)
2909#endif
2910 );
2911}
2912
Bram Moolenaara7870192019-02-14 12:56:36 +01002913#if defined(HAVE_FSYNC) || defined(PROTO)
2914/*
2915 * Call fsync() with Mac-specific exception.
2916 * Return fsync() result: zero for success.
2917 */
2918 int
2919vim_fsync(int fd)
2920{
2921 int r;
2922
2923# ifdef MACOS_X
2924 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002925 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002926# endif
2927 r = fsync(fd);
2928 return r;
2929}
2930#endif
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002933 * Set the name of the current buffer. Use when the buffer doesn't have a
2934 * name and a ":r" or ":w" command with a file name is used.
2935 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002936 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002937set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002938{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002939 buf_T *buf = curbuf;
2940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002941 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002942 if (curbuf->b_p_bl)
2943 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2944 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002945#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002946 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002947 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002948#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002949 if (curbuf != buf)
2950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002951 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002953 return FAIL;
2954 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002955
2956 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2957 curbuf->b_flags |= BF_NOTEDITED;
2958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002959 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002960 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2961 if (curbuf->b_p_bl)
2962 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002963#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002965 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002966#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002968 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002969 if (*curbuf->b_p_ft == NUL)
2970 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002971 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002972 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002973 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002974 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002975
2976 return OK;
2977}
2978
2979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 * Put file name into IObuff with quotes.
2981 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002982 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002983msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 if (fname == NULL)
2986 fname = (char_u *)"-stdin-";
2987 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2988 IObuff[0] = '"';
2989 STRCAT(IObuff, "\" ");
2990}
2991
2992/*
2993 * Append message for text mode to IObuff.
2994 * Return TRUE if something appended.
2995 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002996 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002997msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999#ifndef USE_CRNL
3000 if (eol_type == EOL_DOS)
3001 {
3002 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3003 return TRUE;
3004 }
3005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (eol_type == EOL_MAC)
3007 {
3008 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3009 return TRUE;
3010 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003011#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (eol_type == EOL_UNIX)
3013 {
3014 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3015 return TRUE;
3016 }
3017#endif
3018 return FALSE;
3019}
3020
3021/*
3022 * Append line and character count to IObuff.
3023 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003024 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025msg_add_lines(
3026 int insert_space,
3027 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003028 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 char_u *p;
3031
3032 p = IObuff + STRLEN(IObuff);
3033
3034 if (insert_space)
3035 *p++ = ' ';
3036 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003037 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003038 "%ldL, %lldC", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 else
3040 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003041 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003043 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
3044 NGETTEXT("%lld character", "%lld characters", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003045 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047}
3048
3049/*
3050 * Append message for missing line separator to IObuff.
3051 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3056}
3057
Bram Moolenaar473952e2019-09-28 16:30:04 +02003058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003061#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003062 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3063 // the seconds. Since the roundoff is done when flushing the inode, the
3064 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 return (t1 - t2 > 1 || t2 - t1 > 1);
3066#else
3067 return (t1 != t2);
3068#endif
3069}
3070
3071/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003072 * Return TRUE if file encoding "fenc" requires conversion from or to
3073 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003075 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003078 int same_encoding;
3079 int enc_flags;
3080 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003082 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003083 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003084 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003085 fenc_flags = 0;
3086 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003087 else
3088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003089 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3090 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003091 enc_flags = get_fio_flags(p_enc);
3092 fenc_flags = get_fio_flags(fenc);
3093 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3094 }
3095 if (same_encoding)
3096 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003097 // Specified encoding matches with 'encoding'. This requires
3098 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003099 return enc_unicode != 0;
3100 }
3101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003102 // Encodings differ. However, conversion is not needed when 'enc' is any
3103 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003104 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106
3107/*
3108 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3109 * internal conversion.
3110 * if "ptr" is an empty string, use 'encoding'.
3111 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003112 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003113get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 int prop;
3116
3117 if (*ptr == NUL)
3118 ptr = p_enc;
3119
3120 prop = enc_canon_props(ptr);
3121 if (prop & ENC_UNICODE)
3122 {
3123 if (prop & ENC_2BYTE)
3124 {
3125 if (prop & ENC_ENDIAN_L)
3126 return FIO_UCS2 | FIO_ENDIAN_L;
3127 return FIO_UCS2;
3128 }
3129 if (prop & ENC_4BYTE)
3130 {
3131 if (prop & ENC_ENDIAN_L)
3132 return FIO_UCS4 | FIO_ENDIAN_L;
3133 return FIO_UCS4;
3134 }
3135 if (prop & ENC_2WORD)
3136 {
3137 if (prop & ENC_ENDIAN_L)
3138 return FIO_UTF16 | FIO_ENDIAN_L;
3139 return FIO_UTF16;
3140 }
3141 return FIO_UTF8;
3142 }
3143 if (prop & ENC_LATIN1)
3144 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003145 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 return 0;
3147}
3148
Bram Moolenaar473952e2019-09-28 16:30:04 +02003149#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150/*
3151 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3152 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3153 * Used for conversion between 'encoding' and 'fileencoding'.
3154 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003155 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003156get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 int cp;
3159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003160 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (!enc_utf8 && enc_codepage <= 0)
3162 return 0;
3163
3164 cp = encname2codepage(ptr);
3165 if (cp == 0)
3166 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003167# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (STRCMP(ptr, "utf-8") == 0)
3169 cp = CP_UTF8;
3170 else
3171# endif
3172 return 0;
3173 }
3174 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3175}
3176#endif
3177
Bram Moolenaar473952e2019-09-28 16:30:04 +02003178#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179/*
3180 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3181 * needed for the internal conversion to/from utf-8 or latin1.
3182 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003183 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3187 && (enc_canon_props(ptr) & ENC_MACROMAN))
3188 return FIO_MACROMAN;
3189 return 0;
3190}
3191#endif
3192
3193/*
3194 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3195 * "size" must be at least 2.
3196 * Return the name of the encoding and set "*lenp" to the length.
3197 * Returns NULL when no BOM found.
3198 */
3199 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003200check_for_bom(
3201 char_u *p,
3202 long size,
3203 int *lenp,
3204 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 char *name = NULL;
3207 int len = 2;
3208
3209 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003210 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003212 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 len = 3;
3214 }
3215 else if (p[0] == 0xff && p[1] == 0xfe)
3216 {
3217 if (size >= 4 && p[2] == 0 && p[3] == 0
3218 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003220 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 len = 4;
3222 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003223 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003224 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003225 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003226 // utf-16le is preferred, it also works for ucs-2le text
3227 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 else if (p[0] == 0xfe && p[1] == 0xff
3230 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3231 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003232 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003233 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003234 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003235 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003236 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3239 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003241 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 len = 4;
3243 }
3244
3245 *lenp = len;
3246 return (char_u *)name;
3247}
3248
3249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * Try to find a shortname by comparing the fullname with the current
3251 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003252 * Returns "full_path" or pointer into "full_path" if shortened.
3253 */
3254 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003255shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003256{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003257 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003258 char_u *p = full_path;
3259
Bram Moolenaard9462e32011-04-11 21:35:11 +02003260 dirname = alloc(MAXPATHL);
3261 if (dirname == NULL)
3262 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003263 if (mch_dirname(dirname, MAXPATHL) == OK)
3264 {
3265 p = shorten_fname(full_path, dirname);
3266 if (p == NULL || *p == NUL)
3267 p = full_path;
3268 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003269 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003270 return p;
3271}
3272
3273/*
3274 * Try to find a shortname by comparing the fullname with the current
3275 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 * Returns NULL if not shorter name possible, pointer into "full_path"
3277 * otherwise.
3278 */
3279 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003280shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 int len;
3283 char_u *p;
3284
3285 if (full_path == NULL)
3286 return NULL;
3287 len = (int)STRLEN(dir_name);
3288 if (fnamencmp(dir_name, full_path, len) == 0)
3289 {
3290 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003291#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003293 * MS-Windows: when a file is in the root directory, dir_name will end
3294 * in a slash, since C: by itself does not define a specific dir. In
3295 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 */
3297 if (!((len > 2) && (*(p - 2) == ':')))
3298#endif
3299 {
3300 if (vim_ispathsep(*p))
3301 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003302#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 else
3304 p = NULL;
3305#endif
3306 }
3307 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003308#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /*
3310 * When using a file in the current drive, remove the drive name:
3311 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3312 * a floppy from "A:\dir" to "B:\dir".
3313 */
3314 else if (len > 3
3315 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3316 && full_path[1] == ':'
3317 && vim_ispathsep(full_path[2]))
3318 p = full_path + 2;
3319#endif
3320 else
3321 p = NULL;
3322 return p;
3323}
3324
3325/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003326 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 * When "force" is TRUE: Use full path from now on for files currently being
3328 * edited, both for file name and swap file name. Try to shorten the file
3329 * names a bit, if safe to do so.
3330 * When "force" is FALSE: Only try to shorten absolute file names.
3331 * For buffers that have buftype "nofile" or "scratch": never change the file
3332 * name.
3333 */
3334 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003335shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3336{
3337 char_u *p;
3338
3339 if (buf->b_fname != NULL
3340#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003341 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003342#endif
3343 && !path_with_url(buf->b_fname)
3344 && (force
3345 || buf->b_sfname == NULL
3346 || mch_isFullName(buf->b_sfname)))
3347 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003348 if (buf->b_sfname != buf->b_ffname)
3349 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003350 p = shorten_fname(buf->b_ffname, dirname);
3351 if (p != NULL)
3352 {
3353 buf->b_sfname = vim_strsave(p);
3354 buf->b_fname = buf->b_sfname;
3355 }
3356 if (p == NULL || buf->b_fname == NULL)
3357 buf->b_fname = buf->b_ffname;
3358 }
3359}
3360
3361/*
3362 * Shorten filenames for all buffers.
3363 */
3364 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003365shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 char_u dirname[MAXPATHL];
3368 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003371 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003373 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003374
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003375 // Always make the swap file name a full path, a "nofile" buffer may
3376 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003377 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003380 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003381#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003382 popup_update_preview_title();
3383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384}
3385
3386#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3387 || defined(FEAT_GUI_MSWIN) \
3388 || defined(FEAT_GUI_MAC) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003389 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 || defined(PROTO)
3391/*
3392 * Shorten all filenames in "fnames[count]" by current directory.
3393 */
3394 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003395shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 int i;
3398 char_u dirname[MAXPATHL];
3399 char_u *p;
3400
3401 if (fnames == NULL || count < 1)
3402 return;
3403 mch_dirname(dirname, sizeof(dirname));
3404 for (i = 0; i < count; ++i)
3405 {
3406 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3407 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003408 // shorten_fname() returns pointer in given "fnames[i]". If free
3409 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3410 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 p = vim_strsave(p);
3412 vim_free(fnames[i]);
3413 fnames[i] = p;
3414 }
3415 }
3416}
3417#endif
3418
3419/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003420 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 * fo_o_h.ext for MSDOS or when shortname option set.
3422 *
3423 * Assumed that fname is a valid name found in the filesystem we assure that
3424 * the return value is a different name and ends in 'ext'.
3425 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3426 * characters otherwise.
3427 * Space for the returned name is allocated, must be freed later.
3428 * Returns NULL when out of memory.
3429 */
3430 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003431modname(
3432 char_u *fname,
3433 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003434 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003436 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 fname, ext, prepend_dot);
3438}
3439
3440 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003441buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003442 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003443 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{
3447 char_u *retval;
3448 char_u *s;
3449 char_u *e;
3450 char_u *ptr;
3451 int fnamelen, extlen;
3452
3453 extlen = (int)STRLEN(ext);
3454
3455 /*
3456 * If there is no file name we must get the name of the current directory
3457 * (we need the full path in case :cd is used).
3458 */
3459 if (fname == NULL || *fname == NUL)
3460 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003461 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (retval == NULL)
3463 return NULL;
3464 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3465 (fnamelen = (int)STRLEN(retval)) == 0)
3466 {
3467 vim_free(retval);
3468 return NULL;
3469 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003470 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
3472 retval[fnamelen++] = PATHSEP;
3473 retval[fnamelen] = NUL;
3474 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003475 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477 else
3478 {
3479 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003480 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (retval == NULL)
3482 return NULL;
3483 STRCPY(retval, fname);
3484#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003485 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#endif
3487 }
3488
3489 /*
3490 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3491 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3492 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3493 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3494 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003495 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003497 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003498 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003501 {
3502 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003507 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3509 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 s = ptr + STRLEN(ptr);
3512
3513 /*
3514 * For 8.3 file names we may have to reduce the length.
3515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 /*
3519 * If there is no file name, or the file name ends in '/', and the
3520 * extension starts with '.', put a '_' before the dot, because just
3521 * ".ext" is invalid.
3522 */
3523 if (fname == NULL || *fname == NUL
3524 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 *s++ = '_';
3528 }
3529 /*
3530 * If the extension starts with '.', truncate the base name at 8
3531 * characters
3532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003535 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 s = ptr + 8;
3538 *s = '\0';
3539 }
3540 }
3541 /*
3542 * If the extension doesn't start with '.', and the file name
3543 * doesn't have an extension yet, append a '.'
3544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else if ((e = vim_strchr(ptr, '.')) == NULL)
3546 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /*
3548 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003549 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 */
3551 else if ((int)STRLEN(e) + extlen > 4)
3552 s = e + 4 - extlen;
3553 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003554#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /*
3556 * If there is no file name, and the extension starts with '.', put a
3557 * '_' before the dot, because just ".ext" may be invalid if it's on a
3558 * FAT partition, and on HPFS it doesn't matter.
3559 */
3560 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3561 *s++ = '_';
3562#endif
3563
3564 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003565 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 * ext can start with '.' and cannot exceed 3 more characters.
3567 */
3568 STRCPY(s, ext);
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 /*
3571 * Prepend the dot.
3572 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003573 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003575 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 /*
3580 * Check that, after appending the extension, the file name is really
3581 * different.
3582 */
3583 if (fname != NULL && STRCMP(fname, retval) == 0)
3584 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003585 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 while (--s >= ptr)
3587 {
3588 if (*s != '_')
3589 {
3590 *s = '_';
3591 break;
3592 }
3593 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003594 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 *ptr = 'v';
3596 }
3597 return retval;
3598}
3599
3600/*
3601 * Like fgets(), but if the file line is too long, it is truncated and the
3602 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003603 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 */
3605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003606vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
3608 char *eof;
3609#define FGETS_SIZE 200
3610 char tbuf[FGETS_SIZE];
3611
3612 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003616 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003618 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 do
3620 {
3621 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003622 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3624 }
3625 return (eof == NULL);
3626}
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628/*
3629 * rename() only works if both files are on the same file system, this
3630 * function will (attempts to?) copy the file across if rename fails -- webb
3631 * Return -1 for failure, 0 for success.
3632 */
3633 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003634vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
3636 int fd_in;
3637 int fd_out;
3638 int n;
3639 char *errmsg = NULL;
3640 char *buffer;
3641#ifdef AMIGA
3642 BPTR flock;
3643#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003644 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003645 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003646#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003647 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003648#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003649 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003652 * When the names are identical, there is nothing to do. When they refer
3653 * to the same file (ignoring case and slash/backslash differences) but
3654 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
3656 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003657 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003658 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003659 use_tmp_file = TRUE;
3660 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003661 return 0;
3662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 /*
3665 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3666 */
3667 if (mch_stat((char *)from, &st) < 0)
3668 return -1;
3669
Bram Moolenaar3576da72008-12-30 15:15:57 +00003670#ifdef UNIX
3671 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003672 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003674 // It's possible for the source and destination to be the same file.
3675 // This happens when "from" and "to" differ in case and are on a FAT32
3676 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003677 if (mch_stat((char *)to, &st_to) >= 0
3678 && st.st_dev == st_to.st_dev
3679 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003680 use_tmp_file = TRUE;
3681 }
3682#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003683#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003684 {
3685 BY_HANDLE_FILE_INFORMATION info1, info2;
3686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003687 // It's possible for the source and destination to be the same file.
3688 // In that case go through a temp file name. This makes rename("foo",
3689 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003690 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3691 && win32_fileinfo(to, &info2) == FILEINFO_OK
3692 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3693 && info1.nFileIndexHigh == info2.nFileIndexHigh
3694 && info1.nFileIndexLow == info2.nFileIndexLow)
3695 use_tmp_file = TRUE;
3696 }
3697#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003698
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003699 if (use_tmp_file)
3700 {
3701 char tempname[MAXPATHL + 1];
3702
3703 /*
3704 * Find a name that doesn't exist and is in the same directory.
3705 * Rename "from" to "tempname" and then rename "tempname" to "to".
3706 */
3707 if (STRLEN(from) >= MAXPATHL - 5)
3708 return -1;
3709 STRCPY(tempname, from);
3710 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003711 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003712 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3713 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003714 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003715 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003716 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003717 if (mch_rename(tempname, (char *)to) == 0)
3718 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 // Strange, the second step failed. Try moving the
3720 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003721 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003722 return -1;
3723 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003724 // If it fails for one temp name it will most likely fail
3725 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003726 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003727 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003728 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003729 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003730 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 /*
3733 * Delete the "to" file, this is required on some systems to make the
3734 * mch_rename() work, on other systems it makes sure that we don't have
3735 * two files when the mch_rename() fails.
3736 */
3737
3738#ifdef AMIGA
3739 /*
3740 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3741 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003742 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 * deleting the "from" file (horror!) we lock it during the remove.
3744 *
3745 * When used for making a backup before writing the file: This should not
3746 * happen with ":w", because startscript() should detect this problem and
3747 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3748 * name. This problem does exist with ":w filename", but then the
3749 * original file will be somewhere else so the backup isn't really
3750 * important. If autoscripting is off the rename may fail.
3751 */
3752 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3753#endif
3754 mch_remove(to);
3755#ifdef AMIGA
3756 if (flock)
3757 UnLock(flock);
3758#endif
3759
3760 /*
3761 * First try a normal rename, return if it works.
3762 */
3763 if (mch_rename((char *)from, (char *)to) == 0)
3764 return 0;
3765
3766 /*
3767 * Rename() failed, try copying the file.
3768 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003769 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003770#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003771 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003772 acl = mch_get_acl(from);
3773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3775 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003776 {
3777#ifdef HAVE_ACL
3778 mch_free_acl(acl);
3779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003781 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003783 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003784 fd_out = mch_open((char *)to,
3785 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (fd_out == -1)
3787 {
3788 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003789#ifdef HAVE_ACL
3790 mch_free_acl(acl);
3791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return -1;
3793 }
3794
Bram Moolenaar473952e2019-09-28 16:30:04 +02003795 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (buffer == NULL)
3797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003799 close(fd_in);
3800#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 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003807 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 {
3809 errmsg = _("E208: Error writing to \"%s\"");
3810 break;
3811 }
3812
3813 vim_free(buffer);
3814 close(fd_in);
3815 if (close(fd_out) < 0)
3816 errmsg = _("E209: Error closing \"%s\"");
3817 if (n < 0)
3818 {
3819 errmsg = _("E210: Error reading \"%s\"");
3820 to = from;
3821 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003822#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003823 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003824#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003825#ifdef HAVE_ACL
3826 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003827 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003828#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003829#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003830 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (errmsg != NULL)
3833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003834 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 return -1;
3836 }
3837 mch_remove(from);
3838 return 0;
3839}
3840
3841static int already_warned = FALSE;
3842
3843/*
3844 * Check if any not hidden buffer has been changed.
3845 * Postpone the check if there are characters in the stuff buffer, a global
3846 * command is being executed, a mapping is being executed or an autocommand is
3847 * busy.
3848 * Returns TRUE if some message was written (screen should be redrawn and
3849 * cursor positioned).
3850 */
3851 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003852check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003853 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 buf_T *buf;
3856 int didit = 0;
3857 int n;
3858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003859 // Don't check timestamps while system() or another low-level function may
3860 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (no_check_timestamps > 0)
3862 return FALSE;
3863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003864 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3865 // event and we would keep on checking if the file is steadily growing.
3866 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (focus && did_check_timestamps)
3868 {
3869 need_check_timestamps = TRUE;
3870 return FALSE;
3871 }
3872
3873 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003874 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003875 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 else
3877 {
3878 ++no_wait_return;
3879 did_check_timestamps = TRUE;
3880 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003881 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (buf->b_nwindows > 0)
3885 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003886 bufref_T bufref;
3887
3888 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 n = buf_check_timestamp(buf, focus);
3890 if (didit < n)
3891 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003892 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894 // Autocommands have removed the buffer, start at the
3895 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 buf = firstbuf;
3897 continue;
3898 }
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901 --no_wait_return;
3902 need_check_timestamps = FALSE;
3903 if (need_wait_return && didit == 2)
3904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003906 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 out_flush();
3908 }
3909 }
3910 return didit;
3911}
3912
3913/*
3914 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3915 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3916 * empty.
3917 */
3918 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003919move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921 buf_T *tbuf = curbuf;
3922 int retval = OK;
3923 linenr_T lnum;
3924 char_u *p;
3925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003926 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 curbuf = tobuf;
3928 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3929 {
3930 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3931 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3932 {
3933 vim_free(p);
3934 retval = FAIL;
3935 break;
3936 }
3937 vim_free(p);
3938 }
3939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003940 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if (retval != FAIL)
3942 {
3943 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003944 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003945 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003947 // Oops! We could try putting back the saved lines, but that
3948 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 retval = FAIL;
3950 break;
3951 }
3952 }
3953
3954 curbuf = tbuf;
3955 return retval;
3956}
3957
3958/*
3959 * Check if buffer "buf" has been changed.
3960 * Also check if the file for a new buffer unexpectedly appeared.
3961 * return 1 if a changed buffer was found.
3962 * return 2 if a message has been displayed.
3963 * return 0 otherwise.
3964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003966buf_check_timestamp(
3967 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003968 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003970 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int stat_res;
3972 int retval = 0;
3973 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003974 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003976 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int helpmesg = FALSE;
3978 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003979 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3981 int can_reload = FALSE;
3982#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003983 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 int orig_mode = buf->b_orig_mode;
3985#ifdef FEAT_GUI
3986 int save_mouse_correct = need_mouse_correct;
3987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003989 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003990#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003991 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003992#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003993 bufref_T bufref;
3994
3995 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003997 // If there is no file name, the buffer is not loaded, 'buftype' is
3998 // set, we are in the middle of a save or being called recursively: ignore
3999 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (buf->b_ffname == NULL
4001 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004002 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004005#ifdef FEAT_NETBEANS_INTG
4006 || isNetbeansBuffer(buf)
4007#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004008#ifdef FEAT_TERMINAL
4009 || buf->b_term != NULL
4010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 )
4012 return 0;
4013
4014 if ( !(buf->b_flags & BF_NOTEDITED)
4015 && buf->b_mtime != 0
4016 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4017 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004018 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#ifdef HAVE_ST_MODE
4020 || (int)st.st_mode != buf->b_orig_mode
4021#else
4022 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4023#endif
4024 ))
4025 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004026 long prev_b_mtime = buf->b_mtime;
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 retval = 1;
4029
Bram Moolenaar386bc822018-07-07 18:34:12 +02004030 // set b_mtime to stop further warnings (e.g., when executing
4031 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 if (stat_res < 0)
4033 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004034 // Check the file again later to see if it re-appears.
4035 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 buf->b_orig_size = 0;
4037 buf->b_orig_mode = 0;
4038 }
4039 else
4040 buf_store_time(buf, &st, buf->b_ffname);
4041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004042 // Don't do anything for a directory. Might contain the file
4043 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (mch_isdir(buf->b_fname))
4045 ;
4046
4047 /*
4048 * If 'autoread' is set, the buffer has no changes and the file still
4049 * exists, reload the buffer. Use the buffer-local option value if it
4050 * was set, the global option value otherwise.
4051 */
4052 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4053 && !bufIsChanged(buf) && stat_res >= 0)
4054 reload = TRUE;
4055 else
4056 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004057 if (stat_res < 0)
4058 reason = "deleted";
4059 else if (bufIsChanged(buf))
4060 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004061 /*
4062 * Check if the file contents really changed to avoid giving a
4063 * warning when only the timestamp was set (e.g., checked out of
4064 * CVS). Always warn when the buffer was changed.
4065 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004066 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4067 reason = "changed";
4068 else if (orig_mode != buf->b_orig_mode)
4069 reason = "mode";
4070 else
4071 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 /*
4074 * Only give the warning if there are no FileChangedShell
4075 * autocommands.
4076 * Avoid being called recursively by setting "busy".
4077 */
4078 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004079#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004080 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4081 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004082#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004083 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4085 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004086 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 busy = FALSE;
4088 if (n)
4089 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004090 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004091 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004092#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004093 s = get_vim_var_str(VV_FCS_CHOICE);
4094 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4095 reload = TRUE;
4096 else if (STRCMP(s, "ask") == 0)
4097 n = FALSE;
4098 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004099#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004100 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004102 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004104 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004105 {
4106 // Only give the message once.
4107 if (prev_b_mtime != -1)
4108 mesg = _("E211: File \"%s\" no longer available");
4109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 else
4111 {
4112 helpmesg = TRUE;
4113#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4114 can_reload = TRUE;
4115#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004116 if (reason[2] == 'n')
4117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004119 mesg2 = _("See \":help W12\" for more info.");
4120 }
4121 else if (reason[1] == 'h')
4122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004124 mesg2 = _("See \":help W11\" for more info.");
4125 }
4126 else if (*reason == 'm')
4127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004129 mesg2 = _("See \":help W16\" for more info.");
4130 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004131 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004132 // Only timestamp changed, store it to avoid a warning
4133 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004134 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
4137 }
4138
4139 }
4140 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4141 && vim_fexists(buf->b_ffname))
4142 {
4143 retval = 1;
4144 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4145 buf->b_flags |= BF_NEW_W;
4146#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4147 can_reload = TRUE;
4148#endif
4149 }
4150
4151 if (mesg != NULL)
4152 {
4153 path = home_replace_save(buf, buf->b_fname);
4154 if (path != NULL)
4155 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004158 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004159 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004160#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004161 // Set warningmsg here, before the unimportant and output-specific
4162 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004163 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4166 if (can_reload)
4167 {
4168 if (*mesg2 != NUL)
4169 {
4170 STRCAT(tbuf, "\n");
4171 STRCAT(tbuf, mesg2);
4172 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004173 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4174 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004175 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 reload = TRUE;
4177 }
4178 else
4179#endif
4180 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4181 {
4182 if (*mesg2 != NUL)
4183 {
4184 STRCAT(tbuf, "; ");
4185 STRCAT(tbuf, mesg2);
4186 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004187 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 retval = 2;
4189 }
4190 else
4191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
4194 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004195 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004197 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 msg_clr_eos();
4199 (void)msg_end();
4200 if (emsg_silent == 0)
4201 {
4202 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004203#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004205#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004207 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004209 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 redraw_cmdline = FALSE;
4211 }
4212 }
4213 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215
4216 vim_free(path);
4217 vim_free(tbuf);
4218 }
4219 }
4220
4221 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004223 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004224 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004225#ifdef FEAT_PERSISTENT_UNDO
4226 if (buf->b_p_udf && buf->b_ffname != NULL)
4227 {
4228 char_u hash[UNDO_HASH_SIZE];
4229 buf_T *save_curbuf = curbuf;
4230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004231 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004232 curbuf = buf;
4233 u_compute_hash(hash);
4234 u_write_undo(NULL, FALSE, buf, hash);
4235 curbuf = save_curbuf;
4236 }
4237#endif
4238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004240 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004241 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004242 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4243 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004245 // restore this in case an autocommand has set it; it would break
4246 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 need_mouse_correct = save_mouse_correct;
4248#endif
4249
4250 return retval;
4251}
4252
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004253/*
4254 * Reload a buffer that is already loaded.
4255 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004256 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4257 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004258 */
4259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004260buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004261{
4262 exarg_T ea;
4263 pos_T old_cursor;
4264 linenr_T old_topline;
4265 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004266 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004267 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004268 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004269 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004270 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004272 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004273 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004275 // We only want to read the text from the file, not reset the syntax
4276 // highlighting, clear marks, diff status, etc. Force the fileformat
4277 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004278 if (prep_exarg(&ea, buf) == OK)
4279 {
4280 old_cursor = curwin->w_cursor;
4281 old_topline = curwin->w_topline;
4282
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004283 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004285 // Save all the text, so that the reload can be undone.
4286 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004287 u_sync(FALSE);
4288 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4289 flags |= READ_KEEP_UNDO;
4290 }
4291
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004292 /*
4293 * To behave like when a new file is edited (matters for
4294 * BufReadPost autocommands) we first need to delete the current
4295 * buffer contents. But if reading the file fails we should keep
4296 * the old contents. Can't use memory only, the file might be
4297 * too big. Use a hidden buffer to move the buffer contents to.
4298 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004299 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004300 savebuf = NULL;
4301 else
4302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004304 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004305 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004306 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004308 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004309 curbuf = savebuf;
4310 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004311 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004312 curbuf = buf;
4313 curwin->w_buffer = buf;
4314 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004315 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004316 || move_lines(buf, savebuf) == FAIL)
4317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004318 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004319 buf->b_fname);
4320 saved = FAIL;
4321 }
4322 }
4323
4324 if (saved == OK)
4325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004326 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4327 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004328 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4329 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004330 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004331 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004332#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004333 if (!aborting())
4334#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004335 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004336 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 // Put the text back from the save buffer. First
4339 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004340 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004341 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004342 break;
4343 (void)move_lines(savebuf, buf);
4344 }
4345 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004348 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004349 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004350 if ((flags & READ_KEEP_UNDO) == 0)
4351 {
4352 u_blockfree(buf);
4353 u_clearall(buf);
4354 }
4355 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004358 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004359 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004360 }
4361 }
4362 vim_free(ea.cmd);
4363
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004364 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365 wipe_buffer(savebuf, FALSE);
4366
4367#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004368 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004369 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004370#endif
4371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 // Restore the topline and cursor position and check it (lines may
4373 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004374 if (old_topline > curbuf->b_ml.ml_line_count)
4375 curwin->w_topline = curbuf->b_ml.ml_line_count;
4376 else
4377 curwin->w_topline = old_topline;
4378 curwin->w_cursor = old_cursor;
4379 check_cursor();
4380 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004381 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004382#ifdef FEAT_FOLDING
4383 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004384 win_T *wp;
4385 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004388 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004389 if (wp->w_buffer == curwin->w_buffer
4390 && !foldmethodIsManual(wp))
4391 foldUpdateAll(wp);
4392 }
4393#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004394 // If the mode didn't change and 'readonly' was set, keep the old
4395 // value; the user probably used the ":view" command. But don't
4396 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397 if (orig_mode == curbuf->b_orig_mode)
4398 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004401 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004402 }
4403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004407}
4408
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004410buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004413 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#ifdef HAVE_ST_MODE
4415 buf->b_orig_mode = (int)st->st_mode;
4416#else
4417 buf->b_orig_mode = mch_getperm(fname);
4418#endif
4419}
4420
4421/*
4422 * Adjust the line with missing eol, used for the next write.
4423 * Used for do_filter(), when the input lines for the filter are deleted.
4424 */
4425 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004426write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004428 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004429 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430}
4431
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004432// Subfuncions for readdirex()
4433#ifdef FEAT_EVAL
4434# ifdef MSWIN
4435 static char_u *
4436getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4437{
4438 stat_T st;
4439 unsigned short st_mode;
4440 DWORD flag = wfd->dwFileAttributes;
4441 WCHAR *wp;
4442
4443 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4444 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4445 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4446 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4447
4448 wp = wcsrchr(wfd->cFileName, L'.');
4449 if (wp != NULL)
4450 {
4451 if (_wcsicmp(wp, L".exe") == 0 ||
4452 _wcsicmp(wp, L".com") == 0 ||
4453 _wcsicmp(wp, L".cmd") == 0 ||
4454 _wcsicmp(wp, L".bat") == 0)
4455 st_mode |= _S_IEXEC;
4456 }
4457
4458 // Copy user bits to group/other.
4459 st_mode |= (st_mode & 0700) >> 3;
4460 st_mode |= (st_mode & 0700) >> 6;
4461
4462 st.st_mode = st_mode;
4463 return getfpermst(&st, perm);
4464}
4465
4466 static char_u *
4467getftypewfd(WIN32_FIND_DATAW *wfd)
4468{
4469 DWORD flag = wfd->dwFileAttributes;
4470 DWORD tag = wfd->dwReserved0;
4471
4472 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4473 {
4474 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4475 return (char_u*)"junction";
4476 else if (tag == IO_REPARSE_TAG_SYMLINK)
4477 {
4478 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4479 return (char_u*)"linkd";
4480 else
4481 return (char_u*)"link";
4482 }
4483 return (char_u*)"reparse"; // unknown reparse point type
4484 }
4485 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4486 return (char_u*)"dir";
4487 else
4488 return (char_u*)"file";
4489}
4490
4491 static dict_T *
4492create_readdirex_item(WIN32_FIND_DATAW *wfd)
4493{
4494 dict_T *item;
4495 char_u *p;
4496 varnumber_T size, time;
4497 char_u permbuf[] = "---------";
4498
4499 item = dict_alloc();
4500 if (item == NULL)
4501 return NULL;
4502 item->dv_refcount++;
4503
4504 p = utf16_to_enc(wfd->cFileName, NULL);
4505 if (p == NULL)
4506 goto theend;
4507 if (dict_add_string(item, "name", p) == FAIL)
4508 {
4509 vim_free(p);
4510 goto theend;
4511 }
4512 vim_free(p);
4513
4514 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4515 if (dict_add_number(item, "size", size) == FAIL)
4516 goto theend;
4517
4518 // Convert FILETIME to unix time.
4519 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4520 wfd->ftLastWriteTime.dwLowDateTime)
4521 - 116444736000000000) / 10000000;
4522 if (dict_add_number(item, "time", time) == FAIL)
4523 goto theend;
4524
4525 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4526 goto theend;
4527 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4528 goto theend;
4529
4530 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4531 goto theend;
4532 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4533 goto theend;
4534
4535 return item;
4536
4537theend:
4538 dict_unref(item);
4539 return NULL;
4540}
4541# else
4542 static dict_T *
4543create_readdirex_item(char_u *path, char_u *name)
4544{
4545 dict_T *item;
4546 char *p;
4547 size_t len;
4548 stat_T st;
4549 int ret, link = FALSE;
4550 varnumber_T size;
4551 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004552 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004553 struct passwd *pw;
4554 struct group *gr;
4555
4556 item = dict_alloc();
4557 if (item == NULL)
4558 return NULL;
4559 item->dv_refcount++;
4560
4561 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4562 p = alloc(len);
4563 if (p == NULL)
4564 goto theend;
4565 vim_snprintf(p, len, "%s/%s", path, name);
4566 ret = mch_lstat(p, &st);
4567 if (ret >= 0 && S_ISLNK(st.st_mode))
4568 {
4569 link = TRUE;
4570 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004571 if (ret < 0)
4572 q = (char_u*)"link";
4573
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004574 }
4575 vim_free(p);
4576
4577 if (dict_add_string(item, "name", name) == FAIL)
4578 goto theend;
4579
4580 if (ret >= 0)
4581 {
4582 size = (varnumber_T)st.st_size;
4583 if (S_ISDIR(st.st_mode))
4584 size = 0;
4585 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004586 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004587 size = -2;
4588 if (dict_add_number(item, "size", size) == FAIL)
4589 goto theend;
4590 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4591 goto theend;
4592
4593 if (link)
4594 {
4595 if (S_ISDIR(st.st_mode))
4596 q = (char_u*)"linkd";
4597 else
4598 q = (char_u*)"link";
4599 }
4600 else
4601 q = getftypest(&st);
4602 if (dict_add_string(item, "type", q) == FAIL)
4603 goto theend;
4604 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4605 goto theend;
4606
4607 pw = getpwuid(st.st_uid);
4608 if (pw == NULL)
4609 q = (char_u*)"";
4610 else
4611 q = (char_u*)pw->pw_name;
4612 if (dict_add_string(item, "user", q) == FAIL)
4613 goto theend;
4614 gr = getgrgid(st.st_gid);
4615 if (gr == NULL)
4616 q = (char_u*)"";
4617 else
4618 q = (char_u*)gr->gr_name;
4619 if (dict_add_string(item, "group", q) == FAIL)
4620 goto theend;
4621 }
4622 else
4623 {
4624 if (dict_add_number(item, "size", -1) == FAIL)
4625 goto theend;
4626 if (dict_add_number(item, "time", -1) == FAIL)
4627 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004628 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004629 goto theend;
4630 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4631 goto theend;
4632 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4633 goto theend;
4634 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4635 goto theend;
4636 }
4637 return item;
4638
4639theend:
4640 dict_unref(item);
4641 return NULL;
4642}
4643# endif
4644
4645 static int
4646compare_readdirex_item(const void *p1, const void *p2)
4647{
4648 char_u *name1, *name2;
4649
4650 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4651 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004652 if (readdirex_sort == READDIR_SORT_BYTE)
4653 return STRCMP(name1, name2);
4654 else if (readdirex_sort == READDIR_SORT_IC)
4655 return STRICMP(name1, name2);
4656 else
4657 return STRCOLL(name1, name2);
4658}
4659
4660 static int
4661compare_readdir_item(const void *s1, const void *s2)
4662{
4663 if (readdirex_sort == READDIR_SORT_BYTE)
4664 return STRCMP(*(char **)s1, *(char **)s2);
4665 else if (readdirex_sort == READDIR_SORT_IC)
4666 return STRICMP(*(char **)s1, *(char **)s2);
4667 else
4668 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004669}
4670#endif
4671
Bram Moolenaarda440d22016-01-16 21:27:23 +01004672#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4673/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004674 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004675 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004676 * If "withattr" is TRUE, retrieve the names and their attributes.
4677 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004678 * Return OK for success, FAIL for failure.
4679 */
4680 int
4681readdir_core(
4682 garray_T *gap,
4683 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004684 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004685 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004686 int (*checkitem)(void *context, void *item),
4687 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004688{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004689 int failed = FALSE;
4690 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004691# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004692 char_u *buf;
4693 int ok;
4694 HANDLE hFind = INVALID_HANDLE_VALUE;
4695 WIN32_FIND_DATAW wfd;
4696 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004697# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004698 DIR *dirp;
4699 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004700# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004701
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004702 ga_init2(gap, (int)sizeof(void *), 20);
4703
4704# ifdef FEAT_EVAL
4705# define FREE_ITEM(item) do { \
4706 if (withattr) \
4707 dict_unref((dict_T*)item); \
4708 else \
4709 vim_free(item); \
4710 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004711
4712 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004713# else
4714# define FREE_ITEM(item) vim_free(item)
4715# endif
4716
4717# ifdef MSWIN
4718 buf = alloc(MAXPATHL);
4719 if (buf == NULL)
4720 return FAIL;
4721 STRNCPY(buf, path, MAXPATHL-5);
4722 p = buf + STRLEN(buf);
4723 MB_PTR_BACK(buf, p);
4724 if (*p == '\\' || *p == '/')
4725 *p = NUL;
4726 STRCAT(p, "\\*");
4727
4728 wn = enc_to_utf16(buf, NULL);
4729 if (wn != NULL)
4730 hFind = FindFirstFileW(wn, &wfd);
4731 ok = (hFind != INVALID_HANDLE_VALUE);
4732 if (!ok)
4733 {
4734 failed = TRUE;
4735 smsg(_(e_notopen), path);
4736 }
4737 else
4738 {
4739 while (ok)
4740 {
4741 int ignore;
4742 void *item;
4743 WCHAR *wp;
4744
4745 wp = wfd.cFileName;
4746 ignore = wp[0] == L'.' &&
4747 (wp[1] == NUL ||
4748 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004749 if (ignore)
4750 {
4751 ok = FindNextFileW(hFind, &wfd);
4752 continue;
4753 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004754# ifdef FEAT_EVAL
4755 if (withattr)
4756 item = (void*)create_readdirex_item(&wfd);
4757 else
4758# endif
4759 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4760 if (item == NULL)
4761 {
4762 failed = TRUE;
4763 break;
4764 }
4765
4766 if (!ignore && checkitem != NULL)
4767 {
4768 int r = checkitem(context, item);
4769
4770 if (r < 0)
4771 {
4772 FREE_ITEM(item);
4773 break;
4774 }
4775 if (r == 0)
4776 ignore = TRUE;
4777 }
4778
4779 if (!ignore)
4780 {
4781 if (ga_grow(gap, 1) == OK)
4782 ((void**)gap->ga_data)[gap->ga_len++] = item;
4783 else
4784 {
4785 failed = TRUE;
4786 FREE_ITEM(item);
4787 break;
4788 }
4789 }
4790 else
4791 FREE_ITEM(item);
4792
4793 ok = FindNextFileW(hFind, &wfd);
4794 }
4795 FindClose(hFind);
4796 }
4797
4798 vim_free(buf);
4799 vim_free(wn);
4800# else // MSWIN
4801 dirp = opendir((char *)path);
4802 if (dirp == NULL)
4803 {
4804 failed = TRUE;
4805 smsg(_(e_notopen), path);
4806 }
4807 else
4808 {
4809 for (;;)
4810 {
4811 int ignore;
4812 void *item;
4813
4814 dp = readdir(dirp);
4815 if (dp == NULL)
4816 break;
4817 p = (char_u *)dp->d_name;
4818
4819 ignore = p[0] == '.' &&
4820 (p[1] == NUL ||
4821 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004822 if (ignore)
4823 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004824# ifdef FEAT_EVAL
4825 if (withattr)
4826 item = (void*)create_readdirex_item(path, p);
4827 else
4828# endif
4829 item = (void*)vim_strsave(p);
4830 if (item == NULL)
4831 {
4832 failed = TRUE;
4833 break;
4834 }
4835
4836 if (!ignore && checkitem != NULL)
4837 {
4838 int r = checkitem(context, item);
4839
4840 if (r < 0)
4841 {
4842 FREE_ITEM(item);
4843 break;
4844 }
4845 if (r == 0)
4846 ignore = TRUE;
4847 }
4848
4849 if (!ignore)
4850 {
4851 if (ga_grow(gap, 1) == OK)
4852 ((void**)gap->ga_data)[gap->ga_len++] = item;
4853 else
4854 {
4855 failed = TRUE;
4856 FREE_ITEM(item);
4857 break;
4858 }
4859 }
4860 else
4861 FREE_ITEM(item);
4862 }
4863
4864 closedir(dirp);
4865 }
4866# endif // MSWIN
4867
4868# undef FREE_ITEM
4869
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004870 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004871 {
4872# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004873 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004874 if (withattr)
4875 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4876 compare_readdirex_item);
4877 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004878 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4879 compare_readdir_item);
4880# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004881 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004882# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004883 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004884
4885 return failed ? FAIL : OK;
4886}
4887
4888/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004889 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004890 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004891 */
4892 int
4893delete_recursive(char_u *name)
4894{
4895 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004896 int i;
4897 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004898 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004899
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004900 // A symbolic link to a directory itself is deleted, not the directory it
4901 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004902 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004903# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004904 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004905# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004906 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004907# endif
4908 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004909 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004910 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004911 if (exp == NULL)
4912 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004913 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004914 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004915 for (i = 0; i < ga.ga_len; ++i)
4916 {
4917 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4918 ((char_u **)ga.ga_data)[i]);
4919 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004920 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004921 }
4922 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004923 }
4924 else
4925 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004926 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004927 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004928 }
4929 else
4930 result = mch_remove(name) == 0 ? 0 : -1;
4931
4932 return result;
4933}
4934#endif
4935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004937static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004939# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4940/*
4941 * Open temporary directory and take file lock to prevent
4942 * to be auto-cleaned.
4943 */
4944 static void
4945vim_opentempdir(void)
4946{
4947 DIR *dp = NULL;
4948
4949 if (vim_tempdir_dp != NULL)
4950 return;
4951
4952 dp = opendir((const char*)vim_tempdir);
4953
4954 if (dp != NULL)
4955 {
4956 vim_tempdir_dp = dp;
4957 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4958 }
4959}
4960
4961/*
4962 * Close temporary directory - it automatically release file lock.
4963 */
4964 static void
4965vim_closetempdir(void)
4966{
4967 if (vim_tempdir_dp != NULL)
4968 {
4969 closedir(vim_tempdir_dp);
4970 vim_tempdir_dp = NULL;
4971 }
4972}
4973# endif
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975/*
4976 * Delete the temp directory and all files it contains.
4977 */
4978 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004979vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (vim_tempdir != NULL)
4982 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004983# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4984 vim_closetempdir();
4985# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004986 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004987 gettail(vim_tempdir)[-1] = NUL;
4988 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004989 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004994 * Directory "tempdir" was created. Expand this name to a full path and put
4995 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
4996 * "tempdir" must be no longer than MAXPATHL.
4997 */
4998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005000{
5001 char_u *buf;
5002
Bram Moolenaar964b3742019-05-24 18:54:09 +02005003 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005004 if (buf != NULL)
5005 {
5006 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5007 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005008 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005009 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005010# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5011 vim_opentempdir();
5012# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005013 vim_free(buf);
5014 }
5015}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005016#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005017
5018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 * vim_tempname(): Return a unique name that can be used for a temp file.
5020 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005021 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5022 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 *
5024 * The returned pointer is to allocated memory.
5025 * The returned pointer is NULL if no valid name was found.
5026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005029 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005030 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
5032#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005034#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005035 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#else
5037 char_u itmp[TEMPNAMELEN];
5038#endif
5039
5040#ifdef TEMPDIRNAMES
5041 static char *(tempdirs[]) = {TEMPDIRNAMES};
5042 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005044 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045# endif
5046
5047 /*
5048 * This will create a directory for private use by this instance of Vim.
5049 * This is done once, and the same directory is used for all temp files.
5050 * This method avoids security problems because of symlink attacks et al.
5051 * It's also a bit faster, because we only need to check for an existing
5052 * file when creating the directory and not for each temp file.
5053 */
5054 if (vim_tempdir == NULL)
5055 {
5056 /*
5057 * Try the entries in TEMPDIRNAMES to create the temp directory.
5058 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005059 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005061# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005062 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005063 long nr;
5064 long off;
5065# endif
5066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005067 // Expand $TMP, leave room for "/v1100000/999999999".
5068 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005070 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005072 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005073 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaareaf03392009-11-17 11:08:52 +00005075# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005076 {
5077# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 // Make sure the umask doesn't remove the executable bit.
5079 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005080 mode_t umask_save = umask(077);
5081# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005083 STRCAT(itmp, "vXXXXXX");
5084 if (mkdtemp((char *)itmp) != NULL)
5085 vim_settempdir(itmp);
5086# if defined(UNIX) || defined(VMS)
5087 (void)umask(umask_save);
5088# endif
5089 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005090# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 // Get an arbitrary number of up to 6 digits. When it's
5092 // unlikely that it already exists it will be faster,
5093 // otherwise it doesn't matter. The use of mkdir() avoids any
5094 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005096 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005098 // Try up to 10000 different values until we find a name that
5099 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 for (off = 0; off < 10000L; ++off)
5101 {
5102 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005103# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
Bram Moolenaareaf03392009-11-17 11:08:52 +00005107 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5108# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 // If mkdir() does not set errno to EEXIST, check for
5110 // existing file here. There is a race condition then,
5111 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (mch_stat((char *)itmp, &st) >= 0)
5113 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005114# endif
5115# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005116 // Make sure the umask doesn't remove the executable bit.
5117 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005121# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (r == 0)
5125 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005126 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
5128 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005129# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005130 // If the mkdir() didn't fail because the file/dir exists,
5131 // we probably can't create any dir here, try another
5132 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005137# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (vim_tempdir != NULL)
5139 break;
5140 }
5141 }
5142 }
5143
5144 if (vim_tempdir != NULL)
5145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005146 // There is no need to check if the file exists, because we own the
5147 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5149 return vim_strsave(itmp);
5150 }
5151
5152 return NULL;
5153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005154#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
Bram Moolenaar4f974752019-02-17 17:44:42 +01005156# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005157 WCHAR wszTempFile[_MAX_PATH + 1];
5158 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 char_u *retval;
5160 char_u *p;
5161
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005162 wcscpy(itmp, L"");
5163 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005164 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005165 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
5166 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005167 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005168 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005170 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005172 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005173 // GetTempFileName() will create the file, we don't want that
5174 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005176 // Backslashes in a temp file name cause problems when filtering with
5177 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5178 // didn't set 'shellslash'.
5179 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (*p_shcf == '-' || p_ssl)
5181 for (p = retval; *p; ++p)
5182 if (*p == '\\')
5183 *p = '/';
5184 return retval;
5185
Bram Moolenaar4f974752019-02-17 17:44:42 +01005186# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005189 char_u *p;
5190
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005191 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005192 p = tmpnam((char *)itmp);
5193 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return NULL;
5195# else
5196 char_u *p;
5197
5198# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005199 // mktemp() is not working on VMS. It seems to be
5200 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 sprintf((char *)itmp, "VIM%c", extra_char);
5202 p = (char_u *)tempnam("tmp:", (char *)itmp);
5203 if (p != NULL)
5204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // VMS will use '.LIS' if we don't explicitly specify an extension,
5206 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 STRCPY(itmp, p);
5208 STRCAT(itmp, ".txt");
5209 free(p);
5210 }
5211 else
5212 return NULL;
5213# else
5214 STRCPY(itmp, TEMPNAME);
5215 if ((p = vim_strchr(itmp, '?')) != NULL)
5216 *p = extra_char;
5217 if (mktemp((char *)itmp) == NULL)
5218 return NULL;
5219# endif
5220# endif
5221
5222 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005223# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005224#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225}
5226
5227#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5228/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005229 * Convert all backslashes in fname to forward slashes in-place, unless when
5230 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 */
5232 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005233forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234{
5235 char_u *p;
5236
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005237 if (path_with_url(fname))
5238 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005241 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005243 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 *p = '/';
5245}
5246#endif
5247
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005248/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005249 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5250 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5251 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 * Used for autocommands and 'wildignore'.
5253 * Returns TRUE if there is a match, FALSE otherwise.
5254 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005255 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005256match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 char_u *pattern, // pattern to match with
5258 regprog_T **prog, // pre-compiled regprog or NULL
5259 char_u *fname, // full path of file name
5260 char_u *sfname, // short file name or NULL
5261 char_u *tail, // tail of path
5262 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 regmatch_T regmatch;
5265 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005267 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005268 if (prog != NULL)
5269 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005271 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 /*
5274 * Try for a match with the pattern with:
5275 * 1. the full file name, when the pattern has a '/'.
5276 * 2. the short file name, when the pattern has a '/'.
5277 * 3. the tail of the file name, when the pattern has no '/'.
5278 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005279 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 && ((allow_dirs
5281 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5282 || (sfname != NULL
5283 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005284 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 result = TRUE;
5286
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005287 if (prog != NULL)
5288 *prog = regmatch.regprog;
5289 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005290 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return result;
5292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294#if defined(FEAT_WILDIGN) || defined(PROTO)
5295/*
5296 * Return TRUE if a file matches with a pattern in "list".
5297 * "list" is a comma-separated list of patterns, like 'wildignore'.
5298 * "sfname" is the short file name or NULL, "ffname" the long file name.
5299 */
5300 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005301match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 char_u buf[100];
5304 char_u *tail;
5305 char_u *regpat;
5306 char allow_dirs;
5307 int match;
5308 char_u *p;
5309
5310 tail = gettail(sfname);
5311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 p = list;
5314 while (*p)
5315 {
5316 copy_option_part(&p, buf, 100, ",");
5317 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5318 if (regpat == NULL)
5319 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005320 match = match_file_pat(regpat, NULL, ffname, sfname,
5321 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 vim_free(regpat);
5323 if (match)
5324 return TRUE;
5325 }
5326 return FALSE;
5327}
5328#endif
5329
5330/*
5331 * Convert the given pattern "pat" which has shell style wildcards in it, into
5332 * a regular expression, and return the result in allocated memory. If there
5333 * is a directory path separator to be matched, then TRUE is put in
5334 * allow_dirs, otherwise FALSE is put there -- webb.
5335 * Handle backslashes before special characters, like "\*" and "\ ".
5336 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 * Returns NULL when out of memory.
5338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340file_pat_to_reg_pat(
5341 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005342 char_u *pat_end, // first char after pattern or NULL
5343 char *allow_dirs, // Result passed back out in here
5344 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005346 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 char_u *endp;
5348 char_u *reg_pat;
5349 char_u *p;
5350 int i;
5351 int nested = 0;
5352 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
5354 if (allow_dirs != NULL)
5355 *allow_dirs = FALSE;
5356 if (pat_end == NULL)
5357 pat_end = pat + STRLEN(pat);
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 for (p = pat; p < pat_end; p++)
5360 {
5361 switch (*p)
5362 {
5363 case '*':
5364 case '.':
5365 case ',':
5366 case '{':
5367 case '}':
5368 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 break;
5371#ifdef BACKSLASH_IN_FILENAME
5372 case '\\':
5373 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 break;
5376#endif
5377 default:
5378 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005379 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 ++p;
5382 ++size;
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 break;
5385 }
5386 }
5387 reg_pat = alloc(size + 1);
5388 if (reg_pat == NULL)
5389 return NULL;
5390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393 if (pat[0] == '*')
5394 while (pat[0] == '*' && pat < pat_end - 1)
5395 pat++;
5396 else
5397 reg_pat[i++] = '^';
5398 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005399 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 while (endp - pat > 0 && *endp == '*')
5402 endp--;
5403 add_dollar = FALSE;
5404 }
5405 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5406 {
5407 switch (*p)
5408 {
5409 case '*':
5410 reg_pat[i++] = '.';
5411 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005412 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005413 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 break;
5415 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 case '~':
5417 reg_pat[i++] = '\\';
5418 reg_pat[i++] = *p;
5419 break;
5420 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 reg_pat[i++] = '.';
5422 break;
5423 case '\\':
5424 if (p[1] == NUL)
5425 break;
5426#ifdef BACKSLASH_IN_FILENAME
5427 if (!no_bslash)
5428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005429 // translate:
5430 // "\x" to "\\x" e.g., "dir\file"
5431 // "\*" to "\\.*" e.g., "dir\*.c"
5432 // "\?" to "\\." e.g., "dir\??.c"
5433 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5435 && p[1] != '+')
5436 {
5437 reg_pat[i++] = '[';
5438 reg_pat[i++] = '\\';
5439 reg_pat[i++] = '/';
5440 reg_pat[i++] = ']';
5441 if (allow_dirs != NULL)
5442 *allow_dirs = TRUE;
5443 break;
5444 }
5445 }
5446#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005447 // Undo escaping from ExpandEscape():
5448 // foo\?bar -> foo?bar
5449 // foo\%bar -> foo%bar
5450 // foo\,bar -> foo,bar
5451 // foo\ bar -> foo bar
5452 // Don't unescape \, * and others that are also special in a
5453 // regexp.
5454 // An escaped { must be unescaped since we use magic not
5455 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (*++p == '?'
5457#ifdef BACKSLASH_IN_FILENAME
5458 && no_bslash
5459#endif
5460 )
5461 reg_pat[i++] = '?';
5462 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005463 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005464 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005465 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005466 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5467 {
5468 reg_pat[i++] = '\\';
5469 reg_pat[i++] = '{';
5470 p += 2;
5471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 else
5473 {
5474 if (allow_dirs != NULL && vim_ispathsep(*p)
5475#ifdef BACKSLASH_IN_FILENAME
5476 && (!no_bslash || *p != '\\')
5477#endif
5478 )
5479 *allow_dirs = TRUE;
5480 reg_pat[i++] = '\\';
5481 reg_pat[i++] = *p;
5482 }
5483 break;
5484#ifdef BACKSLASH_IN_FILENAME
5485 case '/':
5486 reg_pat[i++] = '[';
5487 reg_pat[i++] = '\\';
5488 reg_pat[i++] = '/';
5489 reg_pat[i++] = ']';
5490 if (allow_dirs != NULL)
5491 *allow_dirs = TRUE;
5492 break;
5493#endif
5494 case '{':
5495 reg_pat[i++] = '\\';
5496 reg_pat[i++] = '(';
5497 nested++;
5498 break;
5499 case '}':
5500 reg_pat[i++] = '\\';
5501 reg_pat[i++] = ')';
5502 --nested;
5503 break;
5504 case ',':
5505 if (nested)
5506 {
5507 reg_pat[i++] = '\\';
5508 reg_pat[i++] = '|';
5509 }
5510 else
5511 reg_pat[i++] = ',';
5512 break;
5513 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005514 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005516 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 *allow_dirs = TRUE;
5518 reg_pat[i++] = *p;
5519 break;
5520 }
5521 }
5522 if (add_dollar)
5523 reg_pat[i++] = '$';
5524 reg_pat[i] = NUL;
5525 if (nested != 0)
5526 {
5527 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005528 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005530 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005531 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533 return reg_pat;
5534}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005535
5536#if defined(EINTR) || defined(PROTO)
5537/*
5538 * Version of read() that retries when interrupted by EINTR (possibly
5539 * by a SIGWINCH).
5540 */
5541 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005543{
5544 long ret;
5545
5546 for (;;)
5547 {
5548 ret = vim_read(fd, buf, bufsize);
5549 if (ret >= 0 || errno != EINTR)
5550 break;
5551 }
5552 return ret;
5553}
5554
5555/*
5556 * Version of write() that retries when interrupted by EINTR (possibly
5557 * by a SIGWINCH).
5558 */
5559 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005561{
5562 long ret = 0;
5563 long wlen;
5564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005565 // Repeat the write() so long it didn't fail, other than being interrupted
5566 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005567 while (ret < (long)bufsize)
5568 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005569 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005570 if (wlen < 0)
5571 {
5572 if (errno != EINTR)
5573 break;
5574 }
5575 else
5576 ret += wlen;
5577 }
5578 return ret;
5579}
5580#endif