blob: 7b605296b74aba111e45f25fa5943ba12ece0fb4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaarf4888d02009-12-02 12:31:27 +000016#if defined(__TANDEM) || defined(__MINT__)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020019#if defined(UNIX) && defined(FEAT_EVAL)
20# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar217e1b82019-12-01 21:41:28 +010024// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000025#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf077db22019-08-13 00:18:24 +020027static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010028#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010029static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020032static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010035static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000036static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000037
Bram Moolenaar473952e2019-09-28 16:30:04 +020038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010039filemess(
40 buf_T *buf,
41 char_u *name,
42 char_u *s,
43 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020046 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48 if (msg_silent != 0)
49 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010050 msg_add_fname(buf, name); // put file name in IObuff with quotes
51 // If it's extremely long, truncate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 if (STRLEN(IObuff) > IOSIZE - 80)
53 IObuff[IOSIZE - 80] = NUL;
54 STRCAT(IObuff, s);
55 /*
56 * For the first message may have to start a new line.
57 * For further ones overwrite the previous one, reset msg_scroll before
58 * calling filemess().
59 */
60 msg_scroll_save = msg_scroll;
61 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
62 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 check_for_delay(FALSE);
65 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020066 if (prev_msg_col != 0 && msg_col == 0)
67 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 msg_scroll = msg_scroll_save;
69 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010070 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
72 msg_clr_eos();
73 out_flush();
74 msg_scrolled_ign = FALSE;
75}
76
77/*
78 * Read lines from file "fname" into the buffer after line "from".
79 *
80 * 1. We allocate blocks with lalloc, as big as possible.
81 * 2. Each block is filled with characters from the file with a single read().
82 * 3. The lines are inserted in the buffer with ml_append().
83 *
84 * (caller must check that fname != NULL, unless READ_STDIN is used)
85 *
86 * "lines_to_skip" is the number of lines that must be skipped
87 * "lines_to_read" is the number of lines that are appended
88 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
89 *
90 * flags:
91 * READ_NEW starting to edit a new buffer
92 * READ_FILTER reading filter output
93 * READ_STDIN read from stdin instead of a file
94 * READ_BUFFER read from curbuf instead of a file (converting after reading
95 * stdin)
96 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +020097 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020098 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100100 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 */
102 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100103readfile(
104 char_u *fname,
105 char_u *sfname,
106 linenr_T from,
107 linenr_T lines_to_skip,
108 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100109 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100110 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111{
112 int fd = 0;
113 int newfile = (flags & READ_NEW);
114 int check_readonly;
115 int filtering = (flags & READ_FILTER);
116 int read_stdin = (flags & READ_STDIN);
117 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200118 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000119 int set_options = newfile || read_buffer
120 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100121 linenr_T read_buf_lnum = 1; // next line to read from curbuf
122 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 char_u c;
124 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100125 char_u *ptr = NULL; // pointer into read buffer
126 char_u *buffer = NULL; // read buffer
127 char_u *new_buffer = NULL; // init to shut up gcc
128 char_u *line_start = NULL; // init to shut up gcc
129 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 colnr_T len;
131 long size = 0;
132 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200133 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 int skip_read = FALSE;
135#ifdef FEAT_CRYPT
136 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200137 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200139#ifdef FEAT_PERSISTENT_UNDO
140 context_sha256_T sha_ctx;
141 int read_undo_file = FALSE;
142#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100143 int split = 0; // number of split lines
144#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100146 int error = FALSE; // errors encountered
147 int ff_error = EOL_UNKNOWN; // file format with errors
148 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#ifdef UNIX
150 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100151 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#else
153 int perm;
154#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100155 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200157 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 int file_readonly;
159 linenr_T skip_count = 0;
160 linenr_T read_count = 0;
161 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100162 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
163 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100164 int try_mac;
165 int try_dos;
166 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100169 linenr_T conv_error = 0; // line nr with conversion error
170 linenr_T illegal_byte = 0; // line nr with illegal byte
171 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
172 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000173 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100174 // BAD_KEEP, BAD_DROP or character to
175 // replace with
176 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100178 char_u *fenc; // fileencoding to use
179 int fenc_alloced; // fenc_next is in allocated memory
180 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 int advance_fenc = FALSE;
182 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100183#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100184 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100185# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 int did_iconv = FALSE; // TRUE when iconv() failed and trying
187 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100189#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100190 int converted = FALSE; // TRUE if conversion done
191 int notconverted = FALSE; // TRUE if conversion wanted but it
192 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100195 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200196 buf_T *old_curbuf;
197 char_u *old_b_ffname;
198 char_u *old_b_fname;
199 int using_b_ffname;
200 int using_b_fname;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200201
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100202 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200203
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100204 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206 /*
207 * If there is no file name yet, use the one for the read file.
208 * BF_NOTEDITED is set to reflect this.
209 * Don't do this for a read from a filter.
210 * Only do this when 'cpoptions' contains the 'f' flag.
211 */
212 if (curbuf->b_ffname == NULL
213 && !filtering
214 && fname != NULL
215 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
216 && !(flags & READ_DUMMY))
217 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000218 if (set_rw_fname(fname, sfname) == FAIL)
219 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 }
221
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100222 // Remember the initial values of curbuf, curbuf->b_ffname and
223 // curbuf->b_fname to detect whether they are altered as a result of
224 // executing nasty autocommands. Also check if "fname" and "sfname"
225 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200226 old_curbuf = curbuf;
227 old_b_ffname = curbuf->b_ffname;
228 old_b_fname = curbuf->b_fname;
229 using_b_ffname = (fname == curbuf->b_ffname)
230 || (sfname == curbuf->b_ffname);
231 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200232
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100233 // After reading a file the cursor line changes but we don't want to
234 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000235 ex_no_reprint = TRUE;
236
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100237 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000238 need_fileinfo = FALSE;
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 /*
241 * For Unix: Use the short file name whenever possible.
242 * Avoids problems with networks and when directory names are changed.
243 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
244 * another directory, which we don't detect.
245 */
246 if (sfname == NULL)
247 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200248#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 fname = sfname;
250#endif
251
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 /*
253 * The BufReadCmd and FileReadCmd events intercept the reading process by
254 * executing the associated commands instead.
255 */
256 if (!filtering && !read_stdin && !read_buffer)
257 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100258 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100260 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
262 curbuf->b_op_start.col = 0;
263
264 if (newfile)
265 {
266 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
267 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200268 {
269 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200271 if (aborting())
272 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200274 // The BufReadCmd code usually uses ":read" to get the text and
275 // perhaps ":file" to change the buffer name. But we should
276 // consider this to work like ":edit", thus reset the
277 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
278 // same file.
279 if (status == OK)
280 curbuf->b_flags &= ~BF_NOTEDITED;
281 return status;
282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 }
284 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
285 FALSE, NULL, eap))
286#ifdef FEAT_EVAL
287 return aborting() ? FAIL : OK;
288#else
289 return OK;
290#endif
291
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100292 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294
295 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100296 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100298 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300 /*
301 * If the name ends in a path separator, we can't open it. Check here,
302 * because reading the file may actually work, but then creating the swap
303 * file may destroy it! Reported on MS-DOS and Win 95.
304 * If the name is too long we might crash further on, quit here.
305 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000306 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000308 p = fname + STRLEN(fname);
309 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000310 {
311 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
312 msg_end();
313 msg_scroll = msg_save;
314 return FAIL;
315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 }
317
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200318 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200320#ifdef UNIX
321 /*
322 * On Unix it is possible to read a directory, so we have to
323 * check for it before the mch_open().
324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100326 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
327 && !S_ISFIFO(perm) // ... or fifo
328 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000329# ifdef OPEN_CHR_FILES
330 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100331 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 )
334 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100335 int retval = FAIL;
336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100340 retval = NOTDONE;
341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 else
343 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
344 msg_end();
345 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100346 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200348#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100349#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000350 /*
351 * MS-Windows allows opening a device, but we will probably get stuck
352 * trying to read it.
353 */
354 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
355 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000356 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000357 msg_end();
358 msg_scroll = msg_save;
359 return FAIL;
360 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000361#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200362 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000363
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100364 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200365 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 /*
368 * When opening a new file we take the readonly flag from the file.
369 * Default is r/w, can be set to r/o below.
370 * Don't reset it when in readonly mode
371 * Only set/reset b_p_ro when BF_CHECK_RO is set.
372 */
373 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000374 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 curbuf->b_p_ro = FALSE;
376
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200377 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100379 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (mch_stat((char *)fname, &st) >= 0)
381 {
382 buf_store_time(curbuf, &st, fname);
383 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#ifdef UNIX
385 /*
386 * Use the protection bits of the original file for the swap file.
387 * This makes it possible for others to read the name of the
388 * edited file from the swapfile, but only if they can read the
389 * edited file.
390 * Remove the "write" and "execute" bits for group and others
391 * (they must not write the swapfile).
392 * Add the "read" and "write" bits for the user, otherwise we may
393 * not be able to write to the file ourselves.
394 * Setting the bits is done below, after creating the swap file.
395 */
396 swap_mode = (st.st_mode & 0644) | 0600;
397#endif
398#ifdef FEAT_CW_EDITOR
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 // Get the FSSpec on MacOS
400 // TODO: Update it properly when the buffer name changes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
402#endif
403#ifdef VMS
404 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000405 curbuf->b_fab_rat = st.st_fab_rat;
406 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#endif
408 }
409 else
410 {
411 curbuf->b_mtime = 0;
412 curbuf->b_mtime_read = 0;
413 curbuf->b_orig_size = 0;
414 curbuf->b_orig_mode = 0;
415 }
416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417 // Reset the "new file" flag. It will be set again below when the
418 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
420 }
421
422/*
423 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100424 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 */
426 file_readonly = FALSE;
427 if (read_stdin)
428 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100429#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100430 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 setmode(0, O_BINARY);
432#endif
433 }
434 else if (!read_buffer)
435 {
436#ifdef USE_MCH_ACCESS
437 if (
438# ifdef UNIX
439 !(perm & 0222) ||
440# endif
441 mch_access((char *)fname, W_OK))
442 file_readonly = TRUE;
443 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
444#else
445 if (!newfile
446 || readonlymode
447 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
448 {
449 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100450 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
452 }
453#endif
454 }
455
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100456 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {
458#ifndef UNIX
459 int isdir_f;
460#endif
461 msg_scroll = msg_save;
462#ifndef UNIX
463 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100464 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
466 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100467 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 if (isdir_f)
469 {
470 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100471 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 }
473 else
474#endif
475 if (newfile)
476 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200477 if (perm < 0
478#ifdef ENOENT
479 && errno == ENOENT
480#endif
481 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
483 /*
484 * Set the 'new-file' flag, so that when the file has
485 * been created by someone else, a ":w" will complain.
486 */
487 curbuf->b_flags |= BF_NEW;
488
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // Create a swap file now, so that other Vims are warned
490 // that we are editing this file. Don't do this for a
491 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#ifdef FEAT_QUICKFIX
493 if (!bt_dontwrite(curbuf))
494#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000498 if (curbuf != old_curbuf
499 || (using_b_ffname
500 && (old_b_ffname != curbuf->b_ffname))
501 || (using_b_fname
502 && (old_b_fname != curbuf->b_fname)))
503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100504 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000505 return FAIL;
506 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000507 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000508 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200509 filemess(curbuf, sfname,
510 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000511 else
512 filemess(curbuf, sfname,
513 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100515 // Even though this is a new file, it might have been
516 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 check_marks_read();
518#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100519 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200520 if (eap != NULL)
521 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
523 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100524 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 save_file_ff(curbuf);
526
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100527#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100528 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 return FAIL;
530#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 else
534 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000535 filemess(curbuf, sfname, (char_u *)(
536# ifdef EFBIG
537 (errno == EFBIG) ? _("[File too big]") :
538# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200539# ifdef EOVERFLOW
540 (errno == EOVERFLOW) ? _("[File too big]") :
541# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000542 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
545 }
546
547 return FAIL;
548 }
549
550 /*
551 * Only set the 'ro' flag for readonly files the first time they are
552 * loaded. Help files always get readonly mode
553 */
554 if ((check_readonly && file_readonly) || curbuf->b_help)
555 curbuf->b_p_ro = TRUE;
556
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000557 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100559 // Don't change 'eol' if reading from buffer as it will already be
560 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000561 if (!read_buffer)
562 {
563 curbuf->b_p_eol = TRUE;
564 curbuf->b_start_eol = TRUE;
565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000567 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
569
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100570 // Create a swap file now, so that other Vims are warned that we are
571 // editing this file.
572 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573#ifdef FEAT_QUICKFIX
574 if (!bt_dontwrite(curbuf))
575#endif
576 {
577 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000578 if (!read_stdin && (curbuf != old_curbuf
579 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
580 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100582 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000583 if (!read_buffer)
584 close(fd);
585 return FAIL;
586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100588 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000589 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
590 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100591 {
592 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
593
594 /*
595 * If the group-read bit is set but not the world-read bit, then
596 * the group must be equal to the group of the original file. If
597 * we can't make that happen then reset the group-read bit. This
598 * avoids making the swap file readable to more users when the
599 * primary group of the user is too permissive.
600 */
601 if ((swap_mode & 044) == 040)
602 {
603 stat_T swap_st;
604
605 if (mch_stat((char *)swap_fname, &swap_st) >= 0
606 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200607# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100608 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200609 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200610# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200611 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100612 swap_mode &= 0600;
613 }
614
615 (void)mch_setperm(swap_fname, (long)swap_mode);
616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#endif
618 }
619
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200620 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (swap_exists_action == SEA_QUIT)
622 {
623 if (!read_buffer && !read_stdin)
624 close(fd);
625 return FAIL;
626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100628 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
630 /*
631 * Set '[ mark to the line above where the lines go (line 1 if zero).
632 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100633 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
635 curbuf->b_op_start.col = 0;
636
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100637 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
638 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
639 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (!read_buffer)
642 {
643 int m = msg_scroll;
644 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 /*
647 * The file must be closed again, the autocommands may want to change
648 * the file before reading it.
649 */
650 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100651 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 /*
654 * The output from the autocommands should not overwrite anything and
655 * should not be overwritten: Set msg_scroll, restore its value if no
656 * output was done.
657 */
658 msg_scroll = TRUE;
659 if (filtering)
660 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
661 FALSE, curbuf, eap);
662 else if (read_stdin)
663 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
664 FALSE, curbuf, eap);
665 else if (newfile)
666 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
667 FALSE, curbuf, eap);
668 else
669 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
670 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100671 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100672 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
673 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
674 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100675 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if (msg_scrolled == n)
678 msg_scroll = m;
679
680#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 --no_wait_return;
684 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100685 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 return FAIL;
687 }
688#endif
689 /*
690 * Don't allow the autocommands to change the current buffer.
691 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000692 *
693 * Don't allow the autocommands to change the buffer name either
694 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 */
696 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000697 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
698 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
700 {
701 --no_wait_return;
702 msg_scroll = msg_save;
703 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100704 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100706 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100707 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 return FAIL;
709 }
710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100712 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
714
715 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
716 {
717 /*
718 * Show the user that we are busy reading the input. Sometimes this
719 * may take a while. When reading from stdin another program may
720 * still be running, don't move the cursor to the last line, unless
721 * always using the GUI.
722 */
723 if (read_stdin)
724 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100725 if (!is_not_a_term())
726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200728# ifdef VIMDLL
729 if (!gui.in_use)
730# endif
731 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100734 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100735 if (gui.in_use && !gui.dying && !gui.starting)
736 {
737 p = (char_u *)_("Reading from stdin...");
738 gui_write(p, (int)STRLEN(p));
739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743 else if (!read_buffer)
744 filemess(curbuf, sfname, (char_u *)"", 0);
745 }
746
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100747 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
749 /*
750 * Set linecnt now, before the "retry" caused by a wrong guess for
751 * fileformat, and after the autocommands, which may change them.
752 */
753 linecnt = curbuf->b_ml.ml_line_count;
754
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100755 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000756 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000757 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000758 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000759 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000760 curbuf->b_bad_char = eap->bad_char;
761 }
762 else
763 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000766 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
768 if (eap != NULL && eap->force_enc != 0)
769 {
770 fenc = enc_canonize(eap->cmd + eap->force_enc);
771 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000772 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774 else if (curbuf->b_p_bin)
775 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100776 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 fenc_alloced = FALSE;
778 }
779 else if (curbuf->b_help)
780 {
781 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000782 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100784 // Help files are either utf-8 or latin1. Try utf-8 first, if this
785 // fails it must be latin1.
786 // Always do this when 'encoding' is "utf-8". Otherwise only do
787 // this when needed to avoid [converted] remarks all the time.
788 // It is needed when the first line contains non-ASCII characters.
789 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 fenc = (char_u *)"latin1";
791 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000792 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000794 fc = fname[STRLEN(fname) - 1];
795 if (TOLOWER_ASC(fc) == 'x')
796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // Read the first line (and a bit more). Immediately rewind to
798 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100799 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200800 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000801 for (p = firstline; p < firstline + len; ++p)
802 if (*p >= 0x80)
803 {
804 c = TRUE;
805 break;
806 }
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809
810 if (c)
811 {
812 fenc_next = fenc;
813 fenc = (char_u *)"utf-8";
814
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 // When the file is utf-8 but a character doesn't fit in
816 // 'encoding' don't retry. In help text editing utf-8 bytes
817 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000818 if (!enc_utf8)
819 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
821 fenc_alloced = FALSE;
822 }
823 else if (*p_fencs == NUL)
824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 fenc_alloced = FALSE;
827 }
828 else
829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200831 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 /*
835 * Jump back here to retry reading the file in different ways.
836 * Reasons to retry:
837 * - encoding conversion failed: try another one from "fenc_next"
838 * - BOM detected and fenc was set, need to setup conversion
839 * - "fileformat" check failed: try another
840 *
841 * Variables set for special retry actions:
842 * "file_rewind" Rewind the file to start reading it again.
843 * "advance_fenc" Advance "fenc" using "fenc_next".
844 * "skip_read" Re-use already read bytes (BOM detected).
845 * "did_iconv" iconv() conversion failed, try 'charconvert'.
846 * "keep_fileformat" Don't reset "fileformat".
847 *
848 * Other status indicators:
849 * "tmpname" When != NULL did conversion with 'charconvert'.
850 * Output file has to be deleted afterwards.
851 * "iconv_fd" When != -1 did conversion with iconv().
852 */
853retry:
854
855 if (file_rewind)
856 {
857 if (read_buffer)
858 {
859 read_buf_lnum = 1;
860 read_buf_col = 0;
861 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200862 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100864 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 error = TRUE;
866 goto failed;
867 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200870 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000872 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000875 curbuf->b_start_bomb = FALSE;
876 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000877 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
879
880 /*
881 * When retrying with another "fenc" and the first time "fileformat"
882 * will be reset.
883 */
884 if (keep_fileformat)
885 keep_fileformat = FALSE;
886 else
887 {
888 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000891 try_unix = try_dos = try_mac = FALSE;
892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900
Bram Moolenaar13505972019-01-24 15:04:48 +0100901#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (iconv_fd != (iconv_t)-1)
903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100904 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 iconv_close(iconv_fd);
906 iconv_fd = (iconv_t)-1;
907 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910 if (advance_fenc)
911 {
912 /*
913 * Try the next entry in 'fileencodings'.
914 */
915 advance_fenc = FALSE;
916
917 if (eap != NULL && eap->force_enc != 0)
918 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100919 // Conversion given with "++cc=" wasn't possible, read
920 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000922 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (fenc_alloced)
924 vim_free(fenc);
925 fenc = (char_u *)"";
926 fenc_alloced = FALSE;
927 }
928 else
929 {
930 if (fenc_alloced)
931 vim_free(fenc);
932 if (fenc_next != NULL)
933 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200934 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
936 else
937 {
938 fenc = (char_u *)"";
939 fenc_alloced = FALSE;
940 }
941 }
942 if (tmpname != NULL)
943 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100944 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100945 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 }
948
949 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000950 * Conversion may be required when the encoding of the file is different
951 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 */
953 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000954 converted = need_conversion(fenc);
955 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100958 // "ucs-bom" means we need to check the first bytes of the file
959 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (STRCMP(fenc, ENC_UCSBOM) == 0)
961 fio_flags = FIO_UCSBOM;
962
963 /*
964 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
965 * done. This is handled below after read(). Prepare the
966 * fio_flags to avoid having to parse the string each time.
967 * Also check for Unicode to Latin1 conversion, because iconv()
968 * appears not to handle this correctly. This works just like
969 * conversion to UTF-8 except how the resulting character is put in
970 * the buffer.
971 */
972 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
973 fio_flags = get_fio_flags(fenc);
974
Bram Moolenaar4f974752019-02-17 17:44:42 +0100975#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /*
977 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
978 * is handled with MultiByteToWideChar().
979 */
980 if (fio_flags == 0)
981 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar13505972019-01-24 15:04:48 +0100984#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if (fio_flags == 0)
987 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
Bram Moolenaar13505972019-01-24 15:04:48 +0100990#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 /*
992 * Try using iconv() if we can't convert internally.
993 */
994 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +0100995# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 )
999 iconv_fd = (iconv_t)my_iconv_open(
1000 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
Bram Moolenaar13505972019-01-24 15:04:48 +01001003#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /*
1005 * Use the 'charconvert' expression when conversion is required
1006 * and we can't do it internally or with iconv().
1007 */
1008 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001009 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001010# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 )
1014 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001015# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001017# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 // Skip conversion when it's already done (retry for wrong
1019 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (tmpname == NULL)
1021 {
1022 tmpname = readfile_charconvert(fname, fenc, &fd);
1023 if (tmpname == NULL)
1024 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001025 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 advance_fenc = TRUE;
1027 if (fd < 0)
1028 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001029 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001030 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 error = TRUE;
1032 goto failed;
1033 }
1034 goto retry;
1035 }
1036 }
1037 }
1038 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001042#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 )
1046 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001047 // Conversion wanted but we can't.
1048 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 advance_fenc = TRUE;
1050 goto retry;
1051 }
1052 }
1053 }
1054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Set "can_retry" when it's possible to rewind the file and try with
1056 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1057 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001058 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 if (!skip_read)
1061 {
1062 linerest = 0;
1063 filesize = 0;
1064 skip_count = lines_to_skip;
1065 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001067#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001068 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1069 && curbuf->b_ffname != NULL
1070 && curbuf->b_p_udf
1071 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001072 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001073 && !read_stdin
1074 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001075 if (read_undo_file)
1076 sha256_start(&sha_ctx);
1077#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001078#ifdef FEAT_CRYPT
1079 if (curbuf->b_cryptstate != NULL)
1080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001081 // Need to free the state, but keep the key, don't want to ask for
1082 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001083 crypt_free_state(curbuf->b_cryptstate);
1084 curbuf->b_cryptstate = NULL;
1085 }
1086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088
1089 while (!error && !got_int)
1090 {
1091 /*
1092 * We allocate as much space for the file as we can get, plus
1093 * space for the old line plus room for one terminating NUL.
1094 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001095 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001097 if (!skip_read)
1098 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001099#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001100 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001101#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001102 // Use buffer >= 64K. Add linerest to double the size if the
1103 // line gets very long, to avoid a lot of copying. But don't
1104 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001105 size = 0x10000L + linerest;
1106 if (size > 0x100000L)
1107 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001108#endif
1109 }
1110
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001111 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001112 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001115 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 size = 1;
1117 }
1118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
1120 if (!skip_read)
1121 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001122 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001124 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 FALSE)) != NULL)
1126 break;
1127 }
1128 if (new_buffer == NULL)
1129 {
1130 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1131 error = TRUE;
1132 break;
1133 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001134 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1136 vim_free(buffer);
1137 buffer = new_buffer;
1138 ptr = buffer + linerest;
1139 line_start = buffer;
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // May need room to translate into.
1142 // For iconv() we don't really know the required space, use a
1143 // factor ICONV_MULT.
1144 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1145 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1146 // become up to 4 bytes, size must be multiple of 2
1147 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1148 // multiple of 2
1149 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1150 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001151 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001152#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (iconv_fd != (iconv_t)-1)
1154 size = size / ICONV_MULT;
1155 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (fio_flags & FIO_LATIN1)
1158 size = size / 2;
1159 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1160 size = (size * 2 / 3) & ~1;
1161 else if (fio_flags & FIO_UCS4)
1162 size = (size * 2 / 3) & ~3;
1163 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001165#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001167 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001168#endif
1169#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001171 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (conv_restlen > 0)
1175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001176 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 mch_memmove(ptr, conv_rest, conv_restlen);
1178 ptr += conv_restlen;
1179 size -= conv_restlen;
1180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182 if (read_buffer)
1183 {
1184 /*
1185 * Read bytes from curbuf. Used for converting text read
1186 * from stdin.
1187 */
1188 if (read_buf_lnum > from)
1189 size = 0;
1190 else
1191 {
1192 int n, ni;
1193 long tlen;
1194
1195 tlen = 0;
1196 for (;;)
1197 {
1198 p = ml_get(read_buf_lnum) + read_buf_col;
1199 n = (int)STRLEN(p);
1200 if ((int)tlen + n + 1 > size)
1201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001202 // Filled up to "size", append partial line.
1203 // Change NL to NUL to reverse the effect done
1204 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001205 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 for (ni = 0; ni < n; ++ni)
1207 {
1208 if (p[ni] == NL)
1209 ptr[tlen++] = NUL;
1210 else
1211 ptr[tlen++] = p[ni];
1212 }
1213 read_buf_col += n;
1214 break;
1215 }
1216 else
1217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 // Append whole line and new-line. Change NL
1219 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 for (ni = 0; ni < n; ++ni)
1221 {
1222 if (p[ni] == NL)
1223 ptr[tlen++] = NUL;
1224 else
1225 ptr[tlen++] = p[ni];
1226 }
1227 ptr[tlen++] = NL;
1228 read_buf_col = 0;
1229 if (++read_buf_lnum > from)
1230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001231 // When the last line didn't have an
1232 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (!curbuf->b_p_eol)
1234 --tlen;
1235 size = tlen;
1236 break;
1237 }
1238 }
1239 }
1240 }
1241 }
1242 else
1243 {
1244 /*
1245 * Read bytes from the file.
1246 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001247 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001250#ifdef FEAT_CRYPT
1251 /*
1252 * At start of file: Check for magic number of encryption.
1253 */
1254 if (filesize == 0 && size > 0)
1255 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1256 &filesize, newfile, sfname,
1257 &did_ask_for_key);
1258 /*
1259 * Decrypt the read bytes. This is done before checking for
1260 * EOF because the crypt layer may be buffering.
1261 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001262 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1263 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001264 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001265# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266 if (crypt_works_inplace(curbuf->b_cryptstate))
1267 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001268# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001269 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001270# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001271 }
1272 else
1273 {
1274 char_u *newptr = NULL;
1275 int decrypted_size;
1276
1277 decrypted_size = crypt_decode_alloc(
1278 curbuf->b_cryptstate, ptr, size, &newptr);
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 // If the crypt layer is buffering, not producing
1281 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001282 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001283 continue;
1284
1285 if (linerest == 0)
1286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001287 // Simple case: reuse returned buffer (may be
1288 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001289 new_buffer = newptr;
1290 }
1291 else
1292 {
1293 long_u new_size;
1294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001295 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001296 new_size = (long_u)(decrypted_size + linerest + 1);
1297 new_buffer = lalloc(new_size, FALSE);
1298 if (new_buffer == NULL)
1299 {
1300 do_outofmem_msg(new_size);
1301 error = TRUE;
1302 break;
1303 }
1304
1305 mch_memmove(new_buffer, buffer, linerest);
1306 if (newptr != NULL)
1307 mch_memmove(new_buffer + linerest, newptr,
1308 decrypted_size);
1309 }
1310
1311 if (new_buffer != NULL)
1312 {
1313 vim_free(buffer);
1314 buffer = new_buffer;
1315 new_buffer = NULL;
1316 line_start = buffer;
1317 ptr = buffer + linerest;
1318 }
1319 size = decrypted_size;
1320 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001321# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001322 }
1323#endif
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (size <= 0)
1326 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001327 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001330 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001331 /*
1332 * Reached end-of-file but some trailing bytes could
1333 * not be converted. Truncated file?
1334 */
1335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001336 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001337 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001338#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001339 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001340#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001341 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001342 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001343 if (can_retry)
1344 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001345 if (conv_error == 0)
1346 conv_error = curbuf->b_ml.ml_line_count
1347 - linecnt + 1;
1348 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001349 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001350 else if (illegal_byte == 0)
1351 illegal_byte = curbuf->b_ml.ml_line_count
1352 - linecnt + 1;
1353 if (bad_char_behavior == BAD_DROP)
1354 {
1355 *(ptr - conv_restlen) = NUL;
1356 conv_restlen = 0;
1357 }
1358 else
1359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001360 // Replace the trailing bytes with the replacement
1361 // character if we were converting; if we weren't,
1362 // leave the UTF8 checking code to do it, as it
1363 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001364 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001365#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001366 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001367#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001368 ))
1369 {
1370 while (conv_restlen > 0)
1371 {
1372 *(--ptr) = bad_char_behavior;
1373 --conv_restlen;
1374 }
1375 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001376 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001377#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001378 if (iconv_fd != (iconv_t)-1)
1379 {
1380 iconv_close(iconv_fd);
1381 iconv_fd = (iconv_t)-1;
1382 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001383#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001384 }
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 skip_read = FALSE;
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /*
1391 * At start of file (or after crypt magic number): Check for BOM.
1392 * Also check for a BOM for other Unicode encodings, but not after
1393 * converting with 'charconvert' or when a BOM has already been
1394 * found.
1395 */
1396 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001397#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001398 || (cryptkey != NULL
1399 && filesize == crypt_get_header_len(
1400 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 )
1403 && (fio_flags == FIO_UCSBOM
1404 || (!curbuf->b_p_bomb
1405 && tmpname == NULL
1406 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1407 {
1408 char_u *ccname;
1409 int blen;
1410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001411 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (size < 2 || curbuf->b_p_bin)
1413 ccname = NULL;
1414 else
1415 ccname = check_for_bom(ptr, size, &blen,
1416 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1417 if (ccname != NULL)
1418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001419 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 filesize += blen;
1421 size -= blen;
1422 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001423 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001426 curbuf->b_start_bomb = TRUE;
1427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429
1430 if (fio_flags == FIO_UCSBOM)
1431 {
1432 if (ccname == NULL)
1433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 advance_fenc = TRUE;
1436 }
1437 else
1438 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001439 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (fenc_alloced)
1441 vim_free(fenc);
1442 fenc = ccname;
1443 fenc_alloced = FALSE;
1444 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001445 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 skip_read = TRUE;
1447 goto retry;
1448 }
1449 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001451 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001452 ptr -= conv_restlen;
1453 size += conv_restlen;
1454 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /*
1456 * Break here for a read error or end-of-file.
1457 */
1458 if (size <= 0)
1459 break;
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
Bram Moolenaar13505972019-01-24 15:04:48 +01001462#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (iconv_fd != (iconv_t)-1)
1464 {
1465 /*
1466 * Attempt conversion of the read bytes to 'encoding' using
1467 * iconv().
1468 */
1469 const char *fromp;
1470 char *top;
1471 size_t from_size;
1472 size_t to_size;
1473
1474 fromp = (char *)ptr;
1475 from_size = size;
1476 ptr += size;
1477 top = (char *)ptr;
1478 to_size = real_size - size;
1479
1480 /*
1481 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001482 * another conversion. Except for when there is no
1483 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001485 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1486 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1488 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001489 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001490 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001491 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001492 if (conv_error == 0)
1493 conv_error = readfile_linenr(linecnt,
1494 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001496 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001497 ++fromp;
1498 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001499 if (bad_char_behavior == BAD_KEEP)
1500 {
1501 *top++ = *(fromp - 1);
1502 --to_size;
1503 }
1504 else if (bad_char_behavior != BAD_DROP)
1505 {
1506 *top++ = bad_char_behavior;
1507 --to_size;
1508 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
1511 if (from_size > 0)
1512 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001513 // Some remaining characters, keep them for the next
1514 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1516 conv_restlen = (int)from_size;
1517 }
1518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 line_start = ptr - linerest;
1521 mch_memmove(line_start, buffer, (size_t)linerest);
1522 size = (long)((char_u *)top - ptr);
1523 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar4f974752019-02-17 17:44:42 +01001526#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (fio_flags & FIO_CODEPAGE)
1528 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001529 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001530 WCHAR ucs2buf[3];
1531 int ucs2len;
1532 int codepage = FIO_GET_CP(fio_flags);
1533 int bytelen;
1534 int found_bad;
1535 char replstr[2];
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /*
1538 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001539 * a codepage, using standard MS-Windows functions. This
1540 * requires two steps:
1541 * 1. convert from 'fileencoding' to ucs-2
1542 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001544 * Because there may be illegal bytes AND an incomplete byte
1545 * sequence at the end, we may have to do the conversion one
1546 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001549 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001550 if (bad_char_behavior > 0)
1551 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001553 replstr[0] = '?';
1554 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
1556 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001557 * Move the bytes to the end of the buffer, so that we have
1558 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001560 src = ptr + real_size - size;
1561 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001563 /*
1564 * Do the conversion.
1565 */
1566 dst = ptr;
1567 size = size;
1568 while (size > 0)
1569 {
1570 found_bad = FALSE;
1571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001572# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001573 if (codepage == CP_UTF8)
1574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001575 // Handle CP_UTF8 input ourselves to be able to handle
1576 // trailing bytes properly.
1577 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001578 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001579 if (bytelen > size)
1580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001581 // Only got some bytes of a character. Normally
1582 // it's put in "conv_rest", but if it's too long
1583 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001584 if (bytelen <= CONV_RESTLEN)
1585 break;
1586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001587 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001588 bytelen = size;
1589 found_bad = TRUE;
1590 }
1591 else
1592 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001593 int u8c = utf_ptr2char(src);
1594
Bram Moolenaar86e01082005-12-29 22:45:34 +00001595 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001596 found_bad = TRUE;
1597 ucs2buf[0] = u8c;
1598 ucs2len = 1;
1599 }
1600 }
1601 else
1602# endif
1603 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001604 // We don't know how long the byte sequence is, try
1605 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001606 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1607 ++bytelen)
1608 {
1609 ucs2len = MultiByteToWideChar(codepage,
1610 MB_ERR_INVALID_CHARS,
1611 (LPCSTR)src, bytelen,
1612 ucs2buf, 3);
1613 if (ucs2len > 0)
1614 break;
1615 }
1616 if (ucs2len == 0)
1617 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001618 // If we have only one byte then it's probably an
1619 // incomplete byte sequence. Otherwise discard
1620 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001621 if (size == 1)
1622 break;
1623 found_bad = TRUE;
1624 bytelen = 1;
1625 }
1626 }
1627
1628 if (!found_bad)
1629 {
1630 int i;
1631
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001633 if (enc_utf8)
1634 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001635 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001636 for (i = 0; i < ucs2len; ++i)
1637 dst += utf_char2bytes(ucs2buf[i], dst);
1638 }
1639 else
1640 {
1641 BOOL bad = FALSE;
1642 int dstlen;
1643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001644 // From UCS-2 to "enc_codepage". If the
1645 // conversion uses the default character "?",
1646 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001647 dstlen = WideCharToMultiByte(enc_codepage, 0,
1648 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001649 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001650 replstr, &bad);
1651 if (bad)
1652 found_bad = TRUE;
1653 else
1654 dst += dstlen;
1655 }
1656 }
1657
1658 if (found_bad)
1659 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001660 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001661 if (can_retry)
1662 goto rewind_retry;
1663 if (conv_error == 0)
1664 conv_error = readfile_linenr(linecnt, ptr, dst);
1665 if (bad_char_behavior != BAD_DROP)
1666 {
1667 if (bad_char_behavior == BAD_KEEP)
1668 {
1669 mch_memmove(dst, src, bytelen);
1670 dst += bytelen;
1671 }
1672 else
1673 *dst++ = bad_char_behavior;
1674 }
1675 }
1676
1677 src += bytelen;
1678 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001680
1681 if (size > 0)
1682 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001683 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001684 mch_memmove(conv_rest, src, size);
1685 conv_restlen = size;
1686 }
1687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001688 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001689 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001692#endif
1693#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (fio_flags & FIO_MACROMAN)
1695 {
1696 /*
1697 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001698 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001700 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (fio_flags != 0)
1706 {
1707 int u8c;
1708 char_u *dest;
1709 char_u *tail = NULL;
1710
1711 /*
1712 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1713 * "enc_utf8" not set: Convert Unicode to Latin1.
1714 * Go from end to start through the buffer, because the number
1715 * of bytes may increase.
1716 * "dest" points to after where the UTF-8 bytes go, "p" points
1717 * to after the next character to convert.
1718 */
1719 dest = ptr + real_size;
1720 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1721 {
1722 p = ptr + size;
1723 if (fio_flags == FIO_UTF8)
1724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001725 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 tail = ptr + size - 1;
1727 while (tail > ptr && (*tail & 0xc0) == 0x80)
1728 --tail;
1729 if (tail + utf_byte2len(*tail) <= ptr + size)
1730 tail = NULL;
1731 else
1732 p = tail;
1733 }
1734 }
1735 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001737 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 p = ptr + (size & ~1);
1739 if (size & 1)
1740 tail = p;
1741 if ((fio_flags & FIO_UTF16) && p > ptr)
1742 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001743 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (fio_flags & FIO_ENDIAN_L)
1745 {
1746 u8c = (*--p << 8);
1747 u8c += *--p;
1748 }
1749 else
1750 {
1751 u8c = *--p;
1752 u8c += (*--p << 8);
1753 }
1754 if (u8c >= 0xd800 && u8c <= 0xdbff)
1755 tail = p;
1756 else
1757 p += 2;
1758 }
1759 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001762 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 p = ptr + (size & ~3);
1764 if (size & 3)
1765 tail = p;
1766 }
1767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001768 // If there is a trailing incomplete sequence move it to
1769 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (tail != NULL)
1771 {
1772 conv_restlen = (int)((ptr + size) - tail);
1773 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1774 size -= conv_restlen;
1775 }
1776
1777
1778 while (p > ptr)
1779 {
1780 if (fio_flags & FIO_LATIN1)
1781 u8c = *--p;
1782 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1783 {
1784 if (fio_flags & FIO_ENDIAN_L)
1785 {
1786 u8c = (*--p << 8);
1787 u8c += *--p;
1788 }
1789 else
1790 {
1791 u8c = *--p;
1792 u8c += (*--p << 8);
1793 }
1794 if ((fio_flags & FIO_UTF16)
1795 && u8c >= 0xdc00 && u8c <= 0xdfff)
1796 {
1797 int u16c;
1798
1799 if (p == ptr)
1800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001801 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (can_retry)
1803 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001804 if (conv_error == 0)
1805 conv_error = readfile_linenr(linecnt,
1806 ptr, p);
1807 if (bad_char_behavior == BAD_DROP)
1808 continue;
1809 if (bad_char_behavior != BAD_KEEP)
1810 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001813 // found second word of double-word, get the first
1814 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (fio_flags & FIO_ENDIAN_L)
1816 {
1817 u16c = (*--p << 8);
1818 u16c += *--p;
1819 }
1820 else
1821 {
1822 u16c = *--p;
1823 u16c += (*--p << 8);
1824 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001825 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1826 + (u8c & 0x3ff);
1827
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001828 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (u16c < 0xd800 || u16c > 0xdbff)
1830 {
1831 if (can_retry)
1832 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001833 if (conv_error == 0)
1834 conv_error = readfile_linenr(linecnt,
1835 ptr, p);
1836 if (bad_char_behavior == BAD_DROP)
1837 continue;
1838 if (bad_char_behavior != BAD_KEEP)
1839 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 }
1843 else if (fio_flags & FIO_UCS4)
1844 {
1845 if (fio_flags & FIO_ENDIAN_L)
1846 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001847 u8c = (unsigned)*--p << 24;
1848 u8c += (unsigned)*--p << 16;
1849 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 u8c += *--p;
1851 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001852 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001855 u8c += (unsigned)*--p << 8;
1856 u8c += (unsigned)*--p << 16;
1857 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001860 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 if (*--p < 0x80)
1863 u8c = *p;
1864 else
1865 {
1866 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001867 p -= len;
1868 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (len == 0)
1870 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 // Not a valid UTF-8 character, retry with
1872 // another fenc when possible, otherwise just
1873 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (can_retry)
1875 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001876 if (conv_error == 0)
1877 conv_error = readfile_linenr(linecnt,
1878 ptr, p);
1879 if (bad_char_behavior == BAD_DROP)
1880 continue;
1881 if (bad_char_behavior != BAD_KEEP)
1882 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 dest -= utf_char2len(u8c);
1889 (void)utf_char2bytes(u8c, dest);
1890 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001891 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 --dest;
1894 if (u8c >= 0x100)
1895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001896 // character doesn't fit in latin1, retry with
1897 // another fenc when possible, otherwise just
1898 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001899 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001901 if (conv_error == 0)
1902 conv_error = readfile_linenr(linecnt, ptr, p);
1903 if (bad_char_behavior == BAD_DROP)
1904 ++dest;
1905 else if (bad_char_behavior == BAD_KEEP)
1906 *dest = u8c;
1907 else if (eap != NULL && eap->bad_char != 0)
1908 *dest = bad_char_behavior;
1909 else
1910 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 else
1913 *dest = u8c;
1914 }
1915 }
1916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001917 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 line_start = dest - linerest;
1919 mch_memmove(line_start, buffer, (size_t)linerest);
1920 size = (long)((ptr + real_size) - dest);
1921 ptr = dest;
1922 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001923 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001925 int incomplete_tail = FALSE;
1926
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001927 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001928 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001930 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001931 int l;
1932
1933 if (todo <= 0)
1934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (*p >= 0x80)
1936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001937 // A length of 1 means it's an illegal byte. Accept
1938 // an incomplete character at the end though, the next
1939 // read() will get the next bytes, we'll check it
1940 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001941 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001942 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001944 // Avoid retrying with a different encoding when
1945 // a truncated file is more likely, or attempting
1946 // to read the rest of an incomplete sequence when
1947 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001948 if (p > ptr || filesize > 0)
1949 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001950 // Incomplete byte sequence, move it to conv_rest[]
1951 // and try to read the rest of it, unless we've
1952 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001953 if (p > ptr)
1954 {
1955 conv_restlen = todo;
1956 mch_memmove(conv_rest, p, conv_restlen);
1957 size -= conv_restlen;
1958 break;
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001961 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 // Illegal byte. If we can try another encoding
1964 // do that, unless at EOF where a truncated
1965 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001966 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001967 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001968#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001969 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001970 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1971 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001972#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001973 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001974 if (conv_error == 0 && illegal_byte == 0)
1975 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001978 if (bad_char_behavior == BAD_DROP)
1979 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001980 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001981 --p;
1982 --size;
1983 }
1984 else if (bad_char_behavior != BAD_KEEP)
1985 *p = bad_char_behavior;
1986 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001987 else
1988 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001991 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001995 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01001996#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001997 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001999 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002001#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002003 advance_fenc = TRUE;
2004 file_rewind = TRUE;
2005 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 filesize += size;
2011
2012 /*
2013 * when reading the first part of a file: guess EOL type
2014 */
2015 if (fileformat == EOL_UNKNOWN)
2016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (try_dos || try_unix)
2019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002021 if (try_mac)
2022 try_mac = 1;
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 for (p = ptr; p < ptr + size; ++p)
2025 {
2026 if (*p == NL)
2027 {
2028 if (!try_unix
2029 || (try_dos && p > ptr && p[-1] == CAR))
2030 fileformat = EOL_DOS;
2031 else
2032 fileformat = EOL_UNIX;
2033 break;
2034 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002035 else if (*p == CAR && try_mac)
2036 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (fileformat == EOL_UNIX && try_mac)
2041 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 try_mac = 1;
2044 try_unix = 1;
2045 for (; p >= ptr && *p != CAR; p--)
2046 ;
2047 if (p >= ptr)
2048 {
2049 for (p = ptr; p < ptr + size; ++p)
2050 {
2051 if (*p == NL)
2052 try_unix++;
2053 else if (*p == CAR)
2054 try_mac++;
2055 }
2056 if (try_mac > try_unix)
2057 fileformat = EOL_MAC;
2058 }
2059 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002060 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // Looking for CR but found no end-of-line markers at
2062 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002063 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (fileformat == EOL_UNKNOWN && try_mac)
2068 fileformat = EOL_MAC;
2069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (fileformat == EOL_UNKNOWN)
2072 fileformat = default_fileformat();
2073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002075 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 set_fileformat(fileformat, OPT_LOCAL);
2077 }
2078 }
2079
2080 /*
2081 * This loop is executed once for every character read.
2082 * Keep it fast!
2083 */
2084 if (fileformat == EOL_MAC)
2085 {
2086 --ptr;
2087 while (++ptr, --size >= 0)
2088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if ((c = *ptr) != NUL && c != CAR && c != NL)
2091 continue;
2092 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002095 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 else
2097 {
2098 if (skip_count == 0)
2099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 len = (colnr_T) (ptr - line_start + 1);
2102 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2103 {
2104 error = TRUE;
2105 break;
2106 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002107#ifdef FEAT_PERSISTENT_UNDO
2108 if (read_undo_file)
2109 sha256_update(&sha_ctx, line_start, len);
2110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 ++lnum;
2112 if (--read_count == 0)
2113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002114 error = TRUE; // break loop
2115 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 break;
2117 }
2118 }
2119 else
2120 --skip_count;
2121 line_start = ptr + 1;
2122 }
2123 }
2124 }
2125 else
2126 {
2127 --ptr;
2128 while (++ptr, --size >= 0)
2129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002130 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 continue;
2132 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 else
2135 {
2136 if (skip_count == 0)
2137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 len = (colnr_T)(ptr - line_start + 1);
2140 if (fileformat == EOL_DOS)
2141 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002142 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002144 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 ptr[-1] = NUL;
2146 --len;
2147 }
2148 /*
2149 * Reading in Dos format, but no CR-LF found!
2150 * When 'fileformats' includes "unix", delete all
2151 * the lines read so far and start all over again.
2152 * Otherwise give an error message later.
2153 */
2154 else if (ff_error != EOL_DOS)
2155 {
2156 if ( try_unix
2157 && !read_stdin
2158 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002159 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2160 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 set_fileformat(EOL_UNIX, OPT_LOCAL);
2165 file_rewind = TRUE;
2166 keep_fileformat = TRUE;
2167 goto retry;
2168 }
2169 ff_error = EOL_DOS;
2170 }
2171 }
2172 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2173 {
2174 error = TRUE;
2175 break;
2176 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002177#ifdef FEAT_PERSISTENT_UNDO
2178 if (read_undo_file)
2179 sha256_update(&sha_ctx, line_start, len);
2180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 ++lnum;
2182 if (--read_count == 0)
2183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002184 error = TRUE; // break loop
2185 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 break;
2187 }
2188 }
2189 else
2190 --skip_count;
2191 line_start = ptr + 1;
2192 }
2193 }
2194 }
2195 linerest = (long)(ptr - line_start);
2196 ui_breakcheck();
2197 }
2198
2199failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (error && read_count == 0)
2202 error = FALSE;
2203
2204 /*
2205 * If we get EOF in the middle of a line, note the fact and
2206 * complete the line ourselves.
2207 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2208 */
2209 if (!error
2210 && !got_int
2211 && linerest != 0
2212 && !(!curbuf->b_p_bin
2213 && fileformat == EOL_DOS
2214 && *line_start == Ctrl_Z
2215 && ptr == line_start + 1))
2216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002217 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002218 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 curbuf->b_p_eol = FALSE;
2220 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002221 len = (colnr_T)(ptr - line_start + 1);
2222 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 error = TRUE;
2224 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002225 {
2226#ifdef FEAT_PERSISTENT_UNDO
2227 if (read_undo_file)
2228 sha256_update(&sha_ctx, line_start, len);
2229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002235 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002238 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002239 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002240 crypt_free_state(curbuf->b_cryptstate);
2241 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002242 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002243 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2244 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 // Don't set cryptkey to NULL, it's used below as a flag that
2246 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247#endif
2248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 // If editing a new file: set 'fenc' for the current buffer.
2250 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002253 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (fenc_alloced)
2255 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002256#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#endif
2260
2261 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002262 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002263#ifdef HAVE_FD_CLOEXEC
2264 else
2265 {
2266 int fdflags = fcntl(fd, F_GETFD);
2267 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002268 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002269 }
2270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 vim_free(buffer);
2272
2273#ifdef HAVE_DUP
2274 if (read_stdin)
2275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002278 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280#endif
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (tmpname != NULL)
2283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 vim_free(tmpname);
2286 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 /*
2290 * In recovery mode everything but autocommands is skipped.
2291 */
2292 if (!recoverymode)
2293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002294 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2296 {
2297#ifdef FEAT_NETBEANS_INTG
2298 netbeansFireChanges = 0;
2299#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002300 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#ifdef FEAT_NETBEANS_INTG
2302 netbeansFireChanges = 1;
2303#endif
2304 --linecnt;
2305 }
2306 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2307 if (filesize == 0)
2308 linecnt = 0;
2309 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002312#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // After reading the text into the buffer the diff info needs to
2314 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002315 diff_invalidate(curbuf);
2316#endif
2317#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // All folds in the window are invalid now. Mark them for update
2319 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002320 foldUpdateAll(curwin);
2321#endif
2322 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002323 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 appended_lines_mark(from, linecnt);
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#ifndef ALWAYS_USE_GUI
2327 /*
2328 * If we were reading from the same terminal as where messages go,
2329 * the screen will have been messed up.
2330 * Switch on raw mode now and clear the screen.
2331 */
2332 if (read_stdin)
2333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 starttermcap();
2336 screenclear();
2337 }
2338#endif
2339
2340 if (got_int)
2341 {
2342 if (!(flags & READ_DUMMY))
2343 {
2344 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2345 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348 msg_scroll = msg_save;
2349#ifdef FEAT_VIMINFO
2350 check_marks_read();
2351#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002352 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354
2355 if (!filtering && !(flags & READ_DUMMY))
2356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002357 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 c = FALSE;
2359
2360#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 STRCAT(IObuff, _("[fifo]"));
2364 c = TRUE;
2365 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
2368 STRCAT(IObuff, _("[socket]"));
2369 c = TRUE;
2370 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002371# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002372 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002373 {
2374 STRCAT(IObuff, _("[character special]"));
2375 c = TRUE;
2376 }
2377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#endif
2379 if (curbuf->b_p_ro)
2380 {
2381 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2382 c = TRUE;
2383 }
2384 if (read_no_eol_lnum)
2385 {
2386 msg_add_eol();
2387 c = TRUE;
2388 }
2389 if (ff_error == EOL_DOS)
2390 {
2391 STRCAT(IObuff, _("[CR missing]"));
2392 c = TRUE;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (split)
2395 {
2396 STRCAT(IObuff, _("[long lines split]"));
2397 c = TRUE;
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (notconverted)
2400 {
2401 STRCAT(IObuff, _("[NOT converted]"));
2402 c = TRUE;
2403 }
2404 else if (converted)
2405 {
2406 STRCAT(IObuff, _("[converted]"));
2407 c = TRUE;
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#ifdef FEAT_CRYPT
2410 if (cryptkey != NULL)
2411 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002412 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 c = TRUE;
2414 }
2415#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002416 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002418 sprintf((char *)IObuff + STRLEN(IObuff),
2419 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 c = TRUE;
2421 }
2422 else if (illegal_byte > 0)
2423 {
2424 sprintf((char *)IObuff + STRLEN(IObuff),
2425 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2426 c = TRUE;
2427 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002428 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 STRCAT(IObuff, _("[READ ERRORS]"));
2431 c = TRUE;
2432 }
2433 if (msg_add_fileformat(fileformat))
2434 c = TRUE;
2435#ifdef FEAT_CRYPT
2436 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002437 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002438 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 else
2440#endif
2441 msg_add_lines(c, (long)linecnt, filesize);
2442
Bram Moolenaard23a8232018-02-10 18:45:26 +01002443 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 msg_scrolled_ign = TRUE;
2445#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002446 // Don't show the message when reading stdin, it would end up in a
2447 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (read_stdin || read_buffer)
2449 p = msg_may_trunc(FALSE, IObuff);
2450 else
2451#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002452 {
2453 if (msg_col > 0)
2454 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002455 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002458 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002459 // Need to repeat the message after redrawing when:
2460 // - When reading from stdin (the screen will be cleared next).
2461 // - When restart_edit is set (otherwise there will be a delay
2462 // before redrawing).
2463 // - When the screen was scrolled but there is no wait-return
2464 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002465 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 msg_scrolled_ign = FALSE;
2467 }
2468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002469 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002471 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002472 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 curbuf->b_p_ro = TRUE;
2474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002475 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477 /*
2478 * In Ex mode: cursor at last new line.
2479 * Otherwise: cursor at first new line.
2480 */
2481 if (exmode_active)
2482 curwin->w_cursor.lnum = from + linecnt;
2483 else
2484 curwin->w_cursor.lnum = from + 1;
2485 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002486 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002488 if (!cmdmod.lockmarks)
2489 {
2490 // Set '[ and '] marks to the newly read lines.
2491 curbuf->b_op_start.lnum = from + 1;
2492 curbuf->b_op_start.col = 0;
2493 curbuf->b_op_end.lnum = from + linecnt;
2494 curbuf->b_op_end.col = 0;
2495 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002496
Bram Moolenaar4f974752019-02-17 17:44:42 +01002497#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002498 /*
2499 * Work around a weird problem: When a file has two links (only
2500 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002501 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002502 * It's correct again after reading the file, thus reset the timestamp
2503 * here.
2504 */
2505 if (newfile && !read_stdin && !read_buffer
2506 && mch_stat((char *)fname, &st) >= 0)
2507 {
2508 buf_store_time(curbuf, &st, fname);
2509 curbuf->b_mtime_read = curbuf->b_mtime;
2510 }
2511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513 msg_scroll = msg_save;
2514
2515#ifdef FEAT_VIMINFO
2516 /*
2517 * Get the marks before executing autocommands, so they can be used there.
2518 */
2519 check_marks_read();
2520#endif
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002523 * We remember if the last line of the read didn't have
2524 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2525 * or writing the read again with 'binary' on. The latter is required
2526 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002528 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002530 // When reloading a buffer put the cursor at the first line that is
2531 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002532 if (flags & READ_KEEP_UNDO)
2533 u_find_first_changed();
2534
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002535#ifdef FEAT_PERSISTENT_UNDO
2536 /*
2537 * When opening a new file locate undo info and read it.
2538 */
2539 if (read_undo_file)
2540 {
2541 char_u hash[UNDO_HASH_SIZE];
2542
2543 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002544 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002545 }
2546#endif
2547
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002548 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
2550 int m = msg_scroll;
2551 int n = msg_scrolled;
2552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002553 // Save the fileformat now, otherwise the buffer will be considered
2554 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002555 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 save_file_ff(curbuf);
2557
2558 /*
2559 * The output from the autocommands should not overwrite anything and
2560 * should not be overwritten: Set msg_scroll, restore its value if no
2561 * output was done.
2562 */
2563 msg_scroll = TRUE;
2564 if (filtering)
2565 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2566 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002567 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2570 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002571 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2572 /*
2573 * EVENT_FILETYPE was not triggered but the buffer already has a
2574 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2575 */
2576 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2577 TRUE, curbuf);
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 else
2580 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2581 FALSE, NULL, eap);
2582 if (msg_scrolled == n)
2583 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002584# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002585 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590 if (recoverymode && error)
2591 return FAIL;
2592 return OK;
2593}
2594
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002595#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002596/*
2597 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2598 * which is the name of files used for process substitution output by
2599 * some shells on some operating systems, e.g., bash on SunOS.
2600 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2601 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002602 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002604{
2605 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2606 && VIM_ISDIGIT(fname[8])
2607 && *skipdigits(fname + 9) == NUL
2608 && (fname[9] != NUL
2609 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2610}
2611#endif
2612
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002613/*
2614 * From the current line count and characters read after that, estimate the
2615 * line number where we are now.
2616 * Used for error messages that include a line number.
2617 */
2618 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002619readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002620 linenr_T linecnt, // line count before reading more bytes
2621 char_u *p, // start of more bytes read
2622 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002623{
2624 char_u *s;
2625 linenr_T lnum;
2626
2627 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2628 for (s = p; s < endp; ++s)
2629 if (*s == '\n')
2630 ++lnum;
2631 return lnum;
2632}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002635 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2636 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 * Returns OK or FAIL.
2638 */
2639 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002640prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
Bram Moolenaar13505972019-01-24 15:04:48 +01002642 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (eap->cmd == NULL)
2644 return FAIL;
2645
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002646 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2647 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002648 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002649 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002650
2651 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002652 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002653 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return OK;
2655}
2656
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002657/*
2658 * Set default or forced 'fileformat' and 'binary'.
2659 */
2660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002661set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002662{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002663 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002664 if (set_options)
2665 {
2666 if (eap != NULL && eap->force_ff != 0)
2667 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2668 else if (*p_ffs != NUL)
2669 set_fileformat(default_fileformat(), OPT_LOCAL);
2670 }
2671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002672 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002673 if (eap != NULL && eap->force_bin != 0)
2674 {
2675 int oldval = curbuf->b_p_bin;
2676
2677 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2678 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2679 }
2680}
2681
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002682/*
2683 * Set forced 'fileencoding'.
2684 */
2685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002686set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002687{
2688 if (eap->force_enc != 0)
2689 {
2690 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2691
2692 if (fenc != NULL)
2693 set_string_option_direct((char_u *)"fenc", -1,
2694 fenc, OPT_FREE|OPT_LOCAL, 0);
2695 vim_free(fenc);
2696 }
2697}
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699/*
2700 * Find next fileencoding to use from 'fileencodings'.
2701 * "pp" points to fenc_next. It's advanced to the next item.
2702 * When there are no more items, an empty string is returned and *pp is set to
2703 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002704 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2705 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
2707 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002708next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 char_u *p;
2711 char_u *r;
2712
Bram Moolenaarf077db22019-08-13 00:18:24 +02002713 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (**pp == NUL)
2715 {
2716 *pp = NULL;
2717 return (char_u *)"";
2718 }
2719 p = vim_strchr(*pp, ',');
2720 if (p == NULL)
2721 {
2722 r = enc_canonize(*pp);
2723 *pp += STRLEN(*pp);
2724 }
2725 else
2726 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002727 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 *pp = p + 1;
2729 if (r != NULL)
2730 {
2731 p = enc_canonize(r);
2732 vim_free(r);
2733 r = p;
2734 }
2735 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002736 if (r != NULL)
2737 *alloced = TRUE;
2738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002740 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 r = (char_u *)"";
2742 *pp = NULL;
2743 }
2744 return r;
2745}
2746
Bram Moolenaar13505972019-01-24 15:04:48 +01002747#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748/*
2749 * Convert a file with the 'charconvert' expression.
2750 * This closes the file which is to be read, converts it and opens the
2751 * resulting file for reading.
2752 * Returns name of the resulting converted file (the caller should delete it
2753 * after reading it).
2754 * Returns NULL if the conversion failed ("*fdp" is not set) .
2755 */
2756 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002757readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002758 char_u *fname, // name of input file
2759 char_u *fenc, // converted from
2760 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002763 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002765 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002767 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 else
2769 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002770 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 *fdp = -1;
2772 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2773 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002774 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2776 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002777 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
2780 if (errmsg != NULL)
2781 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002782 // Don't use emsg(), it breaks mappings, the retry with
2783 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002784 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (tmpname != NULL)
2786 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002787 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002788 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 }
2791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002792 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (*fdp < 0)
2794 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2795
2796 return tmpname;
2797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798#endif
2799
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002800#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002802 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2804 * *filesizep are updated.
2805 * Return the (new) encryption key, NULL for no encryption.
2806 */
2807 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002808check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002809 char_u *cryptkey, // previous encryption key or NULL
2810 char_u *ptr, // pointer to read bytes
2811 long *sizep, // length of read bytes
2812 off_T *filesizep, // nr of bytes used from file
2813 int newfile, // editing a new buffer
2814 char_u *fname, // file name to display
2815 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002817 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002818 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002819
2820 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002822 // Mark the buffer as read-only until the decryption has taken place.
2823 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002824 curbuf->b_p_ro = TRUE;
2825
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002826 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002827 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002828 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
2830 if (*curbuf->b_p_key)
2831 cryptkey = curbuf->b_p_key;
2832 else
2833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002834 // When newfile is TRUE, store the typed key in the 'key'
2835 // option and don't free it. bf needs hash of the key saved.
2836 // Don't ask for the key again when first time Enter was hit.
2837 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002838 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002839 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002840 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002841 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002842 *did_ask = TRUE;
2843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002844 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (cryptkey != NULL && *cryptkey == NUL)
2846 {
2847 if (cryptkey != curbuf->b_p_key)
2848 vim_free(cryptkey);
2849 cryptkey = NULL;
2850 }
2851 }
2852 }
2853
2854 if (cryptkey != NULL)
2855 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002856 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002857
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002858 curbuf->b_cryptstate = crypt_create_from_header(
2859 method, cryptkey, ptr);
2860 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002862 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002863 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002864 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002865 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002866 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002867 *filesizep += header_len;
2868 *sizep -= header_len;
2869 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002871 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002872 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 // When starting to edit a new file which does not have encryption, clear
2876 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002877 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2879
2880 return cryptkey;
2881}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002882#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002885 * Return TRUE if a file appears to be read-only from the file permissions.
2886 */
2887 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002888check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002889 char_u *fname, // full path to file
2890 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002891{
2892#ifndef USE_MCH_ACCESS
2893 int fd = 0;
2894#endif
2895
2896 return (
2897#ifdef USE_MCH_ACCESS
2898# ifdef UNIX
2899 (perm & 0222) == 0 ||
2900# endif
2901 mch_access((char *)fname, W_OK)
2902#else
2903 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2904 ? TRUE : (close(fd), FALSE)
2905#endif
2906 );
2907}
2908
Bram Moolenaara7870192019-02-14 12:56:36 +01002909#if defined(HAVE_FSYNC) || defined(PROTO)
2910/*
2911 * Call fsync() with Mac-specific exception.
2912 * Return fsync() result: zero for success.
2913 */
2914 int
2915vim_fsync(int fd)
2916{
2917 int r;
2918
2919# ifdef MACOS_X
2920 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002921 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002922# endif
2923 r = fsync(fd);
2924 return r;
2925}
2926#endif
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002929 * Set the name of the current buffer. Use when the buffer doesn't have a
2930 * name and a ":r" or ":w" command with a file name is used.
2931 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002932 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002933set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002934{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002935 buf_T *buf = curbuf;
2936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002938 if (curbuf->b_p_bl)
2939 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2940 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002941#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002942 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002943 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002944#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002945 if (curbuf != buf)
2946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002947 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002948 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002949 return FAIL;
2950 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002951
2952 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2953 curbuf->b_flags |= BF_NOTEDITED;
2954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002955 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002956 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2957 if (curbuf->b_p_bl)
2958 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002959#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002960 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002961 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002962#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002965 if (*curbuf->b_p_ft == NUL)
2966 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002967 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002968 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002969 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002970 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002971
2972 return OK;
2973}
2974
2975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 * Put file name into IObuff with quotes.
2977 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002978 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002979msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 if (fname == NULL)
2982 fname = (char_u *)"-stdin-";
2983 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2984 IObuff[0] = '"';
2985 STRCAT(IObuff, "\" ");
2986}
2987
2988/*
2989 * Append message for text mode to IObuff.
2990 * Return TRUE if something appended.
2991 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002992 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002993msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995#ifndef USE_CRNL
2996 if (eol_type == EOL_DOS)
2997 {
2998 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
2999 return TRUE;
3000 }
3001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (eol_type == EOL_MAC)
3003 {
3004 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3005 return TRUE;
3006 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003007#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (eol_type == EOL_UNIX)
3009 {
3010 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3011 return TRUE;
3012 }
3013#endif
3014 return FALSE;
3015}
3016
3017/*
3018 * Append line and character count to IObuff.
3019 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003020 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003021msg_add_lines(
3022 int insert_space,
3023 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003024 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 char_u *p;
3027
3028 p = IObuff + STRLEN(IObuff);
3029
3030 if (insert_space)
3031 *p++ = ' ';
3032 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003033 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003034 "%ldL, %lldC", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else
3036 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003037 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003039 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
3040 NGETTEXT("%lld character", "%lld characters", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003041 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043}
3044
3045/*
3046 * Append message for missing line separator to IObuff.
3047 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3052}
3053
Bram Moolenaar473952e2019-09-28 16:30:04 +02003054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003055time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003057#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003058 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3059 // the seconds. Since the roundoff is done when flushing the inode, the
3060 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 return (t1 - t2 > 1 || t2 - t1 > 1);
3062#else
3063 return (t1 != t2);
3064#endif
3065}
3066
3067/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003068 * Return TRUE if file encoding "fenc" requires conversion from or to
3069 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003071 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003072need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003074 int same_encoding;
3075 int enc_flags;
3076 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003078 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003079 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003080 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003081 fenc_flags = 0;
3082 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003083 else
3084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003085 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3086 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003087 enc_flags = get_fio_flags(p_enc);
3088 fenc_flags = get_fio_flags(fenc);
3089 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3090 }
3091 if (same_encoding)
3092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003093 // Specified encoding matches with 'encoding'. This requires
3094 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003095 return enc_unicode != 0;
3096 }
3097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003098 // Encodings differ. However, conversion is not needed when 'enc' is any
3099 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003100 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101}
3102
3103/*
3104 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3105 * internal conversion.
3106 * if "ptr" is an empty string, use 'encoding'.
3107 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003108 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 int prop;
3112
3113 if (*ptr == NUL)
3114 ptr = p_enc;
3115
3116 prop = enc_canon_props(ptr);
3117 if (prop & ENC_UNICODE)
3118 {
3119 if (prop & ENC_2BYTE)
3120 {
3121 if (prop & ENC_ENDIAN_L)
3122 return FIO_UCS2 | FIO_ENDIAN_L;
3123 return FIO_UCS2;
3124 }
3125 if (prop & ENC_4BYTE)
3126 {
3127 if (prop & ENC_ENDIAN_L)
3128 return FIO_UCS4 | FIO_ENDIAN_L;
3129 return FIO_UCS4;
3130 }
3131 if (prop & ENC_2WORD)
3132 {
3133 if (prop & ENC_ENDIAN_L)
3134 return FIO_UTF16 | FIO_ENDIAN_L;
3135 return FIO_UTF16;
3136 }
3137 return FIO_UTF8;
3138 }
3139 if (prop & ENC_LATIN1)
3140 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003141 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 return 0;
3143}
3144
Bram Moolenaar473952e2019-09-28 16:30:04 +02003145#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146/*
3147 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3148 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3149 * Used for conversion between 'encoding' and 'fileencoding'.
3150 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003151 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154 int cp;
3155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003156 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (!enc_utf8 && enc_codepage <= 0)
3158 return 0;
3159
3160 cp = encname2codepage(ptr);
3161 if (cp == 0)
3162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003163# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (STRCMP(ptr, "utf-8") == 0)
3165 cp = CP_UTF8;
3166 else
3167# endif
3168 return 0;
3169 }
3170 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3171}
3172#endif
3173
Bram Moolenaar473952e2019-09-28 16:30:04 +02003174#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175/*
3176 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3177 * needed for the internal conversion to/from utf-8 or latin1.
3178 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003180get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3183 && (enc_canon_props(ptr) & ENC_MACROMAN))
3184 return FIO_MACROMAN;
3185 return 0;
3186}
3187#endif
3188
3189/*
3190 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3191 * "size" must be at least 2.
3192 * Return the name of the encoding and set "*lenp" to the length.
3193 * Returns NULL when no BOM found.
3194 */
3195 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196check_for_bom(
3197 char_u *p,
3198 long size,
3199 int *lenp,
3200 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 char *name = NULL;
3203 int len = 2;
3204
3205 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003206 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003208 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 len = 3;
3210 }
3211 else if (p[0] == 0xff && p[1] == 0xfe)
3212 {
3213 if (size >= 4 && p[2] == 0 && p[3] == 0
3214 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003216 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 len = 4;
3218 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003219 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003220 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003221 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003222 // utf-16le is preferred, it also works for ucs-2le text
3223 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225 else if (p[0] == 0xfe && p[1] == 0xff
3226 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003229 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003230 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003231 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003232 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3235 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003237 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 len = 4;
3239 }
3240
3241 *lenp = len;
3242 return (char_u *)name;
3243}
3244
3245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 * Try to find a shortname by comparing the fullname with the current
3247 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003248 * Returns "full_path" or pointer into "full_path" if shortened.
3249 */
3250 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003251shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003252{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003253 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003254 char_u *p = full_path;
3255
Bram Moolenaard9462e32011-04-11 21:35:11 +02003256 dirname = alloc(MAXPATHL);
3257 if (dirname == NULL)
3258 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003259 if (mch_dirname(dirname, MAXPATHL) == OK)
3260 {
3261 p = shorten_fname(full_path, dirname);
3262 if (p == NULL || *p == NUL)
3263 p = full_path;
3264 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003265 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003266 return p;
3267}
3268
3269/*
3270 * Try to find a shortname by comparing the fullname with the current
3271 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 * Returns NULL if not shorter name possible, pointer into "full_path"
3273 * otherwise.
3274 */
3275 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003276shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 int len;
3279 char_u *p;
3280
3281 if (full_path == NULL)
3282 return NULL;
3283 len = (int)STRLEN(dir_name);
3284 if (fnamencmp(dir_name, full_path, len) == 0)
3285 {
3286 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003287#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003289 * MS-Windows: when a file is in the root directory, dir_name will end
3290 * in a slash, since C: by itself does not define a specific dir. In
3291 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
3293 if (!((len > 2) && (*(p - 2) == ':')))
3294#endif
3295 {
3296 if (vim_ispathsep(*p))
3297 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003298#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 else
3300 p = NULL;
3301#endif
3302 }
3303 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003304#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 /*
3306 * When using a file in the current drive, remove the drive name:
3307 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3308 * a floppy from "A:\dir" to "B:\dir".
3309 */
3310 else if (len > 3
3311 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3312 && full_path[1] == ':'
3313 && vim_ispathsep(full_path[2]))
3314 p = full_path + 2;
3315#endif
3316 else
3317 p = NULL;
3318 return p;
3319}
3320
3321/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003322 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 * When "force" is TRUE: Use full path from now on for files currently being
3324 * edited, both for file name and swap file name. Try to shorten the file
3325 * names a bit, if safe to do so.
3326 * When "force" is FALSE: Only try to shorten absolute file names.
3327 * For buffers that have buftype "nofile" or "scratch": never change the file
3328 * name.
3329 */
3330 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003331shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3332{
3333 char_u *p;
3334
3335 if (buf->b_fname != NULL
3336#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003337 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003338#endif
3339 && !path_with_url(buf->b_fname)
3340 && (force
3341 || buf->b_sfname == NULL
3342 || mch_isFullName(buf->b_sfname)))
3343 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003344 if (buf->b_sfname != buf->b_ffname)
3345 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003346 p = shorten_fname(buf->b_ffname, dirname);
3347 if (p != NULL)
3348 {
3349 buf->b_sfname = vim_strsave(p);
3350 buf->b_fname = buf->b_sfname;
3351 }
3352 if (p == NULL || buf->b_fname == NULL)
3353 buf->b_fname = buf->b_ffname;
3354 }
3355}
3356
3357/*
3358 * Shorten filenames for all buffers.
3359 */
3360 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003361shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 char_u dirname[MAXPATHL];
3364 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003367 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003369 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003370
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003371 // Always make the swap file name a full path, a "nofile" buffer may
3372 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003373 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003376 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003377#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003378 popup_update_preview_title();
3379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381
3382#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3383 || defined(FEAT_GUI_MSWIN) \
3384 || defined(FEAT_GUI_MAC) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003385 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 || defined(PROTO)
3387/*
3388 * Shorten all filenames in "fnames[count]" by current directory.
3389 */
3390 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003391shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 int i;
3394 char_u dirname[MAXPATHL];
3395 char_u *p;
3396
3397 if (fnames == NULL || count < 1)
3398 return;
3399 mch_dirname(dirname, sizeof(dirname));
3400 for (i = 0; i < count; ++i)
3401 {
3402 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3403 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003404 // shorten_fname() returns pointer in given "fnames[i]". If free
3405 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3406 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 p = vim_strsave(p);
3408 vim_free(fnames[i]);
3409 fnames[i] = p;
3410 }
3411 }
3412}
3413#endif
3414
3415/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003416 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 * fo_o_h.ext for MSDOS or when shortname option set.
3418 *
3419 * Assumed that fname is a valid name found in the filesystem we assure that
3420 * the return value is a different name and ends in 'ext'.
3421 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3422 * characters otherwise.
3423 * Space for the returned name is allocated, must be freed later.
3424 * Returns NULL when out of memory.
3425 */
3426 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427modname(
3428 char_u *fname,
3429 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003430 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003432 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 fname, ext, prepend_dot);
3434}
3435
3436 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003438 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003439 char_u *fname,
3440 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003441 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *retval;
3444 char_u *s;
3445 char_u *e;
3446 char_u *ptr;
3447 int fnamelen, extlen;
3448
3449 extlen = (int)STRLEN(ext);
3450
3451 /*
3452 * If there is no file name we must get the name of the current directory
3453 * (we need the full path in case :cd is used).
3454 */
3455 if (fname == NULL || *fname == NUL)
3456 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003457 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (retval == NULL)
3459 return NULL;
3460 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3461 (fnamelen = (int)STRLEN(retval)) == 0)
3462 {
3463 vim_free(retval);
3464 return NULL;
3465 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003466 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 retval[fnamelen++] = PATHSEP;
3469 retval[fnamelen] = NUL;
3470 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003471 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 else
3474 {
3475 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003476 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (retval == NULL)
3478 return NULL;
3479 STRCPY(retval, fname);
3480#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003481 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482#endif
3483 }
3484
3485 /*
3486 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3487 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3488 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3489 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3490 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003491 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003493 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003494 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003497 {
3498 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003503 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3505 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 s = ptr + STRLEN(ptr);
3508
3509 /*
3510 * For 8.3 file names we may have to reduce the length.
3511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 /*
3515 * If there is no file name, or the file name ends in '/', and the
3516 * extension starts with '.', put a '_' before the dot, because just
3517 * ".ext" is invalid.
3518 */
3519 if (fname == NULL || *fname == NUL
3520 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 *s++ = '_';
3524 }
3525 /*
3526 * If the extension starts with '.', truncate the base name at 8
3527 * characters
3528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003531 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 s = ptr + 8;
3534 *s = '\0';
3535 }
3536 }
3537 /*
3538 * If the extension doesn't start with '.', and the file name
3539 * doesn't have an extension yet, append a '.'
3540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 else if ((e = vim_strchr(ptr, '.')) == NULL)
3542 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003545 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
3547 else if ((int)STRLEN(e) + extlen > 4)
3548 s = e + 4 - extlen;
3549 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003550#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 /*
3552 * If there is no file name, and the extension starts with '.', put a
3553 * '_' before the dot, because just ".ext" may be invalid if it's on a
3554 * FAT partition, and on HPFS it doesn't matter.
3555 */
3556 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3557 *s++ = '_';
3558#endif
3559
3560 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003561 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 * ext can start with '.' and cannot exceed 3 more characters.
3563 */
3564 STRCPY(s, ext);
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 /*
3567 * Prepend the dot.
3568 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003569 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003571 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 /*
3576 * Check that, after appending the extension, the file name is really
3577 * different.
3578 */
3579 if (fname != NULL && STRCMP(fname, retval) == 0)
3580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003581 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 while (--s >= ptr)
3583 {
3584 if (*s != '_')
3585 {
3586 *s = '_';
3587 break;
3588 }
3589 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003590 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 *ptr = 'v';
3592 }
3593 return retval;
3594}
3595
3596/*
3597 * Like fgets(), but if the file line is too long, it is truncated and the
3598 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003599 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
3601 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003602vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 char *eof;
3605#define FGETS_SIZE 200
3606 char tbuf[FGETS_SIZE];
3607
3608 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3611 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003612 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003614 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 do
3616 {
3617 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003618 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3620 }
3621 return (eof == NULL);
3622}
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624/*
3625 * rename() only works if both files are on the same file system, this
3626 * function will (attempts to?) copy the file across if rename fails -- webb
3627 * Return -1 for failure, 0 for success.
3628 */
3629 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003630vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632 int fd_in;
3633 int fd_out;
3634 int n;
3635 char *errmsg = NULL;
3636 char *buffer;
3637#ifdef AMIGA
3638 BPTR flock;
3639#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003640 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003641 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003642#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003643 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003644#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003645 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003648 * When the names are identical, there is nothing to do. When they refer
3649 * to the same file (ignoring case and slash/backslash differences) but
3650 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
3652 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003653 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003654 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003655 use_tmp_file = TRUE;
3656 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003657 return 0;
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 /*
3661 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3662 */
3663 if (mch_stat((char *)from, &st) < 0)
3664 return -1;
3665
Bram Moolenaar3576da72008-12-30 15:15:57 +00003666#ifdef UNIX
3667 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003668 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003669
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003670 // It's possible for the source and destination to be the same file.
3671 // This happens when "from" and "to" differ in case and are on a FAT32
3672 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003673 if (mch_stat((char *)to, &st_to) >= 0
3674 && st.st_dev == st_to.st_dev
3675 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003676 use_tmp_file = TRUE;
3677 }
3678#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003679#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003680 {
3681 BY_HANDLE_FILE_INFORMATION info1, info2;
3682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003683 // It's possible for the source and destination to be the same file.
3684 // In that case go through a temp file name. This makes rename("foo",
3685 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003686 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3687 && win32_fileinfo(to, &info2) == FILEINFO_OK
3688 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3689 && info1.nFileIndexHigh == info2.nFileIndexHigh
3690 && info1.nFileIndexLow == info2.nFileIndexLow)
3691 use_tmp_file = TRUE;
3692 }
3693#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003694
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003695 if (use_tmp_file)
3696 {
3697 char tempname[MAXPATHL + 1];
3698
3699 /*
3700 * Find a name that doesn't exist and is in the same directory.
3701 * Rename "from" to "tempname" and then rename "tempname" to "to".
3702 */
3703 if (STRLEN(from) >= MAXPATHL - 5)
3704 return -1;
3705 STRCPY(tempname, from);
3706 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003707 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003708 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3709 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003710 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003711 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003712 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003713 if (mch_rename(tempname, (char *)to) == 0)
3714 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003715 // Strange, the second step failed. Try moving the
3716 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003717 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003718 return -1;
3719 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003720 // If it fails for one temp name it will most likely fail
3721 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003722 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003723 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003724 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003725 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003726 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 /*
3729 * Delete the "to" file, this is required on some systems to make the
3730 * mch_rename() work, on other systems it makes sure that we don't have
3731 * two files when the mch_rename() fails.
3732 */
3733
3734#ifdef AMIGA
3735 /*
3736 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3737 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003738 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 * deleting the "from" file (horror!) we lock it during the remove.
3740 *
3741 * When used for making a backup before writing the file: This should not
3742 * happen with ":w", because startscript() should detect this problem and
3743 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3744 * name. This problem does exist with ":w filename", but then the
3745 * original file will be somewhere else so the backup isn't really
3746 * important. If autoscripting is off the rename may fail.
3747 */
3748 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3749#endif
3750 mch_remove(to);
3751#ifdef AMIGA
3752 if (flock)
3753 UnLock(flock);
3754#endif
3755
3756 /*
3757 * First try a normal rename, return if it works.
3758 */
3759 if (mch_rename((char *)from, (char *)to) == 0)
3760 return 0;
3761
3762 /*
3763 * Rename() failed, try copying the file.
3764 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003765 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003766#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003767 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003768 acl = mch_get_acl(from);
3769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3771 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003772 {
3773#ifdef HAVE_ACL
3774 mch_free_acl(acl);
3775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003777 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003779 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003780 fd_out = mch_open((char *)to,
3781 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (fd_out == -1)
3783 {
3784 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003785#ifdef HAVE_ACL
3786 mch_free_acl(acl);
3787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 return -1;
3789 }
3790
Bram Moolenaar473952e2019-09-28 16:30:04 +02003791 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (buffer == NULL)
3793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003795 close(fd_in);
3796#ifdef HAVE_ACL
3797 mch_free_acl(acl);
3798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 return -1;
3800 }
3801
Bram Moolenaar473952e2019-09-28 16:30:04 +02003802 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003803 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 errmsg = _("E208: Error writing to \"%s\"");
3806 break;
3807 }
3808
3809 vim_free(buffer);
3810 close(fd_in);
3811 if (close(fd_out) < 0)
3812 errmsg = _("E209: Error closing \"%s\"");
3813 if (n < 0)
3814 {
3815 errmsg = _("E210: Error reading \"%s\"");
3816 to = from;
3817 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003818#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003819 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003820#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003821#ifdef HAVE_ACL
3822 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003823 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003824#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003825#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003826 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (errmsg != NULL)
3829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003830 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return -1;
3832 }
3833 mch_remove(from);
3834 return 0;
3835}
3836
3837static int already_warned = FALSE;
3838
3839/*
3840 * Check if any not hidden buffer has been changed.
3841 * Postpone the check if there are characters in the stuff buffer, a global
3842 * command is being executed, a mapping is being executed or an autocommand is
3843 * busy.
3844 * Returns TRUE if some message was written (screen should be redrawn and
3845 * cursor positioned).
3846 */
3847 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003848check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003849 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 buf_T *buf;
3852 int didit = 0;
3853 int n;
3854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003855 // Don't check timestamps while system() or another low-level function may
3856 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (no_check_timestamps > 0)
3858 return FALSE;
3859
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003860 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3861 // event and we would keep on checking if the file is steadily growing.
3862 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (focus && did_check_timestamps)
3864 {
3865 need_check_timestamps = TRUE;
3866 return FALSE;
3867 }
3868
3869 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003870 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 else
3873 {
3874 ++no_wait_return;
3875 did_check_timestamps = TRUE;
3876 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003877 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003879 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (buf->b_nwindows > 0)
3881 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003882 bufref_T bufref;
3883
3884 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 n = buf_check_timestamp(buf, focus);
3886 if (didit < n)
3887 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003888 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 // Autocommands have removed the buffer, start at the
3891 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 buf = firstbuf;
3893 continue;
3894 }
3895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 --no_wait_return;
3898 need_check_timestamps = FALSE;
3899 if (need_wait_return && didit == 2)
3900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003901 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003902 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 out_flush();
3904 }
3905 }
3906 return didit;
3907}
3908
3909/*
3910 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3911 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3912 * empty.
3913 */
3914 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003915move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 buf_T *tbuf = curbuf;
3918 int retval = OK;
3919 linenr_T lnum;
3920 char_u *p;
3921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 curbuf = tobuf;
3924 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3925 {
3926 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3927 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3928 {
3929 vim_free(p);
3930 retval = FAIL;
3931 break;
3932 }
3933 vim_free(p);
3934 }
3935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (retval != FAIL)
3938 {
3939 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003940 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003941 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003943 // Oops! We could try putting back the saved lines, but that
3944 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 retval = FAIL;
3946 break;
3947 }
3948 }
3949
3950 curbuf = tbuf;
3951 return retval;
3952}
3953
3954/*
3955 * Check if buffer "buf" has been changed.
3956 * Also check if the file for a new buffer unexpectedly appeared.
3957 * return 1 if a changed buffer was found.
3958 * return 2 if a message has been displayed.
3959 * return 0 otherwise.
3960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003962buf_check_timestamp(
3963 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003964 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003966 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 int stat_res;
3968 int retval = 0;
3969 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003970 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003972 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int helpmesg = FALSE;
3974 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003975 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3977 int can_reload = FALSE;
3978#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003979 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 int orig_mode = buf->b_orig_mode;
3981#ifdef FEAT_GUI
3982 int save_mouse_correct = need_mouse_correct;
3983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003985 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003986#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003987 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003988#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003989 bufref_T bufref;
3990
3991 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003993 // If there is no file name, the buffer is not loaded, 'buftype' is
3994 // set, we are in the middle of a save or being called recursively: ignore
3995 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (buf->b_ffname == NULL
3997 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02003998 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004001#ifdef FEAT_NETBEANS_INTG
4002 || isNetbeansBuffer(buf)
4003#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004004#ifdef FEAT_TERMINAL
4005 || buf->b_term != NULL
4006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 )
4008 return 0;
4009
4010 if ( !(buf->b_flags & BF_NOTEDITED)
4011 && buf->b_mtime != 0
4012 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4013 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004014 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#ifdef HAVE_ST_MODE
4016 || (int)st.st_mode != buf->b_orig_mode
4017#else
4018 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4019#endif
4020 ))
4021 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004022 long prev_b_mtime = buf->b_mtime;
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 retval = 1;
4025
Bram Moolenaar386bc822018-07-07 18:34:12 +02004026 // set b_mtime to stop further warnings (e.g., when executing
4027 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (stat_res < 0)
4029 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004030 // Check the file again later to see if it re-appears.
4031 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 buf->b_orig_size = 0;
4033 buf->b_orig_mode = 0;
4034 }
4035 else
4036 buf_store_time(buf, &st, buf->b_ffname);
4037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004038 // Don't do anything for a directory. Might contain the file
4039 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 if (mch_isdir(buf->b_fname))
4041 ;
4042
4043 /*
4044 * If 'autoread' is set, the buffer has no changes and the file still
4045 * exists, reload the buffer. Use the buffer-local option value if it
4046 * was set, the global option value otherwise.
4047 */
4048 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4049 && !bufIsChanged(buf) && stat_res >= 0)
4050 reload = TRUE;
4051 else
4052 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004053 if (stat_res < 0)
4054 reason = "deleted";
4055 else if (bufIsChanged(buf))
4056 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004057 /*
4058 * Check if the file contents really changed to avoid giving a
4059 * warning when only the timestamp was set (e.g., checked out of
4060 * CVS). Always warn when the buffer was changed.
4061 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004062 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4063 reason = "changed";
4064 else if (orig_mode != buf->b_orig_mode)
4065 reason = "mode";
4066 else
4067 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /*
4070 * Only give the warning if there are no FileChangedShell
4071 * autocommands.
4072 * Avoid being called recursively by setting "busy".
4073 */
4074 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004075#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004076 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4077 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004078#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004079 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4081 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004082 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 busy = FALSE;
4084 if (n)
4085 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004086 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004087 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004088#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004089 s = get_vim_var_str(VV_FCS_CHOICE);
4090 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4091 reload = TRUE;
4092 else if (STRCMP(s, "ask") == 0)
4093 n = FALSE;
4094 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004095#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004096 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004098 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004100 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004101 {
4102 // Only give the message once.
4103 if (prev_b_mtime != -1)
4104 mesg = _("E211: File \"%s\" no longer available");
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 else
4107 {
4108 helpmesg = TRUE;
4109#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4110 can_reload = TRUE;
4111#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004112 if (reason[2] == 'n')
4113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 mesg2 = _("See \":help W12\" for more info.");
4116 }
4117 else if (reason[1] == 'h')
4118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004120 mesg2 = _("See \":help W11\" for more info.");
4121 }
4122 else if (*reason == 'm')
4123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004125 mesg2 = _("See \":help W16\" for more info.");
4126 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004127 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004128 // Only timestamp changed, store it to avoid a warning
4129 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004130 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132 }
4133 }
4134
4135 }
4136 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4137 && vim_fexists(buf->b_ffname))
4138 {
4139 retval = 1;
4140 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4141 buf->b_flags |= BF_NEW_W;
4142#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4143 can_reload = TRUE;
4144#endif
4145 }
4146
4147 if (mesg != NULL)
4148 {
4149 path = home_replace_save(buf, buf->b_fname);
4150 if (path != NULL)
4151 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004152 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004154 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004155 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004156#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // Set warningmsg here, before the unimportant and output-specific
4158 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004159 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4162 if (can_reload)
4163 {
4164 if (*mesg2 != NUL)
4165 {
4166 STRCAT(tbuf, "\n");
4167 STRCAT(tbuf, mesg2);
4168 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004169 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4170 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004171 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 reload = TRUE;
4173 }
4174 else
4175#endif
4176 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4177 {
4178 if (*mesg2 != NUL)
4179 {
4180 STRCAT(tbuf, "; ");
4181 STRCAT(tbuf, mesg2);
4182 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004183 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 retval = 2;
4185 }
4186 else
4187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004191 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004193 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 msg_clr_eos();
4195 (void)msg_end();
4196 if (emsg_silent == 0)
4197 {
4198 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004199#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004201#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004202 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004203 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004205 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 redraw_cmdline = FALSE;
4207 }
4208 }
4209 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211
4212 vim_free(path);
4213 vim_free(tbuf);
4214 }
4215 }
4216
4217 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004219 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004220 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004221#ifdef FEAT_PERSISTENT_UNDO
4222 if (buf->b_p_udf && buf->b_ffname != NULL)
4223 {
4224 char_u hash[UNDO_HASH_SIZE];
4225 buf_T *save_curbuf = curbuf;
4226
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004227 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004228 curbuf = buf;
4229 u_compute_hash(hash);
4230 u_write_undo(NULL, FALSE, buf, hash);
4231 curbuf = save_curbuf;
4232 }
4233#endif
4234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004236 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004237 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004238 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4239 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004241 // restore this in case an autocommand has set it; it would break
4242 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 need_mouse_correct = save_mouse_correct;
4244#endif
4245
4246 return retval;
4247}
4248
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004249/*
4250 * Reload a buffer that is already loaded.
4251 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004252 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4253 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004254 */
4255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004256buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004257{
4258 exarg_T ea;
4259 pos_T old_cursor;
4260 linenr_T old_topline;
4261 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004262 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004263 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004264 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004265 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004266 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004268 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004269 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004271 // We only want to read the text from the file, not reset the syntax
4272 // highlighting, clear marks, diff status, etc. Force the fileformat
4273 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004274 if (prep_exarg(&ea, buf) == OK)
4275 {
4276 old_cursor = curwin->w_cursor;
4277 old_topline = curwin->w_topline;
4278
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004279 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004280 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 // Save all the text, so that the reload can be undone.
4282 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004283 u_sync(FALSE);
4284 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4285 flags |= READ_KEEP_UNDO;
4286 }
4287
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004288 /*
4289 * To behave like when a new file is edited (matters for
4290 * BufReadPost autocommands) we first need to delete the current
4291 * buffer contents. But if reading the file fails we should keep
4292 * the old contents. Can't use memory only, the file might be
4293 * too big. Use a hidden buffer to move the buffer contents to.
4294 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004295 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004296 savebuf = NULL;
4297 else
4298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004299 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004300 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004301 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004302 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004303 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004304 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004305 curbuf = savebuf;
4306 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004307 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004308 curbuf = buf;
4309 curwin->w_buffer = buf;
4310 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004311 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004312 || move_lines(buf, savebuf) == FAIL)
4313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004314 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004315 buf->b_fname);
4316 saved = FAIL;
4317 }
4318 }
4319
4320 if (saved == OK)
4321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004322 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4323 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004324 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4325 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004326 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004328#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004329 if (!aborting())
4330#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004331 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004332 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004334 // Put the text back from the save buffer. First
4335 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004336 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004337 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004338 break;
4339 (void)move_lines(savebuf, buf);
4340 }
4341 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004342 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004345 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004346 if ((flags & READ_KEEP_UNDO) == 0)
4347 {
4348 u_blockfree(buf);
4349 u_clearall(buf);
4350 }
4351 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004354 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004355 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004356 }
4357 }
4358 vim_free(ea.cmd);
4359
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004360 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 wipe_buffer(savebuf, FALSE);
4362
4363#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004365 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004366#endif
4367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004368 // Restore the topline and cursor position and check it (lines may
4369 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004370 if (old_topline > curbuf->b_ml.ml_line_count)
4371 curwin->w_topline = curbuf->b_ml.ml_line_count;
4372 else
4373 curwin->w_topline = old_topline;
4374 curwin->w_cursor = old_cursor;
4375 check_cursor();
4376 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004377 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004378#ifdef FEAT_FOLDING
4379 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004380 win_T *wp;
4381 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004384 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 if (wp->w_buffer == curwin->w_buffer
4386 && !foldmethodIsManual(wp))
4387 foldUpdateAll(wp);
4388 }
4389#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 // If the mode didn't change and 'readonly' was set, keep the old
4391 // value; the user probably used the ":view" command. But don't
4392 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393 if (orig_mode == curbuf->b_orig_mode)
4394 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004397 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004398 }
4399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004401 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004403}
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004406buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
4408 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004409 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410#ifdef HAVE_ST_MODE
4411 buf->b_orig_mode = (int)st->st_mode;
4412#else
4413 buf->b_orig_mode = mch_getperm(fname);
4414#endif
4415}
4416
4417/*
4418 * Adjust the line with missing eol, used for the next write.
4419 * Used for do_filter(), when the input lines for the filter are deleted.
4420 */
4421 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004422write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004425 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426}
4427
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004428// Subfuncions for readdirex()
4429#ifdef FEAT_EVAL
4430# ifdef MSWIN
4431 static char_u *
4432getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4433{
4434 stat_T st;
4435 unsigned short st_mode;
4436 DWORD flag = wfd->dwFileAttributes;
4437 WCHAR *wp;
4438
4439 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4440 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4441 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4442 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4443
4444 wp = wcsrchr(wfd->cFileName, L'.');
4445 if (wp != NULL)
4446 {
4447 if (_wcsicmp(wp, L".exe") == 0 ||
4448 _wcsicmp(wp, L".com") == 0 ||
4449 _wcsicmp(wp, L".cmd") == 0 ||
4450 _wcsicmp(wp, L".bat") == 0)
4451 st_mode |= _S_IEXEC;
4452 }
4453
4454 // Copy user bits to group/other.
4455 st_mode |= (st_mode & 0700) >> 3;
4456 st_mode |= (st_mode & 0700) >> 6;
4457
4458 st.st_mode = st_mode;
4459 return getfpermst(&st, perm);
4460}
4461
4462 static char_u *
4463getftypewfd(WIN32_FIND_DATAW *wfd)
4464{
4465 DWORD flag = wfd->dwFileAttributes;
4466 DWORD tag = wfd->dwReserved0;
4467
4468 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4469 {
4470 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4471 return (char_u*)"junction";
4472 else if (tag == IO_REPARSE_TAG_SYMLINK)
4473 {
4474 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4475 return (char_u*)"linkd";
4476 else
4477 return (char_u*)"link";
4478 }
4479 return (char_u*)"reparse"; // unknown reparse point type
4480 }
4481 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4482 return (char_u*)"dir";
4483 else
4484 return (char_u*)"file";
4485}
4486
4487 static dict_T *
4488create_readdirex_item(WIN32_FIND_DATAW *wfd)
4489{
4490 dict_T *item;
4491 char_u *p;
4492 varnumber_T size, time;
4493 char_u permbuf[] = "---------";
4494
4495 item = dict_alloc();
4496 if (item == NULL)
4497 return NULL;
4498 item->dv_refcount++;
4499
4500 p = utf16_to_enc(wfd->cFileName, NULL);
4501 if (p == NULL)
4502 goto theend;
4503 if (dict_add_string(item, "name", p) == FAIL)
4504 {
4505 vim_free(p);
4506 goto theend;
4507 }
4508 vim_free(p);
4509
4510 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4511 if (dict_add_number(item, "size", size) == FAIL)
4512 goto theend;
4513
4514 // Convert FILETIME to unix time.
4515 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4516 wfd->ftLastWriteTime.dwLowDateTime)
4517 - 116444736000000000) / 10000000;
4518 if (dict_add_number(item, "time", time) == FAIL)
4519 goto theend;
4520
4521 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4522 goto theend;
4523 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4524 goto theend;
4525
4526 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4527 goto theend;
4528 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4529 goto theend;
4530
4531 return item;
4532
4533theend:
4534 dict_unref(item);
4535 return NULL;
4536}
4537# else
4538 static dict_T *
4539create_readdirex_item(char_u *path, char_u *name)
4540{
4541 dict_T *item;
4542 char *p;
4543 size_t len;
4544 stat_T st;
4545 int ret, link = FALSE;
4546 varnumber_T size;
4547 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004548 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004549 struct passwd *pw;
4550 struct group *gr;
4551
4552 item = dict_alloc();
4553 if (item == NULL)
4554 return NULL;
4555 item->dv_refcount++;
4556
4557 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4558 p = alloc(len);
4559 if (p == NULL)
4560 goto theend;
4561 vim_snprintf(p, len, "%s/%s", path, name);
4562 ret = mch_lstat(p, &st);
4563 if (ret >= 0 && S_ISLNK(st.st_mode))
4564 {
4565 link = TRUE;
4566 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004567 if (ret < 0)
4568 q = (char_u*)"link";
4569
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004570 }
4571 vim_free(p);
4572
4573 if (dict_add_string(item, "name", name) == FAIL)
4574 goto theend;
4575
4576 if (ret >= 0)
4577 {
4578 size = (varnumber_T)st.st_size;
4579 if (S_ISDIR(st.st_mode))
4580 size = 0;
4581 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004582 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004583 size = -2;
4584 if (dict_add_number(item, "size", size) == FAIL)
4585 goto theend;
4586 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4587 goto theend;
4588
4589 if (link)
4590 {
4591 if (S_ISDIR(st.st_mode))
4592 q = (char_u*)"linkd";
4593 else
4594 q = (char_u*)"link";
4595 }
4596 else
4597 q = getftypest(&st);
4598 if (dict_add_string(item, "type", q) == FAIL)
4599 goto theend;
4600 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4601 goto theend;
4602
4603 pw = getpwuid(st.st_uid);
4604 if (pw == NULL)
4605 q = (char_u*)"";
4606 else
4607 q = (char_u*)pw->pw_name;
4608 if (dict_add_string(item, "user", q) == FAIL)
4609 goto theend;
4610 gr = getgrgid(st.st_gid);
4611 if (gr == NULL)
4612 q = (char_u*)"";
4613 else
4614 q = (char_u*)gr->gr_name;
4615 if (dict_add_string(item, "group", q) == FAIL)
4616 goto theend;
4617 }
4618 else
4619 {
4620 if (dict_add_number(item, "size", -1) == FAIL)
4621 goto theend;
4622 if (dict_add_number(item, "time", -1) == FAIL)
4623 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004624 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004625 goto theend;
4626 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4627 goto theend;
4628 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4629 goto theend;
4630 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4631 goto theend;
4632 }
4633 return item;
4634
4635theend:
4636 dict_unref(item);
4637 return NULL;
4638}
4639# endif
4640
4641 static int
4642compare_readdirex_item(const void *p1, const void *p2)
4643{
4644 char_u *name1, *name2;
4645
4646 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4647 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
4648 return STRCMP(name1, name2);
4649}
4650#endif
4651
Bram Moolenaarda440d22016-01-16 21:27:23 +01004652#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4653/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004654 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004655 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004656 * If "withattr" is TRUE, retrieve the names and their attributes.
4657 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004658 * Return OK for success, FAIL for failure.
4659 */
4660 int
4661readdir_core(
4662 garray_T *gap,
4663 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004664 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004665 void *context,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004666 int (*checkitem)(void *context, void *item))
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004667{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004668 int failed = FALSE;
4669 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004670# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004671 char_u *buf;
4672 int ok;
4673 HANDLE hFind = INVALID_HANDLE_VALUE;
4674 WIN32_FIND_DATAW wfd;
4675 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004676# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004677 DIR *dirp;
4678 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004679# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004680
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004681 ga_init2(gap, (int)sizeof(void *), 20);
4682
4683# ifdef FEAT_EVAL
4684# define FREE_ITEM(item) do { \
4685 if (withattr) \
4686 dict_unref((dict_T*)item); \
4687 else \
4688 vim_free(item); \
4689 } while (0)
4690# else
4691# define FREE_ITEM(item) vim_free(item)
4692# endif
4693
4694# ifdef MSWIN
4695 buf = alloc(MAXPATHL);
4696 if (buf == NULL)
4697 return FAIL;
4698 STRNCPY(buf, path, MAXPATHL-5);
4699 p = buf + STRLEN(buf);
4700 MB_PTR_BACK(buf, p);
4701 if (*p == '\\' || *p == '/')
4702 *p = NUL;
4703 STRCAT(p, "\\*");
4704
4705 wn = enc_to_utf16(buf, NULL);
4706 if (wn != NULL)
4707 hFind = FindFirstFileW(wn, &wfd);
4708 ok = (hFind != INVALID_HANDLE_VALUE);
4709 if (!ok)
4710 {
4711 failed = TRUE;
4712 smsg(_(e_notopen), path);
4713 }
4714 else
4715 {
4716 while (ok)
4717 {
4718 int ignore;
4719 void *item;
4720 WCHAR *wp;
4721
4722 wp = wfd.cFileName;
4723 ignore = wp[0] == L'.' &&
4724 (wp[1] == NUL ||
4725 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004726 if (ignore)
4727 {
4728 ok = FindNextFileW(hFind, &wfd);
4729 continue;
4730 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004731# ifdef FEAT_EVAL
4732 if (withattr)
4733 item = (void*)create_readdirex_item(&wfd);
4734 else
4735# endif
4736 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4737 if (item == NULL)
4738 {
4739 failed = TRUE;
4740 break;
4741 }
4742
4743 if (!ignore && checkitem != NULL)
4744 {
4745 int r = checkitem(context, item);
4746
4747 if (r < 0)
4748 {
4749 FREE_ITEM(item);
4750 break;
4751 }
4752 if (r == 0)
4753 ignore = TRUE;
4754 }
4755
4756 if (!ignore)
4757 {
4758 if (ga_grow(gap, 1) == OK)
4759 ((void**)gap->ga_data)[gap->ga_len++] = item;
4760 else
4761 {
4762 failed = TRUE;
4763 FREE_ITEM(item);
4764 break;
4765 }
4766 }
4767 else
4768 FREE_ITEM(item);
4769
4770 ok = FindNextFileW(hFind, &wfd);
4771 }
4772 FindClose(hFind);
4773 }
4774
4775 vim_free(buf);
4776 vim_free(wn);
4777# else // MSWIN
4778 dirp = opendir((char *)path);
4779 if (dirp == NULL)
4780 {
4781 failed = TRUE;
4782 smsg(_(e_notopen), path);
4783 }
4784 else
4785 {
4786 for (;;)
4787 {
4788 int ignore;
4789 void *item;
4790
4791 dp = readdir(dirp);
4792 if (dp == NULL)
4793 break;
4794 p = (char_u *)dp->d_name;
4795
4796 ignore = p[0] == '.' &&
4797 (p[1] == NUL ||
4798 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004799 if (ignore)
4800 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004801# ifdef FEAT_EVAL
4802 if (withattr)
4803 item = (void*)create_readdirex_item(path, p);
4804 else
4805# endif
4806 item = (void*)vim_strsave(p);
4807 if (item == NULL)
4808 {
4809 failed = TRUE;
4810 break;
4811 }
4812
4813 if (!ignore && checkitem != NULL)
4814 {
4815 int r = checkitem(context, item);
4816
4817 if (r < 0)
4818 {
4819 FREE_ITEM(item);
4820 break;
4821 }
4822 if (r == 0)
4823 ignore = TRUE;
4824 }
4825
4826 if (!ignore)
4827 {
4828 if (ga_grow(gap, 1) == OK)
4829 ((void**)gap->ga_data)[gap->ga_len++] = item;
4830 else
4831 {
4832 failed = TRUE;
4833 FREE_ITEM(item);
4834 break;
4835 }
4836 }
4837 else
4838 FREE_ITEM(item);
4839 }
4840
4841 closedir(dirp);
4842 }
4843# endif // MSWIN
4844
4845# undef FREE_ITEM
4846
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004847 if (!failed && gap->ga_len > 0)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004848 {
4849# ifdef FEAT_EVAL
4850 if (withattr)
4851 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4852 compare_readdirex_item);
4853 else
4854# endif
4855 sort_strings((char_u **)gap->ga_data, gap->ga_len);
4856 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004857
4858 return failed ? FAIL : OK;
4859}
4860
4861/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004862 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004863 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004864 */
4865 int
4866delete_recursive(char_u *name)
4867{
4868 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004869 int i;
4870 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004871 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004872
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004873 // A symbolic link to a directory itself is deleted, not the directory it
4874 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004875 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004876# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004877 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004878# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004879 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004880# endif
4881 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004882 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004883 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004884 if (exp == NULL)
4885 return -1;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004886 if (readdir_core(&ga, exp, FALSE, NULL, NULL) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004887 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004888 for (i = 0; i < ga.ga_len; ++i)
4889 {
4890 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4891 ((char_u **)ga.ga_data)[i]);
4892 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004893 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004894 }
4895 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004896 }
4897 else
4898 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004899 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004900 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004901 }
4902 else
4903 result = mch_remove(name) == 0 ? 0 : -1;
4904
4905 return result;
4906}
4907#endif
4908
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004910static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004912# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4913/*
4914 * Open temporary directory and take file lock to prevent
4915 * to be auto-cleaned.
4916 */
4917 static void
4918vim_opentempdir(void)
4919{
4920 DIR *dp = NULL;
4921
4922 if (vim_tempdir_dp != NULL)
4923 return;
4924
4925 dp = opendir((const char*)vim_tempdir);
4926
4927 if (dp != NULL)
4928 {
4929 vim_tempdir_dp = dp;
4930 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4931 }
4932}
4933
4934/*
4935 * Close temporary directory - it automatically release file lock.
4936 */
4937 static void
4938vim_closetempdir(void)
4939{
4940 if (vim_tempdir_dp != NULL)
4941 {
4942 closedir(vim_tempdir_dp);
4943 vim_tempdir_dp = NULL;
4944 }
4945}
4946# endif
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948/*
4949 * Delete the temp directory and all files it contains.
4950 */
4951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004952vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (vim_tempdir != NULL)
4955 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004956# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4957 vim_closetempdir();
4958# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004959 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004960 gettail(vim_tempdir)[-1] = NUL;
4961 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004962 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004967 * Directory "tempdir" was created. Expand this name to a full path and put
4968 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
4969 * "tempdir" must be no longer than MAXPATHL.
4970 */
4971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004972vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004973{
4974 char_u *buf;
4975
Bram Moolenaar964b3742019-05-24 18:54:09 +02004976 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004977 if (buf != NULL)
4978 {
4979 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
4980 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004981 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004982 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004983# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4984 vim_opentempdir();
4985# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004986 vim_free(buf);
4987 }
4988}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00004989#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004990
4991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * vim_tempname(): Return a unique name that can be used for a temp file.
4993 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02004994 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
4995 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 *
4997 * The returned pointer is to allocated memory.
4998 * The returned pointer is NULL if no valid name was found.
4999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005001vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005002 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005003 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005006 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005007#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005008 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009#else
5010 char_u itmp[TEMPNAMELEN];
5011#endif
5012
5013#ifdef TEMPDIRNAMES
5014 static char *(tempdirs[]) = {TEMPDIRNAMES};
5015 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005017 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018# endif
5019
5020 /*
5021 * This will create a directory for private use by this instance of Vim.
5022 * This is done once, and the same directory is used for all temp files.
5023 * This method avoids security problems because of symlink attacks et al.
5024 * It's also a bit faster, because we only need to check for an existing
5025 * file when creating the directory and not for each temp file.
5026 */
5027 if (vim_tempdir == NULL)
5028 {
5029 /*
5030 * Try the entries in TEMPDIRNAMES to create the temp directory.
5031 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005032 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005034# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005035 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005036 long nr;
5037 long off;
5038# endif
5039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 // Expand $TMP, leave room for "/v1100000/999999999".
5041 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005043 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005045 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005046 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaareaf03392009-11-17 11:08:52 +00005048# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005049 {
5050# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051 // Make sure the umask doesn't remove the executable bit.
5052 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005053 mode_t umask_save = umask(077);
5054# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005055 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005056 STRCAT(itmp, "vXXXXXX");
5057 if (mkdtemp((char *)itmp) != NULL)
5058 vim_settempdir(itmp);
5059# if defined(UNIX) || defined(VMS)
5060 (void)umask(umask_save);
5061# endif
5062 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005063# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 // Get an arbitrary number of up to 6 digits. When it's
5065 // unlikely that it already exists it will be faster,
5066 // otherwise it doesn't matter. The use of mkdir() avoids any
5067 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005069 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 // Try up to 10000 different values until we find a name that
5072 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 for (off = 0; off < 10000L; ++off)
5074 {
5075 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005076# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
Bram Moolenaareaf03392009-11-17 11:08:52 +00005080 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5081# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // If mkdir() does not set errno to EEXIST, check for
5083 // existing file here. There is a race condition then,
5084 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (mch_stat((char *)itmp, &st) >= 0)
5086 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005087# endif
5088# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 // Make sure the umask doesn't remove the executable bit.
5090 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005094# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (r == 0)
5098 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005099 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005102# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 // If the mkdir() didn't fail because the file/dir exists,
5104 // we probably can't create any dir here, try another
5105 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005110# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (vim_tempdir != NULL)
5112 break;
5113 }
5114 }
5115 }
5116
5117 if (vim_tempdir != NULL)
5118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005119 // There is no need to check if the file exists, because we own the
5120 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5122 return vim_strsave(itmp);
5123 }
5124
5125 return NULL;
5126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
Bram Moolenaar4f974752019-02-17 17:44:42 +01005129# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005130 WCHAR wszTempFile[_MAX_PATH + 1];
5131 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 char_u *retval;
5133 char_u *p;
5134
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005135 wcscpy(itmp, L"");
5136 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005137 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005138 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
5139 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005140 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005141 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005142 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005143 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005145 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005146 // GetTempFileName() will create the file, we don't want that
5147 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005149 // Backslashes in a temp file name cause problems when filtering with
5150 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5151 // didn't set 'shellslash'.
5152 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (*p_shcf == '-' || p_ssl)
5154 for (p = retval; *p; ++p)
5155 if (*p == '\\')
5156 *p = '/';
5157 return retval;
5158
Bram Moolenaar4f974752019-02-17 17:44:42 +01005159# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
5161# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005162 char_u *p;
5163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005164 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005165 p = tmpnam((char *)itmp);
5166 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 return NULL;
5168# else
5169 char_u *p;
5170
5171# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 // mktemp() is not working on VMS. It seems to be
5173 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 sprintf((char *)itmp, "VIM%c", extra_char);
5175 p = (char_u *)tempnam("tmp:", (char *)itmp);
5176 if (p != NULL)
5177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005178 // VMS will use '.LIS' if we don't explicitly specify an extension,
5179 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 STRCPY(itmp, p);
5181 STRCAT(itmp, ".txt");
5182 free(p);
5183 }
5184 else
5185 return NULL;
5186# else
5187 STRCPY(itmp, TEMPNAME);
5188 if ((p = vim_strchr(itmp, '?')) != NULL)
5189 *p = extra_char;
5190 if (mktemp((char *)itmp) == NULL)
5191 return NULL;
5192# endif
5193# endif
5194
5195 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005196# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005197#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198}
5199
5200#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5201/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005202 * Convert all backslashes in fname to forward slashes in-place, unless when
5203 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 */
5205 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005206forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 char_u *p;
5209
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005210 if (path_with_url(fname))
5211 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005213 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005214 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005216 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 *p = '/';
5218}
5219#endif
5220
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005221/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005222 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5223 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5224 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 * Used for autocommands and 'wildignore'.
5226 * Returns TRUE if there is a match, FALSE otherwise.
5227 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005228 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005229match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 char_u *pattern, // pattern to match with
5231 regprog_T **prog, // pre-compiled regprog or NULL
5232 char_u *fname, // full path of file name
5233 char_u *sfname, // short file name or NULL
5234 char_u *tail, // tail of path
5235 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 regmatch_T regmatch;
5238 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005241 if (prog != NULL)
5242 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005244 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 /*
5247 * Try for a match with the pattern with:
5248 * 1. the full file name, when the pattern has a '/'.
5249 * 2. the short file name, when the pattern has a '/'.
5250 * 3. the tail of the file name, when the pattern has no '/'.
5251 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005252 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 && ((allow_dirs
5254 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5255 || (sfname != NULL
5256 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005257 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 result = TRUE;
5259
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005260 if (prog != NULL)
5261 *prog = regmatch.regprog;
5262 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005263 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return result;
5265}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
5267#if defined(FEAT_WILDIGN) || defined(PROTO)
5268/*
5269 * Return TRUE if a file matches with a pattern in "list".
5270 * "list" is a comma-separated list of patterns, like 'wildignore'.
5271 * "sfname" is the short file name or NULL, "ffname" the long file name.
5272 */
5273 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 char_u buf[100];
5277 char_u *tail;
5278 char_u *regpat;
5279 char allow_dirs;
5280 int match;
5281 char_u *p;
5282
5283 tail = gettail(sfname);
5284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 p = list;
5287 while (*p)
5288 {
5289 copy_option_part(&p, buf, 100, ",");
5290 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5291 if (regpat == NULL)
5292 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005293 match = match_file_pat(regpat, NULL, ffname, sfname,
5294 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 vim_free(regpat);
5296 if (match)
5297 return TRUE;
5298 }
5299 return FALSE;
5300}
5301#endif
5302
5303/*
5304 * Convert the given pattern "pat" which has shell style wildcards in it, into
5305 * a regular expression, and return the result in allocated memory. If there
5306 * is a directory path separator to be matched, then TRUE is put in
5307 * allow_dirs, otherwise FALSE is put there -- webb.
5308 * Handle backslashes before special characters, like "\*" and "\ ".
5309 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * Returns NULL when out of memory.
5311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313file_pat_to_reg_pat(
5314 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 char_u *pat_end, // first char after pattern or NULL
5316 char *allow_dirs, // Result passed back out in here
5317 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 char_u *endp;
5321 char_u *reg_pat;
5322 char_u *p;
5323 int i;
5324 int nested = 0;
5325 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 if (allow_dirs != NULL)
5328 *allow_dirs = FALSE;
5329 if (pat_end == NULL)
5330 pat_end = pat + STRLEN(pat);
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 for (p = pat; p < pat_end; p++)
5333 {
5334 switch (*p)
5335 {
5336 case '*':
5337 case '.':
5338 case ',':
5339 case '{':
5340 case '}':
5341 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005342 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 break;
5344#ifdef BACKSLASH_IN_FILENAME
5345 case '\\':
5346 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005347 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 break;
5349#endif
5350 default:
5351 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005352 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 ++p;
5355 ++size;
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
5358 }
5359 }
5360 reg_pat = alloc(size + 1);
5361 if (reg_pat == NULL)
5362 return NULL;
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 if (pat[0] == '*')
5367 while (pat[0] == '*' && pat < pat_end - 1)
5368 pat++;
5369 else
5370 reg_pat[i++] = '^';
5371 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005372 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 while (endp - pat > 0 && *endp == '*')
5375 endp--;
5376 add_dollar = FALSE;
5377 }
5378 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5379 {
5380 switch (*p)
5381 {
5382 case '*':
5383 reg_pat[i++] = '.';
5384 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005386 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 break;
5388 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 case '~':
5390 reg_pat[i++] = '\\';
5391 reg_pat[i++] = *p;
5392 break;
5393 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 reg_pat[i++] = '.';
5395 break;
5396 case '\\':
5397 if (p[1] == NUL)
5398 break;
5399#ifdef BACKSLASH_IN_FILENAME
5400 if (!no_bslash)
5401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005402 // translate:
5403 // "\x" to "\\x" e.g., "dir\file"
5404 // "\*" to "\\.*" e.g., "dir\*.c"
5405 // "\?" to "\\." e.g., "dir\??.c"
5406 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5408 && p[1] != '+')
5409 {
5410 reg_pat[i++] = '[';
5411 reg_pat[i++] = '\\';
5412 reg_pat[i++] = '/';
5413 reg_pat[i++] = ']';
5414 if (allow_dirs != NULL)
5415 *allow_dirs = TRUE;
5416 break;
5417 }
5418 }
5419#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005420 // Undo escaping from ExpandEscape():
5421 // foo\?bar -> foo?bar
5422 // foo\%bar -> foo%bar
5423 // foo\,bar -> foo,bar
5424 // foo\ bar -> foo bar
5425 // Don't unescape \, * and others that are also special in a
5426 // regexp.
5427 // An escaped { must be unescaped since we use magic not
5428 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 if (*++p == '?'
5430#ifdef BACKSLASH_IN_FILENAME
5431 && no_bslash
5432#endif
5433 )
5434 reg_pat[i++] = '?';
5435 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005436 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005437 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005438 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005439 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5440 {
5441 reg_pat[i++] = '\\';
5442 reg_pat[i++] = '{';
5443 p += 2;
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 else
5446 {
5447 if (allow_dirs != NULL && vim_ispathsep(*p)
5448#ifdef BACKSLASH_IN_FILENAME
5449 && (!no_bslash || *p != '\\')
5450#endif
5451 )
5452 *allow_dirs = TRUE;
5453 reg_pat[i++] = '\\';
5454 reg_pat[i++] = *p;
5455 }
5456 break;
5457#ifdef BACKSLASH_IN_FILENAME
5458 case '/':
5459 reg_pat[i++] = '[';
5460 reg_pat[i++] = '\\';
5461 reg_pat[i++] = '/';
5462 reg_pat[i++] = ']';
5463 if (allow_dirs != NULL)
5464 *allow_dirs = TRUE;
5465 break;
5466#endif
5467 case '{':
5468 reg_pat[i++] = '\\';
5469 reg_pat[i++] = '(';
5470 nested++;
5471 break;
5472 case '}':
5473 reg_pat[i++] = '\\';
5474 reg_pat[i++] = ')';
5475 --nested;
5476 break;
5477 case ',':
5478 if (nested)
5479 {
5480 reg_pat[i++] = '\\';
5481 reg_pat[i++] = '|';
5482 }
5483 else
5484 reg_pat[i++] = ',';
5485 break;
5486 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005487 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005489 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 *allow_dirs = TRUE;
5491 reg_pat[i++] = *p;
5492 break;
5493 }
5494 }
5495 if (add_dollar)
5496 reg_pat[i++] = '$';
5497 reg_pat[i] = NUL;
5498 if (nested != 0)
5499 {
5500 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005501 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005503 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005504 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506 return reg_pat;
5507}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005508
5509#if defined(EINTR) || defined(PROTO)
5510/*
5511 * Version of read() that retries when interrupted by EINTR (possibly
5512 * by a SIGWINCH).
5513 */
5514 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005516{
5517 long ret;
5518
5519 for (;;)
5520 {
5521 ret = vim_read(fd, buf, bufsize);
5522 if (ret >= 0 || errno != EINTR)
5523 break;
5524 }
5525 return ret;
5526}
5527
5528/*
5529 * Version of write() that retries when interrupted by EINTR (possibly
5530 * by a SIGWINCH).
5531 */
5532 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005534{
5535 long ret = 0;
5536 long wlen;
5537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005538 // Repeat the write() so long it didn't fail, other than being interrupted
5539 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005540 while (ret < (long)bufsize)
5541 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005542 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005543 if (wlen < 0)
5544 {
5545 if (errno != EINTR)
5546 break;
5547 }
5548 else
5549 ret += wlen;
5550 }
5551 return ret;
5552}
5553#endif