blob: 81a502665252664cf7b70b13e5d21599cc539470 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaarf4888d02009-12-02 12:31:27 +000016#if defined(__TANDEM) || defined(__MINT__)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020019#if defined(UNIX) && defined(FEAT_EVAL)
20# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar217e1b82019-12-01 21:41:28 +010024// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000025#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf077db22019-08-13 00:18:24 +020027static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010028#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010029static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020032static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010035static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000036static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000037
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020038#ifdef FEAT_EVAL
39static int readdirex_sort;
40#endif
41
Bram Moolenaar473952e2019-09-28 16:30:04 +020042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010043filemess(
44 buf_T *buf,
45 char_u *name,
46 char_u *s,
47 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
49 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020050 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52 if (msg_silent != 0)
53 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010054 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020055
Bram Moolenaar217e1b82019-12-01 21:41:28 +010056 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020057 if (STRLEN(IObuff) > IOSIZE - 100)
58 IObuff[IOSIZE - 100] = NUL;
59
60 // Avoid an over-long translation to cause trouble.
61 STRNCAT(IObuff, s, 99);
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 /*
64 * For the first message may have to start a new line.
65 * For further ones overwrite the previous one, reset msg_scroll before
66 * calling filemess().
67 */
68 msg_scroll_save = msg_scroll;
69 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
70 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010071 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 check_for_delay(FALSE);
73 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020074 if (prev_msg_col != 0 && msg_col == 0)
75 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 msg_scroll = msg_scroll_save;
77 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010078 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
80 msg_clr_eos();
81 out_flush();
82 msg_scrolled_ign = FALSE;
83}
84
85/*
86 * Read lines from file "fname" into the buffer after line "from".
87 *
88 * 1. We allocate blocks with lalloc, as big as possible.
89 * 2. Each block is filled with characters from the file with a single read().
90 * 3. The lines are inserted in the buffer with ml_append().
91 *
92 * (caller must check that fname != NULL, unless READ_STDIN is used)
93 *
94 * "lines_to_skip" is the number of lines that must be skipped
95 * "lines_to_read" is the number of lines that are appended
96 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
97 *
98 * flags:
99 * READ_NEW starting to edit a new buffer
100 * READ_FILTER reading filter output
101 * READ_STDIN read from stdin instead of a file
102 * READ_BUFFER read from curbuf instead of a file (converting after reading
103 * stdin)
104 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200105 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200106 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100108 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 */
110 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100111readfile(
112 char_u *fname,
113 char_u *sfname,
114 linenr_T from,
115 linenr_T lines_to_skip,
116 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100117 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100118 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 int fd = 0;
121 int newfile = (flags & READ_NEW);
122 int check_readonly;
123 int filtering = (flags & READ_FILTER);
124 int read_stdin = (flags & READ_STDIN);
125 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000127 int set_options = newfile || read_buffer
128 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100129 linenr_T read_buf_lnum = 1; // next line to read from curbuf
130 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 char_u c;
132 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100133 char_u *ptr = NULL; // pointer into read buffer
134 char_u *buffer = NULL; // read buffer
135 char_u *new_buffer = NULL; // init to shut up gcc
136 char_u *line_start = NULL; // init to shut up gcc
137 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 colnr_T len;
139 long size = 0;
140 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200141 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 int skip_read = FALSE;
143#ifdef FEAT_CRYPT
144 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200145 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200147#ifdef FEAT_PERSISTENT_UNDO
148 context_sha256_T sha_ctx;
149 int read_undo_file = FALSE;
150#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100151 int split = 0; // number of split lines
152#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100154 int error = FALSE; // errors encountered
155 int ff_error = EOL_UNKNOWN; // file format with errors
156 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef UNIX
158 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100159 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#else
161 int perm;
162#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100163 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200165 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 int file_readonly;
167 linenr_T skip_count = 0;
168 linenr_T read_count = 0;
169 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100170 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
171 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100172 int try_mac;
173 int try_dos;
174 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100177 linenr_T conv_error = 0; // line nr with conversion error
178 linenr_T illegal_byte = 0; // line nr with illegal byte
179 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
180 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000181 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100182 // BAD_KEEP, BAD_DROP or character to
183 // replace with
184 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 char_u *fenc; // fileencoding to use
187 int fenc_alloced; // fenc_next is in allocated memory
188 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 int advance_fenc = FALSE;
190 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100191#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100192 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100193# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 int did_iconv = FALSE; // TRUE when iconv() failed and trying
195 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100197#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100198 int converted = FALSE; // TRUE if conversion done
199 int notconverted = FALSE; // TRUE if conversion wanted but it
200 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100202 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100203 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200204 buf_T *old_curbuf;
205 char_u *old_b_ffname;
206 char_u *old_b_fname;
207 int using_b_ffname;
208 int using_b_fname;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200209
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100210 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200211
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100212 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214 /*
215 * If there is no file name yet, use the one for the read file.
216 * BF_NOTEDITED is set to reflect this.
217 * Don't do this for a read from a filter.
218 * Only do this when 'cpoptions' contains the 'f' flag.
219 */
220 if (curbuf->b_ffname == NULL
221 && !filtering
222 && fname != NULL
223 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
224 && !(flags & READ_DUMMY))
225 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000226 if (set_rw_fname(fname, sfname) == FAIL)
227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100230 // Remember the initial values of curbuf, curbuf->b_ffname and
231 // curbuf->b_fname to detect whether they are altered as a result of
232 // executing nasty autocommands. Also check if "fname" and "sfname"
233 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200234 old_curbuf = curbuf;
235 old_b_ffname = curbuf->b_ffname;
236 old_b_fname = curbuf->b_fname;
237 using_b_ffname = (fname == curbuf->b_ffname)
238 || (sfname == curbuf->b_ffname);
239 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200240
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100241 // After reading a file the cursor line changes but we don't want to
242 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000243 ex_no_reprint = TRUE;
244
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100245 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000246 need_fileinfo = FALSE;
247
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 /*
249 * For Unix: Use the short file name whenever possible.
250 * Avoids problems with networks and when directory names are changed.
251 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
252 * another directory, which we don't detect.
253 */
254 if (sfname == NULL)
255 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200256#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 fname = sfname;
258#endif
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 /*
261 * The BufReadCmd and FileReadCmd events intercept the reading process by
262 * executing the associated commands instead.
263 */
264 if (!filtering && !read_stdin && !read_buffer)
265 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100266 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100268 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
270 curbuf->b_op_start.col = 0;
271
272 if (newfile)
273 {
274 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
275 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200276 {
277 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200279 if (aborting())
280 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200282 // The BufReadCmd code usually uses ":read" to get the text and
283 // perhaps ":file" to change the buffer name. But we should
284 // consider this to work like ":edit", thus reset the
285 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
286 // same file.
287 if (status == OK)
288 curbuf->b_flags &= ~BF_NOTEDITED;
289 return status;
290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
293 FALSE, NULL, eap))
294#ifdef FEAT_EVAL
295 return aborting() ? FAIL : OK;
296#else
297 return OK;
298#endif
299
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100300 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
303 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100304 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100306 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
308 /*
309 * If the name ends in a path separator, we can't open it. Check here,
310 * because reading the file may actually work, but then creating the swap
311 * file may destroy it! Reported on MS-DOS and Win 95.
312 * If the name is too long we might crash further on, quit here.
313 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000314 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000316 p = fname + STRLEN(fname);
317 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000318 {
319 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
320 msg_end();
321 msg_scroll = msg_save;
322 return FAIL;
323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200326 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200328#ifdef UNIX
329 /*
330 * On Unix it is possible to read a directory, so we have to
331 * check for it before the mch_open().
332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100334 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
335 && !S_ISFIFO(perm) // ... or fifo
336 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000337# ifdef OPEN_CHR_FILES
338 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100339 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 )
342 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100343 int retval = FAIL;
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100348 retval = NOTDONE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 else
351 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
352 msg_end();
353 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100354 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200356#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100357#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000358 /*
359 * MS-Windows allows opening a device, but we will probably get stuck
360 * trying to read it.
361 */
362 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
363 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000364 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000365 msg_end();
366 msg_scroll = msg_save;
367 return FAIL;
368 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000369#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200370 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000371
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100372 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200373 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 /*
376 * When opening a new file we take the readonly flag from the file.
377 * Default is r/w, can be set to r/o below.
378 * Don't reset it when in readonly mode
379 * Only set/reset b_p_ro when BF_CHECK_RO is set.
380 */
381 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000382 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 curbuf->b_p_ro = FALSE;
384
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200385 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100387 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (mch_stat((char *)fname, &st) >= 0)
389 {
390 buf_store_time(curbuf, &st, fname);
391 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef UNIX
393 /*
394 * Use the protection bits of the original file for the swap file.
395 * This makes it possible for others to read the name of the
396 * edited file from the swapfile, but only if they can read the
397 * edited file.
398 * Remove the "write" and "execute" bits for group and others
399 * (they must not write the swapfile).
400 * Add the "read" and "write" bits for the user, otherwise we may
401 * not be able to write to the file ourselves.
402 * Setting the bits is done below, after creating the swap file.
403 */
404 swap_mode = (st.st_mode & 0644) | 0600;
405#endif
406#ifdef FEAT_CW_EDITOR
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100407 // Get the FSSpec on MacOS
408 // TODO: Update it properly when the buffer name changes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
410#endif
411#ifdef VMS
412 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000413 curbuf->b_fab_rat = st.st_fab_rat;
414 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415#endif
416 }
417 else
418 {
419 curbuf->b_mtime = 0;
420 curbuf->b_mtime_read = 0;
421 curbuf->b_orig_size = 0;
422 curbuf->b_orig_mode = 0;
423 }
424
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100425 // Reset the "new file" flag. It will be set again below when the
426 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
428 }
429
430/*
431 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100432 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 */
434 file_readonly = FALSE;
435 if (read_stdin)
436 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100437#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100438 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 setmode(0, O_BINARY);
440#endif
441 }
442 else if (!read_buffer)
443 {
444#ifdef USE_MCH_ACCESS
445 if (
446# ifdef UNIX
447 !(perm & 0222) ||
448# endif
449 mch_access((char *)fname, W_OK))
450 file_readonly = TRUE;
451 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
452#else
453 if (!newfile
454 || readonlymode
455 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
456 {
457 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100458 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
460 }
461#endif
462 }
463
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100464 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {
466#ifndef UNIX
467 int isdir_f;
468#endif
469 msg_scroll = msg_save;
470#ifndef UNIX
471 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100472 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 */
474 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (isdir_f)
477 {
478 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100479 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 }
481 else
482#endif
483 if (newfile)
484 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200485 if (perm < 0
486#ifdef ENOENT
487 && errno == ENOENT
488#endif
489 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {
491 /*
492 * Set the 'new-file' flag, so that when the file has
493 * been created by someone else, a ":w" will complain.
494 */
495 curbuf->b_flags |= BF_NEW;
496
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // Create a swap file now, so that other Vims are warned
498 // that we are editing this file. Don't do this for a
499 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500#ifdef FEAT_QUICKFIX
501 if (!bt_dontwrite(curbuf))
502#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000506 if (curbuf != old_curbuf
507 || (using_b_ffname
508 && (old_b_ffname != curbuf->b_ffname))
509 || (using_b_fname
510 && (old_b_fname != curbuf->b_fname)))
511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100512 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000513 return FAIL;
514 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000515 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000516 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200517 filemess(curbuf, sfname,
518 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000519 else
520 filemess(curbuf, sfname,
521 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 // Even though this is a new file, it might have been
524 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 check_marks_read();
526#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100527 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200528 if (eap != NULL)
529 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
531 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 save_file_ff(curbuf);
534
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100535#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100536 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 return FAIL;
538#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100539 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 else
542 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000543 filemess(curbuf, sfname, (char_u *)(
544# ifdef EFBIG
545 (errno == EFBIG) ? _("[File too big]") :
546# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200547# ifdef EOVERFLOW
548 (errno == EOVERFLOW) ? _("[File too big]") :
549# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000550 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100551 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553 }
554
555 return FAIL;
556 }
557
558 /*
559 * Only set the 'ro' flag for readonly files the first time they are
560 * loaded. Help files always get readonly mode
561 */
562 if ((check_readonly && file_readonly) || curbuf->b_help)
563 curbuf->b_p_ro = TRUE;
564
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000565 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100567 // Don't change 'eol' if reading from buffer as it will already be
568 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000569 if (!read_buffer)
570 {
571 curbuf->b_p_eol = TRUE;
572 curbuf->b_start_eol = TRUE;
573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000575 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100578 // Create a swap file now, so that other Vims are warned that we are
579 // editing this file.
580 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#ifdef FEAT_QUICKFIX
582 if (!bt_dontwrite(curbuf))
583#endif
584 {
585 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000586 if (!read_stdin && (curbuf != old_curbuf
587 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
588 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100590 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000591 if (!read_buffer)
592 close(fd);
593 return FAIL;
594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100596 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000597 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
598 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100599 {
600 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
601
602 /*
603 * If the group-read bit is set but not the world-read bit, then
604 * the group must be equal to the group of the original file. If
605 * we can't make that happen then reset the group-read bit. This
606 * avoids making the swap file readable to more users when the
607 * primary group of the user is too permissive.
608 */
609 if ((swap_mode & 044) == 040)
610 {
611 stat_T swap_st;
612
613 if (mch_stat((char *)swap_fname, &swap_st) >= 0
614 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200615# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100616 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200617 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200618# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200619 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100620 swap_mode &= 0600;
621 }
622
623 (void)mch_setperm(swap_fname, (long)swap_mode);
624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#endif
626 }
627
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200628 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (swap_exists_action == SEA_QUIT)
630 {
631 if (!read_buffer && !read_stdin)
632 close(fd);
633 return FAIL;
634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100636 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 /*
639 * Set '[ mark to the line above where the lines go (line 1 if zero).
640 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100641 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
643 curbuf->b_op_start.col = 0;
644
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100645 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
646 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
647 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (!read_buffer)
650 {
651 int m = msg_scroll;
652 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
654 /*
655 * The file must be closed again, the autocommands may want to change
656 * the file before reading it.
657 */
658 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100659 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 /*
662 * The output from the autocommands should not overwrite anything and
663 * should not be overwritten: Set msg_scroll, restore its value if no
664 * output was done.
665 */
666 msg_scroll = TRUE;
667 if (filtering)
668 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
669 FALSE, curbuf, eap);
670 else if (read_stdin)
671 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
672 FALSE, curbuf, eap);
673 else if (newfile)
674 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
675 FALSE, curbuf, eap);
676 else
677 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
678 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100680 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
681 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
682 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100683 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (msg_scrolled == n)
686 msg_scroll = m;
687
688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
691 --no_wait_return;
692 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100693 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 return FAIL;
695 }
696#endif
697 /*
698 * Don't allow the autocommands to change the current buffer.
699 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000700 *
701 * Don't allow the autocommands to change the buffer name either
702 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 */
704 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000705 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
706 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
708 {
709 --no_wait_return;
710 msg_scroll = msg_save;
711 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100712 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100714 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100715 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 return FAIL;
717 }
718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100720 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
722
723 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
724 {
725 /*
726 * Show the user that we are busy reading the input. Sometimes this
727 * may take a while. When reading from stdin another program may
728 * still be running, don't move the cursor to the last line, unless
729 * always using the GUI.
730 */
731 if (read_stdin)
732 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100733 if (!is_not_a_term())
734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200736# ifdef VIMDLL
737 if (!gui.in_use)
738# endif
739 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#endif
741#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100742 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100743 if (gui.in_use && !gui.dying && !gui.starting)
744 {
745 p = (char_u *)_("Reading from stdin...");
746 gui_write(p, (int)STRLEN(p));
747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751 else if (!read_buffer)
752 filemess(curbuf, sfname, (char_u *)"", 0);
753 }
754
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100755 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757 /*
758 * Set linecnt now, before the "retry" caused by a wrong guess for
759 * fileformat, and after the autocommands, which may change them.
760 */
761 linecnt = curbuf->b_ml.ml_line_count;
762
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100763 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000764 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000765 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000766 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000767 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000768 curbuf->b_bad_char = eap->bad_char;
769 }
770 else
771 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000774 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 */
776 if (eap != NULL && eap->force_enc != 0)
777 {
778 fenc = enc_canonize(eap->cmd + eap->force_enc);
779 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000780 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 else if (curbuf->b_p_bin)
783 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100784 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 fenc_alloced = FALSE;
786 }
787 else if (curbuf->b_help)
788 {
789 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000790 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Help files are either utf-8 or latin1. Try utf-8 first, if this
793 // fails it must be latin1.
794 // Always do this when 'encoding' is "utf-8". Otherwise only do
795 // this when needed to avoid [converted] remarks all the time.
796 // It is needed when the first line contains non-ASCII characters.
797 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 fenc = (char_u *)"latin1";
799 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000800 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000802 fc = fname[STRLEN(fname) - 1];
803 if (TOLOWER_ASC(fc) == 'x')
804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // Read the first line (and a bit more). Immediately rewind to
806 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100807 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200808 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000809 for (p = firstline; p < firstline + len; ++p)
810 if (*p >= 0x80)
811 {
812 c = TRUE;
813 break;
814 }
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817
818 if (c)
819 {
820 fenc_next = fenc;
821 fenc = (char_u *)"utf-8";
822
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 // When the file is utf-8 but a character doesn't fit in
824 // 'encoding' don't retry. In help text editing utf-8 bytes
825 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000826 if (!enc_utf8)
827 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 }
829 fenc_alloced = FALSE;
830 }
831 else if (*p_fencs == NUL)
832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 fenc_alloced = FALSE;
835 }
836 else
837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200839 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 /*
843 * Jump back here to retry reading the file in different ways.
844 * Reasons to retry:
845 * - encoding conversion failed: try another one from "fenc_next"
846 * - BOM detected and fenc was set, need to setup conversion
847 * - "fileformat" check failed: try another
848 *
849 * Variables set for special retry actions:
850 * "file_rewind" Rewind the file to start reading it again.
851 * "advance_fenc" Advance "fenc" using "fenc_next".
852 * "skip_read" Re-use already read bytes (BOM detected).
853 * "did_iconv" iconv() conversion failed, try 'charconvert'.
854 * "keep_fileformat" Don't reset "fileformat".
855 *
856 * Other status indicators:
857 * "tmpname" When != NULL did conversion with 'charconvert'.
858 * Output file has to be deleted afterwards.
859 * "iconv_fd" When != -1 did conversion with iconv().
860 */
861retry:
862
863 if (file_rewind)
864 {
865 if (read_buffer)
866 {
867 read_buf_lnum = 1;
868 read_buf_col = 0;
869 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200870 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100872 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 error = TRUE;
874 goto failed;
875 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100876 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200878 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000880 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000883 curbuf->b_start_bomb = FALSE;
884 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000885 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887
888 /*
889 * When retrying with another "fenc" and the first time "fileformat"
890 * will be reset.
891 */
892 if (keep_fileformat)
893 keep_fileformat = FALSE;
894 else
895 {
896 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000899 try_unix = try_dos = try_mac = FALSE;
900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100902 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100904 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100906 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
908
Bram Moolenaar13505972019-01-24 15:04:48 +0100909#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (iconv_fd != (iconv_t)-1)
911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100912 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 iconv_close(iconv_fd);
914 iconv_fd = (iconv_t)-1;
915 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 if (advance_fenc)
919 {
920 /*
921 * Try the next entry in 'fileencodings'.
922 */
923 advance_fenc = FALSE;
924
925 if (eap != NULL && eap->force_enc != 0)
926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100927 // Conversion given with "++cc=" wasn't possible, read
928 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000930 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (fenc_alloced)
932 vim_free(fenc);
933 fenc = (char_u *)"";
934 fenc_alloced = FALSE;
935 }
936 else
937 {
938 if (fenc_alloced)
939 vim_free(fenc);
940 if (fenc_next != NULL)
941 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200942 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944 else
945 {
946 fenc = (char_u *)"";
947 fenc_alloced = FALSE;
948 }
949 }
950 if (tmpname != NULL)
951 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100952 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100953 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
955 }
956
957 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000958 * Conversion may be required when the encoding of the file is different
959 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 */
961 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000962 converted = need_conversion(fenc);
963 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
965
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 // "ucs-bom" means we need to check the first bytes of the file
967 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (STRCMP(fenc, ENC_UCSBOM) == 0)
969 fio_flags = FIO_UCSBOM;
970
971 /*
972 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
973 * done. This is handled below after read(). Prepare the
974 * fio_flags to avoid having to parse the string each time.
975 * Also check for Unicode to Latin1 conversion, because iconv()
976 * appears not to handle this correctly. This works just like
977 * conversion to UTF-8 except how the resulting character is put in
978 * the buffer.
979 */
980 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
981 fio_flags = get_fio_flags(fenc);
982
Bram Moolenaar4f974752019-02-17 17:44:42 +0100983#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 /*
985 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
986 * is handled with MultiByteToWideChar().
987 */
988 if (fio_flags == 0)
989 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaar13505972019-01-24 15:04:48 +0100992#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100993 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (fio_flags == 0)
995 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar13505972019-01-24 15:04:48 +0100998#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * Try using iconv() if we can't convert internally.
1001 */
1002 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001003# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 )
1007 iconv_fd = (iconv_t)my_iconv_open(
1008 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar13505972019-01-24 15:04:48 +01001011#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 /*
1013 * Use the 'charconvert' expression when conversion is required
1014 * and we can't do it internally or with iconv().
1015 */
1016 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001017 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001018# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 )
1022 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001023# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001025# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001026 // Skip conversion when it's already done (retry for wrong
1027 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (tmpname == NULL)
1029 {
1030 tmpname = readfile_charconvert(fname, fenc, &fd);
1031 if (tmpname == NULL)
1032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 advance_fenc = TRUE;
1035 if (fd < 0)
1036 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001037 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001038 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 error = TRUE;
1040 goto failed;
1041 }
1042 goto retry;
1043 }
1044 }
1045 }
1046 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001050#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 )
1054 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Conversion wanted but we can't.
1056 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 advance_fenc = TRUE;
1058 goto retry;
1059 }
1060 }
1061 }
1062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Set "can_retry" when it's possible to rewind the file and try with
1064 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1065 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001066 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 if (!skip_read)
1069 {
1070 linerest = 0;
1071 filesize = 0;
1072 skip_count = lines_to_skip;
1073 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001075#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001076 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1077 && curbuf->b_ffname != NULL
1078 && curbuf->b_p_udf
1079 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001080 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001081 && !read_stdin
1082 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001083 if (read_undo_file)
1084 sha256_start(&sha_ctx);
1085#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001086#ifdef FEAT_CRYPT
1087 if (curbuf->b_cryptstate != NULL)
1088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001089 // Need to free the state, but keep the key, don't want to ask for
1090 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001091 crypt_free_state(curbuf->b_cryptstate);
1092 curbuf->b_cryptstate = NULL;
1093 }
1094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096
1097 while (!error && !got_int)
1098 {
1099 /*
1100 * We allocate as much space for the file as we can get, plus
1101 * space for the old line plus room for one terminating NUL.
1102 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001103 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001105 if (!skip_read)
1106 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001107#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001108 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001109#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // Use buffer >= 64K. Add linerest to double the size if the
1111 // line gets very long, to avoid a lot of copying. But don't
1112 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001113 size = 0x10000L + linerest;
1114 if (size > 0x100000L)
1115 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001116#endif
1117 }
1118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001120 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001123 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 size = 1;
1125 }
1126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
1128 if (!skip_read)
1129 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001130 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001132 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 FALSE)) != NULL)
1134 break;
1135 }
1136 if (new_buffer == NULL)
1137 {
1138 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1139 error = TRUE;
1140 break;
1141 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1144 vim_free(buffer);
1145 buffer = new_buffer;
1146 ptr = buffer + linerest;
1147 line_start = buffer;
1148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001149 // May need room to translate into.
1150 // For iconv() we don't really know the required space, use a
1151 // factor ICONV_MULT.
1152 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1153 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1154 // become up to 4 bytes, size must be multiple of 2
1155 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1156 // multiple of 2
1157 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1158 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001159 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001160#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (iconv_fd != (iconv_t)-1)
1162 size = size / ICONV_MULT;
1163 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (fio_flags & FIO_LATIN1)
1166 size = size / 2;
1167 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1168 size = (size * 2 / 3) & ~1;
1169 else if (fio_flags & FIO_UCS4)
1170 size = (size * 2 / 3) & ~3;
1171 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001172 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001173#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001176#endif
1177#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (conv_restlen > 0)
1183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001184 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 mch_memmove(ptr, conv_rest, conv_restlen);
1186 ptr += conv_restlen;
1187 size -= conv_restlen;
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
1190 if (read_buffer)
1191 {
1192 /*
1193 * Read bytes from curbuf. Used for converting text read
1194 * from stdin.
1195 */
1196 if (read_buf_lnum > from)
1197 size = 0;
1198 else
1199 {
1200 int n, ni;
1201 long tlen;
1202
1203 tlen = 0;
1204 for (;;)
1205 {
1206 p = ml_get(read_buf_lnum) + read_buf_col;
1207 n = (int)STRLEN(p);
1208 if ((int)tlen + n + 1 > size)
1209 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001210 // Filled up to "size", append partial line.
1211 // Change NL to NUL to reverse the effect done
1212 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001213 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 for (ni = 0; ni < n; ++ni)
1215 {
1216 if (p[ni] == NL)
1217 ptr[tlen++] = NUL;
1218 else
1219 ptr[tlen++] = p[ni];
1220 }
1221 read_buf_col += n;
1222 break;
1223 }
1224 else
1225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // Append whole line and new-line. Change NL
1227 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 for (ni = 0; ni < n; ++ni)
1229 {
1230 if (p[ni] == NL)
1231 ptr[tlen++] = NUL;
1232 else
1233 ptr[tlen++] = p[ni];
1234 }
1235 ptr[tlen++] = NL;
1236 read_buf_col = 0;
1237 if (++read_buf_lnum > from)
1238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001239 // When the last line didn't have an
1240 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (!curbuf->b_p_eol)
1242 --tlen;
1243 size = tlen;
1244 break;
1245 }
1246 }
1247 }
1248 }
1249 }
1250 else
1251 {
1252 /*
1253 * Read bytes from the file.
1254 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001255 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001258#ifdef FEAT_CRYPT
1259 /*
1260 * At start of file: Check for magic number of encryption.
1261 */
1262 if (filesize == 0 && size > 0)
1263 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1264 &filesize, newfile, sfname,
1265 &did_ask_for_key);
1266 /*
1267 * Decrypt the read bytes. This is done before checking for
1268 * EOF because the crypt layer may be buffering.
1269 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001270 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1271 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001272 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001273# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001274 if (crypt_works_inplace(curbuf->b_cryptstate))
1275 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001276# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001277 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001278# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001279 }
1280 else
1281 {
1282 char_u *newptr = NULL;
1283 int decrypted_size;
1284
1285 decrypted_size = crypt_decode_alloc(
1286 curbuf->b_cryptstate, ptr, size, &newptr);
1287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001288 // If the crypt layer is buffering, not producing
1289 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001290 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001291 continue;
1292
1293 if (linerest == 0)
1294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001295 // Simple case: reuse returned buffer (may be
1296 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001297 new_buffer = newptr;
1298 }
1299 else
1300 {
1301 long_u new_size;
1302
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001303 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001304 new_size = (long_u)(decrypted_size + linerest + 1);
1305 new_buffer = lalloc(new_size, FALSE);
1306 if (new_buffer == NULL)
1307 {
1308 do_outofmem_msg(new_size);
1309 error = TRUE;
1310 break;
1311 }
1312
1313 mch_memmove(new_buffer, buffer, linerest);
1314 if (newptr != NULL)
1315 mch_memmove(new_buffer + linerest, newptr,
1316 decrypted_size);
1317 }
1318
1319 if (new_buffer != NULL)
1320 {
1321 vim_free(buffer);
1322 buffer = new_buffer;
1323 new_buffer = NULL;
1324 line_start = buffer;
1325 ptr = buffer + linerest;
1326 }
1327 size = decrypted_size;
1328 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001329# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001330 }
1331#endif
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (size <= 0)
1334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001335 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001338 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001339 /*
1340 * Reached end-of-file but some trailing bytes could
1341 * not be converted. Truncated file?
1342 */
1343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001344 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001345 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001346#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001347 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001348#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001349 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001350 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001351 if (can_retry)
1352 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001353 if (conv_error == 0)
1354 conv_error = curbuf->b_ml.ml_line_count
1355 - linecnt + 1;
1356 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001357 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001358 else if (illegal_byte == 0)
1359 illegal_byte = curbuf->b_ml.ml_line_count
1360 - linecnt + 1;
1361 if (bad_char_behavior == BAD_DROP)
1362 {
1363 *(ptr - conv_restlen) = NUL;
1364 conv_restlen = 0;
1365 }
1366 else
1367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001368 // Replace the trailing bytes with the replacement
1369 // character if we were converting; if we weren't,
1370 // leave the UTF8 checking code to do it, as it
1371 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001372 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001373#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001374 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001375#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001376 ))
1377 {
1378 while (conv_restlen > 0)
1379 {
1380 *(--ptr) = bad_char_behavior;
1381 --conv_restlen;
1382 }
1383 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001384 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001385#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001386 if (iconv_fd != (iconv_t)-1)
1387 {
1388 iconv_close(iconv_fd);
1389 iconv_fd = (iconv_t)-1;
1390 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001391#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001392 }
1393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 skip_read = FALSE;
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /*
1399 * At start of file (or after crypt magic number): Check for BOM.
1400 * Also check for a BOM for other Unicode encodings, but not after
1401 * converting with 'charconvert' or when a BOM has already been
1402 * found.
1403 */
1404 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001405#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001406 || (cryptkey != NULL
1407 && filesize == crypt_get_header_len(
1408 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 )
1411 && (fio_flags == FIO_UCSBOM
1412 || (!curbuf->b_p_bomb
1413 && tmpname == NULL
1414 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1415 {
1416 char_u *ccname;
1417 int blen;
1418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001419 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (size < 2 || curbuf->b_p_bin)
1421 ccname = NULL;
1422 else
1423 ccname = check_for_bom(ptr, size, &blen,
1424 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1425 if (ccname != NULL)
1426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001427 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 filesize += blen;
1429 size -= blen;
1430 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001431 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001434 curbuf->b_start_bomb = TRUE;
1435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437
1438 if (fio_flags == FIO_UCSBOM)
1439 {
1440 if (ccname == NULL)
1441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001442 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 advance_fenc = TRUE;
1444 }
1445 else
1446 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001447 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (fenc_alloced)
1449 vim_free(fenc);
1450 fenc = ccname;
1451 fenc_alloced = FALSE;
1452 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001453 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 skip_read = TRUE;
1455 goto retry;
1456 }
1457 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001459 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001460 ptr -= conv_restlen;
1461 size += conv_restlen;
1462 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 /*
1464 * Break here for a read error or end-of-file.
1465 */
1466 if (size <= 0)
1467 break;
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
Bram Moolenaar13505972019-01-24 15:04:48 +01001470#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (iconv_fd != (iconv_t)-1)
1472 {
1473 /*
1474 * Attempt conversion of the read bytes to 'encoding' using
1475 * iconv().
1476 */
1477 const char *fromp;
1478 char *top;
1479 size_t from_size;
1480 size_t to_size;
1481
1482 fromp = (char *)ptr;
1483 from_size = size;
1484 ptr += size;
1485 top = (char *)ptr;
1486 to_size = real_size - size;
1487
1488 /*
1489 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001490 * another conversion. Except for when there is no
1491 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001493 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1494 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1496 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001497 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001498 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001499 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001500 if (conv_error == 0)
1501 conv_error = readfile_linenr(linecnt,
1502 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001505 ++fromp;
1506 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001507 if (bad_char_behavior == BAD_KEEP)
1508 {
1509 *top++ = *(fromp - 1);
1510 --to_size;
1511 }
1512 else if (bad_char_behavior != BAD_DROP)
1513 {
1514 *top++ = bad_char_behavior;
1515 --to_size;
1516 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
1519 if (from_size > 0)
1520 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 // Some remaining characters, keep them for the next
1522 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1524 conv_restlen = (int)from_size;
1525 }
1526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 line_start = ptr - linerest;
1529 mch_memmove(line_start, buffer, (size_t)linerest);
1530 size = (long)((char_u *)top - ptr);
1531 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar4f974752019-02-17 17:44:42 +01001534#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (fio_flags & FIO_CODEPAGE)
1536 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001537 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001538 WCHAR ucs2buf[3];
1539 int ucs2len;
1540 int codepage = FIO_GET_CP(fio_flags);
1541 int bytelen;
1542 int found_bad;
1543 char replstr[2];
1544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 /*
1546 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001547 * a codepage, using standard MS-Windows functions. This
1548 * requires two steps:
1549 * 1. convert from 'fileencoding' to ucs-2
1550 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001552 * Because there may be illegal bytes AND an incomplete byte
1553 * sequence at the end, we may have to do the conversion one
1554 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001558 if (bad_char_behavior > 0)
1559 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001561 replstr[0] = '?';
1562 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
1564 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001565 * Move the bytes to the end of the buffer, so that we have
1566 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001568 src = ptr + real_size - size;
1569 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001571 /*
1572 * Do the conversion.
1573 */
1574 dst = ptr;
1575 size = size;
1576 while (size > 0)
1577 {
1578 found_bad = FALSE;
1579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001580# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001581 if (codepage == CP_UTF8)
1582 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001583 // Handle CP_UTF8 input ourselves to be able to handle
1584 // trailing bytes properly.
1585 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001586 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001587 if (bytelen > size)
1588 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001589 // Only got some bytes of a character. Normally
1590 // it's put in "conv_rest", but if it's too long
1591 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001592 if (bytelen <= CONV_RESTLEN)
1593 break;
1594
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001595 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001596 bytelen = size;
1597 found_bad = TRUE;
1598 }
1599 else
1600 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001601 int u8c = utf_ptr2char(src);
1602
Bram Moolenaar86e01082005-12-29 22:45:34 +00001603 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001604 found_bad = TRUE;
1605 ucs2buf[0] = u8c;
1606 ucs2len = 1;
1607 }
1608 }
1609 else
1610# endif
1611 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001612 // We don't know how long the byte sequence is, try
1613 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001614 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1615 ++bytelen)
1616 {
1617 ucs2len = MultiByteToWideChar(codepage,
1618 MB_ERR_INVALID_CHARS,
1619 (LPCSTR)src, bytelen,
1620 ucs2buf, 3);
1621 if (ucs2len > 0)
1622 break;
1623 }
1624 if (ucs2len == 0)
1625 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001626 // If we have only one byte then it's probably an
1627 // incomplete byte sequence. Otherwise discard
1628 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001629 if (size == 1)
1630 break;
1631 found_bad = TRUE;
1632 bytelen = 1;
1633 }
1634 }
1635
1636 if (!found_bad)
1637 {
1638 int i;
1639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001641 if (enc_utf8)
1642 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001643 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001644 for (i = 0; i < ucs2len; ++i)
1645 dst += utf_char2bytes(ucs2buf[i], dst);
1646 }
1647 else
1648 {
1649 BOOL bad = FALSE;
1650 int dstlen;
1651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001652 // From UCS-2 to "enc_codepage". If the
1653 // conversion uses the default character "?",
1654 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001655 dstlen = WideCharToMultiByte(enc_codepage, 0,
1656 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001657 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001658 replstr, &bad);
1659 if (bad)
1660 found_bad = TRUE;
1661 else
1662 dst += dstlen;
1663 }
1664 }
1665
1666 if (found_bad)
1667 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001668 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 if (can_retry)
1670 goto rewind_retry;
1671 if (conv_error == 0)
1672 conv_error = readfile_linenr(linecnt, ptr, dst);
1673 if (bad_char_behavior != BAD_DROP)
1674 {
1675 if (bad_char_behavior == BAD_KEEP)
1676 {
1677 mch_memmove(dst, src, bytelen);
1678 dst += bytelen;
1679 }
1680 else
1681 *dst++ = bad_char_behavior;
1682 }
1683 }
1684
1685 src += bytelen;
1686 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001688
1689 if (size > 0)
1690 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001692 mch_memmove(conv_rest, src, size);
1693 conv_restlen = size;
1694 }
1695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001696 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001697 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001700#endif
1701#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if (fio_flags & FIO_MACROMAN)
1703 {
1704 /*
1705 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001706 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001708 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (fio_flags != 0)
1714 {
1715 int u8c;
1716 char_u *dest;
1717 char_u *tail = NULL;
1718
1719 /*
1720 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1721 * "enc_utf8" not set: Convert Unicode to Latin1.
1722 * Go from end to start through the buffer, because the number
1723 * of bytes may increase.
1724 * "dest" points to after where the UTF-8 bytes go, "p" points
1725 * to after the next character to convert.
1726 */
1727 dest = ptr + real_size;
1728 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1729 {
1730 p = ptr + size;
1731 if (fio_flags == FIO_UTF8)
1732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 tail = ptr + size - 1;
1735 while (tail > ptr && (*tail & 0xc0) == 0x80)
1736 --tail;
1737 if (tail + utf_byte2len(*tail) <= ptr + size)
1738 tail = NULL;
1739 else
1740 p = tail;
1741 }
1742 }
1743 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1744 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001745 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 p = ptr + (size & ~1);
1747 if (size & 1)
1748 tail = p;
1749 if ((fio_flags & FIO_UTF16) && p > ptr)
1750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (fio_flags & FIO_ENDIAN_L)
1753 {
1754 u8c = (*--p << 8);
1755 u8c += *--p;
1756 }
1757 else
1758 {
1759 u8c = *--p;
1760 u8c += (*--p << 8);
1761 }
1762 if (u8c >= 0xd800 && u8c <= 0xdbff)
1763 tail = p;
1764 else
1765 p += 2;
1766 }
1767 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001768 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 p = ptr + (size & ~3);
1772 if (size & 3)
1773 tail = p;
1774 }
1775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001776 // If there is a trailing incomplete sequence move it to
1777 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (tail != NULL)
1779 {
1780 conv_restlen = (int)((ptr + size) - tail);
1781 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1782 size -= conv_restlen;
1783 }
1784
1785
1786 while (p > ptr)
1787 {
1788 if (fio_flags & FIO_LATIN1)
1789 u8c = *--p;
1790 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1791 {
1792 if (fio_flags & FIO_ENDIAN_L)
1793 {
1794 u8c = (*--p << 8);
1795 u8c += *--p;
1796 }
1797 else
1798 {
1799 u8c = *--p;
1800 u8c += (*--p << 8);
1801 }
1802 if ((fio_flags & FIO_UTF16)
1803 && u8c >= 0xdc00 && u8c <= 0xdfff)
1804 {
1805 int u16c;
1806
1807 if (p == ptr)
1808 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001809 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (can_retry)
1811 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001812 if (conv_error == 0)
1813 conv_error = readfile_linenr(linecnt,
1814 ptr, p);
1815 if (bad_char_behavior == BAD_DROP)
1816 continue;
1817 if (bad_char_behavior != BAD_KEEP)
1818 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001821 // found second word of double-word, get the first
1822 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (fio_flags & FIO_ENDIAN_L)
1824 {
1825 u16c = (*--p << 8);
1826 u16c += *--p;
1827 }
1828 else
1829 {
1830 u16c = *--p;
1831 u16c += (*--p << 8);
1832 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001833 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1834 + (u8c & 0x3ff);
1835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (u16c < 0xd800 || u16c > 0xdbff)
1838 {
1839 if (can_retry)
1840 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001841 if (conv_error == 0)
1842 conv_error = readfile_linenr(linecnt,
1843 ptr, p);
1844 if (bad_char_behavior == BAD_DROP)
1845 continue;
1846 if (bad_char_behavior != BAD_KEEP)
1847 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 }
1851 else if (fio_flags & FIO_UCS4)
1852 {
1853 if (fio_flags & FIO_ENDIAN_L)
1854 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001855 u8c = (unsigned)*--p << 24;
1856 u8c += (unsigned)*--p << 16;
1857 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 u8c += *--p;
1859 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001860 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001863 u8c += (unsigned)*--p << 8;
1864 u8c += (unsigned)*--p << 16;
1865 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 if (*--p < 0x80)
1871 u8c = *p;
1872 else
1873 {
1874 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001875 p -= len;
1876 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (len == 0)
1878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001879 // Not a valid UTF-8 character, retry with
1880 // another fenc when possible, otherwise just
1881 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (can_retry)
1883 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001884 if (conv_error == 0)
1885 conv_error = readfile_linenr(linecnt,
1886 ptr, p);
1887 if (bad_char_behavior == BAD_DROP)
1888 continue;
1889 if (bad_char_behavior != BAD_KEEP)
1890 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 dest -= utf_char2len(u8c);
1897 (void)utf_char2bytes(u8c, dest);
1898 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001899 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 --dest;
1902 if (u8c >= 0x100)
1903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001904 // character doesn't fit in latin1, retry with
1905 // another fenc when possible, otherwise just
1906 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001907 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001909 if (conv_error == 0)
1910 conv_error = readfile_linenr(linecnt, ptr, p);
1911 if (bad_char_behavior == BAD_DROP)
1912 ++dest;
1913 else if (bad_char_behavior == BAD_KEEP)
1914 *dest = u8c;
1915 else if (eap != NULL && eap->bad_char != 0)
1916 *dest = bad_char_behavior;
1917 else
1918 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 else
1921 *dest = u8c;
1922 }
1923 }
1924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001925 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 line_start = dest - linerest;
1927 mch_memmove(line_start, buffer, (size_t)linerest);
1928 size = (long)((ptr + real_size) - dest);
1929 ptr = dest;
1930 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001931 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001933 int incomplete_tail = FALSE;
1934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001935 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001936 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001938 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001939 int l;
1940
1941 if (todo <= 0)
1942 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (*p >= 0x80)
1944 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001945 // A length of 1 means it's an illegal byte. Accept
1946 // an incomplete character at the end though, the next
1947 // read() will get the next bytes, we'll check it
1948 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001949 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001950 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 // Avoid retrying with a different encoding when
1953 // a truncated file is more likely, or attempting
1954 // to read the rest of an incomplete sequence when
1955 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001956 if (p > ptr || filesize > 0)
1957 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001958 // Incomplete byte sequence, move it to conv_rest[]
1959 // and try to read the rest of it, unless we've
1960 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001961 if (p > ptr)
1962 {
1963 conv_restlen = todo;
1964 mch_memmove(conv_rest, p, conv_restlen);
1965 size -= conv_restlen;
1966 break;
1967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001969 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // Illegal byte. If we can try another encoding
1972 // do that, unless at EOF where a truncated
1973 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001974 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001975 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001976#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001978 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1979 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001980#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001982 if (conv_error == 0 && illegal_byte == 0)
1983 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001986 if (bad_char_behavior == BAD_DROP)
1987 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001988 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001989 --p;
1990 --size;
1991 }
1992 else if (bad_char_behavior != BAD_KEEP)
1993 *p = bad_char_behavior;
1994 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001995 else
1996 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001999 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002003 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002004#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002005 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002006 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002007 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002009#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002010 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002011 advance_fenc = TRUE;
2012 file_rewind = TRUE;
2013 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 filesize += size;
2019
2020 /*
2021 * when reading the first part of a file: guess EOL type
2022 */
2023 if (fileformat == EOL_UNKNOWN)
2024 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002025 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (try_dos || try_unix)
2027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002029 if (try_mac)
2030 try_mac = 1;
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 for (p = ptr; p < ptr + size; ++p)
2033 {
2034 if (*p == NL)
2035 {
2036 if (!try_unix
2037 || (try_dos && p > ptr && p[-1] == CAR))
2038 fileformat = EOL_DOS;
2039 else
2040 fileformat = EOL_UNIX;
2041 break;
2042 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002043 else if (*p == CAR && try_mac)
2044 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002047 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (fileformat == EOL_UNIX && try_mac)
2049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002050 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 try_mac = 1;
2052 try_unix = 1;
2053 for (; p >= ptr && *p != CAR; p--)
2054 ;
2055 if (p >= ptr)
2056 {
2057 for (p = ptr; p < ptr + size; ++p)
2058 {
2059 if (*p == NL)
2060 try_unix++;
2061 else if (*p == CAR)
2062 try_mac++;
2063 }
2064 if (try_mac > try_unix)
2065 fileformat = EOL_MAC;
2066 }
2067 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002068 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // Looking for CR but found no end-of-line markers at
2070 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002071 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (fileformat == EOL_UNKNOWN && try_mac)
2076 fileformat = EOL_MAC;
2077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (fileformat == EOL_UNKNOWN)
2080 fileformat = default_fileformat();
2081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002083 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 set_fileformat(fileformat, OPT_LOCAL);
2085 }
2086 }
2087
2088 /*
2089 * This loop is executed once for every character read.
2090 * Keep it fast!
2091 */
2092 if (fileformat == EOL_MAC)
2093 {
2094 --ptr;
2095 while (++ptr, --size >= 0)
2096 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002097 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 if ((c = *ptr) != NUL && c != CAR && c != NL)
2099 continue;
2100 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002101 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 else
2105 {
2106 if (skip_count == 0)
2107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 len = (colnr_T) (ptr - line_start + 1);
2110 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2111 {
2112 error = TRUE;
2113 break;
2114 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002115#ifdef FEAT_PERSISTENT_UNDO
2116 if (read_undo_file)
2117 sha256_update(&sha_ctx, line_start, len);
2118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 ++lnum;
2120 if (--read_count == 0)
2121 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 error = TRUE; // break loop
2123 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 break;
2125 }
2126 }
2127 else
2128 --skip_count;
2129 line_start = ptr + 1;
2130 }
2131 }
2132 }
2133 else
2134 {
2135 --ptr;
2136 while (++ptr, --size >= 0)
2137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 continue;
2140 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002141 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 else
2143 {
2144 if (skip_count == 0)
2145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 len = (colnr_T)(ptr - line_start + 1);
2148 if (fileformat == EOL_DOS)
2149 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002150 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 ptr[-1] = NUL;
2154 --len;
2155 }
2156 /*
2157 * Reading in Dos format, but no CR-LF found!
2158 * When 'fileformats' includes "unix", delete all
2159 * the lines read so far and start all over again.
2160 * Otherwise give an error message later.
2161 */
2162 else if (ff_error != EOL_DOS)
2163 {
2164 if ( try_unix
2165 && !read_stdin
2166 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002167 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2168 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 set_fileformat(EOL_UNIX, OPT_LOCAL);
2173 file_rewind = TRUE;
2174 keep_fileformat = TRUE;
2175 goto retry;
2176 }
2177 ff_error = EOL_DOS;
2178 }
2179 }
2180 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2181 {
2182 error = TRUE;
2183 break;
2184 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002185#ifdef FEAT_PERSISTENT_UNDO
2186 if (read_undo_file)
2187 sha256_update(&sha_ctx, line_start, len);
2188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ++lnum;
2190 if (--read_count == 0)
2191 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002192 error = TRUE; // break loop
2193 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 break;
2195 }
2196 }
2197 else
2198 --skip_count;
2199 line_start = ptr + 1;
2200 }
2201 }
2202 }
2203 linerest = (long)(ptr - line_start);
2204 ui_breakcheck();
2205 }
2206
2207failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (error && read_count == 0)
2210 error = FALSE;
2211
2212 /*
2213 * If we get EOF in the middle of a line, note the fact and
2214 * complete the line ourselves.
2215 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2216 */
2217 if (!error
2218 && !got_int
2219 && linerest != 0
2220 && !(!curbuf->b_p_bin
2221 && fileformat == EOL_DOS
2222 && *line_start == Ctrl_Z
2223 && ptr == line_start + 1))
2224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002225 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 curbuf->b_p_eol = FALSE;
2228 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002229 len = (colnr_T)(ptr - line_start + 1);
2230 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 error = TRUE;
2232 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002233 {
2234#ifdef FEAT_PERSISTENT_UNDO
2235 if (read_undo_file)
2236 sha256_update(&sha_ctx, line_start, len);
2237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002243 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
2245#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002246 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002247 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002248 crypt_free_state(curbuf->b_cryptstate);
2249 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002250 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002251 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2252 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 // Don't set cryptkey to NULL, it's used below as a flag that
2254 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255#endif
2256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 // If editing a new file: set 'fenc' for the current buffer.
2258 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002261 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (fenc_alloced)
2263 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002264#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#endif
2268
2269 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002271#ifdef HAVE_FD_CLOEXEC
2272 else
2273 {
2274 int fdflags = fcntl(fd, F_GETFD);
2275 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002276 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002277 }
2278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 vim_free(buffer);
2280
2281#ifdef HAVE_DUP
2282 if (read_stdin)
2283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002286 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288#endif
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (tmpname != NULL)
2291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 vim_free(tmpname);
2294 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 /*
2298 * In recovery mode everything but autocommands is skipped.
2299 */
2300 if (!recoverymode)
2301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002302 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2304 {
2305#ifdef FEAT_NETBEANS_INTG
2306 netbeansFireChanges = 0;
2307#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002308 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#ifdef FEAT_NETBEANS_INTG
2310 netbeansFireChanges = 1;
2311#endif
2312 --linecnt;
2313 }
2314 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2315 if (filesize == 0)
2316 linecnt = 0;
2317 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002320#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002321 // After reading the text into the buffer the diff info needs to
2322 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002323 diff_invalidate(curbuf);
2324#endif
2325#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 // All folds in the window are invalid now. Mark them for update
2327 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002328 foldUpdateAll(curwin);
2329#endif
2330 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002331 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 appended_lines_mark(from, linecnt);
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#ifndef ALWAYS_USE_GUI
2335 /*
2336 * If we were reading from the same terminal as where messages go,
2337 * the screen will have been messed up.
2338 * Switch on raw mode now and clear the screen.
2339 */
2340 if (read_stdin)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 starttermcap();
2344 screenclear();
2345 }
2346#endif
2347
2348 if (got_int)
2349 {
2350 if (!(flags & READ_DUMMY))
2351 {
2352 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2353 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002354 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 msg_scroll = msg_save;
2357#ifdef FEAT_VIMINFO
2358 check_marks_read();
2359#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362
2363 if (!filtering && !(flags & READ_DUMMY))
2364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 c = FALSE;
2367
2368#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002369 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 STRCAT(IObuff, _("[fifo]"));
2372 c = TRUE;
2373 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002374 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 STRCAT(IObuff, _("[socket]"));
2377 c = TRUE;
2378 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002379# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002380 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002381 {
2382 STRCAT(IObuff, _("[character special]"));
2383 c = TRUE;
2384 }
2385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#endif
2387 if (curbuf->b_p_ro)
2388 {
2389 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2390 c = TRUE;
2391 }
2392 if (read_no_eol_lnum)
2393 {
2394 msg_add_eol();
2395 c = TRUE;
2396 }
2397 if (ff_error == EOL_DOS)
2398 {
2399 STRCAT(IObuff, _("[CR missing]"));
2400 c = TRUE;
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (split)
2403 {
2404 STRCAT(IObuff, _("[long lines split]"));
2405 c = TRUE;
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (notconverted)
2408 {
2409 STRCAT(IObuff, _("[NOT converted]"));
2410 c = TRUE;
2411 }
2412 else if (converted)
2413 {
2414 STRCAT(IObuff, _("[converted]"));
2415 c = TRUE;
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#ifdef FEAT_CRYPT
2418 if (cryptkey != NULL)
2419 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002420 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 c = TRUE;
2422 }
2423#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002424 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002426 sprintf((char *)IObuff + STRLEN(IObuff),
2427 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 c = TRUE;
2429 }
2430 else if (illegal_byte > 0)
2431 {
2432 sprintf((char *)IObuff + STRLEN(IObuff),
2433 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2434 c = TRUE;
2435 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002436 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 STRCAT(IObuff, _("[READ ERRORS]"));
2439 c = TRUE;
2440 }
2441 if (msg_add_fileformat(fileformat))
2442 c = TRUE;
2443#ifdef FEAT_CRYPT
2444 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002445 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002446 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 else
2448#endif
2449 msg_add_lines(c, (long)linecnt, filesize);
2450
Bram Moolenaard23a8232018-02-10 18:45:26 +01002451 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 msg_scrolled_ign = TRUE;
2453#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002454 // Don't show the message when reading stdin, it would end up in a
2455 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (read_stdin || read_buffer)
2457 p = msg_may_trunc(FALSE, IObuff);
2458 else
2459#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002460 {
2461 if (msg_col > 0)
2462 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002463 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002466 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002467 // Need to repeat the message after redrawing when:
2468 // - When reading from stdin (the screen will be cleared next).
2469 // - When restart_edit is set (otherwise there will be a delay
2470 // before redrawing).
2471 // - When the screen was scrolled but there is no wait-return
2472 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002473 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 msg_scrolled_ign = FALSE;
2475 }
2476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002477 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002479 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002480 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 curbuf->b_p_ro = TRUE;
2482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002483 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /*
2486 * In Ex mode: cursor at last new line.
2487 * Otherwise: cursor at first new line.
2488 */
2489 if (exmode_active)
2490 curwin->w_cursor.lnum = from + linecnt;
2491 else
2492 curwin->w_cursor.lnum = from + 1;
2493 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002494 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002496 if (!cmdmod.lockmarks)
2497 {
2498 // Set '[ and '] marks to the newly read lines.
2499 curbuf->b_op_start.lnum = from + 1;
2500 curbuf->b_op_start.col = 0;
2501 curbuf->b_op_end.lnum = from + linecnt;
2502 curbuf->b_op_end.col = 0;
2503 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002504
Bram Moolenaar4f974752019-02-17 17:44:42 +01002505#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002506 /*
2507 * Work around a weird problem: When a file has two links (only
2508 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002509 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002510 * It's correct again after reading the file, thus reset the timestamp
2511 * here.
2512 */
2513 if (newfile && !read_stdin && !read_buffer
2514 && mch_stat((char *)fname, &st) >= 0)
2515 {
2516 buf_store_time(curbuf, &st, fname);
2517 curbuf->b_mtime_read = curbuf->b_mtime;
2518 }
2519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521 msg_scroll = msg_save;
2522
2523#ifdef FEAT_VIMINFO
2524 /*
2525 * Get the marks before executing autocommands, so they can be used there.
2526 */
2527 check_marks_read();
2528#endif
2529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002531 * We remember if the last line of the read didn't have
2532 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2533 * or writing the read again with 'binary' on. The latter is required
2534 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002536 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002538 // When reloading a buffer put the cursor at the first line that is
2539 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002540 if (flags & READ_KEEP_UNDO)
2541 u_find_first_changed();
2542
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002543#ifdef FEAT_PERSISTENT_UNDO
2544 /*
2545 * When opening a new file locate undo info and read it.
2546 */
2547 if (read_undo_file)
2548 {
2549 char_u hash[UNDO_HASH_SIZE];
2550
2551 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002552 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002553 }
2554#endif
2555
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002556 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 int m = msg_scroll;
2559 int n = msg_scrolled;
2560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002561 // Save the fileformat now, otherwise the buffer will be considered
2562 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002563 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 save_file_ff(curbuf);
2565
2566 /*
2567 * The output from the autocommands should not overwrite anything and
2568 * should not be overwritten: Set msg_scroll, restore its value if no
2569 * output was done.
2570 */
2571 msg_scroll = TRUE;
2572 if (filtering)
2573 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2574 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002575 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2578 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002579 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2580 /*
2581 * EVENT_FILETYPE was not triggered but the buffer already has a
2582 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2583 */
2584 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2585 TRUE, curbuf);
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 else
2588 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2589 FALSE, NULL, eap);
2590 if (msg_scrolled == n)
2591 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002592# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002593 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 if (recoverymode && error)
2599 return FAIL;
2600 return OK;
2601}
2602
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002603#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002604/*
2605 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2606 * which is the name of files used for process substitution output by
2607 * some shells on some operating systems, e.g., bash on SunOS.
2608 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2609 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002611is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002612{
2613 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2614 && VIM_ISDIGIT(fname[8])
2615 && *skipdigits(fname + 9) == NUL
2616 && (fname[9] != NUL
2617 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2618}
2619#endif
2620
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002621/*
2622 * From the current line count and characters read after that, estimate the
2623 * line number where we are now.
2624 * Used for error messages that include a line number.
2625 */
2626 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002627readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002628 linenr_T linecnt, // line count before reading more bytes
2629 char_u *p, // start of more bytes read
2630 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002631{
2632 char_u *s;
2633 linenr_T lnum;
2634
2635 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2636 for (s = p; s < endp; ++s)
2637 if (*s == '\n')
2638 ++lnum;
2639 return lnum;
2640}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002643 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2644 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * Returns OK or FAIL.
2646 */
2647 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002648prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
Bram Moolenaar13505972019-01-24 15:04:48 +01002650 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (eap->cmd == NULL)
2652 return FAIL;
2653
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002654 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2655 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002656 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002657 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002658
2659 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002660 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002661 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return OK;
2663}
2664
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002665/*
2666 * Set default or forced 'fileformat' and 'binary'.
2667 */
2668 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002669set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002670{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002671 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002672 if (set_options)
2673 {
2674 if (eap != NULL && eap->force_ff != 0)
2675 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2676 else if (*p_ffs != NUL)
2677 set_fileformat(default_fileformat(), OPT_LOCAL);
2678 }
2679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002680 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002681 if (eap != NULL && eap->force_bin != 0)
2682 {
2683 int oldval = curbuf->b_p_bin;
2684
2685 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2686 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2687 }
2688}
2689
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002690/*
2691 * Set forced 'fileencoding'.
2692 */
2693 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002694set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002695{
2696 if (eap->force_enc != 0)
2697 {
2698 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2699
2700 if (fenc != NULL)
2701 set_string_option_direct((char_u *)"fenc", -1,
2702 fenc, OPT_FREE|OPT_LOCAL, 0);
2703 vim_free(fenc);
2704 }
2705}
2706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707/*
2708 * Find next fileencoding to use from 'fileencodings'.
2709 * "pp" points to fenc_next. It's advanced to the next item.
2710 * When there are no more items, an empty string is returned and *pp is set to
2711 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002712 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2713 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 */
2715 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002716next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
2718 char_u *p;
2719 char_u *r;
2720
Bram Moolenaarf077db22019-08-13 00:18:24 +02002721 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (**pp == NUL)
2723 {
2724 *pp = NULL;
2725 return (char_u *)"";
2726 }
2727 p = vim_strchr(*pp, ',');
2728 if (p == NULL)
2729 {
2730 r = enc_canonize(*pp);
2731 *pp += STRLEN(*pp);
2732 }
2733 else
2734 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002735 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 *pp = p + 1;
2737 if (r != NULL)
2738 {
2739 p = enc_canonize(r);
2740 vim_free(r);
2741 r = p;
2742 }
2743 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002744 if (r != NULL)
2745 *alloced = TRUE;
2746 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002748 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 r = (char_u *)"";
2750 *pp = NULL;
2751 }
2752 return r;
2753}
2754
Bram Moolenaar13505972019-01-24 15:04:48 +01002755#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756/*
2757 * Convert a file with the 'charconvert' expression.
2758 * This closes the file which is to be read, converts it and opens the
2759 * resulting file for reading.
2760 * Returns name of the resulting converted file (the caller should delete it
2761 * after reading it).
2762 * Returns NULL if the conversion failed ("*fdp" is not set) .
2763 */
2764 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002765readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002766 char_u *fname, // name of input file
2767 char_u *fenc, // converted from
2768 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002771 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002773 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002775 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else
2777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002778 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 *fdp = -1;
2780 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2781 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002782 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2784 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002785 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787
2788 if (errmsg != NULL)
2789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002790 // Don't use emsg(), it breaks mappings, the retry with
2791 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002792 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (tmpname != NULL)
2794 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002795 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002796 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 }
2799
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002800 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (*fdp < 0)
2802 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2803
2804 return tmpname;
2805}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#endif
2807
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002808#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002810 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2812 * *filesizep are updated.
2813 * Return the (new) encryption key, NULL for no encryption.
2814 */
2815 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002816check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 char_u *cryptkey, // previous encryption key or NULL
2818 char_u *ptr, // pointer to read bytes
2819 long *sizep, // length of read bytes
2820 off_T *filesizep, // nr of bytes used from file
2821 int newfile, // editing a new buffer
2822 char_u *fname, // file name to display
2823 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002825 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002826 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002827
2828 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002830 // Mark the buffer as read-only until the decryption has taken place.
2831 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002832 curbuf->b_p_ro = TRUE;
2833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002834 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002835 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002836 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
2838 if (*curbuf->b_p_key)
2839 cryptkey = curbuf->b_p_key;
2840 else
2841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002842 // When newfile is TRUE, store the typed key in the 'key'
2843 // option and don't free it. bf needs hash of the key saved.
2844 // Don't ask for the key again when first time Enter was hit.
2845 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002846 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002847 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002848 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002849 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002850 *did_ask = TRUE;
2851
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 if (cryptkey != NULL && *cryptkey == NUL)
2854 {
2855 if (cryptkey != curbuf->b_p_key)
2856 vim_free(cryptkey);
2857 cryptkey = NULL;
2858 }
2859 }
2860 }
2861
2862 if (cryptkey != NULL)
2863 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002864 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002865
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002866 curbuf->b_cryptstate = crypt_create_from_header(
2867 method, cryptkey, ptr);
2868 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002870 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002871 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002872 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002873 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002874 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002875 *filesizep += header_len;
2876 *sizep -= header_len;
2877 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002880 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002883 // When starting to edit a new file which does not have encryption, clear
2884 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002885 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2887
2888 return cryptkey;
2889}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002890#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002893 * Return TRUE if a file appears to be read-only from the file permissions.
2894 */
2895 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002896check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002897 char_u *fname, // full path to file
2898 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002899{
2900#ifndef USE_MCH_ACCESS
2901 int fd = 0;
2902#endif
2903
2904 return (
2905#ifdef USE_MCH_ACCESS
2906# ifdef UNIX
2907 (perm & 0222) == 0 ||
2908# endif
2909 mch_access((char *)fname, W_OK)
2910#else
2911 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2912 ? TRUE : (close(fd), FALSE)
2913#endif
2914 );
2915}
2916
Bram Moolenaara7870192019-02-14 12:56:36 +01002917#if defined(HAVE_FSYNC) || defined(PROTO)
2918/*
2919 * Call fsync() with Mac-specific exception.
2920 * Return fsync() result: zero for success.
2921 */
2922 int
2923vim_fsync(int fd)
2924{
2925 int r;
2926
2927# ifdef MACOS_X
2928 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002929 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002930# endif
2931 r = fsync(fd);
2932 return r;
2933}
2934#endif
2935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002937 * Set the name of the current buffer. Use when the buffer doesn't have a
2938 * name and a ":r" or ":w" command with a file name is used.
2939 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002940 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002941set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002942{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002943 buf_T *buf = curbuf;
2944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002946 if (curbuf->b_p_bl)
2947 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2948 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002949#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002950 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002951 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002952#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002953 if (curbuf != buf)
2954 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002955 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002956 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002957 return FAIL;
2958 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002959
2960 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2961 curbuf->b_flags |= BF_NOTEDITED;
2962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002964 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2965 if (curbuf->b_p_bl)
2966 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002967#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002968 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002969 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002970#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002972 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002973 if (*curbuf->b_p_ft == NUL)
2974 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002975 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002976 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002977 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002978 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002979
2980 return OK;
2981}
2982
2983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 * Put file name into IObuff with quotes.
2985 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002986 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002987msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 if (fname == NULL)
2990 fname = (char_u *)"-stdin-";
2991 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2992 IObuff[0] = '"';
2993 STRCAT(IObuff, "\" ");
2994}
2995
2996/*
2997 * Append message for text mode to IObuff.
2998 * Return TRUE if something appended.
2999 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003000 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003001msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003#ifndef USE_CRNL
3004 if (eol_type == EOL_DOS)
3005 {
3006 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3007 return TRUE;
3008 }
3009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (eol_type == EOL_MAC)
3011 {
3012 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3013 return TRUE;
3014 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003015#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (eol_type == EOL_UNIX)
3017 {
3018 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3019 return TRUE;
3020 }
3021#endif
3022 return FALSE;
3023}
3024
3025/*
3026 * Append line and character count to IObuff.
3027 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003029msg_add_lines(
3030 int insert_space,
3031 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003032 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 char_u *p;
3035
3036 p = IObuff + STRLEN(IObuff);
3037
3038 if (insert_space)
3039 *p++ = ' ';
3040 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003041 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003042 "%ldL, %lldC", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
3044 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003045 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003047 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
3048 NGETTEXT("%lld character", "%lld characters", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003049 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051}
3052
3053/*
3054 * Append message for missing line separator to IObuff.
3055 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003056 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003057msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3060}
3061
Bram Moolenaar473952e2019-09-28 16:30:04 +02003062 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003063time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003065#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003066 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3067 // the seconds. Since the roundoff is done when flushing the inode, the
3068 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return (t1 - t2 > 1 || t2 - t1 > 1);
3070#else
3071 return (t1 != t2);
3072#endif
3073}
3074
3075/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003076 * Return TRUE if file encoding "fenc" requires conversion from or to
3077 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003079 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003080need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003082 int same_encoding;
3083 int enc_flags;
3084 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003086 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003087 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003088 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003089 fenc_flags = 0;
3090 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003091 else
3092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003093 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3094 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003095 enc_flags = get_fio_flags(p_enc);
3096 fenc_flags = get_fio_flags(fenc);
3097 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3098 }
3099 if (same_encoding)
3100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003101 // Specified encoding matches with 'encoding'. This requires
3102 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003103 return enc_unicode != 0;
3104 }
3105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003106 // Encodings differ. However, conversion is not needed when 'enc' is any
3107 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003108 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109}
3110
3111/*
3112 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3113 * internal conversion.
3114 * if "ptr" is an empty string, use 'encoding'.
3115 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003116 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003117get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 int prop;
3120
3121 if (*ptr == NUL)
3122 ptr = p_enc;
3123
3124 prop = enc_canon_props(ptr);
3125 if (prop & ENC_UNICODE)
3126 {
3127 if (prop & ENC_2BYTE)
3128 {
3129 if (prop & ENC_ENDIAN_L)
3130 return FIO_UCS2 | FIO_ENDIAN_L;
3131 return FIO_UCS2;
3132 }
3133 if (prop & ENC_4BYTE)
3134 {
3135 if (prop & ENC_ENDIAN_L)
3136 return FIO_UCS4 | FIO_ENDIAN_L;
3137 return FIO_UCS4;
3138 }
3139 if (prop & ENC_2WORD)
3140 {
3141 if (prop & ENC_ENDIAN_L)
3142 return FIO_UTF16 | FIO_ENDIAN_L;
3143 return FIO_UTF16;
3144 }
3145 return FIO_UTF8;
3146 }
3147 if (prop & ENC_LATIN1)
3148 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003149 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return 0;
3151}
3152
Bram Moolenaar473952e2019-09-28 16:30:04 +02003153#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154/*
3155 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3156 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3157 * Used for conversion between 'encoding' and 'fileencoding'.
3158 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003159 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003160get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 int cp;
3163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003164 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (!enc_utf8 && enc_codepage <= 0)
3166 return 0;
3167
3168 cp = encname2codepage(ptr);
3169 if (cp == 0)
3170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003171# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (STRCMP(ptr, "utf-8") == 0)
3173 cp = CP_UTF8;
3174 else
3175# endif
3176 return 0;
3177 }
3178 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3179}
3180#endif
3181
Bram Moolenaar473952e2019-09-28 16:30:04 +02003182#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183/*
3184 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3185 * needed for the internal conversion to/from utf-8 or latin1.
3186 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003187 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003188get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3191 && (enc_canon_props(ptr) & ENC_MACROMAN))
3192 return FIO_MACROMAN;
3193 return 0;
3194}
3195#endif
3196
3197/*
3198 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3199 * "size" must be at least 2.
3200 * Return the name of the encoding and set "*lenp" to the length.
3201 * Returns NULL when no BOM found.
3202 */
3203 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003204check_for_bom(
3205 char_u *p,
3206 long size,
3207 int *lenp,
3208 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 char *name = NULL;
3211 int len = 2;
3212
3213 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003214 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003216 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 len = 3;
3218 }
3219 else if (p[0] == 0xff && p[1] == 0xfe)
3220 {
3221 if (size >= 4 && p[2] == 0 && p[3] == 0
3222 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003224 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 len = 4;
3226 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003227 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003229 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003230 // utf-16le is preferred, it also works for ucs-2le text
3231 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233 else if (p[0] == 0xfe && p[1] == 0xff
3234 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003236 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003237 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003238 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003239 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3243 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003245 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 len = 4;
3247 }
3248
3249 *lenp = len;
3250 return (char_u *)name;
3251}
3252
3253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * Try to find a shortname by comparing the fullname with the current
3255 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003256 * Returns "full_path" or pointer into "full_path" if shortened.
3257 */
3258 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003259shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003260{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003261 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003262 char_u *p = full_path;
3263
Bram Moolenaard9462e32011-04-11 21:35:11 +02003264 dirname = alloc(MAXPATHL);
3265 if (dirname == NULL)
3266 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003267 if (mch_dirname(dirname, MAXPATHL) == OK)
3268 {
3269 p = shorten_fname(full_path, dirname);
3270 if (p == NULL || *p == NUL)
3271 p = full_path;
3272 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003273 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003274 return p;
3275}
3276
3277/*
3278 * Try to find a shortname by comparing the fullname with the current
3279 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 * Returns NULL if not shorter name possible, pointer into "full_path"
3281 * otherwise.
3282 */
3283 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003284shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 int len;
3287 char_u *p;
3288
3289 if (full_path == NULL)
3290 return NULL;
3291 len = (int)STRLEN(dir_name);
3292 if (fnamencmp(dir_name, full_path, len) == 0)
3293 {
3294 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003295#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003297 * MS-Windows: when a file is in the root directory, dir_name will end
3298 * in a slash, since C: by itself does not define a specific dir. In
3299 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 */
3301 if (!((len > 2) && (*(p - 2) == ':')))
3302#endif
3303 {
3304 if (vim_ispathsep(*p))
3305 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003306#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
3308 p = NULL;
3309#endif
3310 }
3311 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003312#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /*
3314 * When using a file in the current drive, remove the drive name:
3315 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3316 * a floppy from "A:\dir" to "B:\dir".
3317 */
3318 else if (len > 3
3319 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3320 && full_path[1] == ':'
3321 && vim_ispathsep(full_path[2]))
3322 p = full_path + 2;
3323#endif
3324 else
3325 p = NULL;
3326 return p;
3327}
3328
3329/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003330 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 * When "force" is TRUE: Use full path from now on for files currently being
3332 * edited, both for file name and swap file name. Try to shorten the file
3333 * names a bit, if safe to do so.
3334 * When "force" is FALSE: Only try to shorten absolute file names.
3335 * For buffers that have buftype "nofile" or "scratch": never change the file
3336 * name.
3337 */
3338 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003339shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3340{
3341 char_u *p;
3342
3343 if (buf->b_fname != NULL
3344#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003345 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003346#endif
3347 && !path_with_url(buf->b_fname)
3348 && (force
3349 || buf->b_sfname == NULL
3350 || mch_isFullName(buf->b_sfname)))
3351 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003352 if (buf->b_sfname != buf->b_ffname)
3353 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003354 p = shorten_fname(buf->b_ffname, dirname);
3355 if (p != NULL)
3356 {
3357 buf->b_sfname = vim_strsave(p);
3358 buf->b_fname = buf->b_sfname;
3359 }
3360 if (p == NULL || buf->b_fname == NULL)
3361 buf->b_fname = buf->b_ffname;
3362 }
3363}
3364
3365/*
3366 * Shorten filenames for all buffers.
3367 */
3368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003369shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 char_u dirname[MAXPATHL];
3372 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003375 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003377 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003378
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003379 // Always make the swap file name a full path, a "nofile" buffer may
3380 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003381 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003384 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003385#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003386 popup_update_preview_title();
3387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388}
3389
3390#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3391 || defined(FEAT_GUI_MSWIN) \
3392 || defined(FEAT_GUI_MAC) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003393 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 || defined(PROTO)
3395/*
3396 * Shorten all filenames in "fnames[count]" by current directory.
3397 */
3398 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003399shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 int i;
3402 char_u dirname[MAXPATHL];
3403 char_u *p;
3404
3405 if (fnames == NULL || count < 1)
3406 return;
3407 mch_dirname(dirname, sizeof(dirname));
3408 for (i = 0; i < count; ++i)
3409 {
3410 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3411 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003412 // shorten_fname() returns pointer in given "fnames[i]". If free
3413 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3414 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 p = vim_strsave(p);
3416 vim_free(fnames[i]);
3417 fnames[i] = p;
3418 }
3419 }
3420}
3421#endif
3422
3423/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003424 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 * fo_o_h.ext for MSDOS or when shortname option set.
3426 *
3427 * Assumed that fname is a valid name found in the filesystem we assure that
3428 * the return value is a different name and ends in 'ext'.
3429 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3430 * characters otherwise.
3431 * Space for the returned name is allocated, must be freed later.
3432 * Returns NULL when out of memory.
3433 */
3434 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435modname(
3436 char_u *fname,
3437 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003438 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003440 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 fname, ext, prepend_dot);
3442}
3443
3444 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003445buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003446 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447 char_u *fname,
3448 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003449 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *retval;
3452 char_u *s;
3453 char_u *e;
3454 char_u *ptr;
3455 int fnamelen, extlen;
3456
3457 extlen = (int)STRLEN(ext);
3458
3459 /*
3460 * If there is no file name we must get the name of the current directory
3461 * (we need the full path in case :cd is used).
3462 */
3463 if (fname == NULL || *fname == NUL)
3464 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003465 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (retval == NULL)
3467 return NULL;
3468 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3469 (fnamelen = (int)STRLEN(retval)) == 0)
3470 {
3471 vim_free(retval);
3472 return NULL;
3473 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003474 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 retval[fnamelen++] = PATHSEP;
3477 retval[fnamelen] = NUL;
3478 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003479 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 else
3482 {
3483 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003484 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (retval == NULL)
3486 return NULL;
3487 STRCPY(retval, fname);
3488#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003489 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490#endif
3491 }
3492
3493 /*
3494 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3495 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3496 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3497 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3498 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003499 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003501 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003502 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003505 {
3506 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003511 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3513 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 s = ptr + STRLEN(ptr);
3516
3517 /*
3518 * For 8.3 file names we may have to reduce the length.
3519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 /*
3523 * If there is no file name, or the file name ends in '/', and the
3524 * extension starts with '.', put a '_' before the dot, because just
3525 * ".ext" is invalid.
3526 */
3527 if (fname == NULL || *fname == NUL
3528 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 *s++ = '_';
3532 }
3533 /*
3534 * If the extension starts with '.', truncate the base name at 8
3535 * characters
3536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003539 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 s = ptr + 8;
3542 *s = '\0';
3543 }
3544 }
3545 /*
3546 * If the extension doesn't start with '.', and the file name
3547 * doesn't have an extension yet, append a '.'
3548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 else if ((e = vim_strchr(ptr, '.')) == NULL)
3550 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 /*
3552 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003553 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 */
3555 else if ((int)STRLEN(e) + extlen > 4)
3556 s = e + 4 - extlen;
3557 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003558#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /*
3560 * If there is no file name, and the extension starts with '.', put a
3561 * '_' before the dot, because just ".ext" may be invalid if it's on a
3562 * FAT partition, and on HPFS it doesn't matter.
3563 */
3564 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3565 *s++ = '_';
3566#endif
3567
3568 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003569 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * ext can start with '.' and cannot exceed 3 more characters.
3571 */
3572 STRCPY(s, ext);
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 /*
3575 * Prepend the dot.
3576 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003577 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003579 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /*
3584 * Check that, after appending the extension, the file name is really
3585 * different.
3586 */
3587 if (fname != NULL && STRCMP(fname, retval) == 0)
3588 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003589 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 while (--s >= ptr)
3591 {
3592 if (*s != '_')
3593 {
3594 *s = '_';
3595 break;
3596 }
3597 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003598 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *ptr = 'v';
3600 }
3601 return retval;
3602}
3603
3604/*
3605 * Like fgets(), but if the file line is too long, it is truncated and the
3606 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003607 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 */
3609 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003610vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 char *eof;
3613#define FGETS_SIZE 200
3614 char tbuf[FGETS_SIZE];
3615
3616 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003620 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003622 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 do
3624 {
3625 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003626 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3628 }
3629 return (eof == NULL);
3630}
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632/*
3633 * rename() only works if both files are on the same file system, this
3634 * function will (attempts to?) copy the file across if rename fails -- webb
3635 * Return -1 for failure, 0 for success.
3636 */
3637 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003638vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 int fd_in;
3641 int fd_out;
3642 int n;
3643 char *errmsg = NULL;
3644 char *buffer;
3645#ifdef AMIGA
3646 BPTR flock;
3647#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003648 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003649 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003650#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003652#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003653 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003656 * When the names are identical, there is nothing to do. When they refer
3657 * to the same file (ignoring case and slash/backslash differences) but
3658 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003661 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003662 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003663 use_tmp_file = TRUE;
3664 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003665 return 0;
3666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 /*
3669 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3670 */
3671 if (mch_stat((char *)from, &st) < 0)
3672 return -1;
3673
Bram Moolenaar3576da72008-12-30 15:15:57 +00003674#ifdef UNIX
3675 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003676 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003678 // It's possible for the source and destination to be the same file.
3679 // This happens when "from" and "to" differ in case and are on a FAT32
3680 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003681 if (mch_stat((char *)to, &st_to) >= 0
3682 && st.st_dev == st_to.st_dev
3683 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003684 use_tmp_file = TRUE;
3685 }
3686#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003687#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003688 {
3689 BY_HANDLE_FILE_INFORMATION info1, info2;
3690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003691 // It's possible for the source and destination to be the same file.
3692 // In that case go through a temp file name. This makes rename("foo",
3693 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003694 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3695 && win32_fileinfo(to, &info2) == FILEINFO_OK
3696 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3697 && info1.nFileIndexHigh == info2.nFileIndexHigh
3698 && info1.nFileIndexLow == info2.nFileIndexLow)
3699 use_tmp_file = TRUE;
3700 }
3701#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003702
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003703 if (use_tmp_file)
3704 {
3705 char tempname[MAXPATHL + 1];
3706
3707 /*
3708 * Find a name that doesn't exist and is in the same directory.
3709 * Rename "from" to "tempname" and then rename "tempname" to "to".
3710 */
3711 if (STRLEN(from) >= MAXPATHL - 5)
3712 return -1;
3713 STRCPY(tempname, from);
3714 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003715 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003716 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3717 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003718 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003719 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003720 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003721 if (mch_rename(tempname, (char *)to) == 0)
3722 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003723 // Strange, the second step failed. Try moving the
3724 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003725 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003726 return -1;
3727 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003728 // If it fails for one temp name it will most likely fail
3729 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003730 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003731 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003732 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003733 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003734 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 /*
3737 * Delete the "to" file, this is required on some systems to make the
3738 * mch_rename() work, on other systems it makes sure that we don't have
3739 * two files when the mch_rename() fails.
3740 */
3741
3742#ifdef AMIGA
3743 /*
3744 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3745 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003746 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 * deleting the "from" file (horror!) we lock it during the remove.
3748 *
3749 * When used for making a backup before writing the file: This should not
3750 * happen with ":w", because startscript() should detect this problem and
3751 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3752 * name. This problem does exist with ":w filename", but then the
3753 * original file will be somewhere else so the backup isn't really
3754 * important. If autoscripting is off the rename may fail.
3755 */
3756 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3757#endif
3758 mch_remove(to);
3759#ifdef AMIGA
3760 if (flock)
3761 UnLock(flock);
3762#endif
3763
3764 /*
3765 * First try a normal rename, return if it works.
3766 */
3767 if (mch_rename((char *)from, (char *)to) == 0)
3768 return 0;
3769
3770 /*
3771 * Rename() failed, try copying the file.
3772 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003773 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003774#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003775 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003776 acl = mch_get_acl(from);
3777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3779 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003780 {
3781#ifdef HAVE_ACL
3782 mch_free_acl(acl);
3783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003785 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003787 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003788 fd_out = mch_open((char *)to,
3789 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (fd_out == -1)
3791 {
3792 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003793#ifdef HAVE_ACL
3794 mch_free_acl(acl);
3795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return -1;
3797 }
3798
Bram Moolenaar473952e2019-09-28 16:30:04 +02003799 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 if (buffer == NULL)
3801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003803 close(fd_in);
3804#ifdef HAVE_ACL
3805 mch_free_acl(acl);
3806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return -1;
3808 }
3809
Bram Moolenaar473952e2019-09-28 16:30:04 +02003810 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003811 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 errmsg = _("E208: Error writing to \"%s\"");
3814 break;
3815 }
3816
3817 vim_free(buffer);
3818 close(fd_in);
3819 if (close(fd_out) < 0)
3820 errmsg = _("E209: Error closing \"%s\"");
3821 if (n < 0)
3822 {
3823 errmsg = _("E210: Error reading \"%s\"");
3824 to = from;
3825 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003826#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003827 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003828#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003829#ifdef HAVE_ACL
3830 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003831 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003832#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003833#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003834 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (errmsg != NULL)
3837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003838 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return -1;
3840 }
3841 mch_remove(from);
3842 return 0;
3843}
3844
3845static int already_warned = FALSE;
3846
3847/*
3848 * Check if any not hidden buffer has been changed.
3849 * Postpone the check if there are characters in the stuff buffer, a global
3850 * command is being executed, a mapping is being executed or an autocommand is
3851 * busy.
3852 * Returns TRUE if some message was written (screen should be redrawn and
3853 * cursor positioned).
3854 */
3855 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003856check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
3859 buf_T *buf;
3860 int didit = 0;
3861 int n;
3862
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003863 // Don't check timestamps while system() or another low-level function may
3864 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (no_check_timestamps > 0)
3866 return FALSE;
3867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3869 // event and we would keep on checking if the file is steadily growing.
3870 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (focus && did_check_timestamps)
3872 {
3873 need_check_timestamps = TRUE;
3874 return FALSE;
3875 }
3876
3877 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003878 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003879 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 else
3881 {
3882 ++no_wait_return;
3883 did_check_timestamps = TRUE;
3884 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003885 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003887 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (buf->b_nwindows > 0)
3889 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003890 bufref_T bufref;
3891
3892 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 n = buf_check_timestamp(buf, focus);
3894 if (didit < n)
3895 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003896 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003898 // Autocommands have removed the buffer, start at the
3899 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 buf = firstbuf;
3901 continue;
3902 }
3903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 --no_wait_return;
3906 need_check_timestamps = FALSE;
3907 if (need_wait_return && didit == 2)
3908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003909 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003910 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 out_flush();
3912 }
3913 }
3914 return didit;
3915}
3916
3917/*
3918 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3919 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3920 * empty.
3921 */
3922 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003923move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 buf_T *tbuf = curbuf;
3926 int retval = OK;
3927 linenr_T lnum;
3928 char_u *p;
3929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003930 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 curbuf = tobuf;
3932 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3933 {
3934 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3935 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3936 {
3937 vim_free(p);
3938 retval = FAIL;
3939 break;
3940 }
3941 vim_free(p);
3942 }
3943
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003944 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (retval != FAIL)
3946 {
3947 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003948 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003949 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003951 // Oops! We could try putting back the saved lines, but that
3952 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 retval = FAIL;
3954 break;
3955 }
3956 }
3957
3958 curbuf = tbuf;
3959 return retval;
3960}
3961
3962/*
3963 * Check if buffer "buf" has been changed.
3964 * Also check if the file for a new buffer unexpectedly appeared.
3965 * return 1 if a changed buffer was found.
3966 * return 2 if a message has been displayed.
3967 * return 0 otherwise.
3968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003970buf_check_timestamp(
3971 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003972 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003974 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int stat_res;
3976 int retval = 0;
3977 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003978 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003980 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 int helpmesg = FALSE;
3982 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003983 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3985 int can_reload = FALSE;
3986#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003987 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int orig_mode = buf->b_orig_mode;
3989#ifdef FEAT_GUI
3990 int save_mouse_correct = need_mouse_correct;
3991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003993 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003994#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003995 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003996#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003997 bufref_T bufref;
3998
3999 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004001 // If there is no file name, the buffer is not loaded, 'buftype' is
4002 // set, we are in the middle of a save or being called recursively: ignore
4003 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (buf->b_ffname == NULL
4005 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004006 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004009#ifdef FEAT_NETBEANS_INTG
4010 || isNetbeansBuffer(buf)
4011#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004012#ifdef FEAT_TERMINAL
4013 || buf->b_term != NULL
4014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 )
4016 return 0;
4017
4018 if ( !(buf->b_flags & BF_NOTEDITED)
4019 && buf->b_mtime != 0
4020 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4021 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004022 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#ifdef HAVE_ST_MODE
4024 || (int)st.st_mode != buf->b_orig_mode
4025#else
4026 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4027#endif
4028 ))
4029 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004030 long prev_b_mtime = buf->b_mtime;
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 retval = 1;
4033
Bram Moolenaar386bc822018-07-07 18:34:12 +02004034 // set b_mtime to stop further warnings (e.g., when executing
4035 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (stat_res < 0)
4037 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004038 // Check the file again later to see if it re-appears.
4039 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 buf->b_orig_size = 0;
4041 buf->b_orig_mode = 0;
4042 }
4043 else
4044 buf_store_time(buf, &st, buf->b_ffname);
4045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004046 // Don't do anything for a directory. Might contain the file
4047 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (mch_isdir(buf->b_fname))
4049 ;
4050
4051 /*
4052 * If 'autoread' is set, the buffer has no changes and the file still
4053 * exists, reload the buffer. Use the buffer-local option value if it
4054 * was set, the global option value otherwise.
4055 */
4056 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4057 && !bufIsChanged(buf) && stat_res >= 0)
4058 reload = TRUE;
4059 else
4060 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004061 if (stat_res < 0)
4062 reason = "deleted";
4063 else if (bufIsChanged(buf))
4064 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004065 /*
4066 * Check if the file contents really changed to avoid giving a
4067 * warning when only the timestamp was set (e.g., checked out of
4068 * CVS). Always warn when the buffer was changed.
4069 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004070 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4071 reason = "changed";
4072 else if (orig_mode != buf->b_orig_mode)
4073 reason = "mode";
4074 else
4075 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 /*
4078 * Only give the warning if there are no FileChangedShell
4079 * autocommands.
4080 * Avoid being called recursively by setting "busy".
4081 */
4082 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004083#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004084 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4085 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004086#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004087 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4089 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004090 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 busy = FALSE;
4092 if (n)
4093 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004094 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004095 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004096#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004097 s = get_vim_var_str(VV_FCS_CHOICE);
4098 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4099 reload = TRUE;
4100 else if (STRCMP(s, "ask") == 0)
4101 n = FALSE;
4102 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004103#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004104 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004106 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004108 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004109 {
4110 // Only give the message once.
4111 if (prev_b_mtime != -1)
4112 mesg = _("E211: File \"%s\" no longer available");
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 else
4115 {
4116 helpmesg = TRUE;
4117#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4118 can_reload = TRUE;
4119#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004120 if (reason[2] == 'n')
4121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004123 mesg2 = _("See \":help W12\" for more info.");
4124 }
4125 else if (reason[1] == 'h')
4126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004128 mesg2 = _("See \":help W11\" for more info.");
4129 }
4130 else if (*reason == 'm')
4131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004133 mesg2 = _("See \":help W16\" for more info.");
4134 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004135 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004136 // Only timestamp changed, store it to avoid a warning
4137 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004138 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 }
4141 }
4142
4143 }
4144 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4145 && vim_fexists(buf->b_ffname))
4146 {
4147 retval = 1;
4148 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4149 buf->b_flags |= BF_NEW_W;
4150#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4151 can_reload = TRUE;
4152#endif
4153 }
4154
4155 if (mesg != NULL)
4156 {
4157 path = home_replace_save(buf, buf->b_fname);
4158 if (path != NULL)
4159 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004160 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004162 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004163 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004164#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004165 // Set warningmsg here, before the unimportant and output-specific
4166 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4170 if (can_reload)
4171 {
4172 if (*mesg2 != NUL)
4173 {
4174 STRCAT(tbuf, "\n");
4175 STRCAT(tbuf, mesg2);
4176 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004177 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4178 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004179 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 reload = TRUE;
4181 }
4182 else
4183#endif
4184 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4185 {
4186 if (*mesg2 != NUL)
4187 {
4188 STRCAT(tbuf, "; ");
4189 STRCAT(tbuf, mesg2);
4190 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004191 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 retval = 2;
4193 }
4194 else
4195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004199 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004201 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 msg_clr_eos();
4203 (void)msg_end();
4204 if (emsg_silent == 0)
4205 {
4206 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004207#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004209#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004210 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004211 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004213 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 redraw_cmdline = FALSE;
4215 }
4216 }
4217 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219
4220 vim_free(path);
4221 vim_free(tbuf);
4222 }
4223 }
4224
4225 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004227 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004228 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004229#ifdef FEAT_PERSISTENT_UNDO
4230 if (buf->b_p_udf && buf->b_ffname != NULL)
4231 {
4232 char_u hash[UNDO_HASH_SIZE];
4233 buf_T *save_curbuf = curbuf;
4234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004235 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004236 curbuf = buf;
4237 u_compute_hash(hash);
4238 u_write_undo(NULL, FALSE, buf, hash);
4239 curbuf = save_curbuf;
4240 }
4241#endif
4242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004244 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004245 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004246 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4247 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004249 // restore this in case an autocommand has set it; it would break
4250 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 need_mouse_correct = save_mouse_correct;
4252#endif
4253
4254 return retval;
4255}
4256
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004257/*
4258 * Reload a buffer that is already loaded.
4259 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004260 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4261 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004262 */
4263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004264buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004265{
4266 exarg_T ea;
4267 pos_T old_cursor;
4268 linenr_T old_topline;
4269 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004270 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004271 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004272 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004273 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004274 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004275
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004277 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004278
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004279 // We only want to read the text from the file, not reset the syntax
4280 // highlighting, clear marks, diff status, etc. Force the fileformat
4281 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004282 if (prep_exarg(&ea, buf) == OK)
4283 {
4284 old_cursor = curwin->w_cursor;
4285 old_topline = curwin->w_topline;
4286
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004287 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004289 // Save all the text, so that the reload can be undone.
4290 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004291 u_sync(FALSE);
4292 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4293 flags |= READ_KEEP_UNDO;
4294 }
4295
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004296 /*
4297 * To behave like when a new file is edited (matters for
4298 * BufReadPost autocommands) we first need to delete the current
4299 * buffer contents. But if reading the file fails we should keep
4300 * the old contents. Can't use memory only, the file might be
4301 * too big. Use a hidden buffer to move the buffer contents to.
4302 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004303 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004304 savebuf = NULL;
4305 else
4306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004308 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004309 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004310 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004311 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004312 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004313 curbuf = savebuf;
4314 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004315 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004316 curbuf = buf;
4317 curwin->w_buffer = buf;
4318 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004319 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004320 || move_lines(buf, savebuf) == FAIL)
4321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004322 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004323 buf->b_fname);
4324 saved = FAIL;
4325 }
4326 }
4327
4328 if (saved == OK)
4329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004330 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4331 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004332 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4333 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004334 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004335 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004336#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004337 if (!aborting())
4338#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004339 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004340 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004342 // Put the text back from the save buffer. First
4343 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004344 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004345 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004346 break;
4347 (void)move_lines(savebuf, buf);
4348 }
4349 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004350 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004353 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004354 if ((flags & READ_KEEP_UNDO) == 0)
4355 {
4356 u_blockfree(buf);
4357 u_clearall(buf);
4358 }
4359 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004362 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004363 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004364 }
4365 }
4366 vim_free(ea.cmd);
4367
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004368 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004369 wipe_buffer(savebuf, FALSE);
4370
4371#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004373 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004374#endif
4375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 // Restore the topline and cursor position and check it (lines may
4377 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004378 if (old_topline > curbuf->b_ml.ml_line_count)
4379 curwin->w_topline = curbuf->b_ml.ml_line_count;
4380 else
4381 curwin->w_topline = old_topline;
4382 curwin->w_cursor = old_cursor;
4383 check_cursor();
4384 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004386#ifdef FEAT_FOLDING
4387 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004388 win_T *wp;
4389 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004392 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393 if (wp->w_buffer == curwin->w_buffer
4394 && !foldmethodIsManual(wp))
4395 foldUpdateAll(wp);
4396 }
4397#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 // If the mode didn't change and 'readonly' was set, keep the old
4399 // value; the user probably used the ":view" command. But don't
4400 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004401 if (orig_mode == curbuf->b_orig_mode)
4402 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004405 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004406 }
4407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004408 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004409 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004410 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004411}
4412
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004414buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004417 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418#ifdef HAVE_ST_MODE
4419 buf->b_orig_mode = (int)st->st_mode;
4420#else
4421 buf->b_orig_mode = mch_getperm(fname);
4422#endif
4423}
4424
4425/*
4426 * Adjust the line with missing eol, used for the next write.
4427 * Used for do_filter(), when the input lines for the filter are deleted.
4428 */
4429 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004430write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004432 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004433 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434}
4435
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004436// Subfuncions for readdirex()
4437#ifdef FEAT_EVAL
4438# ifdef MSWIN
4439 static char_u *
4440getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4441{
4442 stat_T st;
4443 unsigned short st_mode;
4444 DWORD flag = wfd->dwFileAttributes;
4445 WCHAR *wp;
4446
4447 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4448 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4449 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4450 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4451
4452 wp = wcsrchr(wfd->cFileName, L'.');
4453 if (wp != NULL)
4454 {
4455 if (_wcsicmp(wp, L".exe") == 0 ||
4456 _wcsicmp(wp, L".com") == 0 ||
4457 _wcsicmp(wp, L".cmd") == 0 ||
4458 _wcsicmp(wp, L".bat") == 0)
4459 st_mode |= _S_IEXEC;
4460 }
4461
4462 // Copy user bits to group/other.
4463 st_mode |= (st_mode & 0700) >> 3;
4464 st_mode |= (st_mode & 0700) >> 6;
4465
4466 st.st_mode = st_mode;
4467 return getfpermst(&st, perm);
4468}
4469
4470 static char_u *
4471getftypewfd(WIN32_FIND_DATAW *wfd)
4472{
4473 DWORD flag = wfd->dwFileAttributes;
4474 DWORD tag = wfd->dwReserved0;
4475
4476 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4477 {
4478 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4479 return (char_u*)"junction";
4480 else if (tag == IO_REPARSE_TAG_SYMLINK)
4481 {
4482 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4483 return (char_u*)"linkd";
4484 else
4485 return (char_u*)"link";
4486 }
4487 return (char_u*)"reparse"; // unknown reparse point type
4488 }
4489 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4490 return (char_u*)"dir";
4491 else
4492 return (char_u*)"file";
4493}
4494
4495 static dict_T *
4496create_readdirex_item(WIN32_FIND_DATAW *wfd)
4497{
4498 dict_T *item;
4499 char_u *p;
4500 varnumber_T size, time;
4501 char_u permbuf[] = "---------";
4502
4503 item = dict_alloc();
4504 if (item == NULL)
4505 return NULL;
4506 item->dv_refcount++;
4507
4508 p = utf16_to_enc(wfd->cFileName, NULL);
4509 if (p == NULL)
4510 goto theend;
4511 if (dict_add_string(item, "name", p) == FAIL)
4512 {
4513 vim_free(p);
4514 goto theend;
4515 }
4516 vim_free(p);
4517
4518 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4519 if (dict_add_number(item, "size", size) == FAIL)
4520 goto theend;
4521
4522 // Convert FILETIME to unix time.
4523 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4524 wfd->ftLastWriteTime.dwLowDateTime)
4525 - 116444736000000000) / 10000000;
4526 if (dict_add_number(item, "time", time) == FAIL)
4527 goto theend;
4528
4529 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4530 goto theend;
4531 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4532 goto theend;
4533
4534 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4535 goto theend;
4536 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4537 goto theend;
4538
4539 return item;
4540
4541theend:
4542 dict_unref(item);
4543 return NULL;
4544}
4545# else
4546 static dict_T *
4547create_readdirex_item(char_u *path, char_u *name)
4548{
4549 dict_T *item;
4550 char *p;
4551 size_t len;
4552 stat_T st;
4553 int ret, link = FALSE;
4554 varnumber_T size;
4555 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004556 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004557 struct passwd *pw;
4558 struct group *gr;
4559
4560 item = dict_alloc();
4561 if (item == NULL)
4562 return NULL;
4563 item->dv_refcount++;
4564
4565 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4566 p = alloc(len);
4567 if (p == NULL)
4568 goto theend;
4569 vim_snprintf(p, len, "%s/%s", path, name);
4570 ret = mch_lstat(p, &st);
4571 if (ret >= 0 && S_ISLNK(st.st_mode))
4572 {
4573 link = TRUE;
4574 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004575 if (ret < 0)
4576 q = (char_u*)"link";
4577
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004578 }
4579 vim_free(p);
4580
4581 if (dict_add_string(item, "name", name) == FAIL)
4582 goto theend;
4583
4584 if (ret >= 0)
4585 {
4586 size = (varnumber_T)st.st_size;
4587 if (S_ISDIR(st.st_mode))
4588 size = 0;
4589 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004590 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004591 size = -2;
4592 if (dict_add_number(item, "size", size) == FAIL)
4593 goto theend;
4594 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4595 goto theend;
4596
4597 if (link)
4598 {
4599 if (S_ISDIR(st.st_mode))
4600 q = (char_u*)"linkd";
4601 else
4602 q = (char_u*)"link";
4603 }
4604 else
4605 q = getftypest(&st);
4606 if (dict_add_string(item, "type", q) == FAIL)
4607 goto theend;
4608 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4609 goto theend;
4610
4611 pw = getpwuid(st.st_uid);
4612 if (pw == NULL)
4613 q = (char_u*)"";
4614 else
4615 q = (char_u*)pw->pw_name;
4616 if (dict_add_string(item, "user", q) == FAIL)
4617 goto theend;
4618 gr = getgrgid(st.st_gid);
4619 if (gr == NULL)
4620 q = (char_u*)"";
4621 else
4622 q = (char_u*)gr->gr_name;
4623 if (dict_add_string(item, "group", q) == FAIL)
4624 goto theend;
4625 }
4626 else
4627 {
4628 if (dict_add_number(item, "size", -1) == FAIL)
4629 goto theend;
4630 if (dict_add_number(item, "time", -1) == FAIL)
4631 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004632 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004633 goto theend;
4634 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4635 goto theend;
4636 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4637 goto theend;
4638 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4639 goto theend;
4640 }
4641 return item;
4642
4643theend:
4644 dict_unref(item);
4645 return NULL;
4646}
4647# endif
4648
4649 static int
4650compare_readdirex_item(const void *p1, const void *p2)
4651{
4652 char_u *name1, *name2;
4653
4654 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4655 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004656 if (readdirex_sort == READDIR_SORT_BYTE)
4657 return STRCMP(name1, name2);
4658 else if (readdirex_sort == READDIR_SORT_IC)
4659 return STRICMP(name1, name2);
4660 else
4661 return STRCOLL(name1, name2);
4662}
4663
4664 static int
4665compare_readdir_item(const void *s1, const void *s2)
4666{
4667 if (readdirex_sort == READDIR_SORT_BYTE)
4668 return STRCMP(*(char **)s1, *(char **)s2);
4669 else if (readdirex_sort == READDIR_SORT_IC)
4670 return STRICMP(*(char **)s1, *(char **)s2);
4671 else
4672 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004673}
4674#endif
4675
Bram Moolenaarda440d22016-01-16 21:27:23 +01004676#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4677/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004678 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004679 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004680 * If "withattr" is TRUE, retrieve the names and their attributes.
4681 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004682 * Return OK for success, FAIL for failure.
4683 */
4684 int
4685readdir_core(
4686 garray_T *gap,
4687 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004688 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004689 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004690 int (*checkitem)(void *context, void *item),
4691 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004692{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004693 int failed = FALSE;
4694 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004695# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004696 char_u *buf;
4697 int ok;
4698 HANDLE hFind = INVALID_HANDLE_VALUE;
4699 WIN32_FIND_DATAW wfd;
4700 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004701# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004702 DIR *dirp;
4703 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004704# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004705
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004706 ga_init2(gap, (int)sizeof(void *), 20);
4707
4708# ifdef FEAT_EVAL
4709# define FREE_ITEM(item) do { \
4710 if (withattr) \
4711 dict_unref((dict_T*)item); \
4712 else \
4713 vim_free(item); \
4714 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004715
4716 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004717# else
4718# define FREE_ITEM(item) vim_free(item)
4719# endif
4720
4721# ifdef MSWIN
4722 buf = alloc(MAXPATHL);
4723 if (buf == NULL)
4724 return FAIL;
4725 STRNCPY(buf, path, MAXPATHL-5);
4726 p = buf + STRLEN(buf);
4727 MB_PTR_BACK(buf, p);
4728 if (*p == '\\' || *p == '/')
4729 *p = NUL;
4730 STRCAT(p, "\\*");
4731
4732 wn = enc_to_utf16(buf, NULL);
4733 if (wn != NULL)
4734 hFind = FindFirstFileW(wn, &wfd);
4735 ok = (hFind != INVALID_HANDLE_VALUE);
4736 if (!ok)
4737 {
4738 failed = TRUE;
4739 smsg(_(e_notopen), path);
4740 }
4741 else
4742 {
4743 while (ok)
4744 {
4745 int ignore;
4746 void *item;
4747 WCHAR *wp;
4748
4749 wp = wfd.cFileName;
4750 ignore = wp[0] == L'.' &&
4751 (wp[1] == NUL ||
4752 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004753 if (ignore)
4754 {
4755 ok = FindNextFileW(hFind, &wfd);
4756 continue;
4757 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004758# ifdef FEAT_EVAL
4759 if (withattr)
4760 item = (void*)create_readdirex_item(&wfd);
4761 else
4762# endif
4763 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4764 if (item == NULL)
4765 {
4766 failed = TRUE;
4767 break;
4768 }
4769
4770 if (!ignore && checkitem != NULL)
4771 {
4772 int r = checkitem(context, item);
4773
4774 if (r < 0)
4775 {
4776 FREE_ITEM(item);
4777 break;
4778 }
4779 if (r == 0)
4780 ignore = TRUE;
4781 }
4782
4783 if (!ignore)
4784 {
4785 if (ga_grow(gap, 1) == OK)
4786 ((void**)gap->ga_data)[gap->ga_len++] = item;
4787 else
4788 {
4789 failed = TRUE;
4790 FREE_ITEM(item);
4791 break;
4792 }
4793 }
4794 else
4795 FREE_ITEM(item);
4796
4797 ok = FindNextFileW(hFind, &wfd);
4798 }
4799 FindClose(hFind);
4800 }
4801
4802 vim_free(buf);
4803 vim_free(wn);
4804# else // MSWIN
4805 dirp = opendir((char *)path);
4806 if (dirp == NULL)
4807 {
4808 failed = TRUE;
4809 smsg(_(e_notopen), path);
4810 }
4811 else
4812 {
4813 for (;;)
4814 {
4815 int ignore;
4816 void *item;
4817
4818 dp = readdir(dirp);
4819 if (dp == NULL)
4820 break;
4821 p = (char_u *)dp->d_name;
4822
4823 ignore = p[0] == '.' &&
4824 (p[1] == NUL ||
4825 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004826 if (ignore)
4827 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004828# ifdef FEAT_EVAL
4829 if (withattr)
4830 item = (void*)create_readdirex_item(path, p);
4831 else
4832# endif
4833 item = (void*)vim_strsave(p);
4834 if (item == NULL)
4835 {
4836 failed = TRUE;
4837 break;
4838 }
4839
4840 if (!ignore && checkitem != NULL)
4841 {
4842 int r = checkitem(context, item);
4843
4844 if (r < 0)
4845 {
4846 FREE_ITEM(item);
4847 break;
4848 }
4849 if (r == 0)
4850 ignore = TRUE;
4851 }
4852
4853 if (!ignore)
4854 {
4855 if (ga_grow(gap, 1) == OK)
4856 ((void**)gap->ga_data)[gap->ga_len++] = item;
4857 else
4858 {
4859 failed = TRUE;
4860 FREE_ITEM(item);
4861 break;
4862 }
4863 }
4864 else
4865 FREE_ITEM(item);
4866 }
4867
4868 closedir(dirp);
4869 }
4870# endif // MSWIN
4871
4872# undef FREE_ITEM
4873
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004874 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004875 {
4876# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004877 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004878 if (withattr)
4879 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4880 compare_readdirex_item);
4881 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004882 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4883 compare_readdir_item);
4884# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004885 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004886# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004887 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004888
4889 return failed ? FAIL : OK;
4890}
4891
4892/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004893 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004894 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004895 */
4896 int
4897delete_recursive(char_u *name)
4898{
4899 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004900 int i;
4901 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004902 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004903
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004904 // A symbolic link to a directory itself is deleted, not the directory it
4905 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004906 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004907# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004908 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004909# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004910 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004911# endif
4912 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004913 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004914 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004915 if (exp == NULL)
4916 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004917 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004918 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004919 for (i = 0; i < ga.ga_len; ++i)
4920 {
4921 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4922 ((char_u **)ga.ga_data)[i]);
4923 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004924 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004925 }
4926 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004927 }
4928 else
4929 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004930 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004931 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004932 }
4933 else
4934 result = mch_remove(name) == 0 ? 0 : -1;
4935
4936 return result;
4937}
4938#endif
4939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004941static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004943# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4944/*
4945 * Open temporary directory and take file lock to prevent
4946 * to be auto-cleaned.
4947 */
4948 static void
4949vim_opentempdir(void)
4950{
4951 DIR *dp = NULL;
4952
4953 if (vim_tempdir_dp != NULL)
4954 return;
4955
4956 dp = opendir((const char*)vim_tempdir);
4957
4958 if (dp != NULL)
4959 {
4960 vim_tempdir_dp = dp;
4961 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4962 }
4963}
4964
4965/*
4966 * Close temporary directory - it automatically release file lock.
4967 */
4968 static void
4969vim_closetempdir(void)
4970{
4971 if (vim_tempdir_dp != NULL)
4972 {
4973 closedir(vim_tempdir_dp);
4974 vim_tempdir_dp = NULL;
4975 }
4976}
4977# endif
4978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979/*
4980 * Delete the temp directory and all files it contains.
4981 */
4982 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (vim_tempdir != NULL)
4986 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004987# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4988 vim_closetempdir();
4989# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004990 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004991 gettail(vim_tempdir)[-1] = NUL;
4992 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004993 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004998 * Directory "tempdir" was created. Expand this name to a full path and put
4999 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5000 * "tempdir" must be no longer than MAXPATHL.
5001 */
5002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005003vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005004{
5005 char_u *buf;
5006
Bram Moolenaar964b3742019-05-24 18:54:09 +02005007 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005008 if (buf != NULL)
5009 {
5010 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5011 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005012 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005013 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005014# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5015 vim_opentempdir();
5016# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005017 vim_free(buf);
5018 }
5019}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005020#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005021
5022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 * vim_tempname(): Return a unique name that can be used for a temp file.
5024 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005025 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5026 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 *
5028 * The returned pointer is to allocated memory.
5029 * The returned pointer is NULL if no valid name was found.
5030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005032vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
5036#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005037 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005038#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005039 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#else
5041 char_u itmp[TEMPNAMELEN];
5042#endif
5043
5044#ifdef TEMPDIRNAMES
5045 static char *(tempdirs[]) = {TEMPDIRNAMES};
5046 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005048 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049# endif
5050
5051 /*
5052 * This will create a directory for private use by this instance of Vim.
5053 * This is done once, and the same directory is used for all temp files.
5054 * This method avoids security problems because of symlink attacks et al.
5055 * It's also a bit faster, because we only need to check for an existing
5056 * file when creating the directory and not for each temp file.
5057 */
5058 if (vim_tempdir == NULL)
5059 {
5060 /*
5061 * Try the entries in TEMPDIRNAMES to create the temp directory.
5062 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005063 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005065# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005066 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005067 long nr;
5068 long off;
5069# endif
5070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 // Expand $TMP, leave room for "/v1100000/999999999".
5072 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005074 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005076 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005077 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
Bram Moolenaareaf03392009-11-17 11:08:52 +00005079# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005080 {
5081# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // Make sure the umask doesn't remove the executable bit.
5083 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005084 mode_t umask_save = umask(077);
5085# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005086 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005087 STRCAT(itmp, "vXXXXXX");
5088 if (mkdtemp((char *)itmp) != NULL)
5089 vim_settempdir(itmp);
5090# if defined(UNIX) || defined(VMS)
5091 (void)umask(umask_save);
5092# endif
5093 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005094# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005095 // Get an arbitrary number of up to 6 digits. When it's
5096 // unlikely that it already exists it will be faster,
5097 // otherwise it doesn't matter. The use of mkdir() avoids any
5098 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005100 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005102 // Try up to 10000 different values until we find a name that
5103 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 for (off = 0; off < 10000L; ++off)
5105 {
5106 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005107# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaareaf03392009-11-17 11:08:52 +00005111 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5112# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 // If mkdir() does not set errno to EEXIST, check for
5114 // existing file here. There is a race condition then,
5115 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (mch_stat((char *)itmp, &st) >= 0)
5117 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005118# endif
5119# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005120 // Make sure the umask doesn't remove the executable bit.
5121 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005125# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (r == 0)
5129 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005130 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 break;
5132 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005133# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005134 // If the mkdir() didn't fail because the file/dir exists,
5135 // we probably can't create any dir here, try another
5136 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005141# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (vim_tempdir != NULL)
5143 break;
5144 }
5145 }
5146 }
5147
5148 if (vim_tempdir != NULL)
5149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005150 // There is no need to check if the file exists, because we own the
5151 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5153 return vim_strsave(itmp);
5154 }
5155
5156 return NULL;
5157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005158#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
Bram Moolenaar4f974752019-02-17 17:44:42 +01005160# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005161 WCHAR wszTempFile[_MAX_PATH + 1];
5162 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 char_u *retval;
5164 char_u *p;
5165
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005166 wcscpy(itmp, L"");
5167 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005168 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005169 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
5170 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005171 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005172 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005173 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005174 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005176 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005177 // GetTempFileName() will create the file, we don't want that
5178 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005180 // Backslashes in a temp file name cause problems when filtering with
5181 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5182 // didn't set 'shellslash'.
5183 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (*p_shcf == '-' || p_ssl)
5185 for (p = retval; *p; ++p)
5186 if (*p == '\\')
5187 *p = '/';
5188 return retval;
5189
Bram Moolenaar4f974752019-02-17 17:44:42 +01005190# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005193 char_u *p;
5194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005195 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005196 p = tmpnam((char *)itmp);
5197 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 return NULL;
5199# else
5200 char_u *p;
5201
5202# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005203 // mktemp() is not working on VMS. It seems to be
5204 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 sprintf((char *)itmp, "VIM%c", extra_char);
5206 p = (char_u *)tempnam("tmp:", (char *)itmp);
5207 if (p != NULL)
5208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209 // VMS will use '.LIS' if we don't explicitly specify an extension,
5210 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 STRCPY(itmp, p);
5212 STRCAT(itmp, ".txt");
5213 free(p);
5214 }
5215 else
5216 return NULL;
5217# else
5218 STRCPY(itmp, TEMPNAME);
5219 if ((p = vim_strchr(itmp, '?')) != NULL)
5220 *p = extra_char;
5221 if (mktemp((char *)itmp) == NULL)
5222 return NULL;
5223# endif
5224# endif
5225
5226 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005227# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229}
5230
5231#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5232/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005233 * Convert all backslashes in fname to forward slashes in-place, unless when
5234 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 */
5236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005237forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 char_u *p;
5240
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005241 if (path_with_url(fname))
5242 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005244 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005245 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005247 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 *p = '/';
5249}
5250#endif
5251
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005252/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005253 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5254 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5255 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 * Used for autocommands and 'wildignore'.
5257 * Returns TRUE if there is a match, FALSE otherwise.
5258 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005261 char_u *pattern, // pattern to match with
5262 regprog_T **prog, // pre-compiled regprog or NULL
5263 char_u *fname, // full path of file name
5264 char_u *sfname, // short file name or NULL
5265 char_u *tail, // tail of path
5266 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 regmatch_T regmatch;
5269 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005271 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005272 if (prog != NULL)
5273 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005275 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Try for a match with the pattern with:
5279 * 1. the full file name, when the pattern has a '/'.
5280 * 2. the short file name, when the pattern has a '/'.
5281 * 3. the tail of the file name, when the pattern has no '/'.
5282 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005283 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 && ((allow_dirs
5285 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5286 || (sfname != NULL
5287 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005288 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 result = TRUE;
5290
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005291 if (prog != NULL)
5292 *prog = regmatch.regprog;
5293 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005294 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 return result;
5296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
5298#if defined(FEAT_WILDIGN) || defined(PROTO)
5299/*
5300 * Return TRUE if a file matches with a pattern in "list".
5301 * "list" is a comma-separated list of patterns, like 'wildignore'.
5302 * "sfname" is the short file name or NULL, "ffname" the long file name.
5303 */
5304 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005305match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 char_u buf[100];
5308 char_u *tail;
5309 char_u *regpat;
5310 char allow_dirs;
5311 int match;
5312 char_u *p;
5313
5314 tail = gettail(sfname);
5315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005316 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 p = list;
5318 while (*p)
5319 {
5320 copy_option_part(&p, buf, 100, ",");
5321 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5322 if (regpat == NULL)
5323 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005324 match = match_file_pat(regpat, NULL, ffname, sfname,
5325 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 vim_free(regpat);
5327 if (match)
5328 return TRUE;
5329 }
5330 return FALSE;
5331}
5332#endif
5333
5334/*
5335 * Convert the given pattern "pat" which has shell style wildcards in it, into
5336 * a regular expression, and return the result in allocated memory. If there
5337 * is a directory path separator to be matched, then TRUE is put in
5338 * allow_dirs, otherwise FALSE is put there -- webb.
5339 * Handle backslashes before special characters, like "\*" and "\ ".
5340 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 * Returns NULL when out of memory.
5342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005344file_pat_to_reg_pat(
5345 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005346 char_u *pat_end, // first char after pattern or NULL
5347 char *allow_dirs, // Result passed back out in here
5348 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005350 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 char_u *endp;
5352 char_u *reg_pat;
5353 char_u *p;
5354 int i;
5355 int nested = 0;
5356 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 if (allow_dirs != NULL)
5359 *allow_dirs = FALSE;
5360 if (pat_end == NULL)
5361 pat_end = pat + STRLEN(pat);
5362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 for (p = pat; p < pat_end; p++)
5364 {
5365 switch (*p)
5366 {
5367 case '*':
5368 case '.':
5369 case ',':
5370 case '{':
5371 case '}':
5372 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 break;
5375#ifdef BACKSLASH_IN_FILENAME
5376 case '\\':
5377 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005378 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 break;
5380#endif
5381 default:
5382 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005383 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 ++p;
5386 ++size;
5387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 break;
5389 }
5390 }
5391 reg_pat = alloc(size + 1);
5392 if (reg_pat == NULL)
5393 return NULL;
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
5397 if (pat[0] == '*')
5398 while (pat[0] == '*' && pat < pat_end - 1)
5399 pat++;
5400 else
5401 reg_pat[i++] = '^';
5402 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005403 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
5405 while (endp - pat > 0 && *endp == '*')
5406 endp--;
5407 add_dollar = FALSE;
5408 }
5409 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5410 {
5411 switch (*p)
5412 {
5413 case '*':
5414 reg_pat[i++] = '.';
5415 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005416 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005417 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 break;
5419 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 case '~':
5421 reg_pat[i++] = '\\';
5422 reg_pat[i++] = *p;
5423 break;
5424 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 reg_pat[i++] = '.';
5426 break;
5427 case '\\':
5428 if (p[1] == NUL)
5429 break;
5430#ifdef BACKSLASH_IN_FILENAME
5431 if (!no_bslash)
5432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005433 // translate:
5434 // "\x" to "\\x" e.g., "dir\file"
5435 // "\*" to "\\.*" e.g., "dir\*.c"
5436 // "\?" to "\\." e.g., "dir\??.c"
5437 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5439 && p[1] != '+')
5440 {
5441 reg_pat[i++] = '[';
5442 reg_pat[i++] = '\\';
5443 reg_pat[i++] = '/';
5444 reg_pat[i++] = ']';
5445 if (allow_dirs != NULL)
5446 *allow_dirs = TRUE;
5447 break;
5448 }
5449 }
5450#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005451 // Undo escaping from ExpandEscape():
5452 // foo\?bar -> foo?bar
5453 // foo\%bar -> foo%bar
5454 // foo\,bar -> foo,bar
5455 // foo\ bar -> foo bar
5456 // Don't unescape \, * and others that are also special in a
5457 // regexp.
5458 // An escaped { must be unescaped since we use magic not
5459 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 if (*++p == '?'
5461#ifdef BACKSLASH_IN_FILENAME
5462 && no_bslash
5463#endif
5464 )
5465 reg_pat[i++] = '?';
5466 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005467 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005468 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005469 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005470 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5471 {
5472 reg_pat[i++] = '\\';
5473 reg_pat[i++] = '{';
5474 p += 2;
5475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 else
5477 {
5478 if (allow_dirs != NULL && vim_ispathsep(*p)
5479#ifdef BACKSLASH_IN_FILENAME
5480 && (!no_bslash || *p != '\\')
5481#endif
5482 )
5483 *allow_dirs = TRUE;
5484 reg_pat[i++] = '\\';
5485 reg_pat[i++] = *p;
5486 }
5487 break;
5488#ifdef BACKSLASH_IN_FILENAME
5489 case '/':
5490 reg_pat[i++] = '[';
5491 reg_pat[i++] = '\\';
5492 reg_pat[i++] = '/';
5493 reg_pat[i++] = ']';
5494 if (allow_dirs != NULL)
5495 *allow_dirs = TRUE;
5496 break;
5497#endif
5498 case '{':
5499 reg_pat[i++] = '\\';
5500 reg_pat[i++] = '(';
5501 nested++;
5502 break;
5503 case '}':
5504 reg_pat[i++] = '\\';
5505 reg_pat[i++] = ')';
5506 --nested;
5507 break;
5508 case ',':
5509 if (nested)
5510 {
5511 reg_pat[i++] = '\\';
5512 reg_pat[i++] = '|';
5513 }
5514 else
5515 reg_pat[i++] = ',';
5516 break;
5517 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005518 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005520 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 *allow_dirs = TRUE;
5522 reg_pat[i++] = *p;
5523 break;
5524 }
5525 }
5526 if (add_dollar)
5527 reg_pat[i++] = '$';
5528 reg_pat[i] = NUL;
5529 if (nested != 0)
5530 {
5531 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005532 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005534 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005535 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
5537 return reg_pat;
5538}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005539
5540#if defined(EINTR) || defined(PROTO)
5541/*
5542 * Version of read() that retries when interrupted by EINTR (possibly
5543 * by a SIGWINCH).
5544 */
5545 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005547{
5548 long ret;
5549
5550 for (;;)
5551 {
5552 ret = vim_read(fd, buf, bufsize);
5553 if (ret >= 0 || errno != EINTR)
5554 break;
5555 }
5556 return ret;
5557}
5558
5559/*
5560 * Version of write() that retries when interrupted by EINTR (possibly
5561 * by a SIGWINCH).
5562 */
5563 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005565{
5566 long ret = 0;
5567 long wlen;
5568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005569 // Repeat the write() so long it didn't fail, other than being interrupted
5570 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005571 while (ret < (long)bufsize)
5572 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005573 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005574 if (wlen < 0)
5575 {
5576 if (errno != EINTR)
5577 break;
5578 }
5579 else
5580 ret += wlen;
5581 }
5582 return ret;
5583}
5584#endif